cmCommandArgumentParserHelper.cxx 7.0 KB

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