summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/System.c
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2006-09-21 15:49:36 (GMT)
committerBrad King <brad.king@kitware.com>2006-09-21 15:49:36 (GMT)
commit683346fa3d067fd7ac144446bd6a4c673dfec222 (patch)
tree27121cced64d5fd4d9797122b7d013896d6d8dde /Source/kwsys/System.c
parentb6e8574ab191a1ff611454412ecdfaf0154990c7 (diff)
downloadCMake-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.
Diffstat (limited to 'Source/kwsys/System.c')
-rw-r--r--Source/kwsys/System.c166
1 files changed, 166 insertions, 0 deletions
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;
+}