cmake/Source/cmCommandArgumentParserHelper.cxx

295 lines
7.3 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"
2020-02-01 23:06:01 +01:00
#include <cstring>
#include <iostream>
#include <sstream>
2020-08-30 11:54:41 +02:00
#include <utility>
#include <cm/memory>
2021-09-14 00:13:48 +02:00
#include <cm/optional>
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
2016-10-30 18:24:19 +01:00
#include "cmCommandArgumentLexer.h"
2021-09-14 00:13:48 +02:00
#include "cmListFileCache.h"
#include "cmMakefile.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2016-07-09 11:21:54 +02:00
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
int cmCommandArgument_yyparse(yyscan_t yyscanner);
//
cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
{
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
}
2020-08-30 11:54:41 +02:00
auto stVal = cm::make_unique<char[]>(str.size() + 1);
strcpy(stVal.get(), str.c_str());
this->Variables.push_back(std::move(stVal));
return this->Variables.back().get();
}
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) {
2020-02-01 23:06:01 +01:00
return this->AddString(cmEscapeQuotes(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) {
2020-08-30 11:54:41 +02:00
if (cmProp c = this->Makefile->GetState()->GetInitializedCacheValue(var)) {
2016-07-09 11:21:54 +02:00
if (this->EscapeQuotes) {
2020-02-01 23:06:01 +01:00
return this->AddString(cmEscapeQuotes(*c));
}
2018-10-28 12:09:07 +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) {
2021-09-14 00:13:48 +02:00
std::string line;
cmListFileContext const& top = this->Makefile->GetBacktrace().Top();
if (top.DeferId) {
line = cmStrCat("DEFERRED:"_s, *top.DeferId);
} else {
line = std::to_string(this->FileLine);
}
return this->AddString(line);
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
cmProp value = this->Makefile->GetDefinition(var);
2019-11-11 23:01:05 +01:00
if (!value) {
this->Makefile->MaybeWarnUninitialized(var, this->FileName);
if (!this->RemoveEmpty) {
return nullptr;
}
2016-07-09 11:21:54 +02:00
}
if (this->EscapeQuotes && value) {
2021-09-14 00:13:48 +02:00
return this->AddString(cmEscapeQuotes(*value));
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
return this->AddString(cmToCStrSafe(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
2020-02-01 23:06:01 +01:00
std::string ref = cmStrCat('@', var, '@');
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;
2020-08-30 11:54:41 +02:00
auto out = cm::make_unique<char[]>(len);
strcpy(out.get(), in1);
strcat(out.get(), in2);
this->Variables.push_back(std::move(out));
return this->Variables.back().get();
}
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
}
2020-08-30 11:54:41 +02:00
auto out = cm::make_unique<char[]>(len + 1);
memcpy(out.get(), str, len);
out.get()[len] = 0;
pt->str = out.get();
this->Variables.push_back(std::move(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);
2021-09-14 00:13:48 +02:00
int cmCommandArgumentParserHelper::ParseString(std::string const& str,
int verb)
{
2021-09-14 00:13:48 +02:00
if (str.empty()) {
return 0;
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
this->InputSize = str.size();
this->Verbose = verb;
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);
2021-09-14 00:13:48 +02:00
auto* scanBuf = cmCommandArgument_yy_scan_string(str.c_str(), yyscanner);
cmCommandArgument_yyset_extra(this, yyscanner);
cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
int res = cmCommandArgument_yyparse(yyscanner);
2021-09-14 00:13:48 +02:00
cmCommandArgument_yy_delete_buffer(scanBuf, 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();
2021-09-14 00:13:48 +02:00
if (this->Verbose) {
2016-07-09 11:21:54 +02:00
std::cerr << "Expanding [" << str << "] produced: [" << this->Result << "]"
<< std::endl;
}
return 1;
}
void cmCommandArgumentParserHelper::CleanupParser()
{
2020-08-30 11:54:41 +02:00
this->Variables.clear();
}
2021-09-14 00:13:48 +02:00
void cmCommandArgumentParserHelper::Error(const char* str)
{
2021-09-14 00:13:48 +02:00
auto pos = this->InputBufferPos;
auto const isEof = (this->InputSize < this->InputBufferPos);
if (!isEof) {
pos -= this->LastTokenLength;
2016-07-09 11:21:54 +02:00
}
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;
}
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
}
}
2021-09-14 00:13:48 +02:00
void cmCommandArgumentParserHelper::UpdateInputPosition(int const tokenLength)
{
this->InputBufferPos += tokenLength;
this->LastTokenLength = tokenLength;
}