]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf.c
Added block hash function.
[libucw.git] / lib / conf.c
index 9d3b284bf253dee435efe637fd7059507e9cd8ba..c37bb98b81b7982d4409c77f4d1f962e4875f8ae 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "lib/chartype.h"
 #include "lib/fastbuf.h"
-#include "lib/lfs.h"
+#include "lib/pools.h"
 
 #include "lib/conf.h"
 
 #define        MAX_LEVEL       8
 
 static struct cfitem *cfsection;
+static struct mempool *cfpool;
+
+byte *cfdeffile = DEFAULT_CONFIG;
+
+static void CONSTRUCTOR
+conf_init(void)
+{
+       cfpool = mp_new(4096);
+}
+
+void *
+cfg_malloc(uns size)
+{
+       return mp_alloc(cfpool, size);
+}
+
+byte *
+cfg_stralloc(byte *s)
+{
+  uns l = strlen(s);
+  byte *k = cfg_malloc(l + 1);
+  strcpy(k, s);
+  return k;
+}
 
 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;
 }
 
-byte *cf_set_item(byte *sect, byte *name, byte *value)
+int cf_item_count(void)
 {
-       struct cfitem *item;
-       byte *msg=NULL;
+       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 *item, *section;
 
        item=cfsection;
        while(item && strcasecmp(item->name,sect))
                item=item->var;
-       if(!item)       /* ignore unknown section */
+       if(!item)       /* unknown section */
                return NULL;
+       section = 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 */
+}
+
+byte *cf_set_item(byte *sect, byte *name, byte *value)
+{
+       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;
 
        switch(item->type){
                case CT_INT:
                        {
                                char *end;
-                               *((uns *) item->var) = strtoul(value, &end, 0);
-                               if (end && *end)
-                                       msg = "Invalid number";
+                               if(!*value)
+                                       msg="Missing number";
+                               else{
+                                       *((uns *) item->var) = strtoul(value, &end, 0);
+                                       if (end && *end)
+                                               msg = "Invalid number";
+                               }
                                break;
                        }
                case CT_STRING:
-                       *((byte **) item->var) = stralloc(value);
+                       *((byte **) item->var) = cfg_stralloc(value);
                        break;
                case CT_FUNCTION:
-                       msg = ((ci_func) item->var)(item, value);
+                       msg = ((ci_func) item->var)(item, cfg_stralloc(value));
                        break;
                default:
                        msg = "Unknown keyword";
@@ -76,13 +130,13 @@ static int cf_subread(byte *filename,int level)
        byte *msg=NULL;
 
        if(level>=MAX_LEVEL){
-               log(L_ERROR "Too many (%d) nested files when reading %s",level,filename);
+               log(L_ERROR,"Too many (%d) nested files when reading %s",level,filename);
                return 0;
        }
                
-       fd=sh_open(filename,O_RDONLY, 0666);
+       fd=open(filename,O_RDONLY, 0666);
        if(fd<0){
-               log(L_ERROR "Cannot open file %s",filename);
+               log(L_ERROR,"Cannot open configuration file %s: %m",filename);
                return 0;
        }
        b=bfdopen(fd,4096);
@@ -97,6 +151,9 @@ static int cf_subread(byte *filename,int level)
                        break;
                line++;
 
+               c=buf+strlen(buf);
+               while(c>buf && Cspace(c[-1]))
+                       *--c=0;
                c=buf;
                while(*c && Cspace(*c))
                        c++;
@@ -106,42 +163,44 @@ static int cf_subread(byte *filename,int level)
                if(*c=='['){
                        strcpy(def_section,c+1);
                        c=strchr(def_section,']');
-                       if(c)
+                       if(c){
                                *c=0;
-                       else{
+                               if(c[1]){
+                                       msg="Garbage after ]";
+                                       break;
+                               }
+                       }else{
                                msg="Missing ]";
                                break;
                        }
 
-               }else if(*c=='<'){
-                       if(!cf_subread(c+1,level+1)){
-                               msg="";
-                               break;
-                       }
-               
                }else{
                        byte *sect,*name,*value;
 
                        name=c;
-                       c=strpbrk(c," \t");
-                       while(c && *c && Cspace(*c))
+                       while(*c && !Cspace(*c))
+                               c++;
+                       while(*c && Cspace(*c))
                                *c++=0;
-                       if(!c || !*c){
-                               msg="Missing argument";
-                               break;
-                       }
                        value=c;
 
-                       c=strchr(name,'.');
-                       if(!c)
-                               sect=def_section;
-                       else{
-                               sect=name;
-                               *c++=0;
-                               name=c;
-                       }
+                       if(!strcasecmp(name,"include")){
+                               if(!cf_subread(value,level+1)){
+                                       msg="Included from here";
+                                       break;
+                               }
+                       }else{
+                               c=strchr(name,'.');
+                               if(!c)
+                                       sect=def_section;
+                               else{
+                                       sect=name;
+                                       *c++=0;
+                                       name=c;
+                               }
 
-                       msg=cf_set_item(sect,name,value);
+                               msg=cf_set_item(sect,name,value);
+                       }
                        if(msg)
                                break;
                }
@@ -149,7 +208,7 @@ static int cf_subread(byte *filename,int level)
        }       /* for every line */
 
        if(msg)
-               log(L_ERROR "%s, line %d: %s",filename,line,msg);
+               log(L_ERROR,"%s, line %d: %s",filename,line,msg);
        bclose(b);
        return !msg;
 }
@@ -158,6 +217,7 @@ void cf_read(byte *filename)
 {
        if(!cf_subread(filename,0))
                die("Reading config file %s failed",filename);
+       cfdeffile = NULL;
 }
 
 int cf_getopt(int argc,char * const argv[],
@@ -191,6 +251,8 @@ int cf_getopt(int argc,char * const argv[],
                                        name=c;
                                }
 
+                               if (cfdeffile)
+                                       cf_read(cfdeffile);
                                msg=cf_set_item(sect,name,value);
                        }
                        if(msg)
@@ -198,9 +260,11 @@ int cf_getopt(int argc,char * const argv[],
 
                }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);
 }
-