From: Robert Spalek Date: Thu, 20 Apr 2006 17:31:05 +0000 (+0200) Subject: added declaration and initialization of sections X-Git-Tag: holmes-import~645^2~11^2~100 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=8ab69f51fccccbcae521bd7f7e3ae27146fd1217;p=libucw.git added declaration and initialization of sections --- diff --git a/lib/conf2.c b/lib/conf2.c index 88e17fc7..b82210bd 100644 --- a/lib/conf2.c +++ b/lib/conf2.c @@ -11,6 +11,7 @@ #include "lib/lib.h" #include "lib/conf2.h" #include "lib/mempool.h" +#include "lib/clists.h" #include #include @@ -137,8 +138,54 @@ journal_rollback_section(uns new_pool, struct journal_item *oldj, byte *msg) } } +/* Initialization */ + +static struct section { + struct section *prev; + byte *name; + struct cf_section *sec; +} *sections; + +void +cf_declare_section(byte *name, struct cf_section *sec) +{ + struct section *s = sections; + for (; s; s=s->prev) + if (!strcasecmp(s->name, name)) + die("Cannot register cf_section %s twice", name); + s = xmalloc(sizeof(struct section)); + s->prev = sections; + s->name = name; + s->sec = sec; + sections = s; +} + +void +cf_init_section(byte *name, struct cf_section *sec, void *ptr) +{ + if (sec->size) + bzero(ptr, sec->size); + for (uns i=0; sec->cfg[i].cls; i++) + if (sec->cfg[i].cls == CC_SECTION) + cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr); + else if (sec->cfg[i].cls == CC_LIST) + clist_init(sec->cfg[i].ptr); + byte *msg = sec->init(ptr); + if (msg) + die("Cannot initialize section %s: %s", name, msg); +} + +static void +global_init(void) +{ + for (struct section *s=sections; s; s=s->prev) + cf_init_section(s->name, s->sec, NULL); +} + /* Safe loading and reloading */ +byte *cf_def_file = DEFAULT_CONFIG; + static byte *load_file(byte *file); static byte *load_string(byte *string); @@ -348,3 +395,4 @@ cf_parse_dyn(uns number, byte **pars, void **ptr, enum cf_type type) * (uns*) (*ptr - parsers[type].size) = number; return ((cf_parser*) parsers[type].parser) (number, pars, *ptr); } + diff --git a/lib/conf2.h b/lib/conf2.h index cd2deb51..94e85160 100644 --- a/lib/conf2.h +++ b/lib/conf2.h @@ -48,7 +48,7 @@ struct cf_item { void *ptr; // pointer to a global variable or an offset in a section union { enum cf_type type; // type of a static or dynamic attribute - struct cf_section *sub; // declaration of a section or a list + struct cf_section *sec; // declaration of a section or a list cf_parser *par; // parser function } u; }; @@ -71,8 +71,8 @@ struct clist; #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*), .u.type = CT_##T } #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**), .u.type = CT_##T } #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f } -#define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sub = s } -#define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sub = s } +#define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s } +#define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sec = s } /* Configuration items for basic types */ #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1) #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c) @@ -104,7 +104,12 @@ byte *cf_printf(char *fmt, ...); extern uns cf_need_journal; void cf_journal_block(void *ptr, uns len); +/* Declaration */ +void cf_declare_section(byte *name, struct cf_section *sec); +void cf_init_section(byte *name, struct cf_section *sec, void *ptr); + /* Safe reloading and loading of configuration files */ +extern byte *cf_def_file; byte *cf_reload(byte *file); byte *cf_load(byte *file); byte *cf_set(byte *string);