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