X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/bc03039d0f532b6463de33bff60c7d876af6ea5e..f1788cf1c819d4c003ae046367af00203cd027d6:/apt-pkg/contrib/configuration.cc

diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc
index 14a000fa5..9007bf9ec 100644
--- a/apt-pkg/contrib/configuration.cc
+++ b/apt-pkg/contrib/configuration.cc
@@ -15,24 +15,28 @@
    ##################################################################### */
 									/*}}}*/
 // Include files							/*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/configuration.h"
-#endif
+#include <config.h>
+
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.h>
-#include <apti18n.h>
+#include <apt-pkg/macros.h>
+
+#include <ctype.h>
+#include <regex.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#include <vector>
 #include <algorithm>
+#include <string>
+#include <stack>
+#include <vector>
 #include <fstream>
-#include <iostream>
-    
-#include <stdio.h>
-#include <dirent.h>
-#include <sys/stat.h>
-#include <unistd.h>
+
+#include <apti18n.h>
 
 using namespace std;
 									/*}}}*/
@@ -48,8 +52,7 @@ Configuration::Configuration() : ToFree(true)
 }
 Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
 {
-};
-
+}
 									/*}}}*/
 // Configuration::~Configuration - Destructor				/*{{{*/
 // ---------------------------------------------------------------------
@@ -88,7 +91,7 @@ Configuration::~Configuration()
 /* This will lookup a single item by name below another item. It is a 
    helper function for the main lookup function */
 Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
