cmCommandArgumentParserHelper.cxx 7.7 KB

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