cmake/Source/cmCommandArgumentParserHelper.cxx

310 lines
7.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCommandArgumentParserHelper.h"
2016-10-30 18:24:19 +01:00
#include "cmCommandArgumentLexer.h"
#include "cmMakefile.h"
2016-07-09 11:21:54 +02:00
#include "cmState.h"
#include "cmSystemTools.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2016-10-30 18:24:19 +01:00
#include <iostream>
#include <sstream>
#include <string.h>
2013-11-03 12:27:13 +02:00
2016-07-09 11:21:54 +02:00
int cmCommandArgument_yyparse(yyscan_t yyscanner);
//
cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
{
2011-02-07 16:37:25 +01:00
this->WarnUninitialized = false;
this->CheckSystemVars = false;
this->FileLine = -1;
2018-01-26 17:06:56 +01:00
this->FileName = nullptr;
this->RemoveEmpty = true;
this->NoEscapeMode = false;
this->ReplaceAtSyntax = false;
}
cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
{
this->CleanupParser();
}
void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
{
this->FileLine = line;
this->FileName = file;
}
2018-04-23 21:13:27 +02:00
const char* cmCommandArgumentParserHelper::AddString(const std::string& str)
{
2016-07-09 11:21:54 +02:00
if (str.empty()) {
2018-04-23 21:13:27 +02:00
return "";
2016-07-09 11:21:54 +02:00
}
char* stVal = new char[str.size() + 1];
2015-04-27 22:25:09 +02:00
strcpy(stVal, str.c_str());
this->Variables.push_back(stVal);
return stVal;
}
2018-04-23 21:13:27 +02:00
const char* cmCommandArgumentParserHelper::ExpandSpecialVariable(
const char* key, const char* var)
{
2016-07-09 11:21:54 +02:00
if (!key) {
return this->ExpandVariable(var);
2016-07-09 11:21:54 +02:00
}
if (!var) {
2018-04-23 21:13:27 +02:00
return "";
2016-07-09 11:21:54 +02:00
}
if (strcmp(key, "ENV") == 0) {
2016-10-30 18:24:19 +01:00
std::string str;
if (cmSystemTools::GetEnv(var, str)) {
2016-07-09 11:21:54 +02:00
if (this->EscapeQuotes) {
2016-10-30 18:24:19 +01:00
return this->AddString(cmSystemTools::EscapeQuotes(str));
}
2016-10-30 18:24:19 +01:00
return this->AddString(str);
}
2018-04-23 21:13:27 +02:00
return "";
2016-07-09 11:21:54 +02:00
}
if (strcmp(key, "CACHE") == 0) {
if (const char* c =
this->Makefile->GetState()->GetInitializedCacheValue(var)) {
if (this->EscapeQuotes) {
2015-04-27 22:25:09 +02:00
return this->AddString(cmSystemTools::EscapeQuotes(c));
}
2016-10-30 18:24:19 +01:00
return this->AddString(c);
}
2018-04-23 21:13:27 +02:00
return "";
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
std::ostringstream e;
e << "Syntax $" << key << "{} is not supported. "
<< "Only ${}, $ENV{}, and $CACHE{} are allowed.";
this->SetError(e.str());
2018-01-26 17:06:56 +01:00
return nullptr;
}
2018-04-23 21:13:27 +02:00
const char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
{
2016-07-09 11:21:54 +02:00
if (!var) {
2018-01-26 17:06:56 +01:00
return nullptr;
2016-07-09 11:21:54 +02:00
}
if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
2015-04-27 22:25:09 +02:00
std::ostringstream ostr;
ostr << this->FileLine;
2015-04-27 22:25:09 +02:00
return this->AddString(ostr.str());
2016-07-09 11:21:54 +02:00
}
const char* value = this->Makefile->GetDefinition(var);
2016-07-09 11:21:54 +02:00
if (!value && !this->RemoveEmpty) {
2011-02-07 16:37:25 +01: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-07-09 11:21:54 +02:00
if (this->WarnUninitialized && !this->Makefile->VariableInitialized(var)) {
2011-02-07 16:37:25 +01:00
if (this->CheckSystemVars ||
2018-04-23 21:13:27 +02:00
(this->FileName &&
(cmSystemTools::IsSubDirectory(
this->FileName, this->Makefile->GetHomeDirectory()) ||
cmSystemTools::IsSubDirectory(
this->FileName, this->Makefile->GetHomeOutputDirectory())))) {
2015-04-27 22:25:09 +02:00
std::ostringstream msg;
2011-02-07 16:37:25 +01:00
msg << "uninitialized variable \'" << var << "\'";
2016-07-09 11:21:54 +02:00
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
2011-02-07 16:37:25 +01:00
}
}
2018-01-26 17:06:56 +01:00
return nullptr;
2016-07-09 11:21:54 +02:00
}
if (this->EscapeQuotes && value) {
2015-04-27 22:25:09 +02:00
return this->AddString(cmSystemTools::EscapeQuotes(value));
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return this->AddString(value ? value : "");
}
2018-04-23 21:13:27 +02:00
const char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
{
2016-07-09 11:21:54 +02:00
if (this->ReplaceAtSyntax) {
// try to expand the variable
2018-04-23 21:13:27 +02:00
const char* ret = this->ExpandVariable(var);
// if the return was 0 and we want to replace empty strings
2013-03-16 19:13:01 +02:00
// then return an empty string
2016-07-09 11:21:54 +02:00
if (!ret && this->RemoveEmpty) {
2015-04-27 22:25:09 +02:00
return this->AddString("");
2016-07-09 11:21:54 +02:00
}
// if the ret was not 0, then return it
2016-07-09 11:21:54 +02:00
if (ret) {
return ret;
}
2016-07-09 11:21:54 +02:00
}
// at this point we want to put it back because of one of these cases:
2013-03-16 19:13:01 +02:00
// - this->ReplaceAtSyntax is false
// - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
// and the variable was not defined
std::string ref = "@";
ref += var;
ref += "@";
2015-04-27 22:25:09 +02:00
return this->AddString(ref);
}
2018-04-23 21:13:27 +02:00
const char* cmCommandArgumentParserHelper::CombineUnions(const char* in1,
const char* in2)
{
2016-07-09 11:21:54 +02:00
if (!in1) {
return in2;
2016-10-30 18:24:19 +01:00
}
if (!in2) {
return in1;
2016-07-09 11:21:54 +02:00
}
size_t len = strlen(in1) + strlen(in2) + 1;
2016-07-09 11:21:54 +02:00
char* out = new char[len];
strcpy(out, in1);
strcat(out, in2);
this->Variables.push_back(out);
return out;
}
2016-07-09 11:21:54 +02:00
void cmCommandArgumentParserHelper::AllocateParserType(
cmCommandArgumentParserHelper::ParserType* pt, const char* str, int len)
{
2018-01-26 17:06:56 +01:00
pt->str = nullptr;
2016-07-09 11:21:54 +02:00
if (len == 0) {
len = static_cast<int>(strlen(str));
2016-07-09 11:21:54 +02:00
}
if (len == 0) {
return;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
char* out = new char[len + 1];
strncpy(out, str, len);
out[len] = 0;
pt->str = out;
this->Variables.push_back(out);
}
2016-07-09 11:21:54 +02:00
bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
cmCommandArgumentParserHelper::ParserType* pt, char symbol)
{
2016-07-09 11:21:54 +02: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());
}
2016-07-09 11:21:54 +02:00
return false;
}
return true;
}
void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
{
2016-07-09 11:21:54 +02:00
if (!str) {
return 0;
2016-07-09 11:21:54 +02:00
}
this->Verbose = verb;
this->InputBuffer = str;
this->InputBufferPos = 0;
this->CurrentLine = 0;
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
this->Result.clear();
yyscan_t yyscanner;
cmCommandArgument_yylex_init(&yyscanner);
cmCommandArgument_yyset_extra(this, yyscanner);
cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
int res = cmCommandArgument_yyparse(yyscanner);
cmCommandArgument_yylex_destroy(yyscanner);
2016-07-09 11:21:54 +02:00
if (res != 0) {
return 0;
2016-07-09 11:21:54 +02:00
}
this->CleanupParser();
2016-07-09 11:21:54 +02:00
if (Verbose) {
std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
<< std::endl;
}
return 1;
}
void cmCommandArgumentParserHelper::CleanupParser()
{
std::vector<char*>::iterator sit;
2018-01-26 17:06:56 +01:00
for (char* var : this->Variables) {
delete[] var;
2016-07-09 11:21:54 +02:00
}
this->Variables.erase(this->Variables.begin(), this->Variables.end());
}
int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
{
2016-07-09 11:21:54 +02:00
if (maxlen < 1) {
return 0;
2016-07-09 11:21:54 +02:00
}
if (this->InputBufferPos < this->InputBuffer.size()) {
buf[0] = this->InputBuffer[this->InputBufferPos++];
if (buf[0] == '\n') {
this->CurrentLine++;
}
2016-07-09 11:21:54 +02:00
return (1);
}
2016-10-30 18:24:19 +01:00
buf[0] = '\n';
return (0);
}
void cmCommandArgumentParserHelper::Error(const char* str)
{
unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
2015-04-27 22:25:09 +02:00
std::ostringstream ostr;
ostr << str << " (" << pos << ")";
this->SetError(ostr.str());
}
void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
{
this->Makefile = mf;
2011-02-07 16:37:25 +01:00
this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
}
void cmCommandArgumentParserHelper::SetResult(const char* value)
{
2016-07-09 11:21:54 +02:00
if (!value) {
2018-01-26 17:06:56 +01:00
this->Result.clear();
return;
2016-07-09 11:21:54 +02:00
}
this->Result = value;
}
void cmCommandArgumentParserHelper::SetError(std::string const& msg)
{
// Keep only the first error.
2016-07-09 11:21:54 +02:00
if (this->ErrorString.empty()) {
this->ErrorString = msg;
2016-07-09 11:21:54 +02:00
}
}