System.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
  5. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  8. PURPOSE. See the above copyright notices for more information.
  9. =========================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(System.h)
  12. /* Work-around CMake dependency scanning limitation. This must
  13. duplicate the above list of headers. */
  14. #if 0
  15. # include "System.h.in"
  16. #endif
  17. #include <string.h> /* strlen */
  18. #include <ctype.h> /* isalpha */
  19. #include <stdio.h>
  20. /*
  21. Notes:
  22. Make variable replacements open a can of worms. Sometimes they should
  23. be quoted and sometimes not. Sometimes their replacement values are
  24. already quoted.
  25. VS variables cause problems. In order to pass the referenced value
  26. with spaces the reference must be quoted. If the variable value ends
  27. in a backslash then it will escape the ending quote! In order to make
  28. the ending backslash appear we need this:
  29. "$(InputDir)\"
  30. However if there is not a trailing backslash then this will put a
  31. quote in the value so we need:
  32. "$(InputDir)"
  33. Make variable references are platform specific so we should probably
  34. just NOT quote them and let the listfile author deal with it.
  35. */
  36. /*--------------------------------------------------------------------------*/
  37. static int kwsysSystem_Shell__CharIsWhitespace(char c)
  38. {
  39. return ((c == ' ') || (c == '\t'));
  40. }
  41. /*--------------------------------------------------------------------------*/
  42. static int kwsysSystem_Shell__CharNeedsQuotesOnUnix(char c)
  43. {
  44. return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
  45. (c == '&') || (c == '$') || (c == '(') || (c == ')'));
  46. }
  47. /*--------------------------------------------------------------------------*/
  48. static int kwsysSystem_Shell__CharNeedsQuotes(char c, int isUnix, int flags)
  49. {
  50. /* On Windows the built-in command shell echo never needs quotes. */
  51. if(!isUnix && (flags & kwsysSystem_Shell_Flag_EchoWindows))
  52. {
  53. return 0;
  54. }
  55. /* On all platforms quotes are needed to preserve whitespace. */
  56. if(kwsysSystem_Shell__CharIsWhitespace(c))
  57. {
  58. return 1;
  59. }
  60. if(isUnix)
  61. {
  62. /* On UNIX several special characters need quotes to preserve them. */
  63. if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c))
  64. {
  65. return 1;
  66. }
  67. }
  68. else
  69. {
  70. /* On Windows single-quotes must be escaped in some make
  71. environments, such as in mingw32-make. */
  72. if(flags & kwsysSystem_Shell_Flag_Make)
  73. {
  74. if(c == '\'')
  75. {
  76. return 1;
  77. }
  78. }
  79. }
  80. return 0;
  81. }
  82. /*--------------------------------------------------------------------------*/
  83. static int kwsysSystem_Shell__CharIsMakeVariableName(char c)
  84. {
  85. return c && (c == '_' || isalpha(((int)c)));
  86. }
  87. /*--------------------------------------------------------------------------*/
  88. static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c)
  89. {
  90. while(*c == '$' && *(c+1) == '(')
  91. {
  92. const char* skip = c+2;
  93. while(kwsysSystem_Shell__CharIsMakeVariableName(*skip))
  94. {
  95. ++skip;
  96. }
  97. if(*skip == ')')
  98. {
  99. c = skip+1;
  100. }
  101. else
  102. {
  103. break;
  104. }
  105. }
  106. return c;
  107. }
  108. /*
  109. Allowing make variable replacements opens a can of worms. Sometimes
  110. they should be quoted and sometimes not. Sometimes their replacement
  111. values are already quoted or contain escapes.
  112. Some Visual Studio variables cause problems. In order to pass the
  113. referenced value with spaces the reference must be quoted. If the
  114. variable value ends in a backslash then it will escape the ending
  115. quote! In order to make the ending backslash appear we need this:
  116. "$(InputDir)\"
  117. However if there is not a trailing backslash then this will put a
  118. quote in the value so we need:
  119. "$(InputDir)"
  120. This macro decides whether we quote an argument just because it
  121. contains a make variable reference. This should be replaced with a
  122. flag later when we understand applications of this better.
  123. */
  124. #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
  125. /*--------------------------------------------------------------------------*/
  126. static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
  127. int flags)
  128. {
  129. /* Scan the string for characters that require quoting. */
  130. const char* c;
  131. for(c=in; *c; ++c)
  132. {
  133. /* Look for $(MAKEVAR) syntax if requested. */
  134. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  135. {
  136. #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
  137. const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
  138. if(skip != c)
  139. {
  140. /* We need to quote make variable references to preserve the
  141. string with contents substituted in its place. */
  142. return 1;
  143. }
  144. #else
  145. /* Skip over the make variable references if any are present. */
  146. c = kwsysSystem_Shell__SkipMakeVariables(c);
  147. /* Stop if we have reached the end of the string. */
  148. if(!*c)
  149. {
  150. break;
  151. }
  152. #endif
  153. }
  154. /* Check whether this character needs quotes. */
  155. if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags))
  156. {
  157. return 1;
  158. }
  159. }
  160. return 0;
  161. }
  162. /*--------------------------------------------------------------------------*/
  163. static int kwsysSystem_Shell__GetArgumentSize(const char* in,
  164. int isUnix, int flags)
  165. {
  166. /* Start with the length of the original argument, plus one for
  167. either a terminating null or a separating space. */
  168. int size = (int)strlen(in) + 1;
  169. /* String iterator. */
  170. const char* c;
  171. /* Keep track of how many backslashes have been encountered in a row. */
  172. int windows_backslashes = 0;
  173. /* Scan the string for characters that require escaping or quoting. */
  174. for(c=in; *c; ++c)
  175. {
  176. /* Look for $(MAKEVAR) syntax if requested. */
  177. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  178. {
  179. /* Skip over the make variable references if any are present. */
  180. c = kwsysSystem_Shell__SkipMakeVariables(c);
  181. /* Stop if we have reached the end of the string. */
  182. if(!*c)
  183. {
  184. break;
  185. }
  186. }
  187. /* Check whether this character needs escaping for the shell. */
  188. if(isUnix)
  189. {
  190. /* On Unix a few special characters need escaping even inside a
  191. quoted argument. */
  192. if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
  193. {
  194. /* This character needs a backslash to escape it. */
  195. ++size;
  196. }
  197. }
  198. else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
  199. {
  200. /* On Windows the built-in command shell echo never needs escaping. */
  201. }
  202. else
  203. {
  204. /* On Windows only backslashes and double-quotes need escaping. */
  205. if(*c == '\\')
  206. {
  207. /* Found a backslash. It may need to be escaped later. */
  208. ++windows_backslashes;
  209. }
  210. else if(*c == '"')
  211. {
  212. /* Found a double-quote. We need to escape it and all
  213. immediately preceding backslashes. */
  214. size += windows_backslashes + 1;
  215. windows_backslashes = 0;
  216. }
  217. else
  218. {
  219. /* Found another character. This eliminates the possibility
  220. that any immediately preceding backslashes will be
  221. escaped. */
  222. windows_backslashes = 0;
  223. }
  224. }
  225. /* Check whether this character needs escaping for a make tool. */
  226. if(*c == '$')
  227. {
  228. if(flags & kwsysSystem_Shell_Flag_Make)
  229. {
  230. /* In Makefiles a dollar is written $$ so we need one extra
  231. character. */
  232. ++size;
  233. }
  234. else if(flags & kwsysSystem_Shell_Flag_VSIDE)
  235. {
  236. /* In a VS IDE a dollar is written "$" so we need two extra
  237. characters. */
  238. size += 2;
  239. }
  240. }
  241. else if(*c == '#')
  242. {
  243. if((flags & kwsysSystem_Shell_Flag_Make) &&
  244. (flags & kwsysSystem_Shell_Flag_WatcomWMake))
  245. {
  246. /* In Watcom WMake makefiles a pound is written $# so we need
  247. one extra character. */
  248. ++size;
  249. }
  250. }
  251. }
  252. /* Check whether the argument needs surrounding quotes. */
  253. if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags))
  254. {
  255. /* Surrounding quotes are needed. Allocate space for them. */
  256. size += 2;
  257. /* We must escape all ending backslashes when quoting on windows. */
  258. size += windows_backslashes;
  259. }
  260. return size;
  261. }
  262. /*--------------------------------------------------------------------------*/
  263. static char* kwsysSystem_Shell__GetArgument(const char* in, char* out,
  264. int isUnix, int flags)
  265. {
  266. /* String iterator. */
  267. const char* c;
  268. /* Keep track of how many backslashes have been encountered in a row. */
  269. int windows_backslashes = 0;
  270. /* Whether the argument must be quoted. */
  271. int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags);
  272. if(needQuotes)
  273. {
  274. /* Add the opening quote for this argument. */
  275. *out++ = '"';
  276. }
  277. /* Scan the string for characters that require escaping or quoting. */
  278. for(c=in; *c; ++c)
  279. {
  280. /* Look for $(MAKEVAR) syntax if requested. */
  281. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  282. {
  283. const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
  284. if(skip != c)
  285. {
  286. /* Copy to the end of the make variable references. */
  287. while(c != skip)
  288. {
  289. *out++ = *c++;
  290. }
  291. /* Stop if we have reached the end of the string. */
  292. if(!*c)
  293. {
  294. break;
  295. }
  296. }
  297. }
  298. /* Check whether this character needs escaping for the shell. */
  299. if(isUnix)
  300. {
  301. /* On Unix a few special characters need escaping even inside a
  302. quoted argument. */
  303. if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
  304. {
  305. /* This character needs a backslash to escape it. */
  306. *out++ = '\\';
  307. }
  308. }
  309. else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
  310. {
  311. /* On Windows the built-in command shell echo never needs escaping. */
  312. }
  313. else
  314. {
  315. /* On Windows only backslashes and double-quotes need escaping. */
  316. if(*c == '\\')
  317. {
  318. /* Found a backslash. It may need to be escaped later. */
  319. ++windows_backslashes;
  320. }
  321. else if(*c == '"')
  322. {
  323. /* Found a double-quote. Escape all immediately preceding
  324. backslashes. */
  325. while(windows_backslashes > 0)
  326. {
  327. --windows_backslashes;
  328. *out++ = '\\';
  329. }
  330. /* Add the backslash to escape the double-quote. */
  331. *out++ = '\\';
  332. }
  333. else
  334. {
  335. /* We encountered a normal character. This eliminates any
  336. escaping needed for preceding backslashes. */
  337. windows_backslashes = 0;
  338. }
  339. }
  340. /* Check whether this character needs escaping for a make tool. */
  341. if(*c == '$')
  342. {
  343. if(flags & kwsysSystem_Shell_Flag_Make)
  344. {
  345. /* In Makefiles a dollar is written $$. The make tool will
  346. replace it with just $ before passing it to the shell. */
  347. *out++ = '$';
  348. *out++ = '$';
  349. }
  350. else if(flags & kwsysSystem_Shell_Flag_VSIDE)
  351. {
  352. /* In a VS IDE a dollar is written "$". If this is written in
  353. an un-quoted argument it starts a quoted segment, inserts
  354. the $ and ends the segment. If it is written in a quoted
  355. argument it ends quoting, inserts the $ and restarts
  356. quoting. Either way the $ is isolated from surrounding
  357. text to avoid looking like a variable reference. */
  358. *out++ = '"';
  359. *out++ = '$';
  360. *out++ = '"';
  361. }
  362. else
  363. {
  364. /* Otherwise a dollar is written just $. */
  365. *out++ = '$';
  366. }
  367. }
  368. else if(*c == '#')
  369. {
  370. if((flags & kwsysSystem_Shell_Flag_Make) &&
  371. (flags & kwsysSystem_Shell_Flag_WatcomWMake))
  372. {
  373. /* In Watcom WMake makefiles a pound is written $#. The make
  374. tool will replace it with just # before passing it to the
  375. shell. */
  376. *out++ = '$';
  377. *out++ = '#';
  378. }
  379. else
  380. {
  381. /* Otherwise a pound is written just #. */
  382. *out++ = '#';
  383. }
  384. }
  385. else
  386. {
  387. /* Store this character. */
  388. *out++ = *c;
  389. }
  390. }
  391. if(needQuotes)
  392. {
  393. /* Add enough backslashes to escape any trailing ones. */
  394. while(windows_backslashes > 0)
  395. {
  396. --windows_backslashes;
  397. *out++ = '\\';
  398. }
  399. /* Add the closing quote for this argument. */
  400. *out++ = '"';
  401. }
  402. /* Store a terminating null without incrementing. */
  403. *out = 0;
  404. return out;
  405. }
  406. /*--------------------------------------------------------------------------*/
  407. char* kwsysSystem_Shell_GetArgumentForWindows(const char* in,
  408. char* out,
  409. int flags)
  410. {
  411. return kwsysSystem_Shell__GetArgument(in, out, 0, flags);
  412. }
  413. /*--------------------------------------------------------------------------*/
  414. char* kwsysSystem_Shell_GetArgumentForUnix(const char* in,
  415. char* out,
  416. int flags)
  417. {
  418. return kwsysSystem_Shell__GetArgument(in, out, 1, flags);
  419. }
  420. /*--------------------------------------------------------------------------*/
  421. int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, int flags)
  422. {
  423. return kwsysSystem_Shell__GetArgumentSize(in, 0, flags);
  424. }
  425. /*--------------------------------------------------------------------------*/
  426. int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, int flags)
  427. {
  428. return kwsysSystem_Shell__GetArgumentSize(in, 1, flags);
  429. }