cmListCommand.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 == "SET")
  36. {
  37. return this->HandleSetCommand(args);
  38. }
  39. if(subCommand == "INSERT")
  40. {
  41. return this->HandleInsertCommand(args);
  42. }
  43. if(subCommand == "REMOVE")
  44. {
  45. return this->HandleRemoveCommand(args);
  46. }
  47. if(subCommand == "REMOVE_ITEM")
  48. {
  49. return this->HandleRemoveItemCommand(args);
  50. }
  51. std::string e = "does not recognize sub-command "+subCommand;
  52. this->SetError(e.c_str());
  53. return false;
  54. }
  55. //----------------------------------------------------------------------------
  56. bool cmListCommand::GetListString(std::string& listString, const char* var)
  57. {
  58. if ( !var )
  59. {
  60. return false;
  61. }
  62. // get the old value
  63. const char* cacheValue
  64. = this->Makefile->GetDefinition(var);
  65. if(!cacheValue)
  66. {
  67. cmOStringStream str;
  68. str << "cannot find variable: " << var;
  69. this->SetError(str.str().c_str());
  70. return false;
  71. }
  72. listString = cacheValue;
  73. return true;
  74. }
  75. //----------------------------------------------------------------------------
  76. bool cmListCommand::GetList(std::vector<std::string>& list, const char* var)
  77. {
  78. std::string listString;
  79. if ( !this->GetListString(listString, var) )
  80. {
  81. return false;
  82. }
  83. // expand the variable
  84. cmSystemTools::ExpandListArgument(listString, list);
  85. return true;
  86. }
  87. //----------------------------------------------------------------------------
  88. bool cmListCommand::HandleLengthCommand(std::vector<std::string> const& args)
  89. {
  90. if(args.size() != 3)
  91. {
  92. this->SetError("sub-command LENGTH requires two arguments.");
  93. return false;
  94. }
  95. const std::string& listName = args[1];
  96. const std::string& variableName = args[args.size() - 1];
  97. std::vector<std::string> varArgsExpanded;
  98. // do not check the return value here
  99. // if the list var is not found varArgsExpanded will have size 0
  100. // and we will return 0
  101. this->GetList(varArgsExpanded, listName.c_str());
  102. size_t length = varArgsExpanded.size();
  103. char buffer[1024];
  104. sprintf(buffer, "%d", static_cast<int>(length));
  105. this->Makefile->AddDefinition(variableName.c_str(), buffer);
  106. return true;
  107. }
  108. //----------------------------------------------------------------------------
  109. bool cmListCommand::HandleGetCommand(std::vector<std::string> const& args)
  110. {
  111. if(args.size() < 4)
  112. {
  113. this->SetError("sub-command GET requires at least three arguments.");
  114. return false;
  115. }
  116. const std::string& listName = args[1];
  117. const std::string& variableName = args[args.size() - 1];
  118. // expand the variable
  119. std::vector<std::string> varArgsExpanded;
  120. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  121. {
  122. return false;
  123. }
  124. std::string value;
  125. size_t cc;
  126. for ( cc = 2; cc < args.size()-1; cc ++ )
  127. {
  128. int item = atoi(args[cc].c_str());
  129. if (value.size())
  130. {
  131. value += ";";
  132. }
  133. size_t nitem = varArgsExpanded.size();
  134. if ( item < 0 )
  135. {
  136. item = (int)nitem + item;
  137. }
  138. if ( item < 0 || nitem <= (size_t)item )
  139. {
  140. cmOStringStream str;
  141. str << "index: " << item << " out of range (-" << varArgsExpanded.size() << ", " << varArgsExpanded.size()-1 << ")";
  142. this->SetError(str.str().c_str());
  143. return false;
  144. }
  145. value += varArgsExpanded[item];
  146. }
  147. this->Makefile->AddDefinition(variableName.c_str(), value.c_str());
  148. return true;
  149. }
  150. //----------------------------------------------------------------------------
  151. bool cmListCommand::HandleSetCommand(std::vector<std::string> const& args)
  152. {
  153. if(args.size() < 3)
  154. {
  155. this->SetError("sub-command SET requires at least two arguments.");
  156. return false;
  157. }
  158. const std::string& listName = args[1];
  159. // expand the variable
  160. std::string listString;
  161. if ( !this->GetListString(listString, listName.c_str()) )
  162. {
  163. return false;
  164. }
  165. size_t cc;
  166. for ( cc = 2; cc < args.size(); ++ cc )
  167. {
  168. if ( listString.size() )
  169. {
  170. listString += ";";
  171. }
  172. listString += args[cc];
  173. }
  174. this->Makefile->AddDefinition(listName.c_str(), listString.c_str());
  175. return true;
  176. }
  177. //----------------------------------------------------------------------------
  178. bool cmListCommand::HandleInsertCommand(std::vector<std::string> const& args)
  179. {
  180. if(args.size() < 4)
  181. {
  182. this->SetError("sub-command INSERT requires at least three arguments.");
  183. return false;
  184. }
  185. const std::string& listName = args[1];
  186. // expand the variable
  187. std::vector<std::string> varArgsExpanded;
  188. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  189. {
  190. return false;
  191. }
  192. int item = atoi(args[2].c_str());
  193. size_t nitem = varArgsExpanded.size();
  194. if ( item < 0 )
  195. {
  196. item = (int)nitem + item;
  197. }
  198. if ( item < 0 || nitem <= (size_t)item )
  199. {
  200. cmOStringStream str;
  201. str << "index: " << item << " out of range (-" << varArgsExpanded.size() << ", " << varArgsExpanded.size()-1 << ")";
  202. this->SetError(str.str().c_str());
  203. return false;
  204. }
  205. size_t cc;
  206. size_t cnt = 0;
  207. for ( cc = 3; cc < args.size(); ++ cc )
  208. {
  209. varArgsExpanded.insert(varArgsExpanded.begin()+item+cnt, args[cc]);
  210. cnt ++;
  211. }
  212. std::string value;
  213. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  214. {
  215. if (value.size())
  216. {
  217. value += ";";
  218. }
  219. value += varArgsExpanded[cc];
  220. }
  221. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  222. return true;
  223. }
  224. //----------------------------------------------------------------------------
  225. bool cmListCommand::HandleRemoveCommand(std::vector<std::string> const& args)
  226. {
  227. if(args.size() < 3)
  228. {
  229. this->SetError("sub-command REMOVE requires at least two arguments.");
  230. return false;
  231. }
  232. const std::string& listName = args[1];
  233. // expand the variable
  234. std::vector<std::string> varArgsExpanded;
  235. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  236. {
  237. return false;
  238. }
  239. size_t cc;
  240. for ( cc = 2; cc < args.size(); ++ cc )
  241. {
  242. size_t kk = 0;
  243. while ( kk < varArgsExpanded.size() )
  244. {
  245. if ( varArgsExpanded[kk] == args[cc] )
  246. {
  247. varArgsExpanded.erase(varArgsExpanded.begin()+kk);
  248. }
  249. kk ++;
  250. }
  251. }
  252. std::string value;
  253. for ( cc = 0; cc < varArgsExpanded.size(); cc ++ )
  254. {
  255. if (value.size())
  256. {
  257. value += ";";
  258. }
  259. value += varArgsExpanded[cc];
  260. }
  261. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  262. return true;
  263. }
  264. //----------------------------------------------------------------------------
  265. bool cmListCommand::HandleRemoveItemCommand(std::vector<std::string> const& args)
  266. {
  267. if(args.size() < 3)
  268. {
  269. this->SetError("sub-command REMOVE_ITEM requires at least two arguments.");
  270. return false;
  271. }
  272. const std::string& listName = args[1];
  273. // expand the variable
  274. std::vector<std::string> varArgsExpanded;
  275. if ( !this->GetList(varArgsExpanded, listName.c_str()) )
  276. {
  277. return false;
  278. }
  279. size_t cc;
  280. std::vector<size_t> removed;
  281. for ( cc = 2; cc < args.size(); ++ cc )
  282. {
  283. int item = atoi(args[cc].c_str());
  284. size_t nitem = varArgsExpanded.size();
  285. if ( item < 0 )
  286. {
  287. item = (int)nitem + item;
  288. }
  289. if ( item < 0 || nitem <= (size_t)item )
  290. {
  291. cmOStringStream str;
  292. str << "index: " << item << " out of range (-" << varArgsExpanded.size() << ", " << varArgsExpanded.size()-1 << ")";
  293. this->SetError(str.str().c_str());
  294. return false;
  295. }
  296. removed.push_back(static_cast<size_t>(item));
  297. }
  298. std::string value;
  299. for ( cc = 0; cc < varArgsExpanded.size(); ++ cc )
  300. {
  301. size_t kk;
  302. bool found = false;
  303. for ( kk = 0; kk < removed.size(); ++ kk )
  304. {
  305. if ( cc == removed[kk] )
  306. {
  307. found = true;
  308. }
  309. }
  310. if ( !found )
  311. {
  312. if (value.size())
  313. {
  314. value += ";";
  315. }
  316. value += varArgsExpanded[cc];
  317. }
  318. }
  319. this->Makefile->AddDefinition(listName.c_str(), value.c_str());
  320. return true;
  321. }