cmCommandArgumentParserHelper.cxx 8.7 KB

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