2 * UCW Library -- Shell Interface to Configuration Files
4 * (c) 2002--2005 Martin Mares <mj@ucw.cz>
5 * (c) 2006 Robert Spalek <robert@ucw.cz>
6 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
8 * Once we were using this beautiful Shell version, but it turned out
9 * that it doesn't work with nested config files:
11 * eval `sed <cf/sherlock '/^#/d;/^ *$/d;s/ \+$//;
12 * h;s@[^ ]*@@;x;s@[ ].*@@;y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/;G;s/\n//;
13 * /^\[SECTION\]/,/^\[/ {; /^[A-Z]/ { s/^\([^ ]\+\)[ ]*\(.*\)$/SH_\1="\2"/; p; }; };
16 * This software may be freely distributed and used according to the terms
17 * of the GNU Lesser General Public License.
22 #include "lib/getopt.h"
23 #include "lib/clists.h"
24 #include "lib/mempool.h"
25 #include "lib/chartype.h"
37 Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <sections>\n\
39 <sections>\t<section>[;<sections>]\n\
40 <section>\t[*]<name>{[<items>]}\n\
41 <items>\t\t[-]<item>[;<items>]\n\
42 <item>\t\t<static> | <array> | <list>\n\
43 <static>\t<type><name>[=<value>]\n\
44 <list>\t\t@<name>{[<items>]}\n\
45 <array>\t\t<type><name><left-bracket>[<number>]<right-bracket>\n\
46 <value>\t\t[a-zA-Z0-9.-/]* | 'string without single quotes'<value> | \"c-like string\"<value>\n\
50 #\t\t32-bit integer\n\
51 ##\t\t64-bit integer\n\
52 $\t\tFloating point number\n\
55 !\t\tReport unknown items as errors\n\
56 -\t\tDo not dump item's value\n\
70 #define FLAG_NO_UNKNOWN 0x2
87 static struct mempool *pool;
88 static clist sections;
102 die("Missing '%c'", c);
111 uns len = pos - name;
113 die("Expected item/section name");
114 byte *buf = mp_alloc(pool, len + 1);
115 memcpy(buf, name, len);
121 parse_section(struct section *section)
123 #define TRY(x) do{byte *_err=(x); if (_err) die(_err); }while(0)
124 for (uns sep = 0; ; sep = 1)
127 if (!*pos || *pos == '}')
138 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
139 sec->size = sizeof(cnode);
140 clist_init(&sec->list);
142 item->cf.name = parse_name();
143 item->cf.cls = CC_LIST;
152 item = mp_alloc_zero(pool, sizeof(*item));
155 item->flags |= FLAG_HIDE;
158 item->cf.cls = CC_STATIC;
166 item->cf.type = CT_U64;
169 item->cf.type = CT_INT;
172 item->cf.type = CT_DOUBLE;
175 item->cf.type = CT_STRING;
178 die("Invalid type syntax");
181 item->cf.name = parse_name();
187 item->cf.cls = CC_DYNAMIC;
189 while (*pos && *pos != ']')
195 item->cf.number = CF_ANY_NUM;
199 TRY(cf_parse_int(num, &inum));
201 die("Invalid array length");
202 item->cf.number = inum;
210 if (section->item.cf.cls == CC_LIST)
211 die("List items can not have default values");
212 if (item->cf.cls == CC_DYNAMIC)
213 die("Arrays can not have default values");
214 byte *def = pos, *d = def;
215 while (*pos != ';' && *pos != '}' && !Cspace(*pos))
223 die("Unterminated string");
228 else if (*pos == '"')
233 while (*pos != '"' || esc)
236 die("Unterminated string");
245 d = str_unesc(start, start);
251 byte *buf = mp_alloc(pool, len + 1);
252 memcpy(buf, def, len);
254 switch (item->cf.type)
257 item->value.v_ptr = buf;
260 TRY(cf_parse_int(buf, &item->value.v_int));
263 TRY(cf_parse_u64(buf, &item->value.v_u64));
266 TRY(cf_parse_double(buf, &item->value.v_double));
273 if (section->item.cf.cls == CC_LIST)
275 item->cf.ptr = (void *)section->size;
276 section->size += sizeof(union value);
279 item->cf.ptr = &item->value;
280 clist_add_tail(§ion->list, &item->node);
289 for (uns sep = 0; ; sep = 1)
297 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
301 sec->item.flags |= FLAG_NO_UNKNOWN;
303 sec->item.cf.name = parse_name();
306 clist_add_tail(§ions, &sec->item.node);
307 clist_init(&sec->list);
313 static struct cf_section *
314 generate_section(struct section *section)
316 struct cf_section *sec = mp_alloc_zero(pool, sizeof(*sec));
317 if (section->item.cf.cls == CC_LIST)
318 sec->size = section->size;
319 struct cf_item *c = sec->cfg = mp_alloc_zero(pool, sizeof(struct cf_item) * (section->count + 1));
320 CLIST_FOR_EACH(struct item *, item, section->list)
323 if (c->cls == CC_LIST)
324 c->u.sec = generate_section((struct section *)item);
334 type_size(enum cf_type type)
338 case CT_INT: return sizeof(int);
339 case CT_U64: return sizeof(u64);
340 case CT_DOUBLE: return sizeof(double);
341 case CT_STRING: return sizeof(byte *);
347 dump_value(uns array, struct item *item, void *v)
349 byte buf[128], *value = buf;
351 printf("CF_%s_%s='", path.ptr, item->cf.name);
353 printf("CF_%s_%s[%u]='", path.ptr, item->cf.name, ++item->index);
354 switch (item->cf.type)
357 sprintf(buf, "%d", *(int *)v);
360 sprintf(buf, "%Lu", *(u64 *)v);
363 sprintf(buf, "%g", *(double *)v);
385 dump_item(struct item *item, void *ptr, uns path_len)
387 if (item->flags & FLAG_HIDE)
389 byte *val = (byte *)((addr_int_t)ptr + (addr_int_t)item->cf.ptr);
390 if (item->cf.cls == CC_LIST)
392 uns len = strlen(item->cf.name);
393 bb_grow(&path, path_len + len + 1);
394 path.ptr[path_len] = '_';
395 memcpy(path.ptr + path_len + 1, item->cf.name, len);
396 CLIST_FOR_EACH(cnode *, ptr2, *(clist *)val)
397 CLIST_FOR_EACH(struct item *, item2, ((struct section *)item)->list)
398 dump_item(item2, ptr2, path_len + len + 1);
402 bb_grow(&path, path_len + 1)[path_len] = 0;
403 if (item->cf.cls == CC_STATIC)
404 dump_value(!!ptr, item, val);
408 uns len = DARY_LEN(val);
409 uns size = type_size(item->cf.type);
410 for (uns i = 0; i < len; i++, val += size)
411 dump_value(1, item, val);
416 int main(int argc, char **argv)
421 pos = argv[argc - 1];
422 argv[argc - 1] = NULL;
424 pool = mp_new(0x1000);
425 clist_init(§ions);
427 CLIST_FOR_EACH(struct section *, sec, sections)
428 cf_declare_section(sec->item.cf.name, generate_section(sec), !(sec->item.flags & FLAG_NO_UNKNOWN));
430 if (cf_getopt(argc - 1, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
434 CLIST_FOR_EACH(struct section *, section, sections)
436 uns len = strlen(section->item.cf.name);
437 memcpy(bb_grow(&path, len), section->item.cf.name, len);
438 CLIST_FOR_EACH(struct item *, item, section->list)
439 dump_item(item, NULL, len);