cmCommandArgumentParserHelper.cxx 7.9 KB

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