cmCommandArgumentParserHelper.cxx 8.9 KB

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