cmStringCommand.cxx 28 KB

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