cmStringCommand.cxx 14 KB

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