X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fconf.c;h=35f0502b743b007e0591527aabef8a534a24bf08;hb=8dbbd028b76f4813817ab89367bb182a9d6e82f9;hp=b6cf5237297953fe2a6b04ab133e0815aff8639d;hpb=ddd63d22bcda367a5fad4941bd1c96334de9367d;p=libucw.git diff --git a/lib/conf.c b/lib/conf.c index b6cf5237..35f0502b 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -1,28 +1,34 @@ /* - * Sherlock Library -- Reading configuration files + * Sherlock Library -- Reading of configuration files * * (c) 2001 Robert Spalek + * (c) 2003 Martin Mares + * + * 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/chartype.h" +#include "lib/fastbuf.h" +#include "lib/pools.h" + +#include "lib/conf.h" #include #include #include #include #include - -#include "lib/chartype.h" -#include "lib/fastbuf.h" -#include "lib/pools.h" - -#include "lib/conf.h" +#include #define BUFFER 1024 #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) @@ -39,55 +45,191 @@ cfg_malloc(uns size) byte * cfg_stralloc(byte *s) { - uns l = strlen(s); - byte *k = cfg_malloc(l + 1); - strcpy(k, s); - return k; + 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 */ +} + +struct unit { + uns name; /* One-letter name of the unit */ + uns num, den; /* Fraction */ +}; + +static const struct unit units[] = { + { 'd', 86400, 1 }, + { 'h', 3600, 1 }, + { 'k', 1000, 1 }, + { 'm', 1000000, 1 }, + { 'g', 1000000000, 1 }, + { 'K', 1024, 1 }, + { 'M', 1048576, 1 }, + { 'G', 1073741824, 1 }, + { '%', 1, 100 }, + { 0, 0, 0 } +}; + +static const struct unit *cf_lookup_unit(byte *value, byte *end, char **msg) +{ + if (end && *end) { + if (end == value || end[1] || *end >= '0' && *end <= '9') + *msg = "Invalid number"; + else { + for (const struct unit *u=units; u->name; u++) + if (u->name == *end) + return u; + *msg = "Invalid unit"; + } + } + return NULL; +} + +static char cf_rngerr[] = "Number out of range"; + +byte *cf_parse_int(byte *value, uns *varp) +{ + char *msg = NULL; + const struct unit *u; + + if (!*value) + msg = "Missing number"; + else { + errno = 0; + char *end; + uns x = strtoul(value, &end, 0); + if (errno == ERANGE) + msg = cf_rngerr; + else if (u = cf_lookup_unit(value, end, &msg)) { + u64 y = (u64)x * u->num; + if (y % u->den) + msg = "Number is not an integer"; + else { + y /= u->den; + if (y > 0xffffffff) + msg = cf_rngerr; + *varp = y; + } + } else + *varp = x; + } + return msg; +} + +byte *cf_parse_u64(byte *value, u64 *varp) +{ + char *msg = NULL; + const struct unit *u; + + if (!*value) + msg = "Missing number"; + else { + errno = 0; + char *end; + u64 x = strtoull(value, &end, 0); + if (errno == ERANGE) + msg = cf_rngerr; + else if (u = cf_lookup_unit(value, end, &msg)) { + if (x > ~(u64)0 / u->num) + msg = "Number out of range"; + else { + x *= u->num; + if (x % u->den) + msg = "Number is not an integer"; + else + *varp = x / u->den; + } + } else + *varp = x; + } + return msg; +} + +byte *cf_parse_double(byte *value, double *varp) +{ + char *msg = NULL; + const struct unit *u; + + if (!*value) + msg = "Missing number"; + else { + errno = 0; + char *end; + double x = strtoul(value, &end, 0); + if (errno == ERANGE) + msg = cf_rngerr; + else if (u = cf_lookup_unit(value, end, &msg)) + *varp = x * u->num / u->den; + else + *varp = x; + } + return msg; +} + +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; - if(!*value) - msg="Missing number"; - else{ - *((uns *) item->var) = strtoul(value, &end, 0); - if (end && *end) - msg = "Invalid number"; - } - break; - } + msg = cf_parse_int(value, (uns *) item->var); + break; case CT_STRING: *((byte **) item->var) = cfg_stralloc(value); break; case CT_FUNCTION: msg = ((ci_func) item->var)(item, cfg_stralloc(value)); break; + case CT_DOUBLE: + msg = cf_parse_double(value, (double *) item->var); + break; + case CT_U64: + msg = cf_parse_u64(value, (u64 *) item->var); + break; default: msg = "Unknown keyword"; - break; } return msg; @@ -189,6 +331,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[], @@ -196,9 +339,14 @@ int cf_getopt(int argc,char * const argv[], int *longindex) { int res; + static int other_options; do{ res=getopt_long(argc,argv,shortopts,longopts,longindex); + if(res == 'S' || res == 'C') { + if(other_options) + die("The -S and -C options must precede all other arguments"); + } if(res=='S'){ byte *sect,*name,*value; byte *c; @@ -222,16 +370,21 @@ int cf_getopt(int argc,char * const argv[], 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",sect,name,value,msg); + die("Invalid command line argument -S%s.%s=%s: %s",sect,name,value,msg); }else if(res=='C'){ cf_read(optarg); - }else{ /* unhandled option */ + }else{ + /* unhandled option or end of options */ + if(cfdeffile) + cf_read(cfdeffile); + other_options++; return res; } }while(1); } -