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