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