cmCommandArgumentParserHelper.cxx 7.6 KB

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