cmCommandArgumentParserHelper.cxx 6.7 KB

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