cmCommandArgumentParserHelper.cxx 7.3 KB

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