cmCommandArgumentParserHelper.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 "cmCommandArgumentLexer.h"
  5. #include "cmMakefile.h"
  6. #include "cmState.h"
  7. #include "cmSystemTools.h"
  8. #include <iostream>
  9. #include <sstream>
  10. #include <string.h>
  11. int cmCommandArgument_yyparse(yyscan_t yyscanner);
  12. //
  13. cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
  14. {
  15. this->FileLine = -1;
  16. this->FileName = nullptr;
  17. this->RemoveEmpty = true;
  18. this->NoEscapeMode = false;
  19. this->ReplaceAtSyntax = false;
  20. }
  21. cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
  22. {
  23. this->CleanupParser();
  24. }
  25. void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
  26. {
  27. this->FileLine = line;
  28. this->FileName = file;
  29. }
  30. const char* cmCommandArgumentParserHelper::AddString(const std::string& str)
  31. {
  32. if (str.empty()) {
  33. return "";
  34. }
  35. char* stVal = new char[str.size() + 1];
  36. strcpy(stVal, str.c_str());
  37. this->Variables.push_back(stVal);
  38. return stVal;
  39. }
  40. const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
  41. const char* key, const char* var)
  42. {
  43. if (!key) {
  44. return this->ExpandVariable(var);
  45. }
  46. if (!var) {
  47. return "";
  48. }
  49. if (strcmp(key, "ENV") == 0) {
  50. std::string str;
  51. if (cmSystemTools::GetEnv(var, str)) {
  52. if (this->EscapeQuotes) {
  53. return this->AddString(cmSystemTools::EscapeQuotes(str));
  54. }
  55. return this->AddString(str);
  56. }
  57. return "";
  58. }
  59. if (strcmp(key, "CACHE") == 0) {
  60. if (const std::string* c =
  61. this->Makefile->GetState()->GetInitializedCacheValue(var)) {
  62. if (this->EscapeQuotes) {
  63. return this->AddString(cmSystemTools::EscapeQuotes(*c));
  64. }
  65. return this->AddString(*c);
  66. }
  67. return "";
  68. }
  69. std::ostringstream e;
  70. e << "Syntax $" << key << "{} is not supported. "
  71. << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
  72. this->SetError(e.str());
  73. return nullptr;
  74. }
  75. const char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
  76. {
  77. if (!var) {
  78. return nullptr;
  79. }
  80. if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
  81. std::ostringstream ostr;
  82. ostr << this->FileLine;
  83. return this->AddString(ostr.str());
  84. }
  85. const char* value = this->Makefile->GetDefinition(var);
  86. if (!value && !this->RemoveEmpty) {
  87. this->Makefile->MaybeWarnUninitialized(var, this->FileName);
  88. return nullptr;
  89. }
  90. if (this->EscapeQuotes && value) {
  91. return this->AddString(cmSystemTools::EscapeQuotes(value));
  92. }
  93. return this->AddString(value ? value : "");
  94. }
  95. const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  96. {
  97. if (this->ReplaceAtSyntax) {
  98. // try to expand the variable
  99. const char* ret = this->ExpandVariable(var);
  100. // if the return was 0 and we want to replace empty strings
  101. // then return an empty string
  102. if (!ret && this->RemoveEmpty) {
  103. return this->AddString("");
  104. }
  105. // if the ret was not 0, then return it
  106. if (ret) {
  107. return ret;
  108. }
  109. }
  110. // at this point we want to put it back because of one of these cases:
  111. // - this->ReplaceAtSyntax is false
  112. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  113. // and the variable was not defined
  114. std::string ref = "@";
  115. ref += var;
  116. ref += "@";
  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. char* out = new char[len];
  130. strcpy(out, in1);
  131. strcat(out, in2);
  132. this->Variables.push_back(out);
  133. return out;
  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. char* out = new char[len + 1];
  146. memcpy(out, str, len);
  147. out[len] = 0;
  148. pt->str = out;
  149. this->Variables.push_back(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. for (char* var : this->Variables) {
  220. delete[] var;
  221. }
  222. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  223. }
  224. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  225. {
  226. if (maxlen < 1) {
  227. return 0;
  228. }
  229. if (this->InputBufferPos < this->InputBuffer.size()) {
  230. buf[0] = this->InputBuffer[this->InputBufferPos++];
  231. if (buf[0] == '\n') {
  232. this->CurrentLine++;
  233. }
  234. return (1);
  235. }
  236. buf[0] = '\n';
  237. return (0);
  238. }
  239. void cmCommandArgumentParserHelper::Error(const char* str)
  240. {
  241. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  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. }