cmStringCommand.cxx 13 KB

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