cmCommandArgumentParserHelper.cxx 8.1 KB

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