2 * UCW Library -- Reading of 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.
15 CC_END, // end of list
16 CC_STATIC, // single variable or static array
17 CC_DYNAMIC, // dynamically allocated array
18 CC_PARSER, // arbitrary parser function
19 CC_SECTION, // section appears exactly once
20 CC_LIST // list with 0..many nodes
24 CT_INT, CT_U64, CT_DOUBLE, // number types
25 CT_STRING // string type
28 typedef byte *cf_parser(uns number, byte **pars, void *ptr);
29 /* A parser function gets an array of (strdup'ed) strings and a pointer with
30 * the customized information (most likely the target address). It can store
31 * the parsed value anywhere in any way it likes, however it must first call
32 * cf_journal_block() on the overwritten memory block. It returns an error
33 * message or NULL if everything is all right. */
34 typedef byte *cf_hook(void *ptr);
35 /* An init- or commit-hook gets a pointer to the section or NULL if this
36 * is the global section. It returns an error message or NULL if everything
37 * is all right. The init-hook should fill in default values (needed for
38 * dynamically allocated nodes of link lists or for filling global variables
39 * that are run-time dependent). The commit-hook should perform sanity
40 * checks and postprocess the parsed values. Commit-hooks must call
41 * cf_journal_block() too. */
47 int number; // length of an array or #parameters of a parser
48 void *ptr; // pointer to a global variable or an offset in a section
50 enum cf_type type; // type of a static or dynamic attribute
51 struct cf_section *sub; // declaration of a section or a list
52 cf_parser *par; // parser function
57 uns size; // 0 for a global block, sizeof(struct) for a section
58 cf_hook *init; // fills in default values (otherwise 0's are used)
59 cf_hook *commit; // verifies parsed data (optional)
60 struct cf_item *cfg; // CC_END-terminated array of items
63 /* Declaration of cf_section */
64 #define CF_TYPE(s) .size = sizeof(s)
65 #define CF_INIT(f) .init = (cf_hook*) f
66 #define CF_COMMIT(f) .commit = (cf_hook*) f
67 #define CF_ITEMS .cfg = ( struct cf_item[] )
68 #define CF_END { .cls = CC_END }
69 /* Configuration items */
71 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*), .u.type = CT_##T }
72 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**), .u.type = CT_##T }
73 #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
74 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sub = s }
75 #define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sub = s }
76 /* Configuration items for basic types */
77 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1)
78 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c)
79 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c)
80 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1)
81 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c)
82 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c)
83 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1)
84 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c)
85 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c)
86 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,byte*,1)
87 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,byte*,c)
88 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,byte*,c)
90 #define DYN_LEN(a) *(uns*)(a-1)
91 // length of a dynamic array
92 #define DYN_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
93 // creates a static instance of a dynamic array
95 /* Memory allocation */
96 void *cf_malloc(uns size);
97 void *cf_malloc_zero(uns size);
98 byte *cf_strdup(byte *s);
99 byte *cf_printf(char *fmt, ...);
101 /* Undo journal for error recovery */
102 void cf_journal_block(void *ptr, uns len);
104 /* Parsers for basic types */
105 byte *cf_parse_int(uns number, byte **pars, int *ptr);
106 byte *cf_parse_u64(uns number, byte **pars, u64 *ptr);
107 byte *cf_parse_double(uns number, byte **pars, double *ptr);