diff --git a/Source/cmOutputConverter.cxx b/Source/cmOutputConverter.cxx index 161a5b493..7cd6a47b5 100644 --- a/Source/cmOutputConverter.cxx +++ b/Source/cmOutputConverter.cxx @@ -14,10 +14,11 @@ #include "cmAlgorithms.h" #include "cmake.h" -#include - #include +#include /* strlen */ +#include /* isalpha */ + cmOutputConverter::cmOutputConverter(cmState::Snapshot snapshot) : StateSnapshot(snapshot), LinkScriptShell(false) { @@ -330,51 +331,51 @@ std::string cmOutputConverter::EscapeForShell(const std::string& str, int flags = 0; if(this->GetState()->UseWindowsVSIDE()) { - flags |= cmsysSystem_Shell_Flag_VSIDE; + flags |= Shell_Flag_VSIDE; } else if(!this->LinkScriptShell) { - flags |= cmsysSystem_Shell_Flag_Make; + flags |= Shell_Flag_Make; } if(makeVars) { - flags |= cmsysSystem_Shell_Flag_AllowMakeVariables; + flags |= Shell_Flag_AllowMakeVariables; } if(forEcho) { - flags |= cmsysSystem_Shell_Flag_EchoWindows; + flags |= Shell_Flag_EchoWindows; } if(useWatcomQuote) { - flags |= cmsysSystem_Shell_Flag_WatcomQuote; + flags |= Shell_Flag_WatcomQuote; } if(this->GetState()->UseWatcomWMake()) { - flags |= cmsysSystem_Shell_Flag_WatcomWMake; + flags |= Shell_Flag_WatcomWMake; } if(this->GetState()->UseMinGWMake()) { - flags |= cmsysSystem_Shell_Flag_MinGWMake; + flags |= Shell_Flag_MinGWMake; } if(this->GetState()->UseNMake()) { - flags |= cmsysSystem_Shell_Flag_NMake; + flags |= Shell_Flag_NMake; } // Compute the buffer size needed. int size = (this->GetState()->UseWindowsShell() ? - cmsysSystem_Shell_GetArgumentSizeForWindows(str.c_str(), flags) : - cmsysSystem_Shell_GetArgumentSizeForUnix(str.c_str(), flags)); + Shell_GetArgumentSizeForWindows(str.c_str(), flags) : + Shell_GetArgumentSizeForUnix(str.c_str(), flags)); // Compute the shell argument itself. std::vector arg(size); if(this->GetState()->UseWindowsShell()) { - cmsysSystem_Shell_GetArgumentForWindows(str.c_str(), &arg[0], flags); + Shell_GetArgumentForWindows(str.c_str(), &arg[0], flags); } else { - cmsysSystem_Shell_GetArgumentForUnix(str.c_str(), &arg[0], flags); + Shell_GetArgumentForUnix(str.c_str(), &arg[0], flags); } return std::string(&arg[0]); } @@ -417,12 +418,12 @@ cmOutputConverter::EscapeWindowsShellArgument(const char* arg, int shell_flags) { char local_buffer[1024]; char* buffer = local_buffer; - int size = cmsysSystem_Shell_GetArgumentSizeForWindows(arg, shell_flags); + int size = Shell_GetArgumentSizeForWindows(arg, shell_flags); if(size > 1024) { buffer = new char[size]; } - cmsysSystem_Shell_GetArgumentForWindows(arg, buffer, shell_flags); + Shell_GetArgumentForWindows(arg, buffer, shell_flags); std::string result(buffer); if(buffer != local_buffer) { @@ -465,3 +466,585 @@ cmState* cmOutputConverter::GetState() const { return this->StateSnapshot.GetState(); } + +//---------------------------------------------------------------------------- +/* + +Notes: + +Make variable replacements open a can of worms. Sometimes they should +be quoted and sometimes not. Sometimes their replacement values are +already quoted. + +VS variables cause problems. In order to pass the referenced value +with spaces the reference must be quoted. If the variable value ends +in a backslash then it will escape the ending quote! In order to make +the ending backslash appear we need this: + + "$(InputDir)\" + +However if there is not a trailing backslash then this will put a +quote in the value so we need: + + "$(InputDir)" + +Make variable references are platform specific so we should probably +just NOT quote them and let the listfile author deal with it. + +*/ + +/* +TODO: For windows echo: + +To display a pipe (|) or redirection character (< or >) when using the +echo command, use a caret character immediately before the pipe or +redirection character (for example, ^>, ^<, or ^| ). If you need to +use the caret character itself (^), use two in a row (^^). +*/ + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__CharIsWhitespace(char c) +{ + return ((c == ' ') || (c == '\t')); +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__CharNeedsQuotesOnUnix(char c) +{ + return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') || + (c == '&') || (c == '$') || (c == '(') || (c == ')') || + (c == '~') || (c == '<') || (c == '>') || (c == '|') || + (c == '*') || (c == '^') || (c == '\\')); +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__CharNeedsQuotesOnWindows(char c) +{ + return ((c == '\'') || (c == '#') || (c == '&') || + (c == '<') || (c == '>') || (c == '|') || (c == '^')); +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__CharNeedsQuotes(char c, int isUnix, int flags) +{ + /* On Windows the built-in command shell echo never needs quotes. */ + if(!isUnix && (flags & Shell_Flag_EchoWindows)) + { + return 0; + } + + /* On all platforms quotes are needed to preserve whitespace. */ + if(Shell__CharIsWhitespace(c)) + { + return 1; + } + + if(isUnix) + { + /* On UNIX several special characters need quotes to preserve them. */ + if(Shell__CharNeedsQuotesOnUnix(c)) + { + return 1; + } + } + else + { + /* On Windows several special characters need quotes to preserve them. */ + if(Shell__CharNeedsQuotesOnWindows(c)) + { + return 1; + } + } + return 0; +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__CharIsMakeVariableName(char c) +{ + return c && (c == '_' || isalpha(((int)c))); +} + +/*--------------------------------------------------------------------------*/ +const char* cmOutputConverter::Shell__SkipMakeVariables(const char* c) +{ + while(*c == '$' && *(c+1) == '(') + { + const char* skip = c+2; + while(Shell__CharIsMakeVariableName(*skip)) + { + ++skip; + } + if(*skip == ')') + { + c = skip+1; + } + else + { + break; + } + } + return c; +} + +/* +Allowing make variable replacements opens a can of worms. Sometimes +they should be quoted and sometimes not. Sometimes their replacement +values are already quoted or contain escapes. + +Some Visual Studio variables cause problems. In order to pass the +referenced value with spaces the reference must be quoted. If the +variable value ends in a backslash then it will escape the ending +quote! In order to make the ending backslash appear we need this: + + "$(InputDir)\" + +However if there is not a trailing backslash then this will put a +quote in the value so we need: + + "$(InputDir)" + +This macro decides whether we quote an argument just because it +contains a make variable reference. This should be replaced with a +flag later when we understand applications of this better. +*/ +#define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0 + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__ArgumentNeedsQuotes(const char* in, + int isUnix, int flags) +{ + /* The empty string needs quotes. */ + if(!*in) + { + return 1; + } + + /* Scan the string for characters that require quoting. */ + { + const char* c; + for(c=in; *c; ++c) + { + /* Look for $(MAKEVAR) syntax if requested. */ + if(flags & Shell_Flag_AllowMakeVariables) + { +#if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES + const char* skip = Shell__SkipMakeVariables(c); + if(skip != c) + { + /* We need to quote make variable references to preserve the + string with contents substituted in its place. */ + return 1; + } +#else + /* Skip over the make variable references if any are present. */ + c = Shell__SkipMakeVariables(c); + + /* Stop if we have reached the end of the string. */ + if(!*c) + { + break; + } +#endif + } + + /* Check whether this character needs quotes. */ + if(Shell__CharNeedsQuotes(*c, isUnix, flags)) + { + return 1; + } + } + } + + /* On Windows some single character arguments need quotes. */ + if(!isUnix && *in && !*(in+1)) + { + char c = *in; + if((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) + { + return 1; + } + } + + return 0; +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell__GetArgumentSize(const char* in, + int isUnix, int flags) +{ + /* Start with the length of the original argument, plus one for + either a terminating null or a separating space. */ + int size = (int)strlen(in) + 1; + + /* String iterator. */ + const char* c; + + /* Keep track of how many backslashes have been encountered in a row. */ + int windows_backslashes = 0; + + /* Scan the string for characters that require escaping or quoting. */ + for(c=in; *c; ++c) + { + /* Look for $(MAKEVAR) syntax if requested. */ + if(flags & Shell_Flag_AllowMakeVariables) + { + /* Skip over the make variable references if any are present. */ + c = Shell__SkipMakeVariables(c); + + /* Stop if we have reached the end of the string. */ + if(!*c) + { + break; + } + } + + /* Check whether this character needs escaping for the shell. */ + if(isUnix) + { + /* On Unix a few special characters need escaping even inside a + quoted argument. */ + if(*c == '\\' || *c == '"' || *c == '`' || *c == '$') + { + /* This character needs a backslash to escape it. */ + ++size; + } + } + else if(flags & Shell_Flag_EchoWindows) + { + /* On Windows the built-in command shell echo never needs escaping. */ + } + else + { + /* On Windows only backslashes and double-quotes need escaping. */ + if(*c == '\\') + { + /* Found a backslash. It may need to be escaped later. */ + ++windows_backslashes; + } + else if(*c == '"') + { + /* Found a double-quote. We need to escape it and all + immediately preceding backslashes. */ + size += windows_backslashes + 1; + windows_backslashes = 0; + } + else + { + /* Found another character. This eliminates the possibility + that any immediately preceding backslashes will be + escaped. */ + windows_backslashes = 0; + } + } + + /* Check whether this character needs escaping for a make tool. */ + if(*c == '$') + { + if(flags & Shell_Flag_Make) + { + /* In Makefiles a dollar is written $$ so we need one extra + character. */ + ++size; + } + else if(flags & Shell_Flag_VSIDE) + { + /* In a VS IDE a dollar is written "$" so we need two extra + characters. */ + size += 2; + } + } + else if(*c == '#') + { + if((flags & Shell_Flag_Make) && + (flags & Shell_Flag_WatcomWMake)) + { + /* In Watcom WMake makefiles a pound is written $# so we need + one extra character. */ + ++size; + } + } + else if(*c == '%') + { + if((flags & Shell_Flag_VSIDE) || + ((flags & Shell_Flag_Make) && + ((flags & Shell_Flag_MinGWMake) || + (flags & Shell_Flag_NMake)))) + { + /* In the VS IDE, NMake, or MinGW make a percent is written %% + so we need one extra characters. */ + size += 1; + } + } + else if(*c == ';') + { + if(flags & Shell_Flag_VSIDE) + { + /* In a VS IDE a semicolon is written ";" so we need two extra + characters. */ + size += 2; + } + } + } + + /* Check whether the argument needs surrounding quotes. */ + if(Shell__ArgumentNeedsQuotes(in, isUnix, flags)) + { + /* Surrounding quotes are needed. Allocate space for them. */ + if((flags & Shell_Flag_WatcomQuote) && (isUnix)) + { + size += 2; + } + size += 2; + + /* We must escape all ending backslashes when quoting on windows. */ + size += windows_backslashes; + } + + return size; +} + +/*--------------------------------------------------------------------------*/ +char* cmOutputConverter::Shell__GetArgument(const char* in, char* out, + int isUnix, int flags) +{ + /* String iterator. */ + const char* c; + + /* Keep track of how many backslashes have been encountered in a row. */ + int windows_backslashes = 0; + + /* Whether the argument must be quoted. */ + int needQuotes = Shell__ArgumentNeedsQuotes(in, isUnix, flags); + if(needQuotes) + { + /* Add the opening quote for this argument. */ + if(flags & Shell_Flag_WatcomQuote) + { + if(isUnix) + { + *out++ = '"'; + } + *out++ = '\''; + } + else + { + *out++ = '"'; + } + } + + /* Scan the string for characters that require escaping or quoting. */ + for(c=in; *c; ++c) + { + /* Look for $(MAKEVAR) syntax if requested. */ + if(flags & Shell_Flag_AllowMakeVariables) + { + const char* skip = Shell__SkipMakeVariables(c); + if(skip != c) + { + /* Copy to the end of the make variable references. */ + while(c != skip) + { + *out++ = *c++; + } + + /* The make variable reference eliminates any escaping needed + for preceding backslashes. */ + windows_backslashes = 0; + + /* Stop if we have reached the end of the string. */ + if(!*c) + { + break; + } + } + } + + /* Check whether this character needs escaping for the shell. */ + if(isUnix) + { + /* On Unix a few special characters need escaping even inside a + quoted argument. */ + if(*c == '\\' || *c == '"' || *c == '`' || *c == '$') + { + /* This character needs a backslash to escape it. */ + *out++ = '\\'; + } + } + else if(flags & Shell_Flag_EchoWindows) + { + /* On Windows the built-in command shell echo never needs escaping. */ + } + else + { + /* On Windows only backslashes and double-quotes need escaping. */ + if(*c == '\\') + { + /* Found a backslash. It may need to be escaped later. */ + ++windows_backslashes; + } + else if(*c == '"') + { + /* Found a double-quote. Escape all immediately preceding + backslashes. */ + while(windows_backslashes > 0) + { + --windows_backslashes; + *out++ = '\\'; + } + + /* Add the backslash to escape the double-quote. */ + *out++ = '\\'; + } + else + { + /* We encountered a normal character. This eliminates any + escaping needed for preceding backslashes. */ + windows_backslashes = 0; + } + } + + /* Check whether this character needs escaping for a make tool. */ + if(*c == '$') + { + if(flags & Shell_Flag_Make) + { + /* In Makefiles a dollar is written $$. The make tool will + replace it with just $ before passing it to the shell. */ + *out++ = '$'; + *out++ = '$'; + } + else if(flags & Shell_Flag_VSIDE) + { + /* In a VS IDE a dollar is written "$". If this is written in + an un-quoted argument it starts a quoted segment, inserts + the $ and ends the segment. If it is written in a quoted + argument it ends quoting, inserts the $ and restarts + quoting. Either way the $ is isolated from surrounding + text to avoid looking like a variable reference. */ + *out++ = '"'; + *out++ = '$'; + *out++ = '"'; + } + else + { + /* Otherwise a dollar is written just $. */ + *out++ = '$'; + } + } + else if(*c == '#') + { + if((flags & Shell_Flag_Make) && + (flags & Shell_Flag_WatcomWMake)) + { + /* In Watcom WMake makefiles a pound is written $#. The make + tool will replace it with just # before passing it to the + shell. */ + *out++ = '$'; + *out++ = '#'; + } + else + { + /* Otherwise a pound is written just #. */ + *out++ = '#'; + } + } + else if(*c == '%') + { + if((flags & Shell_Flag_VSIDE) || + ((flags & Shell_Flag_Make) && + ((flags & Shell_Flag_MinGWMake) || + (flags & Shell_Flag_NMake)))) + { + /* In the VS IDE, NMake, or MinGW make a percent is written %%. */ + *out++ = '%'; + *out++ = '%'; + } + else + { + /* Otherwise a percent is written just %. */ + *out++ = '%'; + } + } + else if(*c == ';') + { + if(flags & Shell_Flag_VSIDE) + { + /* In a VS IDE a semicolon is written ";". If this is written + in an un-quoted argument it starts a quoted segment, + inserts the ; and ends the segment. If it is written in a + quoted argument it ends quoting, inserts the ; and restarts + quoting. Either way the ; is isolated. */ + *out++ = '"'; + *out++ = ';'; + *out++ = '"'; + } + else + { + /* Otherwise a semicolon is written just ;. */ + *out++ = ';'; + } + } + else + { + /* Store this character. */ + *out++ = *c; + } + } + + if(needQuotes) + { + /* Add enough backslashes to escape any trailing ones. */ + while(windows_backslashes > 0) + { + --windows_backslashes; + *out++ = '\\'; + } + + /* Add the closing quote for this argument. */ + if(flags & Shell_Flag_WatcomQuote) + { + *out++ = '\''; + if(isUnix) + { + *out++ = '"'; + } + } + else + { + *out++ = '"'; + } + } + + /* Store a terminating null without incrementing. */ + *out = 0; + + return out; +} + +/*--------------------------------------------------------------------------*/ +char* cmOutputConverter::Shell_GetArgumentForWindows(const char* in, + char* out, int flags) +{ + return Shell__GetArgument(in, out, 0, flags); +} + +/*--------------------------------------------------------------------------*/ +char* cmOutputConverter::Shell_GetArgumentForUnix(const char* in, + char* out, int flags) +{ + return Shell__GetArgument(in, out, 1, flags); +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell_GetArgumentSizeForWindows(const char* in, + int flags) +{ + return Shell__GetArgumentSize(in, 0, flags); +} + +/*--------------------------------------------------------------------------*/ +int cmOutputConverter::Shell_GetArgumentSizeForUnix(const char* in, + int flags) +{ + return Shell__GetArgumentSize(in, 1, flags); +} diff --git a/Source/cmOutputConverter.h b/Source/cmOutputConverter.h index 8739b979f..ed7739edf 100644 --- a/Source/cmOutputConverter.h +++ b/Source/cmOutputConverter.h @@ -64,6 +64,63 @@ public: void SetLinkScriptShell(bool linkScriptShell); + /** + * Flags to pass to Shell_GetArgumentForWindows or + * Shell_GetArgumentForUnix. These modify the generated + * quoting and escape sequences to work under alternative + * environments. + */ + enum Shell_Flag_e + { + /** The target shell is in a makefile. */ + Shell_Flag_Make = (1<<0), + + /** The target shell is in a VS project file. Do not use with + Shell_Flag_Make. */ + Shell_Flag_VSIDE = (1<<1), + + /** In a windows shell the argument is being passed to "echo". */ + Shell_Flag_EchoWindows = (1<<2), + + /** The target shell is in a Watcom WMake makefile. */ + Shell_Flag_WatcomWMake = (1<<3), + + /** The target shell is in a MinGW Make makefile. */ + Shell_Flag_MinGWMake = (1<<4), + + /** The target shell is in a NMake makefile. */ + Shell_Flag_NMake = (1<<5), + + /** Make variable reference syntax $(MAKEVAR) should not be escaped + to allow a build tool to replace it. Replacement values + containing spaces, quotes, backslashes, or other + non-alphanumeric characters that have significance to some makes + or shells produce undefined behavior. */ + Shell_Flag_AllowMakeVariables = (1<<6), + + /** The target shell quoting uses extra single Quotes for Watcom tools. */ + Shell_Flag_WatcomQuote = (1<<7) + }; + + /** + * Transform the given command line argument for use in a Windows or + * Unix shell. Returns a pointer to the end of the command line + * argument in the provided output buffer. Flags may be passed to + * modify the generated quoting and escape sequences to work under + * alternative environments. + */ + static char* Shell_GetArgumentForWindows(const char* in, char* out, + int flags); + static char* Shell_GetArgumentForUnix(const char* in, char* out, int flags); + + /** + * Compute the size of the buffer required to store the output from + * Shell_GetArgumentForWindows or Shell_GetArgumentForUnix. The flags + * passed must be identical between the two calls. + */ + static int Shell_GetArgumentSizeForWindows(const char* in, int flags); + static int Shell_GetArgumentSizeForUnix(const char* in, int flags); + std::string EscapeForShell(const std::string& str, bool makeVars = false, bool forEcho = false, @@ -102,6 +159,19 @@ private: std::string const& result, OutputFormat format) const; + static int Shell__CharIsWhitespace(char c); + static int Shell__CharNeedsQuotesOnUnix(char c); + static int Shell__CharNeedsQuotesOnWindows(char c); + static int Shell__CharNeedsQuotes(char c, int isUnix, int flags); + static int Shell__CharIsMakeVariableName(char c); + static const char* Shell__SkipMakeVariables(const char* c); + static int Shell__ArgumentNeedsQuotes(const char* in, + int isUnix, int flags); + static int Shell__GetArgumentSize(const char* in, + int isUnix, int flags); + static char* Shell__GetArgument(const char* in, char* out, + int isUnix, int flags); + private: cmState::Snapshot StateSnapshot; diff --git a/Source/cmVisualStudioGeneratorOptions.cxx b/Source/cmVisualStudioGeneratorOptions.cxx index c028ab716..abd350e4a 100644 --- a/Source/cmVisualStudioGeneratorOptions.cxx +++ b/Source/cmVisualStudioGeneratorOptions.cxx @@ -1,7 +1,6 @@ #include "cmVisualStudioGeneratorOptions.h" #include "cmOutputConverter.h" #include "cmSystemTools.h" -#include #include "cmVisualStudio10TargetGenerator.h" static @@ -249,8 +248,8 @@ void cmVisualStudioGeneratorOptions::StoreUnknownFlag(const char* flag) this->FlagString += cmOutputConverter::EscapeWindowsShellArgument( flag, - cmsysSystem_Shell_Flag_AllowMakeVariables | - cmsysSystem_Shell_Flag_VSIDE); + cmOutputConverter::Shell_Flag_AllowMakeVariables | + cmOutputConverter::Shell_Flag_VSIDE); } //---------------------------------------------------------------------------- diff --git a/Source/kwsys/System.c b/Source/kwsys/System.c index 1ee26fac5..ccc7e8164 100644 --- a/Source/kwsys/System.c +++ b/Source/kwsys/System.c @@ -20,8 +20,8 @@ #include /* ptrdiff_t */ #include /* malloc, free */ -#include /* strlen */ -#include /* isalpha */ +#include /* memcpy */ +#include /* isspace */ #include @@ -31,587 +31,6 @@ typedef ptrdiff_t kwsysSystem_ptrdiff_t; typedef int kwsysSystem_ptrdiff_t; #endif -/* - -Notes: - -Make variable replacements open a can of worms. Sometimes they should -be quoted and sometimes not. Sometimes their replacement values are -already quoted. - -VS variables cause problems. In order to pass the referenced value -with spaces the reference must be quoted. If the variable value ends -in a backslash then it will escape the ending quote! In order to make -the ending backslash appear we need this: - - "$(InputDir)\" - -However if there is not a trailing backslash then this will put a -quote in the value so we need: - - "$(InputDir)" - -Make variable references are platform specific so we should probably -just NOT quote them and let the listfile author deal with it. - -*/ - -/* -TODO: For windows echo: - -To display a pipe (|) or redirection character (< or >) when using the -echo command, use a caret character immediately before the pipe or -redirection character (for example, ^>, ^<, or ^| ). If you need to -use the caret character itself (^), use two in a row (^^). -*/ - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__CharIsWhitespace(char c) -{ - return ((c == ' ') || (c == '\t')); -} - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__CharNeedsQuotesOnUnix(char c) -{ - return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') || - (c == '&') || (c == '$') || (c == '(') || (c == ')') || - (c == '~') || (c == '<') || (c == '>') || (c == '|') || - (c == '*') || (c == '^') || (c == '\\')); -} - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__CharNeedsQuotesOnWindows(char c) -{ - return ((c == '\'') || (c == '#') || (c == '&') || - (c == '<') || (c == '>') || (c == '|') || (c == '^')); -} - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__CharNeedsQuotes(char c, int isUnix, int flags) -{ - /* On Windows the built-in command shell echo never needs quotes. */ - if(!isUnix && (flags & kwsysSystem_Shell_Flag_EchoWindows)) - { - return 0; - } - - /* On all platforms quotes are needed to preserve whitespace. */ - if(kwsysSystem_Shell__CharIsWhitespace(c)) - { - return 1; - } - - if(isUnix) - { - /* On UNIX several special characters need quotes to preserve them. */ - if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c)) - { - return 1; - } - } - else - { - /* On Windows several special characters need quotes to preserve them. */ - if(kwsysSystem_Shell__CharNeedsQuotesOnWindows(c)) - { - return 1; - } - } - return 0; -} - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__CharIsMakeVariableName(char c) -{ - return c && (c == '_' || isalpha(((int)c))); -} - -/*--------------------------------------------------------------------------*/ -static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c) -{ - while(*c == '$' && *(c+1) == '(') - { - const char* skip = c+2; - while(kwsysSystem_Shell__CharIsMakeVariableName(*skip)) - { - ++skip; - } - if(*skip == ')') - { - c = skip+1; - } - else - { - break; - } - } - return c; -} - -/* -Allowing make variable replacements opens a can of worms. Sometimes -they should be quoted and sometimes not. Sometimes their replacement -values are already quoted or contain escapes. - -Some Visual Studio variables cause problems. In order to pass the -referenced value with spaces the reference must be quoted. If the -variable value ends in a backslash then it will escape the ending -quote! In order to make the ending backslash appear we need this: - - "$(InputDir)\" - -However if there is not a trailing backslash then this will put a -quote in the value so we need: - - "$(InputDir)" - -This macro decides whether we quote an argument just because it -contains a make variable reference. This should be replaced with a -flag later when we understand applications of this better. -*/ -#define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0 - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix, - int flags) -{ - /* The empty string needs quotes. */ - if(!*in) - { - return 1; - } - - /* Scan the string for characters that require quoting. */ - { - const char* c; - for(c=in; *c; ++c) - { - /* Look for $(MAKEVAR) syntax if requested. */ - if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables) - { -#if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES - const char* skip = kwsysSystem_Shell__SkipMakeVariables(c); - if(skip != c) - { - /* We need to quote make variable references to preserve the - string with contents substituted in its place. */ - return 1; - } -#else - /* Skip over the make variable references if any are present. */ - c = kwsysSystem_Shell__SkipMakeVariables(c); - - /* Stop if we have reached the end of the string. */ - if(!*c) - { - break; - } -#endif - } - - /* Check whether this character needs quotes. */ - if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags)) - { - return 1; - } - } - } - - /* On Windows some single character arguments need quotes. */ - if(!isUnix && *in && !*(in+1)) - { - char c = *in; - if((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) - { - return 1; - } - } - - return 0; -} - -/*--------------------------------------------------------------------------*/ -static int kwsysSystem_Shell__GetArgumentSize(const char* in, - int isUnix, int flags) -{ - /* Start with the length of the original argument, plus one for - either a terminating null or a separating space. */ - int size = (int)strlen(in) + 1; - - /* String iterator. */ - const char* c; - - /* Keep track of how many backslashes have been encountered in a row. */ - int windows_backslashes = 0; - - /* Scan the string for characters that require escaping or quoting. */ - for(c=in; *c; ++c) - { - /* Look for $(MAKEVAR) syntax if requested. */ - if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables) - { - /* Skip over the make variable references if any are present. */ - c = kwsysSystem_Shell__SkipMakeVariables(c); - - /* Stop if we have reached the end of the string. */ - if(!*c) - { - break; - } - } - - /* Check whether this character needs escaping for the shell. */ - if(isUnix) - { - /* On Unix a few special characters need escaping even inside a - quoted argument. */ - if(*c == '\\' || *c == '"' || *c == '`' || *c == '$') - { - /* This character needs a backslash to escape it. */ - ++size; - } - } - else if(flags & kwsysSystem_Shell_Flag_EchoWindows) - { - /* On Windows the built-in command shell echo never needs escaping. */ - } - else - { - /* On Windows only backslashes and double-quotes need escaping. */ - if(*c == '\\') - { - /* Found a backslash. It may need to be escaped later. */ - ++windows_backslashes; - } - else if(*c == '"') - { - /* Found a double-quote. We need to escape it and all - immediately preceding backslashes. */ - size += windows_backslashes + 1; - windows_backslashes = 0; - } - else - { - /* Found another character. This eliminates the possibility - that any immediately preceding backslashes will be - escaped. */ - windows_backslashes = 0; - } - } - - /* Check whether this character needs escaping for a make tool. */ - if(*c == '$') - { - if(flags & kwsysSystem_Shell_Flag_Make) - { - /* In Makefiles a dollar is written $$ so we need one extra - character. */ - ++size; - } - else if(flags & kwsysSystem_Shell_Flag_VSIDE) - { - /* In a VS IDE a dollar is written "$" so we need two extra - characters. */ - size += 2; - } - } - else if(*c == '#') - { - if((flags & kwsysSystem_Shell_Flag_Make) && - (flags & kwsysSystem_Shell_Flag_WatcomWMake)) - { - /* In Watcom WMake makefiles a pound is written $# so we need - one extra character. */ - ++size; - } - } - else if(*c == '%') - { - if((flags & kwsysSystem_Shell_Flag_VSIDE) || - ((flags & kwsysSystem_Shell_Flag_Make) && - ((flags & kwsysSystem_Shell_Flag_MinGWMake) || - (flags & kwsysSystem_Shell_Flag_NMake)))) - { - /* In the VS IDE, NMake, or MinGW make a percent is written %% - so we need one extra characters. */ - size += 1; - } - } - else if(*c == ';') - { - if(flags & kwsysSystem_Shell_Flag_VSIDE) - { - /* In a VS IDE a semicolon is written ";" so we need two extra - characters. */ - size += 2; - } - } - } - - /* Check whether the argument needs surrounding quotes. */ - if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags)) - { - /* Surrounding quotes are needed. Allocate space for them. */ - if((flags & kwsysSystem_Shell_Flag_WatcomQuote) && (isUnix)) - { - size += 2; - } - size += 2; - - /* We must escape all ending backslashes when quoting on windows. */ - size += windows_backslashes; - } - - return size; -} - -/*--------------------------------------------------------------------------*/ -static char* kwsysSystem_Shell__GetArgument(const char* in, char* out, - int isUnix, int flags) -{ - /* String iterator. */ - const char* c; - - /* Keep track of how many backslashes have been encountered in a row. */ - int windows_backslashes = 0; - - /* Whether the argument must be quoted. */ - int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags); - if(needQuotes) - { - /* Add the opening quote for this argument. */ - if(flags & kwsysSystem_Shell_Flag_WatcomQuote) - { - if(isUnix) - { - *out++ = '"'; - } - *out++ = '\''; - } - else - { - *out++ = '"'; - } - } - - /* Scan the string for characters that require escaping or quoting. */ - for(c=in; *c; ++c) - { - /* Look for $(MAKEVAR) syntax if requested. */ - if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables) - { - const char* skip = kwsysSystem_Shell__SkipMakeVariables(c); - if(skip != c) - { - /* Copy to the end of the make variable references. */ - while(c != skip) - { - *out++ = *c++; - } - - /* The make variable reference eliminates any escaping needed - for preceding backslashes. */ - windows_backslashes = 0; - - /* Stop if we have reached the end of the string. */ - if(!*c) - { - break; - } - } - } - - /* Check whether this character needs escaping for the shell. */ - if(isUnix) - { - /* On Unix a few special characters need escaping even inside a - quoted argument. */ - if(*c == '\\' || *c == '"' || *c == '`' || *c == '$') - { - /* This character needs a backslash to escape it. */ - *out++ = '\\'; - } - } - else if(flags & kwsysSystem_Shell_Flag_EchoWindows) - { - /* On Windows the built-in command shell echo never needs escaping. */ - } - else - { - /* On Windows only backslashes and double-quotes need escaping. */ - if(*c == '\\') - { - /* Found a backslash. It may need to be escaped later. */ - ++windows_backslashes; - } - else if(*c == '"') - { - /* Found a double-quote. Escape all immediately preceding - backslashes. */ - while(windows_backslashes > 0) - { - --windows_backslashes; - *out++ = '\\'; - } - - /* Add the backslash to escape the double-quote. */ - *out++ = '\\'; - } - else - { - /* We encountered a normal character. This eliminates any - escaping needed for preceding backslashes. */ - windows_backslashes = 0; - } - } - - /* Check whether this character needs escaping for a make tool. */ - if(*c == '$') - { - if(flags & kwsysSystem_Shell_Flag_Make) - { - /* In Makefiles a dollar is written $$. The make tool will - replace it with just $ before passing it to the shell. */ - *out++ = '$'; - *out++ = '$'; - } - else if(flags & kwsysSystem_Shell_Flag_VSIDE) - { - /* In a VS IDE a dollar is written "$". If this is written in - an un-quoted argument it starts a quoted segment, inserts - the $ and ends the segment. If it is written in a quoted - argument it ends quoting, inserts the $ and restarts - quoting. Either way the $ is isolated from surrounding - text to avoid looking like a variable reference. */ - *out++ = '"'; - *out++ = '$'; - *out++ = '"'; - } - else - { - /* Otherwise a dollar is written just $. */ - *out++ = '$'; - } - } - else if(*c == '#') - { - if((flags & kwsysSystem_Shell_Flag_Make) && - (flags & kwsysSystem_Shell_Flag_WatcomWMake)) - { - /* In Watcom WMake makefiles a pound is written $#. The make - tool will replace it with just # before passing it to the - shell. */ - *out++ = '$'; - *out++ = '#'; - } - else - { - /* Otherwise a pound is written just #. */ - *out++ = '#'; - } - } - else if(*c == '%') - { - if((flags & kwsysSystem_Shell_Flag_VSIDE) || - ((flags & kwsysSystem_Shell_Flag_Make) && - ((flags & kwsysSystem_Shell_Flag_MinGWMake) || - (flags & kwsysSystem_Shell_Flag_NMake)))) - { - /* In the VS IDE, NMake, or MinGW make a percent is written %%. */ - *out++ = '%'; - *out++ = '%'; - } - else - { - /* Otherwise a percent is written just %. */ - *out++ = '%'; - } - } - else if(*c == ';') - { - if(flags & kwsysSystem_Shell_Flag_VSIDE) - { - /* In a VS IDE a semicolon is written ";". If this is written - in an un-quoted argument it starts a quoted segment, - inserts the ; and ends the segment. If it is written in a - quoted argument it ends quoting, inserts the ; and restarts - quoting. Either way the ; is isolated. */ - *out++ = '"'; - *out++ = ';'; - *out++ = '"'; - } - else - { - /* Otherwise a semicolon is written just ;. */ - *out++ = ';'; - } - } - else - { - /* Store this character. */ - *out++ = *c; - } - } - - if(needQuotes) - { - /* Add enough backslashes to escape any trailing ones. */ - while(windows_backslashes > 0) - { - --windows_backslashes; - *out++ = '\\'; - } - - /* Add the closing quote for this argument. */ - if(flags & kwsysSystem_Shell_Flag_WatcomQuote) - { - *out++ = '\''; - if(isUnix) - { - *out++ = '"'; - } - } - else - { - *out++ = '"'; - } - } - - /* Store a terminating null without incrementing. */ - *out = 0; - - return out; -} - -/*--------------------------------------------------------------------------*/ -char* kwsysSystem_Shell_GetArgumentForWindows(const char* in, - char* out, - int flags) -{ - return kwsysSystem_Shell__GetArgument(in, out, 0, flags); -} - -/*--------------------------------------------------------------------------*/ -char* kwsysSystem_Shell_GetArgumentForUnix(const char* in, - char* out, - int flags) -{ - return kwsysSystem_Shell__GetArgument(in, out, 1, flags); -} - -/*--------------------------------------------------------------------------*/ -int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, int flags) -{ - return kwsysSystem_Shell__GetArgumentSize(in, 0, flags); -} - -/*--------------------------------------------------------------------------*/ -int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, int flags) -{ - return kwsysSystem_Shell__GetArgumentSize(in, 1, flags); -} - /*--------------------------------------------------------------------------*/ static int kwsysSystem__AppendByte(char* local, char** begin, char** end, diff --git a/Source/kwsys/System.h.in b/Source/kwsys/System.h.in index f21bf0dca..3f3d3f4b0 100644 --- a/Source/kwsys/System.h.in +++ b/Source/kwsys/System.h.in @@ -24,28 +24,6 @@ #endif #if !@KWSYS_NAMESPACE@_NAME_IS_KWSYS # define kwsysSystem_Parse_CommandForUnix kwsys_ns(System_Parse_CommandForUnix) -# define kwsysSystem_Shell_GetArgumentForWindows kwsys_ns(System_Shell_GetArgumentForWindows) -# define kwsysSystem_Shell_GetArgumentForUnix kwsys_ns(System_Shell_GetArgumentForUnix) -# define kwsysSystem_Shell_GetArgumentSizeForWindows kwsys_ns(System_Shell_GetArgumentSizeForWindows) -# define kwsysSystem_Shell_GetArgumentSizeForUnix kwsys_ns(System_Shell_GetArgumentSizeForUnix) -# define kwsysSystem_Shell_Flag_e kwsys_ns(System_Shell_Flag_e) -# define kwsysSystem_Shell_Flag_Make kwsys_ns(System_Shell_Flag_Make) -# define kwsysSystem_Shell_Flag_VSIDE kwsys_ns(System_Shell_Flag_VSIDE) -# define kwsysSystem_Shell_Flag_EchoWindows kwsys_ns(System_Shell_Flag_EchoWindows) -# define kwsysSystem_Shell_Flag_WatcomWMake kwsys_ns(System_Shell_Flag_WatcomWMake) -# define kwsysSystem_Shell_Flag_MinGWMake kwsys_ns(System_Shell_Flag_MinGWMake) -# define kwsysSystem_Shell_Flag_NMake kwsys_ns(System_Shell_Flag_NMake) -# define kwsysSystem_Shell_Flag_AllowMakeVariables kwsys_ns(System_Shell_Flag_AllowMakeVariables) -# define kwsysSystem_Shell_Flag_WatcomQuote kwsys_ns(System_Shell_Flag_WatcomQuote) -#endif - -#ifdef __VMS -#define @KWSYS_NAMESPACE@System_Shell_GetArgumentForUnix \ - @KWSYS_NAMESPACE@System_Shell_UnixGA -#define @KWSYS_NAMESPACE@System_Shell_GetArgumentSizeForUnix \ - @KWSYS_NAMESPACE@System_Shell_UnixGAS -#define @KWSYS_NAMESPACE@System_Shell_GetArgumentForWindows \ - @KWSYS_NAMESPACE@System_Shell_WindowsGA #endif #if defined(__cplusplus) @@ -53,69 +31,6 @@ extern "C" { #endif -/** - * Transform the given command line argument for use in a Windows or - * Unix shell. Returns a pointer to the end of the command line - * argument in the provided output buffer. Flags may be passed to - * modify the generated quoting and escape sequences to work under - * alternative environments. - */ -kwsysEXPORT char* kwsysSystem_Shell_GetArgumentForWindows(const char* in, - char* out, - int flags); -kwsysEXPORT char* kwsysSystem_Shell_GetArgumentForUnix(const char* in, - char* out, - int flags); - -/** - * Compute the size of the buffer required to store the output from - * kwsysSystem_Shell_GetArgumentForWindows or - * kwsysSystem_Shell_GetArgumentForUnix. The flags passed must be - * identical between the two calls. - */ -kwsysEXPORT int kwsysSystem_Shell_GetArgumentSizeForWindows(const char* in, - int flags); -kwsysEXPORT int kwsysSystem_Shell_GetArgumentSizeForUnix(const char* in, - int flags); - -/** - * Flags to pass to kwsysSystem_Shell_GetArgumentForWindows or - * kwsysSystem_Shell_GetArgumentForUnix. These modify the generated - * quoting and escape sequences to work under alternative - * environments. - */ -enum kwsysSystem_Shell_Flag_e -{ - /** The target shell is in a makefile. */ - kwsysSystem_Shell_Flag_Make = (1<<0), - - /** The target shell is in a VS project file. Do not use with - Shell_Flag_Make. */ - kwsysSystem_Shell_Flag_VSIDE = (1<<1), - - /** In a windows shell the argument is being passed to "echo". */ - kwsysSystem_Shell_Flag_EchoWindows = (1<<2), - - /** The target shell is in a Watcom WMake makefile. */ - kwsysSystem_Shell_Flag_WatcomWMake = (1<<3), - - /** The target shell is in a MinGW Make makefile. */ - kwsysSystem_Shell_Flag_MinGWMake = (1<<4), - - /** The target shell is in a NMake makefile. */ - kwsysSystem_Shell_Flag_NMake = (1<<5), - - /** Make variable reference syntax $(MAKEVAR) should not be escaped - to allow a build tool to replace it. Replacement values - containing spaces, quotes, backslashes, or other - non-alphanumeric characters that have significance to some makes - or shells produce undefined behavior. */ - kwsysSystem_Shell_Flag_AllowMakeVariables = (1<<6), - - /** The target shell quoting uses extra single Quotes for Watcom tools. */ - kwsysSystem_Shell_Flag_WatcomQuote = (1<<7) -}; - /** * Parse a unix-style command line string into separate arguments. * @@ -148,19 +63,6 @@ kwsysEXPORT char** kwsysSystem_Parse_CommandForUnix(const char* command, # undef kwsysEXPORT # if !defined(KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS # undef kwsysSystem_Parse_CommandForUnix -# undef kwsysSystem_Shell_GetArgumentForWindows -# undef kwsysSystem_Shell_GetArgumentForUnix -# undef kwsysSystem_Shell_GetArgumentSizeForWindows -# undef kwsysSystem_Shell_GetArgumentSizeForUnix -# undef kwsysSystem_Shell_Flag_e -# undef kwsysSystem_Shell_Flag_Make -# undef kwsysSystem_Shell_Flag_VSIDE -# undef kwsysSystem_Shell_Flag_EchoWindows -# undef kwsysSystem_Shell_Flag_WatcomWMake -# undef kwsysSystem_Shell_Flag_MinGWMake -# undef kwsysSystem_Shell_Flag_NMake -# undef kwsysSystem_Shell_Flag_AllowMakeVariables -# undef kwsysSystem_Shell_Flag_WatcomQuote # endif #endif