cmCommandArgumentParserHelper.cxx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 "cmMakefile.h"
  13. #include "cmCommandArgumentLexer.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 std::string& str)
  43. {
  44. if ( str.empty() )
  45. {
  46. return this->EmptyVariable;
  47. }
  48. char* stVal = new char[str.size()+1];
  49. strcpy(stVal, str.c_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));
  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));
  87. }
  88. else
  89. {
  90. return this->AddString(c);
  91. }
  92. }
  93. return this->EmptyVariable;
  94. }
  95. std::ostringstream 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. std::ostringstream ostr;
  110. ostr << this->FileLine;
  111. return this->AddString(ostr.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. if (this->CheckSystemVars ||
  122. cmSystemTools::IsSubDirectory(this->FileName,
  123. this->Makefile->GetHomeDirectory()) ||
  124. cmSystemTools::IsSubDirectory(this->FileName,
  125. this->Makefile->GetHomeOutputDirectory()))
  126. {
  127. std::ostringstream msg;
  128. cmListFileBacktrace bt(this->Makefile->GetLocalGenerator());
  129. cmListFileContext lfc;
  130. lfc.FilePath = this->FileName;
  131. lfc.Line = this->FileLine;
  132. bt.push_back(lfc);
  133. msg << "uninitialized variable \'" << var << "\'";
  134. this->Makefile->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
  135. msg.str(), bt);
  136. }
  137. }
  138. return 0;
  139. }
  140. if (this->EscapeQuotes && value)
  141. {
  142. return this->AddString(cmSystemTools::EscapeQuotes(value));
  143. }
  144. return this->AddString(value ? value : "");
  145. }
  146. char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  147. {
  148. if(this->ReplaceAtSyntax)
  149. {
  150. // try to expand the variable
  151. char* ret = this->ExpandVariable(var);
  152. // if the return was 0 and we want to replace empty strings
  153. // then return an empty string
  154. if(!ret && this->RemoveEmpty)
  155. {
  156. return this->AddString("");
  157. }
  158. // if the ret was not 0, then return it
  159. if(ret)
  160. {
  161. return ret;
  162. }
  163. }
  164. // at this point we want to put it back because of one of these cases:
  165. // - this->ReplaceAtSyntax is false
  166. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  167. // and the variable was not defined
  168. std::string ref = "@";
  169. ref += var;
  170. ref += "@";
  171. return this->AddString(ref);
  172. }
  173. char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
  174. {
  175. if ( !in1 )
  176. {
  177. return in2;
  178. }
  179. else if ( !in2 )
  180. {
  181. return in1;
  182. }
  183. size_t len = strlen(in1) + strlen(in2) + 1;
  184. char* out = new char [ len ];
  185. strcpy(out, in1);
  186. strcat(out, in2);
  187. this->Variables.push_back(out);
  188. return out;
  189. }
  190. void cmCommandArgumentParserHelper::AllocateParserType
  191. (cmCommandArgumentParserHelper::ParserType* pt,const char* str, int len)
  192. {
  193. pt->str = 0;
  194. if ( len == 0 )
  195. {
  196. len = static_cast<int>(strlen(str));
  197. }
  198. if ( len == 0 )
  199. {
  200. return;
  201. }
  202. pt->str = new char[ len + 1 ];
  203. strncpy(pt->str, str, len);
  204. pt->str[len] = 0;
  205. this->Variables.push_back(pt->str);
  206. }
  207. bool cmCommandArgumentParserHelper::HandleEscapeSymbol
  208. (cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  209. {
  210. switch ( symbol )
  211. {
  212. case '\\':
  213. case '"':
  214. case ' ':
  215. case '#':
  216. case '(':
  217. case ')':
  218. case '$':
  219. case '@':
  220. case '^':
  221. this->AllocateParserType(pt, &symbol, 1);
  222. break;
  223. case ';':
  224. this->AllocateParserType(pt, "\\;", 2);
  225. break;
  226. case 't':
  227. this->AllocateParserType(pt, "\t", 1);
  228. break;
  229. case 'n':
  230. this->AllocateParserType(pt, "\n", 1);
  231. break;
  232. case 'r':
  233. this->AllocateParserType(pt, "\r", 1);
  234. break;
  235. case '0':
  236. this->AllocateParserType(pt, "\0", 1);
  237. break;
  238. default:
  239. {
  240. std::ostringstream e;
  241. e << "Invalid escape sequence \\" << symbol;
  242. this->SetError(e.str());
  243. }
  244. return false;
  245. }
  246. return true;
  247. }
  248. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  249. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  250. {
  251. if ( !str)
  252. {
  253. return 0;
  254. }
  255. this->Verbose = verb;
  256. this->InputBuffer = str;
  257. this->InputBufferPos = 0;
  258. this->CurrentLine = 0;
  259. this->Result = "";
  260. yyscan_t yyscanner;
  261. cmCommandArgument_yylex_init(&yyscanner);
  262. cmCommandArgument_yyset_extra(this, yyscanner);
  263. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  264. int res = cmCommandArgument_yyparse(yyscanner);
  265. cmCommandArgument_yylex_destroy(yyscanner);
  266. if ( res != 0 )
  267. {
  268. return 0;
  269. }
  270. this->CleanupParser();
  271. if ( Verbose )
  272. {
  273. std::cerr << "Expanding [" << str << "] produced: ["
  274. << this->Result << "]" << std::endl;
  275. }
  276. return 1;
  277. }
  278. void cmCommandArgumentParserHelper::CleanupParser()
  279. {
  280. std::vector<char*>::iterator sit;
  281. for ( sit = this->Variables.begin();
  282. sit != this->Variables.end();
  283. ++ sit )
  284. {
  285. delete [] *sit;
  286. }
  287. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  288. }
  289. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  290. {
  291. if ( maxlen < 1 )
  292. {
  293. return 0;
  294. }
  295. if ( this->InputBufferPos < this->InputBuffer.size() )
  296. {
  297. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  298. if ( buf[0] == '\n' )
  299. {
  300. this->CurrentLine ++;
  301. }
  302. return(1);
  303. }
  304. else
  305. {
  306. buf[0] = '\n';
  307. return( 0 );
  308. }
  309. }
  310. void cmCommandArgumentParserHelper::Error(const char* str)
  311. {
  312. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  313. std::ostringstream ostr;
  314. ostr << str << " (" << pos << ")";
  315. this->SetError(ostr.str());
  316. }
  317. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  318. {
  319. this->Makefile = mf;
  320. this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
  321. this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
  322. }
  323. void cmCommandArgumentParserHelper::SetResult(const char* value)
  324. {
  325. if ( !value )
  326. {
  327. this->Result = "";
  328. return;
  329. }
  330. this->Result = value;
  331. }
  332. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  333. {
  334. // Keep only the first error.
  335. if(this->ErrorString.empty())
  336. {
  337. this->ErrorString = msg;
  338. }
  339. }