cmCommandArgumentParserHelper.cxx 7.0 KB

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