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