[cmake-commits] king committed cmSystemTools.cxx 1.333 1.334 cmSystemTools.h 1.134 1.135

cmake-commits at cmake.org cmake-commits at cmake.org
Thu Feb 1 11:45:39 EST 2007


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv29536

Modified Files:
	cmSystemTools.cxx cmSystemTools.h 
Log Message:
ENH: Added EscapeWindowsShellArgument and ParseWindowsCommandLine methods to cmSystemTools.


Index: cmSystemTools.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.h,v
retrieving revision 1.134
retrieving revision 1.135
diff -u -d -r1.134 -r1.135
--- cmSystemTools.h	12 May 2006 17:53:21 -0000	1.134
+++ cmSystemTools.h	1 Feb 2007 16:45:37 -0000	1.135
@@ -211,7 +211,16 @@
    * Parse arguments out of a single string 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 DisableMessages() { s_DisableMessages = true; }
   static void DisableRunCommandOutput() {s_DisableRunCommandOutput = true; }

Index: cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.333
retrieving revision 1.334
diff -u -d -r1.333 -r1.334
--- cmSystemTools.cxx	14 Dec 2006 15:03:25 -0000	1.333
+++ cmSystemTools.cxx	1 Feb 2007 16:45:37 -0000	1.334
@@ -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 @@
           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;



More information about the Cmake-commits mailing list