cmStringCommand.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. m_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. m_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. m_Makefile->ConfigureString(args[1], output, atOnly, escapeQuotes);
  159. // Store the output in the provided variable.
  160. m_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> <input> [<input>...])\n";
  210. std::string regex = args[2];
  211. std::string outvar = args[3];
  212. // Concatenate all the last arguments together.
  213. std::string input = args[4];
  214. for(unsigned int i=5; i < args.size(); ++i)
  215. {
  216. input += args[i];
  217. }
  218. // Compile the regular expression.
  219. cmsys::RegularExpression re;
  220. if(!re.compile(regex.c_str()))
  221. {
  222. std::string e = "sub-command REGEX, mode MATCH failed to compile regex \""+regex+"\".";
  223. this->SetError(e.c_str());
  224. return false;
  225. }
  226. // Scan through the input for all matches.
  227. std::string output;
  228. if(re.find(input.c_str()))
  229. {
  230. std::string::size_type l = re.start();
  231. std::string::size_type r = re.end();
  232. if(r-l == 0)
  233. {
  234. std::string e = "sub-command REGEX, mode MATCH regex \""+regex+"\" matched an empty string.";
  235. this->SetError(e.c_str());
  236. return false;
  237. }
  238. output = input.substr(l, r-l);
  239. }
  240. // Store the output in the provided variable.
  241. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  242. return true;
  243. }
  244. //----------------------------------------------------------------------------
  245. bool cmStringCommand::RegexMatchAll(std::vector<std::string> const& args)
  246. {
  247. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input> [<input>...])\n";
  248. std::string regex = args[2];
  249. std::string outvar = args[3];
  250. // Concatenate all the last arguments together.
  251. std::string input = args[4];
  252. for(unsigned int i=5; i < args.size(); ++i)
  253. {
  254. input += args[i];
  255. }
  256. // Compile the regular expression.
  257. cmsys::RegularExpression re;
  258. if(!re.compile(regex.c_str()))
  259. {
  260. std::string e = "sub-command REGEX, mode MATCHALL failed to compile regex \""+regex+"\".";
  261. this->SetError(e.c_str());
  262. return false;
  263. }
  264. // Scan through the input for all matches.
  265. std::string output;
  266. const char* p = input.c_str();
  267. while(re.find(p))
  268. {
  269. std::string::size_type l = re.start();
  270. std::string::size_type r = re.end();
  271. if(r-l == 0)
  272. {
  273. std::string e = "sub-command REGEX, mode MATCHALL regex \""+regex+"\" matched an empty string.";
  274. this->SetError(e.c_str());
  275. return false;
  276. }
  277. if(output.length() > 0)
  278. {
  279. output += ";";
  280. }
  281. output += std::string(p+l, r-l);
  282. p += r;
  283. }
  284. // Store the output in the provided variable.
  285. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  286. return true;
  287. }
  288. //----------------------------------------------------------------------------
  289. bool cmStringCommand::RegexReplace(std::vector<std::string> const& args)
  290. {
  291. //"STRING(REGEX REPLACE <regular_expression> <replace_expression> <output variable> <input> [<input>...])\n"
  292. std::string regex = args[2];
  293. std::string replace = args[3];
  294. std::string outvar = args[4];
  295. // Pull apart the replace expression to find the escaped [0-9] values.
  296. std::vector<RegexReplacement> replacement;
  297. std::string::size_type l = 0;
  298. while(l < replace.length())
  299. {
  300. std::string::size_type r = replace.find("\\", l);
  301. if(r == std::string::npos)
  302. {
  303. r = replace.length();
  304. replacement.push_back(replace.substr(l, r-l));
  305. }
  306. else
  307. {
  308. if(r-l > 0)
  309. {
  310. replacement.push_back(replace.substr(l, r-l));
  311. }
  312. if(r == (replace.length()-1))
  313. {
  314. this->SetError("sub-command REGEX, mode REPLACE: "
  315. "replace-expression ends in a backslash.");
  316. return false;
  317. }
  318. if((replace[r+1] >= '0') && (replace[r+1] <= '9'))
  319. {
  320. replacement.push_back(replace[r+1]-'0');
  321. }
  322. else if(replace[r+1] == 'n')
  323. {
  324. replacement.push_back("\n");
  325. }
  326. else if(replace[r+1] == '\\')
  327. {
  328. replacement.push_back("\\");
  329. }
  330. else
  331. {
  332. std::string e = "sub-command REGEX, mode REPLACE: Unknown escape \"";
  333. e += replace.substr(r, 2);
  334. e += "\"in replace-expression.";
  335. this->SetError(e.c_str());
  336. return false;
  337. }
  338. r += 2;
  339. }
  340. l = r;
  341. }
  342. // Concatenate all the last arguments together.
  343. std::string input = args[5];
  344. for(unsigned int i=6; i < args.size(); ++i)
  345. {
  346. input += args[i];
  347. }
  348. // Compile the regular expression.
  349. cmsys::RegularExpression re;
  350. if(!re.compile(regex.c_str()))
  351. {
  352. std::string e = "sub-command REGEX, mode REPLACE failed to compile regex \""+regex+"\".";
  353. this->SetError(e.c_str());
  354. return false;
  355. }
  356. // Scan through the input for all matches.
  357. std::string output;
  358. std::string::size_type base = 0;
  359. while(re.find(input.c_str()+base))
  360. {
  361. std::string::size_type l2 = re.start();
  362. std::string::size_type r = re.end();
  363. // Concatenate the part of the input that was not matched.
  364. output += input.substr(base, l2);
  365. // Make sure the match had some text.
  366. if(r-l2 == 0)
  367. {
  368. std::string e = "sub-command REGEX, mode REPLACE regex \""+regex+"\" matched an empty string.";
  369. this->SetError(e.c_str());
  370. return false;
  371. }
  372. // Concatenate the replacement for the match.
  373. for(unsigned int i=0; i < replacement.size(); ++i)
  374. {
  375. if(replacement[i].number < 0)
  376. {
  377. // This is just a plain-text part of the replacement.
  378. output += replacement[i].value;
  379. }
  380. else
  381. {
  382. // Replace with part of the match.
  383. int n = replacement[i].number;
  384. std::string::size_type start = re.start(n);
  385. std::string::size_type end = re.end(n);
  386. std::string::size_type len = input.length()-base;
  387. if((start != std::string::npos) && (end != std::string::npos) &&
  388. (start <= len) && (end <= len))
  389. {
  390. output += input.substr(base+start, end-start);
  391. }
  392. else
  393. {
  394. std::string e =
  395. "sub-command REGEX, mode REPLACE: replace expression \""+
  396. replace+"\" contains an out-of-range escape for regex \""+
  397. regex+"\".";
  398. this->SetError(e.c_str());
  399. return false;
  400. }
  401. }
  402. }
  403. // Move past the match.
  404. base += r;
  405. }
  406. // Concatenate the text after the last match.
  407. output += input.substr(base, input.length()-base);
  408. // Store the output in the provided variable.
  409. m_Makefile->AddDefinition(outvar.c_str(), output.c_str());
  410. return true;
  411. }
  412. //----------------------------------------------------------------------------
  413. bool cmStringCommand::HandleCompareCommand(std::vector<std::string> const& args)
  414. {
  415. if(args.size() < 2)
  416. {
  417. this->SetError("sub-command COMPARE requires a mode to be specified.");
  418. return false;
  419. }
  420. std::string mode = args[1];
  421. if((mode == "EQUAL") || (mode == "NOTEQUAL") ||
  422. (mode == "LESS") || (mode == "GREATER"))
  423. {
  424. if(args.size() < 5)
  425. {
  426. std::string e = "sub-command COMPARE, mode ";
  427. e += mode;
  428. e += " needs at least 5 arguments total to command.";
  429. this->SetError(e.c_str());
  430. return false;
  431. }
  432. const std::string& left = args[2];
  433. const std::string& right = args[3];
  434. const std::string& outvar = args[4];
  435. bool result;
  436. if(mode == "LESS")
  437. {
  438. result = (left < right);
  439. }
  440. else if(mode == "GREATER")
  441. {
  442. result = (left > right);
  443. }
  444. else if(mode == "EQUAL")
  445. {
  446. result = (left == right);
  447. }
  448. else // if(mode == "NOTEQUAL")
  449. {
  450. result = !(left == right);
  451. }
  452. if(result)
  453. {
  454. m_Makefile->AddDefinition(outvar.c_str(), "1");
  455. }
  456. else
  457. {
  458. m_Makefile->AddDefinition(outvar.c_str(), "0");
  459. }
  460. return true;
  461. }
  462. std::string e = "sub-command COMPARE does not recognize mode "+mode;
  463. this->SetError(e.c_str());
  464. return false;
  465. }
  466. //----------------------------------------------------------------------------
  467. bool cmStringCommand::HandleReplaceCommand(std::vector<std::string> const& args)
  468. {
  469. if(args.size() < 5)
  470. {
  471. this->SetError("sub-command REPLACE requires four arguments.");
  472. return false;
  473. }
  474. const std::string& matchExpression = args[1];
  475. const std::string& replaceExpression = args[2];
  476. const std::string& variableName = args[3];
  477. std::string input = args[4];
  478. for(unsigned int i=5; i < args.size(); ++i)
  479. {
  480. input += args[i];
  481. }
  482. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(), replaceExpression.c_str());
  483. m_Makefile->AddDefinition(variableName.c_str(), input.c_str());
  484. return true;
  485. }
  486. //----------------------------------------------------------------------------
  487. bool cmStringCommand::HandleSubstringCommand(std::vector<std::string> const& args)
  488. {
  489. if(args.size() != 5)
  490. {
  491. this->SetError("sub-command REPLACE requires four arguments.");
  492. return false;
  493. }
  494. const std::string& stringValue = args[1];
  495. int begin = atoi(args[2].c_str());
  496. int end = atoi(args[3].c_str());
  497. const std::string& variableName = args[4];
  498. size_t stringLength = stringValue.size();
  499. int intStringLength = static_cast<int>(stringLength);
  500. if ( begin < 0 || begin > intStringLength )
  501. {
  502. cmOStringStream ostr;
  503. ostr << "begin index: " << begin << " is out of range 0 - " << stringLength;
  504. this->SetError(ostr.str().c_str());
  505. return false;
  506. }
  507. int leftOverLength = intStringLength - begin;
  508. if ( end < 0 || end > leftOverLength )
  509. {
  510. cmOStringStream ostr;
  511. ostr << "end index: " << end << " is out of range " << 0 << " - " << leftOverLength;
  512. this->SetError(ostr.str().c_str());
  513. return false;
  514. }
  515. m_Makefile->AddDefinition(variableName.c_str(), stringValue.substr(begin, end).c_str());
  516. return true;
  517. }
  518. //----------------------------------------------------------------------------
  519. bool cmStringCommand::HandleLengthCommand(std::vector<std::string> const& args)
  520. {
  521. if(args.size() != 3)
  522. {
  523. this->SetError("sub-command LENGTH requires two arguments.");
  524. return false;
  525. }
  526. const std::string& stringValue = args[1];
  527. const std::string& variableName = args[2];
  528. size_t length = stringValue.size();
  529. char buffer[1024];
  530. sprintf(buffer, "%d", static_cast<int>(length));
  531. m_Makefile->AddDefinition(variableName.c_str(), buffer);
  532. return true;
  533. }