reorder includes: add <config.h> if needed and include it at first
[ntk/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 rootDir = (RootItem == 0) ? "" : RootItem->Value;
175 if(rootDir.size() > 0 && rootDir[rootDir.size() - 1] != '/')
176 rootDir.push_back('/');
177
178 const Item *Itm = Lookup(Name);
179 if (Itm == 0 || Itm->Value.empty() == true)
180 {
181 if (Default == 0)
182 return rootDir;
183 else
184 return rootDir + Default;
185 }
186
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
209 return rootDir + val;
210 }
211 /*}}}*/
212 // Configuration::FindDir - Find a directory name /*{{{*/
213 // ---------------------------------------------------------------------
214 /* This is like findfile execept the result is terminated in a / */
215 string Configuration::FindDir(const char *Name,const char *Default) const
216 {
217 string Res = FindFile(Name,Default);
218 if (Res.end()[-1] != '/')
219 return Res + '/';
220 return Res;
221 }
222 /*}}}*/
223 // Configuration::FindVector - Find a vector of values /*{{{*/
224 // ---------------------------------------------------------------------
225 /* Returns a vector of config values under the given item */
226 vector<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 /*}}}*/
242 // Configuration::FindI - Find an integer value /*{{{*/
243 // ---------------------------------------------------------------------
244 /* */
245 int Configuration::FindI(const char *Name,int const &Default) const
246 {
247 const Item *Itm = Lookup(Name);
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
256 return Res;
257 }
258 /*}}}*/
259 // Configuration::FindB - Find a boolean type /*{{{*/
260 // ---------------------------------------------------------------------
261 /* */
262 bool Configuration::FindB(const char *Name,bool const &Default) const
263 {
264 const Item *Itm = Lookup(Name);
265 if (Itm == 0 || Itm->Value.empty() == true)
266 return Default;
267
268 return StringToBool(Itm->Value,Default);
269 }
270 /*}}}*/
271 // Configuration::FindAny - Find an arbitrary type /*{{{*/
272 // ---------------------------------------------------------------------
273 /* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
274 string 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];
303 snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
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 */
315 void Configuration::CndSet(const char *Name,const string &Value)
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 /*}}}*/
324 // Configuration::Set - Set an integer value /*{{{*/
325 // ---------------------------------------------------------------------
326 /* */
327 void 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 /*}}}*/
337 // Configuration::Set - Set a value /*{{{*/
338 // ---------------------------------------------------------------------
339 /* */
340 void Configuration::Set(const char *Name,const string &Value)
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 /* */
351 void Configuration::Set(const char *Name,int const Value)
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;
359 }
360 /*}}}*/
361 // Configuration::Clear - Clear an single value from a list /*{{{*/
362 // ---------------------------------------------------------------------
363 /* */
364 void Configuration::Clear(string const &Name, int const &Value)
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 /* */
374 void Configuration::Clear(string const &Name, string const &Value)
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
400 }
401 /*}}}*/
402 // Configuration::Clear - Clear an entire tree /*{{{*/
403 // ---------------------------------------------------------------------
404 /* */
405 void Configuration::Clear(string const &Name)
406 {
407 Item *Top = Lookup(Name.c_str(),false);
408 if (Top == 0)
409 return;
410
411 Top->Value.clear();
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 /*}}}*/
440 // Configuration::Exists - Returns true if the Name exists /*{{{*/
441 // ---------------------------------------------------------------------
442 /* */
443 bool Configuration::Exists(const char *Name) const
444 {
445 const Item *Itm = Lookup(Name);
446 if (Itm == 0)
447 return false;
448 return true;
449 }
450 /*}}}*/
451 // Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
452 // ---------------------------------------------------------------------
453 /* qualified by /[fdbi] exists */
454 bool Configuration::ExistsAny(const char *Name) const
455 {
456 string key = Name;
457
458 if (key.size() > 2 && key.end()[-2] == '/')
459 {
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 {
468 _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
469 }
470 }
471 return Exists(Name);
472 }
473 /*}}}*/
474 // Configuration::Dump - Dump the config /*{{{*/
475 // ---------------------------------------------------------------------
476 /* Dump the entire configuration space */
477 void Configuration::Dump(ostream& str)
478 {
479 /* Write out all of the configuration directives by walking the
480 configuration tree */
481 const Configuration::Item *Top = Tree(0);
482 for (; Top != 0;)
483 {
484 str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
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;
496 }
497 }
498 /*}}}*/
499
500 // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
501 // ---------------------------------------------------------------------
502 /* Stop sets an optional max recursion depth if this item is being viewed as
503 part of a sub tree. */
504 string Configuration::Item::FullTag(const Item *Stop) const
505 {
506 if (Parent == 0 || Parent->Parent == 0 || Parent == Stop)
507 return Tag;
508 return Parent->FullTag(Stop) + "::" + Tag;
509 }
510 /*}}}*/
511
512 // ReadConfigFile - Read a configuration file /*{{{*/
513 // ---------------------------------------------------------------------
514 /* The configuration format is very much like the named.conf format
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
519 tag/value. AsSectional enables Sectional parsing.*/
520 bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
521 unsigned const &Depth)
522 {
523 // Open the stream for reading
524 ifstream F(FName.c_str(),ios::in);
525 if (!F != 0)
526 return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
527
528 string LineBuffer;
529 string Stack[100];
530 unsigned int StackPos = 0;
531
532 // Parser state
533 string ParentTag;
534
535 int CurLine = 0;
536 bool InComment = false;
537 while (F.eof() == false)
538 {
539 // The raw input line.
540 std::string Input;
541 // The input line with comments stripped.
542 std::string Fragment;
543
544 // Grab the next line of F and place it in Input.
545 do
546 {
547 char *Buffer = new char[1024];
548
549 F.clear();
550 F.getline(Buffer,sizeof(Buffer) / 2);
551
552 Input += Buffer;
553 delete[] Buffer;
554 }
555 while (F.fail() && !F.eof());
556
557 // Expand tabs in the input line and remove leading and trailing
558 // whitespace.
559 {
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 }
577 CurLine++;
578
579 // Now strip comments; if the whole line is contained in a
580 // comment, skip this line.
581
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();
587
588 // Multi line comment
589 if (InComment == true)
590 {
591 for (std::string::const_iterator I = Start;
592 I != End; ++I)
593 {
594 if (*I == '*' && I + 1 != End && I[1] == '/')
595 {
596 Start = I + 2;
597 InComment = false;
598 break;
599 }
600 }
601 if (InComment == true)
602 continue;
603 }
604
605 // Discard single line comments
606 bool InQuote = false;
607 for (std::string::const_iterator I = Start;
608 I != End; ++I)
609 {
610 if (*I == '"')
611 InQuote = !InQuote;
612 if (InQuote == true)
613 continue;
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 {
619 End = I;
620 break;
621 }
622 }
623
624 // Look for multi line comments and build up the
625 // fragment.
626 Fragment.reserve(End - Start);
627 InQuote = false;
628 for (std::string::const_iterator I = Start;
629 I != End; ++I)
630 {
631 if (*I == '"')
632 InQuote = !InQuote;
633 if (InQuote == true)
634 Fragment.push_back(*I);
635 else if (*I == '/' && I + 1 != End && I[1] == '*')
636 {
637 InComment = true;
638 for (std::string::const_iterator J = I;
639 J != End; ++J)
640 {
641 if (*J == '*' && J + 1 != End && J[1] == '/')
642 {
643 // Pretend we just finished walking over the
644 // comment, and don't add anything to the output
645 // fragment.
646 I = J + 1;
647 InComment = false;
648 break;
649 }
650 }
651
652 if (InComment == true)
653 break;
654 }
655 else
656 Fragment.push_back(*I);
657 }
658
659 // Skip blank lines.
660 if (Fragment.empty())
661 continue;
662
663 // The line has actual content; interpret what it means.
664 InQuote = false;
665 Start = Fragment.begin();
666 End = Fragment.end();
667 for (std::string::const_iterator I = Start;
668 I != End; ++I)
669 {
670 if (*I == '"')
671 InQuote = !InQuote;
672
673 if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
674 {
675 // Put the last fragment into the buffer
676 std::string::const_iterator NonWhitespaceStart = Start;
677 std::string::const_iterator NonWhitespaceStop = I;
678 for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; NonWhitespaceStart++)
679 ;
680 for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; NonWhitespaceStop--)
681 ;
682 if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
683 LineBuffer += ' ';
684 LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);
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)
689 char TermChar = *I;
690 Start = I + 1;
691
692 // Syntax Error
693 if (TermChar == '{' && LineBuffer.empty() == true)
694 return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
695
696 // No string on this line
697 if (LineBuffer.empty() == true)
698 {
699 if (TermChar == '}')
700 {
701 if (StackPos == 0)
702 ParentTag = string();
703 else
704 ParentTag = Stack[--StackPos];
705 }
706 continue;
707 }
708
709 // Parse off the tag
710 string Tag;
711 const char *Pos = LineBuffer.c_str();
712 if (ParseQuoteWord(Pos,Tag) == false)
713 return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
714
715 // Parse off the word
716 string Word;
717 bool NoWord = false;
718 if (ParseCWord(Pos,Word) == false &&
719 ParseQuoteWord(Pos,Word) == false)
720 {
721 if (TermChar != '{')
722 {
723 Word = Tag;
724 Tag = "";
725 }
726 else
727 NoWord = true;
728 }
729 if (strlen(Pos) != 0)
730 return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
731
732 // Go down a level
733 if (TermChar == '{')
734 {
735 if (StackPos <= 100)
736 Stack[StackPos++] = ParentTag;
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
747 if (ParentTag.empty() == true)
748 ParentTag = Tag;
749 else
750 ParentTag += string("::") + Tag;
751 Tag = string();
752 }
753
754 // Generate the item name
755 string Item;
756 if (ParentTag.empty() == true)
757 Item = Tag;
758 else
759 {
760 if (TermChar != '{' || Tag.empty() == false)
761 Item = ParentTag + "::" + Tag;
762 else
763 Item = ParentTag;
764 }
765
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 }
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);
794 else
795 {
796 // Set the item in the configuration class
797 if (NoWord == false)
798 Conf.Set(Item,Word);
799 }
800
801 // Empty the buffer
802 LineBuffer.clear();
803
804 // Move up a tag, but only if there is no bit to parse
805 if (TermChar == '}')
806 {
807 if (StackPos == 0)
808 ParentTag.clear();
809 else
810 ParentTag = Stack[--StackPos];
811 }
812
813 }
814 }
815
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 }
840 }
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 /* */
850 bool ReadConfigDir(Configuration &Conf,const string &Dir,
851 bool const &AsSectional, unsigned const &Depth)
852 {
853 vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true);
854
855 // Read the files
856 for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
857 if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
858 return false;
859 return true;
860 }
861 /*}}}*/
862 // MatchAgainstConfig Constructor /*{{{*/
863 Configuration::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;
876 clearPatterns();
877 _error->Warning("Regex compilation error for '%s' in configuration option '%s'",
878 s->c_str(), Config);
879 return;
880 }
881 }
882 if (strings.size() == 0)
883 patterns.push_back(NULL);
884 }
885 /*}}}*/
886 // MatchAgainstConfig Destructor /*{{{*/
887 Configuration::MatchAgainstConfig::~MatchAgainstConfig()
888 {
889 clearPatterns();
890 }
891 void Configuration::MatchAgainstConfig::clearPatterns()
892 {
893 for(std::vector<regex_t *>::const_iterator p = patterns.begin();
894 p != patterns.end(); ++p)
895 {
896 if (*p == NULL) continue;
897 regfree(*p);
898 delete *p;
899 }
900 }
901 /*}}}*/
902 // MatchAgainstConfig::Match - returns true if a pattern matches /*{{{*/
903 bool Configuration::MatchAgainstConfig::Match(char const * str) const
904 {
905 for(std::vector<regex_t *>::const_iterator p = patterns.begin();
906 p != patterns.end(); ++p)
907 if (*p != NULL && regexec(*p, str, 0, 0, 0) == 0)
908 return true;
909
910 return false;
911 }
912 /*}}}*/