cmOutputConverter.cxx 15.5 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmOutputConverter.h"

5
#include <algorithm>
6
#include <assert.h>
7 8
#include <ctype.h>
#include <set>
9 10 11
#include <vector>

#include "cmState.h"
12
#include "cmStringAlgorithms.h"
13
#include "cmSystemTools.h"
14

15
cmOutputConverter::cmOutputConverter(cmStateSnapshot const& snapshot)
16 17
  : StateSnapshot(snapshot)
  , LinkScriptShell(false)
18
{
19
  assert(this->StateSnapshot.IsValid());
20 21
}

22 23
std::string cmOutputConverter::ConvertToOutputForExisting(
  const std::string& remote, OutputFormat format) const
24 25 26 27
{
  // 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.
28 29
  if (this->GetState()->UseWindowsShell() &&
      remote.find(' ') != std::string::npos &&
30
      cmSystemTools::FileExists(remote)) {
31
    std::string tmp;
32
    if (cmSystemTools::GetShortPath(remote, tmp)) {
33 34
      return this->ConvertToOutputFormat(tmp, format);
    }
35
  }
36

37 38
  // Otherwise, perform standard conversion.
  return this->ConvertToOutputFormat(remote, format);
39 40
}

41
std::string cmOutputConverter::ConvertToOutputFormat(cm::string_view source,
42
                                                     OutputFormat output) const
43
{
44
  std::string result(source);
45
  // Convert it to an output path.
46
  if (output == SHELL || output == WATCOMQUOTE) {
47
    result = this->ConvertDirectorySeparatorsForShell(source);
48
    result = this->EscapeForShell(result, true, false, output == WATCOMQUOTE);
49
  } else if (output == RESPONSE) {
50
    result = this->EscapeForShell(result, false, false, false);
51
  }
52 53 54
  return result;
}

55
std::string cmOutputConverter::ConvertDirectorySeparatorsForShell(
56
  cm::string_view source) const
57
{
58
  std::string result(source);
59 60 61
  // 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.
62 63
  if (this->GetState()->UseMSYSShell() && !this->LinkScriptShell) {
    if (result.size() > 2 && result[1] == ':') {
64 65 66
      result[1] = result[0];
      result[0] = '/';
    }
67 68
  }
  if (this->GetState()->UseWindowsShell()) {
69
    std::replace(result.begin(), result.end(), '/', '\\');
70
  }
71 72 73
  return result;
}

74
static bool cmOutputConverterIsShellOperator(cm::string_view str)
75
{
76
  static std::set<cm::string_view> const shellOperators{
77 78 79
    "<", ">", "<<", ">>", "|", "||", "&&", "&>", "1>", "2>", "2>&1", "1>&2"
  };
  return (shellOperators.count(str) != 0);
80 81
}

82
std::string cmOutputConverter::EscapeForShell(cm::string_view str,
83 84
                                              bool makeVars, bool forEcho,
                                              bool useWatcomQuote) const
85 86
{
  // Do not escape shell operators.
87
  if (cmOutputConverterIsShellOperator(str)) {
88
    return std::string(str);
89
  }
90 91 92

  // Compute the flags for the target shell environment.
  int flags = 0;
93
  if (this->GetState()->UseWindowsVSIDE()) {
94
    flags |= Shell_Flag_VSIDE;
95
  } else if (!this->LinkScriptShell) {
96
    flags |= Shell_Flag_Make;
97 98
  }
  if (makeVars) {
99
    flags |= Shell_Flag_AllowMakeVariables;
100 101
  }
  if (forEcho) {
102
    flags |= Shell_Flag_EchoWindows;
103 104
  }
  if (useWatcomQuote) {
105
    flags |= Shell_Flag_WatcomQuote;
106 107
  }
  if (this->GetState()->UseWatcomWMake()) {
108
    flags |= Shell_Flag_WatcomWMake;
109 110
  }
  if (this->GetState()->UseMinGWMake()) {
111
    flags |= Shell_Flag_MinGWMake;
112 113
  }
  if (this->GetState()->UseNMake()) {
114
    flags |= Shell_Flag_NMake;
115
  }
116 117 118
  if (!this->GetState()->UseWindowsShell()) {
    flags |= Shell_Flag_IsUnix;
  }
119

120
  return Shell__GetArgument(str, flags);
121 122
}

