cmStringCommand.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmStringCommand.h"
  14. #include <cmsys/RegularExpression.hxx>
  15. #include <cmsys/SystemTools.hxx>
  16. #include <stdlib.h> // required for atoi
  17. #include <ctype.h>
  18. #include <time.h>
  19. //----------------------------------------------------------------------------
  20. bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
  21. {
  22. if(args.size() < 1)
  23. {
  24. this->SetError("must be called with at least one argument.");
  25. return false;
  26. }
  27. const std::string &subCommand = args[0];
  28. if(subCommand == "REGEX")
  29. {
  30. return this->HandleRegexCommand(args);
  31. }
  32. else if(subCommand == "REPLACE")
  33. {
  34. return this->HandleReplaceCommand(args);
  35. }
  36. else if(subCommand == "TOLOWER")
  37. {
  38. return this->HandleToUpperLowerCommand(args, false);
  39. }
  40. else if(subCommand == "TOUPPER")
  41. {
  42. return this->HandleToUpperLowerCommand(args, true);
  43. }
  44. else if(subCommand == "COMPARE")
  45. {
  46. return this->HandleCompareCommand(args);
  47. }
  48. else if(subCommand == "ASCII")
  49. {
  50. return this->HandleAsciiCommand(args);
  51. }
  52. else if(subCommand == "CONFIGURE")
  53. {
  54. return this->HandleConfigureCommand(args);
  55. }
  56. else if(subCommand == "LENGTH")
  57. {
  58. return this->HandleLengthCommand(args);
  59. }
  60. else if(subCommand == "SUBSTRING")
  61. {
  62. return this->HandleSubstringCommand(args);
  63. }
  64. else if(subCommand == "STRIP")
  65. {
  66. return this->HandleStripCommand(args);
  67. }
  68. else if(subCommand == "RANDOM")
  69. {
  70. return this->HandleRandomCommand(args);
  71. }
  72. std::string e = "does not recognize sub-command "+subCommand;
  73. this->SetError(e.c_str());
  74. return false;
  75. }
  76. //----------------------------------------------------------------------------
  77. bool cmStringCommand::HandleToUpperLowerCommand(
  78. std::vector<std::string> const& args, bool toUpper)
  79. {
  80. if ( args.size() < 3 )
  81. {
  82. this->SetError("no output variable specified");
  83. return false;
  84. }
  85. std::string outvar = args[2];
  86. std::string output;
  87. if (toUpper)
  88. {
  89. output = cmSystemTools::UpperCase(args[1]);
  90. }
  91. else
  92. {
  93. output = cmSystemTools::LowerCase(args[1]);
  94. }
  95. // Store the output in the provided variable.
  96. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  97. return true;
  98. }
  99. //----------------------------------------------------------------------------
  100. bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
  101. {
  102. if ( args.size() < 3 )
  103. {
  104. this->SetError("No output variable specified");
  105. return false;
  106. }
  107. std::string::size_type cc;
  108. std::string outvar = args[args.size()-1];
  109. std::string output = "";
  110. for ( cc = 1; cc < args.size()-1; cc ++ )
  111. {
  112. int ch = atoi(args[cc].c_str());
  113. if ( ch > 0 && ch < 256 )
  114. {
  115. output += static_cast<char>(ch);
  116. }
  117. else
  118. {
  119. std::string error = "Character with code ";
  120. error += ch;
  121. error += " does not exist.";
  122. this->SetError(error.c_str());
  123. return false;
  124. }
  125. }
  126. // Store the output in the provided variable.
  127. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  128. return true;
  129. }
  130. //----------------------------------------------------------------------------
  131. bool cmStringCommand::HandleConfigureCommand(
  132. std::vector<std::string> const& args)
  133. {
  134. if ( args.size() < 2 )
  135. {
  136. this->SetError("No input string specified.");
  137. return false;
  138. }
  139. else if ( args.size() < 3 )
  140. {
  141. this->SetError("No output variable specified.");
  142. return false;
  143. }
  144. // Parse options.
  145. bool escapeQuotes = false;
  146. bool atOnly = false;
  147. for(unsigned int i = 3; i < args.size(); ++i)
  148. {
  149. if(args[i] == "@ONLY")
  150. {
  151. atOnly = true;
  152. }
  153. else if(args[i] == "ESCAPE_QUOTES")
  154. {
  155. escapeQuotes = true;
  156. }
  157. else
  158. {
  159. cmOStringStream err;
  160. err << "Unrecognized argument \"" << args[i] << "\"";
  161. this->SetError(err.str().c_str());
  162. return false;
  163. }
  164. }
  165. // Configure the string.
  166. std::string output;
  167. this->Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
  168. // Store the output in the provided variable.
  169. this->Makefile->AddDefinition(args[2].c_str(), output.c_str());
  170. return true;
  171. }
  172. //----------------------------------------------------------------------------
  173. bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
  174. {
  175. if(args.size() < 2)
  176. {
  177. this->SetError("sub-command REGEX requires a mode to be specified.");
  178. return false;
  179. }
  180. std::string mode = args[1];
  181. if(mode == "MATCH")
  182. {
  183. if(args.size() < 5)
  184. {
  185. this->SetError("sub-command REGEX, mode MATCH needs "
  186. "at least 5 arguments total to command.");
  187. return false;
  188. }
  189. return this->RegexMatch(args);
  190. }
  191. else if(mode == "MATCHALL")
  192. {
  193. if(args.size() < 5)
  194. {
  195. this->SetError("sub-command REGEX, mode MATCHALL needs "
  196. "at least 5 arguments total to command.");
  197. return false;
  198. }
  199. return this->RegexMatchAll(args);
  200. }
  201. else if(mode == "REPLACE")
  202. {
  203. if(args.size() < 6)
  204. {
  205. this->SetError("sub-command REGEX, mode MATCH needs "
  206. "at least 6 arguments total to command.");
  207. return false;
  208. }
  209. return this->RegexReplace(args);
  210. }
  211. std::string e = "sub-command REGEX does not recognize mode "+mode;
  212. this->SetError(e.c_str());
  213. return false;
  214. }
  215. //----------------------------------------------------------------------------
  216. bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
  217. {
  218. //"STRING(REGEX MATCH <regular_expression> <output variable>
  219. // <input> [<input>...])\n";
  220. std::string regex = args[2];
  221. std::string outvar = args[3];
  222. // Concatenate all the last arguments together.
  223. std::string input = args[4];
  224. for(unsigned int i=5; i < args.size(); ++i)
  225. {
  226. input += args[i];
  227. }
  228. // Compile the regular expression.
  229. cmsys::RegularExpression re;
  230. if(!re.compile(regex.c_str()))
  231. {
  232. std::string e =
  233. "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
  234. this->SetError(e.c_str());
  235. return false;
  236. }
  237. // Scan through the input for all matches.
  238. std::string output;
  239. if(re.find(input.c_str()))
  240. {
  241. std::string::size_type l = re.start();
  242. std::string::size_type r = re.end();
  243. if(r-l == 0)
  244. {
  245. std::string e =
  246. "sub-command REGEX, mode MATCH regex \""+regex+
  247. "\" matched an empty string.";
  248. this->SetError(e.c_str());
  249. return false;
  250. }
  251. output = input.substr(l, r-l);
  252. }
  253. // Store the output in the provided variable.
  254. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  255. return true;
  256. }
  257. //----------------------------------------------------------------------------
  258. bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
  259. {
  260. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
  261. // [<input>...])\n";
  262. std::string regex = args[2];
  263. std::string outvar = args[3];
  264. // Concatenate all the last arguments together.
  265. std::string input = args[4];
  266. for(unsigned int i=5; i < args.size(); ++i)
  267. {
  268. input += args[i];
  269. }
  270. // Compile the regular expression.
  271. cmsys::RegularExpression re;
  272. if(!re.compile(regex.c_str()))
  273. {
  274. std::string e =
  275. "sub-command REGEX, mode MATCHALL failed to compile regex \""+
  276. regex+"\".";
  277. this->SetError(e.c_str());
  278. return false;
  279. }
  280. // Scan through the input for all matches.
  281. std::string output;
  282. const char* p = input.c_str();
  283. while(re.find(p))
  284. {
  285. std::string::size_type l = re.start();
  286. std::string::size_type r = re.end();
  287. if(r-l == 0)
  288. {
  289. std::string e = "sub-command REGEX, mode MATCHALL regex \""+
  290. regex+"\" matched an empty string.";
  291. this->SetError(e.c_str());
  292. return false;
  293. }
  294. if(output.length() > 0)
  295. {
  296. output += ";";
  297. }
  298. output += std::string(p+l, r-l);
  299. p += r;
  300. }
  301. // Store the output in the provided variable.
  302. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  303. return true;
  304. }
  305. //----------------------------------------------------------------------------
  306. bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
  307. {
  308. //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
  309. // <output variable> <input> [<input>...])\n"
  310. std::string regex = args[2];
  311. std::string replace = args[3];
  312. std::string outvar = args[4];
  313. // Pull apart the replace expression to find the escaped [0-9] values.
  314. std::vector<RegexReplacement> replacement;
  315. std::string::size_type l = 0;
  316. while(l < replace.length())
  317. {
  318. std::string::size_type r = replace.find("\\", l);
  319. if(r == std::string::npos)
  320. {
  321. r = replace.length();
  322. replacement.push_back(replace.substr(l, r-l));
  323. }
  324. else
  325. {
  326. if(r-l > 0)
  327. {
  328. replacement.push_back(replace.substr(l, r-l));
  329. }
  330. if(r == (replace.length()-1))
  331. {
  332. this->SetError("sub-command REGEX, mode REPLACE: "
  333. "replace-expression ends in a backslash.");
  334. return false;
  335. }
  336. if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
  337. {
  338. replacement.push_back(replace[r+1]-'0');
  339. }
  340. else if(replace[r+1] == 'n')
  341. {
  342. replacement.push_back("\n");
  343. }
  344. else if(replace[r+1] == '\\')
  345. {
  346. replacement.push_back("\\");
  347. }
  348. else
  349. {
  350. std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
  351. e += replace.substr(r, 2);
  352. e += "\"in replace-expression.";
  353. this->SetError(e.c_str());
  354. return false;
  355. }
  356. r += 2;
  357. }
  358. l = r;
  359. }
  360. // Concatenate all the last arguments together.
  361. std::string input = args[5];
  362. for(unsigned int i=6; i < args.size(); ++i)
  363. {
  364. input += args[i];
  365. }
  366. // Compile the regular expression.
  367. cmsys::RegularExpression re;
  368. if(!re.compile(regex.c_str()))
  369. {
  370. std::string e =
  371. "sub-command REGEX, mode REPLACE failed to compile regex \""+
  372. regex+"\".";
  373. this->SetError(e.c_str());
  374. return false;
  375. }
  376. // Scan through the input for all matches.
  377. std::string output;
  378. std::string::size_type base = 0;
  379. while(re.find(input.c_str()+base))
  380. {
  381. std::string::size_type l2 = re.start();
  382. std::string::size_type r = re.end();
  383. // Concatenate the part of the input that was not matched.
  384. output += input.substr(base, l2);
  385. // Make sure the match had some text.
  386. if(r-l2 == 0)
  387. {
  388. std::string e = "sub-command REGEX, mode REPLACE regex \""+
  389. regex+"\" matched an empty string.";
  390. this->SetError(e.c_str());
  391. return false;
  392. }
  393. // Concatenate the replacement for the match.
  394. for(unsigned int i=0; i < replacement.size(); ++i)
  395. {
  396. if(replacement[i].number < 0)
  397. {
  398. // This is just a plain-text part of the replacement.
  399. output += replacement[i].value;
  400. }
  401. else
  402. {
  403. // Replace with part of the match.
  404. int n = replacement[i].number;
  405. std::string::size_type start = re.start(n);
  406. std::string::size_type end = re.end(n);
  407. std::string::size_type len = input.length()-base;
  408. if((start != std::string::npos) && (end != std::string::npos) &&
  409. (start <= len) && (end <= len))
  410. {
  411. output += input.substr(base+start, end-start);
  412. }
  413. else
  414. {
  415. std::string e =
  416. "sub-command REGEX, mode REPLACE: replace expression \""+
  417. replace+"\" contains an out-of-range escape for regex \""+
  418. regex+"\".";
  419. this->SetError(e.c_str());
  420. return false;
  421. }
  422. }
  423. }
  424. // Move past the match.
  425. base += r;
  426. }
  427. // Concatenate the text after the last match.
  428. output += input.substr(base, input.length()-base);
  429. // Store the output in the provided variable.
  430. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  431. return true;
  432. }
  433. //----------------------------------------------------------------------------
  434. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
  435. args)
  436. {
  437. if(args.size() < 2)
  438. {
  439. this->SetError("sub-command COMPARE requires a mode to be specified.");
  440. return false;
  441. }
  442. std::string mode = args[1];
  443. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  444. (mode == "LESS") || (mode == "GREATER"))
  445. {
  446. if(args.size() < 5)
  447. {
  448. std::string e = "sub-command COMPARE, mode ";
  449. e += mode;
  450. e += " needs at least 5 arguments total to command.";
  451. this->SetError(e.c_str());
  452. return false;
  453. }
  454. const std::string& left = args[2];
  455. const std::string& right = args[3];
  456. const std::string& outvar = args[4];
  457. bool result;
  458. if(mode == "LESS")
  459. {
  460. result = (left < right);
  461. }
  462. else if(mode == "GREATER")
  463. {
  464. result = (left > right);
  465. }
  466. else if(mode == "EQUAL")
  467. {
  468. result = (left == right);
  469. }
  470. else // if(mode == "NOTEQUAL")
  471. {
  472. result = !(left == right);
  473. }
  474. if(result)
  475. {
  476. this->Makefile->AddDefinition(outvar.c_str(), "1");
  477. }
  478. else
  479. {
  480. this->Makefile->AddDefinition(outvar.c_str(), "0");
  481. }
  482. return true;
  483. }
  484. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  485. this->SetError(e.c_str());
  486. return false;
  487. }
  488. //----------------------------------------------------------------------------
  489. bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const&
  490. args)
  491. {
  492. if(args.size() < 5)
  493. {
  494. this->SetError("sub-command REPLACE requires four arguments.");
  495. return false;
  496. }
  497. const std::string& matchExpression = args[1];
  498. const std::string& replaceExpression = args[2];
  499. const std::string& variableName = args[3];
  500. std::string input = args[4];
  501. for(unsigned int i=5; i < args.size(); ++i)
  502. {
  503. input += args[i];
  504. }
  505. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  506. replaceExpression.c_str());
  507. this->Makefile->AddDefinition(variableName.c_str(), input.c_str());
  508. return true;
  509. }
  510. //----------------------------------------------------------------------------
  511. bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const&
  512. args)
  513. {
  514. if(args.size() != 5)
  515. {
  516. this->SetError("sub-command REPLACE requires four arguments.");
  517. return false;
  518. }
  519. const std::string& stringValue = args[1];
  520. int begin = atoi(args[2].c_str());
  521. int end = atoi(args[3].c_str());
  522. const std::string& variableName = args[4];
  523. size_t stringLength = stringValue.size();
  524. int intStringLength = static_cast<int>(stringLength);
  525. if ( begin < 0 || begin > intStringLength )
  526. {
  527. cmOStringStream ostr;
  528. ostr << "begin index: " << begin << " is out of range 0 - "
  529. << stringLength;
  530. this->SetError(ostr.str().c_str());
  531. return false;
  532. }
  533. int leftOverLength = intStringLength - begin;
  534. if ( end < 0 || end > leftOverLength )
  535. {
  536. cmOStringStream ostr;
  537. ostr << "end index: " << end << " is out of range " << 0 << " - "
  538. << leftOverLength;
  539. this->SetError(ostr.str().c_str());
  540. return false;
  541. }
  542. this->Makefile->AddDefinition(variableName.c_str(),
  543. stringValue.substr(begin, end).c_str());
  544. return true;
  545. }
  546. //----------------------------------------------------------------------------
  547. bool cmStringCommand
  548. ::HandleLengthCommand(std::vector<std::string> const& args)
  549. {
  550. if(args.size() != 3)
  551. {
  552. this->SetError("sub-command LENGTH requires two arguments.");
  553. return false;
  554. }
  555. const std::string& stringValue = args[1];
  556. const std::string& variableName = args[2];
  557. size_t length = stringValue.size();
  558. char buffer[1024];
  559. sprintf(buffer, "%d", static_cast<int>(length));
  560. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  561. return true;
  562. }
  563. //----------------------------------------------------------------------------
  564. bool cmStringCommand::HandleStripCommand(
  565. std::vector<std::string> const& args)
  566. {
  567. if(args.size() != 3)
  568. {
  569. this->SetError("sub-command LENGTH requires two arguments.");
  570. return false;
  571. }
  572. const std::string& stringValue = args[1];
  573. const std::string& variableName = args[2];
  574. size_t inStringLength = stringValue.size();
  575. size_t startPos = inStringLength + 1;
  576. size_t endPos = 0;
  577. const char* ptr = stringValue.c_str();
  578. size_t cc;
  579. for ( cc = 0; cc < inStringLength; ++ cc )
  580. {
  581. if ( !isspace(*ptr) )
  582. {
  583. if ( startPos > inStringLength )
  584. {
  585. startPos = cc;
  586. }
  587. endPos = cc;
  588. }
  589. ++ ptr;
  590. }
  591. size_t outLength = endPos - startPos + 1;
  592. this->Makefile->AddDefinition(variableName.c_str(),
  593. stringValue.substr(startPos, outLength).c_str());
  594. return true;
  595. }
  596. //----------------------------------------------------------------------------
  597. bool cmStringCommand
  598. ::HandleRandomCommand(std::vector<std::string> const& args)
  599. {
  600. if(args.size() < 2 || args.size() == 3 || args.size() == 5)
  601. {
  602. this->SetError("sub-command RANDOM requires at least one argument.");
  603. return false;
  604. }
  605. int length = 5;
  606. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  607. "QWERTYUIOPASDFGHJKLZXCVBNM"
  608. "0123456789";
  609. std::string alphabet;
  610. if ( args.size() > 3 )
  611. {
  612. size_t i = 1;
  613. size_t stopAt = args.size() - 2;
  614. for ( ; i < stopAt; ++i )
  615. {
  616. if ( args[i] == "LENGTH" )
  617. {
  618. ++i;
  619. length = atoi(args[i].c_str());
  620. }
  621. else if ( args[i] == "ALPHABET" )
  622. {
  623. ++i;
  624. alphabet = args[i];
  625. }
  626. }
  627. }
  628. if ( !alphabet.size() )
  629. {
  630. alphabet = cmStringCommandDefaultAlphabet;
  631. }
  632. double sizeofAlphabet = alphabet.size();
  633. if ( sizeofAlphabet < 1 )
  634. {
  635. this->SetError("sub-command RANDOM invoked with bad alphabet.");
  636. return false;
  637. }
  638. if ( length < 1 )
  639. {
  640. this->SetError("sub-command RANDOM invoked with bad length.");
  641. return false;
  642. }
  643. const std::string& variableName = args[args.size()-1];
  644. std::vector<char> result;
  645. srand((int)time(NULL));
  646. const char* alphaPtr = alphabet.c_str();
  647. int cc;
  648. for ( cc = 0; cc < length; cc ++ )
  649. {
  650. int idx=(int) (sizeofAlphabet* rand()/(RAND_MAX+1.0));
  651. result.push_back(*(alphaPtr + idx));
  652. }
  653. result.push_back(0);
  654. this->Makefile->AddDefinition(variableName.c_str(), &*result.begin());
  655. return true;
  656. }