cmCommandArgumentParserHelper.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  19. {
  20. m_FileLine = -1;
  21. m_FileName = 0;
  22. m_EmptyVariable[0] = 0;
  23. strcpy(m_DCURLYVariable, "${");
  24. strcpy(m_RCURLYVariable, "}");
  25. strcpy(m_ATVariable, "@");
  26. strcpy(m_DOLLARVariable, "$");
  27. strcpy(m_LCURLYVariable, "{");
  28. strcpy(m_BSLASHVariable, "\\");
  29. }
  30. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  31. {
  32. this->CleanupParser();
  33. }
  34. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  35. {
  36. m_FileLine = line;
  37. m_FileName = file;
  38. }
  39. char* cmCommandArgumentParserHelper::AddString(const char* str)
  40. {
  41. if ( !str || !*str )
  42. {
  43. return m_EmptyVariable;
  44. }
  45. char* stVal = new char[strlen(str)+1];
  46. strcpy(stVal, str);
  47. m_Variables.push_back(stVal);
  48. return stVal;
  49. }
  50. char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key, const char* var)
  51. {
  52. if ( !key )
  53. {
  54. return this->ExpandVariable(var);
  55. }
  56. if ( strcmp(key, "ENV") == 0 )
  57. {
  58. char *ptr = getenv(var);
  59. if (ptr)
  60. {
  61. if (m_EscapeQuotes)
  62. {
  63. return this->AddString(cmSystemTools::EscapeQuotes(ptr).c_str());
  64. }
  65. else
  66. {
  67. return ptr;
  68. }
  69. }
  70. return m_EmptyVariable;
  71. }
  72. cmSystemTools::Error("Key ", key, " is not used yet. For now only $ENV{..} is allowed");
  73. return 0;
  74. }
  75. char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  76. {
  77. if(m_FileName && strcmp(var, "CMAKE_CURRENT_LIST_FILE") == 0)
  78. {
  79. return this->AddString(m_FileName);
  80. }
  81. else if(m_FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0)
  82. {
  83. cmOStringStream ostr;
  84. ostr << m_FileLine;
  85. return this->AddString(ostr.str().c_str());
  86. }
  87. const char* value = m_Makefile->GetDefinition(var);
  88. if (m_EscapeQuotes && value)
  89. {
  90. return this->AddString(cmSystemTools::EscapeQuotes(value).c_str());
  91. }
  92. return this->AddString(value);
  93. }
  94. char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
  95. {
  96. if ( !in1 )
  97. {
  98. return in2;
  99. }
  100. else if ( !in2 )
  101. {
  102. return in1;
  103. }
  104. int len = strlen(in1) + strlen(in2) + 1;
  105. char* out = new char [ len ];
  106. strcpy(out, in1);
  107. strcat(out, in2);
  108. m_Variables.push_back(out);
  109. return out;
  110. }
  111. void cmCommandArgumentParserHelper::AllocateParserType(cmCommandArgumentParserHelper::ParserType* pt,
  112. const char* str, int len)
  113. {
  114. pt->str = 0;
  115. if ( len == 0 )
  116. {
  117. len = strlen(str);
  118. }
  119. if ( len == 0 )
  120. {
  121. return;
  122. }
  123. this->UnionsAvailable ++;
  124. pt->str = new char[ len + 1 ];
  125. strncpy(pt->str, str, len);
  126. pt->str[len] = 0;
  127. m_Variables.push_back(pt->str);
  128. // std::cout << (void*) pt->str << " " << pt->str << " JPAllocateParserType" << std::endl;
  129. }
  130. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  131. {
  132. if ( !str)
  133. {
  134. return 0;
  135. }
  136. //printf("Do some parsing: %s\n", str);
  137. this->Verbose = verb;
  138. this->InputBuffer = str;
  139. this->InputBufferPos = 0;
  140. this->CurrentLine = 0;
  141. m_Result = "";
  142. yyscan_t yyscanner;
  143. cmCommandArgument_yylex_init(&yyscanner);
  144. cmCommandArgument_yyset_extra(this, yyscanner);
  145. int res = cmCommandArgument_yyparse(yyscanner);
  146. cmCommandArgument_yylex_destroy(yyscanner);
  147. if ( res != 0 )
  148. {
  149. //str << "CAL_Parser returned: " << res << std::endl;
  150. //std::cerr << "When parsing: [" << str << "]" << std::endl;
  151. return 0;
  152. }
  153. this->CleanupParser();
  154. if ( Verbose )
  155. {
  156. std::cerr << "Expanding [" << str << "] produced: [" << m_Result.c_str() << "]" << std::endl;
  157. }
  158. return 1;
  159. }
  160. void cmCommandArgumentParserHelper::CleanupParser()
  161. {
  162. std::vector<char*>::iterator sit;
  163. for ( sit = m_Variables.begin();
  164. sit != m_Variables.end();
  165. ++ sit )
  166. {
  167. delete [] *sit;
  168. }
  169. m_Variables.erase(m_Variables.begin(), m_Variables.end());
  170. }
  171. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  172. {
  173. //std::cout << "JPLexInput ";
  174. //std::cout.write(buf, maxlen);
  175. //std::cout << std::endl;
  176. if ( maxlen < 1 )
  177. {
  178. return 0;
  179. }
  180. if ( this->InputBufferPos < this->InputBuffer.size() )
  181. {
  182. buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
  183. if ( buf[0] == '\n' )
  184. {
  185. this->CurrentLine ++;
  186. }
  187. return(1);
  188. }
  189. else
  190. {
  191. buf[0] = '\n';
  192. return( 0 );
  193. }
  194. }
  195. void cmCommandArgumentParserHelper::Error(const char* str)
  196. {
  197. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  198. //fprintf(stderr, "Argument Parser Error: %s (%lu / Line: %d)\n", str, pos, this->CurrentLine);
  199. cmOStringStream ostr;
  200. ostr << str << " (" << pos << ")";
  201. /*
  202. int cc;
  203. std::cerr << "String: [";
  204. for ( cc = 0; cc < 30 && *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  205. cc ++ )
  206. {
  207. std::cerr << *(this->InputBuffer.c_str() + this->InputBufferPos + cc);
  208. }
  209. std::cerr << "]" << std::endl;
  210. */
  211. m_Error = ostr.str();
  212. }
  213. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  214. {
  215. m_Makefile = mf;
  216. }
  217. void cmCommandArgumentParserHelper::SetResult(const char* value)
  218. {
  219. if ( !value )
  220. {
  221. m_Result = "";
  222. return;
  223. }
  224. m_Result = value;
  225. }