-					   unsigned long Len,bool Create)
+					   unsigned long const &Len,bool const &Create)
 {
    int Res = 1;
    Item *I = Head->Child;
@@ -121,7 +124,7 @@ Configuration::Item *Configuration::Lookup(Item *Head,const char *S,
 // ---------------------------------------------------------------------
 /* This performs a fully scoped lookup of a given name, possibly creating
    new items */
-Configuration::Item *Configuration::Lookup(const char *Name,bool Create)
+Configuration::Item *Configuration::Lookup(const char *Name,bool const &Create)
 {
    if (Name == 0)
       return Root->Child;
@@ -176,38 +179,53 @@ string Configuration::Find(const char *Name,const char *Default) const
  */
 string Configuration::FindFile(const char *Name,const char *Default) const
 {
+   const Item *RootItem = Lookup("RootDir");
+   std::string result =  (RootItem == 0) ? "" : RootItem->Value;
+   if(result.empty() == false && result[result.size() - 1] != '/')
+     result.push_back('/');
+
    const Item *Itm = Lookup(Name);
    if (Itm == 0 || Itm->Value.empty() == true)
    {
-      if (Default == 0)
-	 return "";
-      else
-	 return Default;
+      if (Default != 0)
+	 result.append(Default);
    }
-   
-   string val = Itm->Value;
-   while (Itm->Parent != 0 && Itm->Parent->Value.empty() == false)
-   {	 
-      // Absolute
-      if (val.length() >= 1 && val[0] == '/')
-         break;
-
-      // ~/foo or ./foo 
-      if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
-	 break;
-	 
-      // ../foo 
-      if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
-	 break;
-      
-      if (Itm->Parent->Value.end()[-1] != '/')
-	 val.insert(0, "/");
+   else
+   {
+      string val = Itm->Value;
+      while (Itm->Parent != 0)
+      {
+	 if (Itm->Parent->Value.empty() == true)
+	 {
+	    Itm = Itm->Parent;
+	    continue;
+	 }
 
-      val.insert(0, Itm->Parent->Value);
-      Itm = Itm->Parent;
-   }
+	 // Absolute
+	 if (val.length() >= 1 && val[0] == '/')
+	 {
+	    if (val.compare(0, 9, "/dev/null") == 0)
+	       val.erase(9);
+	    break;
+	 }
+
+	 // ~/foo or ./foo
+	 if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/')
+	    break;
 
-   return val;
+	 // ../foo
+	 if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/')
+	    break;
+
+	 if (Itm->Parent->Value.end()[-1] != '/')
+	    val.insert(0, "/");
+
+	 val.insert(0, Itm->Parent->Value);
+	 Itm = Itm->Parent;
+      }
+      result.append(val);
+   }
+   return flNormalize(result);
 }
 									/*}}}*/
 // Configuration::FindDir - Find a directory name			/*{{{*/
@@ -217,14 +235,44 @@ string Configuration::FindDir(const char *Name,const char *Default) const
 {
    string Res = FindFile(Name,Default);
    if (Res.end()[-1] != '/')
+   {
+      size_t const found = Res.rfind("/dev/null");
+      if (found != string::npos && found == Res.size() - 9)
+	 return Res; // /dev/null returning
       return Res + '/';
+   }
    return Res;
 }
 									/*}}}*/
+// Configuration::FindVector - Find a vector of values			/*{{{*/
+// ---------------------------------------------------------------------
+/* Returns a vector of config values under the given item */
+vector<string> Configuration::FindVector(const char *Name, std::string const &Default, bool const Keys) const
+{
+   vector<string> Vec;
+   const Item *Top = Lookup(Name);
+   if (Top == NULL)
+      return VectorizeString(Default, ',');
+
+   if (Top->Value.empty() == false)
+      return VectorizeString(Top->Value, ',');
+
+   Item *I = Top->Child;
+   while(I != NULL)
+   {
+      Vec.push_back(Keys ? I->Tag : I->Value);
+      I = I->Next;
+   }
+   if (Vec.empty() == true)
+      return VectorizeString(Default, ',');
+
+   return Vec;
+}
+									/*}}}*/
 // Configuration::FindI - Find an integer value				/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-int Configuration::FindI(const char *Name,int Default) const
+int Configuration::FindI(const char *Name,int const &Default) const
 {
    const Item *Itm = Lookup(Name);
    if (Itm == 0 || Itm->Value.empty() == true)
@@ -241,7 +289,7 @@ int Configuration::FindI(const char *Name,int Default) const
 // Configuration::FindB - Find a boolean type				/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool Configuration::FindB(const char *Name,bool Default) const
+bool Configuration::FindB(const char *Name,bool const &Default) const
 {
    const Item *Itm = Lookup(Name);
    if (Itm == 0 || Itm->Value.empty() == true)
@@ -303,6 +351,19 @@ void Configuration::CndSet(const char *Name,const string &Value)
       Itm->Value = Value;
 }
 									/*}}}*/
+// Configuration::Set - Set an integer value				/*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void Configuration::CndSet(const char *Name,int const Value)
+{
+   Item *Itm = Lookup(Name,true);
+   if (Itm == 0 || Itm->Value.empty() == false)
+      return;
+   char S[300];
+   snprintf(S,sizeof(S),"%i",Value);
+   Itm->Value = S;
+}
+									/*}}}*/
 // Configuration::Set - Set a value					/*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -317,7 +378,7 @@ void Configuration::Set(const char *Name,const string &Value)
 // Configuration::Set - Set an integer value				/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void Configuration::Set(const char *Name,int Value)
+void Configuration::Set(const char *Name,int const &Value)
 {
    Item *Itm = Lookup(Name,true);
    if (Itm == 0)
@@ -330,7 +391,7 @@ void Configuration::Set(const char *Name,int Value)
 // Configuration::Clear - Clear an single value from a list	        /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void Configuration::Clear(const string Name, int Value)
+void Configuration::Clear(string const &Name, int const &Value)
 {
    char S[300];
    snprintf(S,sizeof(S),"%i",Value);
@@ -340,7 +401,7 @@ void Configuration::Clear(const string Name, int Value)
 // Configuration::Clear - Clear an single value from a list	        /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void Configuration::Clear(const string Name, string Value)
+void Configuration::Clear(string const &Name, string const &Value)
 {
    Item *Top = Lookup(Name.c_str(),false);
    if (Top == 0 || Top->Child == 0)
@@ -366,12 +427,24 @@ void Configuration::Clear(const string Name, string Value)
       }
    }
      
+}
+									/*}}}*/
+// Configuration::Clear - Clear everything				/*{{{*/
+// ---------------------------------------------------------------------
+void Configuration::Clear()
+{
+   const Configuration::Item *Top = Tree(0);
+   while( Top != 0 )
+   {
+      Clear(Top->FullTag());
+      Top = Top->Next;
+   }
 }
 									/*}}}*/
 // Configuration::Clear - Clear an entire tree				/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-void Configuration::Clear(string Name)
+void Configuration::Clear(string const &Name)
 {
    Item *Top = Lookup(Name.c_str(),false);
    if (Top == 0) 
@@ -406,6 +479,59 @@ void Configuration::Clear(string Name)
    }
 }
 									/*}}}*/
+void Configuration::MoveSubTree(char const * const OldRootName, char const * const NewRootName)/*{{{*/
+{
+   // prevent NewRoot being a subtree of OldRoot
+   if (OldRootName == nullptr)
+      return;
+   if (NewRootName != nullptr)
+   {
+      if (strcmp(OldRootName, NewRootName) == 0)
+	 return;
+      std::string const oldroot = std::string(OldRootName) + "::";
+      if (strcasestr(NewRootName, oldroot.c_str()) != NULL)
+	 return;
+   }
+
+   Item * Top;
+   Item const * const OldRoot = Top = Lookup(OldRootName, false);
+   if (Top == nullptr)
+      return;
+   std::string NewRoot;
+   if (NewRootName != nullptr)
+      NewRoot.append(NewRootName).append("::");
+
+   Top->Value.clear();
+   Item * const Stop = Top;
+   Top = Top->Child;
+   Stop->Child = 0;
+   for (; Top != 0;)
+   {
+      if (Top->Child != 0)
+      {
+	 Top = Top->Child;
+	 continue;
+      }
+
+      while (Top != 0 && Top->Next == 0)
+      {
+	 Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
+	 Item const * const Tmp = Top;
+	 Top = Top->Parent;
+	 delete Tmp;
+
+	 if (Top == Stop)
+	    return;
+      }
+
+      Set(NewRoot + Top->FullTag(OldRoot), Top->Value);
+      Item const * const Tmp = Top;
+      if (Top != 0)
+	 Top = Top->Next;
+      delete Tmp;
+   }
+}
+									/*}}}*/
 // Configuration::Exists - Returns true if the Name exists		/*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -425,6 +551,7 @@ bool Configuration::ExistsAny(const char *Name) const
    string key = Name;
 
    if (key.size() > 2 && key.end()[-2] == '/')
+   {
       if (key.find_first_of("fdbi",key.size()-1) < key.size())
       {
          key.resize(key.size() - 2);
@@ -435,7 +562,7 @@ bool Configuration::ExistsAny(const char *Name) const
       {
          _error->Warning(_("Unrecognized type abbreviation: '%c'"), key.end()[-3]);
       }
-
+   }
    return Exists(Name);
 }
 									/*}}}*/
@@ -444,24 +571,80 @@ bool Configuration::ExistsAny(const char *Name) const
 /* Dump the entire configuration space */
 void Configuration::Dump(ostream& str)
 {
-   /* Write out all of the configuration directives by walking the 
+   Dump(str, NULL, "%f \"%v\";\n", true);
+}
+void Configuration::Dump(ostream& str, char const * const root,
+			 char const * const formatstr, bool const emptyValue)
+{
+   const Configuration::Item* Top = Tree(root);
+   if (Top == 0)
+      return;
+   const Configuration::Item* const Root = (root == NULL) ? NULL : Top;
+   std::vector<std::string> const format = VectorizeString(formatstr, '%');
+
+   /* Write out all of the configuration directives by walking the
       configuration tree */
-   const Configuration::Item *Top = Tree(0);
-   for (; Top != 0;)
-   {
-      str << Top->FullTag() << " \"" << Top->Value << "\";" << endl;
-      
+   do {
+      if (emptyValue == true || Top->Value.empty() == emptyValue)
+      {
+	 std::vector<std::string>::const_iterator f = format.begin();
+	 str << *f;
+	 for (++f; f != format.end(); ++f)
+	 {
+	    if (f->empty() == true)
+	    {
+	       ++f;
+	       str << '%' << *f;
+	       continue;
+	    }
+	    char const type = (*f)[0];
+	    if (type == 'f')
+	       str << Top->FullTag();
+	    else if (type == 't')
+	       str << Top->Tag;
+	    else if (type == 'v')
+	       str << Top->Value;
+	    else if (type == 'F')
+	       str << QuoteString(Top->FullTag(), "=\"\n");
+	    else if (type == 'T')
+	       str << QuoteString(Top->Tag, "=\"\n");
+	    else if (type == 'V')
+	       str << QuoteString(Top->Value, "=\"\n");
+	    else if (type == 'n')
+	       str << "\n";
+	    else if (type == 'N')
+	       str << "\t";
+	    else
+	       str << '%' << type;
+	    str << f->c_str() + 1;
+	 }
+      }
+
       if (Top->Child != 0)
       {
 	 Top = Top->Child;
 	 continue;
       }
-      
+
       while (Top != 0 && Top->Next == 0)
 	 Top = Top->Parent;
       if (Top != 0)
 	 Top = Top->Next;
-   }
+
+      if (Root != NULL)
+      {
+	 const Configuration::Item* I = Top;
+	 while(I != 0)
+	 {
+	    if (I == Root)
+	       break;
+	    else
+	       I = I->Parent;
+	 }
+	 if (I == 0)
+	    break;
+      }
+   } while (Top != 0);
 }
 									/*}}}*/
 
@@ -485,141 +668,199 @@ string Configuration::Item::FullTag(const Item *Stop) const
    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. AsSectional enables Sectional parsing.*/
-bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
-		    unsigned Depth)
-{   
+static void leaveCurrentScope(std::stack<std::string> &Stack, std::string &ParentTag)
+{
+   if (Stack.empty())
+      ParentTag.clear();
+   else
+   {
+      ParentTag = Stack.top();
+      Stack.pop();
+   }
+}
+bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectional,
+		    unsigned const &Depth)
+{
    // Open the stream for reading
-   ifstream F(FName.c_str(),ios::in); 
-   if (!F != 0)
+   ifstream F(FName.c_str(),ios::in);
+   if (F.fail() == true)
       return _error->Errno("ifstream::ifstream",_("Opening configuration file %s"),FName.c_str());
-   
-   char Buffer[1024];
+
    string LineBuffer;
-   string Stack[100];
-   unsigned int StackPos = 0;
-   
+   std::stack<std::string> Stack;
+
    // Parser state
    string ParentTag;
-   
+
    int CurLine = 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;
+	  delete[] 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++;
-      // This should be made to work instead, but this is better than looping
-      if (F.fail() && !F.eof())
-         return _error->Error(_("Line %d too long (max %d)"), CurLine, sizeof(Buffer));
 
-      _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;
-	    }	    
+	    }
 	 }
 	 if (InComment == true)
 	    continue;
       }
