]> mj.ucw.cz Git - libucw.git/blob - ucw/conf.h
42bf0c9493ca08410794729e7de15f3875a21a01
[libucw.git] / ucw / conf.h
1 /*
2  *      UCW Library -- Configuration files
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 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 /*** === Data types [[conf_types]] ***/
15
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
24 };
25
26 enum cf_type {                          /** Type of a single value. **/
27   CT_INT, CT_U64, CT_DOUBLE,            // number types
28   CT_IP,                                // IP address
29   CT_STRING,                            // string type
30   CT_LOOKUP,                            // in a string table
31   CT_USER                               // user-defined type
32 };
33
34 struct fastbuf;
35
36 /**
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.
42  **/
43 typedef char *cf_parser(uns number, char **pars, void *ptr);
44 /**
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().
48  **/
49 typedef char *cf_parser1(char *string, void *ptr);
50 /**
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()>>.
59  **/
60 typedef char *cf_hook(void *ptr);
61 /**
62  * Dumps the contents of a variable of a user-defined type.
63  **/
64 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
65 /**
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.
69  **/
70 typedef char *cf_copier(void *dest, void *src);
71
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
77 };
78
79 struct cf_section;
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
84   union cf_union {
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
89   } u;
90   enum cf_class cls:16;                 // attribute class
91   enum cf_type type:16;                 // type of a static or dynamic attribute
92 };
93
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
101 };
102
103 /***
104  * [[conf_macros]]
105  * Convenience macros
106  * ~~~~~~~~~~~~~~~~~~
107  *
108  * You could create the structures manually, but you can use these macros to
109  * save some typing.
110  */
111
112 /***
113  * Declaration of <<struct_cf_section,`cf_section`>>
114  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115  *
116  * These macros can be used to configure the <<struct_cf_section,`cf_section`>>
117  * structure.
118  ***/
119
120 /**
121  * Data type of a section.
122  * If you store the section into a structure, use this macro.
123  *
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>>).
126  *
127  * Example:
128  *
129  *   struct list_node {
130  *     cnode n;         // This one is for the list itself
131  *     char *name;
132  *     uns value;
133  *   };
134  *
135  *   struct clist nodes;
136  *
137  *   static struct cf_section node = {
138  *     CF_TYPE(struct list_node),
139  *     CF_ITEMS {
140  *       CF_STRING("name", PTR_TO(struct list_node, name)),
141  *       CF_UNS("value", PTR_TO(struct list_node, value)),
142  *       CF_END
143  *     }
144  *   };
145  *
146  *   static struct cf_section section = {
147  *     CF_LIST("node", &nodes, &node),
148  *     CF_END
149  *   };
150  *
151  * You could use <<def_CF_STATIC,`def_CF_STATIC`>> or <<def_CF_DYNAMIC,`def_CF_DYNAMIC`>>
152  * macros to create arrays.
153  */
154 #define CF_TYPE(s)      .size = sizeof(s)
155 #define CF_INIT(f)      .init = (cf_hook*) f            /** Init <<hooks,hook>>. **/
156 #define CF_COMMIT(f)    .commit = (cf_hook*) f          /** Commit <<hooks,hook>>. **/
157 #define CF_COPY(f)      .copy = (cf_copier*) f          /** <<hooks,Copy function>>. **/
158 #define CF_ITEMS        .cfg = ( struct cf_item[] )     /** List of sub-items. **/
159 #define CF_END          { .cls = CC_END }               /** End of the structure. **/
160 /***
161  * Declaration of a configuration item
162  * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
163  *
164  * Each of these describe single <<struct_cf_item,configuration item>>. They are mostly
165  * for internal use, do not use them directly unless you really know what you are doing.
166  ***/
167
168 /**
169  * Static array of items.
170  * Expects you to allocate the memory and provide pointer to it.
171  **/
172 #define CF_STATIC(n,p,T,t,c)    { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
173 /**
174  * Dynamic array of items.
175  * Expects you to provide pointer to your pointer to data and it will allocate new memory for it
176  * and set your pointer to it.
177  **/
178 #define CF_DYNAMIC(n,p,T,t,c)   { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
179 #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. **/
180 #define CF_SECTION(n,p,s)       { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }                                             /** A sub-section. **/
181 #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. **/
182 #define CF_BITMAP_INT(n,p)      { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) }                     /** A bitmap. **/
183 #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. **/
184 /***
185  * Basic configuration items
186  * ^^^^^^^^^^^^^^^^^^^^^^^^^
187  *
188  * They describe basic data types used in the configuration. This should be enough for
189  * most real-life purposes.
190  *
191  * The parameters are as follows:
192  *
193  * * @n -- name of the item.
194  * * @p -- pointer to the variable where it shall be stored.
195  * * @c -- count.
196  **/
197 #define CF_INT(n,p)             CF_STATIC(n,p,INT,int,1)                /** Single `int` value. **/
198 #define CF_INT_ARY(n,p,c)       CF_STATIC(n,p,INT,int,c)                /** Static array of `int` s. **/
199 #define CF_INT_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,int,c)               /** Dynamic array of `int` s. **/
200 #define CF_UNS(n,p)             CF_STATIC(n,p,INT,uns,1)                /** Single `uns` (`unsigned`) value. **/
201 #define CF_UNS_ARY(n,p,c)       CF_STATIC(n,p,INT,uns,c)                /** Static array of `uns` es. **/
202 #define CF_UNS_DYN(n,p,c)       CF_DYNAMIC(n,p,INT,uns,c)               /** Dynamic array of `uns` es. **/
203 #define CF_U64(n,p)             CF_STATIC(n,p,U64,u64,1)                /** Single unsigned 64bit integer (`u64`). **/
204 #define CF_U64_ARY(n,p,c)       CF_STATIC(n,p,U64,u64,c)                /** Static array of `u64` s. **/
205 #define CF_U64_DYN(n,p,c)       CF_DYNAMIC(n,p,U64,u64,c)               /** Dynamic array of `u64` s. **/
206 #define CF_DOUBLE(n,p)          CF_STATIC(n,p,DOUBLE,double,1)          /** Single instance of `double`. **/
207 #define CF_DOUBLE_ARY(n,p,c)    CF_STATIC(n,p,DOUBLE,double,c)          /** Static array of `double` s. **/
208 #define CF_DOUBLE_DYN(n,p,c)    CF_DYNAMIC(n,p,DOUBLE,double,c)         /** Dynamic array of `double` s. **/
209 #define CF_IP(n,p)              CF_STATIC(n,p,IP,u32,1)                 /** Single IPv4 address. **/
210 #define CF_IP_ARY(n,p,c)        CF_STATIC(n,p,IP,u32,c)                 /** Static array of IP addresses. **/.
211 #define CF_IP_DYN(n,p,c)        CF_DYNAMIC(n,p,IP,u32,c)                /** Dynamic array of IP addresses. **/
212 #define CF_STRING(n,p)          CF_STATIC(n,p,STRING,char*,1)           /** One string. **/
213 #define CF_STRING_ARY(n,p,c)    CF_STATIC(n,p,STRING,char*,c)           /** Static array of strings. **/
214 #define CF_STRING_DYN(n,p,c)    CF_DYNAMIC(n,p,STRING,char*,c)          /** Dynamic array of strings. **/
215 /**
216  * One string out of a predefined set.
217  * You provide the set as an array of strings terminated by NULL (similar to @argv argument
218  * of main()) as the @t parameter.
219  **/
220 #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 }
221 /**
222  * Static array of strings out of predefined set.
223  * See <<def_CF_LOOKUP,`CF_LOOKUP`>>.
224  **/
225 #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 }
226 /**
227  * Dynamic array of strings out of predefined set.
228  * See <<def_CF_LOOKUP,`CF_LOOKUP`>>.
229  **/
230 #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 }
231 /**
232  * A user defined type.
233  * See <<custom_parser,creating custom parsers>> section if you want to know more.
234  **/
235 #define CF_USER(n,p,t)          { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
236 /**
237  * Static array of user defined types (all of the same type).
238  * See <<custom_parser,creating custom parsers>> section.
239  **/
240 #define CF_USER_ARY(n,p,t,c)    { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
241 /**
242  * Dynamic array of user defined types.
243  * See <<custom_parser,creating custom parsers>> section.
244  **/
245 #define CF_USER_DYN(n,p,t,c)    { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
246
247 /**
248  * Any number of dynamic array elements
249  **/
250 #define CF_ANY_NUM              -0x7fffffff
251
252 #define DARY_LEN(a) ((uns*)a)[-1]       /** Length of an dynamic array. **/
253 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
254   // creates a static instance of a dynamic array
255
256 /***
257  * [[alloc]]
258  * Memory allocation
259  * ~~~~~~~~~~~~~~~~~
260  *
261  * Uses <<mempool:,memory pools>> for efficiency and journal recovery.
262  * You should use these routines when implementing custom parsers.
263  ***/
264 struct mempool;
265 extern struct mempool *cf_pool; /** A <<mempool:type_mempool,memory pool>> for configuration parser needs. **/
266 void *cf_malloc(uns size);      /** Returns @size bytes of memory. **/
267 void *cf_malloc_zero(uns size); /** Like @cf_malloc(), but zeroes the memory. **/
268 char *cf_strdup(const char *s); /** Copy a string into @cf_malloc()ed memory. **/
269 char *cf_printf(const char *fmt, ...) FORMAT_CHECK(printf,1,2); /** printf() into @cf_malloc()ed memory. **/
270
271 /***
272  * [[journal]]
273  * Undo journal
274  * ~~~~~~~~~~~~
275  *
276  * For error recovery
277  ***/
278 extern uns cf_need_journal;
279 void cf_journal_block(void *ptr, uns len);
280 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
281
282 /* Declaration: conf-section.c */
283 void cf_declare_section(const char *name, struct cf_section *sec, uns allow_unknown);
284 void cf_init_section(const char *name, struct cf_section *sec, void *ptr, uns do_bzero);
285
286 /*** === Parsers for basic types [[bparser]] ***/
287 char *cf_parse_int(const char *str, int *ptr);          /** Parser for integers. **/
288 char *cf_parse_u64(const char *str, u64 *ptr);          /** Parser for 64 unsigned integers. **/
289 char *cf_parse_double(const char *str, double *ptr);    /** Parser for doubles. **/
290 char *cf_parse_ip(const char *p, u32 *varp);            /** Parser for IP addresses. **/
291
292 #endif
293