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 top-level <<struct_cf_section,`cf_section`>>
119 #define CF_TYPE(s) .size = sizeof(s) /** Type of the section. **/
120 #define CF_INIT(f) .init = (cf_hook*) f /** Init <<hooks,hook>>. **/
121 #define CF_COMMIT(f) .commit = (cf_hook*) f /** Commit <<hooks,hook>>. **/
122 #define CF_COPY(f) .copy = (cf_copier*) f /** <<hooks,Copy function>>. **/
123 #define CF_ITEMS .cfg = ( struct cf_item[] ) /** List of sub-items. **/
124 #define CF_END { .cls = CC_END } /** End of the structure. **/
126 * Declaration of a configuration item
127 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
129 * Each of these describe single <<struct_cf_item,configuration item>>. They are mostly
130 * for internal use, do not use them directly unless you really know what you are doing.
132 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) } /** Static array of items. **/
133 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) } /** Dynamic array of items. **/
134 #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. **/
135 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s } /** A sub-section. **/
136 #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. **/
137 #define CF_BITMAP_INT(n,p) { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) } /** A bitmap. **/
138 #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. **/
140 * Basic configuration items
141 * ^^^^^^^^^^^^^^^^^^^^^^^^^
143 * They describe basic data types used in the configuration. This should be enough for
144 * most real-life purposes.
146 * The parameters are as follows:
148 * * @n -- name of the item.
149 * * @p -- pointer to the variable where it shall be stored.
152 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1) /** Single `int` value. **/
153 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c) /** Static array of `int` s. **/
154 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c) /** Dynamic array of `int` s. **/
155 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1) /** Single `uns` (`unsigned`) value. **/
156 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c) /** Static array of `uns` es. **/
157 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c) /** Dynamic array of `uns` es. **/
158 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1) /** Single unsigned 64bit integer (`u64`). **/
159 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c) /** Static array of `u64` s. **/
160 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c) /** Dynamic array of `u64` s. **/
161 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1) /** Single instance of `double`. **/
162 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c) /** Static array of `double` s. **/
163 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c) /** Dynamic array of `double` s. **/
164 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1) /** Single IPv4 address. **/
165 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c) /** Static array of IP addresses. **/.
166 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c) /** Dynamic array of IP addresses. **/
167 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,char*,1) /** One string. **/
168 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,char*,c) /** Static array of strings. **/
169 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,char*,c) /** Dynamic array of strings. **/
171 * One string out of a predefined set.
172 * You provide the set as an array of strings terminated by NULL (similar to @argv argument
173 * of main()) as the @t parameter.
175 #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 }
177 * Static array of strings out of predefined set.
178 * See <<def_CF_LOOKUP,`CF_LOOKUP`>>.
180 #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 }
182 * Dynamic array of strings out of predefined set.
183 * See <<def_CF_LOOKUP,`CF_LOOKUP`>>.
185 #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 }
187 * A user defined type.
188 * See <<custom_parser,creating custom parsers>> section if you want to know more.
190 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
192 * Static array of user defined types (all of the same type).
193 * See <<custom_parser,creating custom parsers>> section.
195 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
197 * Dynamic array of user defined types.
198 * See <<custom_parser,creating custom parsers>> section.
200 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
202 /* If you aren't picky about the number of parameters */
203 #define CF_ANY_NUM -0x7fffffff
205 #define DARY_LEN(a) ((uns*)a)[-1]
206 // length of a dynamic array
207 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
208 // creates a static instance of a dynamic array
215 * Uses <<mempool:,memory pools>> for efficiency and journal recovery.
216 * You should use these routines when implementing custom parsers.
219 extern struct mempool *cf_pool; /** A <<mempool:type_mempool,memory pool>> for configuration parser needs. **/
220 void *cf_malloc(uns size); /** Returns @size bytes of memory. **/
221 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
222 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
223 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
232 extern uns cf_need_journal;
233 void cf_journal_block(void *ptr, uns len);
234 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
236 /* Declaration: conf-section.c */
237 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
238 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
240 /*** === Parsers for basic types [[bparser]] ***/
241 char *cf_parse_int(const char *str, int *ptr); /** Parser for integers. **/
242 char *cf_parse_u64(const char *str, u64 *ptr); /** Parser for 64 unsigned integers. **/
243 char *cf_parse_double(const char *str, double *ptr); /** Parser for doubles. **/
244 char *cf_parse_ip(const char *p, u32 *varp); /** Parser for IP addresses. **/