Commit 083f7a8f authored by Kitware Robot's avatar Kitware Robot Committed by Brad King
Browse files

KWSys 2015-07-10 (c9336bcf)

Extract upstream KWSys using the following shell commands.

$ git archive --prefix=upstream-kwsys/ c9336bcf | tar x
$ git shortlog --no-merges --abbrev=8 --format='%h %s' b1d560a0..c9336bcf
Brad King (7):
      61e0419f SystemTools: Teach RemoveFile to tolerate missing file
      d217407c SystemTools: Fix preprocessor check WIN32 => _WIN32
      1f19c187 CONTRIBUTING: Spell "offered" correctly
      8504e845 Process: Simplify Windows command line construction
      d3ba91e1 System: Drop Shell_* APIs
      86a24794 SystemTools: Fix GetActualCaseForPath drive letter case handling
      c9336bcf SystemTools: Optimize GetActualCaseForPath memoization

Matt McCormick (1):
      9a6b7c3f cmake: Set CMP0056 to NEW

Zack Galbreath (1):
      8533a79b SystemInformation: Add GetLoadAverage() method
parent d9dea801
......@@ -88,6 +88,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6.3 FATAL_ERROR)
IF(POLICY CMP0025)
CMAKE_POLICY(SET CMP0025 NEW)
ENDIF()
IF(POLICY CMP0056)
CMAKE_POLICY(SET CMP0056 NEW)
ENDIF()
#-----------------------------------------------------------------------------
# If a namespace is not specified, use "kwsys" and enable testing.
......@@ -783,6 +786,15 @@ IF(KWSYS_USE_SystemInformation)
SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY
COMPILE_DEFINITIONS KWSYS_BUILD_SHARED=1)
ENDIF()
IF(UNIX AND NOT CYGWIN)
KWSYS_PLATFORM_CXX_TEST(KWSYS_CXX_HAS_GETLOADAVG
"Checking whether CXX compiler has getloadavg" DIRECT)
IF(KWSYS_CXX_HAS_GETLOADAVG)
SET_PROPERTY(SOURCE SystemInformation.cxx APPEND PROPERTY
COMPILE_DEFINITIONS KWSYS_CXX_HAS_GETLOADAVG=1)
ENDIF()
ENDIF()
ENDIF()
#-----------------------------------------------------------------------------
......
......@@ -29,7 +29,7 @@ License
We do not require any formal copyright assignment or contributor license
agreement. Any contributions intentionally sent upstream are presumed
to be offerred under terms of the OSI-approved BSD 3-clause License.
to be offered under terms of the OSI-approved BSD 3-clause License.
See `Copyright.txt`_ for details.
.. _`Copyright.txt`: Copyright.txt
......@@ -11,14 +11,12 @@
============================================================================*/
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Process.h)
#include KWSYS_HEADER(System.h)
#include KWSYS_HEADER(Encoding.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"
# include "Encoding_c.h.in"
#endif
......@@ -120,11 +118,6 @@ static void kwsysProcessSetupPipeNative(HANDLE native, PHANDLE handle);
static void kwsysProcessCleanupHandle(PHANDLE h);
static void kwsysProcessCleanup(kwsysProcess* cp, int error);
static void kwsysProcessCleanErrorMessage(kwsysProcess* cp);
static int kwsysProcessComputeCommandLength(kwsysProcess* cp,
char const* const* command);
static void kwsysProcessComputeCommandLine(kwsysProcess* cp,
char const* const* command,
char* cmd);
static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
kwsysProcessTime* timeoutTime);
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
......@@ -602,37 +595,69 @@ int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command)
}
}
/* We need to construct a single string representing the command
and its arguments. We will surround each argument containing
spaces with double-quotes. Inside a double-quoted argument, we
need to escape double-quotes and all backslashes before them.
We also need to escape backslashes at the end of an argument
because they come before the closing double-quote for the
argument. */
{
/* First determine the length of the final string. */
int length = kwsysProcessComputeCommandLength(cp, command);
/* Allocate enough space for the command. We do not need an extra
byte for the terminating null because we allocated a space for
the first argument that we will not use. */
char* new_cmd = malloc(length);
if(!new_cmd)
if (cp->Verbatim)
{
/* Out of memory. */
/* Copy the verbatim command line into the buffer. */
newCommands[cp->NumberOfCommands] = kwsysEncoding_DupToWide(*command);
}
else
{
/* Encode the arguments so CommandLineToArgvW can decode
them from the command line string in the child. */
char buffer[32768]; /* CreateProcess max command-line length. */
char* end = buffer + sizeof(buffer);
char* out = buffer;
char const* const* a;
for (a = command; *a; ++a)
{
int quote = !**a; /* Quote the empty string. */
int slashes = 0;
char const* c;
if (a != command && out != end) { *out++ = ' '; }
for (c = *a; !quote && *c; ++c)
{ quote = (*c == ' ' || *c == '\t'); }
if (quote && out != end) { *out++ = '"'; }
for (c = *a; *c; ++c)
{
if (*c == '\\')
{
++slashes;
}
else
{
if (*c == '"')
{
// Add n+1 backslashes to total 2n+1 before internal '"'.
while(slashes-- >= 0 && out != end) { *out++ = '\\'; }
}
slashes = 0;
}
if (out != end) { *out++ = *c; }
}
if (quote)
{
// Add n backslashes to total 2n before ending '"'.
while (slashes-- > 0 && out != end) { *out++ = '\\'; }
if (out != end) { *out++ = '"'; }
}
}
if(out != end)
{
*out = '\0';
newCommands[cp->NumberOfCommands] = kwsysEncoding_DupToWide(buffer);
}
else
{
newCommands[cp->NumberOfCommands] = 0;
}
}
if (!newCommands[cp->NumberOfCommands])
{
/* Out of memory or command line too long. */
free(newCommands);
return 0;
}
/* Construct the command line in the allocated buffer. */
kwsysProcessComputeCommandLine(cp, command,
new_cmd);
newCommands[cp->NumberOfCommands] = kwsysEncoding_DupToWide(new_cmd);
free(new_cmd);
}
/* Save the new array of commands. */
free(cp->Commands);
cp->Commands = newCommands;
......@@ -1961,66 +1986,6 @@ void kwsysProcessCleanErrorMessage(kwsysProcess* cp)
}
}
/*--------------------------------------------------------------------------*/
int kwsysProcessComputeCommandLength(kwsysProcess* cp,
char const* const* command)
{
int length = 0;
if(cp->Verbatim)
{
/* Treat the first argument as a verbatim command line. Use its
length directly and add space for the null-terminator. */
length = (int)strlen(*command)+1;
}
else
{
/* Compute the length of the command line when it is converted to
a single string. Space for the null-terminator is allocated by
the whitespace character allocated for the first argument that
will not be used. */
char const* const* arg;
for(arg = command; *arg; ++arg)
{
/* Add the length of this argument. It already includes room
for a separating space or terminating null. */
length += kwsysSystem_Shell_GetArgumentSizeForWindows(*arg, 0);
}
}
return length;
}
/*--------------------------------------------------------------------------*/
void kwsysProcessComputeCommandLine(kwsysProcess* cp,
char const* const* command,
char* cmd)
{
if(cp->Verbatim)
{
/* Copy the verbatim command line into the buffer. */
strcpy(cmd, *command);
}
else
{
/* Construct the command line in the allocated buffer. */
char const* const* arg;
for(arg = command; *arg; ++arg)
{
/* Add the separating space if this is not the first argument. */
if(arg != command)
{
*cmd++ = ' ';
}
/* Add the current argument. */
cmd = kwsysSystem_Shell_GetArgumentForWindows(*arg, cmd, 0);
}
/* Add the terminating null character to the command line. */
*cmd = 0;
}
}
/*--------------------------------------------------------------------------*/
/* Get the time at which either the process or user timeout will
expire. Returns 1 if the user timeout is first, and 0 otherwise. */
......
......@@ -20,8 +20,8 @@
#include <stddef.h> /* ptrdiff_t */
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strlen */
#include <ctype.h> /* isalpha */
#include <string.h> /* memcpy */
#include <ctype.h> /* isspace */
#include <stdio.h>
......@@ -31,587 +31,6 @@ typedef ptrdiff_t kwsysSystem_ptrdiff_t;
typedef int kwsysSystem_ptrdiff_t;
#endif
/*
Notes:
Make variable replacements open a can of worms. Sometimes they should
be quoted and sometimes not. Sometimes their replacement values are
already quoted.
VS variables cause problems. In order to pass the referenced value
with spaces the reference must be quoted. If the variable value ends
in a backslash then it will escape the ending quote! In order to make
the ending backslash appear we need this:
"$(InputDir)\"
However if there is not a trailing backslash then this will put a
quote in the value so we need:
"$(InputDir)"
Make variable references are platform specific so we should probably
just NOT quote them and let the listfile author deal with it.
*/
/*
TODO: For windows echo:
To display a pipe (|) or redirection character (< or >) when using the
echo command, use a caret character immediately before the pipe or
redirection character (for example, ^>, ^<, or ^| ). If you need to
use the caret character itself (^), use two in a row (^^).
*/
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__CharIsWhitespace(char c)
{
return ((c == ' ') || (c == '\t'));
}
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__CharNeedsQuotesOnUnix(char c)
{
return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
(c == '&') || (c == '$') || (c == '(') || (c == ')') ||
(c == '~') || (c == '<') || (c == '>') || (c == '|') ||
(c == '*') || (c == '^') || (c == '\\'));
}
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__CharNeedsQuotesOnWindows(char c)
{
return ((c == '\'') || (c == '#') || (c == '&') ||
(c == '<') || (c == '>') || (c == '|') || (c == '^'));
}
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__CharNeedsQuotes(char c, int isUnix, int flags)
{
/* On Windows the built-in command shell echo never needs quotes. */
if(!isUnix && (flags & kwsysSystem_Shell_Flag_EchoWindows))
{
return 0;
}
/* On all platforms quotes are needed to preserve whitespace. */
if(kwsysSystem_Shell__CharIsWhitespace(c))
{
return 1;
}
if(isUnix)
{
/* On UNIX several special characters need quotes to preserve them. */
if(kwsysSystem_Shell__CharNeedsQuotesOnUnix(c))
{
return 1;
}
}
else
{
/* On Windows several special characters need quotes to preserve them. */
if(kwsysSystem_Shell__CharNeedsQuotesOnWindows(c))
{
return 1;
}
}
return 0;
}
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__CharIsMakeVariableName(char c)
{
return c && (c == '_' || isalpha(((int)c)));
}
/*--------------------------------------------------------------------------*/
static const char* kwsysSystem_Shell__SkipMakeVariables(const char* c)
{
while(*c == '$' && *(c+1) == '(')
{
const char* skip = c+2;
while(kwsysSystem_Shell__CharIsMakeVariableName(*skip))
{
++skip;
}
if(*skip == ')')
{
c = skip+1;
}
else
{
break;
}
}
return c;
}
/*
Allowing make variable replacements opens a can of worms. Sometimes
they should be quoted and sometimes not. Sometimes their replacement
values are already quoted or contain escapes.
Some Visual Studio variables cause problems. In order to pass the
referenced value with spaces the reference must be quoted. If the
variable value ends in a backslash then it will escape the ending
quote! In order to make the ending backslash appear we need this:
"$(InputDir)\"
However if there is not a trailing backslash then this will put a
quote in the value so we need:
"$(InputDir)"
This macro decides whether we quote an argument just because it
contains a make variable reference. This should be replaced with a
flag later when we understand applications of this better.
*/
#define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
int flags)
{
/* The empty string needs quotes. */
if(!*in)
{
return 1;
}
/* Scan the string for characters that require quoting. */
{
const char* c;
for(c=in; *c; ++c)
{
/* Look for $(MAKEVAR) syntax if requested. */
if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
{
#if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
if(skip != c)
{
/* We need to quote make variable references to preserve the
string with contents substituted in its place. */
return 1;
}
#else
/* Skip over the make variable references if any are present. */
c = kwsysSystem_Shell__SkipMakeVariables(c);
/* Stop if we have reached the end of the string. */
if(!*c)
{
break;
}
#endif
}
/* Check whether this character needs quotes. */
if(kwsysSystem_Shell__CharNeedsQuotes(*c, isUnix, flags))
{
return 1;
}
}
}
/* On Windows some single character arguments need quotes. */
if(!isUnix && *in && !*(in+1))
{
char c = *in;
if((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#'))
{
return 1;
}
}
return 0;
}
/*--------------------------------------------------------------------------*/
static int kwsysSystem_Shell__GetArgumentSize(const char* in,
int isUnix, int flags)
{
/* Start with the length of the original argument, plus one for
either a terminating null or a separating space. */
int size = (int)strlen(in) + 1;
/* String iterator. */
const char* c;
/* Keep track of how many backslashes have been encountered in a row. */
int windows_backslashes = 0;
/* Scan the string for characters that require escaping or quoting. */
for(c=in; *c; ++c)
{
/* Look for $(MAKEVAR) syntax if requested. */
if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
{
/* Skip over the make variable references if any are present. */
c = kwsysSystem_Shell__SkipMakeVariables(c);
/* Stop if we have reached the end of the string. */
if(!*c)
{
break;
}
}
/* Check whether this character needs escaping for the shell. */
if(isUnix)
{
/* On Unix a few special characters need escaping even inside a
quoted argument. */
if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
{
/* This character needs a backslash to escape it. */
++size;
}
}
else if(flags & kwsysSystem_Shell_Flag_EchoWindows)
{
/* On Windows the built-in command shell echo never needs escaping. */
}
else
{
/* On Windows only backslashes and double-quotes need escaping. */
if(*c == '\\')
{
/* Found a backslash. It may need to be escaped later. */
++windows_backslashes;
}
else if(*c == '"')
{
/* Found a double-quote. We need to escape it and all
immediately preceding backslashes. */
size += windows_backslashes + 1;
windows_backslashes = 0;
}
else
{
/* Found another character. This eliminates the possibility
that any immediately preceding backslashes will be
escaped. */
windows_backslashes = 0;
}
}
/* Check whether this character needs escaping for a make tool. */
if(*c == '$')
{
if(flags & kwsysSystem_Shell_Flag_Make)
{
/* In Makefiles a dollar is written $$ so we need one extra
character. */
++size;
}
else if(flags & kwsysSystem_Shell_Flag_VSIDE)
{
/* In a VS IDE a dollar is written "$" so we need two extra
characters. */
size += 2;
}
}
else if(*c == '#')
{
if((flags & kwsysSystem_Shell_Flag_Make) &&
(flags & kwsysSystem_Shell_Flag_WatcomWMake))
{
/* In Watcom WMake makefiles a pound is written $# so we need
one extra character. */
++size;
}
}
else if(*c == '%')
{
if((flags & kwsysSystem_Shell_Flag_VSIDE) ||
((flags & kwsysSystem_Shell_Flag_Make) &&
((flags & kwsysSystem_Shell_Flag_MinGWMake) ||
(flags & kwsysSystem_Shell_Flag_NMake))))
{
/* In the VS IDE, NMake, or MinGW make a percent is written %%
so we need one extra characters. */
size += 1;
}
}
else if(*c == ';')
{
if(flags & kwsysSystem_Shell_Flag_VSIDE)
{
/* In a VS IDE a semicolon is written ";" so we need two extra
characters. */
size += 2;
}
}
}
/* Check whether the argument needs surrounding quotes. */
if(kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags))
{
/* Surrounding quotes are needed. Allocate space for them. */
if((flags & kwsysSystem_Shell_Flag_WatcomQuote) && (isUnix))
{
size += 2;
}
size += 2;
/* We must escape all ending backslashes when quoting on windows. */
size += windows_backslashes;
}
return size;
}
/*--------------------------------------------------------------------------*/
static char* kwsysSystem_Shell__GetArgument(const char* in, char* out,
int isUnix, int flags)
{
/* String iterator. */
const char* c;
/* Keep track of how many backslashes have been encountered in a row. */
int windows_backslashes = 0;
/* Whether the argument must be quoted. */
int needQuotes = kwsysSystem_Shell__ArgumentNeedsQuotes(in, isUnix, flags);
if(needQuotes)
{
/* Add the opening quote for this argument. */
if(flags & kwsysSystem_Shell_Flag_WatcomQuote)
{
if(isUnix)
{
*out++ = '"';
}
*out++ = '\'';
}
else
{
*out++ = '"';
}
}
/* Scan the string for characters that require escaping or quoting. */
for(c=in; *c; ++c)
{
/* Look for $(MAKEVAR) syntax if requested. */
if(flags & kwsysSystem_Shell_Flag_AllowMakeVariables)
{
const char* skip = kwsysSystem_Shell__SkipMakeVariables(c);
if(skip != c)
{
/* Copy to the end of the make variable references. */
while(c != skip)
{
*out++ = *c++;
}
/* The make variable reference eliminates any escaping needed
for preceding backslashes. */
windows_backslashes = 0;
/* Stop if we have reached the end of the string. */
if(!*c)
{
break;
}
}
}
/* Check whether this character needs escaping for the shell. */
if(isUnix)
{
/* On Unix a few special characters need escaping even inside a
quoted argument. */
if(*c == '\\' || *c == '"' || *c == '`' || *c == '$')
{
/* This character needs a backslash to escape it. */
*out++ = '\\';
}