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
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. */
55 uns size; // of the parsed attribute
56 cf_parser1 *parser; // how to parse it
57 cf_dumper1 *dumper; // optional and for debugging purposes only
62 byte *name; // case insensitive
63 int number; // length of an array or #parameters of a parser (negative means at most)
64 void *ptr; // pointer to a global variable or an offset in a section
66 struct cf_section *sec; // declaration of a section or a list
67 cf_parser *par; // parser function
68 char **lookup; // NULL-terminated sequence of allowed strings for lookups
69 struct cf_user_type *utype; // specification of the user-defined type
71 enum cf_class cls:16; // attribute class
72 enum cf_type type:16; // type of a static or dynamic attribute
76 uns size; // 0 for a global block, sizeof(struct) for a section
77 cf_hook *init; // fills in default values (no need to bzero)
78 cf_hook *commit; // verifies parsed data (optional)
79 struct cf_item *cfg; // CC_END-terminated array of items
80 uns flags; // for internal use only
83 /* Declaration of cf_section */
84 #define CF_TYPE(s) .size = sizeof(s)
85 #define CF_INIT(f) .init = (cf_hook*) f
86 #define CF_COMMIT(f) .commit = (cf_hook*) f
87 #define CF_ITEMS .cfg = ( struct cf_item[] )
88 #define CF_END { .cls = CC_END }
89 /* Configuration items */
91 #define CF_STATIC(n,p,T,t,c) { .cls = CC_STATIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t*) }
92 #define CF_DYNAMIC(n,p,T,t,c) { .cls = CC_DYNAMIC, .type = CT_##T, .name = n, .number = c, .ptr = CHECK_PTR_TYPE(p,t**) }
93 #define CF_PARSER(n,p,f,c) { .cls = CC_PARSER, .name = n, .number = c, .ptr = p, .u.par = (cf_parser*) f }
94 #define CF_SECTION(n,p,s) { .cls = CC_SECTION, .name = n, .number = 1, .ptr = p, .u.sec = s }
95 #define CF_LIST(n,p,s) { .cls = CC_LIST, .name = n, .number = 1, .ptr = CHECK_PTR_TYPE(p,struct clist*), .u.sec = s }
96 /* Configuration items for basic types */
97 #define CF_INT(n,p) CF_STATIC(n,p,INT,int,1)
98 #define CF_INT_ARY(n,p,c) CF_STATIC(n,p,INT,int,c)
99 #define CF_INT_DYN(n,p,c) CF_DYNAMIC(n,p,INT,int,c)
100 #define CF_UNS(n,p) CF_STATIC(n,p,INT,uns,1)
101 #define CF_UNS_ARY(n,p,c) CF_STATIC(n,p,INT,uns,c)
102 #define CF_UNS_DYN(n,p,c) CF_DYNAMIC(n,p,INT,uns,c)
103 #define CF_U64(n,p) CF_STATIC(n,p,U64,u64,1)
104 #define CF_U64_ARY(n,p,c) CF_STATIC(n,p,U64,u64,c)
105 #define CF_U64_DYN(n,p,c) CF_DYNAMIC(n,p,U64,u64,c)
106 #define CF_DOUBLE(n,p) CF_STATIC(n,p,DOUBLE,double,1)
107 #define CF_DOUBLE_ARY(n,p,c) CF_STATIC(n,p,DOUBLE,double,c)
108 #define CF_DOUBLE_DYN(n,p,c) CF_DYNAMIC(n,p,DOUBLE,double,c)
109 #define CF_IP(n,p) CF_STATIC(n,p,IP,u32,1)
110 #define CF_IP_ARY(n,p,c) CF_STATIC(n,p,IP,u32,c)
111 #define CF_IP_DYN(n,p,c) CF_DYNAMIC(n,p,IP,u32,c)
112 #define CF_STRING(n,p) CF_STATIC(n,p,STRING,byte*,1)
113 #define CF_STRING_ARY(n,p,c) CF_STATIC(n,p,STRING,byte*,c)
114 #define CF_STRING_DYN(n,p,c) CF_DYNAMIC(n,p,STRING,byte*,c)
115 #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 }
116 #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 }
117 #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 }
118 #define CF_USER(n,p,t) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = 1, .ptr = p, .u.utype = t }
119 #define CF_USER_ARY(n,p,t,c) { .cls = CC_STATIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
120 #define CF_USER_DYN(n,p,t,c) { .cls = CC_DYNAMIC, .type = CT_USER, .name = n, .number = c, .ptr = p, .u.utype = t }
121 // Beware that CF_USER_DYN can only be used on user-defined types of size at least 4
123 /* If you aren't picky about the number of parameters */
124 #define CF_ANY_NUM -0x7fffffff
126 #define DARY_LEN(a) *(uns*)(a-1)
127 // length of a dynamic array
128 #define DARY_ALLOC(type,len,val...) (type[]) { (type)len, ##val } + 1
129 // creates a static instance of a dynamic array
130 // FIXME: overcast doesn't work for the double type
132 /* Memory allocation */
134 extern struct mempool *cf_pool;
135 void *cf_malloc(uns size);
136 void *cf_malloc_zero(uns size);
137 byte *cf_strdup(byte *s);
138 byte *cf_printf(char *fmt, ...) FORMAT_CHECK(printf,1,2);
140 /* Undo journal for error recovery */
141 extern uns cf_need_journal;
142 void cf_journal_block(void *ptr, uns len);
143 #define CF_JOURNAL_VAR(var) cf_journal_block(&(var), sizeof(var))
146 void cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown);
147 void cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero);
149 /* Safe reloading and loading of configuration files */
150 extern byte *cf_def_file;
151 int cf_reload(byte *file);
152 int cf_load(byte *file);
153 int cf_set(byte *string);
155 /* Parsers for basic types */
156 byte *cf_parse_int(byte *str, int *ptr);
157 byte *cf_parse_u64(byte *str, u64 *ptr);
158 byte *cf_parse_double(byte *str, double *ptr);
159 byte *cf_parse_ip(byte *p, u32 *varp);
161 /* Direct access to configuration items */
163 #define CF_OPERATIONS T(CLOSE) T(SET) T(CLEAR) T(APPEND) T(PREPEND) \
164 T(REMOVE) T(EDIT) T(AFTER) T(BEFORE)
165 /* Closing brace finishes previous block.
166 * Basic attributes (static, dynamic, parsed) can be used with SET.
167 * Dynamic arrays can be used with SET, APPEND, PREPEND.
168 * Sections can be used with SET.
169 * Lists can be used with everything. */
171 enum cf_operation { CF_OPERATIONS };
174 byte *cf_find_item(byte *name, struct cf_item *item);
175 byte *cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars);
176 void cf_dump_sections(struct fastbuf *fb);
179 * When using cf_get_opt(), you must prefix your own short/long options by the
180 * CF_(SHORT|LONG)_OPTS.
182 * cf_def_file contains the name of a configuration file that will be
183 * automatically loaded before the first --set option is executed. If no --set
184 * option occurs, it will be loaded after getopt() returns -1 (i.e. at the end
185 * of the configuration options). cf_def_file will be ignored if another
186 * configuration file has already been loaded using the --config option. The
187 * initial value of cf_def_file is DEFAULT_CONFIG from config.h, but you can
188 * override it manually before calling cf_get_opt().
191 #define CF_SHORT_OPTS "C:S:"
192 #define CF_LONG_OPTS {"config", 1, 0, 'C'}, {"set", 1, 0, 'S'},
193 #define CF_NO_LONG_OPTS (const struct option []) { CF_LONG_OPTS { NULL, 0, 0, 0 } }
195 #define CF_USAGE_TAB ""
198 "-C, --config filename\t" CF_USAGE_TAB "Override the default configuration file\n\
199 -S, --set sec.item=val\t" CF_USAGE_TAB "Manual setting of a configuration item\n"
203 int cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index);