cmStringCommand.cxx 26 KB

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