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