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> | @<list>\n\
43 <static>\t[-]<type><name>\n\
44 <list>\t\t<name>{[<items>]}\n\
48 #\t\t32-bit integer\n\
49 ##\t\t64-bit integer\n\
50 $\t\tFloating point number\n\
53 !\t\tReport unknown items as errors\n\
54 -\t\tDo not dump item's value\n\
68 #define FLAG_NO_UNKNOWN 0x2
85 static struct mempool *pool;
86 static clist sections;
100 die("Missing '%c'", c);
109 uns len = pos - name;
111 die("Expected item/section name");
112 byte *buf = mp_alloc(pool, len + 1);
113 memcpy(buf, name, len);
119 parse_section(struct section *section)
121 for (uns sep = 0; ; sep = 1)
124 if (!*pos || *pos == '}')
135 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
136 sec->size = sizeof(cnode);
137 clist_init(&sec->list);
139 item->cf.name = parse_name();
140 item->cf.cls = CC_LIST;
149 item = mp_alloc_zero(pool, sizeof(*item));
152 item->flags |= FLAG_HIDE;
155 item->cf.cls = CC_STATIC;
163 item->cf.type = CT_U64;
166 item->cf.type = CT_INT;
170 item->cf.type = CT_DOUBLE;
174 die("Invalid type syntax");
175 item->cf.type = CT_STRING;
178 item->cf.name = parse_name();
184 if (section->item.cf.cls == CC_LIST)
185 die("List items can not have default values");
186 byte *def = pos, *d = def;
187 while (*pos != ';' && *pos != '}' && !Cspace(*pos))
195 die("Unterminated string");
200 else if (*pos == '"')
205 while (*pos != '"' || esc)
208 die("Unterminated string");
217 d = str_unesc(start, start);
223 byte *buf = mp_alloc(pool, len + 1);
224 memcpy(buf, def, len);
226 switch (item->cf.type)
227 #define TRY(x) do{byte *_err=(x); if (_err) die(_err); }while(0)
230 item->value.v_ptr = buf;
233 TRY(cf_parse_int(buf, &item->value.v_int));
236 TRY(cf_parse_u64(buf, &item->value.v_u64));
239 TRY(cf_parse_double(buf, &item->value.v_double));
247 if (section->item.cf.cls == CC_LIST)
249 item->cf.ptr = (void *)section->size;
250 section->size += sizeof(union value);
253 item->cf.ptr = &item->value;
254 clist_add_tail(§ion->list, &item->node);
262 for (uns sep = 0; ; sep = 1)
270 struct section *sec = mp_alloc_zero(pool, sizeof(*sec));
274 sec->item.flags |= FLAG_NO_UNKNOWN;
276 sec->item.cf.name = parse_name();
279 clist_add_tail(§ions, &sec->item.node);
280 clist_init(&sec->list);
286 static struct cf_section *
287 generate_section(struct section *section)
289 struct cf_section *sec = mp_alloc_zero(pool, sizeof(*sec));
290 if (section->item.cf.cls == CC_LIST)
291 sec->size = section->size;
292 struct cf_item *c = sec->cfg = mp_alloc_zero(pool, sizeof(struct cf_item) * (section->count + 1));
293 CLIST_FOR_EACH(struct item *, item, section->list)
296 if (c->cls == CC_LIST)
297 c->u.sec = generate_section((struct section *)item);
307 dump_item(struct item *item, void *ptr, uns path_len)
309 if (item->flags & FLAG_HIDE)
311 union value *val = (union value *)((addr_int_t)ptr + (addr_int_t)item->cf.ptr);
312 if (item->cf.cls == CC_LIST)
314 uns len = strlen(item->cf.name);
315 bb_grow(&path, path_len + len + 1);
316 path.ptr[path_len] = '_';
317 memcpy(path.ptr + path_len + 1, item->cf.name, len);
318 CLIST_FOR_EACH(cnode *, ptr2, val->list)
319 CLIST_FOR_EACH(struct item *, item2, ((struct section *)item)->list)
320 dump_item(item2, ptr2, path_len + len + 1);
324 byte *name = item->cf.name;
325 byte buf[128], *value = buf;
326 bb_grow(&path, path_len + 1)[path_len] = 0;
328 printf("CF_%s_%s='", path.ptr, name);
330 printf("CF_%s_%s[%u]='", path.ptr, name, item->index++);
331 switch (item->cf.type)
334 sprintf(buf, "%d", val->v_int);
337 sprintf(buf, "%Lu", val->v_u64);
340 sprintf(buf, "%g", val->v_double);
354 die("Apostrophes are not supported in config of scripts");
366 int main(int argc, char **argv)
371 pos = argv[argc - 1];
372 argv[argc - 1] = NULL;
374 pool = mp_new(0x1000);
375 clist_init(§ions);
377 CLIST_FOR_EACH(struct section *, sec, sections)
378 cf_declare_section(sec->item.cf.name, generate_section(sec), !(sec->item.flags & FLAG_NO_UNKNOWN));
380 if (cf_getopt(argc - 1, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) != -1)
384 CLIST_FOR_EACH(struct section *, section, sections)
386 uns len = strlen(section->item.cf.name);
387 memcpy(bb_grow(&path, len), section->item.cf.name, len);
388 CLIST_FOR_EACH(struct item *, item, section->list)
389 dump_item(item, NULL, len);