cmStringCommand.cxx 25 KB

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