cmCommandArgumentParserHelper.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "cmake.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->WarnUninitialized = false;
  17. this->CheckSystemVars = false;
  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. char* stVal = new char[str.size() + 1];
  39. strcpy(stVal, str.c_str());
  40. this->Variables.push_back(stVal);
  41. return stVal;
  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(cmSystemTools::EscapeQuotes(str));
  57. }
  58. return this->AddString(str);
  59. }
  60. return "";
  61. }
  62. if (strcmp(key, "CACHE") == 0) {
  63. if (const char* c =
  64. this->Makefile->GetState()->GetInitializedCacheValue(var)) {
  65. if (this->EscapeQuotes) {
  66. return this->AddString(cmSystemTools::EscapeQuotes(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. std::ostringstream ostr;
  85. ostr << this->FileLine;
  86. return this->AddString(ostr.str());
  87. }
  88. const char* value = this->Makefile->GetDefinition(var);
  89. if (!value && !this->RemoveEmpty) {
  90. // check to see if we need to print a warning
  91. // if strict mode is on and the variable has
  92. // not been "cleared"/initialized with a set(foo ) call
  93. if (this->WarnUninitialized && !this->Makefile->VariableInitialized(var)) {
  94. if (this->CheckSystemVars ||
  95. (this->FileName &&
  96. (cmSystemTools::IsSubDirectory(
  97. this->FileName, this->Makefile->GetHomeDirectory()) ||
  98. cmSystemTools::IsSubDirectory(
  99. this->FileName, this->Makefile->GetHomeOutputDirectory())))) {
  100. std::ostringstream msg;
  101. msg << "uninitialized variable \'" << var << "\'";
  102. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
  103. }
  104. }
  105. return nullptr;
  106. }
  107. if (this->EscapeQuotes && value) {
  108. return this->AddString(cmSystemTools::EscapeQuotes(value));
  109. }
  110. return this->AddString(value ? value : "");
  111. }
  112. const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
  113. {
  114. if (this->ReplaceAtSyntax) {
  115. // try to expand the variable
  116. const char* ret = this->ExpandVariable(var);
  117. // if the return was 0 and we want to replace empty strings
  118. // then return an empty string
  119. if (!ret && this->RemoveEmpty) {
  120. return this->AddString("");
  121. }
  122. // if the ret was not 0, then return it
  123. if (ret) {
  124. return ret;
  125. }
  126. }
  127. // at this point we want to put it back because of one of these cases:
  128. // - this->ReplaceAtSyntax is false
  129. // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
  130. // and the variable was not defined
  131. std::string ref = "@";
  132. ref += var;
  133. ref += "@";
  134. return this->AddString(ref);
  135. }
  136. const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
  137. const char* in2)
  138. {
  139. if (!in1) {
  140. return in2;
  141. }
  142. if (!in2) {
  143. return in1;
  144. }
  145. size_t len = strlen(in1) + strlen(in2) + 1;
  146. char* out = new char[len];
  147. strcpy(out, in1);
  148. strcat(out, in2);
  149. this->Variables.push_back(out);
  150. return out;
  151. }
  152. void cmCommandArgumentParserHelper::AllocateParserType(
  153. cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
  154. {
  155. pt->str = nullptr;
  156. if (len == 0) {
  157. len = static_cast<int>(strlen(str));
  158. }
  159. if (len == 0) {
  160. return;
  161. }
  162. char* out = new char[len + 1];
  163. strncpy(out, str, len);
  164. out[len] = 0;
  165. pt->str = out;
  166. this->Variables.push_back(out);
  167. }
  168. bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
  169. cmCommandArgumentParserHelper::ParserType* pt, char symbol)
  170. {
  171. switch (symbol) {
  172. case '\\':
  173. case '"':
  174. case ' ':
  175. case '#':
  176. case '(':
  177. case ')':
  178. case '$':
  179. case '@':
  180. case '^':
  181. this->AllocateParserType(pt, &symbol, 1);
  182. break;
  183. case ';':
  184. this->AllocateParserType(pt, "\\;", 2);
  185. break;
  186. case 't':
  187. this->AllocateParserType(pt, "\t", 1);
  188. break;
  189. case 'n':
  190. this->AllocateParserType(pt, "\n", 1);
  191. break;
  192. case 'r':
  193. this->AllocateParserType(pt, "\r", 1);
  194. break;
  195. case '0':
  196. this->AllocateParserType(pt, "\0", 1);
  197. break;
  198. default: {
  199. std::ostringstream e;
  200. e << "Invalid escape sequence \\" << symbol;
  201. this->SetError(e.str());
  202. }
  203. return false;
  204. }
  205. return true;
  206. }
  207. void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
  208. int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
  209. {
  210. if (!str) {
  211. return 0;
  212. }
  213. this->Verbose = verb;
  214. this->InputBuffer = str;
  215. this->InputBufferPos = 0;
  216. this->CurrentLine = 0;
  217. this->Result.clear();
  218. yyscan_t yyscanner;
  219. cmCommandArgument_yylex_init(&yyscanner);
  220. cmCommandArgument_yyset_extra(this, yyscanner);
  221. cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
  222. int res = cmCommandArgument_yyparse(yyscanner);
  223. cmCommandArgument_yylex_destroy(yyscanner);
  224. if (res != 0) {
  225. return 0;
  226. }
  227. this->CleanupParser();
  228. if (Verbose) {
  229. std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
  230. << std::endl;
  231. }
  232. return 1;
  233. }
  234. void cmCommandArgumentParserHelper::CleanupParser()
  235. {
  236. std::vector<char*>::iterator sit;
  237. for (char* var : this->Variables) {
  238. delete[] var;
  239. }
  240. this->Variables.erase(this->Variables.begin(), this->Variables.end());
  241. }
  242. int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
  243. {
  244. if (maxlen < 1) {
  245. return 0;
  246. }
  247. if (this->InputBufferPos < this->InputBuffer.size()) {
  248. buf[0] = this->InputBuffer[this->InputBufferPos++];
  249. if (buf[0] == '\n') {
  250. this->CurrentLine++;
  251. }
  252. return (1);
  253. }
  254. buf[0] = '\n';
  255. return (0);
  256. }
  257. void cmCommandArgumentParserHelper::Error(const char* str)
  258. {
  259. unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
  260. std::ostringstream ostr;
  261. ostr << str << " (" << pos << ")";
  262. this->SetError(ostr.str());
  263. }
  264. void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
  265. {
  266. this->Makefile = mf;
  267. this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
  268. this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
  269. }
  270. void cmCommandArgumentParserHelper::SetResult(const char* value)
  271. {
  272. if (!value) {
  273. this->Result.clear();
  274. return;
  275. }
  276. this->Result = value;
  277. }
  278. void cmCommandArgumentParserHelper::SetError(std::string const& msg)
  279. {
  280. // Keep only the first error.
  281. if (this->ErrorString.empty()) {
  282. this->ErrorString = msg;
  283. }
  284. }