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.
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
21 CC_BITMAP // of up to 32 items
25 CT_INT, CT_U64, CT_DOUBLE, // number types
27 CT_STRING, // string type
28 CT_LOOKUP, // in a string table
29 CT_USER // user-defined type
33 typedef byte *cf_parser(uns number, byte **pars, void *ptr);
34 /* A parser function gets an array of (strdup'ed) strings and a pointer with
35 * the customized information (most likely the target address). It can store
36 * the parsed value anywhere in any way it likes, however it must first call
37 * cf_journal_block() on the overwritten memory block. It returns an error
38 * message or NULL if everything is all right. */
39 typedef byte *cf_parser1(byte *string, void *ptr);
40 /* A parser function for user-defined types gets a string and a pointer to
41 * the destination variable. It must store the value within [ptr,ptr+size),
42 * where size is fixed for each type. It should not call cf_journal_block(). */
43 typedef byte *cf_hook(void *ptr);
44 /* An init- or commit-hook gets a pointer to the section or NULL if this
45 * is the global section. It returns an error message or NULL if everything
46 * is all right. The init-hook should fill in default values (needed for
47 * dynamically allocated nodes of link lists or for filling global variables
48 * that are run-time dependent). The commit-hook should perform sanity
49 * checks and postprocess the parsed values. Commit-hooks must call
50 * cf_journal_block() too. Caveat! init-hooks for static sections must not
51 * use cf_malloc() but normal xmalloc(). */
52 typedef void cf_dumper1(struct fastbuf *fb, void *ptr);
53 /* Dumps the contents of a variable of a user-defined type. */
54 typedef byte *cf_copier(void *dest, void *src);
55 /* Similar to init-hook, but it copies attributes from another list node
56 * instead of setting the attributes to default values. You have to provide
57 * it if your node contains parsed values and/or sub-lists. */
60 uns size; // of the parsed attribute
61 byte *name; // name of the type (for dumping)
62 cf_parser1 *parser; // how to parse it
63 cf_dumper1 *dumper; // how to dump the type
68 byte *name; // case insensitive
69 int number; // length of an array or #parameters of a parser (negative means at most)
70 void *ptr; // pointer to a global variable or an offset in a section
72 struct cf_section *sec; // declaration of a section or a list
73 cf_parser *par; // parser function
74 byte **lookup; // NULL-terminated sequence of allowed strings for lookups
75 struct cf_user_type *utype; // specification of the user-defined type
77 enum cf_class cls:16; // attribute class
78 enum cf_type type:16; // type of a static or dynamic attribute
82 uns size; // 0 for a global block, sizeof(struct) for a section
83 cf_hook *init; // fills in default values (no need to bzero)
84 cf_hook *commit; // verifies parsed data (optional)
85 cf_copier *copy; // copies values from another instance (optional, no need to copy basic attributes)
86 struct cf_item *cfg; // CC_END-terminated array of items
87 uns flags; // for internal use only
90 /* Declaration of cf_section */
91 #define CF_TYPE(s) .size = sizeof(s)
92 #define CF_INIT(f) .init = (cf_hook*) f
93 #define CF_COMMIT(f) .commit = (cf_hook*) f
94 #define CF_COPY(f) .copy = (cf_copier*) f
95 #define CF_ITEMS .cfg = ( struct cf_item[] )
96 #define CF_END { .cls = CC_END }
97 /* Configuration items */
98 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
99 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
100 #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
101 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
102 #define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,clist*), .u.sec = s }
103 #define CF_BITMAP_INT(n,p) { .cls = CC_BITMAP, .type = CT_INT, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,u32*) }
104 #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 }
105 /* Configuration items for basic types */
106 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1)
107 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c)
108 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c)
109 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1)
110 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c)
111 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c)
112 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1)
113 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c)
114 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c)
115 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1)
116 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c)
117 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c)
118 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1)
119 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c)
120 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c)
121 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,byte*,1)
122 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,byte*,c)
123 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,byte*,c)
124 #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 }
125 #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 }
126 #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 }
127 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
128 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
129 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
131 /* If you aren't picky about the number of parameters */
132 #define CF_ANY_NUM -0x7fffffff
134 #define DARY_LEN(a) ((uns*)a)[-1]
135 // length of a dynamic array
136 #define DARY_ALLOC(type,len,val...) ((struct { uns l; type a[len]; }) { .l = len, .a = { val } }).a
137 // creates a static instance of a dynamic array
139 /* Memory allocation: conf-alloc.c */
141 extern struct mempool *cf_pool;
142 void *cf_malloc(uns size);
143 void *cf_malloc_zero(uns size);
144 byte *cf_strdup(byte *s);
145 byte *cf_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2);
147 /* Undo journal for error recovery: conf-journal.c */
148 extern uns cf_need_journal;
149 void cf_journal_block(void *ptr, uns len);
150 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
152 /* Declaration: conf-section.c */
153 void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown);
154 void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero);
156 /* Parsers for basic types: conf-parse.c */
157 byte *cf_parse_int(byte *str, int *ptr);
158 byte *cf_parse_u64(byte *str, u64 *ptr);
159 byte *cf_parse_double(byte *str, double *ptr);
160 byte *cf_parse_ip(byte *p, u32 *varp);