2 * Configuration Parsers
4 * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@googlemail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files
8 * (the "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * This provides generic command-line argument and configuration file parsers
29 * which can be used by different applications that are part of this
30 * distribution. It provides most basic types but can be extended on the fly
31 * with more advanced types.
47 #define CONF_HAS_ARG 0x0001
51 void (*set_default) (struct conf_option *opt);
52 void (*free) (struct conf_option *opt);
53 int (*parse) (struct conf_option *opt, bool on, const char *arg);
54 int (*copy) (struct conf_option *opt, const struct conf_option *src);
58 * Bool: expects "mem" to point to a "bool"
59 * Initial state is "false".
62 extern const struct conf_type conf_bool;
65 * Int: expects "mem" to point to an "int"
66 * Initial state is "0".
69 extern const struct conf_type conf_int;
72 * Uint: expects "mem" to point to an "uint"
73 * Initial state is "0"
76 extern const struct conf_type conf_uint;
79 * String: expects "mem" to point to an "char*"
80 * Initial state is NULL. Memory is allocated by the parser and a string is
81 * always zero-terminated.
84 extern const struct conf_type conf_string;
87 * Stringlist: expects "mem" to point to an "char**"
88 * Initial state is NULL. The list is NULL-terminated and each entry is a
89 * zero-terminated string. Memory is allocated by the parser.
92 extern const struct conf_type conf_string_list;
95 * Grabs: expects "mem" to point to an "struct conf_grab*"
96 * Initial state is NULL. See below for the type definition. The memory for the
97 * type is allocated by the parser.
98 * Two small helpers are available to ease the use.
101 extern const struct conf_type conf_grab;
106 unsigned int *num_syms;
110 static inline bool conf_grab_matches(const struct conf_grab *grab,
111 unsigned int ev_mods,
112 unsigned int ev_num_syms,
113 const uint32_t *ev_syms)
115 return shl_grab_has_match(ev_mods, ev_num_syms, ev_syms,
116 grab->num, grab->mods, grab->num_syms,
120 #define CONF_SINGLE_GRAB(_mods, _sym) { \
122 .mods = (unsigned int[]) { (_mods) }, \
123 .num_syms = (unsigned int[]) { 1 }, \
124 .keysyms = (uint32_t*[]) { (uint32_t[]) { (_sym) } }, \
128 * Configuration Context
129 * A configuration context is initialized with an array of config-options and
130 * then can be used to parse different sources. The backing memory is managed by
131 * the user, not by this context.
132 * All options are set to their default values on startup and reset.
137 int conf_ctx_new(struct conf_ctx **out, const struct conf_option *opts,
138 size_t onum, void *mem);
139 void conf_ctx_free(struct conf_ctx *ctx);
140 void conf_ctx_reset(struct conf_ctx *ctx);
141 void *conf_ctx_get_mem(struct conf_ctx *ctx);
143 int conf_ctx_parse_ctx(struct conf_ctx *ctx, const struct conf_ctx *src);
144 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv);
145 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...);
148 * Configuration Options
149 * A configuration option specifies the name of the option, the type, the
150 * backing memory, the default value and more. Each option is represented by
154 #define CONF_LOCKED 0x0001
159 const char *long_name;
160 const struct conf_type *type;
161 int (*aftercheck) (struct conf_option *opt, int argc,
162 char **argv, int idx);
163 int (*copy) (struct conf_option *opt, const struct conf_option *src);
164 int (*file) (struct conf_option *opt, bool on, const char *arg);
169 #define CONF_OPTION(_flags, _short, _long, _type, _aftercheck, _copy, _file, _mem, _def) \
170 { _flags, _short, "no-" _long, _type, _aftercheck, _copy, _file, _mem, _def }
172 #define CONF_OPTION_BOOL_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
173 CONF_OPTION(0, _short, _long, &conf_bool, _aftercheck, _copy, _file, _mem, (void*)(long)_def)
174 #define CONF_OPTION_BOOL(_short, _long, _mem, _def) \
175 CONF_OPTION_BOOL_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
177 #define CONF_OPTION_INT_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
178 CONF_OPTION(0, _short, _long, &conf_int, _aftercheck, _copy, _file, _mem, (void*)(long)_def)
179 #define CONF_OPTION_INT(_short, _long, _mem, _def) \
180 CONF_OPTION_INT_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
182 #define CONF_OPTION_UINT_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
183 CONF_OPTION(0, _short, _long, &conf_uint, _aftercheck, _copy, _file, _mem, (void*)(unsigned long)_def)
184 #define CONF_OPTION_UINT(_short, _long, _mem, _def) \
185 CONF_OPTION_UINT_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
187 #define CONF_OPTION_STRING_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
188 CONF_OPTION(0, _short, _long, &conf_string, _aftercheck, _copy, _file, _mem, _def)
189 #define CONF_OPTION_STRING(_short, _long, _mem, _def) \
190 CONF_OPTION_STRING_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
192 #define CONF_OPTION_STRING_LIST_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
193 CONF_OPTION(0, _short, _long, &conf_string_list, _aftercheck, _copy, _file, _mem, _def)
194 #define CONF_OPTION_STRING_LIST(_short, _long, _mem, _def) \
195 CONF_OPTION_STRING_LIST_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
197 #define CONF_OPTION_GRAB_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
198 CONF_OPTION(0, _short, _long, &conf_grab, _aftercheck, _copy, _file, _mem, _def)
199 #define CONF_OPTION_GRAB(_short, _long, _mem, _def) \
200 CONF_OPTION_GRAB_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
202 #endif /* CONF_CONF_H */