chomp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Get rid of the path to talon from a commandline string on windows find the
  19. -c (if it's there) and step past it to after the quote on the first command:
  20. "g:\program files\talon\talon.exe" -c "gcc -c . . ."
  21. ^------ Returns a pointer to here
  22. Take care of the possibilty that there might be spaces in the command
  23. if it is quoted.
  24. A state-machine is flexible but not all that easy to write. Should investigate
  25. the possiblity of using the Ragel state machine generator perhaps.
  26. */
  27. #define CH_START 0 /* start state */
  28. #define CH_PRE 1 /* spaces before executable name */
  29. #define CH_EXQUOTE 2 /* part of the executable name, outside quotes */
  30. #define CH_INQUOTE 3 /* part of the executable name, in a quoted region */
  31. #define CH_POST 4 /* spaces after executable name */
  32. #define CH_MINUS 5 /* start of -c option */
  33. #define CH_C 6 /* end of -c option */
  34. #define CH_PRECOMMAND 7 /* spaces before shell commands */
  35. #define CH_COMMAND 8 /* first character of shell command */
  36. #define CH_ERR 9 /* Error! */
  37. #include "log.h"
  38. #include "chomp.h"
  39. char * chompCommand(char command[])
  40. {
  41. char *result = command;
  42. int state = CH_START;
  43. while (state != CH_COMMAND && state != CH_ERR)
  44. {
  45. DEBUG(("startstate: %d, char %c ",state, *result));
  46. switch (*result)
  47. {
  48. case ' ':
  49. switch (state)
  50. {
  51. case CH_START:
  52. case CH_PRE:
  53. state = CH_PRE;
  54. break;
  55. case CH_EXQUOTE:
  56. state = CH_POST;
  57. break;
  58. case CH_INQUOTE:
  59. break;
  60. case CH_POST:
  61. break;
  62. case CH_MINUS:
  63. state = CH_C;
  64. break;
  65. case CH_C:
  66. state = CH_PRECOMMAND;
  67. break;
  68. case CH_PRECOMMAND:
  69. break;
  70. default:
  71. state = CH_ERR;
  72. break;
  73. }
  74. break;
  75. case 'c':
  76. switch (state)
  77. {
  78. case CH_START:
  79. case CH_PRE:
  80. state = CH_EXQUOTE;
  81. break;
  82. case CH_EXQUOTE:
  83. case CH_INQUOTE:
  84. break;
  85. case CH_POST:
  86. state = CH_ERR;
  87. break;
  88. case CH_MINUS:
  89. state = CH_C;
  90. break;
  91. case CH_C:
  92. case CH_PRECOMMAND:
  93. default:
  94. state = CH_ERR;
  95. break;
  96. }
  97. break;
  98. case '-':
  99. switch (state)
  100. {
  101. case CH_START:
  102. case CH_PRE:
  103. state = CH_EXQUOTE;
  104. break;
  105. case CH_EXQUOTE:
  106. case CH_INQUOTE:
  107. break;
  108. case CH_POST:
  109. state = CH_MINUS;
  110. break;
  111. case CH_MINUS:
  112. case CH_C:
  113. case CH_PRECOMMAND:
  114. default:
  115. state = CH_ERR;
  116. break;
  117. }
  118. break;
  119. case '"':
  120. switch (state)
  121. {
  122. case CH_START:
  123. case CH_PRE:
  124. case CH_EXQUOTE:
  125. state = CH_INQUOTE;
  126. break;
  127. case CH_INQUOTE:
  128. state = CH_EXQUOTE;
  129. break;
  130. case CH_POST:
  131. case CH_MINUS:
  132. case CH_C:
  133. state = CH_ERR;
  134. break;
  135. case CH_PRECOMMAND:
  136. state = CH_COMMAND;
  137. break;
  138. default:
  139. state = CH_ERR;
  140. break;
  141. }
  142. break;
  143. default:
  144. switch (state)
  145. {
  146. case CH_START:
  147. case CH_PRE:
  148. state = CH_EXQUOTE;
  149. break;
  150. case CH_INQUOTE:
  151. case CH_EXQUOTE:
  152. break;
  153. default:
  154. state = CH_ERR;
  155. break;
  156. }
  157. break;
  158. }
  159. DEBUG(("endstate: %d\n",state));
  160. result ++;
  161. }
  162. if (state == CH_ERR)
  163. return (char *)0;
  164. return result;
  165. }