123
std::string cmOutputConverter::EscapeForCMake(cm::string_view str)
124 125 126
{
  // Always double-quote the argument to take care of most escapes.
  std::string result = "\"";
127 128
  for (const char c : str) {
    if (c == '"') {
129 130
      // Escape the double quote to avoid ending the argument.
      result += "\\\"";
131
    } else if (c == '$') {
132 133
      // Escape the dollar to avoid expanding variables.
      result += "\\$";
134
    } else if (c == '\\') {
135 136
      // Escape the backslash to avoid other escapes.
      result += "\\\\";
137
    } else {
138
      // Other characters will be parsed correctly.
139
      result += c;
140
    }
141
  }
142 143 144 145
  result += "\"";
  return result;
}

146
std::string cmOutputConverter::EscapeWindowsShellArgument(cm::string_view arg,
147
                                                          int shell_flags)
148
{
149
  return Shell__GetArgument(arg, shell_flags);
150 151
}

152
cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
153
  cm::string_view value)
154 155
{
  FortranFormat format = FortranFormatNone;
156
  if (!value.empty()) {
157
    for (std::string const& fi : cmExpandedList(value)) {
158
      if (fi == "FIXED") {
159
        format = FortranFormatFixed;
160
      }
161
      if (fi == "FREE") {
162 163 164
        format = FortranFormatFree;
      }
    }
165
  }
166 167 168
  return format;
}

169 170 171 172 173 174 175 176 177
cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
  const char* value)
{
  if (!value) {
    return FortranFormatNone;
  }
  return GetFortranFormat(cm::string_view(value));
}

178 179 180 181 182 183 184 185 186
void cmOutputConverter::SetLinkScriptShell(bool linkScriptShell)
{
  this->LinkScriptShell = linkScriptShell;
}

cmState* cmOutputConverter::GetState() const
{
  return this->StateSnapshot.GetState();
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

/*

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 (^^).
*/

222
/* Some helpers to identify character classes */
223
static bool Shell__CharIsWhitespace(char c)
224 225 226 227
{
  return ((c == ' ') || (c == '\t'));
}

228
static bool Shell__CharNeedsQuotesOnUnix(char c)
229 230
{
  return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
231 232 233
          (c == '&') || (c == '$') || (c == '(') || (c == ')') || (c == '~') ||
          (c == '<') || (c == '>') || (c == '|') || (c == '*') || (c == '^') ||
          (c == '\\'));
234 235
}

236
static bool Shell__CharNeedsQuotesOnWindows(char c)
237
{
238 239
  return ((c == '\'') || (c == '#') || (c == '&') || (c == '<') ||
          (c == '>') || (c == '|') || (c == '^'));
240 241
}

242
static bool Shell__CharIsMakeVariableName(char c)
243 244 245 246
{
  return c && (c == '_' || isalpha((static_cast<int>(c))));
}

247
bool cmOutputConverter::Shell__CharNeedsQuotes(char c, int flags)
248 249
{
  /* On Windows the built-in command shell echo never needs quotes.  */
250
  if (!(flags & Shell_Flag_IsUnix) && (flags & Shell_Flag_EchoWindows)) {
251
    return false;
252
  }
253 254

  /* On all platforms quotes are needed to preserve whitespace.  */
255
  if (Shell__CharIsWhitespace(c)) {
256
    return true;
257
  }
258

259
  if (flags & Shell_Flag_IsUnix) {
260
    /* On UNIX several special characters need quotes to preserve them.  */
261
    if (Shell__CharNeedsQuotesOnUnix(c)) {
262
      return true;
263
    }
264
  } else {
265
    /* On Windows several special characters need quotes to preserve them.  */
266
    if (Shell__CharNeedsQuotesOnWindows(c)) {
267
      return true;
268
    }
269
  }
270
  return false;
271 272
}

