cmStringCommand.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #define _SCL_SECURE_NO_WARNINGS
  4. #include "cmStringCommand.h"
  5. #include <algorithm>
  6. #include <cctype>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <memory>
  10. #include <cm/iterator>
  11. #include "cmsys/RegularExpression.hxx"
  12. #include "cm_static_string_view.hxx"
  13. #include "cmCryptoHash.h"
  14. #include "cmExecutionStatus.h"
  15. #include "cmGeneratorExpression.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmRange.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmStringReplaceHelper.h"
  21. #include "cmSubcommandTable.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTimestamp.h"
  24. #include "cmUuid.h"
  25. namespace {
  26. bool RegexMatch(std::vector<std::string> const& args,
  27. cmExecutionStatus& status);
  28. bool RegexMatchAll(std::vector<std::string> const& args,
  29. cmExecutionStatus& status);
  30. bool RegexReplace(std::vector<std::string> const& args,
  31. cmExecutionStatus& status);
  32. bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
  33. size_t varIdx, cmMakefile& makefile);
  34. bool HandleHashCommand(std::vector<std::string> const& args,
  35. cmExecutionStatus& status)
  36. {
  37. #if !defined(CMAKE_BOOTSTRAP)
  38. if (args.size() != 3) {
  39. status.SetError(
  40. cmStrCat(args[0], " requires an output variable and an input string"));
  41. return false;
  42. }
  43. std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
  44. if (hash) {
  45. std::string out = hash->HashString(args[2]);
  46. status.GetMakefile().AddDefinition(args[1], out);
  47. return true;
  48. }
  49. return false;
  50. #else
  51. status.SetError(cmStrCat(args[0], " not available during bootstrap"));
  52. return false;
  53. #endif
  54. }
  55. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  56. bool toUpper, cmExecutionStatus& status)
  57. {
  58. if (args.size() < 3) {
  59. status.SetError("no output variable specified");
  60. return false;
  61. }
  62. std::string const& outvar = args[2];
  63. std::string output;
  64. if (toUpper) {
  65. output = cmSystemTools::UpperCase(args[1]);
  66. } else {
  67. output = cmSystemTools::LowerCase(args[1]);
  68. }
  69. // Store the output in the provided variable.
  70. status.GetMakefile().AddDefinition(outvar, output);
  71. return true;
  72. }
  73. bool HandleToUpperCommand(std::vector<std::string> const& args,
  74. cmExecutionStatus& status)
  75. {
  76. return HandleToUpperLowerCommand(args, true, status);
  77. }
  78. bool HandleToLowerCommand(std::vector<std::string> const& args,
  79. cmExecutionStatus& status)
  80. {
  81. return HandleToUpperLowerCommand(args, false, status);
  82. }
  83. bool HandleAsciiCommand(std::vector<std::string> const& args,
  84. cmExecutionStatus& status)
  85. {
  86. if (args.size() < 3) {
  87. status.SetError("No output variable specified");
  88. return false;
  89. }
  90. std::string::size_type cc;
  91. std::string const& outvar = args.back();
  92. std::string output;
  93. for (cc = 1; cc < args.size() - 1; cc++) {
  94. int ch = atoi(args[cc].c_str());
  95. if (ch > 0 && ch < 256) {
  96. output += static_cast<char>(ch);
  97. } else {
  98. std::string error =
  99. cmStrCat("Character with code ", args[cc], " does not exist.");
  100. status.SetError(error);
  101. return false;
  102. }
  103. }
  104. // Store the output in the provided variable.
  105. status.GetMakefile().AddDefinition(outvar, output);
  106. return true;
  107. }
  108. bool HandleHexCommand(std::vector<std::string> const& args,
  109. cmExecutionStatus& status)
  110. {
  111. if (args.size() != 3) {
  112. status.SetError("Incorrect number of arguments");
  113. return false;
  114. }
  115. auto const& instr = args[1];
  116. auto const& outvar = args[2];
  117. std::string output(instr.size() * 2, ' ');
  118. std::string::size_type hexIndex = 0;
  119. for (auto const& c : instr) {
  120. sprintf(&output[hexIndex], "%.2x", static_cast<unsigned char>(c) & 0xFF);
  121. hexIndex += 2;
  122. }
  123. status.GetMakefile().AddDefinition(outvar, output);
  124. return true;
  125. }
  126. bool HandleConfigureCommand(std::vector<std::string> const& args,
  127. cmExecutionStatus& status)
  128. {
  129. if (args.size() < 2) {
  130. status.SetError("No input string specified.");
  131. return false;
  132. }
  133. if (args.size() < 3) {
  134. status.SetError("No output variable specified.");
  135. return false;
  136. }
  137. // Parse options.
  138. bool escapeQuotes = false;
  139. bool atOnly = false;
  140. for (unsigned int i = 3; i < args.size(); ++i) {
  141. if (args[i] == "@ONLY") {
  142. atOnly = true;
  143. } else if (args[i] == "ESCAPE_QUOTES") {
  144. escapeQuotes = true;
  145. } else {
  146. status.SetError(cmStrCat("Unrecognized argument \"", args[i], "\""));
  147. return false;
  148. }
  149. }
  150. // Configure the string.
  151. std::string output;
  152. status.GetMakefile().ConfigureString(args[1], output, atOnly, escapeQuotes);
  153. // Store the output in the provided variable.
  154. status.GetMakefile().AddDefinition(args[2], output);
  155. return true;
  156. }
  157. bool HandleRegexCommand(std::vector<std::string> const& args,
  158. cmExecutionStatus& status)
  159. {
  160. if (args.size() < 2) {
  161. status.SetError("sub-command REGEX requires a mode to be specified.");
  162. return false;
  163. }
  164. std::string const& mode = args[1];
  165. if (mode == "MATCH") {
  166. if (args.size() < 5) {
  167. status.SetError("sub-command REGEX, mode MATCH needs "
  168. "at least 5 arguments total to command.");
  169. return false;
  170. }
  171. return RegexMatch(args, status);
  172. }
  173. if (mode == "MATCHALL") {
  174. if (args.size() < 5) {
  175. status.SetError("sub-command REGEX, mode MATCHALL needs "
  176. "at least 5 arguments total to command.");
  177. return false;
  178. }
  179. return RegexMatchAll(args, status);
  180. }
  181. if (mode == "REPLACE") {
  182. if (args.size() < 6) {
  183. status.SetError("sub-command REGEX, mode REPLACE needs "
  184. "at least 6 arguments total to command.");
  185. return false;
  186. }
  187. return RegexReplace(args, status);
  188. }
  189. std::string e = "sub-command REGEX does not recognize mode " + mode;
  190. status.SetError(e);
  191. return false;
  192. }
  193. bool RegexMatch(std::vector<std::string> const& args,
  194. cmExecutionStatus& status)
  195. {
  196. //"STRING(REGEX MATCH <regular_expression> <output variable>
  197. // <input> [<input>...])\n";
  198. std::string const& regex = args[2];
  199. std::string const& outvar = args[3];
  200. status.GetMakefile().ClearMatches();
  201. // Compile the regular expression.
  202. cmsys::RegularExpression re;
  203. if (!re.compile(regex.c_str())) {
  204. std::string e =
  205. "sub-command REGEX, mode MATCH failed to compile regex \"" + regex +
  206. "\".";
  207. status.SetError(e);
  208. return false;
  209. }
  210. // Concatenate all the last arguments together.
  211. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  212. // Scan through the input for all matches.
  213. std::string output;
  214. if (re.find(input)) {
  215. status.GetMakefile().StoreMatches(re);
  216. std::string::size_type l = re.start();
  217. std::string::size_type r = re.end();
  218. if (r - l == 0) {
  219. std::string e = "sub-command REGEX, mode MATCH regex \"" + regex +
  220. "\" matched an empty string.";
  221. status.SetError(e);
  222. return false;
  223. }
  224. output = input.substr(l, r - l);
  225. }
  226. // Store the output in the provided variable.
  227. status.GetMakefile().AddDefinition(outvar, output);
  228. return true;
  229. }
  230. bool RegexMatchAll(std::vector<std::string> const& args,
  231. cmExecutionStatus& status)
  232. {
  233. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
  234. // [<input>...])\n";
  235. std::string const& regex = args[2];
  236. std::string const& outvar = args[3];
  237. status.GetMakefile().ClearMatches();
  238. // Compile the regular expression.
  239. cmsys::RegularExpression re;
  240. if (!re.compile(regex.c_str())) {
  241. std::string e =
  242. "sub-command REGEX, mode MATCHALL failed to compile regex \"" + regex +
  243. "\".";
  244. status.SetError(e);
  245. return false;
  246. }
  247. // Concatenate all the last arguments together.
  248. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  249. // Scan through the input for all matches.
  250. std::string output;
  251. const char* p = input.c_str();
  252. while (re.find(p)) {
  253. status.GetMakefile().ClearMatches();
  254. status.GetMakefile().StoreMatches(re);
  255. std::string::size_type l = re.start();
  256. std::string::size_type r = re.end();
  257. if (r - l == 0) {
  258. std::string e = "sub-command REGEX, mode MATCHALL regex \"" + regex +
  259. "\" matched an empty string.";
  260. status.SetError(e);
  261. return false;
  262. }
  263. if (!output.empty()) {
  264. output += ";";
  265. }
  266. output += std::string(p + l, r - l);
  267. p += r;
  268. }
  269. // Store the output in the provided variable.
  270. status.GetMakefile().AddDefinition(outvar, output);
  271. return true;
  272. }
  273. bool RegexReplace(std::vector<std::string> const& args,
  274. cmExecutionStatus& status)
  275. {
  276. //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
  277. // <output variable> <input> [<input>...])\n"
  278. std::string const& regex = args[2];
  279. std::string const& replace = args[3];
  280. std::string const& outvar = args[4];
  281. cmStringReplaceHelper replaceHelper(regex, replace, &status.GetMakefile());
  282. if (!replaceHelper.IsReplaceExpressionValid()) {
  283. status.SetError(
  284. "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
  285. return false;
  286. }
  287. status.GetMakefile().ClearMatches();
  288. if (!replaceHelper.IsRegularExpressionValid()) {
  289. std::string e =
  290. "sub-command REGEX, mode REPLACE failed to compile regex \"" + regex +
  291. "\".";
  292. status.SetError(e);
  293. return false;
  294. }
  295. // Concatenate all the last arguments together.
  296. const std::string input =
  297. cmJoin(cmMakeRange(args).advance(5), std::string());
  298. std::string output;
  299. if (!replaceHelper.Replace(input, output)) {
  300. status.SetError(
  301. "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
  302. return false;
  303. }
  304. // Store the output in the provided variable.
  305. status.GetMakefile().AddDefinition(outvar, output);
  306. return true;
  307. }
  308. bool HandleFindCommand(std::vector<std::string> const& args,
  309. cmExecutionStatus& status)
  310. {
  311. // check if all required parameters were passed
  312. if (args.size() < 4 || args.size() > 5) {
  313. status.SetError("sub-command FIND requires 3 or 4 parameters.");
  314. return false;
  315. }
  316. // check if the reverse flag was set or not
  317. bool reverseMode = false;
  318. if (args.size() == 5 && args[4] == "REVERSE") {
  319. reverseMode = true;
  320. }
  321. // if we have 5 arguments the last one must be REVERSE
  322. if (args.size() == 5 && args[4] != "REVERSE") {
  323. status.SetError("sub-command FIND: unknown last parameter");
  324. return false;
  325. }
  326. // local parameter names.
  327. const std::string& sstring = args[1];
  328. const std::string& schar = args[2];
  329. const std::string& outvar = args[3];
  330. // ensure that the user cannot accidentally specify REVERSE as a variable
  331. if (outvar == "REVERSE") {
  332. status.SetError("sub-command FIND does not allow one to select REVERSE as "
  333. "the output variable. "
  334. "Maybe you missed the actual output variable?");
  335. return false;
  336. }
  337. // try to find the character and return its position
  338. size_t pos;
  339. if (!reverseMode) {
  340. pos = sstring.find(schar);
  341. } else {
  342. pos = sstring.rfind(schar);
  343. }
  344. if (std::string::npos != pos) {
  345. status.GetMakefile().AddDefinition(outvar, std::to_string(pos));
  346. return true;
  347. }
  348. // the character was not found, but this is not really an error
  349. status.GetMakefile().AddDefinition(outvar, "-1");
  350. return true;
  351. }
  352. bool HandleCompareCommand(std::vector<std::string> const& args,
  353. cmExecutionStatus& status)
  354. {
  355. if (args.size() < 2) {
  356. status.SetError("sub-command COMPARE requires a mode to be specified.");
  357. return false;
  358. }
  359. std::string const& mode = args[1];
  360. if ((mode == "EQUAL") || (mode == "NOTEQUAL") || (mode == "LESS") ||
  361. (mode == "LESS_EQUAL") || (mode == "GREATER") ||
  362. (mode == "GREATER_EQUAL")) {
  363. if (args.size() < 5) {
  364. std::string e =
  365. cmStrCat("sub-command COMPARE, mode ", mode,
  366. " needs at least 5 arguments total to command.");
  367. status.SetError(e);
  368. return false;
  369. }
  370. const std::string& left = args[2];
  371. const std::string& right = args[3];
  372. const std::string& outvar = args[4];
  373. bool result;
  374. if (mode == "LESS") {
  375. result = (left < right);
  376. } else if (mode == "LESS_EQUAL") {
  377. result = (left <= right);
  378. } else if (mode == "GREATER") {
  379. result = (left > right);
  380. } else if (mode == "GREATER_EQUAL") {
  381. result = (left >= right);
  382. } else if (mode == "EQUAL") {
  383. result = (left == right);
  384. } else // if(mode == "NOTEQUAL")
  385. {
  386. result = !(left == right);
  387. }
  388. if (result) {
  389. status.GetMakefile().AddDefinition(outvar, "1");
  390. } else {
  391. status.GetMakefile().AddDefinition(outvar, "0");
  392. }
  393. return true;
  394. }
  395. std::string e = "sub-command COMPARE does not recognize mode " + mode;
  396. status.SetError(e);
  397. return false;
  398. }
  399. bool HandleReplaceCommand(std::vector<std::string> const& args,
  400. cmExecutionStatus& status)
  401. {
  402. if (args.size() < 5) {
  403. status.SetError("sub-command REPLACE requires at least four arguments.");
  404. return false;
  405. }
  406. const std::string& matchExpression = args[1];
  407. const std::string& replaceExpression = args[2];
  408. const std::string& variableName = args[3];
  409. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  410. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  411. replaceExpression.c_str());
  412. status.GetMakefile().AddDefinition(variableName, input);
  413. return true;
  414. }
  415. bool HandleSubstringCommand(std::vector<std::string> const& args,
  416. cmExecutionStatus& status)
  417. {
  418. if (args.size() != 5) {
  419. status.SetError("sub-command SUBSTRING requires four arguments.");
  420. return false;
  421. }
  422. const std::string& stringValue = args[1];
  423. int begin = atoi(args[2].c_str());
  424. int end = atoi(args[3].c_str());
  425. const std::string& variableName = args[4];
  426. size_t stringLength = stringValue.size();
  427. int intStringLength = static_cast<int>(stringLength);
  428. if (begin < 0 || begin > intStringLength) {
  429. status.SetError(
  430. cmStrCat("begin index: ", begin, " is out of range 0 - ", stringLength));
  431. return false;
  432. }
  433. if (end < -1) {
  434. status.SetError(cmStrCat("end index: ", end, " should be -1 or greater"));
  435. return false;
  436. }
  437. status.GetMakefile().AddDefinition(variableName,
  438. stringValue.substr(begin, end));
  439. return true;
  440. }
  441. bool HandleLengthCommand(std::vector<std::string> const& args,
  442. cmExecutionStatus& status)
  443. {
  444. if (args.size() != 3) {
  445. status.SetError("sub-command LENGTH requires two arguments.");
  446. return false;
  447. }
  448. const std::string& stringValue = args[1];
  449. const std::string& variableName = args[2];
  450. size_t length = stringValue.size();
  451. char buffer[1024];
  452. sprintf(buffer, "%d", static_cast<int>(length));
  453. status.GetMakefile().AddDefinition(variableName, buffer);
  454. return true;
  455. }
  456. bool HandleAppendCommand(std::vector<std::string> const& args,
  457. cmExecutionStatus& status)
  458. {
  459. if (args.size() < 2) {
  460. status.SetError("sub-command APPEND requires at least one argument.");
  461. return false;
  462. }
  463. // Skip if nothing to append.
  464. if (args.size() < 3) {
  465. return true;
  466. }
  467. const std::string& variable = args[1];
  468. std::string value;
  469. const char* oldValue = status.GetMakefile().GetDefinition(variable);
  470. if (oldValue) {
  471. value = oldValue;
  472. }
  473. value += cmJoin(cmMakeRange(args).advance(2), std::string());
  474. status.GetMakefile().AddDefinition(variable, value);
  475. return true;
  476. }
  477. bool HandlePrependCommand(std::vector<std::string> const& args,
  478. cmExecutionStatus& status)
  479. {
  480. if (args.size() < 2) {
  481. status.SetError("sub-command PREPEND requires at least one argument.");
  482. return false;
  483. }
  484. // Skip if nothing to prepend.
  485. if (args.size() < 3) {
  486. return true;
  487. }
  488. const std::string& variable = args[1];
  489. std::string value = cmJoin(cmMakeRange(args).advance(2), std::string());
  490. const char* oldValue = status.GetMakefile().GetDefinition(variable);
  491. if (oldValue) {
  492. value += oldValue;
  493. }
  494. status.GetMakefile().AddDefinition(variable, value);
  495. return true;
  496. }
  497. bool HandleConcatCommand(std::vector<std::string> const& args,
  498. cmExecutionStatus& status)
  499. {
  500. if (args.size() < 2) {
  501. status.SetError("sub-command CONCAT requires at least one argument.");
  502. return false;
  503. }
  504. return joinImpl(args, std::string(), 1, status.GetMakefile());
  505. }
  506. bool HandleJoinCommand(std::vector<std::string> const& args,
  507. cmExecutionStatus& status)
  508. {
  509. if (args.size() < 3) {
  510. status.SetError("sub-command JOIN requires at least two arguments.");
  511. return false;
  512. }
  513. return joinImpl(args, args[1], 2, status.GetMakefile());
  514. }
  515. bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
  516. const size_t varIdx, cmMakefile& makefile)
  517. {
  518. std::string const& variableName = args[varIdx];
  519. // NOTE Items to concat/join placed right after the variable for
  520. // both `CONCAT` and `JOIN` sub-commands.
  521. std::string value = cmJoin(cmMakeRange(args).advance(varIdx + 1), glue);
  522. makefile.AddDefinition(variableName, value);
  523. return true;
  524. }
  525. bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args,
  526. cmExecutionStatus& status)
  527. {
  528. if (args.size() != 3) {
  529. status.SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  530. return false;
  531. }
  532. const std::string& input = args[1];
  533. const std::string& variableName = args[2];
  534. status.GetMakefile().AddDefinition(variableName,
  535. cmSystemTools::MakeCidentifier(input));
  536. return true;
  537. }
  538. bool HandleGenexStripCommand(std::vector<std::string> const& args,
  539. cmExecutionStatus& status)
  540. {
  541. if (args.size() != 3) {
  542. status.SetError("sub-command GENEX_STRIP requires two arguments.");
  543. return false;
  544. }
  545. const std::string& input = args[1];
  546. std::string result = cmGeneratorExpression::Preprocess(
  547. input, cmGeneratorExpression::StripAllGeneratorExpressions);
  548. const std::string& variableName = args[2];
  549. status.GetMakefile().AddDefinition(variableName, result);
  550. return true;
  551. }
  552. bool HandleStripCommand(std::vector<std::string> const& args,
  553. cmExecutionStatus& status)
  554. {
  555. if (args.size() != 3) {
  556. status.SetError("sub-command STRIP requires two arguments.");
  557. return false;
  558. }
  559. const std::string& stringValue = args[1];
  560. const std::string& variableName = args[2];
  561. size_t inStringLength = stringValue.size();
  562. size_t startPos = inStringLength + 1;
  563. size_t endPos = 0;
  564. const char* ptr = stringValue.c_str();
  565. size_t cc;
  566. for (cc = 0; cc < inStringLength; ++cc) {
  567. if (!isspace(*ptr)) {
  568. if (startPos > inStringLength) {
  569. startPos = cc;
  570. }
  571. endPos = cc;
  572. }
  573. ++ptr;
  574. }
  575. size_t outLength = 0;
  576. // if the input string didn't contain any non-space characters, return
  577. // an empty string
  578. if (startPos > inStringLength) {
  579. outLength = 0;
  580. startPos = 0;
  581. } else {
  582. outLength = endPos - startPos + 1;
  583. }
  584. status.GetMakefile().AddDefinition(variableName,
  585. stringValue.substr(startPos, outLength));
  586. return true;
  587. }
  588. bool HandleRepeatCommand(std::vector<std::string> const& args,
  589. cmExecutionStatus& status)
  590. {
  591. cmMakefile& makefile = status.GetMakefile();
  592. // `string(REPEAT "<str>" <times> OUTPUT_VARIABLE)`
  593. enum ArgPos : std::size_t
  594. {
  595. SUB_COMMAND,
  596. VALUE,
  597. TIMES,
  598. OUTPUT_VARIABLE,
  599. TOTAL_ARGS
  600. };
  601. if (args.size() != ArgPos::TOTAL_ARGS) {
  602. makefile.IssueMessage(MessageType::FATAL_ERROR,
  603. "sub-command REPEAT requires three arguments.");
  604. return true;
  605. }
  606. unsigned long times;
  607. if (!cmStrToULong(args[ArgPos::TIMES], &times)) {
  608. makefile.IssueMessage(MessageType::FATAL_ERROR,
  609. "repeat count is not a positive number.");
  610. return true;
  611. }
  612. const auto& stringValue = args[ArgPos::VALUE];
  613. const auto& variableName = args[ArgPos::OUTPUT_VARIABLE];
  614. const auto inStringLength = stringValue.size();
  615. std::string result;
  616. switch (inStringLength) {
  617. case 0u:
  618. // Nothing to do for zero length input strings
  619. break;
  620. case 1u:
  621. // NOTE If the string to repeat consists of the only character,
  622. // use the appropriate constructor.
  623. result = std::string(times, stringValue[0]);
  624. break;
  625. default:
  626. result = std::string(inStringLength * times, char{});
  627. for (auto i = 0u; i < times; ++i) {
  628. std::copy(cm::cbegin(stringValue), cm::cend(stringValue),
  629. &result[i * inStringLength]);
  630. }
  631. break;
  632. }
  633. makefile.AddDefinition(variableName, result);
  634. return true;
  635. }
  636. bool HandleRandomCommand(std::vector<std::string> const& args,
  637. cmExecutionStatus& status)
  638. {
  639. if (args.size() < 2 || args.size() == 3 || args.size() == 5) {
  640. status.SetError("sub-command RANDOM requires at least one argument.");
  641. return false;
  642. }
  643. static bool seeded = false;
  644. bool force_seed = false;
  645. unsigned int seed = 0;
  646. int length = 5;
  647. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  648. "QWERTYUIOPASDFGHJKLZXCVBNM"
  649. "0123456789";
  650. std::string alphabet;
  651. if (args.size() > 3) {
  652. size_t i = 1;
  653. size_t stopAt = args.size() - 2;
  654. for (; i < stopAt; ++i) {
  655. if (args[i] == "LENGTH") {
  656. ++i;
  657. length = atoi(args[i].c_str());
  658. } else if (args[i] == "ALPHABET") {
  659. ++i;
  660. alphabet = args[i];
  661. } else if (args[i] == "RANDOM_SEED") {
  662. ++i;
  663. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  664. force_seed = true;
  665. }
  666. }
  667. }
  668. if (alphabet.empty()) {
  669. alphabet = cmStringCommandDefaultAlphabet;
  670. }
  671. double sizeofAlphabet = static_cast<double>(alphabet.size());
  672. if (sizeofAlphabet < 1) {
  673. status.SetError("sub-command RANDOM invoked with bad alphabet.");
  674. return false;
  675. }
  676. if (length < 1) {
  677. status.SetError("sub-command RANDOM invoked with bad length.");
  678. return false;
  679. }
  680. const std::string& variableName = args.back();
  681. std::vector<char> result;
  682. if (!seeded || force_seed) {
  683. seeded = true;
  684. srand(force_seed ? seed : cmSystemTools::RandomSeed());
  685. }
  686. const char* alphaPtr = alphabet.c_str();
  687. for (int cc = 0; cc < length; cc++) {
  688. int idx = static_cast<int>(sizeofAlphabet * rand() / (RAND_MAX + 1.0));
  689. result.push_back(*(alphaPtr + idx));
  690. }
  691. result.push_back(0);
  692. status.GetMakefile().AddDefinition(variableName, result.data());
  693. return true;
  694. }
  695. bool HandleTimestampCommand(std::vector<std::string> const& args,
  696. cmExecutionStatus& status)
  697. {
  698. if (args.size() < 2) {
  699. status.SetError("sub-command TIMESTAMP requires at least one argument.");
  700. return false;
  701. }
  702. if (args.size() > 4) {
  703. status.SetError("sub-command TIMESTAMP takes at most three arguments.");
  704. return false;
  705. }
  706. unsigned int argsIndex = 1;
  707. const std::string& outputVariable = args[argsIndex++];
  708. std::string formatString;
  709. if (args.size() > argsIndex && args[argsIndex] != "UTC") {
  710. formatString = args[argsIndex++];
  711. }
  712. bool utcFlag = false;
  713. if (args.size() > argsIndex) {
  714. if (args[argsIndex] == "UTC") {
  715. utcFlag = true;
  716. } else {
  717. std::string e = " TIMESTAMP sub-command does not recognize option " +
  718. args[argsIndex] + ".";
  719. status.SetError(e);
  720. return false;
  721. }
  722. }
  723. cmTimestamp timestamp;
  724. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  725. status.GetMakefile().AddDefinition(outputVariable, result);
  726. return true;
  727. }
  728. bool HandleUuidCommand(std::vector<std::string> const& args,
  729. cmExecutionStatus& status)
  730. {
  731. #if !defined(CMAKE_BOOTSTRAP)
  732. unsigned int argsIndex = 1;
  733. if (args.size() < 2) {
  734. status.SetError("UUID sub-command requires an output variable.");
  735. return false;
  736. }
  737. const std::string& outputVariable = args[argsIndex++];
  738. std::string uuidNamespaceString;
  739. std::string uuidName;
  740. std::string uuidType;
  741. bool uuidUpperCase = false;
  742. while (args.size() > argsIndex) {
  743. if (args[argsIndex] == "NAMESPACE") {
  744. ++argsIndex;
  745. if (argsIndex >= args.size()) {
  746. status.SetError("UUID sub-command, NAMESPACE requires a value.");
  747. return false;
  748. }
  749. uuidNamespaceString = args[argsIndex++];
  750. } else if (args[argsIndex] == "NAME") {
  751. ++argsIndex;
  752. if (argsIndex >= args.size()) {
  753. status.SetError("UUID sub-command, NAME requires a value.");
  754. return false;
  755. }
  756. uuidName = args[argsIndex++];
  757. } else if (args[argsIndex] == "TYPE") {
  758. ++argsIndex;
  759. if (argsIndex >= args.size()) {
  760. status.SetError("UUID sub-command, TYPE requires a value.");
  761. return false;
  762. }
  763. uuidType = args[argsIndex++];
  764. } else if (args[argsIndex] == "UPPER") {
  765. ++argsIndex;
  766. uuidUpperCase = true;
  767. } else {
  768. std::string e =
  769. "UUID sub-command does not recognize option " + args[argsIndex] + ".";
  770. status.SetError(e);
  771. return false;
  772. }
  773. }
  774. std::string uuid;
  775. cmUuid uuidGenerator;
  776. std::vector<unsigned char> uuidNamespace;
  777. if (!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace)) {
  778. status.SetError("UUID sub-command, malformed NAMESPACE UUID.");
  779. return false;
  780. }
  781. if (uuidType == "MD5") {
  782. uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
  783. } else if (uuidType == "SHA1") {
  784. uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
  785. } else {
  786. std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
  787. status.SetError(e);
  788. return false;
  789. }
  790. if (uuid.empty()) {
  791. status.SetError("UUID sub-command, generation failed.");
  792. return false;
  793. }
  794. if (uuidUpperCase) {
  795. uuid = cmSystemTools::UpperCase(uuid);
  796. }
  797. status.GetMakefile().AddDefinition(outputVariable, uuid);
  798. return true;
  799. #else
  800. status.SetError(cmStrCat(args[0], " not available during bootstrap"));
  801. return false;
  802. #endif
  803. }
  804. } // namespace
  805. bool cmStringCommand(std::vector<std::string> const& args,
  806. cmExecutionStatus& status)
  807. {
  808. if (args.empty()) {
  809. status.SetError("must be called with at least one argument.");
  810. return false;
  811. }
  812. static cmSubcommandTable const subcommand{
  813. { "REGEX"_s, HandleRegexCommand },
  814. { "REPLACE"_s, HandleReplaceCommand },
  815. { "MD5"_s, HandleHashCommand },
  816. { "SHA1"_s, HandleHashCommand },
  817. { "SHA224"_s, HandleHashCommand },
  818. { "SHA256"_s, HandleHashCommand },
  819. { "SHA384"_s, HandleHashCommand },
  820. { "SHA512"_s, HandleHashCommand },
  821. { "SHA3_224"_s, HandleHashCommand },
  822. { "SHA3_256"_s, HandleHashCommand },
  823. { "SHA3_384"_s, HandleHashCommand },
  824. { "SHA3_512"_s, HandleHashCommand },
  825. { "TOLOWER"_s, HandleToLowerCommand },
  826. { "TOUPPER"_s, HandleToUpperCommand },
  827. { "COMPARE"_s, HandleCompareCommand },
  828. { "ASCII"_s, HandleAsciiCommand },
  829. { "HEX"_s, HandleHexCommand },
  830. { "CONFIGURE"_s, HandleConfigureCommand },
  831. { "LENGTH"_s, HandleLengthCommand },
  832. { "APPEND"_s, HandleAppendCommand },
  833. { "PREPEND"_s, HandlePrependCommand },
  834. { "CONCAT"_s, HandleConcatCommand },
  835. { "JOIN"_s, HandleJoinCommand },
  836. { "SUBSTRING"_s, HandleSubstringCommand },
  837. { "STRIP"_s, HandleStripCommand },
  838. { "REPEAT"_s, HandleRepeatCommand },
  839. { "RANDOM"_s, HandleRandomCommand },
  840. { "FIND"_s, HandleFindCommand },
  841. { "TIMESTAMP"_s, HandleTimestampCommand },
  842. { "MAKE_C_IDENTIFIER"_s, HandleMakeCIdentifierCommand },
  843. { "GENEX_STRIP"_s, HandleGenexStripCommand },
  844. { "UUID"_s, HandleUuidCommand },
  845. };
  846. return subcommand(args[0], args, status);
  847. }