cmListCommand.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 "cmListCommand.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 cmListCommand::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 == "LENGTH")
  28. {
  29. return this->HandleLengthCommand(args);
  30. }
  31. if(subCommand == "GET")
  32. {
  33. return this->HandleGetCommand(args);
  34. }
  35. if(subCommand == "APPEND")
  36. {
  37. return this->HandleAppendCommand(args);
  38. }
  39. if(subCommand == "CONTAINS")
  40. {
  41. return this->HandleContainsCommand(args);
  42. }
  43. if(subCommand == "INSERT")
  44. {
  45. return this->HandleInsertCommand(args);
  46. }
  47. if(subCommand == "REMOVE_AT")
  48. {
  49. return this->HandleRemoveAtCommand(args);
  50. }
  51. if(subCommand == "REMOVE_ITEM")
  52. {
  53. return this->HandleRemoveItemCommand(args);
  54. }
  55. if(subCommand == "SORT")
  56. {
  57. return this->HandleSortCommand(args);
  58. }
  59. if(subCommand == "REVERSE")
  60. {
  61. return this->HandleReverseCommand(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 cmListCommand::GetListString(std::string& listString, const char* var)
  69. {
  70. if ( !var )
  71. {
  72. return false;
  73. }
  74. // get the old value
  75. const char* cacheValue
  76. = this->Makefile->GetDefinition(var);
  77. if(!cacheValue)
  78. {
  79. return false;
  80. }
  81. listString = cacheValue;
  82. return true;
  83. }
  84. //----------------------------------------------------------------------------
  85. bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
  86. {
  87. std::string listString;
  88. if ( !this->GetListString(listString, var) )
  89. {
  90. return false;
  91. }
  92. // expand the variable
  93. cmSystemTools::ExpandListArgument(listString, list);
  94. return true;
  95. }
  96. //----------------------------------------------------------------------------
  97. bool cmListCommand::HandleLengthCommand(std::vector<std::string> const& args)
  98. {
  99. if(args.size() != 3)
  100. {
  101. this->SetError("sub-command LENGTH requires two arguments.");
  102. return false;
  103. }
  104. const std::string& listName = args[1];
  105. const std::string& variableName = args[args.size() - 1];
  106. std::vector<std::string> varArgsExpanded;
  107. // do not check the return value here
  108. // if the list var is not found varArgsExpanded will have size 0
  109. // and we will return 0
  110. this->GetList(varArgsExpanded, listName.c_str());
  111. size_t length = varArgsExpanded.size();
  112. char buffer[1024];
  113. sprintf(buffer, "%d", static_cast<int>(length));
  114. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  115. return true;
  116. }
  117. //----------------------------------------------------------------------------
  118. bool cmListCommand::HandleGetCommand(std::vector<std::string> const& args)
  119. {
  120. if(args.size() < 4)
  121. {
  122. this->SetError("sub-command GET requires at least three arguments.");
  123. return false;
  124. }
  125. const std::string& listName = args[1];
  126. const std::string& variableName = args[args.size() - 1];
  127. // expand the variable
  128. std::vector<std::string> varArgsExpanded;
  129. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  130. {
  131. this->Makefile->AddDefinition(variableName.c_str(), "NOTFOUND");
  132. return true;
  133. }
  134. std::string value;
  135. size_t cc;
  136. for ( cc = 2; cc < args.size()-1; cc ++ )
  137. {
  138. int item = atoi(args[cc].c_str());
  139. if (value.size())
  140. {
  141. value += ";";
  142. }
  143. size_t nitem = varArgsExpanded.size();
  144. if ( item < 0 )
  145. {
  146. item = (int)nitem + item;
  147. }
  148. if ( item < 0 || nitem <= (size_t)item )
  149. {
  150. cmOStringStream str;
  151. str << "index: " << item << " out of range (-"
  152. << varArgsExpanded.size() << ", "
  153. << varArgsExpanded.size()-1 << ")";
  154. this->SetError(str.str().c_str());
  155. return false;
  156. }
  157. value += varArgsExpanded[item];
  158. }
  159. this->Makefile->AddDefinition(variableName.c_str(), value.c_str());
  160. return true;
  161. }
  162. //----------------------------------------------------------------------------
  163. bool cmListCommand::HandleAppendCommand(std::vector<std::string> const& args)
  164. {
  165. if(args.size() < 3)
  166. {
  167. this->SetError("sub-command APPEND requires at least two arguments.");
  168. return false;
  169. }
  170. const std::string& listName = args[1];
  171. // expand the variable
  172. std::string listString;
  173. this->GetListString(listString, listName.c_str());
  174. size_t cc;
  175. for ( cc = 2; cc < args.size(); ++ cc )
  176. {
  177. if ( listString.size() )
  178. {
  179. listString += ";";
  180. }
  181. listString += args[cc];
  182. }
  183. this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
  184. return true;
  185. }
  186. //----------------------------------------------------------------------------
  187. bool cmListCommand::HandleContainsCommand(std::vector<std::string> const& args)
  188. {
  189. if(args.size() != 4)
  190. {
  191. this->SetError("sub-command CONTAINS requires three arguments.");
  192. return false;
  193. }
  194. const std::string& listName = args[1];
  195. const std::string& variableName = args[args.size() - 1];
  196. // expand the variable
  197. std::vector<std::string> varArgsExpanded;
  198. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  199. {
  200. this->Makefile->AddDefinition(variableName.c_str(), "FALSE");
  201. return true;
  202. }
  203. std::vector<std::string>::iterator it;
  204. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  205. {
  206. if ( *it == args[2] )
  207. {
  208. this->Makefile->AddDefinition(variableName.c_str(), "TRUE");
  209. return true;
  210. }
  211. }
  212. this->Makefile->AddDefinition(variableName.c_str(), "FALSE");
  213. return true;
  214. }
  215. //----------------------------------------------------------------------------
  216. bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
  217. {
  218. if(args.size() < 4)
  219. {
  220. this->SetError("sub-command INSERT requires at least three arguments.");
  221. return false;
  222. }
  223. const std::string& listName = args[1];
  224. // expand the variable
  225. int item = atoi(args[2].c_str());
  226. std::vector<std::string> varArgsExpanded;
  227. if ( !this->GetList(varArgsExpanded, listName.c_str()) && item != 0)
  228. {
  229. cmOStringStream str;
  230. str << "index: " << item << " out of range (0, 0)";
  231. this->SetError(str.str().c_str());
  232. return false;
  233. }
  234. if ( varArgsExpanded.size() != 0 )
  235. {
  236. size_t nitem = varArgsExpanded.size();
  237. if ( item < 0 )
  238. {
  239. item = (int)nitem + item;
  240. }
  241. if ( item < 0 || nitem <= (size_t)item )
  242. {
  243. cmOStringStream str;
  244. str << "index: " << item << " out of range (-"
  245. << varArgsExpanded.size() << ", "
  246. << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
  247. this->SetError(str.str().c_str());
  248. return false;
  249. }
  250. }
  251. size_t cc;
  252. size_t cnt = 0;
  253. for ( cc = 3; cc < args.size(); ++ cc )
  254. {
  255. varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
  256. cnt ++;
  257. }
  258. std::string value;
  259. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  260. {
  261. if (value.size())
  262. {
  263. value += ";";
  264. }
  265. value += varArgsExpanded[cc];
  266. }
  267. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  268. return true;
  269. }
  270. //----------------------------------------------------------------------------
  271. bool cmListCommand
  272. ::HandleRemoveItemCommand(std::vector<std::string> const& args)
  273. {
  274. if(args.size() < 3)
  275. {
  276. this->SetError("sub-command REMOVE_ITEM requires two or more arguments.");
  277. return false;
  278. }
  279. const std::string& listName = args[1];
  280. // expand the variable
  281. std::vector<std::string> varArgsExpanded;
  282. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  283. {
  284. this->SetError("sub-command REMOVE_ITEM requires list to be present.");
  285. return false;
  286. }
  287. size_t cc;
  288. for ( cc = 2; cc < args.size(); ++ cc )
  289. {
  290. size_t kk = 0;
  291. while ( kk < varArgsExpanded.size() )
  292. {
  293. if ( varArgsExpanded[kk] == args[cc] )
  294. {
  295. varArgsExpanded.erase(varArgsExpanded.begin()+kk);
  296. }
  297. else
  298. {
  299. kk ++;
  300. }
  301. }
  302. }
  303. std::string value;
  304. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  305. {
  306. if (value.size())
  307. {
  308. value += ";";
  309. }
  310. value += varArgsExpanded[cc];
  311. }
  312. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  313. return true;
  314. }
  315. //----------------------------------------------------------------------------
  316. bool cmListCommand
  317. ::HandleReverseCommand(std::vector<std::string> const& args)
  318. {
  319. if(args.size() < 2)
  320. {
  321. this->SetError("sub-command REVERSE requires a list as an argument.");
  322. return false;
  323. }
  324. const std::string& listName = args[1];
  325. // expand the variable
  326. std::vector<std::string> varArgsExpanded;
  327. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  328. {
  329. this->SetError("sub-command REVERSE requires list to be present.");
  330. return false;
  331. }
  332. std::string value;
  333. std::vector<std::string>::reverse_iterator it;
  334. for ( it = varArgsExpanded.rbegin(); it != varArgsExpanded.rend(); ++ it )
  335. {
  336. if (value.size())
  337. {
  338. value += ";";
  339. }
  340. value += it->c_str();
  341. }
  342. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  343. return true;
  344. }
  345. //----------------------------------------------------------------------------
  346. bool cmListCommand
  347. ::HandleSortCommand(std::vector<std::string> const& args)
  348. {
  349. if(args.size() < 2)
  350. {
  351. this->SetError("sub-command SORT requires a list as an argument.");
  352. return false;
  353. }
  354. const std::string& listName = args[1];
  355. // expand the variable
  356. std::vector<std::string> varArgsExpanded;
  357. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  358. {
  359. this->SetError("sub-command SORT requires list to be present.");
  360. return false;
  361. }
  362. std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
  363. std::string value;
  364. std::vector<std::string>::iterator it;
  365. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  366. {
  367. if (value.size())
  368. {
  369. value += ";";
  370. }
  371. value += it->c_str();
  372. }
  373. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  374. return true;
  375. }
  376. //----------------------------------------------------------------------------
  377. bool cmListCommand::HandleRemoveAtCommand(
  378. std::vector<std::string> const& args)
  379. {
  380. if(args.size() < 3)
  381. {
  382. this->SetError("sub-command REMOVE_AT requires at least "
  383. "two arguments.");
  384. return false;
  385. }
  386. const std::string& listName = args[1];
  387. // expand the variable
  388. std::vector<std::string> varArgsExpanded;
  389. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  390. {
  391. this->SetError("sub-command REMOVE_AT requires list to be present.");
  392. return false;
  393. }
  394. size_t cc;
  395. std::vector<size_t> removed;
  396. for ( cc = 2; cc < args.size(); ++ cc )
  397. {
  398. int item = atoi(args[cc].c_str());
  399. size_t nitem = varArgsExpanded.size();
  400. if ( item < 0 )
  401. {
  402. item = (int)nitem + item;
  403. }
  404. if ( item < 0 || nitem <= (size_t)item )
  405. {
  406. cmOStringStream str;
  407. str << "index: " << item << " out of range (-"
  408. << varArgsExpanded.size() << ", "
  409. << varArgsExpanded.size()-1 << ")";
  410. this->SetError(str.str().c_str());
  411. return false;
  412. }
  413. removed.push_back(static_cast<size_t>(item));
  414. }
  415. std::string value;
  416. for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
  417. {
  418. size_t kk;
  419. bool found = false;
  420. for ( kk = 0; kk < removed.size(); ++ kk )
  421. {
  422. if ( cc == removed[kk] )
  423. {
  424. found = true;
  425. }
  426. }
  427. if ( !found )
  428. {
  429. if (value.size())
  430. {
  431. value += ";";
  432. }
  433. value += varArgsExpanded[cc];
  434. }
  435. }
  436. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  437. return true;
  438. }