2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2015-04-12 14:15:51 +03:00
|
|
|
#include "cmOutputConverter.h"
|
|
|
|
|
|
|
|
#include "cmAlgorithms.h"
|
2016-09-01 21:59:28 +03:00
|
|
|
#include "cmSystemTools.h"
|
2015-04-12 14:15:51 +03:00
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <algorithm>
|
2015-04-12 14:15:51 +03:00
|
|
|
#include <assert.h>
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <set>
|
2016-06-16 00:41:46 +03:00
|
|
|
#include <sstream>
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
cmOutputConverter::cmOutputConverter(cmState::Snapshot snapshot)
|
2016-05-16 17:34:04 +03:00
|
|
|
: StateSnapshot(snapshot)
|
|
|
|
, LinkScriptShell(false)
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
2016-04-14 18:49:32 +03:00
|
|
|
assert(this->StateSnapshot.IsValid());
|
2015-04-12 14:15:51 +03:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:11:05 +03:00
|
|
|
std::string cmOutputConverter::ConvertToOutputForExisting(
|
|
|
|
const std::string& remote, OutputFormat format) const
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
|
|
|
// If this is a windows shell, the result has a space, and the path
|
|
|
|
// already exists, we can use a short-path to reference it without a
|
|
|
|
// space.
|
2016-06-21 00:18:59 +03:00
|
|
|
if (this->GetState()->UseWindowsShell() &&
|
|
|
|
remote.find(' ') != std::string::npos &&
|
2016-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::FileExists(remote.c_str())) {
|
2015-04-12 14:15:51 +03:00
|
|
|
std::string tmp;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmSystemTools::GetShortPath(remote, tmp)) {
|
2015-04-12 14:15:51 +03:00
|
|
|
return this->ConvertToOutputFormat(tmp, format);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
2016-06-21 00:18:59 +03:00
|
|
|
// Otherwise, perform standard conversion.
|
|
|
|
return this->ConvertToOutputFormat(remote, format);
|
2015-04-12 14:15:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmOutputConverter::ConvertToOutputFormat(const std::string& source,
|
2015-06-14 21:27:37 +03:00
|
|
|
OutputFormat output) const
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
|
|
|
std::string result = source;
|
|
|
|
// Convert it to an output path.
|
2016-08-27 19:10:25 +03:00
|
|
|
if (output == SHELL || output == WATCOMQUOTE) {
|
2015-09-24 13:33:01 +03:00
|
|
|
result = this->ConvertDirectorySeparatorsForShell(source);
|
2015-04-12 14:15:51 +03:00
|
|
|
result = this->EscapeForShell(result, true, false, output == WATCOMQUOTE);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (output == RESPONSE) {
|
2015-04-12 14:15:51 +03:00
|
|
|
result = this->EscapeForShell(result, false, false, false);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-09-24 13:33:01 +03:00
|
|
|
std::string cmOutputConverter::ConvertDirectorySeparatorsForShell(
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::string& source) const
|
2015-09-24 13:33:01 +03:00
|
|
|
{
|
|
|
|
std::string result = source;
|
|
|
|
// For the MSYS shell convert drive letters to posix paths, so
|
|
|
|
// that c:/some/path becomes /c/some/path. This is needed to
|
|
|
|
// avoid problems with the shell path translation.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->GetState()->UseMSYSShell() && !this->LinkScriptShell) {
|
|
|
|
if (result.size() > 2 && result[1] == ':') {
|
2015-09-24 13:33:01 +03:00
|
|
|
result[1] = result[0];
|
|
|
|
result[0] = '/';
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->GetState()->UseWindowsShell()) {
|
2015-09-24 13:33:01 +03:00
|
|
|
std::replace(result.begin(), result.end(), '/', '\\');
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-09-24 13:33:01 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-04-12 14:15:51 +03:00
|
|
|
static bool cmOutputConverterNotAbove(const char* a, const char* b)
|
|
|
|
{
|
|
|
|
return (cmSystemTools::ComparePath(a, b) ||
|
|
|
|
cmSystemTools::IsSubDirectory(a, b));
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmOutputConverter::ConvertToRelativePath(
|
|
|
|
const std::vector<std::string>& local, const std::string& in_remote,
|
|
|
|
bool force) const
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
2016-06-16 22:37:57 +03:00
|
|
|
std::string local_path = cmSystemTools::JoinPath(local);
|
|
|
|
return force ? this->ForceToRelativePath(local_path, in_remote)
|
|
|
|
: this->ConvertToRelativePath(local_path, in_remote);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmOutputConverter::ConvertToRelativePath(
|
|
|
|
std::string const& local_path, std::string const& remote_path) const
|
|
|
|
{
|
|
|
|
// The paths should never be quoted.
|
|
|
|
assert(local_path[0] != '\"');
|
|
|
|
assert(remote_path[0] != '\"');
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// The local path should never have a trailing slash.
|
2016-06-16 22:37:57 +03:00
|
|
|
assert(local_path.empty() || local_path[local_path.size() - 1] != '/');
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// If the path is already relative then just return the path.
|
2016-06-16 22:37:57 +03:00
|
|
|
if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) {
|
|
|
|
return remote_path;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
2016-06-16 22:37:57 +03:00
|
|
|
// Skip conversion if the path and local are not both in the source
|
|
|
|
// or both in the binary tree.
|
|
|
|
if (!((cmOutputConverterNotAbove(
|
|
|
|
local_path.c_str(),
|
|
|
|
this->StateSnapshot.GetDirectory().GetRelativePathTopBinary()) &&
|
|
|
|
cmOutputConverterNotAbove(
|
|
|
|
remote_path.c_str(),
|
|
|
|
this->StateSnapshot.GetDirectory().GetRelativePathTopBinary())) ||
|
|
|
|
(cmOutputConverterNotAbove(
|
|
|
|
local_path.c_str(),
|
|
|
|
this->StateSnapshot.GetDirectory().GetRelativePathTopSource()) &&
|
|
|
|
cmOutputConverterNotAbove(
|
|
|
|
remote_path.c_str(),
|
|
|
|
this->StateSnapshot.GetDirectory().GetRelativePathTopSource())))) {
|
|
|
|
return remote_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->ForceToRelativePath(local_path, remote_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmOutputConverter::ForceToRelativePath(
|
|
|
|
std::string const& local_path, std::string const& remote_path)
|
|
|
|
{
|
|
|
|
// The paths should never be quoted.
|
|
|
|
assert(local_path[0] != '\"');
|
|
|
|
assert(remote_path[0] != '\"');
|
|
|
|
|
|
|
|
// The local path should never have a trailing slash.
|
|
|
|
assert(local_path.empty() || local_path[local_path.size() - 1] != '/');
|
|
|
|
|
|
|
|
// If the path is already relative then just return the path.
|
|
|
|
if (!cmSystemTools::FileIsFullPath(remote_path.c_str())) {
|
|
|
|
return remote_path;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// Identify the longest shared path component between the remote
|
|
|
|
// path and the local path.
|
2016-06-16 22:37:57 +03:00
|
|
|
std::vector<std::string> local;
|
|
|
|
cmSystemTools::SplitPath(local_path, local);
|
2015-04-12 14:15:51 +03:00
|
|
|
std::vector<std::string> remote;
|
2016-06-16 22:37:57 +03:00
|
|
|
cmSystemTools::SplitPath(remote_path, remote);
|
2016-05-16 17:34:04 +03:00
|
|
|
unsigned int common = 0;
|
|
|
|
while (common < remote.size() && common < local.size() &&
|
|
|
|
cmSystemTools::ComparePath(remote[common], local[common])) {
|
2015-04-12 14:15:51 +03:00
|
|
|
++common;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// If no part of the path is in common then return the full path.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (common == 0) {
|
2016-06-16 22:37:57 +03:00
|
|
|
return remote_path;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// If the entire path is in common then just return a ".".
|
2016-05-16 17:34:04 +03:00
|
|
|
if (common == remote.size() && common == local.size()) {
|
2015-04-12 14:15:51 +03:00
|
|
|
return ".";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// If the entire path is in common except for a trailing slash then
|
|
|
|
// just return a "./".
|
2016-05-16 17:34:04 +03:00
|
|
|
if (common + 1 == remote.size() && remote[common].empty() &&
|
|
|
|
common == local.size()) {
|
2015-04-12 14:15:51 +03:00
|
|
|
return "./";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// Construct the relative path.
|
|
|
|
std::string relative;
|
|
|
|
|
|
|
|
// First add enough ../ to get up to the level of the shared portion
|
|
|
|
// of the path. Leave off the trailing slash. Note that the last
|
|
|
|
// component of local will never be empty because local should never
|
|
|
|
// have a trailing slash.
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int i = common; i < local.size(); ++i) {
|
2015-04-12 14:15:51 +03:00
|
|
|
relative += "..";
|
2016-05-16 17:34:04 +03:00
|
|
|
if (i < local.size() - 1) {
|
2015-04-12 14:15:51 +03:00
|
|
|
relative += "/";
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// Now add the portion of the destination path that is not included
|
|
|
|
// in the shared portion of the path. Add a slash the first time
|
|
|
|
// only if there was already something in the path. If there was a
|
|
|
|
// trailing slash in the input then the last iteration of the loop
|
|
|
|
// will add a slash followed by an empty string which will preserve
|
|
|
|
// the trailing slash in the output.
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!relative.empty() && !remote.empty()) {
|
2015-04-12 14:15:51 +03:00
|
|
|
relative += "/";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-18 11:45:18 +03:00
|
|
|
relative += cmJoin(cmMakeRange(remote).advance(common), "/");
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// Finally return the path.
|
|
|
|
return relative;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool cmOutputConverterIsShellOperator(const std::string& str)
|
|
|
|
{
|
|
|
|
static std::set<std::string> shellOperators;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (shellOperators.empty()) {
|
2015-04-12 14:15:51 +03:00
|
|
|
shellOperators.insert("<");
|
|
|
|
shellOperators.insert(">");
|
|
|
|
shellOperators.insert("<<");
|
|
|
|
shellOperators.insert(">>");
|
|
|
|
shellOperators.insert("|");
|
|
|
|
shellOperators.insert("||");
|
|
|
|
shellOperators.insert("&&");
|
|
|
|
shellOperators.insert("&>");
|
|
|
|
shellOperators.insert("1>");
|
|
|
|
shellOperators.insert("2>");
|
|
|
|
shellOperators.insert("2>&1");
|
|
|
|
shellOperators.insert("1>&2");
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
return shellOperators.count(str) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmOutputConverter::EscapeForShell(const std::string& str,
|
2016-05-16 17:34:04 +03:00
|
|
|
bool makeVars, bool forEcho,
|
|
|
|
bool useWatcomQuote) const
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
|
|
|
// Do not escape shell operators.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (cmOutputConverterIsShellOperator(str)) {
|
2015-04-12 14:15:51 +03:00
|
|
|
return str;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
|
|
|
// Compute the flags for the target shell environment.
|
|
|
|
int flags = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->GetState()->UseWindowsVSIDE()) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_VSIDE;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (!this->LinkScriptShell) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_Make;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (makeVars) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_AllowMakeVariables;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (forEcho) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_EchoWindows;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (useWatcomQuote) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_WatcomQuote;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->GetState()->UseWatcomWMake()) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_WatcomWMake;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->GetState()->UseMinGWMake()) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_MinGWMake;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->GetState()->UseNMake()) {
|
2015-07-07 22:06:44 +03:00
|
|
|
flags |= Shell_Flag_NMake;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
|
2016-06-16 00:41:46 +03:00
|
|
|
return this->GetState()->UseWindowsShell()
|
|
|
|
? Shell_GetArgumentForWindows(str.c_str(), flags)
|
|
|
|
: Shell_GetArgumentForUnix(str.c_str(), flags);
|
2015-04-12 14:15:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string cmOutputConverter::EscapeForCMake(const std::string& str)
|
|
|
|
{
|
|
|
|
// Always double-quote the argument to take care of most escapes.
|
|
|
|
std::string result = "\"";
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* c = str.c_str(); *c; ++c) {
|
|
|
|
if (*c == '"') {
|
2015-04-12 14:15:51 +03:00
|
|
|
// Escape the double quote to avoid ending the argument.
|
|
|
|
result += "\\\"";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '$') {
|
2015-04-12 14:15:51 +03:00
|
|
|
// Escape the dollar to avoid expanding variables.
|
|
|
|
result += "\\$";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '\\') {
|
2015-04-12 14:15:51 +03:00
|
|
|
// Escape the backslash to avoid other escapes.
|
|
|
|
result += "\\\\";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-04-12 14:15:51 +03:00
|
|
|
// Other characters will be parsed correctly.
|
|
|
|
result += *c;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
result += "\"";
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string cmOutputConverter::EscapeWindowsShellArgument(const char* arg,
|
|
|
|
int shell_flags)
|
2015-07-07 21:31:46 +03:00
|
|
|
{
|
2016-06-16 00:41:46 +03:00
|
|
|
return Shell_GetArgumentForWindows(arg, shell_flags);
|
2015-07-07 21:31:46 +03:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
|
|
|
|
const char* value)
|
2015-04-12 14:15:51 +03:00
|
|
|
{
|
|
|
|
FortranFormat format = FortranFormatNone;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (value && *value) {
|
2015-04-12 14:15:51 +03:00
|
|
|
std::vector<std::string> fmt;
|
|
|
|
cmSystemTools::ExpandListArgument(value, fmt);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (std::vector<std::string>::iterator fi = fmt.begin(); fi != fmt.end();
|
|
|
|
++fi) {
|
|
|
|
if (*fi == "FIXED") {
|
2015-04-12 14:15:51 +03:00
|
|
|
format = FortranFormatFixed;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*fi == "FREE") {
|
2015-04-12 14:15:51 +03:00
|
|
|
format = FortranFormatFree;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-04-12 14:15:51 +03:00
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmOutputConverter::SetLinkScriptShell(bool linkScriptShell)
|
|
|
|
{
|
|
|
|
this->LinkScriptShell = linkScriptShell;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmState* cmOutputConverter::GetState() const
|
|
|
|
{
|
|
|
|
return this->StateSnapshot.GetState();
|
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
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 (^^).
|
|
|
|
*/
|
|
|
|
|
|
|
|
int cmOutputConverter::Shell__CharIsWhitespace(char c)
|
|
|
|
{
|
|
|
|
return ((c == ' ') || (c == '\t'));
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmOutputConverter::Shell__CharNeedsQuotesOnUnix(char c)
|
|
|
|
{
|
|
|
|
return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
|
2016-05-16 17:34:04 +03:00
|
|
|
(c == '&') || (c == '$') || (c == '(') || (c == ')') || (c == '~') ||
|
|
|
|
(c == '<') || (c == '>') || (c == '|') || (c == '*') || (c == '^') ||
|
|
|
|
(c == '\\'));
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmOutputConverter::Shell__CharNeedsQuotesOnWindows(char c)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
return ((c == '\'') || (c == '#') || (c == '&') || (c == '<') ||
|
|
|
|
(c == '>') || (c == '|') || (c == '^'));
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmOutputConverter::Shell__CharNeedsQuotes(char c, int isUnix, int flags)
|
|
|
|
{
|
|
|
|
/* On Windows the built-in command shell echo never needs quotes. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!isUnix && (flags & Shell_Flag_EchoWindows)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* On all platforms quotes are needed to preserve whitespace. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Shell__CharIsWhitespace(c)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isUnix) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* On UNIX several special characters need quotes to preserve them. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Shell__CharNeedsQuotesOnUnix(c)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* On Windows several special characters need quotes to preserve them. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (Shell__CharNeedsQuotesOnWindows(c)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmOutputConverter::Shell__CharIsMakeVariableName(char c)
|
|
|
|
{
|
|
|
|
return c && (c == '_' || isalpha(((int)c)));
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmOutputConverter::Shell__SkipMakeVariables(const char* c)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*c == '$' && *(c + 1) == '(') {
|
|
|
|
const char* skip = c + 2;
|
|
|
|
while (Shell__CharIsMakeVariableName(*skip)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
++skip;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*skip == ')') {
|
|
|
|
c = skip + 1;
|
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
break;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
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
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
int cmOutputConverter::Shell__ArgumentNeedsQuotes(const char* in, int isUnix,
|
|
|
|
int flags)
|
2015-07-07 22:06:44 +03:00
|
|
|
{
|
|
|
|
/* The empty string needs quotes. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!*in) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 1;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Scan the string for characters that require quoting. */
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* c;
|
|
|
|
for (c = in; *c; ++c) {
|
|
|
|
/* Look for $(MAKEVAR) syntax if requested. */
|
|
|
|
if (flags & Shell_Flag_AllowMakeVariables) {
|
2015-07-07 22:06:44 +03:00
|
|
|
#if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* skip = 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;
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
#else
|
2016-05-16 17:34:04 +03:00
|
|
|
/* Skip over the make variable references if any are present. */
|
|
|
|
c = Shell__SkipMakeVariables(c);
|
2015-07-07 22:06:44 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
/* Stop if we have reached the end of the string. */
|
|
|
|
if (!*c) {
|
|
|
|
break;
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
/* Check whether this character needs quotes. */
|
|
|
|
if (Shell__CharNeedsQuotes(*c, isUnix, flags)) {
|
|
|
|
return 1;
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* On Windows some single character arguments need quotes. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!isUnix && *in && !*(in + 1)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
char c = *in;
|
2016-05-16 17:34:04 +03:00
|
|
|
if ((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) {
|
2015-07-07 22:06:44 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-16 00:41:46 +03:00
|
|
|
std::string cmOutputConverter::Shell__GetArgument(const char* in, int isUnix,
|
|
|
|
int flags)
|
2015-07-07 22:06:44 +03:00
|
|
|
{
|
2016-06-16 00:41:46 +03:00
|
|
|
std::ostringstream out;
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* 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 = Shell__ArgumentNeedsQuotes(in, isUnix, flags);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (needQuotes) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Add the opening quote for this argument. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (flags & Shell_Flag_WatcomQuote) {
|
|
|
|
if (isUnix) {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '"';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\'';
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '"';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Scan the string for characters that require escaping or quoting. */
|
2016-05-16 17:34:04 +03:00
|
|
|
for (c = in; *c; ++c) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Look for $(MAKEVAR) syntax if requested. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (flags & Shell_Flag_AllowMakeVariables) {
|
2015-07-07 22:06:44 +03:00
|
|
|
const char* skip = Shell__SkipMakeVariables(c);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (skip != c) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Copy to the end of the make variable references. */
|
2016-05-16 17:34:04 +03:00
|
|
|
while (c != skip) {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << *c++;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* The make variable reference eliminates any escaping needed
|
|
|
|
for preceding backslashes. */
|
|
|
|
windows_backslashes = 0;
|
|
|
|
|
|
|
|
/* Stop if we have reached the end of the string. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!*c) {
|
2015-07-07 22:06:44 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Check whether this character needs escaping for the shell. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isUnix) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* On Unix a few special characters need escaping even inside a
|
|
|
|
quoted argument. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*c == '\\' || *c == '"' || *c == '`' || *c == '$') {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* This character needs a backslash to escape it. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\\';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (flags & Shell_Flag_EchoWindows) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* On Windows the built-in command shell echo never needs escaping. */
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* On Windows only backslashes and double-quotes need escaping. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*c == '\\') {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Found a backslash. It may need to be escaped later. */
|
|
|
|
++windows_backslashes;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '"') {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Found a double-quote. Escape all immediately preceding
|
|
|
|
backslashes. */
|
2016-05-16 17:34:04 +03:00
|
|
|
while (windows_backslashes > 0) {
|
2015-07-07 22:06:44 +03:00
|
|
|
--windows_backslashes;
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\\';
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Add the backslash to escape the double-quote. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\\';
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* We encountered a normal character. This eliminates any
|
|
|
|
escaping needed for preceding backslashes. */
|
|
|
|
windows_backslashes = 0;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Check whether this character needs escaping for a make tool. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*c == '$') {
|
|
|
|
if (flags & Shell_Flag_Make) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* In Makefiles a dollar is written $$. The make tool will
|
|
|
|
replace it with just $ before passing it to the shell. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << "$$";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (flags & Shell_Flag_VSIDE) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* In a VS IDE a dollar is written "$". If this is written in
|
|
|
|
an un-quoted argument it starts a quoted segment, inserts
|
|
|
|
the $ and ends the segment. If it is written in a quoted
|
|
|
|
argument it ends quoting, inserts the $ and restarts
|
|
|
|
quoting. Either way the $ is isolated from surrounding
|
|
|
|
text to avoid looking like a variable reference. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << "\"$\"";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Otherwise a dollar is written just $. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '$';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '#') {
|
|
|
|
if ((flags & Shell_Flag_Make) && (flags & Shell_Flag_WatcomWMake)) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* In Watcom WMake makefiles a pound is written $#. The make
|
|
|
|
tool will replace it with just # before passing it to the
|
|
|
|
shell. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << "$#";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Otherwise a pound is written just #. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '#';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == '%') {
|
|
|
|
if ((flags & Shell_Flag_VSIDE) ||
|
|
|
|
((flags & Shell_Flag_Make) &&
|
|
|
|
((flags & Shell_Flag_MinGWMake) || (flags & Shell_Flag_NMake)))) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* In the VS IDE, NMake, or MinGW make a percent is written %%. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << "%%";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Otherwise a percent is written just %. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '%';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (*c == ';') {
|
|
|
|
if (flags & Shell_Flag_VSIDE) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* In a VS IDE a semicolon is written ";". If this is written
|
|
|
|
in an un-quoted argument it starts a quoted segment,
|
|
|
|
inserts the ; and ends the segment. If it is written in a
|
|
|
|
quoted argument it ends quoting, inserts the ; and restarts
|
|
|
|
quoting. Either way the ; is isolated. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << "\";\"";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Otherwise a semicolon is written just ;. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << ';';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Store this character. */
|
2016-06-16 00:41:46 +03:00
|
|
|
out << *c;
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (needQuotes) {
|
2015-07-07 22:06:44 +03:00
|
|
|
/* Add enough backslashes to escape any trailing ones. */
|
2016-05-16 17:34:04 +03:00
|
|
|
while (windows_backslashes > 0) {
|
2015-07-07 22:06:44 +03:00
|
|
|
--windows_backslashes;
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\\';
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
|
|
|
/* Add the closing quote for this argument. */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (flags & Shell_Flag_WatcomQuote) {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '\'';
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isUnix) {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '"';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2016-06-16 00:41:46 +03:00
|
|
|
out << '"';
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-07-07 22:06:44 +03:00
|
|
|
|
2016-06-16 00:41:46 +03:00
|
|
|
return out.str();
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 00:41:46 +03:00
|
|
|
std::string cmOutputConverter::Shell_GetArgumentForWindows(const char* in,
|
|
|
|
int flags)
|
2015-07-07 22:06:44 +03:00
|
|
|
{
|
2016-06-16 00:41:46 +03:00
|
|
|
return Shell__GetArgument(in, 0, flags);
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|
|
|
|
|
2016-06-16 00:41:46 +03:00
|
|
|
std::string cmOutputConverter::Shell_GetArgumentForUnix(const char* in,
|
|
|
|
int flags)
|
2015-07-07 22:06:44 +03:00
|
|
|
{
|
2016-06-16 00:41:46 +03:00
|
|
|
return Shell__GetArgument(in, 1, flags);
|
2015-07-07 22:06:44 +03:00
|
|
|
}
|