cmStringCommand.cxx 26 KB

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