]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/configuration.cc
try to avoid direct usage of .Fd() if possible and do read()s and co
[apt.git] / apt-pkg / contrib / configuration.cc
CommitLineData
6c139d6e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
5e14c452 3// $Id: configuration.cc,v 1.28 2004/04/30 04:00:15 mdz Exp $
6c139d6e
AL
4/* ######################################################################
5
6 Configuration Class
7
8 This class provides a configuration file and command line parser
9 for a tree-oriented configuration environment. All runtime configuration
10 is stored in here.
7da2b375
AL
11
12 This source is placed in the Public Domain, do with it what you will
13 It was originally written by Jason Gunthorpe <jgg@debian.org>.
6c139d6e
AL
14
15 ##################################################################### */
16 /*}}}*/
17// Include files /*{{{*/
ea542140
DK
18#include <config.h>
19
094a497d 20#include <apt-pkg/configuration.h>
08e8f724 21#include <apt-pkg/error.h>
cdcc6d34 22#include <apt-pkg/strutl.h>
b2e465d6 23#include <apt-pkg/fileutl.h>
6c139d6e 24
b2e465d6 25#include <vector>
b2e465d6 26#include <fstream>
851a45a8 27#include <iostream>
851a45a8 28
ea542140
DK
29#include <apti18n.h>
30
851a45a8 31using namespace std;
6c139d6e 32 /*}}}*/
9c14e3d6
AL
33
34Configuration *_config = new Configuration;
6c139d6e
AL
35
36// Configuration::Configuration - Constructor /*{{{*/
37// ---------------------------------------------------------------------
38/* */
b2e465d6 39Configuration::Configuration() : ToFree(true)
6c139d6e
AL
40{
41 Root = new Item;
b2e465d6
AL
42}
43Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
44{
45};
46
47 /*}}}*/
48// Configuration::~Configuration - Destructor /*{{{*/
49// ---------------------------------------------------------------------
50/* */
51Configuration::~Configuration()
52{
53 if (ToFree == false)
54 return;
55
56 Item *Top = Root;
57 for (; Top != 0;)
58 {
59 if (Top->Child != 0)
60 {
61 Top = Top->Child;
62 continue;
63 }
64
65 while (Top != 0 && Top->Next == 0)
66 {
67 Item *Parent = Top->Parent;
68 delete Top;
69 Top = Parent;
70 }
71 if (Top != 0)
72 {
73 Item *Next = Top->Next;
74 delete Top;
75 Top = Next;
76 }
77 }
6c139d6e
AL
78}
79 /*}}}*/
0a8a80e5 80// Configuration::Lookup - Lookup a single item /*{{{*/
6c139d6e
AL
81// ---------------------------------------------------------------------
82/* This will lookup a single item by name below another item. It is a
83 helper function for the main lookup function */
84Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
ce857f32 85 unsigned long const &Len,bool const &Create)
6c139d6e
AL
86{
87 int Res = 1;
88 Item *I = Head->Child;
89 Item **Last = &Head->Child;
9c14e3d6 90
7f25bdff
AL
91 // Empty strings match nothing. They are used for lists.
92 if (Len != 0)
93 {
94 for (; I != 0; Last = &I->Next, I = I->Next)
851a45a8 95 if ((Res = stringcasecmp(I->Tag,S,S + Len)) == 0)
7f25bdff
AL
96 break;
97 }
98 else
99 for (; I != 0; Last = &I->Next, I = I->Next);
100
6c139d6e
AL
101 if (Res == 0)
102 return I;
103 if (Create == false)
104 return 0;
105
106 I = new Item;
171c75f1 107 I->Tag.assign(S,Len);
6c139d6e 108 I->Next = *Last;
9c14e3d6 109 I->Parent = Head;
6c139d6e
AL
110 *Last = I;
111 return I;
112}
113 /*}}}*/
114// Configuration::Lookup - Lookup a fully scoped item /*{{{*/
115// ---------------------------------------------------------------------
116/* This performs a fully scoped lookup of a given name, possibly creating
117 new items */
ce857f32 118Configuration::Item *Configuration::Lookup(const char *Name,bool const &Create)
6c139d6e 119{
0a8a80e5
AL
120 if (Name == 0)
121 return Root->Child;
122
6c139d6e
AL
123 const char *Start = Name;
124 const char *End = Start + strlen(Name);
125 const char *TagEnd = Name;
126 Item *Itm = Root;
7f25bdff 127 for (; End - TagEnd >= 2; TagEnd++)
6c139d6e
AL
128 {
129 if (TagEnd[0] == ':' && TagEnd[1] == ':')
130 {
131 Itm = Lookup(Itm,Start,TagEnd - Start,Create);
132 if (Itm == 0)
133 return 0;
134 TagEnd = Start = TagEnd + 2;
135 }
136 }
137
7f25bdff
AL
138 // This must be a trailing ::, we create unique items in a list
139 if (End - Start == 0)
140 {
141 if (Create == false)
142 return 0;
143 }
144
6c139d6e 145 Itm = Lookup(Itm,Start,End - Start,Create);
6c139d6e
AL
146 return Itm;
147}
148 /*}}}*/
149// Configuration::Find - Find a value /*{{{*/
150// ---------------------------------------------------------------------
151/* */
b2e465d6 152string Configuration::Find(const char *Name,const char *Default) const
6c139d6e 153{
b2e465d6 154 const Item *Itm = Lookup(Name);
6c139d6e 155 if (Itm == 0 || Itm->Value.empty() == true)
9c14e3d6
AL
156 {
157 if (Default == 0)
171c75f1 158 return "";
9c14e3d6
AL
159 else
160 return Default;
161 }
162
6c139d6e
AL
163 return Itm->Value;
164}
165 /*}}}*/
3b5421b4 166// Configuration::FindFile - Find a Filename /*{{{*/
9c14e3d6
AL
167// ---------------------------------------------------------------------
168/* Directories are stored as the base dir in the Parent node and the
3b5421b4 169 sub directory in sub nodes with the final node being the end filename
9c14e3d6 170 */
b2e465d6 171string Configuration::FindFile(const char *Name,const char *Default) const
9c14e3d6 172{
db2cca11
OS
173 const Item *RootItem = Lookup("RootDir");
174 std::string rootDir = (RootItem == 0) ? "" : RootItem->Value;
175 if(rootDir.size() > 0 && rootDir[rootDir.size() - 1] != '/')
176 rootDir.push_back('/');
177
b2e465d6 178 const Item *Itm = Lookup(Name);
9c14e3d6
AL
179 if (Itm == 0 || Itm->Value.empty() == true)
180 {
181 if (Default == 0)
17b3813c 182 return rootDir;
9c14e3d6 183 else
17b3813c 184 return rootDir + Default;
9c14e3d6
AL
185 }
186
b2e465d6
AL
187 string val = Itm->Value;
188 while (Itm->Parent != 0 && Itm->Parent->Value.empty() == false)
189 {
190 // Absolute
191 if (val.length() >= 1 && val[0] == '/')
192 break;
193
194 // ~/foo or ./foo
195 if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
196 break;
197
198 // ../foo
199 if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
200 break;
201
202 if (Itm->Parent->Value.end()[-1] != '/')
203 val.insert(0, "/");
204
205 val.insert(0, Itm->Parent->Value);
206 Itm = Itm->Parent;
207 }
208
db2cca11 209 return rootDir + val;
9c14e3d6
AL
210}
211 /*}}}*/
3b5421b4
AL
212// Configuration::FindDir - Find a directory name /*{{{*/
213// ---------------------------------------------------------------------
214/* This is like findfile execept the result is terminated in a / */
b2e465d6 215string Configuration::FindDir(const char *Name,const char *Default) const
3b5421b4
AL
216{
217 string Res = FindFile(Name,Default);
218 if (Res.end()[-1] != '/')
219 return Res + '/';
220 return Res;
221}
222 /*}}}*/
50c409c4
DK
223// Configuration::FindVector - Find a vector of values /*{{{*/
224// ---------------------------------------------------------------------
225/* Returns a vector of config values under the given item */
226vector<string> Configuration::FindVector(const char *Name) const
227{
228 vector<string> Vec;
229 const Item *Top = Lookup(Name);
230 if (Top == NULL)
231 return Vec;
232
233 Item *I = Top->Child;
234 while(I != NULL)
235 {
236 Vec.push_back(I->Value);
237 I = I->Next;
238 }
239 return Vec;
240}
241 /*}}}*/
6c139d6e
AL
242// Configuration::FindI - Find an integer value /*{{{*/
243// ---------------------------------------------------------------------
244/* */
ce857f32 245int Configuration::FindI(const char *Name,int const &Default) const
6c139d6e 246{
b2e465d6 247 const Item *Itm = Lookup(Name);
6c139d6e
AL
248 if (Itm == 0 || Itm->Value.empty() == true)
249 return Default;
250
251 char *End;
252 int Res = strtol(Itm->Value.c_str(),&End,0);
253 if (End == Itm->Value.c_str())
254 return Default;
255
08e8f724
AL
256 return Res;
257}
258 /*}}}*/
259// Configuration::FindB - Find a boolean type /*{{{*/
260// ---------------------------------------------------------------------
261/* */
ce857f32 262bool Configuration::FindB(const char *Name,bool const &Default) const
08e8f724 263{
b2e465d6 264 const Item *Itm = Lookup(Name);
08e8f724
AL
265 if (Itm == 0 || Itm->Value.empty() == true)
266 return Default;
267
3b5421b4 268 return StringToBool(Itm->Value,Default);
6c139d6e
AL
269}
270 /*}}}*/
b2e465d6
AL
271// Configuration::FindAny - Find an arbitrary type /*{{{*/
272// ---------------------------------------------------------------------
273/* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
274string Configuration::FindAny(const char *Name,const char *Default) const
275{
276 string key = Name;
277 char type = 0;
278
279 if (key.size() > 2 && key.end()[-2] == '/')
280 {
281 type = key.end()[-1];
282 key.resize(key.size() - 2);
283 }
284
285 switch (type)
286 {
287 // file
288 case 'f':
289 return FindFile(key.c_str(), Default);
290
291 // directory
292 case 'd':
293 return FindDir(key.c_str(), Default);
294
295 // bool
296 case 'b':
297 return FindB(key, Default) ? "true" : "false";
298
299 // int
300 case 'i':
301 {
302 char buf[16];
cc99a102 303 snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
b2e465d6
AL
304 return buf;
305 }
306 }
307
308 // fallback
309 return Find(Name, Default);
310}
311 /*}}}*/
312// Configuration::CndSet - Conditinal Set a value /*{{{*/
313// ---------------------------------------------------------------------
314/* This will not overwrite */
171c75f1 315void Configuration::CndSet(const char *Name,const string &Value)
b2e465d6
AL
316{
317 Item *Itm = Lookup(Name,true);
318 if (Itm == 0)
319 return;
320 if (Itm->Value.empty() == true)
321 Itm->Value = Value;
322}
323 /*}}}*/
78485ab2
DK
324// Configuration::Set - Set an integer value /*{{{*/
325// ---------------------------------------------------------------------
326/* */
327void Configuration::CndSet(const char *Name,int const Value)
328{
329 Item *Itm = Lookup(Name,true);
330 if (Itm == 0 || Itm->Value.empty() == false)
331 return;
332 char S[300];
333 snprintf(S,sizeof(S),"%i",Value);
334 Itm->Value = S;
335}
336 /*}}}*/
6c139d6e
AL
337// Configuration::Set - Set a value /*{{{*/
338// ---------------------------------------------------------------------
339/* */
171c75f1 340void Configuration::Set(const char *Name,const string &Value)
6c139d6e
AL
341{
342 Item *Itm = Lookup(Name,true);
343 if (Itm == 0)
344 return;
345 Itm->Value = Value;
346}
347 /*}}}*/
348// Configuration::Set - Set an integer value /*{{{*/
349// ---------------------------------------------------------------------
350/* */
0df8c3dc 351void Configuration::Set(const char *Name,int const &Value)
6c139d6e
AL
352{
353 Item *Itm = Lookup(Name,true);
354 if (Itm == 0)
355 return;
356 char S[300];
357 snprintf(S,sizeof(S),"%i",Value);
358 Itm->Value = S;
75ef8f14
MV
359}
360 /*}}}*/
361// Configuration::Clear - Clear an single value from a list /*{{{*/
362// ---------------------------------------------------------------------
363/* */
ce857f32 364void Configuration::Clear(string const &Name, int const &Value)
75ef8f14
MV
365{
366 char S[300];
367 snprintf(S,sizeof(S),"%i",Value);
368 Clear(Name, S);
369}
370 /*}}}*/
371// Configuration::Clear - Clear an single value from a list /*{{{*/
372// ---------------------------------------------------------------------
373/* */
ce857f32 374void Configuration::Clear(string const &Name, string const &Value)
75ef8f14
MV
375{
376 Item *Top = Lookup(Name.c_str(),false);
377 if (Top == 0 || Top->Child == 0)
378 return;
379
380 Item *Tmp, *Prev, *I;
381 Prev = I = Top->Child;
382
383 while(I != NULL)
384 {
385 if(I->Value == Value)
386 {
387 Tmp = I;
388 // was first element, point parent to new first element
389 if(Top->Child == Tmp)
390 Top->Child = I->Next;
391 I = I->Next;
392 Prev->Next = I;
393 delete Tmp;
394 } else {
395 Prev = I;
396 I = I->Next;
397 }
398 }
399
6c139d6e
AL
400}
401 /*}}}*/
b2e465d6
AL
402// Configuration::Clear - Clear an entire tree /*{{{*/
403// ---------------------------------------------------------------------
404/* */
ce857f32 405void Configuration::Clear(string const &Name)
b2e465d6
AL
406{
407 Item *Top = Lookup(Name.c_str(),false);
75ef8f14 408 if (Top == 0)
b2e465d6 409 return;
75ef8f14 410
171c75f1 411 Top->Value.clear();
b2e465d6
AL
412 Item *Stop = Top;
413 Top = Top->Child;
414 Stop->Child = 0;
415 for (; Top != 0;)
416 {
417 if (Top->Child != 0)
418 {
419 Top = Top->Child;
420 continue;
421 }
422
423 while (Top != 0 && Top->Next == 0)
424 {
425 Item *Tmp = Top;
426 Top = Top->Parent;
427 delete Tmp;
428
429 if (Top == Stop)
430 return;
431 }
432
433 Item *Tmp = Top;
434 if (Top != 0)
435 Top = Top->Next;
436 delete Tmp;
437 }
438}
439 /*}}}*/
08e8f724
AL
440// Configuration::Exists - Returns true if the Name exists /*{{{*/
441// ---------------------------------------------------------------------
442/* */
b2e465d6 443bool Configuration::Exists(const char *Name) const
08e8f724 444{
b2e465d6 445 const Item *Itm = Lookup(Name);
08e8f724
AL
446 if (Itm == 0)
447 return false;
448 return true;
449}
450 /*}}}*/
b2e465d6
AL
451// Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
452// ---------------------------------------------------------------------
453/* qualified by /[fdbi] exists */
454bool Configuration::ExistsAny(const char *Name) const
455{
456 string key = Name;
457
8d998026 458 if (key.size() > 2 && key.end()[-2] == '/')
ff6bf1be 459 {
8d998026
AL
460 if (key.find_first_of("fdbi",key.size()-1) < key.size())
461 {
462 key.resize(key.size() - 2);
463 if (Exists(key.c_str()))
464 return true;
465 }
466 else
467 {
69a63027 468 _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
8d998026 469 }
ff6bf1be 470 }
b2e465d6
AL
471 return Exists(Name);
472}
473 /*}}}*/
93bf083d
AL
474// Configuration::Dump - Dump the config /*{{{*/
475// ---------------------------------------------------------------------
476/* Dump the entire configuration space */
ff2a211a 477void Configuration::Dump(ostream& str)
93bf083d
AL
478{
479 /* Write out all of the configuration directives by walking the
480 configuration tree */
b2e465d6 481 const Configuration::Item *Top = Tree(0);
93bf083d
AL
482 for (; Top != 0;)
483 {
ff2a211a 484 str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
93bf083d
AL
485
486 if (Top->Child != 0)
487 {
488 Top = Top->Child;
489 continue;
490 }
491
492 while (Top != 0 && Top->Next == 0)
493 Top = Top->Parent;
494 if (Top != 0)
495 Top = Top->Next;
b2e465d6 496 }
93bf083d
AL
497}
498 /*}}}*/
08e8f724 499
0a8a80e5
AL
500// Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
501// ---------------------------------------------------------------------
b2e465d6
AL
502/* Stop sets an optional max recursion depth if this item is being viewed as
503 part of a sub tree. */
504string Configuration::Item::FullTag(const Item *Stop) const
0a8a80e5 505{
b2e465d6 506 if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
0a8a80e5 507 return Tag;
b2e465d6 508 return Parent->FullTag(Stop) + "::" + Tag;
0a8a80e5
AL
509}
510 /*}}}*/
511
08e8f724
AL
512// ReadConfigFile - Read a configuration file /*{{{*/
513// ---------------------------------------------------------------------
514/* The configuration format is very much like the named.conf format
b2e465d6
AL
515 used in bind8, in fact this routine can parse most named.conf files.
516 Sectional config files are like bind's named.conf where there are
517 sections like 'zone "foo.org" { .. };' This causes each section to be
518 added in with a tag like "zone::foo.org" instead of being split
6df63aa6 519 tag/value. AsSectional enables Sectional parsing.*/
ce857f32
DK
520bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
521 unsigned const &Depth)
b2e465d6 522{
08e8f724 523 // Open the stream for reading
851a45a8 524 ifstream F(FName.c_str(),ios::in);
08e8f724 525 if (!F != 0)
b2e465d6 526 return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
3f807f6c 527
08e8f724 528 string LineBuffer;
a4e87467
AL
529 string Stack[100];
530 unsigned int StackPos = 0;
08e8f724
AL
531
532 // Parser state
533 string ParentTag;
534
535 int CurLine = 0;
536 bool InComment = false;
537 while (F.eof() == false)
538 {
3f807f6c
DB
539 // The raw input line.
540 std::string Input;
541 // The input line with comments stripped.
542 std::string Fragment;
8a27f5da
DB
543
544 // Grab the next line of F and place it in Input.
3f807f6c
DB
545 do
546 {
547 char *Buffer = new char[1024];
548
549 F.clear();
550 F.getline(Buffer,sizeof(Buffer) / 2);
551
552 Input += Buffer;
8aea8c3f 553 delete[] Buffer;
3f807f6c
DB
554 }
555 while (F.fail() && !F.eof());
556
8a27f5da
DB
557 // Expand tabs in the input line and remove leading and trailing
558 // whitespace.
3f807f6c 559 {
3f807f6c
DB
560 const int BufferSize = Input.size() * 8 + 1;
561 char *Buffer = new char[BufferSize];
562 try
563 {
564 memcpy(Buffer, Input.c_str(), Input.size() + 1);
565
566 _strtabexpand(Buffer, BufferSize);
567 _strstrip(Buffer);
568 Input = Buffer;
569 }
570 catch(...)
571 {
572 delete[] Buffer;
573 throw;
574 }
575 delete[] Buffer;
576 }
08e8f724 577 CurLine++;
5e14c452 578
8a27f5da
DB
579 // Now strip comments; if the whole line is contained in a
580 // comment, skip this line.
581
3f807f6c
DB
582 // The first meaningful character in the current fragment; will
583 // be adjusted below as we remove bytes from the front.
584 std::string::const_iterator Start = Input.begin();
585 // The last meaningful character in the current fragment.
586 std::string::const_iterator End = Input.end();
08e8f724
AL
587
588 // Multi line comment
589 if (InComment == true)
590 {
3f807f6c
DB
591 for (std::string::const_iterator I = Start;
592 I != End; ++I)
08e8f724 593 {
3f807f6c 594 if (*I == '*' && I + 1 != End && I[1] == '/')
08e8f724 595 {
3f807f6c 596 Start = I + 2;
08e8f724
AL
597 InComment = false;
598 break;
599 }
600 }
601 if (InComment == true)
602 continue;
603 }
604
605 // Discard single line comments
bfd22fc0 606 bool InQuote = false;
3f807f6c
DB
607 for (std::string::const_iterator I = Start;
608 I != End; ++I)
08e8f724 609 {
bfd22fc0
AL
610 if (*I == '"')
611 InQuote = !InQuote;
612 if (InQuote == true)
613 continue;
c914647f
DK
614
615 if ((*I == '/' && I + 1 != End && I[1] == '/') ||
616 (*I == '#' && strcmp(string(I,I+6).c_str(),"#clear") != 0 &&
617 strcmp(string(I,I+8).c_str(),"#include") != 0))
618 {
3f807f6c 619 End = I;
08e8f724
AL
620 break;
621 }
622 }
7834cb57 623
3f807f6c
DB
624 // Look for multi line comments and build up the
625 // fragment.
626 Fragment.reserve(End - Start);
7834cb57 627 InQuote = false;
3f807f6c
DB
628 for (std::string::const_iterator I = Start;
629 I != End; ++I)
08e8f724 630 {
bfd22fc0
AL
631 if (*I == '"')
632 InQuote = !InQuote;
633 if (InQuote == true)
3f807f6c
DB
634 Fragment.push_back(*I);
635 else if (*I == '/' && I + 1 != End && I[1] == '*')
08e8f724
AL
636 {
637 InComment = true;
3f807f6c
DB
638 for (std::string::const_iterator J = I;
639 J != End; ++J)
08e8f724 640 {
3f807f6c 641 if (*J == '*' && J + 1 != End && J[1] == '/')
08e8f724 642 {
3f807f6c
DB
643 // Pretend we just finished walking over the
644 // comment, and don't add anything to the output
645 // fragment.
646 I = J + 1;
08e8f724
AL
647 InComment = false;
648 break;
649 }
650 }
651
652 if (InComment == true)
3f807f6c 653 break;
08e8f724 654 }
3f807f6c
DB
655 else
656 Fragment.push_back(*I);
08e8f724 657 }
3f807f6c
DB
658
659 // Skip blank lines.
660 if (Fragment.empty())
08e8f724
AL
661 continue;
662
8a27f5da 663 // The line has actual content; interpret what it means.
7834cb57 664 InQuote = false;
3f807f6c
DB
665 Start = Fragment.begin();
666 End = Fragment.end();
667 for (std::string::const_iterator I = Start;
668 I != End; ++I)
08e8f724 669 {
7834cb57
AL
670 if (*I == '"')
671 InQuote = !InQuote;
672
673 if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
08e8f724 674 {
b2e465d6 675 // Put the last fragment into the buffer
a4dcbf1c
DB
676 std::string::const_iterator NonWhitespaceStart = Start;
677 std::string::const_iterator NonWhitespaceStop = I;
f7f0d6c7 678 for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; ++NonWhitespaceStart)
3f807f6c 679 ;
f7f0d6c7 680 for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; --NonWhitespaceStop)
3f807f6c 681 ;
a4dcbf1c 682 if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
08e8f724 683 LineBuffer += ' ';
a4dcbf1c 684 LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);
3f807f6c
DB
685
686 // Drop this from the input string, saving the character
687 // that terminated the construct we just closed. (i.e., a
688 // brace or a semicolon)
08e8f724 689 char TermChar = *I;
3f807f6c 690 Start = I + 1;
08e8f724
AL
691
692 // Syntax Error
693 if (TermChar == '{' && LineBuffer.empty() == true)
b2e465d6 694 return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
08e8f724 695
b2e465d6 696 // No string on this line
08e8f724 697 if (LineBuffer.empty() == true)
b2e465d6
AL
698 {
699 if (TermChar == '}')
700 {
701 if (StackPos == 0)
702 ParentTag = string();
703 else
704 ParentTag = Stack[--StackPos];
705 }
08e8f724 706 continue;
b2e465d6
AL
707 }
708
08e8f724 709 // Parse off the tag
7f25bdff
AL
710 string Tag;
711 const char *Pos = LineBuffer.c_str();
712 if (ParseQuoteWord(Pos,Tag) == false)
db0db9fe 713 return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
b2e465d6
AL
714
715 // Parse off the word
716 string Word;
bcae6dd4 717 bool NoWord = false;
b2e465d6
AL
718 if (ParseCWord(Pos,Word) == false &&
719 ParseQuoteWord(Pos,Word) == false)
720 {
721 if (TermChar != '{')
722 {
723 Word = Tag;
724 Tag = "";
bcae6dd4 725 }
4ae405e9
AL
726 else
727 NoWord = true;
b2e465d6
AL
728 }
729 if (strlen(Pos) != 0)
730 return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
731
08e8f724
AL
732 // Go down a level
733 if (TermChar == '{')
734 {
a4e87467
AL
735 if (StackPos <= 100)
736 Stack[StackPos++] = ParentTag;
b2e465d6
AL
737
738 /* Make sectional tags incorperate the section into the
739 tag string */
740 if (AsSectional == true && Word.empty() == false)
741 {
742 Tag += "::" ;
743 Tag += Word;
744 Word = "";
745 }
746
08e8f724
AL
747 if (ParentTag.empty() == true)
748 ParentTag = Tag;
749 else
750 ParentTag += string("::") + Tag;
751 Tag = string();
752 }
bfd22fc0 753
08e8f724
AL
754 // Generate the item name
755 string Item;
756 if (ParentTag.empty() == true)
757 Item = Tag;
758 else
759 {
7f25bdff 760 if (TermChar != '{' || Tag.empty() == false)
08e8f724 761 Item = ParentTag + "::" + Tag;
7f25bdff
AL
762 else
763 Item = ParentTag;
08e8f724
AL
764 }
765
b2e465d6
AL
766 // Specials
767 if (Tag.length() >= 1 && Tag[0] == '#')
768 {
769 if (ParentTag.empty() == false)
770 return _error->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName.c_str(),CurLine);
771 Tag.erase(Tag.begin());
772 if (Tag == "clear")
773 Conf.Clear(Word);
774 else if (Tag == "include")
775 {
776 if (Depth > 10)
777 return _error->Error(_("Syntax error %s:%u: Too many nested includes"),FName.c_str(),CurLine);
778 if (Word.length() > 2 && Word.end()[-1] == '/')
779 {
780 if (ReadConfigDir(Conf,Word,AsSectional,Depth+1) == false)
781 return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
782 }
783 else
784 {
785 if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
786 return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
787 }
788 }
789 else
790 return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
791 }
c3a3a1b1
DK
792 else if (Tag.empty() == true && NoWord == false && Word == "#clear")
793 return _error->Error(_("Syntax error %s:%u: clear directive requires an option tree as argument"),FName.c_str(),CurLine);
b2e465d6
AL
794 else
795 {
796 // Set the item in the configuration class
bcae6dd4
AL
797 if (NoWord == false)
798 Conf.Set(Item,Word);
b2e465d6
AL
799 }
800
08e8f724 801 // Empty the buffer
171c75f1 802 LineBuffer.clear();
b2e465d6
AL
803
804 // Move up a tag, but only if there is no bit to parse
805 if (TermChar == '}')
806 {
807 if (StackPos == 0)
171c75f1 808 ParentTag.clear();
b2e465d6
AL
809 else
810 ParentTag = Stack[--StackPos];
811 }
812
08e8f724 813 }
08e8f724
AL
814 }
815
3f807f6c
DB
816 // Store the remaining text, if any, in the current line buffer.
817
818 // NB: could change this to use string-based operations; I'm
819 // using strstrip now to ensure backwards compatibility.
820 // -- dburrows 2008-04-01
821 {
822 char *Buffer = new char[End - Start + 1];
823 try
824 {
825 std::copy(Start, End, Buffer);
826 Buffer[End - Start] = '\0';
827
828 const char *Stripd = _strstrip(Buffer);
829 if (*Stripd != 0 && LineBuffer.empty() == false)
830 LineBuffer += " ";
831 LineBuffer += Stripd;
832 }
833 catch(...)
834 {
835 delete[] Buffer;
836 throw;
837 }
838 delete[] Buffer;
839 }
08e8f724 840 }
b2e465d6
AL
841
842 if (LineBuffer.empty() == false)
843 return _error->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName.c_str(),CurLine);
844 return true;
845}
846 /*}}}*/
847// ReadConfigDir - Read a directory of config files /*{{{*/
848// ---------------------------------------------------------------------
849/* */
a74cd17a 850bool ReadConfigDir(Configuration &Conf,const string &Dir,
be4eec61 851 bool const &AsSectional, unsigned const &Depth)
52643bec 852{
e29a6bb1 853 vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true);
b2e465d6
AL
854
855 // Read the files
f7f0d6c7 856 for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
b2e465d6
AL
857 if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
858 return false;
08e8f724
AL
859 return true;
860}
861 /*}}}*/
1f2933a8
DK
862// MatchAgainstConfig Constructor /*{{{*/
863Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
864{
865 std::vector<std::string> const strings = _config->FindVector(Config);
866 for (std::vector<std::string>::const_iterator s = strings.begin();
867 s != strings.end(); ++s)
868 {
869 regex_t *p = new regex_t;
870 if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0)
871 patterns.push_back(p);
872 else
873 {
874 regfree(p);
875 delete p;
44edc41e
MV
876 _error->Warning("Invalid regular expression '%s' in configuration "
877 "option '%s' will be ignored.",
878 s->c_str(), Config);
879 continue;
1f2933a8 880 }
b093a199
DK
881 }
882 if (strings.size() == 0)
883 patterns.push_back(NULL);
1f2933a8
DK
884}
885 /*}}}*/
886// MatchAgainstConfig Destructor /*{{{*/
887Configuration::MatchAgainstConfig::~MatchAgainstConfig()
b093a199
DK
888{
889 clearPatterns();
890}
891void Configuration::MatchAgainstConfig::clearPatterns()
1f2933a8
DK
892{
893 for(std::vector<regex_t *>::const_iterator p = patterns.begin();
894 p != patterns.end(); ++p)
895 {
b093a199 896 if (*p == NULL) continue;
1f2933a8
DK
897 regfree(*p);
898 delete *p;
899 }
44edc41e 900 patterns.clear();
1f2933a8
DK
901}
902 /*}}}*/
903// MatchAgainstConfig::Match - returns true if a pattern matches /*{{{*/
904bool Configuration::MatchAgainstConfig::Match(char const * str) const
905{
906 for(std::vector<regex_t *>::const_iterator p = patterns.begin();
907 p != patterns.end(); ++p)
b093a199 908 if (*p != NULL && regexec(*p, str, 0, 0, 0) == 0)
1f2933a8
DK
909 return true;
910
911 return false;
912}
913 /*}}}*/