]> mj.ucw.cz Git - libucw.git/blob - ucw/opt.h
Opt: Merged fixes from Gigamail.
[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  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_OPT_H
11 #define _UCW_OPT_H
12
13 #include <ucw/lib.h>
14 #include <ucw/conf.h>
15
16 #include <stdlib.h>
17 #include <stdio.h>
18
19 #ifdef CONFIG_UCW_CLEAN_ABI
20 #define opt_conf_hook_internal ucw_opt_conf_hook_internal
21 #define opt_conf_internal ucw_opt_conf_internal
22 #define opt_help_internal ucw_opt_help_internal
23 #define opt_parse ucw_opt_parse
24 #define opt_section_root ucw_opt_section_root
25 #endif
26
27 #define OPT_EXIT_BAD_ARGS 2
28
29 /***
30  * [[opt]]
31  ***/
32
33 enum opt_class {
34   OPT_CL_END,     // end of list
35   OPT_CL_BOOL,    // boolean value
36   OPT_CL_STATIC,  // static value
37   OPT_CL_SWITCH,  // lookup/switch
38   OPT_CL_INC,     // incremental value
39   OPT_CL_CALL,    // call a function
40   OPT_CL_USER,    // user defined value
41   OPT_CL_SECTION, // subsection
42   OPT_CL_HELP,    // help line
43   OPT_CL_HOOK,    // hook
44 };
45
46 struct opt_section;
47 struct opt_item {
48   const char * name;                    // long-op
49   int letter;                           // short-op
50   void * ptr;                           // where to save
51   const char * help;                    // description in --help
52   union opt_union {
53     struct opt_section * section;       // subsection for OPT_SECTION
54     int value;                          // value for OPT_SWITCH
55     void (* call)(struct opt_item * opt, const char * value, void * data);  // function to call for OPT_CALL
56     struct cf_user_type * utype;        // specification of the user-defined type
57   } u;
58   u16 flags;
59   byte cls;                             // enum opt_class
60   byte type;                            // enum cf_type
61 };
62
63 struct opt_section {
64   struct opt_item * opt;
65 };
66
67 #define OPT_ITEMS       .opt = ( struct opt_item[] )  /** List of sub-items. **/
68
69 /***
70  * Sub-items to be enclosed in OPT_ITEMS { } list
71  * ----------------------------------------------
72  *
73  *  OPT_HELP_OPTION declares --help and prints a line about that
74  *  OPT_HELP prints a line into help
75  *  OPT_HELP2 prints two strings onto a line using the same tab structure as the option listing
76  *  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.
77  *  OPT_STRING, OPT_UNS, OPT_INT declare simple string/uns/int option
78  *  OPT_SWITCH declares one choice of a switch statement; these have common target and different `value`s; last wins unless OPT_SINGLE is set;
79  *             parser fails if it matches an OPT_SWITCH with OPT_SINGLE set and also target set.
80  *             Target must be of signed integer type; it is set to -1 if no switch appears at the command-line.
81  *  OPT_CALL calls the given function with an argument, giving also the opt_item structure and some custom data.
82  *  OPT_HOOK is called at the specified place: before option parsing, before value parsing and after value parsing as specified in @flags;
83  *             OPT_HOOK_BEFORE_ARG gets @opt and @value set to NULL;
84  *             OPT_HOOK_BEFORE_VALUE gets both @opt and @value set.
85  *             OPT_HOOK_AFTER_VALUE gets both @opt and @value set.
86  *  OPT_USER declares a custom type of value defined by the given @cf_user_type in @ttype
87  *  OPT_INC declares an incremental value like -v/--verbose
88  *  OPT_SECTION declares a subsection
89  *
90  ***/
91
92 #define OPT_HELP_OPTION(help) OPT_CALL(0, "help", opt_show_help_internal, &help, OPT_NO_VALUE, "Show this help")
93 #define OPT_HELP(line) { .help = line, .cls = OPT_CL_HELP }
94 #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 }
95 #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 }
96 // FIXME: U64 and DOUBLE are not described in the comment above
97 #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 }
98 #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 }
99 #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 }
100 // FIXME: Does IP deserve a basic type? Wouldn't a pre-defined user type be better?
101 // Especially, this would provide an easy extension for IPv6.
102 #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 }
103 // FIXME: Semantics not clear from the description above
104 #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 }
105 #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 }
106 #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 }
107 // FIXME: Check that the target is of the right type (likewise in other statically typed options)
108 #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 }
109 #define OPT_SECTION(sec) { .cls = OPT_CL_SECTION, .u.section = &sec }
110 #define OPT_HOOK(fn, data, fl) { .cls = OPT_CL_HOOK, .u.call = fn, .flags = OPT_NO_HELP | fl, .ptr = data }
111 #define OPT_END { .cls = OPT_CL_END }
112
113 /***
114  * UCW Conf options
115  * ~~~~~~~~~~~~~~~~
116  *
117  * OPT_CONF_OPTIONS declares -C and -S as described in @getopt.h
118  ***/
119
120 #ifdef CONFIG_UCW_DEBUG
121 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_DUMPCONFIG, OPT_CONF_HOOK
122 #else
123 #define OPT_CONF_OPTIONS    OPT_CONF_CONFIG, OPT_CONF_SET, OPT_CONF_HOOK
124 #endif
125
126 #define OPT_CONF_CONFIG     OPT_CALL('C', "config", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Override the default configuration file")
127 #define OPT_CONF_SET        OPT_CALL('S', "set", opt_conf_internal, NULL, OPT_REQUIRED_VALUE, "Manual setting of a configuration item")
128 #define OPT_CONF_DUMPCONFIG OPT_CALL(0, "dumpconfig", opt_conf_internal, NULL, OPT_NO_VALUE, "Dump program configuration")
129 #define OPT_CONF_HOOK       OPT_HOOK(opt_conf_hook_internal, NULL, OPT_HOOK_BEFORE_VALUE)
130
131 void opt_conf_internal(struct opt_item * opt, const char * value, void * data);
132 void opt_conf_hook_internal(struct opt_item * opt, const char * value, void * data);
133
134 /***
135  * Predefined shortopt arguments
136  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137  *
138  * for the preceding calls if positional args wanted.
139  * Arguments are processed in the order of the numbers given to them. There must be first
140  * the args with OPT_REQUIRED (see below) and after them the args without OPT_REQUIRED, no mixing.
141  * You may define a catch-all option as OPT_POSITIONAL_TAIL. After this, no positional arg is allowed.
142  * You may shuffle the positional arguments in any way in the opt sections but the numbering must obey
143  * the rules given here.
144  ***/
145 // FIXME: The previous paragraph is almost incomprehensible
146
147 // FIXME: Is numbering from 1 natural here?
148 // FIXME: Are there any rules for mixing of positional arguments with options?
149 #define OPT_POSITIONAL(n)   (OPT_POSITIONAL_TAIL+(n))
150 #define OPT_POSITIONAL_TAIL 256
151
152
153 /***
154  * Flags for the preceding calls
155  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
156  ***/
157
158 #define OPT_REQUIRED        0x1         /** Argument must appear at the command line **/
159 #define OPT_REQUIRED_VALUE  0x2         /** Argument must have a value **/
160 #define OPT_NO_VALUE        0x4         /** Argument must have no value **/
161 #define OPT_MAYBE_VALUE     0x8         /** Argument may have a value **/
162 #define OPT_VALUE_FLAGS     (OPT_REQUIRED_VALUE | OPT_NO_VALUE | OPT_MAYBE_VALUE)
163 #define OPT_NEGATIVE        0x10        /** Reversing the effect of OPT_INC or saving @false into OPT_BOOL **/
164 #define OPT_NO_HELP         0x20        /** Omit this line from help **/
165 #define OPT_LAST_ARG        0x40        /** Stop processing argv after this line **/
166 #define OPT_SINGLE          0x100       /** Argument must appear at most once **/
167 #define OPT_MULTIPLE        0x200       /** Argument may appear any time; will save all the values into a simple list **/
168 #define OPT_HOOK_BEFORE_ARG     0x1000  /** Call before option parsing **/
169 #define OPT_HOOK_BEFORE_VALUE   0x2000  /** Call before value parsing **/
170 #define OPT_HOOK_AFTER_VALUE    0x4000  /** Call after value parsing **/
171
172 extern const struct opt_section * opt_section_root;
173 void opt_help(const struct opt_section * help);
174
175 static inline void opt_usage(void) {
176   fprintf(stderr, "Run with argument --help for more information.\n");
177 }
178
179 static inline void opt_show_help_internal(struct opt_item * opt UNUSED, const char * value UNUSED, void * data) {
180   opt_help(data);
181   exit(0);
182 }
183
184 /**
185  * Parse all the arguments.
186  **/
187 void opt_parse(const struct opt_section * options, char ** argv);
188 // FIXME: When parsing finishes (possibly due to OPT_LAST_ARG), what is guaranteed
189 // about the state of argv[]?
190
191 #endif