cmStringCommand.cxx 25 KB

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