]> mj.ucw.cz Git - libucw.git/blobdiff - lib/conf.c
The StringMap file contains an end marker pointing past the last
[libucw.git] / lib / conf.c
index 34cede8feb8e007606465a21dea3554134b24a01..c96d9fb304cd71e029eab9353e4abdbde4baab8f 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)
 {
@@ -31,19 +55,40 @@ void cf_register(struct cfitem *items)
        cfsection=items;
 }
 
-byte *cf_set_item(byte *sect, byte *name, byte *value)
+int cf_item_count(void)
+{
+       struct cfitem *sect, *item;
+       int count = 0;
+       for (sect = cfsection; sect; sect = sect->var)
+               for (item = sect+1; sect->type; sect++)
+                       count++;
+       return count;
+}
+
+struct cfitem *cf_get_item(byte *sect, byte *name)
 {
        struct cfitem *item;
-       byte *msg=NULL;
 
        item=cfsection;
        while(item && strcasecmp(item->name,sect))
                item=item->var;
-       if(!item)       /* ignore unknown section */
+       if(!item)       /* unknown section */
                return NULL;
 
        for(item++; item->type && strcasecmp(item->name,name); item++);
 
+       return item;    /* item->type == 0 if not found */
+}
+
+byte *cf_set_item(byte *sect, byte *name, byte *value)
+{
+       struct cfitem *item;
+       byte *msg=NULL;
+
+       item=cf_get_item(sect,name);
+       if(!item)       /* ignore unknown section */
+               return NULL;
+
        switch(item->type){
                case CT_INT:
                        {
@@ -58,10 +103,10 @@ byte *cf_set_item(byte *sect, byte *name, byte *value)
                                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";
@@ -84,7 +129,7 @@ static int cf_subread(byte *filename,int level)
                return 0;
        }
                
-       fd=sh_open(filename,O_RDONLY, 0666);
+       fd=open(filename,O_RDONLY, 0666);
        if(fd<0){
                log(L_ERROR,"Cannot open configuration file %s: %m",filename);
                return 0;
@@ -167,6 +212,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[],
@@ -200,6 +246,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)
@@ -207,6 +255,10 @@ int cf_getopt(int argc,char * const argv[],
 
                }else if(res=='C'){
                        cf_read(optarg);
+               }else if(res==-1){
+                       if(cfdeffile)
+                               cf_read(cfdeffile);
+                       return res;
                }else{  /* unhandled option */
                        return res;
                }