Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / kwsys / System.c
blob6cfc311a1a94a9b2599bc1b359009612d32cf8c2
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__CharNeedsQuotes(char c, int isUnix, int flags)
80 /* On Windows the built-in command shell echo never needs quotes. */
81 if(!isUnix && (flags & kwsysSystem_Shell_Flag_EchoWindows))
83 return 0;
86 /* On all platforms quotes are needed to preserve whitespace. */
87 if(kwsysSystem_Shell__CharIsWhitespace(c))
89 return 1;
92 if(isUnix)
94 /* On UNIX several special characters need quotes to preserve them. */
95 if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c))
97 return 1;
100 else
102 /* On Windows single-quotes must be escaped in some make
103 environments, such as in mingw32-make. */
104 if(flags & kwsysSystem_Shell_Flag_Make)
106 if(c == '\'')
108 return 1;
112 return 0;
115 /*--------------------------------------------------------------------------*/
116 static int kwsysSystem_Shell__CharIsMakeVariableName(char c)
118 return c && (c == '_' || isalpha(((int)c)));
121 /*--------------------------------------------------------------------------*/
122 static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c)
124 while(*c == '$' && *(c+1) == '(')
126 const char* skip = c+2;
127 while(kwsysSystem_Shell__CharIsMakeVariableName(*skip))
129 ++skip;
131 if(*skip == ')')
133 c = skip+1;
135 else
137 break;
140 return c;
144 Allowing make variable replacements opens a can of worms. Sometimes
145 they should be quoted and sometimes not. Sometimes their replacement
146 values are already quoted or contain escapes.
148 Some Visual Studio variables cause problems. In order to pass the
149 referenced value with spaces the reference must be quoted. If the
150 variable value ends in a backslash then it will escape the ending
151 quote! In order to make the ending backslash appear we need this:
153 "$(InputDir)\"
155 However if there is not a trailing backslash then this will put a
156 quote in the value so we need:
158 "$(InputDir)"
160 This macro decides whether we quote an argument just because it
161 contains a make variable reference. This should be replaced with a
162 flag later when we understand applications of this better.
164 #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
166 /*--------------------------------------------------------------------------*/
167 static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
168 int flags)
170 /* Scan the string for characters that require quoting. */
172 const char* c;
173 for(c=in; *c; ++c)
175 /* Look for $(MAKEVAR) syntax if requested. */
176 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
178 #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
179 const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
180 if(skip != c)
182 /* We need to quote make variable references to preserve the
183 string with contents substituted in its place. */
184 return 1;
186 #else
187 /* Skip over the make variable references if any are present. */
188 c = kwsysSystem_Shell__SkipMakeVariables(c);
190 /* Stop if we have reached the end of the string. */
191 if(!*c)
193 break;
195 #endif
198 /* Check whether this character needs quotes. */
199 if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags))
201 return 1;
206 /* On Windows some single character arguments need quotes. */
207 if(!isUnix && *in && !*(in+1))
209 char c = *in;
210 if((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#'))
212 return 1;
216 return 0;
219 /*--------------------------------------------------------------------------*/
220 static int kwsysSystem_Shell__GetArgumentSize(const char* in,
221 int isUnix, int flags)
223 /* Start with the length of the original argument, plus one for
224 either a terminating null or a separating space. */
225 int size = (int)strlen(in) + 1;
227 /* String iterator. */
228 const char* c;
230 /* Keep track of how many backslashes have been encountered in a row. */
231 int windows_backslashes = 0;
233 /* Scan the string for characters that require escaping or quoting. */
234 for(c=in; *c; ++c)
236 /* Look for $(MAKEVAR) syntax if requested. */
237 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
239 /* Skip over the make variable references if any are present. */
240 c = kwsysSystem_Shell__SkipMakeVariables(c);
242 /* Stop if we have reached the end of the string. */
243 if(!*c)
245 break;
249 /* Check whether this character needs escaping for the shell. */
250 if(isUnix)
252 /* On Unix a few special characters need escaping even inside a
253 quoted argument. */
254 if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
256 /* This character needs a backslash to escape it. */
257 ++size;
260 else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
262 /* On Windows the built-in command shell echo never needs escaping. */
264 else
266 /* On Windows only backslashes and double-quotes need escaping. */
267 if(*c == '\\')
269 /* Found a backslash. It may need to be escaped later. */
270 ++windows_backslashes;
272 else if(*c == '"')
274 /* Found a double-quote. We need to escape it and all
275 immediately preceding backslashes. */
276 size += windows_backslashes + 1;
277 windows_backslashes = 0;
279 else
281 /* Found another character. This eliminates the possibility
282 that any immediately preceding backslashes will be
283 escaped. */
284 windows_backslashes = 0;
288 /* Check whether this character needs escaping for a make tool. */
289 if(*c == '$')
291 if(flags & kwsysSystem_Shell_Flag_Make)
293 /* In Makefiles a dollar is written $$ so we need one extra
294 character. */
295 ++size;
297 else if(flags & kwsysSystem_Shell_Flag_VSIDE)
299 /* In a VS IDE a dollar is written "$" so we need two extra
300 characters. */
301 size += 2;
304 else if(*c == '#')
306 if((flags & kwsysSystem_Shell_Flag_Make) &&
307 (flags & kwsysSystem_Shell_Flag_WatcomWMake))
309 /* In Watcom WMake makefiles a pound is written $# so we need
310 one extra character. */
311 ++size;
314 else if(*c == '%')
316 if((flags & kwsysSystem_Shell_Flag_VSIDE) ||
317 ((flags & kwsysSystem_Shell_Flag_Make) &&
318 ((flags & kwsysSystem_Shell_Flag_MinGWMake) ||
319 (flags & kwsysSystem_Shell_Flag_NMake))))
321 /* In the VS IDE, NMake, or MinGW make a percent is written %%
322 so we need one extra characters. */
323 size += 1;
326 else if(*c == ';')
328 if(flags & kwsysSystem_Shell_Flag_VSIDE)
330 /* In a VS IDE a semicolon is written ";" so we need two extra
331 characters. */
332 size += 2;
337 /* Check whether the argument needs surrounding quotes. */
338 if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags))
340 /* Surrounding quotes are needed. Allocate space for them. */
341 size += 2;
343 /* We must escape all ending backslashes when quoting on windows. */
344 size += windows_backslashes;
347 return size;
350 /*--------------------------------------------------------------------------*/
351 static char* kwsysSystem_Shell__GetArgument(const char* in, char* out,
352 int isUnix, int flags)
354 /* String iterator. */
355 const char* c;
357 /* Keep track of how many backslashes have been encountered in a row. */
358 int windows_backslashes = 0;
360 /* Whether the argument must be quoted. */
361 int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags);
362 if(needQuotes)
364 /* Add the opening quote for this argument. */
365 *out++ = '"';
368 /* Scan the string for characters that require escaping or quoting. */
369 for(c=in; *c; ++c)
371 /* Look for $(MAKEVAR) syntax if requested. */
372 if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
374 const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
375 if(skip != c)
377 /* Copy to the end of the make variable references. */
378 while(c != skip)
380 *out++ = *c++;
383 /* Stop if we have reached the end of the string. */
384 if(!*c)
386 break;
391 /* Check whether this character needs escaping for the shell. */
392 if(isUnix)
394 /* On Unix a few special characters need escaping even inside a
395 quoted argument. */
396 if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
398 /* This character needs a backslash to escape it. */
399 *out++ = '\\';
402 else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
404 /* On Windows the built-in command shell echo never needs escaping. */
406 else
408 /* On Windows only backslashes and double-quotes need escaping. */
409 if(*c == '\\')
411 /* Found a backslash. It may need to be escaped later. */
412 ++windows_backslashes;
414 else if(*c == '"')
416 /* Found a double-quote. Escape all immediately preceding
417 backslashes. */
418 while(windows_backslashes > 0)
420 --windows_backslashes;
421 *out++ = '\\';
424 /* Add the backslash to escape the double-quote. */
425 *out++ = '\\';
427 else
429 /* We encountered a normal character. This eliminates any
430 escaping needed for preceding backslashes. */
431 windows_backslashes = 0;
435 /* Check whether this character needs escaping for a make tool. */
436 if(*c == '$')
438 if(flags & kwsysSystem_Shell_Flag_Make)
440 /* In Makefiles a dollar is written $$. The make tool will
441 replace it with just $ before passing it to the shell. */
442 *out++ = '$';
443 *out++ = '$';
445 else if(flags & kwsysSystem_Shell_Flag_VSIDE)
447 /* In a VS IDE a dollar is written "$". If this is written in
448 an un-quoted argument it starts a quoted segment, inserts
449 the $ and ends the segment. If it is written in a quoted
450 argument it ends quoting, inserts the $ and restarts
451 quoting. Either way the $ is isolated from surrounding
452 text to avoid looking like a variable reference. */
453 *out++ = '"';
454 *out++ = '$';
455 *out++ = '"';
457 else
459 /* Otherwise a dollar is written just $. */
460 *out++ = '$';
463 else if(*c == '#')
465 if((flags & kwsysSystem_Shell_Flag_Make) &&
466 (flags & kwsysSystem_Shell_Flag_WatcomWMake))
468 /* In Watcom WMake makefiles a pound is written $#. The make
469 tool will replace it with just # before passing it to the
470 shell. */
471 *out++ = '$';
472 *out++ = '#';
474 else
476 /* Otherwise a pound is written just #. */
477 *out++ = '#';
480 else if(*c == '%')
482 if((flags & kwsysSystem_Shell_Flag_VSIDE) ||
483 ((flags & kwsysSystem_Shell_Flag_Make) &&
484 ((flags & kwsysSystem_Shell_Flag_MinGWMake) ||
485 (flags & kwsysSystem_Shell_Flag_NMake))))
487 /* In the VS IDE, NMake, or MinGW make a percent is written %%. */
488 *out++ = '%';
489 *out++ = '%';
491 else
493 /* Otherwise a percent is written just %. */
494 *out++ = '%';
497 else if(*c == ';')
499 if(flags & kwsysSystem_Shell_Flag_VSIDE)
501 /* In a VS IDE a semicolon is written ";". If this is written
502 in an un-quoted argument it starts a quoted segment,
503 inserts the ; and ends the segment. If it is written in a
504 quoted argument it ends quoting, inserts the ; and restarts
505 quoting. Either way the ; is isolated. */
506 *out++ = '"';
507 *out++ = ';';
508 *out++ = '"';
510 else
512 /* Otherwise a semicolon is written just ;. */
513 *out++ = ';';
516 else
518 /* Store this character. */
519 *out++ = *c;
523 if(needQuotes)
525 /* Add enough backslashes to escape any trailing ones. */
526 while(windows_backslashes > 0)
528 --windows_backslashes;
529 *out++ = '\\';
532 /* Add the closing quote for this argument. */
533 *out++ = '"';
536 /* Store a terminating null without incrementing. */
537 *out = 0;
539 return out;
542 /*--------------------------------------------------------------------------*/
543 char* kwsysSystem_Shell_GetArgumentForWindows(const char* in,
544 char* out,
545 int flags)
547 return kwsysSystem_Shell__GetArgument(in, out, 0, flags);
550 /*--------------------------------------------------------------------------*/
551 char* kwsysSystem_Shell_GetArgumentForUnix(const char* in,
552 char* out,
553 int flags)
555 return kwsysSystem_Shell__GetArgument(in, out, 1, flags);
558 /*--------------------------------------------------------------------------*/
559 int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, int flags)
561 return kwsysSystem_Shell__GetArgumentSize(in, 0, flags);
564 /*--------------------------------------------------------------------------*/
565 int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, int flags)
567 return kwsysSystem_Shell__GetArgumentSize(in, 1, flags);