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.
This commit is contained in:
parent
b6e8574ab1
commit
683346fa3d
@ -123,6 +123,7 @@ IF(KWSYS_STANDALONE OR CMake_SOURCE_DIR)
|
|||||||
SET(KWSYS_USE_Process 1)
|
SET(KWSYS_USE_Process 1)
|
||||||
SET(KWSYS_USE_RegularExpression 1)
|
SET(KWSYS_USE_RegularExpression 1)
|
||||||
SET(KWSYS_USE_Registry 1)
|
SET(KWSYS_USE_Registry 1)
|
||||||
|
SET(KWSYS_USE_System 1)
|
||||||
SET(KWSYS_USE_SystemTools 1)
|
SET(KWSYS_USE_SystemTools 1)
|
||||||
SET(KWSYS_USE_CommandLineArguments 1)
|
SET(KWSYS_USE_CommandLineArguments 1)
|
||||||
SET(KWSYS_USE_FundamentalType 1)
|
SET(KWSYS_USE_FundamentalType 1)
|
||||||
@ -580,6 +581,9 @@ IF(KWSYS_USE_Glob)
|
|||||||
SET(KWSYS_USE_SystemTools 1)
|
SET(KWSYS_USE_SystemTools 1)
|
||||||
SET(KWSYS_USE_RegularExpression 1)
|
SET(KWSYS_USE_RegularExpression 1)
|
||||||
ENDIF(KWSYS_USE_Glob)
|
ENDIF(KWSYS_USE_Glob)
|
||||||
|
IF(KWSYS_USE_Process)
|
||||||
|
SET(KWSYS_USE_System 1)
|
||||||
|
ENDIF(KWSYS_USE_Process)
|
||||||
|
|
||||||
# Add selected C++ classes.
|
# Add selected C++ classes.
|
||||||
SET(cppclasses
|
SET(cppclasses
|
||||||
@ -593,7 +597,7 @@ FOREACH(cpp ${cppclasses})
|
|||||||
ENDFOREACH(cpp)
|
ENDFOREACH(cpp)
|
||||||
|
|
||||||
# Add selected C components.
|
# Add selected C components.
|
||||||
FOREACH(c Process Base64 FundamentalType Terminal)
|
FOREACH(c Process Base64 FundamentalType Terminal System)
|
||||||
IF(KWSYS_USE_${c})
|
IF(KWSYS_USE_${c})
|
||||||
SET(KWSYS_H_FILES ${KWSYS_H_FILES} ${c})
|
SET(KWSYS_H_FILES ${KWSYS_H_FILES} ${c})
|
||||||
ENDIF(KWSYS_USE_${c})
|
ENDIF(KWSYS_USE_${c})
|
||||||
@ -621,7 +625,7 @@ IF(KWSYS_USE_Process)
|
|||||||
ENDIF(KWSYS_USE_Process)
|
ENDIF(KWSYS_USE_Process)
|
||||||
|
|
||||||
# Add selected C sources.
|
# Add selected C sources.
|
||||||
FOREACH(c Base64 Terminal)
|
FOREACH(c Base64 Terminal System)
|
||||||
IF(KWSYS_USE_${c})
|
IF(KWSYS_USE_${c})
|
||||||
SET(KWSYS_C_SRCS ${KWSYS_C_SRCS} ${c}.c)
|
SET(KWSYS_C_SRCS ${KWSYS_C_SRCS} ${c}.c)
|
||||||
ENDIF(KWSYS_USE_${c})
|
ENDIF(KWSYS_USE_${c})
|
||||||
|
@ -13,11 +13,13 @@
|
|||||||
=========================================================================*/
|
=========================================================================*/
|
||||||
#include "kwsysPrivate.h"
|
#include "kwsysPrivate.h"
|
||||||
#include KWSYS_HEADER(Process.h)
|
#include KWSYS_HEADER(Process.h)
|
||||||
|
#include KWSYS_HEADER(System.h)
|
||||||
|
|
||||||
/* Work-around CMake dependency scanning limitation. This must
|
/* Work-around CMake dependency scanning limitation. This must
|
||||||
duplicate the above list of headers. */
|
duplicate the above list of headers. */
|
||||||
#if 0
|
#if 0
|
||||||
# include "Process.h.in"
|
# include "Process.h.in"
|
||||||
|
# include "System.h.in"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -2085,59 +2087,9 @@ int kwsysProcessComputeCommandLength(kwsysProcess* cp,
|
|||||||
char const* const* arg;
|
char const* const* arg;
|
||||||
for(arg = command; *arg; ++arg)
|
for(arg = command; *arg; ++arg)
|
||||||
{
|
{
|
||||||
/* Keep track of how many backslashes have been encountered in a
|
/* Add the length of this argument. It already includes room
|
||||||
row in this argument. */
|
for a separating space or terminating null. */
|
||||||
int backslashes = 0;
|
length += kwsysSystem_Windows_ShellArgumentSize(*arg);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2160,87 +2112,14 @@ void kwsysProcessComputeCommandLine(kwsysProcess* cp,
|
|||||||
char const* const* arg;
|
char const* const* arg;
|
||||||
for(arg = command; *arg; ++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. */
|
/* Add the separating space if this is not the first argument. */
|
||||||
if(arg != command)
|
if(arg != command)
|
||||||
{
|
{
|
||||||
*cmd++ = ' ';
|
*cmd++ = ' ';
|
||||||
}
|
}
|
||||||
|
|
||||||
if(spaces)
|
/* Add the current argument. */
|
||||||
{
|
cmd = kwsysSystem_Windows_ShellArgument(*arg, cmd);
|
||||||
/* 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 terminating null character to the command line. */
|
/* Add the terminating null character to the command line. */
|
||||||
|
166
Source/kwsys/System.c
Normal file
166
Source/kwsys/System.c
Normal file
@ -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;
|
||||||
|
}
|
62
Source/kwsys/System.h.in
Normal file
62
Source/kwsys/System.h.in
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user