From a7cb891bfea5a2e47d91612eb6dd728dfbfbd45a Mon Sep 17 00:00:00 2001 From: Robert Spalek Date: Sun, 14 Jan 2001 12:16:57 +0000 Subject: [PATCH] lib/conf.[ch] rewritten lib/conf-test.c updated --- lib/conf-test.c | 34 ++++++++------ lib/conf.c | 118 ++++++++++++++++-------------------------------- lib/conf.h | 45 ++++++++++++++---- 3 files changed, 94 insertions(+), 103 deletions(-) diff --git a/lib/conf-test.c b/lib/conf-test.c index 002939b1..6e9891c3 100644 --- a/lib/conf-test.c +++ b/lib/conf-test.c @@ -35,12 +35,13 @@ static byte *set_nastaveni(struct cfitem *item, byte *value) } static struct cfitem jmeno[]={ - {"robert", ct_int, &robert}, - {"spalek", ct_int, &spalek}, - {"heslo", ct_string, &heslo}, - {"nastaveni1", ct_function, &set_nastaveni}, - {"nastaveni2", ct_function, &set_nastaveni}, - {NULL, 0, NULL} + {"jmeno", CT_SECTION, NULL}, + {"robert", CT_INT, &robert}, + {"spalek", CT_INT, &spalek}, + {"heslo", CT_STRING, &heslo}, + {"nastaveni1", CT_FUNCTION, &set_nastaveni}, + {"nastaveni2", CT_FUNCTION, &set_nastaveni}, + {NULL, CT_STOP, NULL} }; static int vek=22; @@ -48,15 +49,17 @@ static int vyska=178; static int vaha=66; static struct cfitem telo[]={ - {"vek", ct_int, &vek}, - {"vyska", ct_int, &vyska}, - {"vaha", ct_int, &vaha}, - {NULL, 0, NULL} + {"telo", CT_SECTION, NULL}, + {"vek", CT_INT, &vek}, + {"vyska", CT_INT, &vyska}, + {"vaha", CT_INT, &vaha}, + {NULL, CT_STOP, NULL} }; -static byte shortopts[] = "abcp:q:r::"; +static byte shortopts[] = CF_SHORT_OPTS "abcp:q:r::"; static struct option longopts[] = { + CF_LONG_OPTS {"ahoj", 0, 0, 'a'}, {"bida", 0, 0, 'b'}, {"citron", 0, 0, 'c'}, @@ -70,12 +73,13 @@ int main(int argc, char *argv[]) { int c; - cf_register("jmeno",jmeno); - cf_register("telo",telo); - cf_register_opts(shortopts,longopts); + initlog(argv[0]); + + cf_register(jmeno); + cf_register(telo); while(1){ - c=cf_getopt(argc,argv,NULL); + c=cf_getopt(argc,argv,shortopts,longopts,NULL); if(c==-1) break; else switch(c){ diff --git a/lib/conf.c b/lib/conf.c index 3eb612b5..9d3b284b 100644 --- a/lib/conf.c +++ b/lib/conf.c @@ -1,70 +1,51 @@ -/* Reading conf files - * Robert Spalek, (c) 2001, robert@ucw.cz - * $Id: conf.c,v 1.2 2001/01/08 10:26:37 robert Exp $ +/* + * Sherlock Library -- Reading configuration files + * + * (c) 2001 Robert Spalek */ +#include "lib/lib.h" + #include #include #include -#include #include #include -#include "lib/lib.h" +#include "lib/chartype.h" #include "lib/fastbuf.h" +#include "lib/lfs.h" #include "lib/conf.h" #define BUFFER 1024 #define MAX_LEVEL 8 -#define MAX_SECTIONS 64 - -static struct { - byte *section; - struct cfitem *items; -} cfsection[MAX_SECTIONS]; -static int cfsections; - -#define MAX_SHORT_OPTS 128 -#define MAX_LONG_OPTS 64 +static struct cfitem *cfsection; -static byte shortopts[MAX_SHORT_OPTS] = "S:C:"; -static int shortlen=4; - -static struct option longopts[MAX_LONG_OPTS] = -{ - {"set", 1, 0, 'S'}, - {"config", 1, 0, 'C'} -}; -static int longlen=2; - -void cf_register(byte *section,struct cfitem *items) +void cf_register(struct cfitem *items) { - if(cfsections>=MAX_SECTIONS) - die("too many modules %d",cfsections); - cfsection[cfsections].section=section; - cfsection[cfsections].items=items; - cfsections++; + if(items[0].type!=CT_SECTION) + die("Invalid configuration section, first item must be of type CT_SECTION"); + items[0].var=cfsection; + cfsection=items; } -static byte *cf_set_item(byte *sect, byte *name, byte *value) +byte *cf_set_item(byte *sect, byte *name, byte *value) { - int idsect; struct cfitem *item; byte *msg=NULL; - idsect=0; - while(idsect=cfsections) /* ignore unknown section */ + item=cfsection; + while(item && strcasecmp(item->name,sect)) + item=item->var; + if(!item) /* ignore unknown section */ return NULL; - item=cfsection[idsect].items; - while(item->type && strcasecmp(name,item->name)) - item++; + for(item++; item->type && strcasecmp(item->name,name); item++); + switch(item->type){ - case ct_int: + case CT_INT: { char *end; *((uns *) item->var) = strtoul(value, &end, 0); @@ -72,10 +53,10 @@ static byte *cf_set_item(byte *sect, byte *name, byte *value) msg = "Invalid number"; break; } - case ct_string: + case CT_STRING: *((byte **) item->var) = stralloc(value); break; - case ct_function: + case CT_FUNCTION: msg = ((ci_func) item->var)(item, value); break; default: @@ -88,21 +69,23 @@ static byte *cf_set_item(byte *sect, byte *name, byte *value) static int cf_subread(byte *filename,int level) { + int fd; struct fastbuf *b; byte def_section[BUFFER]; int line; byte *msg=NULL; if(level>=MAX_LEVEL){ - log("Too many nested files %d",level); + log(L_ERROR "Too many (%d) nested files when reading %s",level,filename); return 0; } - b=bopen(filename,O_RDONLY,4096); - if(!b){ - log("Cannot open file %s",filename); + fd=sh_open(filename,O_RDONLY, 0666); + if(fd<0){ + log(L_ERROR "Cannot open file %s",filename); return 0; } + b=bfdopen(fd,4096); def_section[0]=0; line=0; @@ -115,7 +98,7 @@ static int cf_subread(byte *filename,int level) line++; c=buf; - while(*c && isspace(*c)) + while(*c && Cspace(*c)) c++; if(!*c || *c=='#') continue; @@ -141,7 +124,7 @@ static int cf_subread(byte *filename,int level) name=c; c=strpbrk(c," \t"); - while(c && *c && isspace(*c)) + while(c && *c && Cspace(*c)) *c++=0; if(!c || !*c){ msg="Missing argument"; @@ -166,40 +149,20 @@ static int cf_subread(byte *filename,int level) } /* for every line */ if(msg) - log("%s, line %d: %s",filename,line,msg); + log(L_ERROR "%s, line %d: %s",filename,line,msg); bclose(b); return !msg; } -int cf_read(byte *filename) +void cf_read(byte *filename) { - return cf_subread(filename,0); -} - -void cf_read_err(byte *filename) -{ - if(!cf_read(filename)) + if(!cf_subread(filename,0)) die("Reading config file %s failed",filename); } -void cf_register_opts(byte *so,struct option *lo) -{ - int l; - - l=strlen(so); - if(shortlen+l>=MAX_SHORT_OPTS) - die("Too many short options %d",shortlen+l); - strcat(shortopts,so); - - l=longlen; - while(lo->name){ - if(l>=MAX_LONG_OPTS) - die("Too many long options %d",l); - longopts[l++]=*lo++; - } -} - -int cf_getopt(int argc,char * const argv[], int *longindex) +int cf_getopt(int argc,char * const argv[], + const char *shortopts,const struct option *longopts, + int *longindex) { int res; @@ -210,7 +173,6 @@ int cf_getopt(int argc,char * const argv[], int *longindex) byte *c; byte *msg=NULL; - name=optarg; c=strchr(name,'='); if(!c){ @@ -235,12 +197,10 @@ int cf_getopt(int argc,char * const argv[], int *longindex) die("Invalid command line argument %s.%s=%s: %s",sect,name,value,msg); }else if(res=='C'){ - cf_read_err(optarg); - + cf_read(optarg); }else{ /* unhandled option */ return res; } - }while(1); } diff --git a/lib/conf.h b/lib/conf.h index 015ff0c1..c94f1ad0 100644 --- a/lib/conf.h +++ b/lib/conf.h @@ -1,9 +1,20 @@ -/* Reading conf files - * Robert Spalek, (c) 2001, robert@ucw.cz - * $Id: conf.h,v 1.1 2001/01/07 21:21:53 robert Exp $ +/* + * Sherlock Library -- Reading configuration files + * + * (c) 2001 Robert Spalek */ -enum cftype { ct_stop, ct_int, ct_string, ct_function }; +/* + * Every module places its configuration setting into some section. Section is + * an array of cfitem, whose first record is of type CT_SECTION and contains + * the name of the section. The configuration sections are registered by + * calling cf_register(). + * + * item->var is a pointer to the destination variable or to the special parsing + * function. + */ + +enum cftype { CT_STOP, CT_SECTION, CT_INT, CT_STRING, CT_FUNCTION }; struct cfitem { byte *name; @@ -13,10 +24,26 @@ struct cfitem { typedef byte *(*ci_func)(struct cfitem *, byte *); -void cf_register(byte *section,struct cfitem *items); -void cf_register_opts(byte *so,struct option *lo); +void cf_register(struct cfitem *items); + +/* + * Direct setting of configuration items and parsing the configuration file. + */ + +byte *cf_set_item(byte *sect, byte *name, byte *value); +void cf_read(byte *filename); + +/* + * When using cf_getopt, you must prefix your own short/long options by the + * CF_(SHORT|LONG)_OPTS. + */ + +#define CF_SHORT_OPTS "S:C:" +#define CF_LONG_OPTS \ + {"set", 1, 0, 'S'},\ + {"config", 1, 0, 'C'}, -int cf_read(byte *filename); -void cf_read_err(byte *filename); -int cf_getopt(int argc,char * const argv[], int *longindex); +int cf_getopt(int argc,char * const argv[], + const char *shortopts,const struct option *longopts, + int *longindex); -- 2.39.2