cmStringCommand.cxx 28 KB

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