cmStringCommand.cxx 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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. if ( end < -1 )
  685. {
  686. cmOStringStream ostr;
  687. ostr << "end index: " << end << " should be -1 or greater";
  688. this->SetError(ostr.str());
  689. return false;
  690. }
  691. this->Makefile->AddDefinition(variableName,
  692. stringValue.substr(begin, end).c_str());
  693. return true;
  694. }
  695. //----------------------------------------------------------------------------
  696. bool cmStringCommand
  697. ::HandleLengthCommand(std::vector<std::string> const& args)
  698. {
  699. if(args.size() != 3)
  700. {
  701. this->SetError("sub-command LENGTH requires two arguments.");
  702. return false;
  703. }
  704. const std::string& stringValue = args[1];
  705. const std::string& variableName = args[2];
  706. size_t length = stringValue.size();
  707. char buffer[1024];
  708. sprintf(buffer, "%d", static_cast<int>(length));
  709. this->Makefile->AddDefinition(variableName, buffer);
  710. return true;
  711. }
  712. //----------------------------------------------------------------------------
  713. bool cmStringCommand
  714. ::HandleConcatCommand(std::vector<std::string> const& args)
  715. {
  716. if(args.size() < 2)
  717. {
  718. this->SetError("sub-command CONCAT requires at least one argument.");
  719. return false;
  720. }
  721. std::string const& variableName = args[1];
  722. std::string value;
  723. for(unsigned int i = 2; i < args.size(); ++i)
  724. {
  725. value += args[i];
  726. }
  727. this->Makefile->AddDefinition(variableName, value.c_str());
  728. return true;
  729. }
  730. //----------------------------------------------------------------------------
  731. bool cmStringCommand
  732. ::HandleMakeCIdentifierCommand(std::vector<std::string> const& args)
  733. {
  734. if(args.size() != 3)
  735. {
  736. this->SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  737. return false;
  738. }
  739. const std::string& input = args[1];
  740. const std::string& variableName = args[2];
  741. this->Makefile->AddDefinition(variableName,
  742. cmSystemTools::MakeCidentifier(input).c_str());
  743. return true;
  744. }
  745. //----------------------------------------------------------------------------
  746. bool cmStringCommand
  747. ::HandleGenexStripCommand(std::vector<std::string> const& args)
  748. {
  749. if(args.size() != 3)
  750. {
  751. this->SetError("sub-command GENEX_STRIP requires two arguments.");
  752. return false;
  753. }
  754. const std::string& input = args[1];
  755. std::string result = cmGeneratorExpression::Preprocess(input,
  756. cmGeneratorExpression::StripAllGeneratorExpressions);
  757. const std::string& variableName = args[2];
  758. this->Makefile->AddDefinition(variableName, result.c_str());
  759. return true;
  760. }
  761. //----------------------------------------------------------------------------
  762. bool cmStringCommand::HandleStripCommand(
  763. std::vector<std::string> const& args)
  764. {
  765. if(args.size() != 3)
  766. {
  767. this->SetError("sub-command STRIP requires two arguments.");
  768. return false;
  769. }
  770. const std::string& stringValue = args[1];
  771. const std::string& variableName = args[2];
  772. size_t inStringLength = stringValue.size();
  773. size_t startPos = inStringLength + 1;
  774. size_t endPos = 0;
  775. const char* ptr = stringValue.c_str();
  776. size_t cc;
  777. for ( cc = 0; cc < inStringLength; ++ cc )
  778. {
  779. if ( !isspace(*ptr) )
  780. {
  781. if ( startPos > inStringLength )
  782. {
  783. startPos = cc;
  784. }
  785. endPos = cc;
  786. }
  787. ++ ptr;
  788. }
  789. size_t outLength = 0;
  790. // if the input string didn't contain any non-space characters, return
  791. // an empty string
  792. if (startPos > inStringLength)
  793. {
  794. outLength = 0;
  795. startPos = 0;
  796. }
  797. else
  798. {
  799. outLength=endPos - startPos + 1;
  800. }
  801. this->Makefile->AddDefinition(variableName,
  802. stringValue.substr(startPos, outLength).c_str());
  803. return true;
  804. }
  805. //----------------------------------------------------------------------------
  806. bool cmStringCommand
  807. ::HandleRandomCommand(std::vector<std::string> const& args)
  808. {
  809. if(args.size() < 2 || args.size() == 3 || args.size() == 5)
  810. {
  811. this->SetError("sub-command RANDOM requires at least one argument.");
  812. return false;
  813. }
  814. static bool seeded = false;
  815. bool force_seed = false;
  816. unsigned int seed = 0;
  817. int length = 5;
  818. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  819. "QWERTYUIOPASDFGHJKLZXCVBNM"
  820. "0123456789";
  821. std::string alphabet;
  822. if ( args.size() > 3 )
  823. {
  824. size_t i = 1;
  825. size_t stopAt = args.size() - 2;
  826. for ( ; i < stopAt; ++i )
  827. {
  828. if ( args[i] == "LENGTH" )
  829. {
  830. ++i;
  831. length = atoi(args[i].c_str());
  832. }
  833. else if ( args[i] == "ALPHABET" )
  834. {
  835. ++i;
  836. alphabet = args[i];
  837. }
  838. else if ( args[i] == "RANDOM_SEED" )
  839. {
  840. ++i;
  841. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  842. force_seed = true;
  843. }
  844. }
  845. }
  846. if ( !alphabet.size() )
  847. {
  848. alphabet = cmStringCommandDefaultAlphabet;
  849. }
  850. double sizeofAlphabet = static_cast<double>(alphabet.size());
  851. if ( sizeofAlphabet < 1 )
  852. {
  853. this->SetError("sub-command RANDOM invoked with bad alphabet.");
  854. return false;
  855. }
  856. if ( length < 1 )
  857. {
  858. this->SetError("sub-command RANDOM invoked with bad length.");
  859. return false;
  860. }
  861. const std::string& variableName = args[args.size()-1];
  862. std::vector<char> result;
  863. if (!seeded || force_seed)
  864. {
  865. seeded = true;
  866. srand(force_seed? seed : cmSystemTools::RandomSeed());
  867. }
  868. const char* alphaPtr = alphabet.c_str();
  869. int cc;
  870. for ( cc = 0; cc < length; cc ++ )
  871. {
  872. int idx=(int) (sizeofAlphabet* rand()/(RAND_MAX+1.0));
  873. result.push_back(*(alphaPtr + idx));
  874. }
  875. result.push_back(0);
  876. this->Makefile->AddDefinition(variableName, &*result.begin());
  877. return true;
  878. }
  879. //----------------------------------------------------------------------------
  880. bool cmStringCommand
  881. ::HandleTimestampCommand(std::vector<std::string> const& args)
  882. {
  883. if(args.size() < 2)
  884. {
  885. this->SetError("sub-command TIMESTAMP requires at least one argument.");
  886. return false;
  887. }
  888. else if(args.size() > 4)
  889. {
  890. this->SetError("sub-command TIMESTAMP takes at most three arguments.");
  891. return false;
  892. }
  893. unsigned int argsIndex = 1;
  894. const std::string &outputVariable = args[argsIndex++];
  895. std::string formatString;
  896. if(args.size() > argsIndex && args[argsIndex] != "UTC")
  897. {
  898. formatString = args[argsIndex++];
  899. }
  900. bool utcFlag = false;
  901. if(args.size() > argsIndex)
  902. {
  903. if(args[argsIndex] == "UTC")
  904. {
  905. utcFlag = true;
  906. }
  907. else
  908. {
  909. std::string e = " TIMESTAMP sub-command does not recognize option " +
  910. args[argsIndex] + ".";
  911. this->SetError(e);
  912. return false;
  913. }
  914. }
  915. cmTimestamp timestamp;
  916. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  917. this->Makefile->AddDefinition(outputVariable, result.c_str());
  918. return true;
  919. }
  920. bool cmStringCommand
  921. ::HandleUuidCommand(std::vector<std::string> const& args)
  922. {
  923. #if defined(CMAKE_BUILD_WITH_CMAKE)
  924. unsigned int argsIndex = 1;
  925. if(args.size() < 2)
  926. {
  927. this->SetError("UUID sub-command requires an output variable.");
  928. return false;
  929. }
  930. const std::string &outputVariable = args[argsIndex++];
  931. std::string uuidNamespaceString;
  932. std::string uuidName;
  933. std::string uuidType;
  934. bool uuidUpperCase = false;
  935. while(args.size() > argsIndex)
  936. {
  937. if(args[argsIndex] == "NAMESPACE")
  938. {
  939. ++argsIndex;
  940. if(argsIndex >= args.size())
  941. {
  942. this->SetError("UUID sub-command, NAMESPACE requires a value.");
  943. return false;
  944. }
  945. uuidNamespaceString = args[argsIndex++];
  946. }
  947. else if(args[argsIndex] == "NAME")
  948. {
  949. ++argsIndex;
  950. if(argsIndex >= args.size())
  951. {
  952. this->SetError("UUID sub-command, NAME requires a value.");
  953. return false;
  954. }
  955. uuidName = args[argsIndex++];
  956. }
  957. else if(args[argsIndex] == "TYPE")
  958. {
  959. ++argsIndex;
  960. if(argsIndex >= args.size())
  961. {
  962. this->SetError("UUID sub-command, TYPE requires a value.");
  963. return false;
  964. }
  965. uuidType = args[argsIndex++];
  966. }
  967. else if(args[argsIndex] == "UPPER")
  968. {
  969. ++argsIndex;
  970. uuidUpperCase = true;
  971. }
  972. else
  973. {
  974. std::string e = "UUID sub-command does not recognize option " +
  975. args[argsIndex] + ".";
  976. this->SetError(e);
  977. return false;
  978. }
  979. }
  980. std::string uuid;
  981. cmUuid uuidGenerator;
  982. std::vector<unsigned char> uuidNamespace;
  983. if(!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace))
  984. {
  985. this->SetError("UUID sub-command, malformed NAMESPACE UUID.");
  986. return false;
  987. }
  988. if(uuidType == "MD5")
  989. {
  990. uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
  991. }
  992. else if(uuidType == "SHA1")
  993. {
  994. uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
  995. }
  996. else
  997. {
  998. std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
  999. this->SetError(e);
  1000. return false;
  1001. }
  1002. if(uuid.empty())
  1003. {
  1004. this->SetError("UUID sub-command, generation failed.");
  1005. return false;
  1006. }
  1007. if(uuidUpperCase)
  1008. {
  1009. uuid = cmSystemTools::UpperCase(uuid);
  1010. }
  1011. this->Makefile->AddDefinition(outputVariable, uuid.c_str());
  1012. return true;
  1013. #else
  1014. cmOStringStream e;
  1015. e << args[0] << " not available during bootstrap";
  1016. this->SetError(e.str().c_str());
  1017. return false;
  1018. #endif
  1019. }