cmCommandArgumentParserHelper.cxx 6.4 KB

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