cmCommandArgumentParserHelper.cxx 8.0 KB

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