Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / System.c
blob831f3f013c79390e198fd53e142219c38c09ab1c
1 /*=========================================================================
3 Program: KWSys - Kitware System Library
4 Module: $RCSfile: System.c,v $
6 Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
13 =========================================================================*/
14 #include "kwsysPrivate.h"
15 #include KWSYS_HEADER(System.h)
17 /* Work-around CMake dependency scanning limitation. This must
18 duplicate the above list of headers. */
19 #if 0
20 # include "System.h.in"
21 #endif
23 #include <string.h> /* strlen */
24 #include <ctype.h> /* isalpha */
26 #include <stdio.h>
30 Notes:
32 Make variable replacements open a can of worms. Sometimes they should
33 be quoted and sometimes not. Sometimes their replacement values are
34 already quoted.
36 VS variables cause problems. In order to pass the referenced value
37 with spaces the reference must be quoted. If the variable value ends
38 in a backslash then it will escape the ending quote! In order to make
39 the ending backslash appear we need this:
41 "$(InputDir)\"
43 However if there is not a trailing backslash then this will put a
44 quote in the value so we need:
46 "$(InputDir)"
48 Make variable references are platform specific so we should probably
49 just NOT quote them and let the listfile author deal with it.
54 TODO: For windows echo:
56 To display a pipe (|) or redirection character (< or >) when using the
57 echo command, use a caret character immediately before the pipe or
58 redirection character (for example, ^>, ^<, or ^| ). If you need to
59 use the caret character itself (^), use two in a row (^^).
62 /*--------------------------------------------------------------------------*/
63 static int kwsysSystem_Shell__CharIsWhitespace(char c)
65 return ((c == ' ') || (c == '\t'));
68 /*--------------------------------------------------------------------------*/
69 static int kwsysSystem_Shell__CharNeedsQuotesOnUnix(char c)
71 return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
72 (c == '&') || (c == '$') || (c == '(') || (c == ')') ||
73 (c == '~') || (c == '<') || (c == '>') || (c == '|') ||
74 (c == '*') || (c == '^') || (c == '\\'));
77 /*--------------------------------------------------------------------------*/
78 static int kwsysSystem_Shell__CharNeedsQuotesOnWindows(char c)
80 return ((c == '\'') || (c == '#') || (c == '&') ||
81 (c == '<') || (c == '>') || (c == '|') || (c == '^'));
84 /*--------------------------------------------------------------------------*/
85 static int kwsysSystem_Shell__CharNeedsQuotes(char c, int isUnix, int flags)
87 /* On Windows the built-in command shell echo never needs quotes. */
88 if(!isUnix && (flags & kwsysSystem_Shell_Flag_EchoWindows))
90 return 0;
93 /* On all platforms quotes are needed to preserve whitespace. */
94 if(kwsysSystem_Shell__CharIsWhitespace(c))
96 return 1;
99 if(isUnix)
101 /* On UNIX several special characters need quotes to preserve them. */
102 if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c))
104 return 1;
107 else
109 /* On Windows several special characters need quotes to preserve them. */
110 if(kwsysSystem_Shell__CharNeedsQuotesOnWindows(c))
112 return 1;
115 return 0;
118 /*--------------------------------------------------------------------------*/
119 static int kwsysSystem_Shell__CharIsMakeVariableName(char c)
121 return c && (c == '_' || isalpha(((int)c)));
124 /*--------------------------------------------------------------------------*/
125 static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c)
127 while(*c == '$' && *(c+1) == '(')
129 const char* skip = c+2;
130 while(kwsysSystem_Shell__CharIsMakeVariableName(*skip))
132 ++skip;
134 if(*skip == ')')
136 c = skip+1;
138 else
140 break;
143 return c;
147 Allowing make variable replacements opens a can of worms. Sometimes
148 they should be quoted and sometimes not. Sometimes their replacement
149 values are already quoted or contain escapes.
151 Some Visual Studio variables cause problems. In order to pass the
152 referenced value with spaces the reference must be quoted. If the
153 variable value ends in a backslash then it will escape the ending
154 quote! In order to make the ending backslash appear we need this:
156 "$(InputDir)\"
158 However if there is not a trailing backslash then this will put a
159 quote in the value so we need:
161 "$(InputDir)"
163 This macro decides whether we quote an argument just because it
164 contains a make variable reference. This should be replaced with a
165 flag later when we understand applications of this better.
167 #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
169 /*--------------------------------------------------------------------------*/
170 static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
171 int flags)
173 /* The empty string needs quotes. */
174 if(!*in)
176 return 1;
179 /* Scan the string for characters that require quoting. */
181 const char* c;
182 for(c=in; *c; ++c)
184 /* Look for $(MAKEVAR) syntax if requested. */
185 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
187 #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
188 const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
189 if(skip != c)
191 /* We need to quote make variable references to preserve the
192 string with contents substituted in its place. */
193 return 1;
195 #else
196 /* Skip over the make variable references if any are present. */
197 c = kwsysSystem_Shell__SkipMakeVariables(c);
199 /* Stop if we have reached the end of the string. */
200 if(!*c)
202 break;
204 #endif
207 /* Check whether this character needs quotes. */
208 if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags))
210 return 1;
215 /* On Windows some single character arguments need quotes. */
216 if(!isUnix && *in && !*(in+1))
218 char c = *in;
219 if((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#'))
221 return 1;
225 return 0;
228 /*--------------------------------------------------------------------------*/
229 static int kwsysSystem_Shell__GetArgumentSize(const char* in,
230 int isUnix, int flags)
232 /* Start with the length of the original argument, plus one for
233 either a terminating null or a separating space. */
234 int size = (int)strlen(in) + 1;
236 /* String iterator. */
237 const char* c;
239 /* Keep track of how many backslashes have been encountered in a row. */
240 int windows_backslashes = 0;
242 /* Scan the string for characters that require escaping or quoting. */
243 for(c=in; *c; ++c)
245 /* Look for $(MAKEVAR) syntax if requested. */
246 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
248 /* Skip over the make variable references if any are present. */
249 c = kwsysSystem_Shell__SkipMakeVariables(c);
251 /* Stop if we have reached the end of the string. */
252 if(!*c)
254 break;
258 /* Check whether this character needs escaping for the shell. */
259 if(isUnix)
261 /* On Unix a few special characters need escaping even inside a
262 quoted argument. */
263 if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
265 /* This character needs a backslash to escape it. */
266 ++size;
269 else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
271 /* On Windows the built-in command shell echo never needs escaping. */
273 else
275 /* On Windows only backslashes and double-quotes need escaping. */
276 if(*c == '\\')
278 /* Found a backslash. It may need to be escaped later. */
279 ++windows_backslashes;
281 else if(*c == '"')
283 /* Found a double-quote. We need to escape it and all
284 immediately preceding backslashes. */
285 size += windows_backslashes + 1;
286 windows_backslashes = 0;
288 else
290 /* Found another character. This eliminates the possibility
291 that any immediately preceding backslashes will be
292 escaped. */
293 windows_backslashes = 0;
297 /* Check whether this character needs escaping for a make tool. */
298 if(*c == '$')
300 if(flags & kwsysSystem_Shell_Flag_Make)
302 /* In Makefiles a dollar is written $$ so we need one extra
303 character. */
304 ++size;
306 else if(flags & kwsysSystem_Shell_Flag_VSIDE)
308 /* In a VS IDE a dollar is written "$" so we need two extra
309 characters. */
310 size += 2;
313 else if(*c == '#')
315 if((flags & kwsysSystem_Shell_Flag_Make) &&
316 (flags & kwsysSystem_Shell_Flag_WatcomWMake))
318 /* In Watcom WMake makefiles a pound is written $# so we need
319 one extra character. */
320 ++size;
323 else if(*c == '%')
325 if((flags & kwsysSystem_Shell_Flag_VSIDE) ||
326 ((flags & kwsysSystem_Shell_Flag_Make) &&
327 ((flags & kwsysSystem_Shell_Flag_MinGWMake) ||
328 (flags & kwsysSystem_Shell_Flag_NMake))))
330 /* In the VS IDE, NMake, or MinGW make a percent is written %%
331 so we need one extra characters. */
332 size += 1;
335 else if(*c == ';')
337 if(flags & kwsysSystem_Shell_Flag_VSIDE)
339 /* In a VS IDE a semicolon is written ";" so we need two extra
340 characters. */
341 size += 2;
346 /* Check whether the argument needs surrounding quotes. */
347 if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags))
349 /* Surrounding quotes are needed. Allocate space for them. */
350 size += 2;
352 /* We must escape all ending backslashes when quoting on windows. */
353 size += windows_backslashes;
356 return size;
359 /*--------------------------------------------------------------------------*/
360 static char* kwsysSystem_Shell__GetArgument(const char* in, char* out,
361 int isUnix, int flags)
363 /* String iterator. */
364 const char* c;
366 /* Keep track of how many backslashes have been encountered in a row. */
367 int windows_backslashes = 0;
369 /* Whether the argument must be quoted. */
370 int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags);
371 if(needQuotes)
373 /* Add the opening quote for this argument. */
374 *out++ = '"';
377 /* Scan the string for characters that require escaping or quoting. */
378 for(c=in; *c; ++c)
380 /* Look for $(MAKEVAR) syntax if requested. */
381 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
383 const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
384 if(skip != c)
386 /* Copy to the end of the make variable references. */
387 while(c != skip)
389 *out++ = *c++;
392 /* The make variable reference eliminates any escaping needed
393 for preceding backslashes. */
394 windows_backslashes = 0;
396 /* Stop if we have reached the end of the string. */
397 if(!*c)
399 break;
404 /* Check whether this character needs escaping for the shell. */
405 if(isUnix)
407 /* On Unix a few special characters need escaping even inside a
408 quoted argument. */
409 if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
411 /* This character needs a backslash to escape it. */
412 *out++ = '\\';
415 else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
417 /* On Windows the built-in command shell echo never needs escaping. */
419 else
421 /* On Windows only backslashes and double-quotes need escaping. */
422 if(*c == '\\')
424 /* Found a backslash. It may need to be escaped later. */
425 ++windows_backslashes;
427 else if(*c == '"')
429 /* Found a double-quote. Escape all immediately preceding
430 backslashes. */
431 while(windows_backslashes > 0)
433 --windows_backslashes;
434 *out++ = '\\';
437 /* Add the backslash to escape the double-quote. */
438 *out++ = '\\';
440 else
442 /* We encountered a normal character. This eliminates any
443 escaping needed for preceding backslashes. */
444 windows_backslashes = 0;
448 /* Check whether this character needs escaping for a make tool. */
449 if(*c == '$')
451 if(flags & kwsysSystem_Shell_Flag_Make)
453 /* In Makefiles a dollar is written $$. The make tool will
454 replace it with just $ before passing it to the shell. */
455 *out++ = '$';
456 *out++ = '$';
458 else if(flags & kwsysSystem_Shell_Flag_VSIDE)
460 /* In a VS IDE a dollar is written "$". If this is written in
461 an un-quoted argument it starts a quoted segment, inserts
462 the $ and ends the segment. If it is written in a quoted
463 argument it ends quoting, inserts the $ and restarts
464 quoting. Either way the $ is isolated from surrounding
465 text to avoid looking like a variable reference. */
466 *out++ = '"';
467 *out++ = '$';
468 *out++ = '"';
470 else
472 /* Otherwise a dollar is written just $. */
473 *out++ = '$';
476 else if(*c == '#')
478 if((flags & kwsysSystem_Shell_Flag_Make) &&
479 (flags & kwsysSystem_Shell_Flag_WatcomWMake))
481 /* In Watcom WMake makefiles a pound is written $#. The make
482 tool will replace it with just # before passing it to the
483 shell. */
484 *out++ = '$';
485 *out++ = '#';
487 else
489 /* Otherwise a pound is written just #. */
490 *out++ = '#';
493 else if(*c == '%')
495 if((flags & kwsysSystem_Shell_Flag_VSIDE) ||
496 ((flags & kwsysSystem_Shell_Flag_Make) &&
497 ((flags & kwsysSystem_Shell_Flag_MinGWMake) ||
498 (flags & kwsysSystem_Shell_Flag_NMake))))
500 /* In the VS IDE, NMake, or MinGW make a percent is written %%. */
501 *out++ = '%';
502 *out++ = '%';
504 else
506 /* Otherwise a percent is written just %. */
507 *out++ = '%';
510 else if(*c == ';')
512 if(flags & kwsysSystem_Shell_Flag_VSIDE)
514 /* In a VS IDE a semicolon is written ";". If this is written
515 in an un-quoted argument it starts a quoted segment,
516 inserts the ; and ends the segment. If it is written in a
517 quoted argument it ends quoting, inserts the ; and restarts
518 quoting. Either way the ; is isolated. */
519 *out++ = '"';
520 *out++ = ';';
521 *out++ = '"';
523 else
525 /* Otherwise a semicolon is written just ;. */
526 *out++ = ';';
529 else
531 /* Store this character. */
532 *out++ = *c;
536 if(needQuotes)
538 /* Add enough backslashes to escape any trailing ones. */
539 while(windows_backslashes > 0)
541 --windows_backslashes;
542 *out++ = '\\';
545 /* Add the closing quote for this argument. */
546 *out++ = '"';
549 /* Store a terminating null without incrementing. */
550 *out = 0;
552 return out;
555 /*--------------------------------------------------------------------------*/
556 char* kwsysSystem_Shell_GetArgumentForWindows(const char* in,
557 char* out,
558 int flags)
560 return kwsysSystem_Shell__GetArgument(in, out, 0, flags);
563 /*--------------------------------------------------------------------------*/
564 char* kwsysSystem_Shell_GetArgumentForUnix(const char* in,
565 char* out,
566 int flags)
568 return kwsysSystem_Shell__GetArgument(in, out, 1, flags);
571 /*--------------------------------------------------------------------------*/
572 int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, int flags)
574 return kwsysSystem_Shell__GetArgumentSize(in, 0, flags);
577 /*--------------------------------------------------------------------------*/
578 int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, int flags)
580 return kwsysSystem_Shell__GetArgumentSize(in, 1, flags);