]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/configuration.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: configuration.cc,v 1.28 2004/04/30 04:00:15 mdz Exp $
4 /* ######################################################################
8 This class provides a configuration file and command line parser
9 for a tree-oriented configuration environment. All runtime configuration
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>.
15 ##################################################################### */
17 // Include files /*{{{*/
19 #pragma implementation "apt-pkg/configuration.h"
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/fileutl.h>
40 Configuration
*_config
= new Configuration
;
42 // Configuration::Configuration - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
45 Configuration::Configuration() : ToFree(true)
49 Configuration::Configuration(const Item
*Root
) : Root((Item
*)Root
), ToFree(false)
54 // Configuration::~Configuration - Destructor /*{{{*/
55 // ---------------------------------------------------------------------
57 Configuration::~Configuration()
71 while (Top
!= 0 && Top
->Next
== 0)
73 Item
*Parent
= Top
->Parent
;
79 Item
*Next
= Top
->Next
;
86 // Configuration::Lookup - Lookup a single item /*{{{*/
87 // ---------------------------------------------------------------------
88 /* This will lookup a single item by name below another item. It is a
89 helper function for the main lookup function */
90 Configuration::Item
*Configuration::Lookup(Item
*Head
,const char *S
,
91 unsigned long Len
,bool Create
)
94 Item
*I
= Head
->Child
;
95 Item
**Last
= &Head
->Child
;
97 // Empty strings match nothing. They are used for lists.
100 for (; I
!= 0; Last
= &I
->Next
, I
= I
->Next
)
101 if ((Res
= stringcasecmp(I
->Tag
,S
,S
+ Len
)) == 0)
105 for (; I
!= 0; Last
= &I
->Next
, I
= I
->Next
);
113 I
->Tag
= string(S
,Len
);
120 // Configuration::Lookup - Lookup a fully scoped item /*{{{*/
121 // ---------------------------------------------------------------------
122 /* This performs a fully scoped lookup of a given name, possibly creating
124 Configuration::Item
*Configuration::Lookup(const char *Name
,bool Create
)
129 const char *Start
= Name
;
130 const char *End
= Start
+ strlen(Name
);
131 const char *TagEnd
= Name
;
133 for (; End
- TagEnd
>= 2; TagEnd
++)
135 if (TagEnd
[0] == ':' && TagEnd
[1] == ':')
137 Itm
= Lookup(Itm
,Start
,TagEnd
- Start
,Create
);
140 TagEnd
= Start
= TagEnd
+ 2;
144 // This must be a trailing ::, we create unique items in a list
145 if (End
- Start
== 0)
151 Itm
= Lookup(Itm
,Start
,End
- Start
,Create
);
155 // Configuration::Find - Find a value /*{{{*/
156 // ---------------------------------------------------------------------
158 string
Configuration::Find(const char *Name
,const char *Default
) const
160 const Item
*Itm
= Lookup(Name
);
161 if (Itm
== 0 || Itm
->Value
.empty() == true)
172 // Configuration::FindFile - Find a Filename /*{{{*/
173 // ---------------------------------------------------------------------
174 /* Directories are stored as the base dir in the Parent node and the
175 sub directory in sub nodes with the final node being the end filename
177 string
Configuration::FindFile(const char *Name
,const char *Default
) const
179 const Item
*Itm
= Lookup(Name
);
180 if (Itm
== 0 || Itm
->Value
.empty() == true)
188 string val
= Itm
->Value
;
189 while (Itm
->Parent
!= 0 && Itm
->Parent
->Value
.empty() == false)
192 if (val
.length() >= 1 && val
[0] == '/')
196 if (val
.length() >= 2 && (val
[0] == '~' || val
[0] == '.') && val
[1] == '/')
200 if (val
.length() >= 3 && val
[0] == '.' && val
[1] == '.' && val
[2] == '/')
203 if (Itm
->Parent
->Value
.end()[-1] != '/')
206 val
.insert(0, Itm
->Parent
->Value
);
213 // Configuration::FindDir - Find a directory name /*{{{*/
214 // ---------------------------------------------------------------------
215 /* This is like findfile execept the result is terminated in a / */
216 string
Configuration::FindDir(const char *Name
,const char *Default
) const
218 string Res
= FindFile(Name
,Default
);
219 if (Res
.end()[-1] != '/')
224 // Configuration::FindI - Find an integer value /*{{{*/
225 // ---------------------------------------------------------------------
227 int Configuration::FindI(const char *Name
,int Default
) const
229 const Item
*Itm
= Lookup(Name
);
230 if (Itm
== 0 || Itm
->Value
.empty() == true)
234 int Res
= strtol(Itm
->Value
.c_str(),&End
,0);
235 if (End
== Itm
->Value
.c_str())
241 // Configuration::FindB - Find a boolean type /*{{{*/
242 // ---------------------------------------------------------------------
244 bool Configuration::FindB(const char *Name
,bool Default
) const
246 const Item
*Itm
= Lookup(Name
);
247 if (Itm
== 0 || Itm
->Value
.empty() == true)
250 return StringToBool(Itm
->Value
,Default
);
253 // Configuration::FindAny - Find an arbitrary type /*{{{*/
254 // ---------------------------------------------------------------------
255 /* a key suffix of /f, /d, /b or /i calls Find{File,Dir,B,I} */
256 string
Configuration::FindAny(const char *Name
,const char *Default
) const
261 if (key
.size() > 2 && key
.end()[-2] == '/')
263 type
= key
.end()[-1];
264 key
.resize(key
.size() - 2);
271 return FindFile(key
.c_str(), Default
);
275 return FindDir(key
.c_str(), Default
);
279 return FindB(key
, Default
) ? "true" : "false";
285 snprintf(buf
, sizeof(buf
)-1, "%d", FindI(key
, Default
? atoi(Default
) : 0 ));
291 return Find(Name
, Default
);
294 // Configuration::CndSet - Conditinal Set a value /*{{{*/
295 // ---------------------------------------------------------------------
296 /* This will not overwrite */
297 void Configuration::CndSet(const char *Name
,string Value
)
299 Item
*Itm
= Lookup(Name
,true);
302 if (Itm
->Value
.empty() == true)
306 // Configuration::Set - Set a value /*{{{*/
307 // ---------------------------------------------------------------------
309 void Configuration::Set(const char *Name
,string Value
)
311 Item
*Itm
= Lookup(Name
,true);
317 // Configuration::Set - Set an integer value /*{{{*/
318 // ---------------------------------------------------------------------
320 void Configuration::Set(const char *Name
,int Value
)
322 Item
*Itm
= Lookup(Name
,true);
326 snprintf(S
,sizeof(S
),"%i",Value
);
330 // Configuration::Clear - Clear an single value from a list /*{{{*/
331 // ---------------------------------------------------------------------
333 void Configuration::Clear(string Name
, int Value
)
336 snprintf(S
,sizeof(S
),"%i",Value
);
340 // Configuration::Clear - Clear an single value from a list /*{{{*/
341 // ---------------------------------------------------------------------
343 void Configuration::Clear(string Name
, string Value
)
345 Item
*Top
= Lookup(Name
.c_str(),false);
346 if (Top
== 0 || Top
->Child
== 0)
349 Item
*Tmp
, *Prev
, *I
;
350 Prev
= I
= Top
->Child
;
354 if(I
->Value
== Value
)
357 // was first element, point parent to new first element
358 if(Top
->Child
== Tmp
)
359 Top
->Child
= I
->Next
;
371 // Configuration::Clear - Clear an entire tree /*{{{*/
372 // ---------------------------------------------------------------------
374 void Configuration::Clear(string Name
)
376 Item
*Top
= Lookup(Name
.c_str(),false);
380 Top
->Value
= string();
392 while (Top
!= 0 && Top
->Next
== 0)
409 // Configuration::Exists - Returns true if the Name exists /*{{{*/
410 // ---------------------------------------------------------------------
412 bool Configuration::Exists(const char *Name
) const
414 const Item
*Itm
= Lookup(Name
);
420 // Configuration::ExistsAny - Returns true if the Name, possibly /*{{{*/
421 // ---------------------------------------------------------------------
422 /* qualified by /[fdbi] exists */
423 bool Configuration::ExistsAny(const char *Name
) const
427 if (key
.size() > 2 && key
.end()[-2] == '/')
428 if (key
.find_first_of("fdbi",key
.size()-1) < key
.size())
430 key
.resize(key
.size() - 2);
431 if (Exists(key
.c_str()))
436 _error
->Warning(_("Unrecognized type abbreviation: '%c'"), key
.end()[-3]);
442 // Configuration::Dump - Dump the config /*{{{*/
443 // ---------------------------------------------------------------------
444 /* Dump the entire configuration space */
445 void Configuration::Dump(ostream
& str
)
447 /* Write out all of the configuration directives by walking the
448 configuration tree */
449 const Configuration::Item
*Top
= Tree(0);
452 str
<< Top
->FullTag() << " \"" << Top
->Value
<< "\";" << endl
;
460 while (Top
!= 0 && Top
->Next
== 0)
468 // Configuration::Item::FullTag - Return the fully scoped tag /*{{{*/
469 // ---------------------------------------------------------------------
470 /* Stop sets an optional max recursion depth if this item is being viewed as
471 part of a sub tree. */
472 string
Configuration::Item::FullTag(const Item
*Stop
) const
474 if (Parent
== 0 || Parent
->Parent
== 0 || Parent
== Stop
)
476 return Parent
->FullTag(Stop
) + "::" + Tag
;
480 // ReadConfigFile - Read a configuration file /*{{{*/
481 // ---------------------------------------------------------------------
482 /* The configuration format is very much like the named.conf format
483 used in bind8, in fact this routine can parse most named.conf files.
484 Sectional config files are like bind's named.conf where there are
485 sections like 'zone "foo.org" { .. };' This causes each section to be
486 added in with a tag like "zone::foo.org" instead of being split
487 tag/value. AsSectional enables Sectional parsing.*/
488 bool ReadConfigFile(Configuration
&Conf
,string FName
,bool AsSectional
,
491 // Open the stream for reading
492 ifstream
F(FName
.c_str(),ios::in
);
494 return _error
->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName
.c_str());
499 unsigned int StackPos
= 0;
505 bool InComment
= false;
506 while (F
.eof() == false)
508 F
.getline(Buffer
,sizeof(Buffer
));
510 // This should be made to work instead, but this is better than looping
511 if (F
.fail() && !F
.eof())
512 return _error
->Error(_("Line %d too long (max %d)"), CurLine
, sizeof(Buffer
));
514 _strtabexpand(Buffer
,sizeof(Buffer
));
517 // Multi line comment
518 if (InComment
== true)
520 for (const char *I
= Buffer
; *I
!= 0; I
++)
522 if (*I
== '*' && I
[1] == '/')
524 memmove(Buffer
,I
+2,strlen(I
+2) + 1);
529 if (InComment
== true)
533 // Discard single line comments
534 bool InQuote
= false;
535 for (char *I
= Buffer
; *I
!= 0; I
++)
542 if (*I
== '/' && I
[1] == '/')
549 // Look for multi line comments
551 for (char *I
= Buffer
; *I
!= 0; I
++)
558 if (*I
== '/' && I
[1] == '*')
561 for (char *J
= Buffer
; *J
!= 0; J
++)
563 if (*J
== '*' && J
[1] == '/')
565 memmove(I
,J
+2,strlen(J
+2) + 1);
571 if (InComment
== true)
583 // We now have a valid line fragment
585 for (char *I
= Buffer
; *I
!= 0;)
590 if (InQuote
== false && (*I
== '{' || *I
== ';' || *I
== '}'))
592 // Put the last fragment into the buffer
593 char *Start
= Buffer
;
595 for (; Start
!= I
&& isspace(*Start
) != 0; Start
++);
596 for (; Stop
!= Start
&& isspace(Stop
[-1]) != 0; Stop
--);
597 if (LineBuffer
.empty() == false && Stop
- Start
!= 0)
599 LineBuffer
+= string(Start
,Stop
- Start
);
601 // Remove the fragment
603 memmove(Buffer
,I
+ 1,strlen(I
+ 1) + 1);
607 if (TermChar
== '{' && LineBuffer
.empty() == true)
608 return _error
->Error(_("Syntax error %s:%u: Block starts with no name."),FName
.c_str(),CurLine
);
610 // No string on this line
611 if (LineBuffer
.empty() == true)
616 ParentTag
= string();
618 ParentTag
= Stack
[--StackPos
];
625 const char *Pos
= LineBuffer
.c_str();
626 if (ParseQuoteWord(Pos
,Tag
) == false)
627 return _error
->Error(_("Syntax error %s:%u: Malformed tag"),FName
.c_str(),CurLine
);
629 // Parse off the word
632 if (ParseCWord(Pos
,Word
) == false &&
633 ParseQuoteWord(Pos
,Word
) == false)
643 if (strlen(Pos
) != 0)
644 return _error
->Error(_("Syntax error %s:%u: Extra junk after value"),FName
.c_str(),CurLine
);
650 Stack
[StackPos
++] = ParentTag
;
652 /* Make sectional tags incorperate the section into the
654 if (AsSectional
== true && Word
.empty() == false)
661 if (ParentTag
.empty() == true)
664 ParentTag
+= string("::") + Tag
;
668 // Generate the item name
670 if (ParentTag
.empty() == true)
674 if (TermChar
!= '{' || Tag
.empty() == false)
675 Item
= ParentTag
+ "::" + Tag
;
681 if (Tag
.length() >= 1 && Tag
[0] == '#')
683 if (ParentTag
.empty() == false)
684 return _error
->Error(_("Syntax error %s:%u: Directives can only be done at the top level"),FName
.c_str(),CurLine
);
685 Tag
.erase(Tag
.begin());
688 else if (Tag
== "include")
691 return _error
->Error(_("Syntax error %s:%u: Too many nested includes"),FName
.c_str(),CurLine
);
692 if (Word
.length() > 2 && Word
.end()[-1] == '/')
694 if (ReadConfigDir(Conf
,Word
,AsSectional
,Depth
+1) == false)
695 return _error
->Error(_("Syntax error %s:%u: Included from here"),FName
.c_str(),CurLine
);
699 if (ReadConfigFile(Conf
,Word
,AsSectional
,Depth
+1) == false)
700 return _error
->Error(_("Syntax error %s:%u: Included from here"),FName
.c_str(),CurLine
);
704 return _error
->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName
.c_str(),CurLine
,Tag
.c_str());
708 // Set the item in the configuration class
714 LineBuffer
= string();
716 // Move up a tag, but only if there is no bit to parse
720 ParentTag
= string();
722 ParentTag
= Stack
[--StackPos
];
730 // Store the fragment
731 const char *Stripd
= _strstrip(Buffer
);
732 if (*Stripd
!= 0 && LineBuffer
.empty() == false)
734 LineBuffer
+= Stripd
;
737 if (LineBuffer
.empty() == false)
738 return _error
->Error(_("Syntax error %s:%u: Extra junk at end of file"),FName
.c_str(),CurLine
);
742 // ReadConfigDir - Read a directory of config files /*{{{*/
743 // ---------------------------------------------------------------------
745 bool ReadConfigDir(Configuration
&Conf
,string Dir
,bool AsSectional
,
748 DIR *D
= opendir(Dir
.c_str());
750 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
754 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
))
756 if (Ent
->d_name
[0] == '.')
759 // Skip bad file names ala run-parts
760 const char *C
= Ent
->d_name
;
762 if (isalpha(*C
) == 0 && isdigit(*C
) == 0 && *C
!= '_' && *C
!= '-')
767 // Make sure it is a file and not something else
768 string File
= flCombine(Dir
,Ent
->d_name
);
770 if (stat(File
.c_str(),&St
) != 0 || S_ISREG(St
.st_mode
) == 0)
773 List
.push_back(File
);
777 sort(List
.begin(),List
.end());
780 for (vector
<string
>::const_iterator I
= List
.begin(); I
!= List
.end(); I
++)
781 if (ReadConfigFile(Conf
,*I
,AsSectional
,Depth
) == false)