cmCommandArgumentParserHelper.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCommandArgumentParserHelper.h"
  11. #include "cmMakefile.h"
  12. #include "cmOutputConverter.h"
  13. #include "cmState.h"
  14. #include "cmSystemTools.h"
  15. #include "cmCommandArgumentLexer.h"
  16. int cmCommandArgument_yyparse(yyscan_t yyscanner);
  17. //
  18. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  19. {
  20. this->WarnUninitialized = false;
  21. this->CheckSystemVars = false;
  22. this->FileLine = -1;
  23. this->FileName = CM_NULLPTR;
  24. this->RemoveEmpty = true;
  25. this->EmptyVariable[0] = 0;
  26. strcpy(this->DCURLYVariable, "${");
  27. strcpy(this->RCURLYVariable, "}");
  28. strcpy(this->ATVariable, "@");
  29. strcpy(this->DOLLARVariable, "$");
  30. strcpy(this->LCURLYVariable, "{");
  31. strcpy(this->BSLASHVariable, "\\");
  32. this->NoEscapeMode = false;
  33. this->ReplaceAtSyntax = false;
  34. }
  35. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  36. {
  37. this->CleanupParser();
  38. }
  39. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  40. {
  41. this->FileLine = line;
  42. this->FileName = file;
  43. }
  44. char* cmCommandArgumentParserHelper::AddString(const std::string& str)
  45. {
  46. if (str.empty()) {
  47. return this->EmptyVariable;
  48. }
  49. char* stVal = new char[str.size() + 1];
  50. strcpy(stVal, str.c_str());
  51. this->Variables.push_back(stVal);
  52. return stVal;
  53. }
  54. char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key,
  55. const char* var)
  56. {
  57. if (!key) {
  58. return this->ExpandVariable(var);
  59. }
  60. if (!var) {
  61. return this->EmptyVariable;
  62. }
  63. if (strcmp(key, "ENV") == 0) {
  64. std::string str;
  65. if (cmSystemTools::GetEnv(var, str)) {
  66. if (this->EscapeQuotes) {
  67. return this->AddString(cmSystemTools::EscapeQuotes(str.c_str()));
  68. }
  69. return this->AddString(str);
  70. }
  71. return this->EmptyVariable;
  72. }
  73. if (strcmp(key, "CACHE") == 0) {
  74. if (const char* c =
  75. this->Makefile->GetState()->GetInitializedCacheValue(var)) {
  76. if (this->EscapeQuotes) {
  77. return this->AddString(cmSystemTools::EscapeQuotes(c));
  78. }
  79. return this->AddString(c);
  80. }
  81. return this->EmptyVariable;
  82. }
  83. std::ostringstream e;
  84. e << "Syntax $" << key << "{} is not supported. "
  85. << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
  86. this->SetError(e.str());
  87. return CM_NULLPTR;
  88. }
  89. char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  90. {
  91. if (!var) {
  92. return CM_NULLPTR;
  93. }
  94. if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
  95. std::ostringstream ostr;
  96. ostr << this->FileLine;
  97. return this->AddString(ostr.str());
  98. }
  99. const char* value = this->Makefile->GetDefinition(var);
  100. if (!value && !this->RemoveEmpty) {
  101. // check to see if we need to print a warning
  102. // if strict mode is on and the variable has
  103. // not been "cleared"/initialized with a set(foo ) call
  104. if (this->WarnUninitialized && !this->Makefile->VariableInitialized(var)) {
  105. if (this->CheckSystemVars ||
  106. cmSystemTools::IsSubDirectory(this->FileName,
  107. this->Makefile->GetHomeDirectory()) ||
  108. cmSystemTools::IsSubDirectory(
  109. this->FileName, this->Makefile->GetHomeOutputDirectory())) {
  110. std::ostringstream msg;
  111. msg << "uninitialized variable \'" << var << "\'";
  112. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
  113. }
  114. }
  115. return CM_NULLPTR;
  116. }
  117. if (this->EscapeQuotes && value) {
  118. return this->AddString(cmSystemTools::EscapeQuotes(value));
  119. }
  120. return this->AddString(value ? value : "");
  121. }
  122. char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  123. {
  124. if (this->ReplaceAtSyntax) {
  125. // try to expand the variable
  126. char* ret = this->ExpandVariable(var);
  127. // if the return was 0 and we want to replace empty strings
  128. // then return an empty string
  129. if (!ret && this->RemoveEmpty) {
  130. return this->AddString("");
  131. }
  132. // if the ret was not 0, then return it
  133. if (ret) {
  134. return ret;
  135. }
  136. }
  137. // at this point we want to put it back because of one of these cases:
  138. // - this->ReplaceAtSyntax is false
  139. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  140. // and the variable was not defined
  141. std::string ref = "@";
  142. ref += var;
  143. ref += "@";
  144. return this->AddString(ref);
  145. }
  146. char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
  147. {
  148. if (!in1) {
  149. return in2;
  150. }
  151. if (!in2) {
  152. return in1;
  153. }
  154. size_t len = strlen(in1) + strlen(in2) + 1;
  155. char* out = new char[len];
  156. strcpy(out, in1);
  157. strcat(out, in2);
  158. this->Variables.push_back(out);
  159. return out;
  160. }
  161. void cmCommandArgumentParserHelper::AllocateParserType(
  162. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  163. {
  164. pt->str = CM_NULLPTR;
  165. if (len == 0) {
  166. len = static_cast<int>(strlen(str));
  167. }
  168. if (len == 0) {
  169. return;
  170. }
  171. pt->str = new char[len + 1];
  172. strncpy(pt->str, str, len);
  173. pt->str[len] = 0;
  174. this->Variables.push_back(pt->str);
  175. }
  176. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  177. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  178. {
  179. switch (symbol) {
  180. case '\\':
  181. case '"':
  182. case ' ':
  183. case '#':
  184. case '(':
  185. case ')':
  186. case '$':
  187. case '@':
  188. case '^':
  189. this->AllocateParserType(pt, &symbol, 1);
  190. break;
  191. case ';':
  192. this->AllocateParserType(pt, "\\;", 2);
  193. break;
  194. case 't':
  195. this->AllocateParserType(pt, "\t", 1);
  196. break;
  197. case 'n':
  198. this->AllocateParserType(pt, "\n", 1);
  199. break;
  200. case 'r':
  201. this->AllocateParserType(pt, "\r", 1);
  202. break;
  203. case '0':
  204. this->AllocateParserType(pt, "\0", 1);
  205. break;
  206. default: {
  207. std::ostringstream e;
  208. e << "Invalid escape sequence \\" << symbol;
  209. this->SetError(e.str());
  210. }
  211. return false;
  212. }
  213. return true;
  214. }
  215. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  216. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  217. {
  218. if (!str) {
  219. return 0;
  220. }
  221. this->Verbose = verb;
  222. this->InputBuffer = str;
  223. this->InputBufferPos = 0;
  224. this->CurrentLine = 0;
  225. this->Result = "";
  226. yyscan_t yyscanner;
  227. cmCommandArgument_yylex_init(&yyscanner);
  228. cmCommandArgument_yyset_extra(this, yyscanner);
  229. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  230. int res = cmCommandArgument_yyparse(yyscanner);
  231. cmCommandArgument_yylex_destroy(yyscanner);
  232. if (res != 0) {
  233. return 0;
  234. }
  235. this->CleanupParser();
  236. if (Verbose) {
  237. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  238. << std::endl;
  239. }
  240. return 1;
  241. }
  242. void cmCommandArgumentParserHelper::CleanupParser()
  243. {
  244. std::vector<char*>::iterator sit;
  245. for (sit = this->Variables.begin(); sit != this->Variables.end(); ++sit) {
  246. delete[] * sit;
  247. }
  248. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  249. }
  250. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  251. {
  252. if (maxlen < 1) {
  253. return 0;
  254. }
  255. if (this->InputBufferPos < this->InputBuffer.size()) {
  256. buf[0] = this->InputBuffer[this->InputBufferPos++];
  257. if (buf[0] == '\n') {
  258. this->CurrentLine++;
  259. }
  260. return (1);
  261. }
  262. buf[0] = '\n';
  263. return (0);
  264. }
  265. void cmCommandArgumentParserHelper::Error(const char* str)
  266. {
  267. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  268. std::ostringstream ostr;
  269. ostr << str << " (" << pos << ")";
  270. this->SetError(ostr.str());
  271. }
  272. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  273. {
  274. this->Makefile = mf;
  275. this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
  276. this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
  277. }
  278. void cmCommandArgumentParserHelper::SetResult(const char* value)
  279. {
  280. if (!value) {
  281. this->Result = "";
  282. return;
  283. }
  284. this->Result = value;
  285. }
  286. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  287. {
  288. // Keep only the first error.
  289. if (this->ErrorString.empty()) {
  290. this->ErrorString = msg;
  291. }
  292. }