]> mj.ucw.cz Git - libucw.git/blob - ucw/getopt.h
Packages: Fixed installation of obsolete ucw-daemon-helper.
[libucw.git] / ucw / getopt.h
1 /*
2  *      UCW Library -- Parsing of configuration and command-line options
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2012 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_GETOPT_H
12 #define _UCW_GETOPT_H
13
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
19 #endif
20
21 #ifdef CONFIG_UCW_OWN_GETOPT
22 #include <ucw/getopt/getopt-sh.h>
23 #else
24 #include <getopt.h>
25 #endif
26
27 /***
28  * [[conf_getopt]]
29  * Loading by @cf_getopt()
30  * ~~~~~~~~~~~~~~~~~~~~~~~
31  ***/
32
33 /**
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.
36  */
37 extern char *cf_def_file;
38 /**
39  * Name of environment variable that can override what configuration is loaded.
40  * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
41  **/
42 extern char *cf_env_file;
43 /**
44  * Short options for loading configuration by @cf_getopt().
45  * Prepend to your own options.
46  **/
47 #define CF_SHORT_OPTS   "C:S:"
48 /**
49  * Long options for loading configuration by @cf_getopt().
50  * Prepend to your own options.
51  **/
52 #define CF_LONG_OPTS    {"config",      1, 0, 'C'}, {"set",             1, 0, 'S'}, CF_LONG_OPTS_DEBUG
53 /**
54  * Use this constant as @long_opts parameter of @cf_getopt() if you do
55  * not have any long options in your program.
56  **/
57 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
58 #ifndef CF_USAGE_TAB
59 #define CF_USAGE_TAB ""
60 #endif
61 /**
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.
65  **/
66 #define CF_USAGE        \
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
69
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"
73 #else
74 #define CF_LONG_OPTS_DEBUG
75 #define CF_USAGE_DEBUG
76 #endif
77
78 /**
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.
85  *
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.
89  **/
90 int cf_getopt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);
91
92 void reset_getopt(void);        /** If you want to start parsing of the arguments from the first one again. **/
93
94 #endif