sbsv2/raptor/util/talon/chomp.c
author jjkang
Fri, 25 Jun 2010 19:56:45 +0800
changeset 604 b33dd54aaa52
parent 602 3145852acc89
permissions -rw-r--r--
add cpptoolsplat
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
602
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     1
/*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     3
* All rights reserved.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     4
* This component and the accompanying materials are made available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     6
* which accompanies this distribution, and is available
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     8
*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
     9
* Initial Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    11
*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    12
* Contributors:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    13
*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    14
* Description: 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    15
*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    16
*/
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    17
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    18
/*
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    19
   Get rid of the path to talon from a commandline string on windows find the 
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    20
   -c (if it's there) and step past it to after the quote on the first command:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    21
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    22
   "g:\program files\talon\talon.exe" -c "gcc -c . . ."
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    23
                                          ^------ Returns a pointer to here
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    24
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    25
   Take care of the possibilty that there might be spaces in the command
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    26
   if it is quoted.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    27
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    28
   A state-machine is flexible but not all that easy to write.  Should investigate
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    29
   the possiblity of using the Ragel state machine generator perhaps.
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    30
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    31
*/
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    32
#define CH_START 0 	/* start state */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    33
#define CH_PRE 1	/* spaces before executable name */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    34
#define CH_EXQUOTE 2    /* part of the executable name, outside quotes */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    35
#define CH_INQUOTE 3	/* part of the executable name, in a quoted region */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    36
#define CH_POST 4	/* spaces after executable name */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    37
#define CH_MINUS 5	/* start of -c option */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    38
#define CH_C 6		/* end of -c option */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    39
#define CH_PRECOMMAND 7 /* spaces before shell commands */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    40
#define CH_COMMAND 8	/* first character of shell command */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    41
#define CH_ERR 9	/* Error! */
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    42
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    43
#include "log.h"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    44
#include "chomp.h"
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    45
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    46
char * chompCommand(char command[])
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    47
{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    48
	char *result = command;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    49
	int state = CH_START;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    50
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    51
	while (state != CH_COMMAND && state != CH_ERR)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    52
	{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    53
		DEBUG(("startstate: %d, char %c ",state, *result));
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    54
		switch (*result)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    55
		{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    56
			case ' ':
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    57
				switch (state)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    58
				{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    59
					case CH_START:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    60
					case CH_PRE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    61
						state = CH_PRE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    62
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    63
					case CH_EXQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    64
						state = CH_POST;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    65
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    66
					case CH_INQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    67
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    68
					case CH_POST:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    69
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    70
					case CH_MINUS:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    71
						state = CH_C;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    72
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    73
					case CH_C:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    74
						state = CH_PRECOMMAND;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    75
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    76
					case CH_PRECOMMAND:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    77
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    78
					default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    79
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    80
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    81
				}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    82
			break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    83
			case 'c':
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    84
				switch (state)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    85
				{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    86
					case CH_START:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    87
					case CH_PRE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    88
						state = CH_EXQUOTE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    89
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    90
					case CH_EXQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    91
					case CH_INQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    92
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    93
					case CH_POST:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    94
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    95
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    96
					case CH_MINUS:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    97
						state = CH_C;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    98
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
    99
					case CH_C:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   100
					case CH_PRECOMMAND:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   101
					default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   102
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   103
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   104
				}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   105
			break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   106
			case '-':
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   107
				switch (state)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   108
				{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   109
					case CH_START:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   110
					case CH_PRE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   111
						state = CH_EXQUOTE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   112
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   113
					case CH_EXQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   114
					case CH_INQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   115
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   116
					case CH_POST:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   117
						state = CH_MINUS;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   118
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   119
					case CH_MINUS:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   120
					case CH_C:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   121
					case CH_PRECOMMAND:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   122
					default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   123
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   124
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   125
				}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   126
			break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   127
			case '"':
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   128
				switch (state)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   129
				{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   130
					case CH_START:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   131
					case CH_PRE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   132
					case CH_EXQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   133
						state = CH_INQUOTE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   134
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   135
					case CH_INQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   136
						state = CH_EXQUOTE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   137
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   138
					case CH_POST:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   139
					case CH_MINUS:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   140
					case CH_C:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   141
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   142
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   143
					case CH_PRECOMMAND:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   144
						state = CH_COMMAND;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   145
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   146
					default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   147
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   148
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   149
				}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   150
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   151
			break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   152
			default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   153
				switch (state)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   154
				{
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   155
					case CH_START:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   156
					case CH_PRE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   157
						state = CH_EXQUOTE;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   158
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   159
					case CH_INQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   160
					case CH_EXQUOTE:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   161
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   162
					default:
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   163
						state = CH_ERR;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   164
						break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   165
				}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   166
			break;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   167
		}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   168
		DEBUG(("endstate: %d\n",state));
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   169
		result ++;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   170
		
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   171
	}
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   172
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   173
	if (state == CH_ERR)
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   174
		return (char *)0;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   175
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   176
	return result;
3145852acc89 add releasing to new structure
jjkang
parents:
diff changeset
   177
}