2 * Configuration Parsers
4 * Copyright (c) 2012 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
33 #ifndef CONFIG_CONFIG_H
34 #define CONFIG_CONFIG_H
46 #define CONF_HAS_ARG 0x0001
50 void (*set_default) (struct conf_option *opt);
51 void (*free) (struct conf_option *opt);
52 int (*parse) (struct conf_option *opt, bool on, const char *arg);
53 int (*copy) (struct conf_option *opt, const struct conf_option *src);
57 * Bool: expects "mem" to point to a "bool"
58 * Initial state is "false".
61 extern const struct conf_type conf_bool;
64 * Int: expects "mem" to point to an "int"
65 * Initial state is "0".
68 extern const struct conf_type conf_int;
71 * Uint: expects "mem" to point to an "uint"
72 * Initial state is "0"
75 extern const struct conf_type conf_uint;
78 * String: expects "mem" to point to an "char*"
79 * Initial state is NULL. Memory is allocated by the parser and a string is
80 * always zero-terminated.
83 extern const struct conf_type conf_string;
86 * Stringlist: expects "mem" to point to an "char**"
87 * Initial state is NULL. The list is NULL-terminated and each entry is a
88 * zero-terminated string. Memory is allocated by the parser.
91 extern const struct conf_type conf_string_list;
94 * Grabs: expects "mem" to point to an "struct conf_grab*"
95 * Initial state is NULL. See below for the type definition. The memory for the
96 * type is allocated by the parser.
97 * Two small helpers are available to ease the use.
100 extern const struct conf_type conf_grab;
105 unsigned int *num_syms;
109 static inline bool conf_grab_matches(const struct conf_grab *grab,
110 unsigned int ev_mods,
111 unsigned int ev_num_syms,
112 const uint32_t *ev_syms)
114 return shl_grab_has_match(ev_mods, ev_num_syms, ev_syms,
115 grab->num, grab->mods, grab->num_syms,
119 #define CONF_SINGLE_GRAB(_mods, _sym) { \
121 .mods = (unsigned int[]) { (_mods) }, \
122 .num_syms = (unsigned int[]) { 1 }, \
123 .keysyms = (uint32_t*[]) { (uint32_t[]) { (_sym) } }, \
127 * Configuration Context
128 * A configuration context is initialized with an array of config-options and
129 * then can be used to parse different sources. The backing memory is managed by
130 * the user, not by this context.
131 * All options are set to their default values on startup and reset.
136 int conf_ctx_new(struct conf_ctx **out, const struct conf_option *opts,
137 size_t onum, void *mem);
138 void conf_ctx_free(struct conf_ctx *ctx);
139 void conf_ctx_reset(struct conf_ctx *ctx);
140 void *conf_ctx_get_mem(struct conf_ctx *ctx);
142 int conf_ctx_parse_ctx(struct conf_ctx *ctx, const struct conf_ctx *src);
143 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv);
144 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...);
147 * Configuration Options
148 * A configuration option specifies the name of the option, the type, the
149 * backing memory, the default value and more. Each option is represented by
153 #define CONF_DONE 0x0001
154 #define CONF_LOCKED 0x0002
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 /* CONFIG_CONFIG_H */