273 274
cm::string_view::iterator cmOutputConverter::Shell__SkipMakeVariables(
  cm::string_view::iterator c, cm::string_view::iterator end)
275
{
276 277 278
  while ((c != end && (c + 1) != end) && (*c == '$' && *(c + 1) == '(')) {
    cm::string_view::iterator skip = c + 2;
    while ((skip != end) && Shell__CharIsMakeVariableName(*skip)) {
279
      ++skip;
280
    }
281
    if ((skip != end) && *skip == ')') {
282 283
      c = skip + 1;
    } else {
284 285
      break;
    }
286
  }
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
  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

313 314
bool cmOutputConverter::Shell__ArgumentNeedsQuotes(cm::string_view in,
                                                   int flags)
315 316
{
  /* The empty string needs quotes.  */
317
  if (in.empty()) {
318
    return true;
319
  }
320 321

  /* Scan the string for characters that require quoting.  */
322 323 324 325
  for (cm::string_view::iterator cit = in.begin(), cend = in.end();
       cit != cend; ++cit) {
    /* Look for $(MAKEVAR) syntax if requested.  */
    if (flags & Shell_Flag_AllowMakeVariables) {
326
#if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
327 328 329 330 331 332
      cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend);
      if (skip != cit) {
        /* We need to quote make variable references to preserve the
           string with contents substituted in its place.  */
        return true;
      }
333
#else
334 335
      /* Skip over the make variable references if any are present.  */
      cit = Shell__SkipMakeVariables(cit, cend);
336

337 338 339
      /* Stop if we have reached the end of the string.  */
      if (cit == cend) {
        break;
340
      }
341 342
#endif
    }
343

344 345 346
    /* Check whether this character needs quotes.  */
    if (Shell__CharNeedsQuotes(*cit, flags)) {
      return true;
347 348 349 350
    }
  }

  /* On Windows some single character arguments need quotes.  */
351 352
  if (flags & Shell_Flag_IsUnix && in.size() == 1) {
    char c = in[0];
353
    if ((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) {
354
      return true;
355
    }
356
  }
357

358
  return false;
359 360
}

361 362
std::string cmOutputConverter::Shell__GetArgument(cm::string_view in,
                                                  int flags)
363
{
364 365
  /* Output will be at least as long as input string.  */
  std::string out;
366
  out.reserve(in.size());
367 368 369 370 371

  /* Keep track of how many backslashes have been encountered in a row.  */
  int windows_backslashes = 0;

  /* Whether the argument must be quoted.  */
372
  int needQuotes = Shell__ArgumentNeedsQuotes(in, flags);
373
  if (needQuotes) {
374
    /* Add the opening quote for this argument.  */
375
    if (flags & Shell_Flag_WatcomQuote) {
376
      if (flags & Shell_Flag_IsUnix) {
377
        out += '"';
378
      }
379
      out += '\'';
380
    } else {
381
      out += '"';
382
    }
383
  }
384 385

  /* Scan the string for characters that require escaping or quoting.  */
386 387
  for (cm::string_view::iterator cit = in.begin(), cend = in.end();
       cit != cend; ++cit) {
388
    /* Look for $(MAKEVAR) syntax if requested.  */
389
    if (flags & Shell_Flag_AllowMakeVariables) {
390 391
      cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend);
      if (skip != cit) {
392
        /* Copy to the end of the make variable references.  */
393 394
        while (cit != skip) {
          out += *cit++;
395
        }
396 397 398 399 400 401

        /* The make variable reference eliminates any escaping needed
           for preceding backslashes.  */
        windows_backslashes = 0;

        /* Stop if we have reached the end of the string.  */
402
        if (cit == cend) {
403 404 405
          break;
        }
      }
406
    }
407 408

    /* Check whether this character needs escaping for the shell.  */
409
    if (flags & Shell_Flag_IsUnix) {
410 411
      /* On Unix a few special characters need escaping even inside a
         quoted argument.  */
412
      if (*cit == '\\' || *cit == '"' || *cit == '`' || *cit == '$') {
413
        /* This character needs a backslash to escape it.  */
414
        out += '\\';
415
      }
416
    } else if (flags & Shell_Flag_EchoWindows) {
417
      /* On Windows the built-in command shell echo never needs escaping.  */
418
    } else {
419
      /* On Windows only backslashes and double-quotes need escaping.  */
420
      if (*cit == '\\') {
421 422
        /* Found a backslash.  It may need to be escaped later.  */
        ++windows_backslashes;
423
      } else if (*cit == '"') {
424 425
        /* Found a double-quote.  Escape all immediately preceding
           backslashes.  */
426
        while (windows_backslashes > 0) {
427
          --windows_backslashes;
428
          out += '\\';
429
        }
430 431

        /* Add the backslash to escape the double-quote.  */
432
        out += '\\';
433
      } else {
434 435 436 437
        /* We encountered a normal character.  This eliminates any
           escaping needed for preceding backslashes.  */
        windows_backslashes = 0;
      }
438
    }
439 440

    /* Check whether this character needs escaping for a make tool.  */
441
    if (*cit == '$') {
442
      if (flags & Shell_Flag_Make) {
443 444
        /* In Makefiles a dollar is written $$.  The make tool will
           replace it with just $ before passing it to the shell.  */
445
        out += "$$";
446
      } else if (flags & Shell_Flag_VSIDE) {
447 448 449 450 451 452
        /* 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.  */
453
        out += "\"$\"";
454
      } else {
455
        /* Otherwise a dollar is written just $. */
456
        out += '$';
457
      }
458
    } else if (*cit == '#') {
459
      if ((flags & Shell_Flag_Make) && (flags & Shell_Flag_WatcomWMake)) {
460 461 462
        /* In Watcom WMake makefiles a pound is written $#.  The make
           tool will replace it with just # before passing it to the
           shell.  */
463
        out += "$#";
464
      } else {
465
        /* Otherwise a pound is written just #. */
466
        out += '#';
467
      }
468
    } else if (*cit == '%') {
469 470 471
      if ((flags & Shell_Flag_VSIDE) ||
          ((flags & Shell_Flag_Make) &&
           ((flags & Shell_Flag_MinGWMake) || (flags & Shell_Flag_NMake)))) {
472
        /* In the VS IDE, NMake, or MinGW make a percent is written %%.  */
473
        out += "%%";
474
      } else {
475
        /* Otherwise a percent is written just %. */
476
        out += '%';
477
      }
478
    } else if (*cit == ';') {
479
      if (flags & Shell_Flag_VSIDE) {
480 481 482 483 484
        /* 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.  */
485
        out += "\";\"";
486
      } else {
487
        /* Otherwise a semicolon is written just ;. */
488
        out += ';';
489
      }
490
    } else {
491
      /* Store this character.  */
492
      out += *cit;
493
    }
494
  }
495

496
  if (needQuotes) {
497
    /* Add enough backslashes to escape any trailing ones.  */
498
    while (windows_backslashes > 0) {
499
      --windows_backslashes;
500
      out += '\\';
501
    }
502 503

    /* Add the closing quote for this argument.  */
504
    if (flags & Shell_Flag_WatcomQuote) {
505
      out += '\'';
506
      if (flags & Shell_Flag_IsUnix) {
507
        out += '"';
508
      }
509
    } else {
510
      out += '"';
511
    }
512
  }
513

514
  return out;
515
}