2 * UCW Library -- Configuration files: getopt wrapper
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include <ucw/conf-internal.h>
14 #include <ucw/getopt.h>
15 #include <ucw/fastbuf.h>
19 #ifndef CONFIG_UCW_DEFAULT_CONFIG
20 #define CONFIG_UCW_DEFAULT_CONFIG NULL
22 char *cf_def_file = CONFIG_UCW_DEFAULT_CONFIG;
24 #ifndef CONFIG_UCW_ENV_VAR_CONFIG
25 #define CONFIG_UCW_ENV_VAR_CONFIG NULL
27 char *cf_env_file = CONFIG_UCW_ENV_VAR_CONFIG;
30 load_default(struct cf_context *cc)
32 if (cc->config_loaded++)
37 if (cf_env_file && (env = getenv(cf_env_file)))
40 die("Cannot load config file %s", env);
42 else if (cf_load(cf_def_file))
43 die("Cannot load default config %s", cf_def_file);
47 // We need to create an empty pool and initialize all configuration items
48 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
51 cf_journal_commit_transaction(1, oldj);
56 end_of_options(struct cf_context *cc)
59 if (cc->postpone_commit && cf_close_group())
60 die("Loading of configuration failed");
64 cf_getopt(int argc, char *const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
66 struct cf_context *cc = cf_get_context();
67 if (!cc->postpone_commit)
72 int res = getopt_long(argc, argv, short_opts, long_opts, long_index);
73 if (res == 'S' || res == 'C' || res == 0x64436667)
75 if (cc->other_options)
76 die("The -S and -C options must precede all other arguments");
81 die("Cannot set %s", optarg);
86 die("Cannot load config file %s", optarg);
88 #ifdef CONFIG_UCW_DEBUG
92 struct fastbuf *b = bfdopen(1, 4096);
101 /* unhandled option or end of options */
102 if (res != ':' && res != '?')