{
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;
-}
/* 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
{
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
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;
{
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)
**/
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. **/
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) {
* 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. **/
/**
{
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");
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)