-      
+
       // 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] == '/')
-         {
-	    *I = 0;
+
+	 if ((*I == '/' && I + 1 != End && I[1] == '/') ||
+	     (*I == '#' && strcmp(string(I,I+6).c_str(),"#clear") != 0 &&
+	      strcmp(string(I,I+8).c_str(),"#include") != 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)
 	       return _error->Error(_("Syntax error %s:%u: Block starts with no name."),FName.c_str(),CurLine);
-	    
+
 	    // No string on this line
 	    if (LineBuffer.empty() == true)
 	    {
 	       if (TermChar == '}')
-	       {
-		  if (StackPos == 0)
-		     ParentTag = string();
-		  else
-		     ParentTag = Stack[--StackPos];
-	       }
+		  leaveCurrentScope(Stack, ParentTag);
 	       continue;
 	    }
-	    
+
 	    // Parse off the tag
 	    string Tag;
 	    const char *Pos = LineBuffer.c_str();
@@ -646,25 +887,23 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
 	    // Go down a level
 	    if (TermChar == '{')
 	    {
-	       if (StackPos <= 100)
-		  Stack[StackPos++] = ParentTag;
-	       
+	       Stack.push(ParentTag);
+
 	       /* Make sectional tags incorperate the section into the
 	          tag string */
 	       if (AsSectional == true && Word.empty() == false)
 	       {
-		  Tag += "::" ;
-		  Tag += Word;
-		  Word = "";
+		  Tag.append("::").append(Word);
+		  Word.clear();
 	       }
-	       
+
 	       if (ParentTag.empty() == true)
 		  ParentTag = Tag;
 	       else
-		  ParentTag += string("::") + Tag;
-	       Tag = string();
+		  ParentTag.append("::").append(Tag);
+	       Tag.clear();
 	    }
-	    
+
 	    // Generate the item name
 	    string Item;
 	    if (ParentTag.empty() == true)
@@ -676,7 +915,7 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
 	       else
 		  Item = ParentTag;
 	    }
-	    
+
 	    // Specials
 	    if (Tag.length() >= 1 && Tag[0] == '#')
 	    {
@@ -698,40 +937,53 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
 		  {
 		     if (ReadConfigFile(Conf,Word,AsSectional,Depth+1) == false)
 			return _error->Error(_("Syntax error %s:%u: Included from here"),FName.c_str(),CurLine);
-		  }		  
+		  }
 	       }
 	       else
 		  return _error->Error(_("Syntax error %s:%u: Unsupported directive '%s'"),FName.c_str(),CurLine,Tag.c_str());
 	    }
