]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/configuration.cc
Merge branch 'debian/sid' into debian/experimental
[apt.git] / apt-pkg / contrib / configuration.cc
index ece05e8f630db3d8c9894fa754f542cd1253b9c9..4380d64b9ed2af6e930db118b5a73861ab36edb1 100644 (file)
    ##################################################################### */
                                                                        /*}}}*/
 // Include files                                                       /*{{{*/
    ##################################################################### */
                                                                        /*}}}*/
 // Include files                                                       /*{{{*/
+#include <config.h>
+
 #include <apt-pkg/configuration.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/fileutl.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 <algorithm>
+#include <string>
 #include <vector>
 #include <fstream>
 #include <vector>
 #include <fstream>
-#include <iostream>
+
+#include <apti18n.h>
 
 using namespace std;
                                                                        /*}}}*/
 
 using namespace std;
                                                                        /*}}}*/
@@ -39,8 +50,7 @@ Configuration::Configuration() : ToFree(true)
 }
 Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
 {
 }
 Configuration::Configuration(const Item *Root) : Root((Item *)Root), ToFree(false)
 {
-};
-
+}
                                                                        /*}}}*/
 // Configuration::~Configuration - Destructor                          /*{{{*/
 // ---------------------------------------------------------------------
                                                                        /*}}}*/
 // Configuration::~Configuration - Destructor                          /*{{{*/
 // ---------------------------------------------------------------------
@@ -168,42 +178,60 @@ 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");
 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('/');
+   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)
    {
 
    const Item *Itm = Lookup(Name);
    if (Itm == 0 || Itm->Value.empty() == true)
    {
-      if (Default == 0)
-        return rootDir;
-      else
-        return rootDir + 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;
+
+        // ../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 rootDir + val;
+   // do some normalisation by removing // and /./ from the path
+   size_t found = string::npos;
+   while ((found = result.find("/./")) != string::npos)
+      result.replace(found, 3, "/");
+   while ((found = result.find("//")) != string::npos)
+      result.replace(found, 2, "/");
+
+   return result;
 }
                                                                        /*}}}*/
 // Configuration::FindDir - Find a directory name                      /*{{{*/
 }
                                                                        /*}}}*/
 // Configuration::FindDir - Find a directory name                      /*{{{*/
@@ -213,19 +241,27 @@ string Configuration::FindDir(const char *Name,const char *Default) const
 {
    string Res = FindFile(Name,Default);
    if (Res.end()[-1] != '/')
 {
    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 + '/';
+   }
    return Res;
 }
                                                                        /*}}}*/
 // Configuration::FindVector - Find a vector of values                 /*{{{*/
 // ---------------------------------------------------------------------
 /* Returns a vector of config values under the given item */
    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) const
+vector<string> Configuration::FindVector(const char *Name, std::string const &Default) const
 {
    vector<string> Vec;
    const Item *Top = Lookup(Name);
    if (Top == NULL)
 {
    vector<string> Vec;
    const Item *Top = Lookup(Name);
    if (Top == NULL)
-      return Vec;
+      return VectorizeString(Default, ',');
+
+   if (Top->Value.empty() == false)
+      return VectorizeString(Top->Value, ',');
 
    Item *I = Top->Child;
    while(I != NULL)
 
    Item *I = Top->Child;
    while(I != NULL)
@@ -233,6 +269,9 @@ vector<string> Configuration::FindVector(const char *Name) const
       Vec.push_back(I->Value);
       I = I->Next;
    }
       Vec.push_back(I->Value);
       I = I->Next;
    }
+   if (Vec.empty() == true)
+      return VectorizeString(Default, ',');
+
    return Vec;
 }
                                                                        /*}}}*/
    return Vec;
 }
                                                                        /*}}}*/
@@ -394,6 +433,18 @@ void Configuration::Clear(string const &Name, string const &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                         /*{{{*/
 }
                                                                        /*}}}*/
 // Configuration::Clear - Clear an entire tree                         /*{{{*/
@@ -473,24 +524,80 @@ bool Configuration::ExistsAny(const char *Name) const
 /* Dump the entire configuration space */
 void Configuration::Dump(ostream& str)
 {
 /* 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 */
       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;
       }
       if (Top->Child != 0)
       {
         Top = Top->Child;
         continue;
       }
-      
+
       while (Top != 0 && Top->Next == 0)
         Top = Top->Parent;
       if (Top != 0)
         Top = Top->Next;
       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);
 }
                                                                        /*}}}*/
 
 }
                                                                        /*}}}*/
 
@@ -729,7 +836,7 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio
            // Go down a level
            if (TermChar == '{')
            {
            // Go down a level
            if (TermChar == '{')
            {
-              if (StackPos <= 100)
+              if (StackPos < sizeof(Stack)/sizeof(std::string))
                  Stack[StackPos++] = ParentTag;
               
               /* Make sectional tags incorperate the section into the
                  Stack[StackPos++] = ParentTag;
               
               /* Make sectional tags incorperate the section into the
@@ -870,13 +977,13 @@ Configuration::MatchAgainstConfig::MatchAgainstConfig(char const * Config)
       {
         regfree(p);
         delete p;
       {
         regfree(p);
         delete p;
-        clearPatterns();
-        _error->Warning("Regex compilation error for '%s' in configuration option '%s'",
-                               s->c_str(), Config);
-        return;
+        _error->Warning("Invalid regular expression '%s' in configuration "
+                         "option '%s' will be ignored.",
+                         s->c_str(), Config);
+        continue;
       }
    }
       }
    }
-   if (strings.size() == 0)
+   if (strings.empty() == true)
       patterns.push_back(NULL);
 }
                                                                        /*}}}*/
       patterns.push_back(NULL);
 }
                                                                        /*}}}*/
@@ -894,6 +1001,7 @@ void Configuration::MatchAgainstConfig::clearPatterns()
       regfree(*p);
       delete *p;
    }
       regfree(*p);
       delete *p;
    }
+   patterns.clear();
 }
                                                                        /*}}}*/
 // MatchAgainstConfig::Match - returns true if a pattern matches       /*{{{*/
 }
                                                                        /*}}}*/
 // MatchAgainstConfig::Match - returns true if a pattern matches       /*{{{*/