cmCommandArgumentParserHelper.cxx 7.7 KB

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