]> mj.ucw.cz Git - libucw.git/commitdiff
Conf: Revive cf_def_file and cf_env_file
authorMartin Mares <mj@ucw.cz>
Sat, 28 Apr 2012 17:41:58 +0000 (19:41 +0200)
committerMartin Mares <mj@ucw.cz>
Sun, 29 Apr 2012 11:57:30 +0000 (13:57 +0200)
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
ucw/conf-input.c
ucw/conf-internal.h
ucw/conf-section.c
ucw/conf-test.c
ucw/conf.h
ucw/doc/conf.txt
ucw/getopt.h
ucw/mempool.c
ucw/redblack-test.c

index dc74b66a9509d724bb6f2d3df75b174983906bec..1584143cb98a24b959dda540935105d2ea084620 100644 (file)
@@ -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;
-}
index fff753ce01743606735983ca6ec7bcabef7272c5..92acb35d9baf831d377788549343730ba0fc3861 100644 (file)
@@ -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
     {
index cfd2dbde8db99ac0af9caf0694cb16aeca3b507d..7f10a85fa911ce3e459ebae2f6332064e21242ba 100644 (file)
@@ -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
index 4448ada6afc3da053582a031264d933d6c223862..c7bd32473be5c3bb8d686f5578ff8ab86d023045 100644 (file)
@@ -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;
index 29653f5338f04553385ae2cc8d244dcfe0b0739d..34d1f36bc9e316350bd8d2eab7194423037144c8 100644 (file)
@@ -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)
index bf106274b2f239ad900041a8a2fd0ee2743da9c3..a15d0da28c2eca376bb904e4eb552273efd8371b 100644 (file)
@@ -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. **/
index 37f2a294daa03aca73551ea8c92a416e50ae57ba..31698a62b9d6f4fafb8ad78a6c6b1c535844356d 100644 (file)
@@ -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) {
index 919c98d29ffe6107981206935e33e0549619ebbf..6263d47fb6df6acd8e19968216cab5cd9614efbf 100644 (file)
@@ -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. **/
 /**
index e8e30e46f3cd38930f32548e27b351fc1b44f60f..521724a8b582accde6f68a649f793c0d5c05e6ba 100644 (file)
@@ -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");
 
index 911f8250a76ec2343bb8b3bfc02b4400322b7c40..191473db85542efe53a8842345c4f7cfc31f3e89 100644 (file)
@@ -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)