X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fconf.c;h=de6598105cfe08e540eeda36a4725a37b3b37d6e;hb=2c9cf5b6e38800f6d79e5140f3b8565961374627;hp=35f0502b743b007e0591527aabef8a534a24bf08;hpb=3f852dbf02d4a71de5470885e57b11726ad5ac77;p=libucw.git diff --git a/lib/conf.c b/lib/conf.c index 35f0502b..de659810 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -1,5 +1,5 @@ /* - * Sherlock Library -- Reading of configuration files + * UCW Library -- Reading of configuration files * * (c) 2001 Robert Spalek * (c) 2003 Martin Mares @@ -11,7 +11,7 @@ #include "lib/lib.h" #include "lib/chartype.h" #include "lib/fastbuf.h" -#include "lib/pools.h" +#include "lib/mempool.h" #include "lib/conf.h" @@ -28,6 +28,10 @@ static struct cfitem *cfsection; struct mempool *cfpool; +#ifndef DEFAULT_CONFIG +#define DEFAULT_CONFIG NULL +#endif + byte *cfdeffile = DEFAULT_CONFIG; static void CONSTRUCTOR @@ -43,12 +47,9 @@ cfg_malloc(uns size) } byte * -cfg_stralloc(byte *s) +cfg_strdup(byte *s) { - uns l = strlen(s); - byte *k = cfg_malloc(l + 1); - strcpy(k, s); - return k; + return mp_strdup(cfpool, s); } void cf_register(struct cfitem *items) @@ -217,10 +218,10 @@ byte *cf_set_item(byte *sect, byte *name, byte *value) msg = cf_parse_int(value, (uns *) item->var); break; case CT_STRING: - *((byte **) item->var) = cfg_stralloc(value); + *((byte **) item->var) = cfg_strdup(value); break; case CT_FUNCTION: - msg = ((ci_func) item->var)(item, cfg_stralloc(value)); + msg = ((ci_func) item->var)(item, cfg_strdup(value)); break; case CT_DOUBLE: msg = cf_parse_double(value, (double *) item->var); @@ -334,6 +335,41 @@ void cf_read(byte *filename) cfdeffile = NULL; } +static void cf_opt_S(void) +{ + byte *sect,*name,*value; + byte *c; + byte *msg=NULL; + byte arg[strlen(optarg)+1]; + + strcpy(arg, optarg); + name = arg; + c=strchr(name,'='); + if(!c){ + msg="Missing argument"; + sect=value=""; + }else{ + *c++=0; + value=c; + + c=strchr(name,'.'); + if(!c) + sect=""; + else{ + sect=name; + *c++=0; + name=c; + } + + if (cfdeffile) + cf_read(cfdeffile); + msg=cf_set_item(sect,name,value); + } + if(msg) + die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg); + +} + int cf_getopt(int argc,char * const argv[], const char *shortopts,const struct option *longopts, int *longindex) @@ -348,35 +384,7 @@ int cf_getopt(int argc,char * const argv[], die("The -S and -C options must precede all other arguments"); } if(res=='S'){ - byte *sect,*name,*value; - byte *c; - byte *msg=NULL; - - name=optarg; - c=strchr(name,'='); - if(!c){ - msg="Missing argument"; - sect=value=""; - }else{ - *c++=0; - value=c; - - c=strchr(name,'.'); - if(!c) - sect=""; - else{ - sect=name; - *c++=0; - name=c; - } - - if (cfdeffile) - cf_read(cfdeffile); - msg=cf_set_item(sect,name,value); - } - if(msg) - die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg); - + cf_opt_S(); }else if(res=='C'){ cf_read(optarg); }else{