cmCommandArgumentParserHelper.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCommandArgumentParserHelper.h"
  4. #include "cmCommandArgumentLexer.h"
  5. #include "cmMakefile.h"
  6. #include "cmState.h"
  7. #include "cmStringAlgorithms.h"
  8. #include "cmSystemTools.h"
  9. #include <iostream>
  10. #include <sstream>
  11. #include <string.h>
  12. int cmCommandArgument_yyparse(yyscan_t yyscanner);
  13. //
  14. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  15. {
  16. this->FileLine = -1;
  17. this->FileName = nullptr;
  18. this->RemoveEmpty = true;
  19. this->NoEscapeMode = false;
  20. this->ReplaceAtSyntax = false;
  21. }
  22. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  23. {
  24. this->CleanupParser();
  25. }
  26. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  27. {
  28. this->FileLine = line;
  29. this->FileName = file;
  30. }
  31. const char* cmCommandArgumentParserHelper::AddString(const std::string& str)
  32. {
  33. if (str.empty()) {
  34. return "";
  35. }
  36. char* stVal = new char[str.size() + 1];
  37. strcpy(stVal, str.c_str());
  38. this->Variables.push_back(stVal);
  39. return stVal;
  40. }
  41. const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
  42. const char* key, const char* var)
  43. {
  44. if (!key) {
  45. return this->ExpandVariable(var);
  46. }
  47. if (!var) {
  48. return "";
  49. }
  50. if (strcmp(key, "ENV") == 0) {
  51. std::string str;
  52. if (cmSystemTools::GetEnv(var, str)) {
  53. if (this->EscapeQuotes) {
  54. return this->AddString(cmEscapeQuotes(str));
  55. }
  56. return this->AddString(str);
  57. }
  58. return "";
  59. }
  60. if (strcmp(key, "CACHE") == 0) {
  61. if (const std::string* c =
  62. this->Makefile->GetState()->GetInitializedCacheValue(var)) {
  63. if (this->EscapeQuotes) {
  64. return this->AddString(cmEscapeQuotes(*c));
  65. }
  66. return this->AddString(*c);
  67. }
  68. return "";
  69. }
  70. std::ostringstream e;
  71. e << "Syntax $" << key << "{} is not supported. "
  72. << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
  73. this->SetError(e.str());
  74. return nullptr;
  75. }
  76. const char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  77. {
  78. if (!var) {
  79. return nullptr;
  80. }
  81. if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
  82. return this->AddString(std::to_string(this->FileLine));
  83. }
  84. const char* value = this->Makefile->GetDefinition(var);
  85. if (!value) {
  86. this->Makefile->MaybeWarnUninitialized(var, this->FileName);
  87. if (!this->RemoveEmpty) {
  88. return nullptr;
  89. }
  90. }
  91. if (this->EscapeQuotes && value) {
  92. return this->AddString(cmEscapeQuotes(value));
  93. }
  94. return this->AddString(value ? value : "");
  95. }
  96. const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  97. {
  98. if (this->ReplaceAtSyntax) {
  99. // try to expand the variable
  100. const char* ret = this->ExpandVariable(var);
  101. // if the return was 0 and we want to replace empty strings
  102. // then return an empty string
  103. if (!ret && this->RemoveEmpty) {
  104. return this->AddString("");
  105. }
  106. // if the ret was not 0, then return it
  107. if (ret) {
  108. return ret;
  109. }
  110. }
  111. // at this point we want to put it back because of one of these cases:
  112. // - this->ReplaceAtSyntax is false
  113. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  114. // and the variable was not defined
  115. std::string ref = cmStrCat('@', var, '@');
  116. return this->AddString(ref);
  117. }
  118. const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
  119. const char* in2)
  120. {
  121. if (!in1) {
  122. return in2;
  123. }
  124. if (!in2) {
  125. return in1;
  126. }
  127. size_t len = strlen(in1) + strlen(in2) + 1;
  128. char* out = new char[len];
  129. strcpy(out, in1);
  130. strcat(out, in2);
  131. this->Variables.push_back(out);
  132. return out;
  133. }
  134. void cmCommandArgumentParserHelper::AllocateParserType(
  135. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  136. {
  137. pt->str = nullptr;
  138. if (len == 0) {
  139. len = static_cast<int>(strlen(str));
  140. }
  141. if (len == 0) {
  142. return;
  143. }
  144. char* out = new char[len + 1];
  145. memcpy(out, str, len);
  146. out[len] = 0;
  147. pt->str = out;
  148. this->Variables.push_back(out);
  149. }
  150. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  151. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  152. {
  153. switch (symbol) {
  154. case '\\':
  155. case '"':
  156. case ' ':
  157. case '#':
  158. case '(':
  159. case ')':
  160. case '$':
  161. case '@':
  162. case '^':
  163. this->AllocateParserType(pt, &symbol, 1);
  164. break;
  165. case ';':
  166. this->AllocateParserType(pt, "\\;", 2);
  167. break;
  168. case 't':
  169. this->AllocateParserType(pt, "\t", 1);
  170. break;
  171. case 'n':
  172. this->AllocateParserType(pt, "\n", 1);
  173. break;
  174. case 'r':
  175. this->AllocateParserType(pt, "\r", 1);
  176. break;
  177. case '0':
  178. this->AllocateParserType(pt, "\0", 1);
  179. break;
  180. default: {
  181. std::ostringstream e;
  182. e << "Invalid escape sequence \\" << symbol;
  183. this->SetError(e.str());
  184. }
  185. return false;
  186. }
  187. return true;
  188. }
  189. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  190. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  191. {
  192. if (!str) {
  193. return 0;
  194. }
  195. this->Verbose = verb;
  196. this->InputBuffer = str;
  197. this->InputBufferPos = 0;
  198. this->CurrentLine = 0;
  199. this->Result.clear();
  200. yyscan_t yyscanner;
  201. cmCommandArgument_yylex_init(&yyscanner);
  202. cmCommandArgument_yyset_extra(this, yyscanner);
  203. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  204. int res = cmCommandArgument_yyparse(yyscanner);
  205. cmCommandArgument_yylex_destroy(yyscanner);
  206. if (res != 0) {
  207. return 0;
  208. }
  209. this->CleanupParser();
  210. if (Verbose) {
  211. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  212. << std::endl;
  213. }
  214. return 1;
  215. }
  216. void cmCommandArgumentParserHelper::CleanupParser()
  217. {
  218. for (char* var : this->Variables) {
  219. delete[] var;
  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. return 0;
  227. }
  228. if (this->InputBufferPos < this->InputBuffer.size()) {
  229. buf[0] = this->InputBuffer[this->InputBufferPos++];
  230. if (buf[0] == '\n') {
  231. this->CurrentLine++;
  232. }
  233. return (1);
  234. }
  235. buf[0] = '\n';
  236. return (0);
  237. }
  238. void cmCommandArgumentParserHelper::Error(const char* str)
  239. {
  240. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  241. std::ostringstream ostr;
  242. ostr << str << " (" << pos << ")";
  243. this->SetError(ostr.str());
  244. }
  245. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  246. {
  247. this->Makefile = mf;
  248. }
  249. void cmCommandArgumentParserHelper::SetResult(const char* value)
  250. {
  251. if (!value) {
  252. this->Result.clear();
  253. return;
  254. }
  255. this->Result = value;
  256. }
  257. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  258. {
  259. // Keep only the first error.
  260. if (this->ErrorString.empty()) {
  261. this->ErrorString = msg;
  262. }
  263. }