cmCommandArgumentParserHelper.cxx 8.4 KB

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