cmCommandArgumentParserHelper.cxx 6.9 KB

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