cmCommandArgumentParserHelper.cxx 7.3 KB

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