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. */
20 # include "System.h.in"
23 #include <string.h> /* strlen */
24 #include <ctype.h> /* isalpha */
32 Make variable replacements open a can of worms. Sometimes they should
33 be quoted and sometimes not. Sometimes their replacement values are
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:
43 However if there is not a trailing backslash then this will put a
44 quote in the value so we need:
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
))
93 /* On all platforms quotes are needed to preserve whitespace. */
94 if(kwsysSystem_Shell__CharIsWhitespace(c
))
101 /* On UNIX several special characters need quotes to preserve them. */
102 if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c
))
109 /* On Windows several special characters need quotes to preserve them. */
110 if(kwsysSystem_Shell__CharNeedsQuotesOnWindows(c
))
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
))
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:
158 However if there is not a trailing backslash then this will put a
159 quote in the value so we need:
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
,
173 /* The empty string needs quotes. */
179 /* Scan the string for characters that require quoting. */
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
);
191 /* We need to quote make variable references to preserve the
192 string with contents substituted in its place. */
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. */
207 /* Check whether this character needs quotes. */
208 if(kwsysSystem_Shell__CharNeedsQuotes(*c
, isUnix
, flags
))
215 /* On Windows some single character arguments need quotes. */
216 if(!isUnix
&& *in
&& !*(in
+1))
219 if((c
== '?') || (c
== '&') || (c
== '^') || (c
== '|') || (c
== '#'))
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. */
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. */
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. */
258 /* Check whether this character needs escaping for the shell. */
261 /* On Unix a few special characters need escaping even inside a
263 if(*c
== '\\' || *c
== '"' || *c
== '`' || *c
== '$')
265 /* This character needs a backslash to escape it. */
269 else if(flags
& kwsysSystem_Shell_Flag_EchoWindows
)
271 /* On Windows the built-in command shell echo never needs escaping. */
275 /* On Windows only backslashes and double-quotes need escaping. */
278 /* Found a backslash. It may need to be escaped later. */
279 ++windows_backslashes
;
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;
290 /* Found another character. This eliminates the possibility
291 that any immediately preceding backslashes will be
293 windows_backslashes
= 0;
297 /* Check whether this character needs escaping for a make tool. */
300 if(flags
& kwsysSystem_Shell_Flag_Make
)
302 /* In Makefiles a dollar is written $$ so we need one extra
306 else if(flags
& kwsysSystem_Shell_Flag_VSIDE
)
308 /* In a VS IDE a dollar is written "$" so we need two extra
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. */
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. */
337 if(flags
& kwsysSystem_Shell_Flag_VSIDE
)
339 /* In a VS IDE a semicolon is written ";" so we need two extra
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. */
352 /* We must escape all ending backslashes when quoting on windows. */
353 size
+= windows_backslashes
;
359 /*--------------------------------------------------------------------------*/
360 static char* kwsysSystem_Shell__GetArgument(const char* in
, char* out
,
361 int isUnix
, int flags
)
363 /* String iterator. */
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
);
373 /* Add the opening quote for this argument. */
377 /* Scan the string for characters that require escaping or quoting. */
380 /* Look for $(MAKEVAR) syntax if requested. */
381 if(flags
& kwsysSystem_Shell_Flag_AllowMakeVariables
)
383 const char* skip
= kwsysSystem_Shell__SkipMakeVariables(c
);
386 /* Copy to the end of the make variable references. */
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. */
404 /* Check whether this character needs escaping for the shell. */
407 /* On Unix a few special characters need escaping even inside a
409 if(*c
== '\\' || *c
== '"' || *c
== '`' || *c
== '$')
411 /* This character needs a backslash to escape it. */
415 else if(flags
& kwsysSystem_Shell_Flag_EchoWindows
)
417 /* On Windows the built-in command shell echo never needs escaping. */
421 /* On Windows only backslashes and double-quotes need escaping. */
424 /* Found a backslash. It may need to be escaped later. */
425 ++windows_backslashes
;
429 /* Found a double-quote. Escape all immediately preceding
431 while(windows_backslashes
> 0)
433 --windows_backslashes
;
437 /* Add the backslash to escape the double-quote. */
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. */
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. */
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. */
472 /* Otherwise a dollar is written just $. */
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
489 /* Otherwise a pound is written just #. */
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 %%. */
506 /* Otherwise a percent is written just %. */
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. */
525 /* Otherwise a semicolon is written just ;. */
531 /* Store this character. */
538 /* Add enough backslashes to escape any trailing ones. */
539 while(windows_backslashes
> 0)
541 --windows_backslashes
;
545 /* Add the closing quote for this argument. */
549 /* Store a terminating null without incrementing. */
555 /*--------------------------------------------------------------------------*/
556 char* kwsysSystem_Shell_GetArgumentForWindows(const char* in
,
560 return kwsysSystem_Shell__GetArgument(in
, out
, 0, flags
);
563 /*--------------------------------------------------------------------------*/
564 char* kwsysSystem_Shell_GetArgumentForUnix(const char* in
,
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
);