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/conf-internal.h"
24 #include "lib/clists.h"
25 #include "lib/mempool.h"
26 #include "lib/chartype.h"
38 Usage: config [-C<configfile>] [-S<section>.<option>=<value>] <sections>\n\
40 <sections>\t<section>[;<sections>]\n\
41 <section>\t[*]<name>{[<items>]}\n\
42 <items>\t\t[-]<item>[;<items>]\n\
43 <item>\t\t<static> | <array> | <list>\n\
44 <static>\t<type><name>[=<value>]\n\
45 <list>\t\t@<name>{[<items>]}\n\
46 <array>\t\t<type><name><left-bracket>[<number>]<right-bracket>\n\
47 <value>\t\t[a-zA-Z0-9.-/]* | 'string without single quotes'<value> | \"c-like string\"<value>\n\
51 #\t\t32-bit integer\n\
52 ##\t\t64-bit integer\n\
53 $\t\tFloating point number\n\
56 !\t\tReport unknown items as errors\n\
57 -\t\tDo not dump item's value\n\
71 #define FLAG_NO_UNKNOWN 0x2
88 static struct mempool *pool;
89 static clist sections;
103 die("Missing '%c'", c);
112 uns len = pos - name;
114 die("Expected item/section name");
115 byte *buf = mp_alloc(pool, len + 1);
116 memcpy(buf, name, len);
122 parse_section(struct section *section)
124 #define TRY(x) do{byte *_err=(x); if (_err) die(_err); }while(0)
125 for (uns sep = 0; ; sep = 1)
128 if (!*pos || *pos == '}')
139 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
140 sec->size = sizeof(cnode);
141 clist_init(&sec->list);
143 item->cf.name = parse_name();
144 item->cf.cls = CC_LIST;
153 item = mp_alloc_zero(pool, sizeof(*item));
156 item->flags |= FLAG_HIDE;
159 item->cf.cls = CC_STATIC;
167 item->cf.type = CT_U64;
170 item->cf.type = CT_INT;
173 item->cf.type = CT_DOUBLE;
176 item->cf.type = CT_STRING;
179 die("Invalid type syntax");
182 item->cf.name = parse_name();
188 item->cf.cls = CC_DYNAMIC;
190 while (*pos && *pos != ']')
196 item->cf.number = CF_ANY_NUM;
200 TRY(cf_parse_int(num, &inum));
202 die("Invalid array length");
203 item->cf.number = inum;
211 if (section->item.cf.cls == CC_LIST)
212 die("List items can not have default values");
213 if (item->cf.cls == CC_DYNAMIC)
214 die("Arrays can not have default values");
215 byte *def = pos, *d = def;
216 while (*pos != ';' && *pos != '}' && !Cspace(*pos))
224 die("Unterminated string");
229 else if (*pos == '"')
234 while (*pos != '"' || esc)
237 die("Unterminated string");
246 d = str_unesc(start, start);
252 byte *buf = mp_alloc(pool, len + 1);
253 memcpy(buf, def, len);
255 switch (item->cf.type)
258 item->value.v_ptr = buf;
261 TRY(cf_parse_int(buf, &item->value.v_int));
264 TRY(cf_parse_u64(buf, &item->value.v_u64));
267 TRY(cf_parse_double(buf, &item->value.v_double));
274 if (section->item.cf.cls == CC_LIST)
276 item->cf.ptr = (void *)section->size;
277 section->size += sizeof(union value);
280 item->cf.ptr = &item->value;
281 clist_add_tail(§ion->list, &item->node);
290 for (uns sep = 0; ; sep = 1)
298 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
302 sec->item.flags |= FLAG_NO_UNKNOWN;
304 sec->item.cf.name = parse_name();
307 clist_add_tail(§ions, &sec->item.node);
308 clist_init(&sec->list);
314 static struct cf_section *
315 generate_section(struct section *section)
317 struct cf_section *sec = mp_alloc_zero(pool, sizeof(*sec));
318 if (section->item.cf.cls == CC_LIST)
319 sec->size = section->size;
320 struct cf_item *c = sec->cfg = mp_alloc_zero(pool, sizeof(struct cf_item) * (section->count + 1));
321 CLIST_FOR_EACH(struct item *, item, section->list)
324 if (c->cls == CC_LIST)
325 c->u.sec = generate_section((struct section *)item);
335 dump_value(uns array, struct item *item, void *v)
337 byte buf[128], *value = buf;
339 printf("CF_%s_%s='", path.ptr, item->cf.name);
341 printf("CF_%s_%s[%u]='", path.ptr, item->cf.name, ++item->index);
342 switch (item->cf.type)
345 sprintf(buf, "%d", *(int *)v);
348 sprintf(buf, "%Lu", *(u64 *)v);
351 sprintf(buf, "%g", *(double *)v);
373 dump_item(struct item *item, void *ptr, uns path_len)
375 if (item->flags & FLAG_HIDE)
377 byte *val = (byte *)((addr_int_t)ptr + (addr_int_t)item->cf.ptr);
378 if (item->cf.cls == CC_LIST)
380 uns len = strlen(item->cf.name);
381 bb_grow(&path, path_len + len + 1);
382 path.ptr[path_len] = '_';
383 memcpy(path.ptr + path_len + 1, item->cf.name, len);
384 CLIST_FOR_EACH(cnode *, ptr2, *(clist *)val)
385 CLIST_FOR_EACH(struct item *, item2, ((struct section *)item)->list)
386 dump_item(item2, ptr2, path_len + len + 1);
390 bb_grow(&path, path_len + 1)[path_len] = 0;
391 if (item->cf.cls == CC_STATIC)
392 dump_value(!!ptr, item, val);
396 uns len = DARY_LEN(val);
397 uns size = cf_type_size(item->cf.type, NULL);
398 for (uns i = 0; i < len; i++, val += size)
399 dump_value(1, item, val);
404 int main(int argc, char **argv)
409 pos = argv[argc - 1];
410 argv[argc - 1] = NULL;
412 pool = mp_new(0x1000);
413 clist_init(§ions);
415 CLIST_FOR_EACH(struct section *, sec, sections)
416 cf_declare_section(sec->item.cf.name, generate_section(sec), !(sec->item.flags & FLAG_NO_UNKNOWN));
418 if (cf_getopt(argc - 1, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
422 CLIST_FOR_EACH(struct section *, section, sections)
424 uns len = strlen(section->item.cf.name);
425 memcpy(bb_grow(&path, len), section->item.cf.name, len);
426 CLIST_FOR_EACH(struct item *, item, section->list)
427 dump_item(item, NULL, len);