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__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
))
86 /* On all platforms quotes are needed to preserve whitespace. */
87 if(kwsysSystem_Shell__CharIsWhitespace(c
))
94 /* On UNIX several special characters need quotes to preserve them. */
95 if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c
))
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
)
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
))
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:
155 However if there is not a trailing backslash then this will put a
156 quote in the value so we need:
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
,
170 /* Scan the string for characters that require quoting. */
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
);
182 /* We need to quote make variable references to preserve the
183 string with contents substituted in its place. */
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. */
198 /* Check whether this character needs quotes. */
199 if(kwsysSystem_Shell__CharNeedsQuotes(*c
, isUnix
, flags
))
206 /* On Windows some single character arguments need quotes. */
207 if(!isUnix
&& *in
&& !*(in
+1))
210 if((c
== '?') || (c
== '&') || (c
== '^') || (c
== '|') || (c
== '#'))
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. */
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. */
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. */
249 /* Check whether this character needs escaping for the shell. */
252 /* On Unix a few special characters need escaping even inside a
254 if(*c
== '\\' || *c
== '"' || *c
== '`' || *c
== '$')
256 /* This character needs a backslash to escape it. */
260 else if(flags
& kwsysSystem_Shell_Flag_EchoWindows
)
262 /* On Windows the built-in command shell echo never needs escaping. */
266 /* On Windows only backslashes and double-quotes need escaping. */
269 /* Found a backslash. It may need to be escaped later. */
270 ++windows_backslashes
;
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;
281 /* Found another character. This eliminates the possibility
282 that any immediately preceding backslashes will be
284 windows_backslashes
= 0;
288 /* Check whether this character needs escaping for a make tool. */
291 if(flags
& kwsysSystem_Shell_Flag_Make
)
293 /* In Makefiles a dollar is written $$ so we need one extra
297 else if(flags
& kwsysSystem_Shell_Flag_VSIDE
)
299 /* In a VS IDE a dollar is written "$" so we need two extra
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. */
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. */
328 if(flags
& kwsysSystem_Shell_Flag_VSIDE
)
330 /* In a VS IDE a semicolon is written ";" so we need two extra
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. */
343 /* We must escape all ending backslashes when quoting on windows. */
344 size
+= windows_backslashes
;
350 /*--------------------------------------------------------------------------*/
351 static char* kwsysSystem_Shell__GetArgument(const char* in
, char* out
,
352 int isUnix
, int flags
)
354 /* String iterator. */
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
);
364 /* Add the opening quote for this argument. */
368 /* Scan the string for characters that require escaping or quoting. */
371 /* Look for $(MAKEVAR) syntax if requested. */
372 if(flags
& kwsysSystem_Shell_Flag_AllowMakeVariables
)
374 const char* skip
= kwsysSystem_Shell__SkipMakeVariables(c
);
377 /* Copy to the end of the make variable references. */
383 /* Stop if we have reached the end of the string. */
391 /* Check whether this character needs escaping for the shell. */
394 /* On Unix a few special characters need escaping even inside a
396 if(*c
== '\\' || *c
== '"' || *c
== '`' || *c
== '$')
398 /* This character needs a backslash to escape it. */
402 else if(flags
& kwsysSystem_Shell_Flag_EchoWindows
)
404 /* On Windows the built-in command shell echo never needs escaping. */
408 /* On Windows only backslashes and double-quotes need escaping. */
411 /* Found a backslash. It may need to be escaped later. */
412 ++windows_backslashes
;
416 /* Found a double-quote. Escape all immediately preceding
418 while(windows_backslashes
> 0)
420 --windows_backslashes
;
424 /* Add the backslash to escape the double-quote. */
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. */
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. */
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. */
459 /* Otherwise a dollar is written just $. */
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
476 /* Otherwise a pound is written just #. */
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 %%. */
493 /* Otherwise a percent is written just %. */
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. */
512 /* Otherwise a semicolon is written just ;. */
518 /* Store this character. */
525 /* Add enough backslashes to escape any trailing ones. */
526 while(windows_backslashes
> 0)
528 --windows_backslashes
;
532 /* Add the closing quote for this argument. */
536 /* Store a terminating null without incrementing. */
542 /*--------------------------------------------------------------------------*/
543 char* kwsysSystem_Shell_GetArgumentForWindows(const char* in
,
547 return kwsysSystem_Shell__GetArgument(in
, out
, 0, flags
);
550 /*--------------------------------------------------------------------------*/
551 char* kwsysSystem_Shell_GetArgumentForUnix(const char* in
,
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
);