+	    else if (Tag.empty() == true && NoWord == false && Word == "#clear")
+	       return _error->Error(_("Syntax error %s:%u: clear directive requires an option tree as argument"),FName.c_str(),CurLine);
 	    else
 	    {
 	       // Set the item in the configuration class
 	       if (NoWord == false)
 		  Conf.Set(Item,Word);
 	    }
-	    
+
 	    // Empty the buffer
 	    LineBuffer.clear();
-	    
+
 	    // Move up a tag, but only if there is no bit to parse
 	    if (TermChar == '}')
-	    {
-	       if (StackPos == 0)
-		  ParentTag.clear();
-	       else
-		  ParentTag = Stack[--StackPos];
-	    }
-	    
+	       leaveCurrentScope(Stack, ParentTag);
 	 }
-	 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)
@@ -742,44 +994,67 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool AsSectional,
 // ReadConfigDir - Read a directory of config files			/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool ReadConfigDir(Configuration &Conf,const string &Dir,bool AsSectional,
-		   unsigned Depth)
-{   
-   DIR *D = opendir(Dir.c_str());
-   if (D == 0)
-      return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
-
-   vector<string> List;
-   
-   for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
-   {
-      if (Ent->d_name[0] == '.')
-	 continue;
-      
-      // 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 (*C != 0)
-	 continue;
-      
-      // Make sure it is a file and not something else
-      string File = flCombine(Dir,Ent->d_name);
-      struct stat St;
-      if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
-	 continue;
-      
-      List.push_back(File);      
-   }   
-   closedir(D);
-   
-   sort(List.begin(),List.end());
+bool ReadConfigDir(Configuration &Conf,const string &Dir,
+		   bool const &AsSectional, unsigned const &Depth)
+{
+   vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true);
 
    // Read the files
-   for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
+   for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
       if (ReadConfigFile(Conf,*I,AsSectional,Depth) == false)
 	 return false;
    return true;
 }
 									/*}}}*/
