2 * UCW Library -- Parsing of configuration and command-line options
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.
14 #ifdef CONFIG_UCW_CLEAN_ABI
15 #define cf_def_file ucw_cf_def_file
16 #define cf_env_file ucw_cf_env_file
17 #define cf_getopt ucw_cf_getopt
18 #define reset_getopt ucw_reset_getopt
21 #ifdef CONFIG_UCW_OWN_GETOPT
22 #include <ucw/getopt/getopt-sh.h>
29 * Loading by @cf_getopt()
30 * ~~~~~~~~~~~~~~~~~~~~~~~
34 * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded.
35 * You can set it to something else manually.
37 extern char *cf_def_file;
39 * Name of environment variable that can override what configuration is loaded.
40 * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
42 extern char *cf_env_file;
44 * Short options for loading configuration by @cf_getopt().
45 * Prepend to your own options.
47 #define CF_SHORT_OPTS "C:S:"
49 * Long options for loading configuration by @cf_getopt().
50 * Prepend to your own options.
52 #define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'}, CF_LONG_OPTS_DEBUG
54 * Use this constant as @long_opts parameter of @cf_getopt() if you do
55 * not have any long options in your program.
57 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
59 #define CF_USAGE_TAB ""
62 * This macro provides text describing usage of the configuration
63 * loading options. Concatenate with description of your options and
64 * write to the user, if he/she provides invalid options.
67 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
68 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n" CF_USAGE_DEBUG
70 #ifdef CONFIG_UCW_DEBUG
71 #define CF_LONG_OPTS_DEBUG { "dumpconfig", 0, 0, 0x64436667 } ,
72 #define CF_USAGE_DEBUG " --dumpconfig\t" CF_USAGE_TAB "Dump program configuration\n"
74 #define CF_LONG_OPTS_DEBUG
75 #define CF_USAGE_DEBUG
79 * Takes care of parsing the command-line arguments, loading the
80 * default configuration file (<<var_cf_def_file,`cf_def_file`>>) and processing
81 * configuration options. The calling convention is the same as with GNU getopt_long(),
82 * but you must prefix your own short/long options by the
83 * <<def_CF_LONG_OPTS,`CF_LONG_OPTS`>> or <<def_CF_SHORT_OPTS,`CF_SHORT_OPTS`>> or
84 * pass <<def_CF_NO_LONG_OPTS,`CF_NO_LONG_OPTS`>> if there are no long options.
86 * The default configuration file can be overwritten by the --config options,
87 * which must come first. During parsing of all other options, the configuration
88 * is already available.
90 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
92 void reset_getopt(void); /** If you want to start parsing of the arguments from the first one again. **/