cmCommandArgumentParserHelper.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCommandArgumentParserHelper.h"
  11. #include "cmSystemTools.h"
  12. #include "cmCommandArgumentLexer.h"
  13. #include "cmMakefile.h"
  14. int cmCommandArgument_yyparse( yyscan_t yyscanner );
  15. //
  16. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  17. {
  18. this->WarnUninitialized = false;
  19. this->CheckSystemVars = false;
  20. this->FileLine = -1;
  21. this->FileName = 0;
  22. this->RemoveEmpty = true;
  23. this->EmptyVariable[0] = 0;
  24. strcpy(this->DCURLYVariable, "${");
  25. strcpy(this->RCURLYVariable, "}");
  26. strcpy(this->ATVariable, "@");
  27. strcpy(this->DOLLARVariable, "$");
  28. strcpy(this->LCURLYVariable, "{");
  29. strcpy(this->BSLASHVariable, "\\");
  30. this->NoEscapeMode = false;
  31. this->ReplaceAtSyntax = false;
  32. }
  33. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  34. {
  35. this->CleanupParser();
  36. }
  37. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  38. {
  39. this->FileLine = line;
  40. this->FileName = file;
  41. }
  42. char* cmCommandArgumentParserHelper::AddString(const char* str)
  43. {
  44. if ( !str || !*str )
  45. {
  46. return this->EmptyVariable;
  47. }
  48. char* stVal = new char[strlen(str)+1];
  49. strcpy(stVal, str);
  50. this->Variables.push_back(stVal);
  51. return stVal;
  52. }
  53. char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key,
  54. const char* var)
  55. {
  56. if ( !key )
  57. {
  58. return this->ExpandVariable(var);
  59. }
  60. if(!var)
  61. {
  62. return this->EmptyVariable;
  63. }
  64. if ( strcmp(key, "ENV") == 0 )
  65. {
  66. char *ptr = getenv(var);
  67. if (ptr)
  68. {
  69. if (this->EscapeQuotes)
  70. {
  71. return this->AddString(cmSystemTools::EscapeQuotes(ptr).c_str());
  72. }
  73. else
  74. {
  75. return ptr;
  76. }
  77. }
  78. return this->EmptyVariable;
  79. }
  80. if ( strcmp(key, "CACHE") == 0 )
  81. {
  82. if(const char* c = this->Makefile->GetCacheManager()->GetCacheValue(var))
  83. {
  84. if(this->EscapeQuotes)
  85. {
  86. return this->AddString(cmSystemTools::EscapeQuotes(c).c_str());
  87. }
  88. else
  89. {
  90. return this->AddString(c);
  91. }
  92. }
  93. return this->EmptyVariable;
  94. }
  95. cmOStringStream e;
  96. e << "Syntax $" << key << "{} is not supported. "
  97. << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
  98. this->SetError(e.str());
  99. return 0;
  100. }
  101. char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  102. {
  103. if(!var)
  104. {
  105. return 0;
  106. }
  107. if(this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0)
  108. {
  109. cmOStringStream ostr;
  110. ostr << this->FileLine;
  111. return this->AddString(ostr.str().c_str());
  112. }
  113. const char* value = this->Makefile->GetDefinition(var);
  114. if(!value && !this->RemoveEmpty)
  115. {
  116. // check to see if we need to print a warning
  117. // if strict mode is on and the variable has
  118. // not been "cleared"/initialized with a set(foo ) call
  119. if(this->WarnUninitialized && !this->Makefile->VariableInitialized(var))
  120. {
  121. const char* srcRoot = this->Makefile->GetDefinition("CMAKE_SOURCE_DIR");
  122. const char* binRoot = this->Makefile->GetDefinition("CMAKE_BINARY_DIR");
  123. if (this->CheckSystemVars ||
  124. cmSystemTools::IsSubDirectory(this->FileName, this->Makefile->GetHomeDirectory()) ||
  125. cmSystemTools::IsSubDirectory(this->FileName, this->Makefile->GetHomeOutputDirectory()))
  126. {
  127. cmOStringStream msg;
  128. msg << this->FileName << ":" << this->FileLine << ":" <<
  129. " warning: uninitialized variable \'" << var << "\'";
  130. cmSystemTools::Message(msg.str().c_str());
  131. }
  132. }
  133. return 0;
  134. }
  135. if (this->EscapeQuotes && value)
  136. {
  137. return this->AddString(cmSystemTools::EscapeQuotes(value).c_str());
  138. }
  139. return this->AddString(value);
  140. }
  141. char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  142. {
  143. if(this->ReplaceAtSyntax)
  144. {
  145. // try to expand the variable
  146. char* ret = this->ExpandVariable(var);
  147. // if the return was 0 and we want to replace empty strings
  148. // then return an empty string
  149. if(!ret && this->RemoveEmpty)
  150. {
  151. return this->AddString(ret);
  152. }
  153. // if the ret was not 0, then return it
  154. if(ret)
  155. {
  156. return ret;
  157. }
  158. }
  159. // at this point we want to put it back because of one of these cases:
  160. // - this->ReplaceAtSyntax is false
  161. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  162. // and the variable was not defined
  163. std::string ref = "@";
  164. ref += var;
  165. ref += "@";
  166. return this->AddString(ref.c_str());
  167. }
  168. char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
  169. {
  170. if ( !in1 )
  171. {
  172. return in2;
  173. }
  174. else if ( !in2 )
  175. {
  176. return in1;
  177. }
  178. size_t len = strlen(in1) + strlen(in2) + 1;
  179. char* out = new char [ len ];
  180. strcpy(out, in1);
  181. strcat(out, in2);
  182. this->Variables.push_back(out);
  183. return out;
  184. }
  185. void cmCommandArgumentParserHelper::AllocateParserType
  186. (cmCommandArgumentParserHelper::ParserType* pt,const char* str, int len)
  187. {
  188. pt->str = 0;
  189. if ( len == 0 )
  190. {
  191. len = static_cast<int>(strlen(str));
  192. }
  193. if ( len == 0 )
  194. {
  195. return;
  196. }
  197. pt->str = new char[ len + 1 ];
  198. strncpy(pt->str, str, len);
  199. pt->str[len] = 0;
  200. this->Variables.push_back(pt->str);
  201. }
  202. bool cmCommandArgumentParserHelper::HandleEscapeSymbol
  203. (cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  204. {
  205. switch ( symbol )
  206. {
  207. case '\\':
  208. case '"':
  209. case ' ':
  210. case '#':
  211. case '(':
  212. case ')':
  213. case '$':
  214. case '@':
  215. case '^':
  216. this->AllocateParserType(pt, &symbol, 1);
  217. break;
  218. case ';':
  219. this->AllocateParserType(pt, "\\;", 2);
  220. break;
  221. case 't':
  222. this->AllocateParserType(pt, "\t", 1);
  223. break;
  224. case 'n':
  225. this->AllocateParserType(pt, "\n", 1);
  226. break;
  227. case 'r':
  228. this->AllocateParserType(pt, "\r", 1);
  229. break;
  230. case '0':
  231. this->AllocateParserType(pt, "\0", 1);
  232. break;
  233. default:
  234. {
  235. cmOStringStream e;
  236. e << "Invalid escape sequence \\" << symbol;
  237. this->SetError(e.str());
  238. }
  239. return false;
  240. }
  241. return true;
  242. }
  243. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  244. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  245. {
  246. if ( !str)
  247. {
  248. return 0;
  249. }
  250. this->Verbose = verb;
  251. this->InputBuffer = str;
  252. this->InputBufferPos = 0;
  253. this->CurrentLine = 0;
  254. this->Result = "";
  255. yyscan_t yyscanner;
  256. cmCommandArgument_yylex_init(&yyscanner);
  257. cmCommandArgument_yyset_extra(this, yyscanner);
  258. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  259. int res = cmCommandArgument_yyparse(yyscanner);
  260. cmCommandArgument_yylex_destroy(yyscanner);
  261. if ( res != 0 )
  262. {
  263. return 0;
  264. }
  265. this->CleanupParser();
  266. if ( Verbose )
  267. {
  268. std::cerr << "Expanding [" << str << "] produced: ["
  269. << this->Result.c_str() << "]" << std::endl;
  270. }
  271. return 1;
  272. }
  273. void cmCommandArgumentParserHelper::CleanupParser()
  274. {
  275. std::vector<char*>::iterator sit;
  276. for ( sit = this->Variables.begin();
  277. sit != this->Variables.end();
  278. ++ sit )
  279. {
  280. delete [] *sit;
  281. }
  282. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  283. }
  284. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  285. {
  286. if ( maxlen < 1 )
  287. {
  288. return 0;
  289. }
  290. if ( this->InputBufferPos < this->InputBuffer.size() )
  291. {
  292. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  293. if ( buf[0] == '\n' )
  294. {
  295. this->CurrentLine ++;
  296. }
  297. return(1);
  298. }
  299. else
  300. {
  301. buf[0] = '\n';
  302. return( 0 );
  303. }
  304. }
  305. void cmCommandArgumentParserHelper::Error(const char* str)
  306. {
  307. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  308. cmOStringStream ostr;
  309. ostr << str << " (" << pos << ")";
  310. this->SetError(ostr.str());
  311. }
  312. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  313. {
  314. this->Makefile = mf;
  315. this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
  316. this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
  317. }
  318. void cmCommandArgumentParserHelper::SetResult(const char* value)
  319. {
  320. if ( !value )
  321. {
  322. this->Result = "";
  323. return;
  324. }
  325. this->Result = value;
  326. }
  327. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  328. {
  329. // Keep only the first error.
  330. if(this->ErrorString.empty())
  331. {
  332. this->ErrorString = msg;
  333. }
  334. }