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