cmStringCommand.cxx 26 KB

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