2 * UCW Library -- Configuration files
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2006 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 /*** === Data types [[conf_types]] ***/
16 enum cf_class { /** Class of the configuration item. **/
17 CC_END, // end of list
18 CC_STATIC, // single variable or static array
19 CC_DYNAMIC, // dynamically allocated array
20 CC_PARSER, // arbitrary parser function
21 CC_SECTION, // section appears exactly once
22 CC_LIST, // list with 0..many nodes
23 CC_BITMAP // of up to 32 items
26 enum cf_type { /** Type of a single value. **/
27 CT_INT, CT_U64, CT_DOUBLE, // number types
29 CT_STRING, // string type
30 CT_LOOKUP, // in a string table
31 CT_USER // user-defined type
37 * A parser function gets an array of (strdup'ed) strings and a pointer with
38 * the customized information (most likely the target address). It can store
39 * the parsed value anywhere in any way it likes, however it must first call
40 * @cf_journal_block() on the overwritten memory block. It returns an error
41 * message or NULL if everything is all right.
43 typedef char *cf_parser(uns number, char **pars, void *ptr);
45 * A parser function for user-defined types gets a string and a pointer to
46 * the destination variable. It must store the value within [ptr,ptr+size),
47 * where size is fixed for each type. It should not call @cf_journal_block().
49 typedef char *cf_parser1(char *string, void *ptr);
51 * An init- or commit-hook gets a pointer to the section or NULL if this
52 * is the global section. It returns an error message or NULL if everything
53 * is all right. The init-hook should fill in default values (needed for
54 * dynamically allocated nodes of link lists or for filling global variables
55 * that are run-time dependent). The commit-hook should perform sanity
56 * checks and postprocess the parsed values. Commit-hooks must call
57 * @cf_journal_block() too. Caveat! init-hooks for static sections must not
58 * use @cf_malloc() but normal <<memory:xmalloc()>>.
60 typedef char *cf_hook(void *ptr);
62 * Dumps the contents of a variable of a user-defined type.
64 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
66 * Similar to init-hook, but it copies attributes from another list node
67 * instead of setting the attributes to default values. You have to provide
68 * it if your node contains parsed values and/or sub-lists.
70 typedef char *cf_copier(void *dest, void *src);
72 struct cf_user_type { /** Structure to store information about user-defined variable type. **/
73 uns size; // of the parsed attribute
74 char *name; // name of the type (for dumping)
75 cf_parser1 *parser; // how to parse it
76 cf_dumper1 *dumper; // how to dump the type
80 struct cf_item { /** Single configuration item. **/
81 const char *name; // case insensitive
82 int number; // length of an array or #parameters of a parser (negative means at most)
83 void *ptr; // pointer to a global variable or an offset in a section
85 struct cf_section *sec; // declaration of a section or a list
86 cf_parser *par; // parser function
87 const char * const *lookup; // NULL-terminated sequence of allowed strings for lookups
88 struct cf_user_type *utype; // specification of the user-defined type
90 enum cf_class cls:16; // attribute class
91 enum cf_type type:16; // type of a static or dynamic attribute
94 struct cf_section { /** A section. **/
95 uns size; // 0 for a global block, sizeof(struct) for a section
96 cf_hook *init; // fills in default values (no need to bzero)
97 cf_hook *commit; // verifies parsed data (optional)
98 cf_copier *copy; // copies values from another instance (optional, no need to copy basic attributes)
99 struct cf_item *cfg; // CC_END-terminated array of items
100 uns flags; // for internal use only
108 * You could create the structures manually, but you can use these macros to
113 * Declaration of <<struct_cf_section,`cf_section`>>
114 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116 * These macros can be used to configure the <<struct_cf_section,`cf_section`>>
121 * Data type of a section.
122 * If you store the section into a structure, use this macro.
124 * Storing a section into a structure is useful mostly when you may have multiple instances of the
125 * section (eg. <<conf_multi,array or list>>).
130 * cnode n; // This one is for the list itself
135 * static struct clist nodes;
137 * static struct cf_section node = {
138 * CF_TYPE(struct list_node),
140 * CF_STRING("name", PTR_TO(struct list_node, name)),
141 * CF_UNS("value", PTR_TO(struct list_node, value)),
146 * static struct cf_section section = {
147 * CF_LIST("node", &nodes, &node),
151 * You could use <<def_CF_STATIC,`CF_STATIC`>> or <<def_CF_DYNAMIC,`CF_DYNAMIC`>>
152 * macros to create arrays.
154 #define CF_TYPE(s) .size = sizeof(s)
156 * An init <<hooks,hook>>.
157 * You can use this to initialize dynamically allocated items (for a dynamic array or list).
158 * The hook returns an error message or NULL if everything was OK.
160 #define CF_INIT(f) .init = (cf_hook*) f
162 * A commit <<hooks,hook>>.
163 * You can use this one to check sanity of loaded data and postprocess them.
164 * You must call @cf_journal_block() if you change anything.
166 * Return error message or NULL if everything went OK.
168 #define CF_COMMIT(f) .commit = (cf_hook*) f
170 * A <<hooks,copy function>>.
171 * You need to provide one for too complicated sections where a memcpy is not
172 * enough to copy it properly. It happens, for example, when you have a dynamically
173 * allocated section containing a list of other sections.
175 * You return an error message or NULL if you succeed.
177 #define CF_COPY(f) .copy = (cf_copier*) f /** **/
178 #define CF_ITEMS .cfg = ( struct cf_item[] ) /** List of sub-items. **/
179 #define CF_END { .cls = CC_END } /** End of the structure. **/
181 * Declaration of a configuration item
182 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
184 * Each of these describe single <<struct_cf_item,configuration item>>. They are mostly
185 * for internal use, do not use them directly unless you really know what you are doing.
189 * Static array of items.
190 * Expects you to allocate the memory and provide pointer to it.
192 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
194 * Dynamic array of items.
195 * Expects you to provide pointer to your pointer to data and it will allocate new memory for it
196 * and set your pointer to it.
198 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
199 #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. **/
200 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s } /** A sub-section. **/
201 #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. **/
202 #define CF_BITMAP_INT(n,p) { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) } /** A bitmap. **/
203 #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. **/
205 * Basic configuration items
206 * ^^^^^^^^^^^^^^^^^^^^^^^^^
208 * They describe basic data types used in the configuration. This should be enough for
209 * most real-life purposes.
211 * The parameters are as follows:
213 * * @n -- name of the item.
214 * * @p -- pointer to the variable where it shall be stored.
217 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1) /** Single `int` value. **/
218 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c) /** Static array of integers. **/
219 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c) /** Dynamic array of integers. **/
220 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1) /** Single `uns` (`unsigned`) value. **/
221 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c) /** Static array of unsigned integers. **/
222 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c) /** Dynamic array of unsigned integers. **/
223 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1) /** Single unsigned 64bit integer (`u64`). **/
224 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c) /** Static array of u64s. **/
225 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c) /** Dynamic array of u64s. **/
226 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1) /** Single instance of `double`. **/
227 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c) /** Static array of doubles. **/
228 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c) /** Dynamic array of doubles. **/
229 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1) /** Single IPv4 address. **/
230 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c) /** Static array of IP addresses. **/.
231 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c) /** Dynamic array of IP addresses. **/
234 * You provide a pointer to a `char *` variable and it will fill it with
235 * dynamically allocated string. For example:
237 * static char *string = "Default string";
239 * static struct cf_section section = {
241 * CF_STRING("string", &string),
246 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,char*,1)
247 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,char*,c) /** Static array of strings. **/
248 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,char*,c) /** Dynamic array of strings. **/
250 * One string out of a predefined set.
251 * You provide the set as an array of strings terminated by NULL (similar to @argv argument
252 * of main()) as the @t parameter.
254 * The configured variable (pointer to `int`) is set to index of the string.
255 * So, it works this way:
257 * static *strings[] = { "First", "Second", "Third", NULL };
259 * static int variable;
261 * static struct cf_section section = {
263 * CF_LOOKUP("choice", &variable, strings),
268 * Now, if the configuration contains `choice "Second"`, `variable` will be set to 1.
270 #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 }
272 * Static array of strings out of predefined set.
274 #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 }
276 * Dynamic array of strings out of predefined set.
278 #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 }
280 * A user-defined type.
281 * See <<custom_parser,creating custom parsers>> section if you want to know more.
283 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
285 * Static array of user-defined types (all of the same type).
286 * See <<custom_parser,creating custom parsers>> section.
288 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
290 * Dynamic array of user-defined types.
291 * See <<custom_parser,creating custom parsers>> section.
293 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
296 * Any number of dynamic array elements
298 #define CF_ANY_NUM -0x7fffffff
300 #define DARY_LEN(a) ((uns*)a)[-1] /** Length of an dynamic array. **/
301 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
302 // creates a static instance of a dynamic array
309 * Uses <<mempool:,memory pools>> for efficiency and journal recovery.
310 * You should use these routines when implementing custom parsers.
314 * A <<mempool:type_mempool,memory pool>> for configuration parser needs.
315 * Memory allocated from here is valid as long as the current config is loaded
316 * (if you allocate some memory and rollback the transaction or you load some
317 * other configuration, it gets lost).
319 extern struct mempool *cf_pool;
320 void *cf_malloc(uns size); /** Returns @size bytes of memory. Allocates from <<var_cf_pool,`cf_pool`>>. **/
321 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
322 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
323 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
330 * For error recovery when <<reload,reloading configuration>>.
332 extern uns cf_need_journal; /** Is the journal needed? If you do not reload configuration, you set this to 0 and gain a little more performance and free memory. **/
334 * When a block of memory is about to be changed, put the old value
335 * into journal with this function. You need to call it from a <<hooks,commit hook>>
336 * if you change anything. It is used internally by low-level parsers.
337 * <<custom_parser,Custom parsers>> do not need to call it, it is called
340 void cf_journal_block(void *ptr, uns len);
341 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var)) // Store single value into journal.
345 * Section declaration
346 * ~~~~~~~~~~~~~~~~~~~
350 * Plug another top-level section into the configuration system.
351 * @name is the name in the configuration file,
352 * @sec is pointer to the section description.
353 * If @allow_unknown is set to 0 and a variable not described in @sec
354 * is found in the configuration file, it produces an error.
355 * If you set it to 1, all such variables are ignored.
357 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
359 * If you have a section in a structure and you want to initialize it
360 * (eg. if you want a copy of default values outside the configuration),
361 * you can use this. It initializes it recursively.
363 * This is used mostly internally. You probably do not need it.
365 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
369 * Parsers for basic types
370 * ~~~~~~~~~~~~~~~~~~~~~~~
372 * Each of them gets a string to parse and pointer to store the value.
373 * It returns either NULL or error message.
375 * The parsers support units. See <<config:units,their list>>.
377 char *cf_parse_int(const char *str, int *ptr); /** Parser for integers. **/
378 char *cf_parse_u64(const char *str, u64 *ptr); /** Parser for 64 unsigned integers. **/
379 char *cf_parse_double(const char *str, double *ptr); /** Parser for doubles. **/
380 char *cf_parse_ip(const char *p, u32 *varp); /** Parser for IP addresses. **/