2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2005-06-08 14:41:05 +00:00
|
|
|
#include "cmCommandArgumentParserHelper.h"
|
|
|
|
|
2016-09-01 18:59:28 +00:00
|
|
|
#include <cm_kwiml.h>
|
|
|
|
|
|
|
|
#include "cmCommandArgumentLexer.h"
|
2005-06-08 14:41:05 +00:00
|
|
|
#include "cmMakefile.h"
|
2016-05-02 19:42:32 +00:00
|
|
|
#include "cmState.h"
|
|
|
|
#include "cmSystemTools.h"
|
2016-09-01 18:59:28 +00:00
|
|
|
#include "cmake.h"
|
2005-06-08 14:41:05 +00:00
|
|
|
|
2016-09-01 18:59:28 +00:00
|
|
|
#include <cmConfigure.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string.h>
|
2013-08-07 14:14:58 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
int cmCommandArgument_yyparse(yyscan_t yyscanner);
|
2005-06-17 19:50:08 +00:00
|
|
|
//
|
2005-06-08 14:41:05 +00:00
|
|
|
cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
|
|
|
|
{
|
2010-08-25 16:35:40 +00:00
|
|
|
this->WarnUninitialized = false;
|
2010-09-01 15:24:20 +00:00
|
|
|
this->CheckSystemVars = false;
|
2006-03-15 16:02:08 +00:00
|
|
|
this->FileLine = -1;
|
2016-06-27 20:44:16 +00:00
|
|
|
this->FileName = CM_NULLPTR;
|
2007-02-09 18:44:37 +00:00
|
|
|
this->RemoveEmpty = true;
|
2006-03-15 16:02:08 +00:00
|
|
|
this->EmptyVariable[0] = 0;
|
|
|
|
strcpy(this->DCURLYVariable, "${");
|
|
|
|
strcpy(this->RCURLYVariable, "}");
|
2016-05-16 14:34:04 +00:00
|
|
|
strcpy(this->ATVariable, "@");
|
2006-03-15 16:02:08 +00:00
|
|
|
strcpy(this->DOLLARVariable, "$");
|
|
|
|
strcpy(this->LCURLYVariable, "{");
|
|
|
|
strcpy(this->BSLASHVariable, "\\");
|
2005-06-17 19:50:08 +00:00
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
this->NoEscapeMode = false;
|
2006-10-04 18:37:42 +00:00
|
|
|
this->ReplaceAtSyntax = false;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
|
|
|
|
{
|
|
|
|
this->CleanupParser();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->FileLine = line;
|
|
|
|
this->FileName = file;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 02:16:12 +00:00
|
|
|
char* cmCommandArgumentParserHelper::AddString(const std::string& str)
|
2005-06-08 14:41:05 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (str.empty()) {
|
2006-03-15 16:02:08 +00:00
|
|
|
return this->EmptyVariable;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
char* stVal = new char[str.size() + 1];
|
2014-02-04 02:16:12 +00:00
|
|
|
strcpy(stVal, str.c_str());
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Variables.push_back(stVal);
|
2005-06-14 19:49:30 +00:00
|
|
|
return stVal;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 17:42:58 +00:00
|
|
|
char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key,
|
2006-05-10 18:15:15 +00:00
|
|
|
const char* var)
|
2005-06-08 14:41:05 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!key) {
|
2005-06-08 14:41:05 +00:00
|
|
|
return this->ExpandVariable(var);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (!var) {
|
2009-10-21 13:04:50 +00:00
|
|
|
return this->EmptyVariable;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (strcmp(key, "ENV") == 0) {
|
2016-07-07 21:54:05 +00:00
|
|
|
std::string str;
|
|
|
|
if (cmSystemTools::GetEnv(var, str)) {
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->EscapeQuotes) {
|
2016-09-04 14:55:05 +00:00
|
|
|
return this->AddString(cmSystemTools::EscapeQuotes(str));
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
2016-08-18 18:36:29 +00:00
|
|
|
return this->AddString(str);
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
return this->EmptyVariable;
|
|
|
|
}
|
|
|
|
if (strcmp(key, "CACHE") == 0) {
|
|
|
|
if (const char* c =
|
|
|
|
this->Makefile->GetState()->GetInitializedCacheValue(var)) {
|
|
|
|
if (this->EscapeQuotes) {
|
2014-03-10 23:04:11 +00:00
|
|
|
return this->AddString(cmSystemTools::EscapeQuotes(c));
|
2008-09-25 14:21:15 +00:00
|
|
|
}
|
2016-08-18 18:36:29 +00:00
|
|
|
return this->AddString(c);
|
2008-09-25 14:21:15 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
return this->EmptyVariable;
|
|
|
|
}
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream e;
|
2008-09-24 12:51:19 +00:00
|
|
|
e << "Syntax $" << key << "{} is not supported. "
|
2008-09-25 14:21:15 +00:00
|
|
|
<< "Only ${}, $ENV{}, and $CACHE{} are allowed.";
|
2008-09-24 12:51:19 +00:00
|
|
|
this->SetError(e.str());
|
2016-06-27 20:44:16 +00:00
|
|
|
return CM_NULLPTR;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-06 20:20:02 +00:00
|
|
|
char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
|
2005-06-08 14:41:05 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!var) {
|
2016-06-27 20:44:16 +00:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream ostr;
|
2006-03-15 16:02:08 +00:00
|
|
|
ostr << this->FileLine;
|
2014-03-10 23:04:11 +00:00
|
|
|
return this->AddString(ostr.str());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
const char* value = this->Makefile->GetDefinition(var);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!value && !this->RemoveEmpty) {
|
2010-07-12 19:48:51 +00:00
|
|
|
// check to see if we need to print a warning
|
|
|
|
// if strict mode is on and the variable has
|
|
|
|
// not been "cleared"/initialized with a set(foo ) call
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->WarnUninitialized && !this->Makefile->VariableInitialized(var)) {
|
2010-09-08 16:03:42 +00:00
|
|
|
if (this->CheckSystemVars ||
|
2010-09-16 15:49:58 +00:00
|
|
|
cmSystemTools::IsSubDirectory(this->FileName,
|
|
|
|
this->Makefile->GetHomeDirectory()) ||
|
2016-05-16 14:34:04 +00:00
|
|
|
cmSystemTools::IsSubDirectory(
|
|
|
|
this->FileName, this->Makefile->GetHomeOutputDirectory())) {
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream msg;
|
2010-12-07 21:38:25 +00:00
|
|
|
msg << "uninitialized variable \'" << var << "\'";
|
2016-04-15 14:13:12 +00:00
|
|
|
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
|
2010-04-13 13:21:31 +00:00
|
|
|
}
|
2007-02-09 18:44:37 +00:00
|
|
|
}
|
2016-06-27 20:44:16 +00:00
|
|
|
return CM_NULLPTR;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (this->EscapeQuotes && value) {
|
2014-03-10 23:04:11 +00:00
|
|
|
return this->AddString(cmSystemTools::EscapeQuotes(value));
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2014-02-04 02:16:12 +00:00
|
|
|
return this->AddString(value ? value : "");
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 18:37:42 +00:00
|
|
|
char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ReplaceAtSyntax) {
|
2007-02-09 18:44:37 +00:00
|
|
|
// try to expand the variable
|
2007-06-06 20:20:02 +00:00
|
|
|
char* ret = this->ExpandVariable(var);
|
2007-02-09 18:44:37 +00:00
|
|
|
// if the return was 0 and we want to replace empty strings
|
2012-08-13 17:42:58 +00:00
|
|
|
// then return an empty string
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!ret && this->RemoveEmpty) {
|
2014-02-04 02:16:12 +00:00
|
|
|
return this->AddString("");
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-02-09 18:44:37 +00:00
|
|
|
// if the ret was not 0, then return it
|
2016-05-16 14:34:04 +00:00
|
|
|
if (ret) {
|
2007-02-09 18:44:37 +00:00
|
|
|
return ret;
|
2006-10-04 18:37:42 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2007-02-09 18:44:37 +00:00
|
|
|
// at this point we want to put it back because of one of these cases:
|
2012-08-13 17:42:58 +00:00
|
|
|
// - this->ReplaceAtSyntax is false
|
2007-02-09 18:44:37 +00:00
|
|
|
// - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
|
|
|
|
// and the variable was not defined
|
|
|
|
std::string ref = "@";
|
|
|
|
ref += var;
|
|
|
|
ref += "@";
|
2014-03-10 23:04:11 +00:00
|
|
|
return this->AddString(ref);
|
2006-10-04 18:37:42 +00:00
|
|
|
}
|
|
|
|
|
2005-06-13 14:11:44 +00:00
|
|
|
char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
|
2005-06-08 14:41:05 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!in1) {
|
2005-06-13 14:11:44 +00:00
|
|
|
return in2;
|
2016-08-18 18:36:29 +00:00
|
|
|
}
|
|
|
|
if (!in2) {
|
2005-06-13 14:11:44 +00:00
|
|
|
return in1;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-30 18:49:56 +00:00
|
|
|
size_t len = strlen(in1) + strlen(in2) + 1;
|
2016-05-16 14:34:04 +00:00
|
|
|
char* out = new char[len];
|
2005-06-13 14:11:44 +00:00
|
|
|
strcpy(out, in1);
|
|
|
|
strcat(out, in2);
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Variables.push_back(out);
|
2005-06-14 19:49:30 +00:00
|
|
|
return out;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
void cmCommandArgumentParserHelper::AllocateParserType(
|
|
|
|
cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
|
2005-06-08 14:41:05 +00:00
|
|
|
{
|
2016-06-27 20:44:16 +00:00
|
|
|
pt->str = CM_NULLPTR;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (len == 0) {
|
2006-07-26 15:46:22 +00:00
|
|
|
len = static_cast<int>(strlen(str));
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (len == 0) {
|
2005-06-08 14:41:05 +00:00
|
|
|
return;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
pt->str = new char[len + 1];
|
2005-06-08 14:41:05 +00:00
|
|
|
strncpy(pt->str, str, len);
|
|
|
|
pt->str[len] = 0;
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Variables.push_back(pt->str);
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
|
|
|
|
cmCommandArgumentParserHelper::ParserType* pt, char symbol)
|
2005-06-17 19:50:08 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
switch (symbol) {
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
case ' ':
|
|
|
|
case '#':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '$':
|
|
|
|
case '@':
|
|
|
|
case '^':
|
|
|
|
this->AllocateParserType(pt, &symbol, 1);
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
this->AllocateParserType(pt, "\\;", 2);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
this->AllocateParserType(pt, "\t", 1);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
this->AllocateParserType(pt, "\n", 1);
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
this->AllocateParserType(pt, "\r", 1);
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
this->AllocateParserType(pt, "\0", 1);
|
|
|
|
break;
|
|
|
|
default: {
|
|
|
|
std::ostringstream e;
|
|
|
|
e << "Invalid escape sequence \\" << symbol;
|
|
|
|
this->SetError(e.str());
|
2005-06-17 19:50:08 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-06-17 19:50:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-04 19:57:33 +00:00
|
|
|
void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
|
|
|
|
|
2005-06-08 14:41:05 +00:00
|
|
|
int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!str) {
|
2005-06-08 14:41:05 +00:00
|
|
|
return 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-06-08 14:41:05 +00:00
|
|
|
this->Verbose = verb;
|
|
|
|
this->InputBuffer = str;
|
|
|
|
this->InputBufferPos = 0;
|
|
|
|
this->CurrentLine = 0;
|
2012-08-13 17:42:58 +00:00
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Result = "";
|
2005-06-08 14:41:05 +00:00
|
|
|
|
|
|
|
yyscan_t yyscanner;
|
|
|
|
cmCommandArgument_yylex_init(&yyscanner);
|
|
|
|
cmCommandArgument_yyset_extra(this, yyscanner);
|
2007-06-04 19:57:33 +00:00
|
|
|
cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
|
2005-06-08 14:41:05 +00:00
|
|
|
int res = cmCommandArgument_yyparse(yyscanner);
|
|
|
|
cmCommandArgument_yylex_destroy(yyscanner);
|
2016-05-16 14:34:04 +00:00
|
|
|
if (res != 0) {
|
2005-06-08 14:41:05 +00:00
|
|
|
return 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2005-06-08 14:41:05 +00:00
|
|
|
|
|
|
|
this->CleanupParser();
|
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
if (Verbose) {
|
|
|
|
std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
|
|
|
|
<< std::endl;
|
|
|
|
}
|
2005-06-08 14:41:05 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentParserHelper::CleanupParser()
|
|
|
|
{
|
2005-06-14 19:49:30 +00:00
|
|
|
std::vector<char*>::iterator sit;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (sit = this->Variables.begin(); sit != this->Variables.end(); ++sit) {
|
|
|
|
delete[] * sit;
|
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Variables.erase(this->Variables.begin(), this->Variables.end());
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (maxlen < 1) {
|
2005-06-08 14:41:05 +00:00
|
|
|
return 0;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (this->InputBufferPos < this->InputBuffer.size()) {
|
|
|
|
buf[0] = this->InputBuffer[this->InputBufferPos++];
|
|
|
|
if (buf[0] == '\n') {
|
|
|
|
this->CurrentLine++;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
return (1);
|
|
|
|
}
|
2016-08-18 18:36:29 +00:00
|
|
|
buf[0] = '\n';
|
|
|
|
return (0);
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
2005-06-14 19:49:30 +00:00
|
|
|
|
2005-06-08 14:41:05 +00:00
|
|
|
void cmCommandArgumentParserHelper::Error(const char* str)
|
|
|
|
{
|
|
|
|
unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
|
2015-01-05 19:31:31 +00:00
|
|
|
std::ostringstream ostr;
|
2005-06-13 14:00:59 +00:00
|
|
|
ostr << str << " (" << pos << ")";
|
2008-09-24 12:51:19 +00:00
|
|
|
this->SetError(ostr.str());
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
|
|
|
|
{
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile = mf;
|
2010-08-25 16:35:40 +00:00
|
|
|
this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
|
2010-09-01 15:24:20 +00:00
|
|
|
this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmCommandArgumentParserHelper::SetResult(const char* value)
|
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!value) {
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Result = "";
|
2005-06-08 14:41:05 +00:00
|
|
|
return;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Result = value;
|
2005-06-08 14:41:05 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 12:51:19 +00:00
|
|
|
void cmCommandArgumentParserHelper::SetError(std::string const& msg)
|
|
|
|
{
|
|
|
|
// Keep only the first error.
|
2016-05-16 14:34:04 +00:00
|
|
|
if (this->ErrorString.empty()) {
|
2008-09-24 12:51:19 +00:00
|
|
|
this->ErrorString = msg;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2008-09-24 12:51:19 +00:00
|
|
|
}
|