]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf.c
I decided to turn off cf/url-equiv for indexation. however, after the indexer
[libucw.git] / lib / conf.c
index b899b55e98bc19b1c75271b4ccba2e5fe0e83887..7e4443bea453a9a2697e0cee85650510e4f8edab 100644 (file)
@@ -2,6 +2,9 @@
  *     Sherlock Library -- Reading configuration files
  *
  *     (c) 2001 Robert Spalek <robert@ucw.cz>
  *     Sherlock Library -- Reading configuration files
  *
  *     (c) 2001 Robert Spalek <robert@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
 #include "lib/lib.h"
  */
 
 #include "lib/lib.h"
@@ -22,7 +25,9 @@
 #define        MAX_LEVEL       8
 
 static struct cfitem *cfsection;
 #define        MAX_LEVEL       8
 
 static struct cfitem *cfsection;
-static struct mempool *cfpool;
+struct mempool *cfpool;
+
+byte *cfdeffile = DEFAULT_CONFIG;
 
 static void CONSTRUCTOR
 conf_init(void)
 
 static void CONSTRUCTOR
 conf_init(void)
@@ -47,23 +52,36 @@ cfg_stralloc(byte *s)
 
 void cf_register(struct cfitem *items)
 {
 
 void cf_register(struct cfitem *items)
 {
-       if(items[0].type!=CT_SECTION)
-               die("Invalid configuration section, first item must be of type CT_SECTION");
+       if(items[0].type!=CT_SECTION && items[0].type!=CT_INCOMPLETE_SECTION)
+               die("cf_register: Invalid section type");
        items[0].var=cfsection;
        cfsection=items;
 }
 
        items[0].var=cfsection;
        cfsection=items;
 }
 
+int cf_item_count(void)
+{
+       struct cfitem *sect, *item;
+       int count = 0;
+       for (sect = cfsection; sect; sect = sect->var)
+               for (item = sect+1; item->type; item++)
+                       count++;
+       return count;
+}
+
 struct cfitem *cf_get_item(byte *sect, byte *name)
 {
 struct cfitem *cf_get_item(byte *sect, byte *name)
 {
-       struct cfitem *item;
+       struct cfitem *item, *section;
 
        item=cfsection;
        while(item && strcasecmp(item->name,sect))
                item=item->var;
        if(!item)       /* unknown section */
                return NULL;
 
        item=cfsection;
        while(item && strcasecmp(item->name,sect))
                item=item->var;
        if(!item)       /* unknown section */
                return NULL;
+       section = item;
 
        for(item++; item->type && strcasecmp(item->name,name); item++);
 
        for(item++; item->type && strcasecmp(item->name,name); item++);
+       if (!item->type && section->type == CT_INCOMPLETE_SECTION)
+               return NULL;
 
        return item;    /* item->type == 0 if not found */
 }
 
        return item;    /* item->type == 0 if not found */
 }
@@ -73,6 +91,8 @@ byte *cf_set_item(byte *sect, byte *name, byte *value)
        struct cfitem *item;
        byte *msg=NULL;
 
        struct cfitem *item;
        byte *msg=NULL;
 
+       if (!*sect)
+               return "Empty section name";
        item=cf_get_item(sect,name);
        if(!item)       /* ignore unknown section */
                return NULL;
        item=cf_get_item(sect,name);
        if(!item)       /* ignore unknown section */
                return NULL;
@@ -200,6 +220,7 @@ void cf_read(byte *filename)
 {
        if(!cf_subread(filename,0))
                die("Reading config file %s failed",filename);
 {
        if(!cf_subread(filename,0))
                die("Reading config file %s failed",filename);
+       cfdeffile = NULL;
 }
 
 int cf_getopt(int argc,char * const argv[],
 }
 
 int cf_getopt(int argc,char * const argv[],
@@ -233,6 +254,8 @@ int cf_getopt(int argc,char * const argv[],
                                        name=c;
                                }
 
                                        name=c;
                                }
 
+                               if (cfdeffile)
+                                       cf_read(cfdeffile);
                                msg=cf_set_item(sect,name,value);
                        }
                        if(msg)
                                msg=cf_set_item(sect,name,value);
                        }
                        if(msg)
@@ -240,9 +263,11 @@ int cf_getopt(int argc,char * const argv[],
 
                }else if(res=='C'){
                        cf_read(optarg);
 
                }else if(res=='C'){
                        cf_read(optarg);
-               }else{  /* unhandled option */
+               }else{
+                       /* unhandled option or end of options */
+                       if(cfdeffile)
+                               cf_read(cfdeffile);
                        return res;
                }
        }while(1);
 }
                        return res;
                }
        }while(1);
 }
-