2 * UCW Library -- Configuration files
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include <ucw/clists.h>
20 * Configuration contexts
21 * ~~~~~~~~~~~~~~~~~~~~~~
23 * The state of the configuration parser is stored within a configuration context.
24 * One such context is automatically created during initialization of the library
25 * and you need not care about more, as long as you use a single configuration file.
27 * In full generality, you can define as many context as you wish and switch
28 * between them. Each thread has its own pointer to the current context, which
29 * must not be shared with other threads.
32 /** Create a new configuration context. **/
33 struct cf_context *cf_new_context(void);
36 * Free a configuration context. The context must not be set as current
37 * for any thread, nor can it be the default context.
39 * All configuration settings made within the context are rolled back
40 * (except when journalling is turned off). All memory allocated on behalf
41 * of the context is freed, which includes memory obtained by calls to
44 void cf_free_context(struct cf_context *cc);
47 * Make the given configuration context current and return the previously
48 * active context. Both the new and the old context may be NULL.
50 struct cf_context *cf_switch_context(struct cf_context *cc);
52 /*** === Data types [[conf_types]] ***/
54 enum cf_class { /** Class of the configuration item. **/
55 CC_END, // end of list
56 CC_STATIC, // single variable or static array
57 CC_DYNAMIC, // dynamically allocated array
58 CC_PARSER, // arbitrary parser function
59 CC_SECTION, // section appears exactly once
60 CC_LIST, // list with 0..many nodes
61 CC_BITMAP // of up to 32 items
64 enum cf_type { /** Type of a single value. **/
65 CT_INT, CT_U64, CT_DOUBLE, // number types
67 CT_STRING, // string type
68 CT_LOOKUP, // in a string table
69 CT_USER // user-defined type
75 * A parser function gets an array of (strdup'ed) strings and a pointer with
76 * the customized information (most likely the target address). It can store
77 * the parsed value anywhere in any way it likes, however it must first call
78 * @cf_journal_block() on the overwritten memory block. It returns an error
79 * message or NULL if everything is all right.
81 typedef char *cf_parser(uns number, char **pars, void *ptr);
83 * A parser function for user-defined types gets a string and a pointer to
84 * the destination variable. It must store the value within [ptr,ptr+size),
85 * where size is fixed for each type. It should not call @cf_journal_block().
87 typedef char *cf_parser1(char *string, void *ptr);
89 * An init- or commit-hook gets a pointer to the section or NULL if this
90 * is the global section. It returns an error message or NULL if everything
91 * is all right. The init-hook should fill in default values (needed for
92 * dynamically allocated nodes of link lists or for filling global variables
93 * that are run-time dependent). The commit-hook should perform sanity
94 * checks and postprocess the parsed values. Commit-hooks must call
95 * @cf_journal_block() too. Caveat! init-hooks for static sections must not
96 * use @cf_malloc() but normal <<memory:xmalloc()>>.
98 typedef char *cf_hook(void *ptr);
100 * Dumps the contents of a variable of a user-defined type.
102 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
104 * Similar to init-hook, but it copies attributes from another list node
105 * instead of setting the attributes to default values. You have to provide
106 * it if your node contains parsed values and/or sub-lists.
108 typedef char *cf_copier(void *dest, void *src);
110 struct cf_user_type { /** Structure to store information about user-defined variable type. **/
111 uns size; // of the parsed attribute
112 char *name; // name of the type (for dumping)
113 cf_parser1 *parser; // how to parse it
114 cf_dumper1 *dumper; // how to dump the type
118 struct cf_item { /** Single configuration item. **/
119 const char *name; // case insensitive
120 int number; // length of an array or #parameters of a parser (negative means at most)
121 void *ptr; // pointer to a global variable or an offset in a section
123 struct cf_section *sec; // declaration of a section or a list
124 cf_parser *par; // parser function
125 const char * const *lookup; // NULL-terminated sequence of allowed strings for lookups
126 struct cf_user_type *utype; // specification of the user-defined type
128 enum cf_class cls:16; // attribute class
129 enum cf_type type:16; // type of a static or dynamic attribute
132 struct cf_section { /** A section. **/
133 uns size; // 0 for a global block, sizeof(struct) for a section
134 cf_hook *init; // fills in default values (no need to bzero)
135 cf_hook *commit; // verifies parsed data (optional)
136 cf_copier *copy; // copies values from another instance (optional, no need to copy basic attributes)
137 struct cf_item *cfg; // CC_END-terminated array of items
138 uns flags; // for internal use only
146 * You could create the structures manually, but you can use these macros to
151 * Declaration of <<struct_cf_section,`cf_section`>>
152 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154 * These macros can be used to configure the <<struct_cf_section,`cf_section`>>
159 * Data type of a section.
160 * If you store the section into a structure, use this macro.
162 * Storing a section into a structure is useful mostly when you may have multiple instances of the
163 * section (eg. <<conf_multi,array or list>>).
168 * cnode n; // This one is for the list itself
173 * static struct clist nodes;
175 * static struct cf_section node = {
176 * CF_TYPE(struct list_node),
178 * CF_STRING("name", PTR_TO(struct list_node, name)),
179 * CF_UNS("value", PTR_TO(struct list_node, value)),
184 * static struct cf_section section = {
185 * CF_LIST("node", &nodes, &node),
189 * You could use <<def_CF_STATIC,`CF_STATIC`>> or <<def_CF_DYNAMIC,`CF_DYNAMIC`>>
190 * macros to create arrays.
192 #define CF_TYPE(s) .size = sizeof(s)
194 * An init <<hooks,hook>>.
195 * You can use this to initialize dynamically allocated items (for a dynamic array or list).
196 * The hook returns an error message or NULL if everything was OK.
198 #define CF_INIT(f) .init = (cf_hook*) f
200 * A commit <<hooks,hook>>.
201 * You can use this one to check sanity of loaded data and postprocess them.
202 * You must call @cf_journal_block() if you change anything.
204 * Return error message or NULL if everything went OK.
206 #define CF_COMMIT(f) .commit = (cf_hook*) f
208 * A <<hooks,copy function>>.
209 * You need to provide one for too complicated sections where a memcpy is not
210 * enough to copy it properly. It happens, for example, when you have a dynamically
211 * allocated section containing a list of other sections.
213 * You return an error message or NULL if you succeed.
215 #define CF_COPY(f) .copy = (cf_copier*) f /** **/
216 #define CF_ITEMS .cfg = ( struct cf_item[] ) /** List of sub-items. **/
217 #define CF_END { .cls = CC_END } /** End of the structure. **/
219 * Declaration of a configuration item
220 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
222 * Each of these describe single <<struct_cf_item,configuration item>>. They are mostly
223 * for internal use, do not use them directly unless you really know what you are doing.
227 * Static array of items.
228 * Expects you to allocate the memory and provide pointer to it.
230 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
232 * Dynamic array of items.
233 * Expects you to provide pointer to your pointer to data and it will allocate new memory for it
234 * and set your pointer to it.
236 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
237 #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f } /** A low-level parser. **/
238 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s } /** A sub-section. **/
239 #define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,clist*), .u.sec = s } /** A list with sub-items. **/
240 #define CF_BITMAP_INT(n,p) { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) } /** A bitmap. **/
241 #define CF_BITMAP_LOOKUP(n,p,t) { .cls = CC_BITMAP, .type = CT_LOOKUP, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*), .u.lookup = t } /** A bitmap with named bits. **/
243 * Basic configuration items
244 * ^^^^^^^^^^^^^^^^^^^^^^^^^
246 * They describe basic data types used in the configuration. This should be enough for
247 * most real-life purposes.
249 * The parameters are as follows:
251 * * @n -- name of the item.
252 * * @p -- pointer to the variable where it shall be stored.
255 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1) /** Single `int` value. **/
256 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c) /** Static array of integers. **/
257 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c) /** Dynamic array of integers. **/
258 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1) /** Single `uns` (`unsigned`) value. **/
259 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c) /** Static array of unsigned integers. **/
260 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c) /** Dynamic array of unsigned integers. **/
261 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1) /** Single unsigned 64bit integer (`u64`). **/
262 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c) /** Static array of u64s. **/
263 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c) /** Dynamic array of u64s. **/
264 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1) /** Single instance of `double`. **/
265 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c) /** Static array of doubles. **/
266 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c) /** Dynamic array of doubles. **/
267 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1) /** Single IPv4 address. **/
268 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c) /** Static array of IP addresses. **/.
269 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c) /** Dynamic array of IP addresses. **/
272 * You provide a pointer to a `char *` variable and it will fill it with
273 * dynamically allocated string. For example:
275 * static char *string = "Default string";
277 * static struct cf_section section = {
279 * CF_STRING("string", &string),
284 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,char*,1)
285 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,char*,c) /** Static array of strings. **/
286 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,char*,c) /** Dynamic array of strings. **/
288 * One string out of a predefined set.
289 * You provide the set as an array of strings terminated by NULL (similar to @argv argument
290 * of main()) as the @t parameter.
292 * The configured variable (pointer to `int`) is set to index of the string.
293 * So, it works this way:
295 * static *strings[] = { "First", "Second", "Third", NULL };
297 * static int variable;
299 * static struct cf_section section = {
301 * CF_LOOKUP("choice", &variable, strings),
306 * Now, if the configuration contains `choice "Second"`, `variable` will be set to 1.
308 #define CF_LOOKUP(n,p,t) { .cls = CC_STATIC, .type = CT_LOOKUP, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,int*), .u.lookup = t }
310 * Static array of strings out of predefined set.
312 #define CF_LOOKUP_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_LOOKUP, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,int*), .u.lookup = t }
314 * Dynamic array of strings out of predefined set.
316 #define CF_LOOKUP_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_LOOKUP, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,int**), .u.lookup = t }
318 * A user-defined type.
319 * See <<custom_parser,creating custom parsers>> section if you want to know more.
321 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
323 * Static array of user-defined types (all of the same type).
324 * See <<custom_parser,creating custom parsers>> section.
326 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
328 * Dynamic array of user-defined types.
329 * See <<custom_parser,creating custom parsers>> section.
331 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
334 * Any number of dynamic array elements
336 #define CF_ANY_NUM -0x7fffffff
338 #define DARY_LEN(a) ((uns*)a)[-1] /** Length of an dynamic array. **/
339 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
340 // creates a static instance of a dynamic array
347 * Each configuration context has one or more <<mempool:,memory pools>>, where all
348 * data related to the configuration are stored.
350 * The following set of functions allocate from these pools. The allocated memory
351 * is valid as long as the current configuration (when the configuration file is
352 * reloaded or rolled back, or the context is deleted, it gets lost).
354 * Memory allocated from within custom parsers should be allocated from the pools.
356 struct mempool *cf_get_pool(void); /** Return a pointer to the current configuration pool. **/
357 void *cf_malloc(uns size); /** Returns @size bytes of memory allocated from the current configuration pool. **/
358 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
359 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
360 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
367 * For error recovery when <<reload,reloading configuration>>.
370 * By default, the configuration mechanism remembers all changes in a journal,
371 * so that the configuration can be rolled back or reloaded. This function
372 * can be used to disable journalling, which saves some memory.
374 void cf_set_journalling(int enable);
376 * When a block of memory is about to be changed, put the old value
377 * into journal with this function. You need to call it from a <<hooks,commit hook>>
378 * if you change anything. It is used internally by low-level parsers.
379 * <<custom_parser,Custom parsers>> do not need to call it, it is called
382 void cf_journal_block(void *ptr, uns len);
383 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var)) // Store single value into journal.
387 * Section declaration
388 * ~~~~~~~~~~~~~~~~~~~
392 * Plug another top-level section into the configuration system.
393 * @name is the name in the configuration file,
394 * @sec is pointer to the section description.
395 * If @allow_unknown is set to 0 and a variable not described in @sec
396 * is found in the configuration file, it produces an error.
397 * If you set it to 1, all such variables are ignored.
399 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
401 * If you have a section in a structure and you want to initialize it
402 * (eg. if you want a copy of default values outside the configuration),
403 * you can use this. It initializes it recursively.
405 * This is used mostly internally. You probably do not need it.
407 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
411 * Parsers for basic types
412 * ~~~~~~~~~~~~~~~~~~~~~~~
414 * Each of them gets a string to parse and pointer to store the value.
415 * It returns either NULL or error message.
417 * The parsers support units. See <<config:units,their list>>.
419 char *cf_parse_int(const char *str, int *ptr); /** Parser for integers. **/
420 char *cf_parse_u64(const char *str, u64 *ptr); /** Parser for 64 unsigned integers. **/
421 char *cf_parse_double(const char *str, double *ptr); /** Parser for doubles. **/
422 char *cf_parse_ip(const char *p, u32 *varp); /** Parser for IP addresses. **/