1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmCommandArgumentParserHelper.h"
14 #include "cmSystemTools.h"
15 #include "cmMakefile.h"
17 #include "cmCommandArgumentLexer.h"
19 int cmCommandArgument_yyparse( yyscan_t yyscanner );
21 cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
23 this->WarnUninitialized = false;
24 this->CheckSystemVars = false;
27 this->RemoveEmpty = true;
28 this->EmptyVariable[0] = 0;
29 strcpy(this->DCURLYVariable, "${");
30 strcpy(this->RCURLYVariable, "}");
31 strcpy(this->ATVariable, "@");
32 strcpy(this->DOLLARVariable, "$");
33 strcpy(this->LCURLYVariable, "{");
34 strcpy(this->BSLASHVariable, "\\");
36 this->NoEscapeMode = false;
37 this->ReplaceAtSyntax = false;
41 cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
43 this->CleanupParser();
46 void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
48 this->FileLine = line;
49 this->FileName = file;
52 char* cmCommandArgumentParserHelper::AddString(const char* str)
56 return this->EmptyVariable;
58 char* stVal = new char[strlen(str)+1];
60 this->Variables.push_back(stVal);
64 char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key,
69 return this->ExpandVariable(var);
73 return this->EmptyVariable;
75 if ( strcmp(key, "ENV") == 0 )
77 char *ptr = getenv(var);
80 if (this->EscapeQuotes)
82 return this->AddString(cmSystemTools::EscapeQuotes(ptr).c_str());
89 return this->EmptyVariable;
91 if ( strcmp(key, "CACHE") == 0 )
93 if(const char* c = this->Makefile->GetCacheManager()->GetCacheValue(var))
95 if(this->EscapeQuotes)
97 return this->AddString(cmSystemTools::EscapeQuotes(c).c_str());
101 return this->AddString(c);
104 return this->EmptyVariable;
107 e << "Syntax $" << key << "{} is not supported. "
108 << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
109 this->SetError(e.str());
113 char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
119 if(this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0)
121 cmOStringStream ostr;
122 ostr << this->FileLine;
123 return this->AddString(ostr.str().c_str());
125 const char* value = this->Makefile->GetDefinition(var);
126 if(!value && !this->RemoveEmpty)
128 // check to see if we need to print a warning
129 // if strict mode is on and the variable has
130 // not been "cleared"/initialized with a set(foo ) call
131 if(this->WarnUninitialized && !this->Makefile->VariableInitialized(var))
133 if (this->CheckSystemVars ||
134 cmSystemTools::IsSubDirectory(this->FileName,
135 this->Makefile->GetHomeDirectory()) ||
136 cmSystemTools::IsSubDirectory(this->FileName,
137 this->Makefile->GetHomeOutputDirectory()))
140 cmListFileBacktrace bt;
141 cmListFileContext lfc;
142 lfc.FilePath = this->FileName;
143 lfc.Line = this->FileLine;
145 msg << "uninitialized variable \'" << var << "\'";
146 this->Makefile->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
147 msg.str().c_str(), bt);
152 if (this->EscapeQuotes && value)
154 return this->AddString(cmSystemTools::EscapeQuotes(value).c_str());
156 return this->AddString(value);
159 char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
161 if(this->ReplaceAtSyntax)
163 // try to expand the variable
164 char* ret = this->ExpandVariable(var);
165 // if the return was 0 and we want to replace empty strings
166 // then return an empty string
167 if(!ret && this->RemoveEmpty)
169 return this->AddString(ret);
171 // if the ret was not 0, then return it
177 // at this point we want to put it back because of one of these cases:
178 // - this->ReplaceAtSyntax is false
179 // - this->ReplaceAtSyntax is true, but this->RemoveEmpty is false,
180 // and the variable was not defined
181 std::string ref = "@";
184 return this->AddString(ref.c_str());
187 char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
197 size_t len = strlen(in1) + strlen(in2) + 1;
198 char* out = new char [ len ];
201 this->Variables.push_back(out);
205 void cmCommandArgumentParserHelper::AllocateParserType
206 (cmCommandArgumentParserHelper::ParserType* pt,const char* str, int len)
211 len = static_cast<int>(strlen(str));
217 pt->str = new char[ len + 1 ];
218 strncpy(pt->str, str, len);
220 this->Variables.push_back(pt->str);
223 bool cmCommandArgumentParserHelper::HandleEscapeSymbol
224 (cmCommandArgumentParserHelper::ParserType* pt, char symbol)
237 this->AllocateParserType(pt, &symbol, 1);
240 this->AllocateParserType(pt, "\\;", 2);
243 this->AllocateParserType(pt, "\t", 1);
246 this->AllocateParserType(pt, "\n", 1);
249 this->AllocateParserType(pt, "\r", 1);
252 this->AllocateParserType(pt, "\0", 1);
257 e << "Invalid escape sequence \\" << symbol;
258 this->SetError(e.str());
265 void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
267 int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
273 this->Verbose = verb;
274 this->InputBuffer = str;
275 this->InputBufferPos = 0;
276 this->CurrentLine = 0;
281 cmCommandArgument_yylex_init(&yyscanner);
282 cmCommandArgument_yyset_extra(this, yyscanner);
283 cmCommandArgument_SetupEscapes(yyscanner, this->NoEscapeMode);
284 int res = cmCommandArgument_yyparse(yyscanner);
285 cmCommandArgument_yylex_destroy(yyscanner);
291 this->CleanupParser();
295 std::cerr << "Expanding [" << str << "] produced: ["
296 << this->Result.c_str() << "]" << std::endl;
301 void cmCommandArgumentParserHelper::CleanupParser()
303 std::vector<char*>::iterator sit;
304 for ( sit = this->Variables.begin();
305 sit != this->Variables.end();
310 this->Variables.erase(this->Variables.begin(), this->Variables.end());
313 int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
319 if ( this->InputBufferPos < this->InputBuffer.size() )
321 buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
322 if ( buf[0] == '\n' )
324 this->CurrentLine ++;
335 void cmCommandArgumentParserHelper::Error(const char* str)
337 unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
338 cmOStringStream ostr;
339 ostr << str << " (" << pos << ")";
340 this->SetError(ostr.str());
343 void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
346 this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
347 this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
350 void cmCommandArgumentParserHelper::SetResult(const char* value)
357 this->Result = value;
360 void cmCommandArgumentParserHelper::SetError(std::string const& msg)
362 // Keep only the first error.
363 if(this->ErrorString.empty())
365 this->ErrorString = msg;