From b541de765129b6bf28c5601bb355779652001150 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 28 Apr 2012 19:41:58 +0200 Subject: [PATCH] Conf: Revive cf_def_file and cf_env_file They are tightly coupled to cf_getopt(), which is not reentrant anyway, so let us leave them global to keep backward compatibility. --- ucw/conf-context.c | 16 ---------------- ucw/conf-input.c | 18 ++++++++++++++---- ucw/conf-internal.h | 2 -- ucw/conf-section.c | 2 +- ucw/conf-test.c | 2 +- ucw/conf.h | 13 ------------- ucw/doc/conf.txt | 2 +- ucw/getopt.h | 10 ++++++++++ ucw/mempool.c | 2 +- ucw/redblack-test.c | 2 +- 10 files changed, 29 insertions(+), 40 deletions(-) diff --git a/ucw/conf-context.c b/ucw/conf-context.c index dc74b66a..1584143c 100644 --- a/ucw/conf-context.c +++ b/ucw/conf-context.c @@ -61,23 +61,7 @@ cf_obtain_context(void) { struct cf_context *cc = cf_new_context(); uc->cf_context = cc; - cc->def_file = CONFIG_UCW_DEFAULT_CONFIG; - cc->env_file = CONFIG_UCW_ENV_VAR_CONFIG; cc->is_active = 1; } return uc->cf_context; } - -void -cf_set_default_file(char *name) -{ - struct cf_context *cc = cf_obtain_context(); - cc->def_file = name; -} - -void -cf_set_env_override(char *name) -{ - struct cf_context *cc = cf_obtain_context(); - cc->env_file = name; -} diff --git a/ucw/conf-input.c b/ucw/conf-input.c index fff753ce..92acb35d 100644 --- a/ucw/conf-input.c +++ b/ucw/conf-input.c @@ -426,21 +426,31 @@ cf_set(const char *string) /* Command-line parser */ +#ifndef CONFIG_UCW_DEFAULT_CONFIG +#define CONFIG_UCW_DEFAULT_CONFIG NULL +#endif +char *cf_def_file = CONFIG_UCW_DEFAULT_CONFIG; + +#ifndef CONFIG_UCW_ENV_VAR_CONFIG +#define CONFIG_UCW_ENV_VAR_CONFIG NULL +#endif +char *cf_env_file = CONFIG_UCW_ENV_VAR_CONFIG; + static void load_default(struct cf_context *cc) { if (cc->def_loaded++) return; - if (cc->def_file) + if (cf_def_file) { char *env; - if (cc->env_file && (env = getenv(cc->env_file))) + if (cf_env_file && (env = getenv(cf_env_file))) { if (cf_load(env)) die("Cannot load config file %s", env); } - else if (cf_load(cc->def_file)) - die("Cannot load default config %s", cc->def_file); + else if (cf_load(cf_def_file)) + die("Cannot load default config %s", cf_def_file); } else { diff --git a/ucw/conf-internal.h b/ucw/conf-internal.h index cfd2dbde..7f10a85f 100644 --- a/ucw/conf-internal.h +++ b/ucw/conf-internal.h @@ -43,8 +43,6 @@ struct cf_context { struct mempool *pool; int is_active; int need_journal; - char *def_file; - char *env_file; int def_loaded; struct cf_parser_state *parser; uns everything_committed; // after the 1st load, this flag is set on diff --git a/ucw/conf-section.c b/ucw/conf-section.c index 4448ada6..c7bd3247 100644 --- a/ucw/conf-section.c +++ b/ucw/conf-section.c @@ -94,7 +94,7 @@ inspect_section(struct cf_section *sec) void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown) { - struct cf_context *cc = cf_get_context(); + struct cf_context *cc = cf_obtain_context(); if (!cc->sections.cfg) { cc->sections.size = 50; diff --git a/ucw/conf-test.c b/ucw/conf-test.c index 29653f53..34d1f36b 100644 --- a/ucw/conf-test.c +++ b/ucw/conf-test.c @@ -193,7 +193,7 @@ main(int argc, char *argv[]) { log_init(argv[0]); cf_declare_section("top", &cf_top, 0); - cf_set_default_file("ucw/conf-test.cf"); + cf_def_file = "ucw/conf-test.cf"; int opt; while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0) diff --git a/ucw/conf.h b/ucw/conf.h index bf106274..a15d0da2 100644 --- a/ucw/conf.h +++ b/ucw/conf.h @@ -55,19 +55,6 @@ struct cf_context *cf_switch_context(struct cf_context *cc); **/ struct cf_context *cf_obtain_context(void); -/** - * Set name of default configuration file. May be NULL if there should be - * no such default. - * FIXME: Explain where it is used - **/ -void cf_set_default_file(char *name); - -/** - * Set name of environment variable used to override the name of the default - * configuration file. May be NULL if there should be no such variable. - **/ -void cf_set_env_override(char *name); - /*** === Data types [[conf_types]] ***/ enum cf_class { /** Class of the configuration item. **/ diff --git a/ucw/doc/conf.txt b/ucw/doc/conf.txt index 37f2a294..31698a62 100644 --- a/ucw/doc/conf.txt +++ b/ucw/doc/conf.txt @@ -114,7 +114,7 @@ configuration files. static int verbose; int main(int argc, char *argv[]) { - cf_set_default_file("default.cf"); + cf_def_file = "default.cf"; int opt; while((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0) switch(opt) { diff --git a/ucw/getopt.h b/ucw/getopt.h index 919c98d2..6263d47f 100644 --- a/ucw/getopt.h +++ b/ucw/getopt.h @@ -27,6 +27,16 @@ void reset_getopt(void); /** If you want to start parsing of the arguments from * These functions can be used to to safely load or reload configuration. */ +/** + * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded. + * You can set it to something else manually. + */ +extern char *cf_def_file; +/** + * Name of environment variable that can override what configuration is loaded. + * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`. + **/ +extern char *cf_env_file; int cf_reload(const char *file); /** Reload configuration from @file, replace the old one. **/ int cf_load(const char *file); /** Load configuration from @file. If @file is NULL, reload all loaded configuration files. **/ /** diff --git a/ucw/mempool.c b/ucw/mempool.c index e8e30e46..521724a8 100644 --- a/ucw/mempool.c +++ b/ucw/mempool.c @@ -374,7 +374,7 @@ int main(int argc, char **argv) { srand(time(NULL)); log_init(argv[0]); - cf_set_default_file(NULL); + cf_def_file = NULL; if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 || argc != optind) die("Invalid usage"); diff --git a/ucw/redblack-test.c b/ucw/redblack-test.c index 911f8250..191473db 100644 --- a/ucw/redblack-test.c +++ b/ucw/redblack-test.c @@ -127,7 +127,7 @@ main(int argc, char **argv) struct my_tree t; struct my2_tree t2; int i; - cf_set_default_file(NULL); + cf_def_file = NULL; log_init(argv[0]); while ((opt = cf_getopt(argc, argv, options, CF_NO_LONG_OPTS, NULL)) >= 0) switch (opt) -- 2.39.2