// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: configuration.cc,v 1.15 2001/02/20 07:03:17 jgg Exp $
+// $Id: configuration.cc,v 1.28 2004/04/30 04:00:15 mdz Exp $
/* ######################################################################
Configuration Class
This class provides a configuration file and command line parser
for a tree-oriented configuration environment. All runtime configuration
is stored in here.
+
+ This source is placed in the Public Domain, do with it what you will
+ It was originally written by Jason Gunthorpe <jgg@debian.org>.
##################################################################### */
/*}}}*/
// Include files /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/configuration.h"
-#endif
#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>
#include <vector>
#include <algorithm>
#include <fstream>
+#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
+
+using namespace std;
/*}}}*/
Configuration *_config = new Configuration;
if (Len != 0)
{
for (; I != 0; Last = &I->Next, I = I->Next)
- if ((Res = stringcasecmp(I->Tag.begin(),I->Tag.end(),S,S + Len)) == 0)
+ if ((Res = stringcasecmp(I->Tag,S,S + Len)) == 0)
break;
}
else
return 0;
I = new Item;
- I->Tag = string(S,Len);
+ I->Tag.assign(S,Len);
I->Next = *Last;
I->Parent = Head;
*Last = I;
if (Itm == 0 || Itm->Value.empty() == true)
{
if (Default == 0)
- return string();
+ return "";
else
return Default;
}
*/
string Configuration::FindFile(const char *Name,const char *Default) const
{
+ const Item *RootItem = Lookup("RootDir");
+ std::string rootDir = (RootItem == 0) ? "" : RootItem->Value;
+ if(rootDir.size() > 0 && rootDir[rootDir.size() - 1] != '/')
+ rootDir.push_back('/');
+
const Item *Itm = Lookup(Name);
if (Itm == 0 || Itm->Value.empty() == true)
{
if (Default == 0)
- return string();
+ return "";
else
return Default;
}
Itm = Itm->Parent;
}
- return val;
+ return rootDir + val;
}
/*}}}*/
// Configuration::FindDir - Find a directory name /*{{{*/
case 'i':
{
char buf[16];
- snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default));
+ snprintf(buf, sizeof(buf)-1, "%d", FindI(key, Default ? atoi(Default) : 0 ));
return buf;
}
}
// Configuration::CndSet - Conditinal Set a value /*{{{*/
// ---------------------------------------------------------------------
/* This will not overwrite */
-void Configuration::CndSet(const char *Name,string Value)
+void Configuration::CndSet(const char *Name,const string &Value)
{
Item *Itm = Lookup(Name,true);
if (Itm == 0)
// Configuration::Set - Set a value /*{{{*/
// ---------------------------------------------------------------------
/* */
-void Configuration::Set(const char *Name,string Value)
+void Configuration::Set(const char *Name,const string &Value)
{
Item *Itm = Lookup(Name,true);
if (Itm == 0)
char S[300];
snprintf(S,sizeof(S),"%i",Value);
Itm->Value = S;
+}
+ /*}}}*/
+// Configuration::Clear - Clear an single value from a list /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void Configuration::Clear(const string Name, int Value)
+{
+ char S[300];
+ snprintf(S,sizeof(S),"%i",Value);
+ Clear(Name, S);
+}
+ /*}}}*/
+// Configuration::Clear - Clear an single value from a list /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void Configuration::Clear(const string Name, string Value)
+{
+ Item *Top = Lookup(Name.c_str(),false);
+ if (Top == 0 || Top->Child == 0)
+ return;
+
+ Item *Tmp, *Prev, *I;
+ Prev = I = Top->Child;
+
+ while(I != NULL)
+ {
+ if(I->Value == Value)
+ {
+ Tmp = I;
+ // was first element, point parent to new first element
+ if(Top->Child == Tmp)
+ Top->Child = I->Next;
+ I = I->Next;
+ Prev->Next = I;
+ delete Tmp;
+ } else {
+ Prev = I;
+ I = I->Next;
+ }
+ }
+
}
/*}}}*/
// Configuration::Clear - Clear an entire tree /*{{{*/
void Configuration::Clear(string Name)
{
Item *Top = Lookup(Name.c_str(),false);
- if (Top == 0)
+ if (Top == 0)
return;
-
- Top->Value = string();
+
+ Top->Value.clear();
Item *Stop = Top;
Top = Top->Child;
Stop->Child = 0;
{
string key = Name;
- if (key.size() > 2 && key.end()[-2] == '/' &&
- key.find_first_of("fdbi",key.size()-1) < key.size())
+ if (key.size() > 2 && key.end()[-2] == '/')
{
- key.resize(key.size() - 2);
- if (Exists(key.c_str()))
- return true;
+ if (key.find_first_of("fdbi",key.size()-1) < key.size())
+ {
+ key.resize(key.size() - 2);
+ if (Exists(key.c_str()))
+ return true;
+ }
+ else
+ {
+ _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
+ }
}
-
return Exists(Name);
}
/*}}}*/
// Configuration::Dump - Dump the config /*{{{*/
// ---------------------------------------------------------------------
/* Dump the entire configuration space */
-void Configuration::Dump()
+void Configuration::Dump(ostream& str)
{
/* Write out all of the configuration directives by walking the
configuration tree */
const Configuration::Item *Top = Tree(0);
for (; Top != 0;)
{
- clog << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
+ str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
if (Top->Child != 0)
{
Sectional config files are like bind's named.conf where there are
sections like 'zone "foo.org" { .. };' This causes each section to be
added in with a tag like "zone::foo.org" instead of being split
- tag/value. */
-bool ReadConfigFile(Configuration &Conf,string FName,bool AsSectional,
+ tag/value. AsSectional enables Sectional parsing.*/
+bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
unsigned Depth)
{
// Open the stream for reading
- ifstream F(FName.c_str(),ios::in | ios::nocreate);
+ ifstream F(FName.c_str(),ios::in);
if (!F != 0)
return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
-
- char Buffer[300];
+
string LineBuffer;
string Stack[100];
unsigned int StackPos = 0;
bool InComment = false;
while (F.eof() == false)
{
- F.getline(Buffer,sizeof(Buffer));
+ // The raw input line.
+ std::string Input;
+ // The input line with comments stripped.
+ std::string Fragment;
+
+ // Grab the next line of F and place it in Input.
+ do
+ {
+ char *Buffer = new char[1024];
+
+ F.clear();
+ F.getline(Buffer,sizeof(Buffer) / 2);
+
+ Input += Buffer;
+ }
+ while (F.fail() && !F.eof());
+
+ // Expand tabs in the input line and remove leading and trailing
+ // whitespace.
+ {
+ const int BufferSize = Input.size() * 8 + 1;
+ char *Buffer = new char[BufferSize];
+ try
+ {
+ memcpy(Buffer, Input.c_str(), Input.size() + 1);
+
+ _strtabexpand(Buffer, BufferSize);
+ _strstrip(Buffer);
+ Input = Buffer;
+ }
+ catch(...)
+ {
+ delete[] Buffer;
+ throw;
+ }
+ delete[] Buffer;
+ }
CurLine++;
- _strtabexpand(Buffer,sizeof(Buffer));
- _strstrip(Buffer);
+
+ // Now strip comments; if the whole line is contained in a
+ // comment, skip this line.
+
+ // The first meaningful character in the current fragment; will
+ // be adjusted below as we remove bytes from the front.
+ std::string::const_iterator Start = Input.begin();
+ // The last meaningful character in the current fragment.
+ std::string::const_iterator End = Input.end();
// Multi line comment
if (InComment == true)
{
- for (const char *I = Buffer; *I != 0; I++)
+ for (std::string::const_iterator I = Start;
+ I != End; ++I)
{
- if (*I == '*' && I[1] == '/')
+ if (*I == '*' && I + 1 != End && I[1] == '/')
{
- memmove(Buffer,I+2,strlen(I+2) + 1);
+ Start = I + 2;
InComment = false;
break;
}
// Discard single line comments
bool InQuote = false;
- for (char *I = Buffer; *I != 0; I++)
+ for (std::string::const_iterator I = Start;
+ I != End; ++I)
{
if (*I == '"')
InQuote = !InQuote;
if (InQuote == true)
continue;
- if (*I == '/' && I[1] == '/')
+ if (*I == '/' && I + 1 != End && I[1] == '/')
{
- *I = 0;
+ End = I;
break;
}
}
- // Look for multi line comments
+ // Look for multi line comments and build up the
+ // fragment.
+ Fragment.reserve(End - Start);
InQuote = false;
- for (char *I = Buffer; *I != 0; I++)
+ for (std::string::const_iterator I = Start;
+ I != End; ++I)
{
if (*I == '"')
InQuote = !InQuote;
if (InQuote == true)
- continue;
-
- if (*I == '/' && I[1] == '*')
+ Fragment.push_back(*I);
+ else if (*I == '/' && I + 1 != End && I[1] == '*')
{
InComment = true;
- for (char *J = Buffer; *J != 0; J++)
+ for (std::string::const_iterator J = I;
+ J != End; ++J)
{
- if (*J == '*' && J[1] == '/')
+ if (*J == '*' && J + 1 != End && J[1] == '/')
{
- memmove(I,J+2,strlen(J+2) + 1);
+ // Pretend we just finished walking over the
+ // comment, and don't add anything to the output
+ // fragment.
+ I = J + 1;
InComment = false;
break;
}
}
if (InComment == true)
- {
- *I = 0;
- break;
- }
+ break;
}
+ else
+ Fragment.push_back(*I);
}
-
- // Blank
- if (Buffer[0] == 0)
+
+ // Skip blank lines.
+ if (Fragment.empty())
continue;
- // We now have a valid line fragment
+ // The line has actual content; interpret what it means.
InQuote = false;
- for (char *I = Buffer; *I != 0;)
+ Start = Fragment.begin();
+ End = Fragment.end();
+ for (std::string::const_iterator I = Start;
+ I != End; ++I)
{
if (*I == '"')
InQuote = !InQuote;
if (InQuote == false && (*I == '{' || *I == ';' || *I == '}'))
{
// Put the last fragment into the buffer
- char *Start = Buffer;
- char *Stop = I;
- for (; Start != I && isspace(*Start) != 0; Start++);
- for (; Stop != Start && isspace(Stop[-1]) != 0; Stop--);
- if (LineBuffer.empty() == false && Stop - Start != 0)
+ std::string::const_iterator NonWhitespaceStart = Start;
+ std::string::const_iterator NonWhitespaceStop = I;
+ for (; NonWhitespaceStart != I && isspace(*NonWhitespaceStart) != 0; NonWhitespaceStart++)
+ ;
+ for (; NonWhitespaceStop != NonWhitespaceStart && isspace(NonWhitespaceStop[-1]) != 0; NonWhitespaceStop--)
+ ;
+ if (LineBuffer.empty() == false && NonWhitespaceStop - NonWhitespaceStart != 0)
LineBuffer += ' ';
- LineBuffer += string(Start,Stop - Start);
-
- // Remove the fragment
+ LineBuffer += string(NonWhitespaceStart, NonWhitespaceStop);
+
+ // Drop this from the input string, saving the character
+ // that terminated the construct we just closed. (i.e., a
+ // brace or a semicolon)
char TermChar = *I;
- memmove(Buffer,I + 1,strlen(I + 1) + 1);
- I = Buffer;
+ Start = I + 1;
// Syntax Error
if (TermChar == '{' && LineBuffer.empty() == true)
string Tag;
const char *Pos = LineBuffer.c_str();
if (ParseQuoteWord(Pos,Tag) == false)
- return _error->Error(_("Syntax error %s:%u: Malformed Tag"),FName.c_str(),CurLine);
+ return _error->Error(_("Syntax error %s:%u: Malformed tag"),FName.c_str(),CurLine);
// Parse off the word
string Word;
+ bool NoWord = false;
if (ParseCWord(Pos,Word) == false &&
ParseQuoteWord(Pos,Word) == false)
{
{
Word = Tag;
Tag = "";
- }
+ }
+ else
+ NoWord = true;
}
if (strlen(Pos) != 0)
return _error->Error(_("Syntax error %s:%u: Extra junk after value"),FName.c_str(),CurLine);
else
{
// Set the item in the configuration class
- Conf.Set(Item,Word);
+ if (NoWord == false)
+ Conf.Set(Item,Word);
}
// Empty the buffer
- LineBuffer = string();
+ LineBuffer.clear();
// Move up a tag, but only if there is no bit to parse
if (TermChar == '}')
{
if (StackPos == 0)
- ParentTag = string();
+ ParentTag.clear();
else
ParentTag = Stack[--StackPos];
}
}
- else
- I++;
}
- // Store the fragment
- const char *Stripd = _strstrip(Buffer);
- if (*Stripd != 0 && LineBuffer.empty() == false)
- LineBuffer += " ";
- LineBuffer += Stripd;
+ // Store the remaining text, if any, in the current line buffer.
+
+ // NB: could change this to use string-based operations; I'm
+ // using strstrip now to ensure backwards compatibility.
+ // -- dburrows 2008-04-01
+ {
+ char *Buffer = new char[End - Start + 1];
+ try
+ {
+ std::copy(Start, End, Buffer);
+ Buffer[End - Start] = '\0';
+
+ const char *Stripd = _strstrip(Buffer);
+ if (*Stripd != 0 && LineBuffer.empty() == false)
+ LineBuffer += " ";
+ LineBuffer += Stripd;
+ }
+ catch(...)
+ {
+ delete[] Buffer;
+ throw;
+ }
+ delete[] Buffer;
+ }
}
if (LineBuffer.empty() == false)
// ReadConfigDir - Read a directory of config files /*{{{*/
// ---------------------------------------------------------------------
/* */
-bool ReadConfigDir(Configuration &Conf,string Dir,bool AsSectional,
- unsigned Depth)
+bool ReadConfigDir(Configuration &Conf,const string &Dir,bool AsSectional,
+ unsigned Depth)
{
- static const char *BadExts[] = {".disabled",".dpkg-old",".dpkg-dist",
- ".rpmsave",".rpmorig","~",",v",0};
-
DIR *D = opendir(Dir.c_str());
if (D == 0)
return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
{
- if (strcmp(Ent->d_name,".") == 0 ||
- strcmp(Ent->d_name,"..") == 0)
+ if (Ent->d_name[0] == '.')
continue;
- // Skip bad extensions
- const char **I;
- for (I = BadExts; *I != 0; I++)
- {
- if (strcmp(Ent->d_name + strlen(Ent->d_name) - strlen(*I),*I) == 0)
+ // Skip bad file names ala run-parts
+ const char *C = Ent->d_name;
+ for (; *C != 0; C++)
+ if (isalpha(*C) == 0 && isdigit(*C) == 0 && *C != '_' && *C != '-')
break;
- }
-
- if (*I != 0)
+ if (*C != 0)
continue;
// Make sure it is a file and not something else