cmCommandArgumentParserHelper.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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) {
  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(cmSystemTools::EscapeQuotes(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 = "@";
  117. ref += var;
  118. ref += "@";
  119. return this->AddString(ref);
  120. }
  121. const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
  122. const char* in2)
  123. {
  124. if (!in1) {
  125. return in2;
  126. }
  127. if (!in2) {
  128. return in1;
  129. }
  130. size_t len = strlen(in1) + strlen(in2) + 1;
  131. char* out = new char[len];
  132. strcpy(out, in1);
  133. strcat(out, in2);
  134. this->Variables.push_back(out);
  135. return out;
  136. }
  137. void cmCommandArgumentParserHelper::AllocateParserType(
  138. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  139. {
  140. pt->str = nullptr;
  141. if (len == 0) {
  142. len = static_cast<int>(strlen(str));
  143. }
  144. if (len == 0) {
  145. return;
  146. }
  147. char* out = new char[len + 1];
  148. memcpy(out, str, len);
  149. out[len] = 0;
  150. pt->str = out;
  151. this->Variables.push_back(out);
  152. }
  153. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  154. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  155. {
  156. switch (symbol) {
  157. case '\\':
  158. case '"':
  159. case ' ':
  160. case '#':
  161. case '(':
  162. case ')':
  163. case '$':
  164. case '@':
  165. case '^':
  166. this->AllocateParserType(pt, &symbol, 1);
  167. break;
  168. case ';':
  169. this->AllocateParserType(pt, "\\;", 2);
  170. break;
  171. case 't':
  172. this->AllocateParserType(pt, "\t", 1);
  173. break;
  174. case 'n':
  175. this->AllocateParserType(pt, "\n", 1);
  176. break;
  177. case 'r':
  178. this->AllocateParserType(pt, "\r", 1);
  179. break;
  180. case '0':
  181. this->AllocateParserType(pt, "\0", 1);
  182. break;
  183. default: {
  184. std::ostringstream e;
  185. e << "Invalid escape sequence \\" << symbol;
  186. this->SetError(e.str());
  187. }
  188. return false;
  189. }
  190. return true;
  191. }
  192. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  193. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  194. {
  195. if (!str) {
  196. return 0;
  197. }
  198. this->Verbose = verb;
  199. this->InputBuffer = str;
  200. this->InputBufferPos = 0;
  201. this->CurrentLine = 0;
  202. this->Result.clear();
  203. yyscan_t yyscanner;
  204. cmCommandArgument_yylex_init(&yyscanner);
  205. cmCommandArgument_yyset_extra(this, yyscanner);
  206. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  207. int res = cmCommandArgument_yyparse(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. for (char* var : this->Variables) {
  222. delete[] var;
  223. }
  224. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  225. }
  226. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  227. {
  228. if (maxlen < 1) {
  229. return 0;
  230. }
  231. if (this->InputBufferPos < this->InputBuffer.size()) {
  232. buf[0] = this->InputBuffer[this->InputBufferPos++];
  233. if (buf[0] == '\n') {
  234. this->CurrentLine++;
  235. }
  236. return (1);
  237. }
  238. buf[0] = '\n';
  239. return (0);
  240. }
  241. void cmCommandArgumentParserHelper::Error(const char* str)
  242. {
  243. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  244. std::ostringstream ostr;
  245. ostr << str << " (" << pos << ")";
  246. this->SetError(ostr.str());
  247. }
  248. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  249. {
  250. this->Makefile = mf;
  251. }
  252. void cmCommandArgumentParserHelper::SetResult(const char* value)
  253. {
  254. if (!value) {
  255. this->Result.clear();
  256. return;
  257. }
  258. this->Result = value;
  259. }
  260. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  261. {
  262. // Keep only the first error.
  263. if (this->ErrorString.empty()) {
  264. this->ErrorString = msg;
  265. }
  266. }