System.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 == ';') ||
  45. (c == '&') || (c == '$') || (c == '(') || (c == ')'));
  46. }
  47. /*--------------------------------------------------------------------------*/
  48. static int kwsysSystem_Shell__CharNeedsQuotes(char c, int isUnix, int flags)
  49. {
  50. /* On all platforms quotes are needed to preserve whitespace. */
  51. if(kwsysSystem_Shell__CharIsWhitespace(c))
  52. {
  53. return 1;
  54. }
  55. if(isUnix)
  56. {
  57. /* On UNIX several special characters need quotes to preserve them. */
  58. if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c))
  59. {
  60. return 1;
  61. }
  62. }
  63. else
  64. {
  65. /* On Windows single-quotes must be escaped in some make
  66. environments, such as in mingw32-make. */
  67. if(flags & kwsysSystem_Shell_Flag_Make)
  68. {
  69. if(c == '\'')
  70. {
  71. return 1;
  72. }
  73. }
  74. }
  75. return 0;
  76. }
  77. /*--------------------------------------------------------------------------*/
  78. static int kwsysSystem_Shell__CharIsMakeVariableName(char c)
  79. {
  80. return c && (c == '_' || isalpha(((int)c)));
  81. }
  82. /*--------------------------------------------------------------------------*/
  83. static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c)
  84. {
  85. while(*c == '$' && *(c+1) == '(')
  86. {
  87. const char* skip = c+2;
  88. while(kwsysSystem_Shell__CharIsMakeVariableName(*skip))
  89. {
  90. ++skip;
  91. }
  92. if(*skip == ')')
  93. {
  94. c = skip+1;
  95. }
  96. else
  97. {
  98. break;
  99. }
  100. }
  101. return c;
  102. }
  103. /*
  104. Allowing make variable replacements opens a can of worms. Sometimes
  105. they should be quoted and sometimes not. Sometimes their replacement
  106. values are already quoted or contain escapes.
  107. Some Visual Studio variables cause problems. In order to pass the
  108. referenced value with spaces the reference must be quoted. If the
  109. variable value ends in a backslash then it will escape the ending
  110. quote! In order to make the ending backslash appear we need this:
  111. "$(InputDir)\"
  112. However if there is not a trailing backslash then this will put a
  113. quote in the value so we need:
  114. "$(InputDir)"
  115. This macro decides whether we quote an argument just because it
  116. contains a make variable reference. This should be replaced with a
  117. flag later when we understand applications of this better.
  118. */
  119. #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
  120. /*--------------------------------------------------------------------------*/
  121. static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
  122. int flags)
  123. {
  124. /* Scan the string for characters that require quoting. */
  125. const char* c;
  126. for(c=in; *c; ++c)
  127. {
  128. /* Look for $(MAKEVAR) syntax if requested. */
  129. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  130. {
  131. #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
  132. const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
  133. if(skip != c)
  134. {
  135. /* We need to quote make variable references to preserve the
  136. string with contents substituted in its place. */
  137. return 1;
  138. }
  139. #else
  140. /* Skip over the make variable references if any are present. */
  141. c = kwsysSystem_Shell__SkipMakeVariables(c);
  142. /* Stop if we have reached the end of the string. */
  143. if(!*c)
  144. {
  145. break;
  146. }
  147. #endif
  148. }
  149. /* Check whether this character needs quotes. */
  150. if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags))
  151. {
  152. return 1;
  153. }
  154. }
  155. return 0;
  156. }
  157. /*--------------------------------------------------------------------------*/
  158. static int kwsysSystem_Shell__GetArgumentSize(const char* in,
  159. int isUnix, int flags)
  160. {
  161. /* Start with the length of the original argument, plus one for
  162. either a terminating null or a separating space. */
  163. int size = (int)strlen(in) + 1;
  164. /* String iterator. */
  165. const char* c;
  166. /* Keep track of how many backslashes have been encountered in a row. */
  167. int windows_backslashes = 0;
  168. /* Scan the string for characters that require escaping or quoting. */
  169. for(c=in; *c; ++c)
  170. {
  171. /* Look for $(MAKEVAR) syntax if requested. */
  172. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  173. {
  174. /* Skip over the make variable references if any are present. */
  175. c = kwsysSystem_Shell__SkipMakeVariables(c);
  176. /* Stop if we have reached the end of the string. */
  177. if(!*c)
  178. {
  179. break;
  180. }
  181. }
  182. /* Check whether this character needs escaping. */
  183. if(isUnix)
  184. {
  185. /* On Unix a few special characters need escaping even inside a
  186. quoted argument. */
  187. if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
  188. {
  189. /* This character needs a backslash to escape it. */
  190. ++size;
  191. }
  192. }
  193. else
  194. {
  195. /* On Windows only backslashes and double-quotes need escaping. */
  196. if(*c == '\\')
  197. {
  198. /* Found a backslash. It may need to be escaped later. */
  199. ++windows_backslashes;
  200. }
  201. else if(*c == '"')
  202. {
  203. /* Found a double-quote. We need to escape it and all
  204. immediately preceding backslashes. */
  205. size += windows_backslashes + 1;
  206. windows_backslashes = 0;
  207. }
  208. else
  209. {
  210. /* Found another character. This eliminates the possibility
  211. that any immediately preceding backslashes will be
  212. escaped. */
  213. windows_backslashes = 0;
  214. }
  215. }
  216. /* The dollar sign needs special handling in some environments. */
  217. if(*c == '$')
  218. {
  219. if(flags & kwsysSystem_Shell_Flag_Make)
  220. {
  221. /* In Makefiles a dollar is written $$ so we need one extra
  222. character. */
  223. ++size;
  224. }
  225. else if(flags & kwsysSystem_Shell_Flag_VSIDE)
  226. {
  227. /* In a VS IDE a dollar is written "$" so we need two extra
  228. characters. */
  229. size += 2;
  230. }
  231. }
  232. }
  233. /* Check whether the argument needs surrounding quotes. */
  234. if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags))
  235. {
  236. /* Surrounding quotes are needed. Allocate space for them. */
  237. size += 2;
  238. /* We must escape all ending backslashes when quoting on windows. */
  239. size += windows_backslashes;
  240. }
  241. return size;
  242. }
  243. /*--------------------------------------------------------------------------*/
  244. static char* kwsysSystem_Shell__GetArgument(const char* in, char* out,
  245. int isUnix, int flags)
  246. {
  247. /* String iterator. */
  248. const char* c;
  249. /* Keep track of how many backslashes have been encountered in a row. */
  250. int windows_backslashes = 0;
  251. /* Whether the argument must be quoted. */
  252. int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags);
  253. if(needQuotes)
  254. {
  255. /* Add the opening quote for this argument. */
  256. *out++ = '"';
  257. }
  258. /* Scan the string for characters that require escaping or quoting. */
  259. for(c=in; *c; ++c)
  260. {
  261. /* Look for $(MAKEVAR) syntax if requested. */
  262. if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
  263. {
  264. const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
  265. if(skip != c)
  266. {
  267. /* Copy to the end of the make variable references. */
  268. while(c != skip)
  269. {
  270. *out++ = *c++;
  271. }
  272. /* Stop if we have reached the end of the string. */
  273. if(!*c)
  274. {
  275. break;
  276. }
  277. }
  278. }
  279. /* Check whether this character needs escaping. */
  280. if(isUnix)
  281. {
  282. /* On Unix a few special characters need escaping even inside a
  283. quoted argument. */
  284. if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
  285. {
  286. /* This character needs a backslash to escape it. */
  287. *out++ = '\\';
  288. }
  289. }
  290. else
  291. {
  292. /* On Windows only backslashes and double-quotes need escaping. */
  293. if(*c == '\\')
  294. {
  295. /* Found a backslash. It may need to be escaped later. */
  296. ++windows_backslashes;
  297. }
  298. else if(*c == '"')
  299. {
  300. /* Found a double-quote. Escape all immediately preceding
  301. backslashes. */
  302. while(windows_backslashes > 0)
  303. {
  304. --windows_backslashes;
  305. *out++ = '\\';
  306. }
  307. /* Add the backslash to escape the double-quote. */
  308. *out++ = '\\';
  309. }
  310. else
  311. {
  312. /* We encountered a normal character. This eliminates any
  313. escaping needed for preceding backslashes. */
  314. windows_backslashes = 0;
  315. }
  316. }
  317. /* The dollar sign needs special handling in some environments. */
  318. if(*c == '$')
  319. {
  320. if(flags & kwsysSystem_Shell_Flag_Make)
  321. {
  322. /* In Makefiles a dollar is written $$. The make tool will
  323. replace it with just $ before passing it to the shell. */
  324. *out++ = '$';
  325. *out++ = '$';
  326. }
  327. else if(flags & kwsysSystem_Shell_Flag_VSIDE)
  328. {
  329. /* In a VS IDE a dollar is written "$". If this is written in
  330. an un-quoted argument it starts a quoted segment, inserts
  331. the $ and ends the segment. If it is written in a quoted
  332. argument it ends quoting, inserts the $ and restarts
  333. quoting. Either way the $ is isolated from surrounding
  334. text to avoid looking like a variable reference. */
  335. *out++ = '"';
  336. *out++ = '$';
  337. *out++ = '"';
  338. }
  339. else
  340. {
  341. /* Otherwise a dollar is written just $. */
  342. *out++ = '$';
  343. }
  344. }
  345. else
  346. {
  347. /* Store this character. */
  348. *out++ = *c;
  349. }
  350. }
  351. if(needQuotes)
  352. {
  353. /* Add enough backslashes to escape any trailing ones. */
  354. while(windows_backslashes > 0)
  355. {
  356. --windows_backslashes;
  357. *out++ = '\\';
  358. }
  359. /* Add the closing quote for this argument. */
  360. *out++ = '"';
  361. }
  362. /* Store a terminating null without incrementing. */
  363. *out = 0;
  364. return out;
  365. }
  366. /*--------------------------------------------------------------------------*/
  367. char* kwsysSystem_Shell_GetArgumentForWindows(const char* in,
  368. char* out,
  369. int flags)
  370. {
  371. return kwsysSystem_Shell__GetArgument(in, out, 0, flags);
  372. }
  373. /*--------------------------------------------------------------------------*/
  374. char* kwsysSystem_Shell_GetArgumentForUnix(const char* in,
  375. char* out,
  376. int flags)
  377. {
  378. return kwsysSystem_Shell__GetArgument(in, out, 1, flags);
  379. }
  380. /*--------------------------------------------------------------------------*/
  381. int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, int flags)
  382. {
  383. return kwsysSystem_Shell__GetArgumentSize(in, 0, flags);
  384. }
  385. /*--------------------------------------------------------------------------*/
  386. int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, int flags)
  387. {
  388. return kwsysSystem_Shell__GetArgumentSize(in, 1, flags);
  389. }