cmStringCommand.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. //----------------------------------------------------------------------------
  19. bool cmStringCommand::InitialPass(std::vector<std::string> const& args)
  20. {
  21. if(args.size() < 1)
  22. {
  23. this->SetError("must be called with at least one argument.");
  24. return false;
  25. }
  26. const std::string &subCommand = args[0];
  27. if(subCommand == "REGEX")
  28. {
  29. return this->HandleRegexCommand(args);
  30. }
  31. else if(subCommand == "REPLACE")
  32. {
  33. return this->HandleReplaceCommand(args);
  34. }
  35. else if(subCommand == "TOLOWER")
  36. {
  37. return this->HandleToUpperLowerCommand(args, false);
  38. }
  39. else if(subCommand == "TOUPPER")
  40. {
  41. return this->HandleToUpperLowerCommand(args, true);
  42. }
  43. else if(subCommand == "COMPARE")
  44. {
  45. return this->HandleCompareCommand(args);
  46. }
  47. else if(subCommand == "ASCII")
  48. {
  49. return this->HandleAsciiCommand(args);
  50. }
  51. else if(subCommand == "CONFIGURE")
  52. {
  53. return this->HandleConfigureCommand(args);
  54. }
  55. else if(subCommand == "LENGTH")
  56. {
  57. return this->HandleLengthCommand(args);
  58. }
  59. else if(subCommand == "SUBSTRING")
  60. {
  61. return this->HandleSubstringCommand(args);
  62. }
  63. std::string e = "does not recognize sub-command "+subCommand;
  64. this->SetError(e.c_str());
  65. return false;
  66. }
  67. //----------------------------------------------------------------------------
  68. bool cmStringCommand::HandleToUpperLowerCommand(
  69. std::vector<std::string> const& args, bool toUpper)
  70. {
  71. if ( args.size() < 3 )
  72. {
  73. this->SetError("no output variable specified");
  74. return false;
  75. }
  76. std::string outvar = args[2];
  77. std::string output;
  78. if (toUpper)
  79. {
  80. output = cmSystemTools::UpperCase(args[1]);
  81. }
  82. else
  83. {
  84. output = cmSystemTools::LowerCase(args[1]);
  85. }
  86. // Store the output in the provided variable.
  87. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  88. return true;
  89. }
  90. //----------------------------------------------------------------------------
  91. bool cmStringCommand::HandleAsciiCommand(std::vector<std::string> const& args)
  92. {
  93. if ( args.size() < 3 )
  94. {
  95. this->SetError("No output variable specified");
  96. return false;
  97. }
  98. std::string::size_type cc;
  99. std::string outvar = args[args.size()-1];
  100. std::string output = "";
  101. for ( cc = 1; cc < args.size()-1; cc ++ )
  102. {
  103. int ch = atoi(args[cc].c_str());
  104. if ( ch > 0 && ch < 256 )
  105. {
  106. output += static_cast<char>(ch);
  107. }
  108. else
  109. {
  110. std::string error = "Character with code ";
  111. error += ch;
  112. error += " does not exist.";
  113. this->SetError(error.c_str());
  114. return false;
  115. }
  116. }
  117. // Store the output in the provided variable.
  118. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  119. return true;
  120. }
  121. //----------------------------------------------------------------------------
  122. bool cmStringCommand::HandleConfigureCommand(
  123. std::vector<std::string> const& args)
  124. {
  125. if ( args.size() < 2 )
  126. {
  127. this->SetError("No input string specified.");
  128. return false;
  129. }
  130. else if ( args.size() < 3 )
  131. {
  132. this->SetError("No output variable specified.");
  133. return false;
  134. }
  135. // Parse options.
  136. bool escapeQuotes = false;
  137. bool atOnly = false;
  138. for(unsigned int i = 3; i < args.size(); ++i)
  139. {
  140. if(args[i] == "@ONLY")
  141. {
  142. atOnly = true;
  143. }
  144. else if(args[i] == "ESCAPE_QUOTES")
  145. {
  146. escapeQuotes = true;
  147. }
  148. else
  149. {
  150. cmOStringStream err;
  151. err << "Unrecognized argument \"" << args[i] << "\"";
  152. this->SetError(err.str().c_str());
  153. return false;
  154. }
  155. }
  156. // Configure the string.
  157. std::string output;
  158. this->Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
  159. // Store the output in the provided variable.
  160. this->Makefile->AddDefinition(args[2].c_str(), output.c_str());
  161. return true;
  162. }
  163. //----------------------------------------------------------------------------
  164. bool cmStringCommand::HandleRegexCommand(std::vector<std::string> const& args)
  165. {
  166. if(args.size() < 2)
  167. {
  168. this->SetError("sub-command REGEX requires a mode to be specified.");
  169. return false;
  170. }
  171. std::string mode = args[1];
  172. if(mode == "MATCH")
  173. {
  174. if(args.size() < 5)
  175. {
  176. this->SetError("sub-command REGEX, mode MATCH needs "
  177. "at least 5 arguments total to command.");
  178. return false;
  179. }
  180. return this->RegexMatch(args);
  181. }
  182. else if(mode == "MATCHALL")
  183. {
  184. if(args.size() < 5)
  185. {
  186. this->SetError("sub-command REGEX, mode MATCHALL needs "
  187. "at least 5 arguments total to command.");
  188. return false;
  189. }
  190. return this->RegexMatchAll(args);
  191. }
  192. else if(mode == "REPLACE")
  193. {
  194. if(args.size() < 6)
  195. {
  196. this->SetError("sub-command REGEX, mode MATCH needs "
  197. "at least 6 arguments total to command.");
  198. return false;
  199. }
  200. return this->RegexReplace(args);
  201. }
  202. std::string e = "sub-command REGEX does not recognize mode "+mode;
  203. this->SetError(e.c_str());
  204. return false;
  205. }
  206. //----------------------------------------------------------------------------
  207. bool cmStringCommand::RegexMatch(std::vector<std::string> const& args)
  208. {
  209. //"STRING(REGEX MATCH <regular_expression> <output variable>
  210. // <input> [<input>...])\n";
  211. std::string regex = args[2];
  212. std::string outvar = args[3];
  213. // Concatenate all the last arguments together.
  214. std::string input = args[4];
  215. for(unsigned int i=5; i < args.size(); ++i)
  216. {
  217. input += args[i];
  218. }
  219. // Compile the regular expression.
  220. cmsys::RegularExpression re;
  221. if(!re.compile(regex.c_str()))
  222. {
  223. std::string e =
  224. "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
  225. this->SetError(e.c_str());
  226. return false;
  227. }
  228. // Scan through the input for all matches.
  229. std::string output;
  230. if(re.find(input.c_str()))
  231. {
  232. std::string::size_type l = re.start();
  233. std::string::size_type r = re.end();
  234. if(r-l == 0)
  235. {
  236. std::string e =
  237. "sub-command REGEX, mode MATCH regex \""+regex+
  238. "\" matched an empty string.";
  239. this->SetError(e.c_str());
  240. return false;
  241. }
  242. output = input.substr(l, r-l);
  243. }
  244. // Store the output in the provided variable.
  245. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  246. return true;
  247. }
  248. //----------------------------------------------------------------------------
  249. bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
  250. {
  251. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
  252. // [<input>...])\n";
  253. std::string regex = args[2];
  254. std::string outvar = args[3];
  255. // Concatenate all the last arguments together.
  256. std::string input = args[4];
  257. for(unsigned int i=5; i < args.size(); ++i)
  258. {
  259. input += args[i];
  260. }
  261. // Compile the regular expression.
  262. cmsys::RegularExpression re;
  263. if(!re.compile(regex.c_str()))
  264. {
  265. std::string e =
  266. "sub-command REGEX, mode MATCHALL failed to compile regex \""+
  267. regex+"\".";
  268. this->SetError(e.c_str());
  269. return false;
  270. }
  271. // Scan through the input for all matches.
  272. std::string output;
  273. const char* p = input.c_str();
  274. while(re.find(p))
  275. {
  276. std::string::size_type l = re.start();
  277. std::string::size_type r = re.end();
  278. if(r-l == 0)
  279. {
  280. std::string e = "sub-command REGEX, mode MATCHALL regex \""+
  281. regex+"\" matched an empty string.";
  282. this->SetError(e.c_str());
  283. return false;
  284. }
  285. if(output.length() > 0)
  286. {
  287. output += ";";
  288. }
  289. output += std::string(p+l, r-l);
  290. p += r;
  291. }
  292. // Store the output in the provided variable.
  293. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  294. return true;
  295. }
  296. //----------------------------------------------------------------------------
  297. bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
  298. {
  299. //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
  300. // <output variable> <input> [<input>...])\n"
  301. std::string regex = args[2];
  302. std::string replace = args[3];
  303. std::string outvar = args[4];
  304. // Pull apart the replace expression to find the escaped [0-9] values.
  305. std::vector<RegexReplacement> replacement;
  306. std::string::size_type l = 0;
  307. while(l < replace.length())
  308. {
  309. std::string::size_type r = replace.find("\\", l);
  310. if(r == std::string::npos)
  311. {
  312. r = replace.length();
  313. replacement.push_back(replace.substr(l, r-l));
  314. }
  315. else
  316. {
  317. if(r-l > 0)
  318. {
  319. replacement.push_back(replace.substr(l, r-l));
  320. }
  321. if(r == (replace.length()-1))
  322. {
  323. this->SetError("sub-command REGEX, mode REPLACE: "
  324. "replace-expression ends in a backslash.");
  325. return false;
  326. }
  327. if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
  328. {
  329. replacement.push_back(replace[r+1]-'0');
  330. }
  331. else if(replace[r+1] == 'n')
  332. {
  333. replacement.push_back("\n");
  334. }
  335. else if(replace[r+1] == '\\')
  336. {
  337. replacement.push_back("\\");
  338. }
  339. else
  340. {
  341. std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
  342. e += replace.substr(r, 2);
  343. e += "\"in replace-expression.";
  344. this->SetError(e.c_str());
  345. return false;
  346. }
  347. r += 2;
  348. }
  349. l = r;
  350. }
  351. // Concatenate all the last arguments together.
  352. std::string input = args[5];
  353. for(unsigned int i=6; i < args.size(); ++i)
  354. {
  355. input += args[i];
  356. }
  357. // Compile the regular expression.
  358. cmsys::RegularExpression re;
  359. if(!re.compile(regex.c_str()))
  360. {
  361. std::string e =
  362. "sub-command REGEX, mode REPLACE failed to compile regex \""+
  363. regex+"\".";
  364. this->SetError(e.c_str());
  365. return false;
  366. }
  367. // Scan through the input for all matches.
  368. std::string output;
  369. std::string::size_type base = 0;
  370. while(re.find(input.c_str()+base))
  371. {
  372. std::string::size_type l2 = re.start();
  373. std::string::size_type r = re.end();
  374. // Concatenate the part of the input that was not matched.
  375. output += input.substr(base, l2);
  376. // Make sure the match had some text.
  377. if(r-l2 == 0)
  378. {
  379. std::string e = "sub-command REGEX, mode REPLACE regex \""+
  380. regex+"\" matched an empty string.";
  381. this->SetError(e.c_str());
  382. return false;
  383. }
  384. // Concatenate the replacement for the match.
  385. for(unsigned int i=0; i < replacement.size(); ++i)
  386. {
  387. if(replacement[i].number < 0)
  388. {
  389. // This is just a plain-text part of the replacement.
  390. output += replacement[i].value;
  391. }
  392. else
  393. {
  394. // Replace with part of the match.
  395. int n = replacement[i].number;
  396. std::string::size_type start = re.start(n);
  397. std::string::size_type end = re.end(n);
  398. std::string::size_type len = input.length()-base;
  399. if((start != std::string::npos) && (end != std::string::npos) &&
  400. (start <= len) && (end <= len))
  401. {
  402. output += input.substr(base+start, end-start);
  403. }
  404. else
  405. {
  406. std::string e =
  407. "sub-command REGEX, mode REPLACE: replace expression \""+
  408. replace+"\" contains an out-of-range escape for regex \""+
  409. regex+"\".";
  410. this->SetError(e.c_str());
  411. return false;
  412. }
  413. }
  414. }
  415. // Move past the match.
  416. base += r;
  417. }
  418. // Concatenate the text after the last match.
  419. output += input.substr(base, input.length()-base);
  420. // Store the output in the provided variable.
  421. this->Makefile->AddDefinition(outvar.c_str(), output.c_str());
  422. return true;
  423. }
  424. //----------------------------------------------------------------------------
  425. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const&
  426. args)
  427. {
  428. if(args.size() < 2)
  429. {
  430. this->SetError("sub-command COMPARE requires a mode to be specified.");
  431. return false;
  432. }
  433. std::string mode = args[1];
  434. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  435. (mode == "LESS") || (mode == "GREATER"))
  436. {
  437. if(args.size() < 5)
  438. {
  439. std::string e = "sub-command COMPARE, mode ";
  440. e += mode;
  441. e += " needs at least 5 arguments total to command.";
  442. this->SetError(e.c_str());
  443. return false;
  444. }
  445. const std::string& left = args[2];
  446. const std::string& right = args[3];
  447. const std::string& outvar = args[4];
  448. bool result;
  449. if(mode == "LESS")
  450. {
  451. result = (left < right);
  452. }
  453. else if(mode == "GREATER")
  454. {
  455. result = (left > right);
  456. }
  457. else if(mode == "EQUAL")
  458. {
  459. result = (left == right);
  460. }
  461. else // if(mode == "NOTEQUAL")
  462. {
  463. result = !(left == right);
  464. }
  465. if(result)
  466. {
  467. this->Makefile->AddDefinition(outvar.c_str(), "1");
  468. }
  469. else
  470. {
  471. this->Makefile->AddDefinition(outvar.c_str(), "0");
  472. }
  473. return true;
  474. }
  475. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  476. this->SetError(e.c_str());
  477. return false;
  478. }
  479. //----------------------------------------------------------------------------
  480. bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const&
  481. args)
  482. {
  483. if(args.size() < 5)
  484. {
  485. this->SetError("sub-command REPLACE requires four arguments.");
  486. return false;
  487. }
  488. const std::string& matchExpression = args[1];
  489. const std::string& replaceExpression = args[2];
  490. const std::string& variableName = args[3];
  491. std::string input = args[4];
  492. for(unsigned int i=5; i < args.size(); ++i)
  493. {
  494. input += args[i];
  495. }
  496. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  497. replaceExpression.c_str());
  498. this->Makefile->AddDefinition(variableName.c_str(), input.c_str());
  499. return true;
  500. }
  501. //----------------------------------------------------------------------------
  502. bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const&
  503. args)
  504. {
  505. if(args.size() != 5)
  506. {
  507. this->SetError("sub-command REPLACE requires four arguments.");
  508. return false;
  509. }
  510. const std::string& stringValue = args[1];
  511. int begin = atoi(args[2].c_str());
  512. int end = atoi(args[3].c_str());
  513. const std::string& variableName = args[4];
  514. size_t stringLength = stringValue.size();
  515. int intStringLength = static_cast<int>(stringLength);
  516. if ( begin < 0 || begin > intStringLength )
  517. {
  518. cmOStringStream ostr;
  519. ostr << "begin index: " << begin << " is out of range 0 - "
  520. << stringLength;
  521. this->SetError(ostr.str().c_str());
  522. return false;
  523. }
  524. int leftOverLength = intStringLength - begin;
  525. if ( end < 0 || end > leftOverLength )
  526. {
  527. cmOStringStream ostr;
  528. ostr << "end index: " << end << " is out of range " << 0 << " - "
  529. << leftOverLength;
  530. this->SetError(ostr.str().c_str());
  531. return false;
  532. }
  533. this->Makefile->AddDefinition(variableName.c_str(),
  534. stringValue.substr(begin, end).c_str());
  535. return true;
  536. }
  537. //----------------------------------------------------------------------------
  538. bool cmStringCommand
  539. ::HandleLengthCommand(std::vector<std::string> const& args)
  540. {
  541. if(args.size() != 3)
  542. {
  543. this->SetError("sub-command LENGTH requires two arguments.");
  544. return false;
  545. }
  546. const std::string& stringValue = args[1];
  547. const std::string& variableName = args[2];
  548. size_t length = stringValue.size();
  549. char buffer[1024];
  550. sprintf(buffer, "%d", static_cast<int>(length));
  551. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  552. return true;
  553. }