diff options
author | Brad King <brad.king@kitware.com> | 2006-09-21 15:49:36 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2006-09-21 15:49:36 (GMT) |
commit | 683346fa3d067fd7ac144446bd6a4c673dfec222 (patch) | |
tree | 27121cced64d5fd4d9797122b7d013896d6d8dde | |
parent | b6e8574ab191a1ff611454412ecdfaf0154990c7 (diff) | |
download | CMake-683346fa3d067fd7ac144446bd6a4c673dfec222.zip CMake-683346fa3d067fd7ac144446bd6a4c673dfec222.tar.gz CMake-683346fa3d067fd7ac144446bd6a4c673dfec222.tar.bz2 |
ENH: Adding 'System' component of C sources to hold system tools written in C. Moved windows shell command line argument escaping code to kwsysSystem_Windows_ShellArgument and kwsysSystem_Windows_ShellArgumentSize.
-rw-r--r-- | Source/kwsys/CMakeLists.txt | 8 | ||||
-rw-r--r-- | Source/kwsys/ProcessWin32.c | 135 | ||||
-rw-r--r-- | Source/kwsys/System.c | 166 | ||||
-rw-r--r-- | Source/kwsys/System.h.in | 62 |
4 files changed, 241 insertions, 130 deletions
diff --git a/Source/kwsys/CMakeLists.txt b/Source/kwsys/CMakeLists.txt index b249575..4e664b2 100644 --- a/Source/kwsys/CMakeLists.txt +++ b/Source/kwsys/CMakeLists.txt @@ -123,6 +123,7 @@ IF(KWSYS_STANDALONE OR CMake_SOURCE_DIR) SET(KWSYS_USE_Process 1) SET(KWSYS_USE_RegularExpression 1) SET(KWSYS_USE_Registry 1) + SET(KWSYS_USE_System 1) SET(KWSYS_USE_SystemTools 1) SET(KWSYS_USE_CommandLineArguments 1) SET(KWSYS_USE_FundamentalType 1) @@ -580,6 +581,9 @@ IF(KWSYS_USE_Glob) SET(KWSYS_USE_SystemTools 1) SET(KWSYS_USE_RegularExpression 1) ENDIF(KWSYS_USE_Glob) +IF(KWSYS_USE_Process) + SET(KWSYS_USE_System 1) +ENDIF(KWSYS_USE_Process) # Add selected C++ classes. SET(cppclasses @@ -593,7 +597,7 @@ FOREACH(cpp ${cppclasses}) ENDFOREACH(cpp) # Add selected C components. -FOREACH(c Process Base64 FundamentalType Terminal) +FOREACH(c Process Base64 FundamentalType Terminal System) IF(KWSYS_USE_${c}) SET(KWSYS_H_FILES ${KWSYS_H_FILES} ${c}) ENDIF(KWSYS_USE_${c}) @@ -621,7 +625,7 @@ IF(KWSYS_USE_Process) ENDIF(KWSYS_USE_Process) # Add selected C sources. -FOREACH(c Base64 Terminal) +FOREACH(c Base64 Terminal System) IF(KWSYS_USE_${c}) SET(KWSYS_C_SRCS ${KWSYS_C_SRCS} ${c}.c) ENDIF(KWSYS_USE_${c}) diff --git a/Source/kwsys/ProcessWin32.c b/Source/kwsys/ProcessWin32.c index 089c955..1fe6cb0 100644 --- a/Source/kwsys/ProcessWin32.c +++ b/Source/kwsys/ProcessWin32.c @@ -13,11 +13,13 @@ =========================================================================*/ #include "kwsysPrivate.h" #include KWSYS_HEADER(Process.h) +#include KWSYS_HEADER(System.h) /* Work-around CMake dependency scanning limitation. This must duplicate the above list of headers. */ #if 0 # include "Process.h.in" +# include "System.h.in" #endif /* @@ -2085,59 +2087,9 @@ int kwsysProcessComputeCommandLength(kwsysProcess* cp, char const* const* arg; for(arg = command; *arg; ++arg) { - /* Keep track of how many backslashes have been encountered in a - row in this argument. */ - int backslashes = 0; - int spaces = 0; - const char* c; - - /* Scan the string for spaces. If there are no spaces, we can - pass the argument verbatim. */ - for(c=*arg; *c; ++c) - { - if(*c == ' ' || *c == '\t') - { - spaces = 1; - break; - } - } - - /* Add the length of the argument, plus 1 for the space - separating the arguments. */ - length += (int)strlen(*arg) + 1; - - if(spaces) - { - /* Add 2 for double quotes since spaces are present. */ - length += 2; - - /* Scan the string to find characters that need escaping. */ - for(c=*arg; *c; ++c) - { - if(*c == '\\') - { - /* Found a backslash. It may need to be escaped later. */ - ++backslashes; - } - else if(*c == '"') - { - /* Found a double-quote. We need to escape it and all - immediately preceding backslashes. */ - length += backslashes + 1; - backslashes = 0; - } - else - { - /* Found another character. This eliminates the possibility - that any immediately preceding backslashes will be - escaped. */ - backslashes = 0; - } - } - - /* We need to escape all ending backslashes. */ - length += backslashes; - } + /* Add the length of this argument. It already includes room + for a separating space or terminating null. */ + length += kwsysSystem_Windows_ShellArgumentSize(*arg); } } @@ -2160,87 +2112,14 @@ void kwsysProcessComputeCommandLine(kwsysProcess* cp, char const* const* arg; for(arg = command; *arg; ++arg) { - /* Keep track of how many backslashes have been encountered in a - row in an argument. */ - int backslashes = 0; - int spaces = 0; - const char* c; - - /* Scan the string for spaces. If there are no spaces, we can - pass the argument verbatim. */ - for(c=*arg; *c; ++c) - { - if(*c == ' ' || *c == '\t') - { - spaces = 1; - break; - } - } - /* Add the separating space if this is not the first argument. */ if(arg != command) { *cmd++ = ' '; } - if(spaces) - { - /* Add the opening double-quote for this argument. */ - *cmd++ = '"'; - - /* Add the characters of the argument, possibly escaping them. */ - for(c=*arg; *c; ++c) - { - if(*c == '\\') - { - /* Found a backslash. It may need to be escaped later. */ - ++backslashes; - *cmd++ = '\\'; - } - else if(*c == '"') - { - /* Add enough backslashes to escape any that preceded the - double-quote. */ - while(backslashes > 0) - { - --backslashes; - *cmd++ = '\\'; - } - - /* Add the backslash to escape the double-quote. */ - *cmd++ = '\\'; - - /* Add the double-quote itself. */ - *cmd++ = '"'; - } - else - { - /* We encountered a normal character. This eliminates any - escaping needed for preceding backslashes. Add the - character. */ - backslashes = 0; - *cmd++ = *c; - } - } - - /* Add enough backslashes to escape any trailing ones. */ - while(backslashes > 0) - { - --backslashes; - *cmd++ = '\\'; - } - - /* Add the closing double-quote for this argument. */ - *cmd++ = '"'; - } - else - { - /* No spaces. Add the argument verbatim. */ - for(c=*arg; *c; ++c) - { - *cmd++ = *c; - } - } + /* Add the current argument. */ + cmd = kwsysSystem_Windows_ShellArgument(*arg, cmd); } /* Add the terminating null character to the command line. */ diff --git a/Source/kwsys/System.c b/Source/kwsys/System.c new file mode 100644 index 0000000..17626aa --- /dev/null +++ b/Source/kwsys/System.c @@ -0,0 +1,166 @@ +/*========================================================================= + + Program: KWSys - Kitware System Library + Module: $RCSfile$ + + Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved. + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "kwsysPrivate.h" +#include KWSYS_HEADER(System.h) + +/* Work-around CMake dependency scanning limitation. This must + duplicate the above list of headers. */ +#if 0 +# include "System.h.in" +#endif + +#include <string.h> /* strlen */ + +/*--------------------------------------------------------------------------*/ +int kwsysSystem_Windows_ShellArgumentSize(const char* in) +{ + /* Start with the length of the original argument, plus one for + either a terminating null or a separating space. */ + int length = (int)strlen(in) + 1; + + /* Keep track of how many backslashes have been encountered in a row. */ + int backslashes = 0; + + /* Scan the string for spaces. */ + const char* c; + for(c=in; *c; ++c) + { + if(*c == ' ' || *c == '\t') + { + break; + } + } + + /* If there are no spaces, we do not need any extra length. */ + if(!*c) + { + return length; + } + + /* Add 2 for double quotes since spaces are present. */ + length += 2; + + /* Scan the string to find characters that need escaping. */ + for(c=in; *c; ++c) + { + if(*c == '\\') + { + /* Found a backslash. It may need to be escaped later. */ + ++backslashes; + } + else if(*c == '"') + { + /* Found a double-quote. We need to escape it and all + immediately preceding backslashes. */ + length += backslashes + 1; + backslashes = 0; + } + else + { + /* Found another character. This eliminates the possibility + that any immediately preceding backslashes will be + escaped. */ + backslashes = 0; + } + } + + /* We need to escape all ending backslashes. */ + length += backslashes; + + return length; +} + +/*--------------------------------------------------------------------------*/ +char* kwsysSystem_Windows_ShellArgument(const char* in, char* out) +{ + /* Keep track of how many backslashes have been encountered in a row. */ + int backslashes = 0; + + /* Scan the string for spaces. */ + const char* c; + for(c=in; *c; ++c) + { + if(*c == ' ' || *c == '\t') + { + break; + } + } + + /* If there are no spaces, we can pass the argument verbatim. */ + if(!*c) + { + /* Just copy the string. */ + for(c=in; *c; ++c) + { + *out++ = *c; + } + + /* Store a terminating null without incrementing. */ + *out = 0; + return out; + } + + /* Add the opening double-quote for this argument. */ + *out++ = '"'; + + /* Add the characters of the argument, possibly escaping them. */ + for(c=in; *c; ++c) + { + if(*c == '\\') + { + /* Found a backslash. It may need to be escaped later. */ + ++backslashes; + *out++ = '\\'; + } + else if(*c == '"') + { + /* Add enough backslashes to escape any that preceded the + double-quote. */ + while(backslashes > 0) + { + --backslashes; + *out++ = '\\'; + } + + /* Add the backslash to escape the double-quote. */ + *out++ = '\\'; + + /* Add the double-quote itself. */ + *out++ = '"'; + } + else + { + /* We encountered a normal character. This eliminates any + escaping needed for preceding backslashes. Add the + character. */ + backslashes = 0; + *out++ = *c; + } + } + + /* Add enough backslashes to escape any trailing ones. */ + while(backslashes > 0) + { + --backslashes; + *out++ = '\\'; + } + + /* Add the closing double-quote for this argument. */ + *out++ = '"'; + + /* Store a terminating null without incrementing. */ + *out = 0; + + return out; +} diff --git a/Source/kwsys/System.h.in b/Source/kwsys/System.h.in new file mode 100644 index 0000000..db4859e --- /dev/null +++ b/Source/kwsys/System.h.in @@ -0,0 +1,62 @@ +/*========================================================================= + + Program: KWSys - Kitware System Library + Module: $RCSfile$ + + Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved. + See Copyright.txt or http://www.kitware.com/Copyright.htm for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef @KWSYS_NAMESPACE@_System_h +#define @KWSYS_NAMESPACE@_System_h + +#include <@KWSYS_NAMESPACE@/Configure.h> + +/* Redefine all public interface symbol names to be in the proper + namespace. These macros are used internally to kwsys only, and are + not visible to user code. Use kwsysHeaderDump.pl to reproduce + these macros after making changes to the interface. */ +#if !defined(KWSYS_NAMESPACE) +# define kwsys_ns(x) @KWSYS_NAMESPACE@##x +# define kwsysEXPORT @KWSYS_NAMESPACE@_EXPORT +#endif +#define kwsysSystem_Windows_ShellArgument kwsys_ns(System_Windows_ShellArgument) +#define kwsysSystem_Windows_ShellArgumentSize kwsys_ns(System_Windows_ShellArgumentSize) + +#if defined(__cplusplus) +extern "C" +{ +#endif + +/** + * Escape the given command line argument for use in a windows shell. + * Returns a pointer to the end of the command line argument in the + * given buffer. + */ +kwsysEXPORT char* kwsysSystem_Windows_ShellArgument(const char* in, char* out); + +/** + * Compute the size of the buffer needed to store the result of + * kwsysSystem_Windows_ShellArgument. The return value includes space + * for a null-terminator. + */ +kwsysEXPORT int kwsysSystem_Windows_ShellArgumentSize(const char* in); + +#if defined(__cplusplus) +} /* extern "C" */ +#endif + +/* If we are building a kwsys .c or .cxx file, let it use these macros. + Otherwise, undefine them to keep the namespace clean. */ +#if !defined(KWSYS_NAMESPACE) +# undef kwsys_ns +# undef kwsysEXPORT +# undef kwsysSystem_Windows_ShellArgument +# undef kwsysSystem_Windows_ShellArgumentSize +#endif + +#endif |