+// MatchAgainstConfig Constructor					/*{{{*/
+Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
+{
+   std::vector<std::string> const strings = _config->FindVector(Config);
+   for (std::vector<std::string>::const_iterator s = strings.begin();
+	s != strings.end(); ++s)
+   {
+      regex_t *p = new regex_t;
+      if (regcomp(p, s->c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB) == 0)
+	 patterns.push_back(p);
+      else
+      {
+	 regfree(p);
+	 delete p;
+	 _error->Warning("Invalid regular expression '%s' in configuration "
+                         "option '%s' will be ignored.",
+                         s->c_str(), Config);
+	 continue;
+      }
+   }
+   if (strings.empty() == true)
+      patterns.push_back(NULL);
+}
+									/*}}}*/
+// MatchAgainstConfig Destructor					/*{{{*/
+Configuration::MatchAgainstConfig::~MatchAgainstConfig()
+{
+   clearPatterns();
+}
+void Configuration::MatchAgainstConfig::clearPatterns()
+{
+   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
+	p != patterns.end(); ++p)
+   {
+      if (*p == NULL) continue;
+      regfree(*p);
+      delete *p;
+   }
+   patterns.clear();
+}
+									/*}}}*/
+// MatchAgainstConfig::Match - returns true if a pattern matches	/*{{{*/
+bool Configuration::MatchAgainstConfig::Match(char const * str) const
+{
+   for(std::vector<regex_t *>::const_iterator p = patterns.begin();
+	p != patterns.end(); ++p)
+      if (*p != NULL && regexec(*p, str, 0, 0, 0) == 0)
+	 return true;
+
+   return false;
+}
+									/*}}}*/