]> mj.ucw.cz Git - libucw.git/blob - ucw/conf.h
ABI tools: Better reporting of missing files
[libucw.git] / ucw / conf.h
1 /*
2  *      UCW Library -- Configuration files
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2014 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_CONF_H
12 #define _UCW_CONF_H
13
14 #include <ucw/clists.h>
15 #include <ucw/gary.h>
16
17 #ifdef CONFIG_UCW_CLEAN_ABI
18 #define cf_close_group ucw_cf_close_group
19 #define cf_declare_rel_section ucw_cf_declare_rel_section
20 #define cf_declare_section ucw_cf_declare_section
21 #define cf_delete_context ucw_cf_delete_context
22 #define cf_dump_sections ucw_cf_dump_sections
23 #define cf_find_item ucw_cf_find_item
24 #define cf_get_pool ucw_cf_get_pool
25 #define cf_init_section ucw_cf_init_section
26 #define cf_journal_block ucw_cf_journal_block
27 #define cf_journal_commit_transaction ucw_cf_journal_commit_transaction
28 #define cf_journal_new_transaction ucw_cf_journal_new_transaction
29 #define cf_journal_rollback_transaction ucw_cf_journal_rollback_transaction
30 #define cf_load ucw_cf_load
31 #define cf_malloc ucw_cf_malloc
32 #define cf_malloc_zero ucw_cf_malloc_zero
33 #define cf_modify_item ucw_cf_modify_item
34 #define cf_new_context ucw_cf_new_context
35 #define cf_open_group ucw_cf_open_group
36 #define cf_parse_double ucw_cf_parse_double
37 #define cf_parse_int ucw_cf_parse_int
38 #define cf_parse_ip ucw_cf_parse_ip
39 #define cf_parse_u64 ucw_cf_parse_u64
40 #define cf_printf ucw_cf_printf
41 #define cf_reload ucw_cf_reload
42 #define cf_revert ucw_cf_revert
43 #define cf_set ucw_cf_set
44 #define cf_set_journalling ucw_cf_set_journalling
45 #define cf_strdup ucw_cf_strdup
46 #define cf_switch_context ucw_cf_switch_context
47 #endif
48
49 struct mempool;
50
51 /***
52  * [[conf_ctxt]]
53  * Configuration contexts
54  * ~~~~~~~~~~~~~~~~~~~~~~
55  *
56  * The state of the configuration parser is stored within a configuration context.
57  * One such context is automatically created during initialization of the library
58  * and you need not care about more, as long as you use a single configuration file.
59  *
60  * In full generality, you can define as many contexts as you wish and switch
61  * between them. Each thread has its own pointer to the current context, which
62  * must not be shared with other threads.
63  ***/
64
65 /** Create a new configuration context. **/
66 struct cf_context *cf_new_context(void);
67
68 /**
69  * Free a configuration context. The context must not be set as current
70  * for any thread, nor can it be the default context.
71  *
72  * All configuration settings made within the context are rolled back
73  * (except when journalling is turned off). All memory allocated on behalf
74  * of the context is freed, which includes memory obtained by calls to
75  * @cf_malloc().
76  **/
77 void cf_delete_context(struct cf_context *cc);
78
79 /**
80  * Make the given configuration context current and return the previously
81  * active context. Both the new and the old context may be NULL.
82  **/
83 struct cf_context *cf_switch_context(struct cf_context *cc);
84
85 /***
86  * [[conf_load]]
87  * Safe configuration loading
88  * ~~~~~~~~~~~~~~~~~~~~~~~~~~
89  *
90  * These functions can be used to to safely load or reload configuration.
91  */
92
93 /**
94  * Load configuration from @file.
95  * Returns a non-zero value upon error. In that case, all changes to the
96  * configuration specified in the file are undone.
97  **/
98 int cf_load(const char *file);
99
100 /**
101  * Reload configuration from @file, replace the old one.
102  * If @file is NULL, reload all loaded configuration files and re-apply
103  * bits of configuration passed to @cf_set().
104  * Returns a non-zero value upon error. In that case, all configuration
105  * settings are rolled back to the state before calling this function.
106  **/
107 int cf_reload(const char *file);
108
109 /**
110  * Parse some part of configuration passed in @string.
111  * The syntax is the same as in the <<config:,configuration file>>.
112  * Returns a non-zero value upon error. In that case, all changes to the
113  * configuration specified by the already executed parts of the string
114  * are undone.
115  **/
116 int cf_set(const char *string);
117
118 /**
119  * Sometimes, the configuration is split to multiple files and when only
120  * some of the are loaded, the settings are not consistent -- for example,
121  * they might have been rejected by a commit hook, because a mandatory setting
122  * is missing.
123  *
124  * This function opens a configuration group, in which multiple files can be
125  * loaded and all commit hooks are deferred until the group is closed.
126  **/
127 void cf_open_group(void);
128
129 /**
130  * Close a group opened by @cf_open_group(). Returns a non-zero value upon error,
131  * which usually means that a commit hook has failed.
132  **/
133 int cf_close_group(void);
134
135 /**
136  * Return all configuration items to their initial state before loading the
137  * configuration file. If journalling is disabled, it does nothing.
138  **/
139 void cf_revert(void);
140
141 /*** === Data types [[conf_types]] ***/
142
143 enum cf_class {                         /** Class of the configuration item. **/
144   CC_END,                               // end of list
145   CC_STATIC,                            // single variable or static array
146   CC_DYNAMIC,                           // dynamically allocated array
147   CC_PARSER,                            // arbitrary parser function
148   CC_SECTION,                           // section appears exactly once
149   CC_LIST,                              // list with 0..many nodes
150   CC_BITMAP                             // of up to 32 items
151 };
152
153 enum cf_type {                          /** Type of a single value. **/
154   CT_INT, CT_U64, CT_DOUBLE,            // number types
155   CT_IP,                                // IP address
156   CT_STRING,                            // string type
157   CT_LOOKUP,                            // in a string table
158   CT_USER                               // user-defined type
159 };
160
161 struct fastbuf;
162
163 /**
164  * A parser function gets an array of (strdup'ed) strings and a pointer with
165  * the customized information (most likely the target address).  It can store
166  * the parsed value anywhere in any way it likes, however it must first call
167  * @cf_journal_block() on the overwritten memory block.  It returns an error
168  * message or NULL if everything is all right.
169  **/
170 typedef char *cf_parser(uint number, char **pars, void *ptr);
171 /**
172  * A parser function for user-defined types gets a string and a pointer to
173  * the destination variable.  It must store the value within [ptr,ptr+size),
174  * where size is fixed for each type.  It should not call @cf_journal_block().
175  **/
176 typedef char *cf_parser1(char *string, void *ptr);
177 /**
178  * An init- or commit-hook gets a pointer to the section or NULL if this
179  * is the global section.  It returns an error message or NULL if everything
180  * is all right.  The init-hook should fill in default values (needed for
181  * dynamically allocated nodes of link lists or for filling global variables
182  * that are run-time dependent).  The commit-hook should perform sanity
183  * checks and postprocess the parsed values.  Commit-hooks must call
184  * @cf_journal_block() too.  Caveat! init-hooks for static sections must not
185  * use @cf_malloc() but normal <<memory:xmalloc()>>.
186  **/
187 typedef char *cf_hook(void *ptr);
188 /**
189  * Dumps the contents of a variable of a user-defined type.
190  **/
191 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
192 /**
193  * Similar to init-hook, but it copies attributes from another list node
194  * instead of setting the attributes to default values.  You have to provide
195  * it if your node contains parsed values and/or sub-lists.
196  **/
197 typedef char *cf_copier(void *dest, void *src);
198
199 struct cf_user_type {                   /** Structure to store information about user-defined variable type. **/
200   uint size;                            // of the parsed attribute
201   char *name;                           // name of the type (for dumping)
202   cf_parser1 *parser;                   // how to parse it
203   cf_dumper1 *dumper;                   // how to dump the type
204 };
205
206 struct cf_section;
207 struct cf_item {                        /** Single configuration item. **/
208   const char *name;                     // case insensitive
209   int number;                           // length of an array or #parameters of a parser (negative means at most)
210   void *ptr;                            // pointer to a global variable or an offset in a section
211   union cf_union {
212     struct cf_section *sec;             // declaration of a section or a list
213     cf_parser *par;                     // parser function
214     const char * const *lookup;         // NULL-terminated sequence of allowed strings for lookups
215     struct cf_user_type *utype;         // specification of the user-defined type
216   } u;
217   enum cf_class cls:16;                 // attribute class
218   enum cf_type type:16;                 // type of a static or dynamic attribute
219 };
220
221 struct cf_section {                     /** A section. **/
222   uint size;                            // 0 for a global block, sizeof(struct) for a section
223   cf_hook *init;                        // fills in default values (no need to bzero)
224   cf_hook *commit;                      // verifies parsed data (optional)
225   cf_copier *copy;                      // copies values from another instance (optional, no need to copy basic attributes)
226   struct cf_item *cfg;                  // CC_END-terminated array of items
227   uint flags;                           // for internal use only
228 };
229
230 /***
231  * [[conf_macros]]
232  * Convenience macros
233  * ~~~~~~~~~~~~~~~~~~
234  *
235  * You could create the structures manually, but you can use these macros to
236  * save some typing.
237  */
238
239 /***
240  * Declaration of <<struct_cf_section,`cf_section`>>
241  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
242  *
243  * These macros can be used to configure the <<struct_cf_section,`cf_section`>>
244  * structure.
245  ***/
246
247 /**
248  * Data type of a section.
249  * If you store the section into a structure, use this macro.
250  *
251  * Storing a section into a structure is useful mostly when you may have multiple instances of the
252  * section (eg. <<conf_multi,array or list>>).
253  *
254  * Example:
255  *
256  *   struct list_node {
257  *     cnode n;         // This one is for the list itself
258  *     char *name;
259  *     uint value;
260  *   };
261  *
262  *   static struct clist nodes;
263  *
264  *   static struct cf_section node = {
265  *     CF_TYPE(struct list_node),
266  *     CF_ITEMS {
267  *       CF_STRING("name", PTR_TO(struct list_node, name)),
268  *       CF_UINT("value", PTR_TO(struct list_node, value)),
269  *       CF_END
270  *     }
271  *   };
272  *
273  *   static struct cf_section section = {
274  *     CF_LIST("node", &nodes, &node),
275  *     CF_END
276  *   };
277  *
278  * You could use <<def_CF_STATIC,`CF_STATIC`>> or <<def_CF_DYNAMIC,`CF_DYNAMIC`>>
279  * macros to create arrays.
280  */
281 #define CF_TYPE(s)      .size = sizeof(s)
282 /**
283  * An init <<hooks,hook>>.
284  * You can use this to initialize dynamically allocated items (for a dynamic array or list).
285  * The hook returns an error message or NULL if everything was OK.
286  */
287 #define CF_INIT(f)      .init = (cf_hook*) f
288 /**
289  * A commit <<hooks,hook>>.
290  * You can use this one to check sanity of loaded data and postprocess them.
291  * You must call @cf_journal_block() if you change anything.
292  *
293  * Return error message or NULL if everything went OK.
294  **/
295 #define CF_COMMIT(f)    .commit = (cf_hook*) f
296 /**
297  * A <<hooks,copy function>>.
298  * You need to provide one for too complicated sections where a memcpy is not
299  * enough to copy it properly. It happens, for example, when you have a dynamically
300  * allocated section containing a list of other sections.
301  *
302  * You return an error message or NULL if you succeed.
303  **/
304 #define CF_COPY(f)      .copy = (cf_copier*) f          /**  **/
305 #define CF_ITEMS        .cfg = ( struct cf_item[] )     /** List of sub-items. **/
306 #define CF_END          { .cls = CC_END }               /** End of the structure. **/
307 /***
308  * Declaration of a configuration item
309  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
310  *
311  * Each of these describe single <<struct_cf_item,configuration item>>. They are mostly
312  * for internal use, do not use them directly unless you really know what you are doing.
313  ***/
314
315 /**
316  * Static array of items.
317  * Expects you to allocate the memory and provide pointer to it.
318  **/
319 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
320 /**
321  * Dynamic array of items.
322  * Expects you to provide pointer to your pointer to data and it will allocate new memory for it
323  * and set your pointer to it.
324  **/
325 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
326 #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. **/
327 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }                                             /** A sub-section. **/
328 #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. **/
329 #define CF_BITMAP_INT(n,p)      { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) }                     /** A bitmap. **/
330 #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. **/
331 /***
332  * Basic configuration items
333  * ^^^^^^^^^^^^^^^^^^^^^^^^^
334  *
335  * They describe basic data types used in the configuration. This should be enough for
336  * most real-life purposes.
337  *
338  * The parameters are as follows:
339  *
340  * * @n -- name of the item.
341  * * @p -- pointer to the variable where it shall be stored.
342  * * @c -- count.
343  **/
344 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)                /** Single `int` value. **/
345 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)                /** Static array of integers. **/
346 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)               /** Dynamic array of integers. **/
347 #define CF_UINT(n,p)            CF_STATIC(n,p,INT,uint,1)               /** Single `uint` (`unsigned`) value. **/
348 #define CF_UINT_ARY(n,p,c)      CF_STATIC(n,p,INT,uint,c)               /** Static array of unsigned integers. **/
349 #define CF_UINT_DYN(n,p,c)      CF_DYNAMIC(n,p,INT,uint,c)              /** Dynamic array of unsigned integers. **/
350 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)                /** Single unsigned 64bit integer (`u64`). **/
351 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)                /** Static array of u64s. **/
352 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)               /** Dynamic array of u64s. **/
353 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)          /** Single instance of `double`. **/
354 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)          /** Static array of doubles. **/
355 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)         /** Dynamic array of doubles. **/
356 #define CF_IP(n,p)              CF_STATIC(n,p,IP,u32,1)                 /** Single IPv4 address. **/
357 #define CF_IP_ARY(n,p,c)        CF_STATIC(n,p,IP,u32,c)                 /** Static array of IP addresses. **/.
358 #define CF_IP_DYN(n,p,c)        CF_DYNAMIC(n,p,IP,u32,c)                /** Dynamic array of IP addresses. **/
359
360 /* FIXME: Backwards compatibility only, should not be used at is will be removed soon. */
361 #define CF_UNS CF_UINT
362 #define CF_UNS_ARY CF_UINT_ARY
363 #define CF_UNS_DYN CF_UINT_DYN
364
365 /**
366  * A string.
367  * You provide a pointer to a `char *` variable and it will fill it with
368  * dynamically allocated string. For example:
369  *
370  *   static char *string = "Default string";
371  *
372  *   static struct cf_section section = {
373  *     CF_ITEMS {
374  *       CF_STRING("string", &string),
375  *       CF_END
376  *     }
377  *   };
378  **/
379 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,char*,1)
380 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,char*,c)           /** Static array of strings. **/
381 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,char*,c)          /** Dynamic array of strings. **/
382 /**
383  * One string out of a predefined set.
384  * You provide the set as an array of strings terminated by NULL (similar to @argv argument
385  * of main()) as the @t parameter.
386  *
387  * The configured variable (pointer to `int`) is set to index of the string.
388  * So, it works this way:
389  *
390  *   static *strings[] = { "First", "Second", "Third", NULL };
391  *
392  *   static int variable;
393  *
394  *   static struct cf_section section = {
395  *     CF_ITEMS {
396  *       CF_LOOKUP("choice", &variable, strings),
397  *       CF_END
398  *     }
399  *   };
400  *
401  * Now, if the configuration contains `choice "Second"`, `variable` will be set to 1.
402  **/
403 #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 }
404 /**
405  * Static array of strings out of predefined set.
406  **/
407 #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 }
408 /**
409  * Dynamic array of strings out of predefined set.
410  **/
411 #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 }
412 /**
413  * A user-defined type.
414  * See <<custom_parser,creating custom parsers>> section if you want to know more.
415  **/
416 #define CF_USER(n,p,t)          { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
417 /**
418  * Static array of user-defined types (all of the same type).
419  * See <<custom_parser,creating custom parsers>> section.
420  **/
421 #define CF_USER_ARY(n,p,t,c)    { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
422 /**
423  * Dynamic array of user-defined types.
424  * See <<custom_parser,creating custom parsers>> section.
425  **/
426 #define CF_USER_DYN(n,p,t,c)    { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
427
428 /**
429  * Any number of dynamic array elements
430  **/
431 #define CF_ANY_NUM              -0x7fffffff
432
433 #define DARY_LEN(a) GARY_SIZE(a)                /** Length of an dynamic array. An alias for `GARY_SIZE`. **/
434
435 /***
436  * [[alloc]]
437  * Memory allocation
438  * ~~~~~~~~~~~~~~~~~
439  *
440  * Each configuration context has one or more <<mempool:,memory pools>>, where all
441  * data related to the configuration are stored.
442  *
443  * The following set of functions allocate from these pools. The allocated memory
444  * is valid as long as the current configuration (when the configuration file is
445  * reloaded or rolled back, or the context is deleted, it gets lost).
446  *
447  * Memory allocated from within custom parsers should be allocated from the pools.
448  *
449  * Please note that the pool is not guaranteed to exist before you call cf_load(),
450  * cf_set(), or cf_getopt() on the particular context.
451  ***/
452 struct mempool *cf_get_pool(void); /** Return a pointer to the current configuration pool. **/
453 void *cf_malloc(uint size);     /** Returns @size bytes of memory allocated from the current configuration pool. **/
454 void *cf_malloc_zero(uint size);        /** Like @cf_malloc(), but zeroes the memory. **/
455 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
456 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
457
458 /***
459  * [[journal]]
460  * Undo journal
461  * ~~~~~~~~~~~~
462  *
463  * The configuration system uses a simple journaling mechanism, which makes
464  * it possible to undo changes to configuration. A typical example is loading
465  * of configuration by cf_load(): internally, it creates a transaction, applies
466  * all changes specified by the configuration and if one of them fails, the whole
467  * journal is replayed to restore the whole original state. Similarly, cf_reload()
468  * uses the journal to switch between configurations.
469  *
470  * In most cases, you need not care about the journal, except when you need
471  * to change some data from a <<hooks,hook>>, or if you want to call cf_modify_item() and then
472  * undo the changes.
473  ***/
474 /**
475  * This function can be used to disable the whole journalling mechanism.
476  * It saves some memory, but it makes undoing of configuration changes impossible,
477  * which breaks for example cf_reload().
478  **/
479 void cf_set_journalling(int enable);
480 /**
481  * When a block of memory is about to be changed, put the old value
482  * into journal with this function. You need to call it from a <<hooks,commit hook>>
483  * if you change anything. It is used internally by low-level parsers.
484  * <<custom_parser,Custom parsers>> do not need to call it, it is called
485  * before them.
486  **/
487 void cf_journal_block(void *ptr, uint len);
488 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))       // Store a single value into the journal
489
490 struct cf_journal_item;         /** Opaque identifier of the journal state. **/
491 /**
492  * Starts a new transaction. It returns the current state so you can
493  * get back to it. The @new_pool parameter tells if a new memory pool
494  * should be created and used from now.
495  **/
496 struct cf_journal_item *cf_journal_new_transaction(uint new_pool);
497 /**
498  * Marks current state as a complete transaction. The @new_pool
499  * parameter tells if the transaction was created with new memory pool
500  * (the parameter must be the same as the one with
501  * @cf_journal_new_transaction() was called with). The @oldj parameter
502  * is the journal state returned from last
503  * @cf_journal_new_transaction() call.
504  **/
505 void cf_journal_commit_transaction(uint new_pool, struct cf_journal_item *oldj);
506 /**
507  * Returns to an old journal state, reverting anything the current
508  * transaction did. The @new_pool parameter must be the same as the
509  * one you used when you created the transaction. The @oldj parameter
510  * is the journal state you got from @cf_journal_new_transaction() --
511  * it is the state to return to.
512  **/
513 void cf_journal_rollback_transaction(uint new_pool, struct cf_journal_item *oldj);
514
515 /***
516  * [[declare]]
517  * Section declaration
518  * ~~~~~~~~~~~~~~~~~~~
519  **/
520
521 /**
522  * Plug another top-level section into the configuration system.
523  * @name is the name in the configuration file,
524  * @sec is pointer to the section description.
525  * If @allow_unknown is set to 0 and a variable not described in @sec
526  * is found in the configuration file, it produces an error.
527  * If you set it to 1, all such variables are ignored.
528  *
529  * Please note that a single section definition cannot be used in multiple
530  * configuration contexts simultaneously.
531  **/
532 void cf_declare_section(const char *name, struct cf_section *sec, uint allow_unknown);
533 /**
534  * Like @cf_declare_section(), but instead of item pointers, the section
535  * contains offsets relative to @ptr. In other words, it does the same
536  * as `CF_SECTION`, but for top-level sections.
537  **/
538 void cf_declare_rel_section(const char *name, struct cf_section *sec, void *ptr, uint allow_unknown);
539 /**
540  * If you have a section in a structure and you want to initialize it
541  * (eg. if you want a copy of default values outside the configuration),
542  * you can use this. It initializes it recursively.
543  *
544  * This is used mostly internally. You probably do not need it.
545  **/
546 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uint do_bzero);
547
548 /***
549  * [[bparser]]
550  * Parsers for basic types
551  * ~~~~~~~~~~~~~~~~~~~~~~~
552  *
553  * Each of them gets a string to parse and pointer to store the value.
554  * It returns either NULL or error message.
555  *
556  * The parsers support units. See <<config:units,their list>>.
557  ***/
558 char *cf_parse_int(const char *str, int *ptr);          /** Parser for integers. **/
559 char *cf_parse_u64(const char *str, u64 *ptr);          /** Parser for 64 unsigned integers. **/
560 char *cf_parse_double(const char *str, double *ptr);    /** Parser for doubles. **/
561 char *cf_parse_ip(const char *p, u32 *varp);            /** Parser for IP addresses. **/
562
563 /***
564  * [[conf_direct]]
565  * Direct access
566  * ~~~~~~~~~~~~~
567  *
568  * Direct access to configuration items.
569  * You probably should not need this, but in your do, you have to handle
570  * <<journal,journalling>> yourself.
571  ***/
572
573 /**
574  * List of operations used on items.
575  * This macro is used to generate internal source code,
576  * but you may be interested in the list of operations it creates.
577  *
578  * Each operation corresponds to the same-named operation
579  * described in <<config:operations,configuration syntax>>.
580  **/
581 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(ALL) \
582   T(APPEND) T(PREPEND) T(REMOVE) T(EDIT) T(AFTER) T(BEFORE) T(COPY) T(RESET)
583   /* Closing brace finishes previous block.
584    * Basic attributes (static, dynamic, parsed) can be used with SET.
585    * Dynamic arrays can be used with SET, APPEND, PREPEND.
586    * Sections can be used with SET.
587    * Lists can be used with everything. */
588 #define T(x) OP_##x,
589 enum cf_operation { CF_OPERATIONS };    /** Allowed operations on items. See <<def_CF_OPERATIONS,`CF_OPERATIONS`>> for list (they have an `OP_` prefix -- it means you use `OP_SET` instead of just `SET`). **/
590 #undef T
591
592 /**
593  * Searches for a configuration item called @name.
594  * If it is found, it is copied into @item and NULL is returned.
595  * Otherwise, an error is returned and @item is zeroed.
596  **/
597 char *cf_find_item(const char *name, struct cf_item *item);
598 /**
599  * Performs a single operation on a given item.
600  **/
601 char *cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars);
602
603 /***
604  * [[conf_dump]]
605  * Debug dumping
606  * ~~~~~~~~~~~~~
607  ***/
608
609 struct fastbuf;
610 /**
611  * Write the current state of all configuration items into @fb.
612  **/
613 void cf_dump_sections(struct fastbuf *fb);
614
615 #endif