cmCommandArgumentParserHelper.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. cmListFileContext const& top = this->Makefile->GetBacktrace().Top();
  90. if (top.DeferId) {
  91. line = cmStrCat("DEFERRED:"_s, *top.DeferId);
  92. } else {
  93. line = std::to_string(this->FileLine);
  94. }
  95. return this->AddString(line);
  96. }
  97. cmValue value = this->Makefile->GetDefinition(var);
  98. if (!value) {
  99. this->Makefile->MaybeWarnUninitialized(var, this->FileName);
  100. if (!this->RemoveEmpty) {
  101. return nullptr;
  102. }
  103. }
  104. if (this->EscapeQuotes && value) {
  105. return this->AddString(cmEscapeQuotes(*value));
  106. }
  107. return this->AddString(value);
  108. }
  109. const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  110. {
  111. if (this->ReplaceAtSyntax) {
  112. // try to expand the variable
  113. const char* ret = this->ExpandVariable(var);
  114. // if the return was 0 and we want to replace empty strings
  115. // then return an empty string
  116. if (!ret && this->RemoveEmpty) {
  117. return this->AddString("");
  118. }
  119. // if the ret was not 0, then return it
  120. if (ret) {
  121. return ret;
  122. }
  123. }
  124. // at this point we want to put it back because of one of these cases:
  125. // - this->ReplaceAtSyntax is false
  126. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  127. // and the variable was not defined
  128. std::string ref = cmStrCat('@', var, '@');
  129. return this->AddString(ref);
  130. }
  131. const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
  132. const char* in2)
  133. {
  134. if (!in1) {
  135. return in2;
  136. }
  137. if (!in2) {
  138. return in1;
  139. }
  140. size_t len = strlen(in1) + strlen(in2) + 1;
  141. auto out = cm::make_unique<char[]>(len);
  142. strcpy(out.get(), in1);
  143. strcat(out.get(), in2);
  144. this->Variables.push_back(std::move(out));
  145. return this->Variables.back().get();
  146. }
  147. void cmCommandArgumentParserHelper::AllocateParserType(
  148. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  149. {
  150. pt->str = nullptr;
  151. if (len == 0) {
  152. len = static_cast<int>(strlen(str));
  153. }
  154. if (len == 0) {
  155. return;
  156. }
  157. auto out = cm::make_unique<char[]>(len + 1);
  158. memcpy(out.get(), str, len);
  159. out.get()[len] = 0;
  160. pt->str = out.get();
  161. this->Variables.push_back(std::move(out));
  162. }
  163. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  164. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  165. {
  166. switch (symbol) {
  167. case '\\':
  168. case '"':
  169. case ' ':
  170. case '#':
  171. case '(':
  172. case ')':
  173. case '$':
  174. case '@':
  175. case '^':
  176. this->AllocateParserType(pt, &symbol, 1);
  177. break;
  178. case ';':
  179. this->AllocateParserType(pt, "\\;", 2);
  180. break;
  181. case 't':
  182. this->AllocateParserType(pt, "\t", 1);
  183. break;
  184. case 'n':
  185. this->AllocateParserType(pt, "\n", 1);
  186. break;
  187. case 'r':
  188. this->AllocateParserType(pt, "\r", 1);
  189. break;
  190. case '0':
  191. this->AllocateParserType(pt, "\0", 1);
  192. break;
  193. default: {
  194. std::ostringstream e;
  195. e << "Invalid escape sequence \\" << symbol;
  196. this->SetError(e.str());
  197. }
  198. return false;
  199. }
  200. return true;
  201. }
  202. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  203. int cmCommandArgumentParserHelper::ParseString(std::string const& str,
  204. int verb)
  205. {
  206. if (str.empty()) {
  207. return 0;
  208. }
  209. this->InputSize = str.size();
  210. this->Verbose = verb;
  211. this->Result.clear();
  212. yyscan_t yyscanner;
  213. cmCommandArgument_yylex_init(&yyscanner);
  214. auto* scanBuf = cmCommandArgument_yy_scan_string(str.c_str(), yyscanner);
  215. cmCommandArgument_yyset_extra(this, yyscanner);
  216. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  217. int res = cmCommandArgument_yyparse(yyscanner);
  218. cmCommandArgument_yy_delete_buffer(scanBuf, yyscanner);
  219. cmCommandArgument_yylex_destroy(yyscanner);
  220. if (res != 0) {
  221. return 0;
  222. }
  223. this->CleanupParser();
  224. if (this->Verbose) {
  225. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  226. << std::endl;
  227. }
  228. return 1;
  229. }
  230. void cmCommandArgumentParserHelper::CleanupParser()
  231. {
  232. this->Variables.clear();
  233. }
  234. void cmCommandArgumentParserHelper::Error(const char* str)
  235. {
  236. auto pos = this->InputBufferPos;
  237. auto const isEof = (this->InputSize < this->InputBufferPos);
  238. if (!isEof) {
  239. pos -= this->LastTokenLength;
  240. }
  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. }
  264. void cmCommandArgumentParserHelper::UpdateInputPosition(int const tokenLength)
  265. {
  266. this->InputBufferPos += tokenLength;
  267. this->LastTokenLength = tokenLength;
  268. }