cmOutputConverter.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmOutputConverter.h"
  4. #include <algorithm>
  5. #include <assert.h>
  6. #include <ctype.h>
  7. #include <set>
  8. #include <vector>
  9. #include "cmState.h"
  10. #include "cmSystemTools.h"
  11. cmOutputConverter::cmOutputConverter(cmStateSnapshot const& snapshot)
  12. : StateSnapshot(snapshot)
  13. , LinkScriptShell(false)
  14. {
  15. assert(this->StateSnapshot.IsValid());
  16. }
  17. std::string cmOutputConverter::ConvertToOutputForExisting(
  18. const std::string& remote, OutputFormat format) const
  19. {
  20. // If this is a windows shell, the result has a space, and the path
  21. // already exists, we can use a short-path to reference it without a
  22. // space.
  23. if (this->GetState()->UseWindowsShell() &&
  24. remote.find(' ') != std::string::npos &&
  25. cmSystemTools::FileExists(remote)) {
  26. std::string tmp;
  27. if (cmSystemTools::GetShortPath(remote, tmp)) {
  28. return this->ConvertToOutputFormat(tmp, format);
  29. }
  30. }
  31. // Otherwise, perform standard conversion.
  32. return this->ConvertToOutputFormat(remote, format);
  33. }
  34. std::string cmOutputConverter::ConvertToOutputFormat(cm::string_view source,
  35. OutputFormat output) const
  36. {
  37. std::string result(source);
  38. // Convert it to an output path.
  39. if (output == SHELL || output == WATCOMQUOTE) {
  40. result = this->ConvertDirectorySeparatorsForShell(source);
  41. result = this->EscapeForShell(result, true, false, output == WATCOMQUOTE);
  42. } else if (output == RESPONSE) {
  43. result = this->EscapeForShell(result, false, false, false);
  44. }
  45. return result;
  46. }
  47. std::string cmOutputConverter::ConvertDirectorySeparatorsForShell(
  48. cm::string_view source) const
  49. {
  50. std::string result(source);
  51. // For the MSYS shell convert drive letters to posix paths, so
  52. // that c:/some/path becomes /c/some/path. This is needed to
  53. // avoid problems with the shell path translation.
  54. if (this->GetState()->UseMSYSShell() && !this->LinkScriptShell) {
  55. if (result.size() > 2 && result[1] == ':') {
  56. result[1] = result[0];
  57. result[0] = '/';
  58. }
  59. }
  60. if (this->GetState()->UseWindowsShell()) {
  61. std::replace(result.begin(), result.end(), '/', '\\');
  62. }
  63. return result;
  64. }
  65. static bool cmOutputConverterIsShellOperator(cm::string_view str)
  66. {
  67. static std::set<cm::string_view> const shellOperators{
  68. "<", ">", "<<", ">>", "|", "||", "&&", "&>", "1>", "2>", "2>&1", "1>&2"
  69. };
  70. return (shellOperators.count(str) != 0);
  71. }
  72. std::string cmOutputConverter::EscapeForShell(cm::string_view str,
  73. bool makeVars, bool forEcho,
  74. bool useWatcomQuote) const
  75. {
  76. // Do not escape shell operators.
  77. if (cmOutputConverterIsShellOperator(str)) {
  78. return std::string(str);
  79. }
  80. // Compute the flags for the target shell environment.
  81. int flags = 0;
  82. if (this->GetState()->UseWindowsVSIDE()) {
  83. flags |= Shell_Flag_VSIDE;
  84. } else if (!this->LinkScriptShell) {
  85. flags |= Shell_Flag_Make;
  86. }
  87. if (makeVars) {
  88. flags |= Shell_Flag_AllowMakeVariables;
  89. }
  90. if (forEcho) {
  91. flags |= Shell_Flag_EchoWindows;
  92. }
  93. if (useWatcomQuote) {
  94. flags |= Shell_Flag_WatcomQuote;
  95. }
  96. if (this->GetState()->UseWatcomWMake()) {
  97. flags |= Shell_Flag_WatcomWMake;
  98. }
  99. if (this->GetState()->UseMinGWMake()) {
  100. flags |= Shell_Flag_MinGWMake;
  101. }
  102. if (this->GetState()->UseNMake()) {
  103. flags |= Shell_Flag_NMake;
  104. }
  105. if (!this->GetState()->UseWindowsShell()) {
  106. flags |= Shell_Flag_IsUnix;
  107. }
  108. return Shell__GetArgument(str, flags);
  109. }
  110. std::string cmOutputConverter::EscapeForCMake(cm::string_view str)
  111. {
  112. // Always double-quote the argument to take care of most escapes.
  113. std::string result = "\"";
  114. for (const char c : str) {
  115. if (c == '"') {
  116. // Escape the double quote to avoid ending the argument.
  117. result += "\\\"";
  118. } else if (c == '$') {
  119. // Escape the dollar to avoid expanding variables.
  120. result += "\\$";
  121. } else if (c == '\\') {
  122. // Escape the backslash to avoid other escapes.
  123. result += "\\\\";
  124. } else {
  125. // Other characters will be parsed correctly.
  126. result += c;
  127. }
  128. }
  129. result += "\"";
  130. return result;
  131. }
  132. std::string cmOutputConverter::EscapeWindowsShellArgument(cm::string_view arg,
  133. int shell_flags)
  134. {
  135. return Shell__GetArgument(arg, shell_flags);
  136. }
  137. cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
  138. cm::string_view value)
  139. {
  140. FortranFormat format = FortranFormatNone;
  141. if (!value.empty()) {
  142. for (std::string const& fi : cmSystemTools::ExpandedListArgument(value)) {
  143. if (fi == "FIXED") {
  144. format = FortranFormatFixed;
  145. }
  146. if (fi == "FREE") {
  147. format = FortranFormatFree;
  148. }
  149. }
  150. }
  151. return format;
  152. }
  153. cmOutputConverter::FortranFormat cmOutputConverter::GetFortranFormat(
  154. const char* value)
  155. {
  156. if (!value) {
  157. return FortranFormatNone;
  158. }
  159. return GetFortranFormat(cm::string_view(value));
  160. }
  161. void cmOutputConverter::SetLinkScriptShell(bool linkScriptShell)
  162. {
  163. this->LinkScriptShell = linkScriptShell;
  164. }
  165. cmState* cmOutputConverter::GetState() const
  166. {
  167. return this->StateSnapshot.GetState();
  168. }
  169. /*
  170. Notes:
  171. Make variable replacements open a can of worms. Sometimes they should
  172. be quoted and sometimes not. Sometimes their replacement values are
  173. already quoted.
  174. VS variables cause problems. In order to pass the referenced value
  175. with spaces the reference must be quoted. If the variable value ends
  176. in a backslash then it will escape the ending quote! In order to make
  177. the ending backslash appear we need this:
  178. "$(InputDir)\"
  179. However if there is not a trailing backslash then this will put a
  180. quote in the value so we need:
  181. "$(InputDir)"
  182. Make variable references are platform specific so we should probably
  183. just NOT quote them and let the listfile author deal with it.
  184. */
  185. /*
  186. TODO: For windows echo:
  187. To display a pipe (|) or redirection character (< or >) when using the
  188. echo command, use a caret character immediately before the pipe or
  189. redirection character (for example, ^>, ^<, or ^| ). If you need to
  190. use the caret character itself (^), use two in a row (^^).
  191. */
  192. /* Some helpers to identify character classes */
  193. static bool Shell__CharIsWhitespace(char c)
  194. {
  195. return ((c == ' ') || (c == '\t'));
  196. }
  197. static bool Shell__CharNeedsQuotesOnUnix(char c)
  198. {
  199. return ((c == '\'') || (c == '`') || (c == ';') || (c == '#') ||
  200. (c == '&') || (c == '$') || (c == '(') || (c == ')') || (c == '~') ||
  201. (c == '<') || (c == '>') || (c == '|') || (c == '*') || (c == '^') ||
  202. (c == '\\'));
  203. }
  204. static bool Shell__CharNeedsQuotesOnWindows(char c)
  205. {
  206. return ((c == '\'') || (c == '#') || (c == '&') || (c == '<') ||
  207. (c == '>') || (c == '|') || (c == '^'));
  208. }
  209. static bool Shell__CharIsMakeVariableName(char c)
  210. {
  211. return c && (c == '_' || isalpha((static_cast<int>(c))));
  212. }
  213. bool cmOutputConverter::Shell__CharNeedsQuotes(char c, int flags)
  214. {
  215. /* On Windows the built-in command shell echo never needs quotes. */
  216. if (!(flags & Shell_Flag_IsUnix) && (flags & Shell_Flag_EchoWindows)) {
  217. return false;
  218. }
  219. /* On all platforms quotes are needed to preserve whitespace. */
  220. if (Shell__CharIsWhitespace(c)) {
  221. return true;
  222. }
  223. if (flags & Shell_Flag_IsUnix) {
  224. /* On UNIX several special characters need quotes to preserve them. */
  225. if (Shell__CharNeedsQuotesOnUnix(c)) {
  226. return true;
  227. }
  228. } else {
  229. /* On Windows several special characters need quotes to preserve them. */
  230. if (Shell__CharNeedsQuotesOnWindows(c)) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. cm::string_view::iterator cmOutputConverter::Shell__SkipMakeVariables(
  237. cm::string_view::iterator c, cm::string_view::iterator end)
  238. {
  239. while ((c != end && (c + 1) != end) && (*c == '$' && *(c + 1) == '(')) {
  240. cm::string_view::iterator skip = c + 2;
  241. while ((skip != end) && Shell__CharIsMakeVariableName(*skip)) {
  242. ++skip;
  243. }
  244. if ((skip != end) && *skip == ')') {
  245. c = skip + 1;
  246. } else {
  247. break;
  248. }
  249. }
  250. return c;
  251. }
  252. /*
  253. Allowing make variable replacements opens a can of worms. Sometimes
  254. they should be quoted and sometimes not. Sometimes their replacement
  255. values are already quoted or contain escapes.
  256. Some Visual Studio variables cause problems. In order to pass the
  257. referenced value with spaces the reference must be quoted. If the
  258. variable value ends in a backslash then it will escape the ending
  259. quote! In order to make the ending backslash appear we need this:
  260. "$(InputDir)\"
  261. However if there is not a trailing backslash then this will put a
  262. quote in the value so we need:
  263. "$(InputDir)"
  264. This macro decides whether we quote an argument just because it
  265. contains a make variable reference. This should be replaced with a
  266. flag later when we understand applications of this better.
  267. */
  268. #define KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES 0
  269. bool cmOutputConverter::Shell__ArgumentNeedsQuotes(cm::string_view in,
  270. int flags)
  271. {
  272. /* The empty string needs quotes. */
  273. if (in.empty()) {
  274. return true;
  275. }
  276. /* Scan the string for characters that require quoting. */
  277. for (cm::string_view::iterator cit = in.begin(), cend = in.end();
  278. cit != cend; ++cit) {
  279. /* Look for $(MAKEVAR) syntax if requested. */
  280. if (flags & Shell_Flag_AllowMakeVariables) {
  281. #if KWSYS_SYSTEM_SHELL_QUOTE_MAKE_VARIABLES
  282. cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend);
  283. if (skip != cit) {
  284. /* We need to quote make variable references to preserve the
  285. string with contents substituted in its place. */
  286. return true;
  287. }
  288. #else
  289. /* Skip over the make variable references if any are present. */
  290. cit = Shell__SkipMakeVariables(cit, cend);
  291. /* Stop if we have reached the end of the string. */
  292. if (cit == cend) {
  293. break;
  294. }
  295. #endif
  296. }
  297. /* Check whether this character needs quotes. */
  298. if (Shell__CharNeedsQuotes(*cit, flags)) {
  299. return true;
  300. }
  301. }
  302. /* On Windows some single character arguments need quotes. */
  303. if (flags & Shell_Flag_IsUnix && in.size() == 1) {
  304. char c = in[0];
  305. if ((c == '?') || (c == '&') || (c == '^') || (c == '|') || (c == '#')) {
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. std::string cmOutputConverter::Shell__GetArgument(cm::string_view in,
  312. int flags)
  313. {
  314. /* Output will be at least as long as input string. */
  315. std::string out;
  316. out.reserve(in.size());
  317. /* Keep track of how many backslashes have been encountered in a row. */
  318. int windows_backslashes = 0;
  319. /* Whether the argument must be quoted. */
  320. int needQuotes = Shell__ArgumentNeedsQuotes(in, flags);
  321. if (needQuotes) {
  322. /* Add the opening quote for this argument. */
  323. if (flags & Shell_Flag_WatcomQuote) {
  324. if (flags & Shell_Flag_IsUnix) {
  325. out += '"';
  326. }
  327. out += '\'';
  328. } else {
  329. out += '"';
  330. }
  331. }
  332. /* Scan the string for characters that require escaping or quoting. */
  333. for (cm::string_view::iterator cit = in.begin(), cend = in.end();
  334. cit != cend; ++cit) {
  335. /* Look for $(MAKEVAR) syntax if requested. */
  336. if (flags & Shell_Flag_AllowMakeVariables) {
  337. cm::string_view::iterator skip = Shell__SkipMakeVariables(cit, cend);
  338. if (skip != cit) {
  339. /* Copy to the end of the make variable references. */
  340. while (cit != skip) {
  341. out += *cit++;
  342. }
  343. /* The make variable reference eliminates any escaping needed
  344. for preceding backslashes. */
  345. windows_backslashes = 0;
  346. /* Stop if we have reached the end of the string. */
  347. if (cit == cend) {
  348. break;
  349. }
  350. }
  351. }
  352. /* Check whether this character needs escaping for the shell. */
  353. if (flags & Shell_Flag_IsUnix) {
  354. /* On Unix a few special characters need escaping even inside a
  355. quoted argument. */
  356. if (*cit == '\\' || *cit == '"' || *cit == '`' || *cit == '$') {
  357. /* This character needs a backslash to escape it. */
  358. out += '\\';
  359. }
  360. } else if (flags & Shell_Flag_EchoWindows) {
  361. /* On Windows the built-in command shell echo never needs escaping. */
  362. } else {
  363. /* On Windows only backslashes and double-quotes need escaping. */
  364. if (*cit == '\\') {
  365. /* Found a backslash. It may need to be escaped later. */
  366. ++windows_backslashes;
  367. } else if (*cit == '"') {
  368. /* Found a double-quote. Escape all immediately preceding
  369. backslashes. */
  370. while (windows_backslashes > 0) {
  371. --windows_backslashes;
  372. out += '\\';
  373. }
  374. /* Add the backslash to escape the double-quote. */
  375. out += '\\';
  376. } else {
  377. /* We encountered a normal character. This eliminates any
  378. escaping needed for preceding backslashes. */
  379. windows_backslashes = 0;
  380. }
  381. }
  382. /* Check whether this character needs escaping for a make tool. */
  383. if (*cit == '$') {
  384. if (flags & Shell_Flag_Make) {
  385. /* In Makefiles a dollar is written $$. The make tool will
  386. replace it with just $ before passing it to the shell. */
  387. out += "$$";
  388. } else if (flags & Shell_Flag_VSIDE) {
  389. /* In a VS IDE a dollar is written "$". If this is written in
  390. an un-quoted argument it starts a quoted segment, inserts
  391. the $ and ends the segment. If it is written in a quoted
  392. argument it ends quoting, inserts the $ and restarts
  393. quoting. Either way the $ is isolated from surrounding
  394. text to avoid looking like a variable reference. */
  395. out += "\"$\"";
  396. } else {
  397. /* Otherwise a dollar is written just $. */
  398. out += '$';
  399. }
  400. } else if (*cit == '#') {
  401. if ((flags & Shell_Flag_Make) && (flags & Shell_Flag_WatcomWMake)) {
  402. /* In Watcom WMake makefiles a pound is written $#. The make
  403. tool will replace it with just # before passing it to the
  404. shell. */
  405. out += "$#";
  406. } else {
  407. /* Otherwise a pound is written just #. */
  408. out += '#';
  409. }
  410. } else if (*cit == '%') {
  411. if ((flags & Shell_Flag_VSIDE) ||
  412. ((flags & Shell_Flag_Make) &&
  413. ((flags & Shell_Flag_MinGWMake) || (flags & Shell_Flag_NMake)))) {
  414. /* In the VS IDE, NMake, or MinGW make a percent is written %%. */
  415. out += "%%";
  416. } else {
  417. /* Otherwise a percent is written just %. */
  418. out += '%';
  419. }
  420. } else if (*cit == ';') {
  421. if (flags & Shell_Flag_VSIDE) {
  422. /* In a VS IDE a semicolon is written ";". If this is written
  423. in an un-quoted argument it starts a quoted segment,
  424. inserts the ; and ends the segment. If it is written in a
  425. quoted argument it ends quoting, inserts the ; and restarts
  426. quoting. Either way the ; is isolated. */
  427. out += "\";\"";
  428. } else {
  429. /* Otherwise a semicolon is written just ;. */
  430. out += ';';
  431. }
  432. } else {
  433. /* Store this character. */
  434. out += *cit;
  435. }
  436. }
  437. if (needQuotes) {
  438. /* Add enough backslashes to escape any trailing ones. */
  439. while (windows_backslashes > 0) {
  440. --windows_backslashes;
  441. out += '\\';
  442. }
  443. /* Add the closing quote for this argument. */
  444. if (flags & Shell_Flag_WatcomQuote) {
  445. out += '\'';
  446. if (flags & Shell_Flag_IsUnix) {
  447. out += '"';
  448. }
  449. } else {
  450. out += '"';
  451. }
  452. }
  453. return out;
  454. }