cmListCommand.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 == "FIND")
  40. {
  41. return this->HandleFindCommand(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() < 2)
  166. {
  167. this->SetError("sub-command APPEND requires at least one argument.");
  168. return false;
  169. }
  170. // Skip if nothing to append.
  171. if(args.size() < 3)
  172. {
  173. return true;
  174. }
  175. const std::string& listName = args[1];
  176. // expand the variable
  177. std::string listString;
  178. this->GetListString(listString, listName.c_str());
  179. size_t cc;
  180. for ( cc = 2; cc < args.size(); ++ cc )
  181. {
  182. if ( listString.size() )
  183. {
  184. listString += ";";
  185. }
  186. listString += args[cc];
  187. }
  188. this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
  189. return true;
  190. }
  191. //----------------------------------------------------------------------------
  192. bool cmListCommand::HandleFindCommand(std::vector<std::string> const& args)
  193. {
  194. if(args.size() != 4)
  195. {
  196. this->SetError("sub-command FIND requires three arguments.");
  197. return false;
  198. }
  199. const std::string& listName = args[1];
  200. const std::string& variableName = args[args.size() - 1];
  201. // expand the variable
  202. std::vector<std::string> varArgsExpanded;
  203. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  204. {
  205. this->Makefile->AddDefinition(variableName.c_str(), "-1");
  206. return true;
  207. }
  208. std::vector<std::string>::iterator it;
  209. unsigned int index = 0;
  210. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  211. {
  212. if ( *it == args[2] )
  213. {
  214. char indexString[32];
  215. sprintf(indexString, "%d", index);
  216. this->Makefile->AddDefinition(variableName.c_str(), indexString);
  217. return true;
  218. }
  219. index++;
  220. }
  221. this->Makefile->AddDefinition(variableName.c_str(), "-1");
  222. return true;
  223. }
  224. //----------------------------------------------------------------------------
  225. bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
  226. {
  227. if(args.size() < 4)
  228. {
  229. this->SetError("sub-command INSERT requires at least three arguments.");
  230. return false;
  231. }
  232. const std::string& listName = args[1];
  233. // expand the variable
  234. int item = atoi(args[2].c_str());
  235. std::vector<std::string> varArgsExpanded;
  236. if ( !this->GetList(varArgsExpanded, listName.c_str()) && item != 0)
  237. {
  238. cmOStringStream str;
  239. str << "index: " << item << " out of range (0, 0)";
  240. this->SetError(str.str().c_str());
  241. return false;
  242. }
  243. if ( varArgsExpanded.size() != 0 )
  244. {
  245. size_t nitem = varArgsExpanded.size();
  246. if ( item < 0 )
  247. {
  248. item = (int)nitem + item;
  249. }
  250. if ( item < 0 || nitem <= (size_t)item )
  251. {
  252. cmOStringStream str;
  253. str << "index: " << item << " out of range (-"
  254. << varArgsExpanded.size() << ", "
  255. << (varArgsExpanded.size() == 0?0:(varArgsExpanded.size()-1)) << ")";
  256. this->SetError(str.str().c_str());
  257. return false;
  258. }
  259. }
  260. size_t cc;
  261. size_t cnt = 0;
  262. for ( cc = 3; cc < args.size(); ++ cc )
  263. {
  264. varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
  265. cnt ++;
  266. }
  267. std::string value;
  268. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  269. {
  270. if (value.size())
  271. {
  272. value += ";";
  273. }
  274. value += varArgsExpanded[cc];
  275. }
  276. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  277. return true;
  278. }
  279. //----------------------------------------------------------------------------
  280. bool cmListCommand
  281. ::HandleRemoveItemCommand(std::vector<std::string> const& args)
  282. {
  283. if(args.size() < 3)
  284. {
  285. this->SetError("sub-command REMOVE_ITEM requires two or more arguments.");
  286. return false;
  287. }
  288. const std::string& listName = args[1];
  289. // expand the variable
  290. std::vector<std::string> varArgsExpanded;
  291. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  292. {
  293. this->SetError("sub-command REMOVE_ITEM requires list to be present.");
  294. return false;
  295. }
  296. size_t cc;
  297. for ( cc = 2; cc < args.size(); ++ cc )
  298. {
  299. size_t kk = 0;
  300. while ( kk < varArgsExpanded.size() )
  301. {
  302. if ( varArgsExpanded[kk] == args[cc] )
  303. {
  304. varArgsExpanded.erase(varArgsExpanded.begin()+kk);
  305. }
  306. else
  307. {
  308. kk ++;
  309. }
  310. }
  311. }
  312. std::string value;
  313. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  314. {
  315. if (value.size())
  316. {
  317. value += ";";
  318. }
  319. value += varArgsExpanded[cc];
  320. }
  321. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  322. return true;
  323. }
  324. //----------------------------------------------------------------------------
  325. bool cmListCommand
  326. ::HandleReverseCommand(std::vector<std::string> const& args)
  327. {
  328. if(args.size() < 2)
  329. {
  330. this->SetError("sub-command REVERSE requires a list as an argument.");
  331. return false;
  332. }
  333. const std::string& listName = args[1];
  334. // expand the variable
  335. std::vector<std::string> varArgsExpanded;
  336. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  337. {
  338. this->SetError("sub-command REVERSE requires list to be present.");
  339. return false;
  340. }
  341. std::string value;
  342. std::vector<std::string>::reverse_iterator it;
  343. for ( it = varArgsExpanded.rbegin(); it != varArgsExpanded.rend(); ++ it )
  344. {
  345. if (value.size())
  346. {
  347. value += ";";
  348. }
  349. value += it->c_str();
  350. }
  351. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  352. return true;
  353. }
  354. //----------------------------------------------------------------------------
  355. bool cmListCommand
  356. ::HandleSortCommand(std::vector<std::string> const& args)
  357. {
  358. if(args.size() < 2)
  359. {
  360. this->SetError("sub-command SORT requires a list as an argument.");
  361. return false;
  362. }
  363. const std::string& listName = args[1];
  364. // expand the variable
  365. std::vector<std::string> varArgsExpanded;
  366. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  367. {
  368. this->SetError("sub-command SORT requires list to be present.");
  369. return false;
  370. }
  371. std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
  372. std::string value;
  373. std::vector<std::string>::iterator it;
  374. for ( it = varArgsExpanded.begin(); it != varArgsExpanded.end(); ++ it )
  375. {
  376. if (value.size())
  377. {
  378. value += ";";
  379. }
  380. value += it->c_str();
  381. }
  382. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  383. return true;
  384. }
  385. //----------------------------------------------------------------------------
  386. bool cmListCommand::HandleRemoveAtCommand(
  387. std::vector<std::string> const& args)
  388. {
  389. if(args.size() < 3)
  390. {
  391. this->SetError("sub-command REMOVE_AT requires at least "
  392. "two arguments.");
  393. return false;
  394. }
  395. const std::string& listName = args[1];
  396. // expand the variable
  397. std::vector<std::string> varArgsExpanded;
  398. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  399. {
  400. this->SetError("sub-command REMOVE_AT requires list to be present.");
  401. return false;
  402. }
  403. size_t cc;
  404. std::vector<size_t> removed;
  405. for ( cc = 2; cc < args.size(); ++ cc )
  406. {
  407. int item = atoi(args[cc].c_str());
  408. size_t nitem = varArgsExpanded.size();
  409. if ( item < 0 )
  410. {
  411. item = (int)nitem + item;
  412. }
  413. if ( item < 0 || nitem <= (size_t)item )
  414. {
  415. cmOStringStream str;
  416. str << "index: " << item << " out of range (-"
  417. << varArgsExpanded.size() << ", "
  418. << varArgsExpanded.size()-1 << ")";
  419. this->SetError(str.str().c_str());
  420. return false;
  421. }
  422. removed.push_back(static_cast<size_t>(item));
  423. }
  424. std::string value;
  425. for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
  426. {
  427. size_t kk;
  428. bool found = false;
  429. for ( kk = 0; kk < removed.size(); ++ kk )
  430. {
  431. if ( cc == removed[kk] )
  432. {
  433. found = true;
  434. }
  435. }
  436. if ( !found )
  437. {
  438. if (value.size())
  439. {
  440. value += ";";
  441. }
  442. value += varArgsExpanded[cc];
  443. }
  444. }
  445. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  446. return true;
  447. }