packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmCommandArgumentParserHelper.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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"
13
14 #include "cmSystemTools.h"
15 #include "cmMakefile.h"
16
17 #include "cmCommandArgumentLexer.h"
18
19 int cmCommandArgument_yyparse( yyscan_t yyscanner );
20 //
21 cmCommandArgumentParserHelper::cmCommandArgumentParserHelper()
22 {
23   this->WarnUninitialized = false;
24   this->CheckSystemVars = false;
25   this->FileLine = -1;
26   this->FileName = 0;
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, "\\");
35
36   this->NoEscapeMode = false;
37   this->ReplaceAtSyntax = false;
38 }
39
40
41 cmCommandArgumentParserHelper::~cmCommandArgumentParserHelper()
42 {
43   this->CleanupParser();
44 }
45
46 void cmCommandArgumentParserHelper::SetLineFile(long line, const char* file)
47 {
48   this->FileLine = line;
49   this->FileName = file;
50 }
51
52 char* cmCommandArgumentParserHelper::AddString(const char* str)
53 {
54   if ( !str || !*str )
55     {
56     return this->EmptyVariable;
57     }
58   char* stVal = new char[strlen(str)+1];
59   strcpy(stVal, str);
60   this->Variables.push_back(stVal);
61   return stVal;
62 }
63
64 char* cmCommandArgumentParserHelper::ExpandSpecialVariable(const char* key,
65                                                            const char* var)
66 {
67   if ( !key )
68     {
69     return this->ExpandVariable(var);
70     }
71   if(!var)
72     {
73     return this->EmptyVariable;
74     }
75   if ( strcmp(key, "ENV") == 0 )
76     {
77     char *ptr = getenv(var);
78     if (ptr)
79       {
80       if (this->EscapeQuotes)
81         {
82         return this->AddString(cmSystemTools::EscapeQuotes(ptr).c_str());
83         }
84       else
85         {
86         return ptr;
87         }
88       }
89     return this->EmptyVariable;
90     }
91   if ( strcmp(key, "CACHE") == 0 )
92     {
93     if(const char* c = this->Makefile->GetCacheManager()->GetCacheValue(var))
94       {
95       if(this->EscapeQuotes)
96         {
97         return this->AddString(cmSystemTools::EscapeQuotes(c).c_str());
98         }
99       else
100         {
101         return this->AddString(c);
102         }
103       }
104     return this->EmptyVariable;
105     }
106   cmOStringStream e;
107   e << "Syntax $" << key << "{} is not supported.  "
108     << "Only ${}, $ENV{}, and $CACHE{} are allowed.";
109   this->SetError(e.str());
110   return 0;
111 }
112
113 char* cmCommandArgumentParserHelper::ExpandVariable(const char* var)
114 {
115   if(!var)
116     {
117     return 0;
118     }
119   if(this->FileLine >= 0 && strcmp(var, "CMAKE_CURRENT_LIST_LINE") == 0)
120     {
121     cmOStringStream ostr;
122     ostr << this->FileLine;
123     return this->AddString(ostr.str().c_str());
124     }
125   const char* value = this->Makefile->GetDefinition(var);
126   if(!value && !this->RemoveEmpty)
127     {
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))
132       {
133       if (this->CheckSystemVars ||
134           cmSystemTools::IsSubDirectory(this->FileName,
135                                         this->Makefile->GetHomeDirectory()) ||
136           cmSystemTools::IsSubDirectory(this->FileName,
137                                      this->Makefile->GetHomeOutputDirectory()))
138         {
139         cmOStringStream msg;
140         cmListFileBacktrace bt;
141         cmListFileContext lfc;
142         lfc.FilePath = this->FileName;
143         lfc.Line = this->FileLine;
144         bt.push_back(lfc);
145         msg << "uninitialized variable \'" << var << "\'";
146         this->Makefile->GetCMakeInstance()->IssueMessage(cmake::AUTHOR_WARNING,
147                                                         msg.str().c_str(), bt);
148         }
149       }
150     return 0;
151     }
152   if (this->EscapeQuotes && value)
153     {
154     return this->AddString(cmSystemTools::EscapeQuotes(value).c_str());
155     }
156   return this->AddString(value);
157 }
158
159 char* cmCommandArgumentParserHelper::ExpandVariableForAt(const char* var)
160 {
161   if(this->ReplaceAtSyntax)
162     {
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)
168       {
169       return this->AddString(ret);
170       }
171     // if the ret was not 0, then return it
172     if(ret)
173       {
174       return ret;
175       }
176     }
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 = "@";
182   ref += var;
183   ref += "@";
184   return this->AddString(ref.c_str());
185 }
186
187 char* cmCommandArgumentParserHelper::CombineUnions(char* in1, char* in2)
188 {
189   if ( !in1 )
190     {
191     return in2;
192     }
193   else if ( !in2 )
194     {
195     return in1;
196     }
197   size_t len = strlen(in1) + strlen(in2) + 1;
198   char* out = new char [ len ];
199   strcpy(out, in1);
200   strcat(out, in2);
201   this->Variables.push_back(out);
202   return out;
203 }
204
205 void cmCommandArgumentParserHelper::AllocateParserType
206 (cmCommandArgumentParserHelper::ParserType* pt,const char* str, int len)
207 {
208   pt->str = 0;
209   if ( len == 0 )
210     {
211     len = static_cast<int>(strlen(str));
212     }
213   if ( len == 0 )
214     {
215     return;
216     }
217   pt->str = new char[ len + 1 ];
218   strncpy(pt->str, str, len);
219   pt->str[len] = 0;
220   this->Variables.push_back(pt->str);
221 }
222
223 bool cmCommandArgumentParserHelper::HandleEscapeSymbol
224 (cmCommandArgumentParserHelper::ParserType* pt, char symbol)
225 {
226   switch ( symbol )
227     {
228   case '\\':
229   case '"':
230   case ' ':
231   case '#':
232   case '(':
233   case ')':
234   case '$':
235   case '@':
236   case '^':
237     this->AllocateParserType(pt, &symbol, 1);
238     break;
239   case ';':
240     this->AllocateParserType(pt, "\\;", 2);
241     break;
242   case 't':
243     this->AllocateParserType(pt, "\t", 1);
244     break;
245   case 'n':
246     this->AllocateParserType(pt, "\n", 1);
247     break;
248   case 'r':
249     this->AllocateParserType(pt, "\r", 1);
250     break;
251   case '0':
252     this->AllocateParserType(pt, "\0", 1);
253     break;
254   default:
255     {
256     cmOStringStream e;
257     e << "Invalid escape sequence \\" << symbol;
258     this->SetError(e.str());
259     }
260     return false;
261     }
262   return true;
263 }
264
265 void cmCommandArgument_SetupEscapes(yyscan_t yyscanner, bool noEscapes);
266
267 int cmCommandArgumentParserHelper::ParseString(const char* str, int verb)
268 {
269   if ( !str)
270     {
271     return 0;
272     }
273   this->Verbose = verb;
274   this->InputBuffer = str;
275   this->InputBufferPos = 0;
276   this->CurrentLine = 0;
277
278   this->Result = "";
279
280   yyscan_t yyscanner;
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);
286   if ( res != 0 )
287     {
288     return 0;
289     }
290
291   this->CleanupParser();
292
293   if ( Verbose )
294     {
295     std::cerr << "Expanding [" << str << "] produced: ["
296               << this->Result.c_str() << "]" << std::endl;
297     }
298   return 1;
299 }
300
301 void cmCommandArgumentParserHelper::CleanupParser()
302 {
303   std::vector<char*>::iterator sit;
304   for ( sit = this->Variables.begin();
305     sit != this->Variables.end();
306     ++ sit )
307     {
308     delete [] *sit;
309     }
310   this->Variables.erase(this->Variables.begin(), this->Variables.end());
311 }
312
313 int cmCommandArgumentParserHelper::LexInput(char* buf, int maxlen)
314 {
315   if ( maxlen < 1 )
316     {
317     return 0;
318     }
319   if ( this->InputBufferPos < this->InputBuffer.size() )
320     {
321     buf[0] = this->InputBuffer[ this->InputBufferPos++ ];
322     if ( buf[0] == '\n' )
323       {
324       this->CurrentLine ++;
325       }
326     return(1);
327     }
328   else
329     {
330     buf[0] = '\n';
331     return( 0 );
332     }
333 }
334
335 void cmCommandArgumentParserHelper::Error(const char* str)
336 {
337   unsigned long pos = static_cast<unsigned long>(this->InputBufferPos);
338   cmOStringStream ostr;
339   ostr << str << " (" << pos << ")";
340   this->SetError(ostr.str());
341 }
342
343 void cmCommandArgumentParserHelper::SetMakefile(const cmMakefile* mf)
344 {
345   this->Makefile = mf;
346   this->WarnUninitialized = mf->GetCMakeInstance()->GetWarnUninitialized();
347   this->CheckSystemVars = mf->GetCMakeInstance()->GetCheckSystemVars();
348 }
349
350 void cmCommandArgumentParserHelper::SetResult(const char* value)
351 {
352   if ( !value )
353     {
354     this->Result = "";
355     return;
356     }
357   this->Result = value;
358 }
359
360 void cmCommandArgumentParserHelper::SetError(std::string const& msg)
361 {
362   // Keep only the first error.
363   if(this->ErrorString.empty())
364     {
365     this->ErrorString = msg;
366     }
367 }