cmCommandArgumentParserHelper.cxx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = (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. this->AllocateParserType(pt, &symbol, 1);
  159. break;
  160. case ';':
  161. this->AllocateParserType(pt, "\\;", 2);
  162. break;
  163. case 't':
  164. this->AllocateParserType(pt, "\t", 1);
  165. break;
  166. case 'n':
  167. this->AllocateParserType(pt, "\n", 1);
  168. break;
  169. case 'r':
  170. this->AllocateParserType(pt, "\r", 1);
  171. break;
  172. case '0':
  173. this->AllocateParserType(pt, "\0", 1);
  174. break;
  175. default:
  176. char buffer[2];
  177. buffer[0] = symbol;
  178. buffer[1] = 0;
  179. cmSystemTools::Error("Invalid escape sequence \\", buffer);
  180. return false;
  181. }
  182. return true;
  183. }
  184. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  185. {
  186. if ( !str)
  187. {
  188. return 0;
  189. }
  190. this->Verbose = verb;
  191. this->InputBuffer = str;
  192. this->InputBufferPos = 0;
  193. this->CurrentLine = 0;
  194. this->Result = "";
  195. yyscan_t yyscanner;
  196. cmCommandArgument_yylex_init(&yyscanner);
  197. cmCommandArgument_yyset_extra(this, yyscanner);
  198. int res = cmCommandArgument_yyparse(yyscanner);
  199. cmCommandArgument_yylex_destroy(yyscanner);
  200. if ( res != 0 )
  201. {
  202. return 0;
  203. }
  204. this->CleanupParser();
  205. if ( Verbose )
  206. {
  207. std::cerr << "Expanding [" << str << "] produced: ["
  208. << this->Result.c_str() << "]" << std::endl;
  209. }
  210. return 1;
  211. }
  212. void cmCommandArgumentParserHelper::CleanupParser()
  213. {
  214. std::vector<char*>::iterator sit;
  215. for ( sit = this->Variables.begin();
  216. sit != this->Variables.end();
  217. ++ sit )
  218. {
  219. delete [] *sit;
  220. }
  221. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  222. }
  223. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  224. {
  225. if ( maxlen < 1 )
  226. {
  227. return 0;
  228. }
  229. if ( this->InputBufferPos < this->InputBuffer.size() )
  230. {
  231. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  232. if ( buf[0] == '\n' )
  233. {
  234. this->CurrentLine ++;
  235. }
  236. return(1);
  237. }
  238. else
  239. {
  240. buf[0] = '\n';
  241. return( 0 );
  242. }
  243. }
  244. void cmCommandArgumentParserHelper::Error(const char* str)
  245. {
  246. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  247. cmOStringStream ostr;
  248. ostr << str << " (" << pos << ")";
  249. this->ErrorString = ostr.str();
  250. }
  251. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  252. {
  253. this->Makefile = mf;
  254. }
  255. void cmCommandArgumentParserHelper::SetResult(const char* value)
  256. {
  257. if ( !value )
  258. {
  259. this->Result = "";
  260. return;
  261. }
  262. this->Result = value;
  263. }