]> mj.ucw.cz Git - libucw.git/blob - ucw/opt.h
Opt: State of opt-conf moved to opt_context
[libucw.git] / ucw / opt.h
1 /*
2  *      UCW Library -- Parsing of command line options
3  *
4  *      (c) 2013 Jan Moskyto Matejka <mq@ucw.cz>
5  *      (c) 2014 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_OPT_H
12 #define _UCW_OPT_H
13
14 #include <ucw/lib.h>
15 #include <ucw/conf.h>
16
17 #include <stdlib.h>
18 #include <stdio.h>
19
20 #ifdef CONFIG_UCW_CLEAN_ABI
21 #define cf_def_file ucw_cf_def_file
22 #define cf_env_file ucw_cf_env_file
23 #define opt_conf_hook_internal ucw_opt_conf_hook_internal
24 #define opt_failure ucw_opt_failure
25 #define opt_handle_config ucw_opt_handle_config
26 #define opt_handle_dumpconfig ucw_opt_handle_dumpconfig
27 #define opt_handle_help ucw_opt_handle_help
28 #define opt_handle_set ucw_opt_handle_set
29 #define opt_help ucw_opt_help
30 #define opt_parse ucw_opt_parse
31 #endif
32
33 #define OPT_EXIT_BAD_ARGS 2
34
35 /***
36  * [[opt]]
37  ***/
38
39 enum opt_class {
40   OPT_CL_END,     // end of list
41   OPT_CL_BOOL,    // boolean value
42   OPT_CL_STATIC,  // static value
43   OPT_CL_SWITCH,  // lookup/switch
44   OPT_CL_INC,     // incremental value
45   OPT_CL_CALL,    // call a function
46   OPT_CL_USER,    // user defined value
47   OPT_CL_SECTION, // subsection
48   OPT_CL_HELP,    // help line
49   OPT_CL_HOOK,    // hook
50 };
51
52 struct opt_section;
53 struct opt_item {
54   const char * name;                    // long-op
55   int letter;                           // short-op
56   void * ptr;                           // where to save
57   const char * help;                    // description in --help
58   union opt_union {
59     struct opt_section * section;       // subsection for OPT_SECTION
60     int value;                          // value for OPT_SWITCH
61     void (* call)(struct opt_item * opt, const char * value, void * data);  // function to call for OPT_CALL
62     void (* hook)(struct opt_item * opt, uns event, const char * value, void * data);  // function to call for OPT_CL_HOOK
63     struct cf_user_type * utype;        // specification of the user-defined type
64   } u;
65   u16 flags;
66   byte cls;                             // enum opt_class
67   byte type;                            // enum cf_type
68 };
69
70 struct opt_section {
71   struct opt_item * opt;
72 };
73
74 #define OPT_ITEMS       .opt = ( struct opt_item[] )  /** List of sub-items. **/
75
76 /***
77  * Sub-items to be enclosed in OPT_ITEMS { } list
78  * ----------------------------------------------
79  *
80  *  OPT_HELP_OPTION declares --help and prints a line about that
81  *  OPT_HELP prints a line into help
82  *  OPT_BOOL declares boolean option with an auto-negation (--sth and --no-sth). It's also possible to write --sth=y/yes/true/1/n/no/false/0.
83  *  OPT_STRING, OPT_UNS, OPT_INT declare simple string/uns/int option
84  *  OPT_SWITCH declares one choice of a switch statement; these have common target and different `value`s; last wins unless OPT_SINGLE is set;
85  *             parser fails if it matches an OPT_SWITCH with OPT_SINGLE set and also target set.
86  *             Target must be of signed integer type; it is set to -1 if no switch appears at the command-line.
87  *  OPT_CALL calls the given function with an argument, giving also the opt_item structure and some custom data.
88  *  OPT_HOOK is called at the specified place: before option parsing, before value parsing and after value parsing as specified in @flags;
89  *             OPT_HOOK_BEFORE_ARG gets @opt and @value set to NULL;
90  *             OPT_HOOK_BEFORE_VALUE gets both @opt and @value set.
91  *             OPT_HOOK_AFTER_VALUE gets both @opt and @value set.
92  *  OPT_USER declares a custom type of value defined by the given @cf_user_type in @ttype
93  *  OPT_INC declares an incremental value like -v/--verbose
94  *  OPT_SECTION declares a subsection
95  *
96  ***/
97
98 #define OPT_HELP_OPTION OPT_CALL(0, "help", opt_handle_help, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_NO_VALUE, "\tShow this help")
99 #define OPT_HELP(line) { .help = line, .cls = OPT_CL_HELP }
100 #define OPT_BOOL(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_BOOL, .type = CT_INT }
101 #define OPT_STRING(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_STRING }
102 // FIXME: U64 and DOUBLE are not described in the comment above
103 #define OPT_U64(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_U64 }
104 #define OPT_INT(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_INT }
105 #define OPT_DOUBLE(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_DOUBLE }
106 // FIXME: Does IP deserve a basic type? Wouldn't a pre-defined user type be better?
107 // Especially, this would provide an easy extension for IPv6.
108 #define OPT_IP(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_STATIC, .type = CT_IP }
109 // FIXME: Semantics not clear from the description above
110 #define OPT_SWITCH(shortopt, longopt, target, val, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .help = desc, .flags = fl, .cls = OPT_CL_SWITCH, .type = CT_LOOKUP, .u.value = val }
111 #define OPT_CALL(shortopt, longopt, fn, data, fl, desc) { .letter = shortopt, .name = longopt, .ptr = data, .help = desc, .u.call = fn, .flags = fl, .cls = OPT_CL_CALL, .type = CT_USER }
112 #define OPT_USER(shortopt, longopt, target, ttype, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .u.utype = &ttype, .flags = fl, .help = desc, .cls = OPT_CL_USER, .type = CT_USER }
113 // FIXME: Check that the target is of the right type (likewise in other statically typed options)
114 #define OPT_INC(shortopt, longopt, target, fl, desc) { .letter = shortopt, .name = longopt, .ptr = &target, .flags = fl, .help = desc, .cls = OPT_CL_INC, .type = CT_INT }
115 #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec }
116 #define OPT_HOOK(fn, data, events) { .cls = OPT_CL_HOOK, .u.hook = fn, .flags = events, .ptr = data }
117 #define OPT_END { .cls = OPT_CL_END }
118
119 /***
120  * UCW Conf options
121  * ~~~~~~~~~~~~~~~~
122  *
123  * OPT_CONF_OPTIONS declares -C and -S as described in @getopt.h
124  ***/
125
126 #ifdef CONFIG_UCW_DEBUG
127 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_DUMPCONFIG, OPT_CONF_HOOK
128 #else
129 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_HOOK
130 #endif
131
132 #define OPT_CONF_CONFIG     OPT_CALL('C', "config", opt_handle_config, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_REQUIRED_VALUE, "<file>\tOverride the default configuration file")
133 #define OPT_CONF_SET        OPT_CALL('S', "set", opt_handle_set, NULL, OPT_BEFORE_CONFIG | OPT_INTERNAL | OPT_REQUIRED_VALUE, "<item>\tManual setting of a configuration item")
134 #define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_handle_dumpconfig, NULL, OPT_INTERNAL | OPT_NO_VALUE, "\tDump program configuration")
135 #define OPT_CONF_HOOK       OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE | OPT_HOOK_FINAL | OPT_HOOK_INTERNAL)
136
137 void opt_handle_config(struct opt_item * opt, const char * value, void * data);
138 void opt_handle_set(struct opt_item * opt, const char * value, void * data);
139 void opt_handle_dumpconfig(struct opt_item * opt, const char * value, void * data);
140 void opt_conf_hook_internal(struct opt_item * opt, uns event, const char * value, void * data);
141
142 // XXX: This is duplicated with <ucw/getopt.h>, but that one will hopefully go away one day.
143 /**
144  * The default config (as set by `CONFIG_UCW_DEFAULT_CONFIG`) or NULL if already loaded.
145  * You can set it to something else manually.
146  */
147 extern char *cf_def_file;
148 /**
149  * Name of environment variable that can override what configuration is loaded.
150  * Defaults to `CONFIG_UCW_ENV_VAR_CONFIG`.
151  **/
152 extern char *cf_env_file;
153
154 /***
155  * Predefined shortopt arguments
156  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157  *
158  * for the preceding calls if positional args wanted.
159  * Arguments are processed in the order of the numbers given to them. There must be first
160  * the args with OPT_REQUIRED (see below) and after them the args without OPT_REQUIRED, no mixing.
161  * You may define a catch-all option as OPT_POSITIONAL_TAIL. After this, no positional arg is allowed.
162  * You may shuffle the positional arguments in any way in the opt sections but the numbering must obey
163  * the rules given here.
164  ***/
165 // FIXME: The previous paragraph is almost incomprehensible
166
167 // FIXME: Is numbering from 1 natural here?
168 // FIXME: Are there any rules for mixing of positional arguments with options?
169 #define OPT_POSITIONAL(n)   (OPT_POSITIONAL_TAIL+(n))
170 #define OPT_POSITIONAL_TAIL 128
171
172
173 /***
174  * Flags for the preceding calls
175  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176  ***/
177
178 #define OPT_REQUIRED        0x1         /** Argument must appear at the command line **/
179 #define OPT_REQUIRED_VALUE  0x2         /** Argument must have a value **/
180 #define OPT_NO_VALUE        0x4         /** Argument must have no value **/
181 #define OPT_MAYBE_VALUE     0x8         /** Argument may have a value **/
182 #define OPT_VALUE_FLAGS     (OPT_REQUIRED_VALUE | OPT_NO_VALUE | OPT_MAYBE_VALUE)
183 #define OPT_NEGATIVE        0x10        /** Reversing the effect of OPT_INC or saving @false into OPT_BOOL **/
184 #define OPT_NO_HELP         0x20        /** Omit this line from help **/
185 #define OPT_LAST_ARG        0x40        /** Stop processing argv after this line **/
186 #define OPT_SINGLE          0x100       /** Argument must appear at most once **/
187 #define OPT_MULTIPLE        0x200       /** Argument may appear any time; will save all the values into a simple list **/
188 #define OPT_SEEN_AS_LONG    0x400       // Used internally to signal that we currently process the long form of the option
189 #define OPT_BEFORE_CONFIG   0x800       /** Argument may appear before config file is loaded **/
190 #define OPT_INTERNAL        0x4000      // Used internally to ask for passing of struct opt_context to OPT_CALL
191
192 // For hooks, the flags contain a combination of events.
193 #define OPT_HOOK_BEFORE_ARG     0x1     /** Call before option parsing **/
194 #define OPT_HOOK_BEFORE_VALUE   0x2     /** Call before value parsing **/
195 #define OPT_HOOK_AFTER_VALUE    0x4     /** Call after value parsing **/
196 #define OPT_HOOK_FINAL          0x8     /** Call just before opt_parse() returns **/
197 #define OPT_HOOK_INTERNAL       0x4000  // Used internally to ask for passing of struct opt_context
198
199 void opt_failure(const char * mesg, ...) FORMAT_CHECK(printf,1,2) NONRET;
200 void opt_help(const struct opt_section * sec);
201 void opt_handle_help(struct opt_item * opt, const char * value, void * data);
202
203 /**
204  * Parse all arguments, given in a NULL-terminated array of strings.
205  *
206  * Typically, this is called from `main(argc, argv)` as `opt_parse(options, argv+1)`,
207  * skipping the 0th argument, which contains program name.
208  *
209  * Returns the number of arguments used (which need not be all of them
210  * if `OPT_LAST_ARG` was encountered).
211  *
212  * The argument array is left untouched.
213  * However, option values are not necessarily copied, the variables
214  * set by the parser may point to the argument array.
215  **/
216 int opt_parse(const struct opt_section * options, char ** argv);
217
218 #endif