cmCommandArgumentParserHelper.cxx 8.1 KB

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