X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fconf2.h;h=0f49ec9ca321990841ad2ea0f1f822cbbde30235;hb=1ab52b1a51cb828a5868bfcca555d6752136fb52;hp=0a847e7daf117025f27dd0b1fb87da6e5a8f57c5;hpb=8ef9f8db115f0d0f136d3b6ebc4d3a77069ca406;p=libucw.git diff --git a/lib/conf2.h b/lib/conf2.h index 0a847e7d..0f49ec9c 100644 --- a/lib/conf2.h +++ b/lib/conf2.h @@ -22,6 +22,7 @@ enum cf_class { enum cf_type { CT_INT, CT_U64, CT_DOUBLE, // number types + CT_IP, // IP address CT_STRING // string type }; @@ -58,6 +59,7 @@ struct cf_section { cf_hook *init; // fills in default values (otherwise 0's are used) cf_hook *commit; // verifies parsed data (optional) struct cf_item *cfg; // CC_END-terminated array of items + uns flags; // for internal use only }; /* Declaration of cf_section */ @@ -86,6 +88,9 @@ struct clist; #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1) #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c) #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c) +#define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1) +#define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c) +#define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c) #define CF_STRING(n,p) CF_STATIC(n,p,STRING,byte*,1) #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,byte*,c) #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,byte*,c) @@ -101,25 +106,52 @@ extern struct mempool *cf_pool; void *cf_malloc(uns size); void *cf_malloc_zero(uns size); byte *cf_strdup(byte *s); -byte *cf_printf(char *fmt, ...); +byte *cf_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2); /* Undo journal for error recovery */ 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_declare_section(byte *name, struct cf_section *sec, uns allow_unknown); 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); +int cf_reload(byte *file); +int cf_load(byte *file); +int cf_set(byte *string); /* Parsers for basic types */ byte *cf_parse_int(byte *str, int *ptr); byte *cf_parse_u64(byte *str, u64 *ptr); byte *cf_parse_double(byte *str, double *ptr); +byte *cf_parse_ip(byte *p, u32 *varp); + +/* + * When using cf_get_opt(), you must prefix your own short/long options by the + * CF_(SHORT|LONG)_OPTS. + * + * cf_def_file contains the name of a configuration file that will be + * automatically loaded before the first --set option is executed. If no --set + * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end + * of the configuration options). cf_def_file will be ignored if another + * configuration file has already been loaded using the --config option. The + * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can + * override it manually before calling cf_get_opt(). + */ + +#define CF_SHORT_OPTS "S:C:" +#define CF_LONG_OPTS {"set", 1, 0, 'S'}, {"config", 1, 0, 'C'}, +#define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } } +#ifndef CF_USAGE_TAB +#define CF_USAGE_TAB "" +#endif +#define CF_USAGE \ +"-S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n\ +-C, --config filename\t" CF_USAGE_TAB "Overwrite default configuration file\n" + +struct option; +int cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index); #endif