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
112 /* Declaration of cf_section */
113 #define CF_TYPE(s) .size = sizeof(s)
114 #define CF_INIT(f) .init = (cf_hook*) f
115 #define CF_COMMIT(f) .commit = (cf_hook*) f
116 #define CF_COPY(f) .copy = (cf_copier*) f
117 #define CF_ITEMS .cfg = ( struct cf_item[] )
118 #define CF_END { .cls = CC_END }
119 /* Configuration items */
120 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
121 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
122 #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
123 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
124 #define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,clist*), .u.sec = s }
125 #define CF_BITMAP_INT(n,p) { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) }
126 #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 }
127 /* Configuration items for basic types */
128 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1)
129 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c)
130 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c)
131 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1)
132 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c)
133 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c)
134 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1)
135 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c)
136 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c)
137 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1)
138 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c)
139 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c)
140 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1)
141 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c)
142 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c)
143 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,char*,1)
144 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,char*,c)
145 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,char*,c)
146 #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 }
147 #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 }
148 #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 }
149 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
150 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
151 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
153 /* If you aren't picky about the number of parameters */
154 #define CF_ANY_NUM -0x7fffffff
156 #define DARY_LEN(a) ((uns*)a)[-1]
157 // length of a dynamic array
158 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
159 // creates a static instance of a dynamic array
166 * Uses <<mempool:,memory pools>> for efficiency and journal recovery.
167 * You should use these routines when implementing custom parsers.
170 extern struct mempool *cf_pool; /** A <<mempool:type_mempool,memory pool>> for configuration parser needs. **/
171 void *cf_malloc(uns size); /** Returns @size bytes of memory. **/
172 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
173 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
174 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
183 extern uns cf_need_journal;
184 void cf_journal_block(void *ptr, uns len);
185 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
187 /* Declaration: conf-section.c */
188 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
189 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
191 /*** === Parsers for basic types [[bparser]] ***/
192 char *cf_parse_int(const char *str, int *ptr); /** Parser for integers. **/
193 char *cf_parse_u64(const char *str, u64 *ptr); /** Parser for 64 unsigned integers. **/
194 char *cf_parse_double(const char *str, double *ptr); /** Parser for doubles. **/
195 char *cf_parse_ip(const char *p, u32 *varp); /** Parser for IP addresses. **/