CMake/Source/cmCommandArgumentParserHelper.cxx

317 lines
8.1 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
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"
#include <cm_kwiml.h>
#include "cmCommandArgumentLexer.h"
2005-06-08 14:41:05 +00:00
#include "cmMakefile.h"
#include "cmState.h"
#include "cmSystemTools.h"
#include "cmake.h"
2005-06-08 14:41:05 +00:00
#include <cmConfigure.h>
#include <iostream>
#include <sstream>
#include <string.h>
int cmCommandArgument_yyparse(yyscan_t yyscanner);
//
2005-06-08 14:41:05 +00:00
cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
{
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;
this->RemoveEmpty = true;
2006-03-15 16:02:08 +00:00
this->EmptyVariable[0] = 0;
strcpy(this->DCURLYVariable, "${");
strcpy(this->RCURLYVariable, "}");
strcpy(this->ATVariable, "@");
2006-03-15 16:02:08 +00:00
strcpy(this->DOLLARVariable, "$");
strcpy(this->LCURLYVariable, "{");
strcpy(this->BSLASHVariable, "\\");
2006-03-15 16:02:08 +00:00
this->NoEscapeMode = false;
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
}
char* cmCommandArgumentParserHelper::AddString(const std::string& str)
2005-06-08 14:41:05 +00:00
{
if (str.empty()) {
2006-03-15 16:02:08 +00:00
return this->EmptyVariable;
}
char* stVal = new char[str.size() + 1];
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
}
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
{
if (!key) {
2005-06-08 14:41:05 +00:00
return this->ExpandVariable(var);
}
if (!var) {
return this->EmptyVariable;
}
if (strcmp(key, "ENV") == 0) {
std::string str;
if (cmSystemTools::GetEnv(var, str)) {
if (this->EscapeQuotes) {
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
}
return this->EmptyVariable;
}
if (strcmp(key, "CACHE") == 0) {
if (const char* c =
this->Makefile->GetState()->GetInitializedCacheValue(var)) {
if (this->EscapeQuotes) {
return this->AddString(cmSystemTools::EscapeQuotes(c));
}
2016-08-18 18:36:29 +00:00
return this->AddString(c);
}
return this->EmptyVariable;
}
std::ostringstream e;
e << "Syntax $" << key << "{} is not supported. "
<< "Only ${}, $ENV{}, and $CACHE{} are allowed.";
this->SetError(e.str());
2016-06-27 20:44:16 +00:00
return CM_NULLPTR;
2005-06-08 14:41:05 +00:00
}
char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
2005-06-08 14:41:05 +00:00
{
if (!var) {
2016-06-27 20:44:16 +00:00
return CM_NULLPTR;
}
if (this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0) {
std::ostringstream ostr;
2006-03-15 16:02:08 +00:00
ostr << this->FileLine;
return this->AddString(ostr.str());
}
2006-03-15 16:02:08 +00:00
const char* value = this->Makefile->GetDefinition(var);
if (!value && !this->RemoveEmpty) {
// 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
if (this->WarnUninitialized && !this->Makefile->VariableInitialized(var)) {
if (this->CheckSystemVars ||
2010-09-16 15:49:58 +00:00
cmSystemTools::IsSubDirectory(this->FileName,
this->Makefile->GetHomeDirectory()) ||
cmSystemTools::IsSubDirectory(
this->FileName, this->Makefile->GetHomeOutputDirectory())) {
std::ostringstream msg;
2010-12-07 21:38:25 +00:00
msg << "uninitialized variable \'" << var << "\'";
this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, msg.str());
}
}
2016-06-27 20:44:16 +00:00
return CM_NULLPTR;
}
if (this->EscapeQuotes && value) {
return this->AddString(cmSystemTools::EscapeQuotes(value));
}
return this->AddString(value ? value : "");
2005-06-08 14:41:05 +00:00
}
char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
{
if (this->ReplaceAtSyntax) {
// try to expand the variable
char* ret = this->ExpandVariable(var);
// if the return was 0 and we want to replace empty strings
// then return an empty string
if (!ret && this->RemoveEmpty) {
return this->AddString("");
}
// if the ret was not 0, then return it
if (ret) {
return ret;
}
}
// at this point we want to put it back because of one of these cases:
// - 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 += "@";
return this->AddString(ref);
}
2005-06-13 14:11:44 +00:00
char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
2005-06-08 14:41:05 +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;
}
size_t len = strlen(in1) + strlen(in2) + 1;
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
}
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;
if (len == 0) {
2006-07-26 15:46:22 +00:00
len = static_cast<int>(strlen(str));
}
if (len == 0) {
2005-06-08 14:41:05 +00:00
return;
}
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
}
bool cmCommandArgumentParserHelper::HandleEscapeSymbol(
cmCommandArgumentParserHelper::ParserType* pt, char symbol)
{
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());
}
return false;
}
return true;
}
void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
2005-06-08 14:41:05 +00:00
int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
{
if (!str) {
2005-06-08 14:41:05 +00:00
return 0;
}
2005-06-08 14:41:05 +00:00
this->Verbose = verb;
this->InputBuffer = str;
this->InputBufferPos = 0;
this->CurrentLine = 0;
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);
cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
2005-06-08 14:41:05 +00:00
int res = cmCommandArgument_yyparse(yyscanner);
cmCommandArgument_yylex_destroy(yyscanner);
if (res != 0) {
2005-06-08 14:41:05 +00:00
return 0;
}
2005-06-08 14:41:05 +00:00
this->CleanupParser();
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;
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)
{
if (maxlen < 1) {
2005-06-08 14:41:05 +00:00
return 0;
}
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
}
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);
std::ostringstream ostr;
2005-06-13 14:00:59 +00:00
ostr << str << " (" << pos << ")";
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;
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)
{
if (!value) {
2006-03-15 16:02:08 +00:00
this->Result = "";
2005-06-08 14:41:05 +00:00
return;
}
2006-03-15 16:02:08 +00:00
this->Result = value;
2005-06-08 14:41:05 +00:00
}
void cmCommandArgumentParserHelper::SetError(std::string const& msg)
{
// Keep only the first error.
if (this->ErrorString.empty()) {
this->ErrorString = msg;
}
}