cmStringCommand.cxx 29 KB

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