cmCommandArgumentParserHelper.cxx 7.3 KB

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