cmCommandArgumentParserHelper.cxx 8.9 KB

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