cmCommandArgumentParserHelper.cxx 7.5 KB

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