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