diff options
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 3144f5a..7af78fb 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -21,6 +21,7 @@ #include <cmsys/RegularExpression.hxx> #include <cmsys/Directory.hxx> +#include <cmsys/System.h> // support for realpath call #ifndef _WIN32 @@ -370,6 +371,95 @@ bool cmSystemTools::IsOff(const char* val) v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE"); } +//---------------------------------------------------------------------------- +void cmSystemTools::ParseWindowsCommandLine(const char* command, + std::vector<std::string>& args) +{ + // See the MSDN document "Parsing C Command-Line Arguments" at + // http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx for rules + // of parsing the windows command line. + + bool in_argument = false; + bool in_quotes = false; + int backslashes = 0; + std::string arg; + for(const char* c = command;*c; ++c) + { + if(*c == '\\') + { + ++backslashes; + in_argument = true; + } + else if(*c == '"') + { + int backslash_pairs = backslashes >> 1; + int backslash_escaped = backslashes & 1; + arg.append(backslash_pairs, '\\'); + backslashes = 0; + if(backslash_escaped) + { + /* An odd number of backslashes precede this quote. + It is escaped. */ + arg.append(1, '"'); + } + else + { + /* An even number of backslashes precede this quote. + It is not escaped. */ + in_quotes = !in_quotes; + } + in_argument = true; + } + else + { + arg.append(backslashes, '\\'); + backslashes = 0; + if(isspace(*c)) + { + if(in_quotes) + { + arg.append(1, *c); + } + else if(in_argument) + { + args.push_back(arg); + arg = ""; + in_argument = false; + } + } + else + { + in_argument = true; + arg.append(1, *c); + } + } + } + arg.append(backslashes, '\\'); + if(in_argument) + { + args.push_back(arg); + } +} + +std::string cmSystemTools::EscapeWindowsShellArgument(const char* arg, + int shell_flags) +{ + char local_buffer[1024]; + char* buffer = local_buffer; + int size = cmsysSystem_Shell_GetArgumentSizeForWindows(arg, shell_flags); + if(size > 1024) + { + buffer = new char[size]; + } + cmsysSystem_Shell_GetArgumentForWindows(arg, buffer, shell_flags); + std::string result(buffer); + if(buffer != local_buffer) + { + delete [] buffer; + } + return result; +} + std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command) { std::vector<cmStdString> args; |