cmStringCommand.cxx 26 KB

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