cmStringCommand.cxx 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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->Makefile->ClearMatches();
  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->Makefile->StoreMatches(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->Makefile->ClearMatches();
  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->Makefile->StoreMatches(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->Makefile->ClearMatches();
  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->Makefile->StoreMatches(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. bool cmStringCommand::HandleFindCommand(std::vector<std::string> const&
  501. args)
  502. {
  503. // check if all required parameters were passed
  504. if(args.size() < 4 || args.size() > 5)
  505. {
  506. this->SetError("sub-command FIND requires 3 or 4 parameters.");
  507. return false;
  508. }
  509. // check if the reverse flag was set or not
  510. bool reverseMode = false;
  511. if(args.size() == 5 && args[4] == "REVERSE")
  512. {
  513. reverseMode = true;
  514. }
  515. // if we have 5 arguments the last one must be REVERSE
  516. if(args.size() == 5 && args[4] != "REVERSE")
  517. {
  518. this->SetError("sub-command FIND: unknown last parameter");
  519. return false;
  520. }
  521. // local parameter names.
  522. const std::string& sstring = args[1];
  523. const std::string& schar = args[2];
  524. const std::string& outvar = args[3];
  525. // ensure that the user cannot accidentally specify REVERSE as a variable
  526. if(outvar == "REVERSE")
  527. {
  528. this->SetError("sub-command FIND does not allow to select REVERSE as "
  529. "the output variable. "
  530. "Maybe you missed the actual output variable?");
  531. return false;
  532. }
  533. // try to find the character and return its position
  534. size_t pos;
  535. if(!reverseMode)
  536. {
  537. pos = sstring.find(schar);
  538. }
  539. else
  540. {
  541. pos = sstring.rfind(schar);
  542. }
  543. if(std::string::npos != pos)
  544. {
  545. cmOStringStream s;
  546. s << pos;
  547. this->Makefile->AddDefinition(outvar, s.str().c_str());
  548. return true;
  549. }
  550. // the character was not found, but this is not really an error
  551. this->Makefile->AddDefinition(outvar, "-1");
  552. return true;
  553. }
  554. //----------------------------------------------------------------------------
  555. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
  556. args)
  557. {
  558. if(args.size() < 2)
  559. {
  560. this->SetError("sub-command COMPARE requires a mode to be specified.");
  561. return false;
  562. }
  563. std::string mode = args[1];
  564. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  565. (mode == "LESS") || (mode == "GREATER"))
  566. {
  567. if(args.size() < 5)
  568. {
  569. std::string e = "sub-command COMPARE, mode ";
  570. e += mode;
  571. e += " needs at least 5 arguments total to command.";
  572. this->SetError(e);
  573. return false;
  574. }
  575. const std::string& left = args[2];
  576. const std::string& right = args[3];
  577. const std::string& outvar = args[4];
  578. bool result;
  579. if(mode == "LESS")
  580. {
  581. result = (left < right);
  582. }
  583. else if(mode == "GREATER")
  584. {
  585. result = (left > right);
  586. }
  587. else if(mode == "EQUAL")
  588. {
  589. result = (left == right);
  590. }
  591. else // if(mode == "NOTEQUAL")
  592. {
  593. result = !(left == right);
  594. }
  595. if(result)
  596. {
  597. this->Makefile->AddDefinition(outvar, "1");
  598. }
  599. else
  600. {
  601. this->Makefile->AddDefinition(outvar, "0");
  602. }
  603. return true;
  604. }
  605. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  606. this->SetError(e);
  607. return false;
  608. }
  609. //----------------------------------------------------------------------------
  610. bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const&
  611. args)
  612. {
  613. if(args.size() < 5)
  614. {
  615. this->SetError("sub-command REPLACE requires at least four arguments.");
  616. return false;
  617. }
  618. const std::string& matchExpression = args[1];
  619. const std::string& replaceExpression = args[2];
  620. const std::string& variableName = args[3];
  621. std::string input = args[4];
  622. for(unsigned int i=5; i < args.size(); ++i)
  623. {
  624. input += args[i];
  625. }
  626. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  627. replaceExpression.c_str());
  628. this->Makefile->AddDefinition(variableName, input.c_str());
  629. return true;
  630. }
  631. //----------------------------------------------------------------------------
  632. bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const&
  633. args)
  634. {
  635. if(args.size() != 5)
  636. {
  637. this->SetError("sub-command SUBSTRING requires four arguments.");
  638. return false;
  639. }
  640. const std::string& stringValue = args[1];
  641. int begin = atoi(args[2].c_str());
  642. int end = atoi(args[3].c_str());
  643. const std::string& variableName = args[4];
  644. size_t stringLength = stringValue.size();
  645. int intStringLength = static_cast<int>(stringLength);
  646. if ( begin < 0 || begin > intStringLength )
  647. {
  648. cmOStringStream ostr;
  649. ostr << "begin index: " << begin << " is out of range 0 - "
  650. << stringLength;
  651. this->SetError(ostr.str());
  652. return false;
  653. }
  654. int leftOverLength = intStringLength - begin;
  655. if ( end < -1 || end > leftOverLength )
  656. {
  657. cmOStringStream ostr;
  658. ostr << "end index: " << end << " is out of range -1 - "
  659. << leftOverLength;
  660. this->SetError(ostr.str());
  661. return false;
  662. }
  663. this->Makefile->AddDefinition(variableName,
  664. stringValue.substr(begin, end).c_str());
  665. return true;
  666. }
  667. //----------------------------------------------------------------------------
  668. bool cmStringCommand
  669. ::HandleLengthCommand(std::vector<std::string> const& args)
  670. {
  671. if(args.size() != 3)
  672. {
  673. this->SetError("sub-command LENGTH requires two arguments.");
  674. return false;
  675. }
  676. const std::string& stringValue = args[1];
  677. const std::string& variableName = args[2];
  678. size_t length = stringValue.size();
  679. char buffer[1024];
  680. sprintf(buffer, "%d", static_cast<int>(length));
  681. this->Makefile->AddDefinition(variableName, buffer);
  682. return true;
  683. }
  684. //----------------------------------------------------------------------------
  685. bool cmStringCommand
  686. ::HandleConcatCommand(std::vector<std::string> const& args)
  687. {
  688. if(args.size() < 2)
  689. {
  690. this->SetError("sub-command CONCAT requires at least one argument.");
  691. return false;
  692. }
  693. std::string const& variableName = args[1];
  694. std::string value;
  695. for(unsigned int i = 2; i < args.size(); ++i)
  696. {
  697. value += args[i];
  698. }
  699. this->Makefile->AddDefinition(variableName, value.c_str());
  700. return true;
  701. }
  702. //----------------------------------------------------------------------------
  703. bool cmStringCommand
  704. ::HandleMakeCIdentifierCommand(std::vector<std::string> const& args)
  705. {
  706. if(args.size() != 3)
  707. {
  708. this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  709. return false;
  710. }
  711. const std::string& input = args[1];
  712. const std::string& variableName = args[2];
  713. this->Makefile->AddDefinition(variableName,
  714. cmSystemTools::MakeCidentifier(input.c_str()).c_str());
  715. return true;
  716. }
  717. //----------------------------------------------------------------------------
  718. bool cmStringCommand
  719. ::HandleGenexStripCommand(std::vector<std::string> const& args)
  720. {
  721. if(args.size() != 3)
  722. {
  723. this->SetError("sub-command GENEX_STRIP requires two arguments.");
  724. return false;
  725. }
  726. const std::string& input = args[1];
  727. std::string result = cmGeneratorExpression::Preprocess(input,
  728. cmGeneratorExpression::StripAllGeneratorExpressions);
  729. const std::string& variableName = args[2];
  730. this->Makefile->AddDefinition(variableName, result.c_str());
  731. return true;
  732. }
  733. //----------------------------------------------------------------------------
  734. bool cmStringCommand::HandleStripCommand(
  735. std::vector<std::string> const& args)
  736. {
  737. if(args.size() != 3)
  738. {
  739. this->SetError("sub-command STRIP requires two arguments.");
  740. return false;
  741. }
  742. const std::string& stringValue = args[1];
  743. const std::string& variableName = args[2];
  744. size_t inStringLength = stringValue.size();
  745. size_t startPos = inStringLength + 1;
  746. size_t endPos = 0;
  747. const char* ptr = stringValue.c_str();
  748. size_t cc;
  749. for ( cc = 0; cc < inStringLength; ++ cc )
  750. {
  751. if ( !isspace(*ptr) )
  752. {
  753. if ( startPos > inStringLength )
  754. {
  755. startPos = cc;
  756. }
  757. endPos = cc;
  758. }
  759. ++ ptr;
  760. }
  761. size_t outLength = 0;
  762. // if the input string didn't contain any non-space characters, return
  763. // an empty string
  764. if (startPos > inStringLength)
  765. {
  766. outLength = 0;
  767. startPos = 0;
  768. }
  769. else
  770. {
  771. outLength=endPos - startPos + 1;
  772. }
  773. this->Makefile->AddDefinition(variableName,
  774. stringValue.substr(startPos, outLength).c_str());
  775. return true;
  776. }
  777. //----------------------------------------------------------------------------
  778. bool cmStringCommand
  779. ::HandleRandomCommand(std::vector<std::string> const& args)
  780. {
  781. if(args.size() < 2 || args.size() == 3 || args.size() == 5)
  782. {
  783. this->SetError("sub-command RANDOM requires at least one argument.");
  784. return false;
  785. }
  786. static bool seeded = false;
  787. bool force_seed = false;
  788. unsigned int seed = 0;
  789. int length = 5;
  790. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  791. "QWERTYUIOPASDFGHJKLZXCVBNM"
  792. "0123456789";
  793. std::string alphabet;
  794. if ( args.size() > 3 )
  795. {
  796. size_t i = 1;
  797. size_t stopAt = args.size() - 2;
  798. for ( ; i < stopAt; ++i )
  799. {
  800. if ( args[i] == "LENGTH" )
  801. {
  802. ++i;
  803. length = atoi(args[i].c_str());
  804. }
  805. else if ( args[i] == "ALPHABET" )
  806. {
  807. ++i;
  808. alphabet = args[i];
  809. }
  810. else if ( args[i] == "RANDOM_SEED" )
  811. {
  812. ++i;
  813. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  814. force_seed = true;
  815. }
  816. }
  817. }
  818. if ( !alphabet.size() )
  819. {
  820. alphabet = cmStringCommandDefaultAlphabet;
  821. }
  822. double sizeofAlphabet = static_cast<double>(alphabet.size());
  823. if ( sizeofAlphabet < 1 )
  824. {
  825. this->SetError("sub-command RANDOM invoked with bad alphabet.");
  826. return false;
  827. }
  828. if ( length < 1 )
  829. {
  830. this->SetError("sub-command RANDOM invoked with bad length.");
  831. return false;
  832. }
  833. const std::string& variableName = args[args.size()-1];
  834. std::vector<char> result;
  835. if (!seeded || force_seed)
  836. {
  837. seeded = true;
  838. srand(force_seed? seed : cmSystemTools::RandomSeed());
  839. }
  840. const char* alphaPtr = alphabet.c_str();
  841. int cc;
  842. for ( cc = 0; cc < length; cc ++ )
  843. {
  844. int idx=(int) (sizeofAlphabet* rand()/(RAND_MAX+1.0));
  845. result.push_back(*(alphaPtr + idx));
  846. }
  847. result.push_back(0);
  848. this->Makefile->AddDefinition(variableName, &*result.begin());
  849. return true;
  850. }
  851. //----------------------------------------------------------------------------
  852. bool cmStringCommand
  853. ::HandleTimestampCommand(std::vector<std::string> const& args)
  854. {
  855. if(args.size() < 2)
  856. {
  857. this->SetError("sub-command TIMESTAMP requires at least one argument.");
  858. return false;
  859. }
  860. else if(args.size() > 4)
  861. {
  862. this->SetError("sub-command TIMESTAMP takes at most three arguments.");
  863. return false;
  864. }
  865. unsigned int argsIndex = 1;
  866. const std::string &outputVariable = args[argsIndex++];
  867. std::string formatString;
  868. if(args.size() > argsIndex && args[argsIndex] != "UTC")
  869. {
  870. formatString = args[argsIndex++];
  871. }
  872. bool utcFlag = false;
  873. if(args.size() > argsIndex)
  874. {
  875. if(args[argsIndex] == "UTC")
  876. {
  877. utcFlag = true;
  878. }
  879. else
  880. {
  881. std::string e = " TIMESTAMP sub-command does not recognize option " +
  882. args[argsIndex] + ".";
  883. this->SetError(e);
  884. return false;
  885. }
  886. }
  887. cmTimestamp timestamp;
  888. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  889. this->Makefile->AddDefinition(outputVariable, result.c_str());
  890. return true;
  891. }
  892. bool cmStringCommand
  893. ::HandleUuidCommand(std::vector<std::string> const& args)
  894. {
  895. #if defined(CMAKE_BUILD_WITH_CMAKE)
  896. unsigned int argsIndex = 1;
  897. if(args.size() < 2)
  898. {
  899. this->SetError("UUID sub-command requires an output variable.");
  900. return false;
  901. }
  902. const std::string &outputVariable = args[argsIndex++];
  903. std::string uuidNamespaceString;
  904. std::string uuidName;
  905. std::string uuidType;
  906. bool uuidUpperCase = false;
  907. while(args.size() > argsIndex)
  908. {
  909. if(args[argsIndex] == "NAMESPACE")
  910. {
  911. ++argsIndex;
  912. if(argsIndex >= args.size())
  913. {
  914. this->SetError("UUID sub-command, NAMESPACE requires a value.");
  915. return false;
  916. }
  917. uuidNamespaceString = args[argsIndex++];
  918. }
  919. else if(args[argsIndex] == "NAME")
  920. {
  921. ++argsIndex;
  922. if(argsIndex >= args.size())
  923. {
  924. this->SetError("UUID sub-command, NAME requires a value.");
  925. return false;
  926. }
  927. uuidName = args[argsIndex++];
  928. }
  929. else if(args[argsIndex] == "TYPE")
  930. {
  931. ++argsIndex;
  932. if(argsIndex >= args.size())
  933. {
  934. this->SetError("UUID sub-command, TYPE requires a value.");
  935. return false;
  936. }
  937. uuidType = args[argsIndex++];
  938. }
  939. else if(args[argsIndex] == "UPPER")
  940. {
  941. ++argsIndex;
  942. uuidUpperCase = true;
  943. }
  944. else
  945. {
  946. std::string e = "UUID sub-command does not recognize option " +
  947. args[argsIndex] + ".";
  948. this->SetError(e);
  949. return false;
  950. }
  951. }
  952. std::string uuid;
  953. cmUuid uuidGenerator;
  954. std::vector<unsigned char> uuidNamespace;
  955. if(!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace))
  956. {
  957. this->SetError("UUID sub-command, malformed NAMESPACE UUID.");
  958. return false;
  959. }
  960. if(uuidType == "MD5")
  961. {
  962. uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
  963. }
  964. else if(uuidType == "SHA1")
  965. {
  966. uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
  967. }
  968. else
  969. {
  970. std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
  971. this->SetError(e);
  972. return false;
  973. }
  974. if(uuid.empty())
  975. {
  976. this->SetError("UUID sub-command, generation failed.");
  977. return false;
  978. }
  979. if(uuidUpperCase)
  980. {
  981. uuid = cmSystemTools::UpperCase(uuid);
  982. }
  983. this->Makefile->AddDefinition(outputVariable, uuid.c_str());
  984. return true;
  985. #else
  986. cmOStringStream e;
  987. e << args[0] << " not available during bootstrap";
  988. this->SetError(e.str().c_str());
  989. return false;
  990. #endif
  991. }