ENH: Added EscapeWindowsShellArgument and ParseWindowsCommandLine methods to cmSystemTools.

This commit is contained in:
Brad King 2007-02-01 11:45:37 -05:00
parent f46704fefe
commit d37abb6b5d
2 changed files with 100 additions and 1 deletions

View File

@ -21,6 +21,7 @@
#include <cmsys/RegularExpression.hxx> #include <cmsys/RegularExpression.hxx>
#include <cmsys/Directory.hxx> #include <cmsys/Directory.hxx>
#include <cmsys/System.h>
// support for realpath call // support for realpath call
#ifndef _WIN32 #ifndef _WIN32
@ -370,6 +371,95 @@ bool cmSystemTools::IsOff(const char* val)
v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE"); 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> cmSystemTools::ParseArguments(const char* command)
{ {
std::vector<cmStdString> args; std::vector<cmStdString> args;

View File

@ -211,7 +211,16 @@ public:
* Parse arguments out of a single string command * Parse arguments out of a single string command
*/ */
static std::vector<cmStdString> ParseArguments(const char* command); static std::vector<cmStdString> ParseArguments(const char* command);
/** Parse arguments out of a windows command line string. */
static void ParseWindowsCommandLine(const char* command,
std::vector<std::string>& args);
/** Compute an escaped version of the given argument for use in a
windows shell. See kwsys/System.h.in for details. */
static std::string EscapeWindowsShellArgument(const char* arg,
int shell_flags);
static void EnableMessages() { s_DisableMessages = false; } static void EnableMessages() { s_DisableMessages = false; }
static void DisableMessages() { s_DisableMessages = true; } static void DisableMessages() { s_DisableMessages = true; }
static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; } static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }