cmStringCommand.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 <memory>
  10. #include <sstream>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "cmAlgorithms.h"
  14. #include "cmCryptoHash.h"
  15. #include "cmGeneratorExpression.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmRange.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_BUILD_WITH_CMAKE)
  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.c_str());
  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.c_str());
  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.c_str());
  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.c_str());
  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.c_str());
  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.c_str());
  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.c_str());
  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().c_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.c_str());
  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,
  487. stringValue.substr(begin, end).c_str());
  488. return true;
  489. }
  490. bool cmStringCommand::HandleLengthCommand(std::vector<std::string> const& args)
  491. {
  492. if (args.size() != 3) {
  493. this->SetError("sub-command LENGTH requires two arguments.");
  494. return false;
  495. }
  496. const std::string& stringValue = args[1];
  497. const std::string& variableName = args[2];
  498. size_t length = stringValue.size();
  499. char buffer[1024];
  500. sprintf(buffer, "%d", static_cast<int>(length));
  501. this->Makefile->AddDefinition(variableName, buffer);
  502. return true;
  503. }
  504. bool cmStringCommand::HandleAppendCommand(std::vector<std::string> const& args)
  505. {
  506. if (args.size() < 2) {
  507. this->SetError("sub-command APPEND requires at least one argument.");
  508. return false;
  509. }
  510. // Skip if nothing to append.
  511. if (args.size() < 3) {
  512. return true;
  513. }
  514. const std::string& variable = args[1];
  515. std::string value;
  516. const char* oldValue = this->Makefile->GetDefinition(variable);
  517. if (oldValue) {
  518. value = oldValue;
  519. }
  520. value += cmJoin(cmMakeRange(args).advance(2), std::string());
  521. this->Makefile->AddDefinition(variable, value.c_str());
  522. return true;
  523. }
  524. bool cmStringCommand::HandlePrependCommand(
  525. std::vector<std::string> const& args)
  526. {
  527. if (args.size() < 2) {
  528. this->SetError("sub-command PREPEND requires at least one argument.");
  529. return false;
  530. }
  531. // Skip if nothing to prepend.
  532. if (args.size() < 3) {
  533. return true;
  534. }
  535. const std::string& variable = args[1];
  536. std::string value = cmJoin(cmMakeRange(args).advance(2), std::string());
  537. const char* oldValue = this->Makefile->GetDefinition(variable);
  538. if (oldValue) {
  539. value += oldValue;
  540. }
  541. this->Makefile->AddDefinition(variable, value.c_str());
  542. return true;
  543. }
  544. bool cmStringCommand::HandleConcatCommand(std::vector<std::string> const& args)
  545. {
  546. if (args.size() < 2) {
  547. this->SetError("sub-command CONCAT requires at least one argument.");
  548. return false;
  549. }
  550. return this->joinImpl(args, std::string(), 1);
  551. }
  552. bool cmStringCommand::HandleJoinCommand(std::vector<std::string> const& args)
  553. {
  554. if (args.size() < 3) {
  555. this->SetError("sub-command JOIN requires at least two arguments.");
  556. return false;
  557. }
  558. return this->joinImpl(args, args[1], 2);
  559. }
  560. bool cmStringCommand::joinImpl(std::vector<std::string> const& args,
  561. std::string const& glue, const size_t varIdx)
  562. {
  563. std::string const& variableName = args[varIdx];
  564. // NOTE Items to concat/join placed right after the variable for
  565. // both `CONCAT` and `JOIN` sub-commands.
  566. std::string value = cmJoin(cmMakeRange(args).advance(varIdx + 1), glue);
  567. this->Makefile->AddDefinition(variableName, value.c_str());
  568. return true;
  569. }
  570. bool cmStringCommand::HandleMakeCIdentifierCommand(
  571. std::vector<std::string> const& args)
  572. {
  573. if (args.size() != 3) {
  574. this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  575. return false;
  576. }
  577. const std::string& input = args[1];
  578. const std::string& variableName = args[2];
  579. this->Makefile->AddDefinition(variableName,
  580. cmSystemTools::MakeCidentifier(input).c_str());
  581. return true;
  582. }
  583. bool cmStringCommand::HandleGenexStripCommand(
  584. std::vector<std::string> const& args)
  585. {
  586. if (args.size() != 3) {
  587. this->SetError("sub-command GENEX_STRIP requires two arguments.");
  588. return false;
  589. }
  590. const std::string& input = args[1];
  591. std::string result = cmGeneratorExpression::Preprocess(
  592. input, cmGeneratorExpression::StripAllGeneratorExpressions);
  593. const std::string& variableName = args[2];
  594. this->Makefile->AddDefinition(variableName, result.c_str());
  595. return true;
  596. }
  597. bool cmStringCommand::HandleStripCommand(std::vector<std::string> const& args)
  598. {
  599. if (args.size() != 3) {
  600. this->SetError("sub-command STRIP requires two arguments.");
  601. return false;
  602. }
  603. const std::string& stringValue = args[1];
  604. const std::string& variableName = args[2];
  605. size_t inStringLength = stringValue.size();
  606. size_t startPos = inStringLength + 1;
  607. size_t endPos = 0;
  608. const char* ptr = stringValue.c_str();
  609. size_t cc;
  610. for (cc = 0; cc < inStringLength; ++cc) {
  611. if (!isspace(*ptr)) {
  612. if (startPos > inStringLength) {
  613. startPos = cc;
  614. }
  615. endPos = cc;
  616. }
  617. ++ptr;
  618. }
  619. size_t outLength = 0;
  620. // if the input string didn't contain any non-space characters, return
  621. // an empty string
  622. if (startPos > inStringLength) {
  623. outLength = 0;
  624. startPos = 0;
  625. } else {
  626. outLength = endPos - startPos + 1;
  627. }
  628. this->Makefile->AddDefinition(
  629. variableName, stringValue.substr(startPos, outLength).c_str());
  630. return true;
  631. }
  632. bool cmStringCommand::HandleRepeatCommand(std::vector<std::string> const& args)
  633. {
  634. // `string(REPEAT "<str>" <times> OUTPUT_VARIABLE)`
  635. enum ArgPos : std::size_t
  636. {
  637. SUB_COMMAND,
  638. VALUE,
  639. TIMES,
  640. OUTPUT_VARIABLE,
  641. TOTAL_ARGS
  642. };
  643. if (args.size() != ArgPos::TOTAL_ARGS) {
  644. this->Makefile->IssueMessage(
  645. MessageType::FATAL_ERROR,
  646. "sub-command REPEAT requires three arguments.");
  647. return true;
  648. }
  649. unsigned long times;
  650. if (!cmSystemTools::StringToULong(args[ArgPos::TIMES].c_str(), &times)) {
  651. this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
  652. "repeat count is not a positive number.");
  653. return true;
  654. }
  655. const auto& stringValue = args[ArgPos::VALUE];
  656. const auto& variableName = args[ArgPos::OUTPUT_VARIABLE];
  657. const auto inStringLength = stringValue.size();
  658. std::string result;
  659. switch (inStringLength) {
  660. case 0u:
  661. // Nothing to do for zero length input strings
  662. break;
  663. case 1u:
  664. // NOTE If the string to repeat consists of the only character,
  665. // use the appropriate constructor.
  666. result = std::string(times, stringValue[0]);
  667. break;
  668. default:
  669. result = std::string(inStringLength * times, char{});
  670. for (auto i = 0u; i < times; ++i) {
  671. std::copy(cm::cbegin(stringValue), cm::cend(stringValue),
  672. &result[i * inStringLength]);
  673. }
  674. break;
  675. }
  676. this->Makefile->AddDefinition(variableName, result.c_str());
  677. return true;
  678. }
  679. bool cmStringCommand::HandleRandomCommand(std::vector<std::string> const& args)
  680. {
  681. if (args.size() < 2 || args.size() == 3 || args.size() == 5) {
  682. this->SetError("sub-command RANDOM requires at least one argument.");
  683. return false;
  684. }
  685. static bool seeded = false;
  686. bool force_seed = false;
  687. unsigned int seed = 0;
  688. int length = 5;
  689. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  690. "QWERTYUIOPASDFGHJKLZXCVBNM"
  691. "0123456789";
  692. std::string alphabet;
  693. if (args.size() > 3) {
  694. size_t i = 1;
  695. size_t stopAt = args.size() - 2;
  696. for (; i < stopAt; ++i) {
  697. if (args[i] == "LENGTH") {
  698. ++i;
  699. length = atoi(args[i].c_str());
  700. } else if (args[i] == "ALPHABET") {
  701. ++i;
  702. alphabet = args[i];
  703. } else if (args[i] == "RANDOM_SEED") {
  704. ++i;
  705. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  706. force_seed = true;
  707. }
  708. }
  709. }
  710. if (alphabet.empty()) {
  711. alphabet = cmStringCommandDefaultAlphabet;
  712. }
  713. double sizeofAlphabet = static_cast<double>(alphabet.size());
  714. if (sizeofAlphabet < 1) {
  715. this->SetError("sub-command RANDOM invoked with bad alphabet.");
  716. return false;
  717. }
  718. if (length < 1) {
  719. this->SetError("sub-command RANDOM invoked with bad length.");
  720. return false;
  721. }
  722. const std::string& variableName = args.back();
  723. std::vector<char> result;
  724. if (!seeded || force_seed) {
  725. seeded = true;
  726. srand(force_seed ? seed : cmSystemTools::RandomSeed());
  727. }
  728. const char* alphaPtr = alphabet.c_str();
  729. for (int cc = 0; cc < length; cc++) {
  730. int idx = static_cast<int>(sizeofAlphabet * rand() / (RAND_MAX + 1.0));
  731. result.push_back(*(alphaPtr + idx));
  732. }
  733. result.push_back(0);
  734. this->Makefile->AddDefinition(variableName, result.data());
  735. return true;
  736. }
  737. bool cmStringCommand::HandleTimestampCommand(
  738. std::vector<std::string> const& args)
  739. {
  740. if (args.size() < 2) {
  741. this->SetError("sub-command TIMESTAMP requires at least one argument.");
  742. return false;
  743. }
  744. if (args.size() > 4) {
  745. this->SetError("sub-command TIMESTAMP takes at most three arguments.");
  746. return false;
  747. }
  748. unsigned int argsIndex = 1;
  749. const std::string& outputVariable = args[argsIndex++];
  750. std::string formatString;
  751. if (args.size() > argsIndex && args[argsIndex] != "UTC") {
  752. formatString = args[argsIndex++];
  753. }
  754. bool utcFlag = false;
  755. if (args.size() > argsIndex) {
  756. if (args[argsIndex] == "UTC") {
  757. utcFlag = true;
  758. } else {
  759. std::string e = " TIMESTAMP sub-command does not recognize option " +
  760. args[argsIndex] + ".";
  761. this->SetError(e);
  762. return false;
  763. }
  764. }
  765. cmTimestamp timestamp;
  766. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  767. this->Makefile->AddDefinition(outputVariable, result.c_str());
  768. return true;
  769. }
  770. bool cmStringCommand::HandleUuidCommand(std::vector<std::string> const& args)
  771. {
  772. #if defined(CMAKE_BUILD_WITH_CMAKE)
  773. unsigned int argsIndex = 1;
  774. if (args.size() < 2) {
  775. this->SetError("UUID sub-command requires an output variable.");
  776. return false;
  777. }
  778. const std::string& outputVariable = args[argsIndex++];
  779. std::string uuidNamespaceString;
  780. std::string uuidName;
  781. std::string uuidType;
  782. bool uuidUpperCase = false;
  783. while (args.size() > argsIndex) {
  784. if (args[argsIndex] == "NAMESPACE") {
  785. ++argsIndex;
  786. if (argsIndex >= args.size()) {
  787. this->SetError("UUID sub-command, NAMESPACE requires a value.");
  788. return false;
  789. }
  790. uuidNamespaceString = args[argsIndex++];
  791. } else if (args[argsIndex] == "NAME") {
  792. ++argsIndex;
  793. if (argsIndex >= args.size()) {
  794. this->SetError("UUID sub-command, NAME requires a value.");
  795. return false;
  796. }
  797. uuidName = args[argsIndex++];
  798. } else if (args[argsIndex] == "TYPE") {
  799. ++argsIndex;
  800. if (argsIndex >= args.size()) {
  801. this->SetError("UUID sub-command, TYPE requires a value.");
  802. return false;
  803. }
  804. uuidType = args[argsIndex++];
  805. } else if (args[argsIndex] == "UPPER") {
  806. ++argsIndex;
  807. uuidUpperCase = true;
  808. } else {
  809. std::string e =
  810. "UUID sub-command does not recognize option " + args[argsIndex] + ".";
  811. this->SetError(e);
  812. return false;
  813. }
  814. }
  815. std::string uuid;
  816. cmUuid uuidGenerator;
  817. std::vector<unsigned char> uuidNamespace;
  818. if (!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace)) {
  819. this->SetError("UUID sub-command, malformed NAMESPACE UUID.");
  820. return false;
  821. }
  822. if (uuidType == "MD5") {
  823. uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
  824. } else if (uuidType == "SHA1") {
  825. uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
  826. } else {
  827. std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
  828. this->SetError(e);
  829. return false;
  830. }
  831. if (uuid.empty()) {
  832. this->SetError("UUID sub-command, generation failed.");
  833. return false;
  834. }
  835. if (uuidUpperCase) {
  836. uuid = cmSystemTools::UpperCase(uuid);
  837. }
  838. this->Makefile->AddDefinition(outputVariable, uuid.c_str());
  839. return true;
  840. #else
  841. std::ostringstream e;
  842. e << args[0] << " not available during bootstrap";
  843. this->SetError(e.str().c_str());
  844. return false;
  845. #endif
  846. }