cmCommandArgumentParserHelper.cxx 6.9 KB

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