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.
12 #include "lib/conf2.h"
13 #include "lib/mempool.h"
14 #include "lib/clists.h"
20 /* Memory allocation */
22 struct mempool *cf_pool; // current pool for loading new configuration
23 static struct old_pools {
24 struct old_pools *prev;
26 } *pools; // link-list of older cf_pool's
31 return mp_alloc(cf_pool, size);
35 cf_malloc_zero(uns size)
37 return mp_alloc_zero(cf_pool, size);
43 return mp_strdup(cf_pool, s);
47 cf_printf(char *fmt, ...)
51 byte *res = mp_vprintf(cf_pool, fmt, args);
58 uns cf_need_journal; // some programs do not need journal
59 static struct journal_item {
60 struct journal_item *prev;
67 cf_journal_block(void *ptr, uns len)
71 struct journal_item *ji = cf_malloc(sizeof(struct journal_item) + len);
75 memcpy(ji->copy, ptr, len);
81 // swaps the contents of the memory and the journal, and reverses the list
83 struct journal_item *curr, *prev, *next;
84 for (next=NULL, curr=journal; curr; next=curr, curr=prev)
88 for (uns i=0; i<curr->len; i++)
90 byte x = curr->copy[i];
91 curr->copy[i] = curr->ptr[i];
98 static struct journal_item *
99 journal_new_section(uns new_pool)
102 cf_pool = mp_new(1<<14);
103 struct journal_item *oldj = journal;
109 journal_commit_section(uns new_pool, struct journal_item *oldj)
113 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
120 struct journal_item **j = &journal;
128 journal_rollback_section(uns new_pool, struct journal_item *oldj, byte *msg)
130 if (!cf_need_journal)
131 die("Cannot rollback the configuration, because the journal is disabled. Error: %s", msg);
137 cf_pool = pools ? pools->pool : NULL;
143 #define SEC_FLAG_DYNAMIC 0x80000000 // contains a dynamic attribute
144 #define SEC_FLAG_NUMBER 0x7fffffff // number of entries
146 static struct cf_section sections; // root section
148 static struct cf_item *
149 find_subitem(struct cf_section *sec, byte *name)
151 struct cf_item *ci = sec->cfg;
152 for (; ci->cls; ci++)
153 if (!strcasecmp(ci->name, name))
159 inspect_section(struct cf_section *sec)
163 for (ci=sec->cfg; ci->cls; ci++)
164 if (ci->cls == CC_SECTION) {
165 inspect_section(ci->u.sec);
166 sec->flags |= ci->u.sec->flags & SEC_FLAG_DYNAMIC;
167 } else if (ci->cls == CC_LIST) {
168 inspect_section(ci->u.sec);
169 sec->flags |= SEC_FLAG_DYNAMIC;
170 } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_PARSER && ci->number < 0)
171 sec->flags |= SEC_FLAG_DYNAMIC;
172 sec->flags |= ci - sec->cfg;
176 cf_declare_section(byte *name, struct cf_section *sec)
181 sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
183 struct cf_item *ci = find_subitem(§ions, name);
185 die("Cannot register section %s twice", name);
186 ci->cls = CC_SECTION;
191 inspect_section(sec);
193 if (ci - sections.cfg >= (int) sections.size)
195 sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
196 bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
202 cf_init_section(byte *name, struct cf_section *sec, void *ptr)
205 bzero(ptr, sec->size);
206 for (uns i=0; sec->cfg[i].cls; i++)
207 if (sec->cfg[i].cls == CC_SECTION)
208 cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr);
209 else if (sec->cfg[i].cls == CC_LIST)
210 clist_init(sec->cfg[i].ptr);
211 byte *msg = sec->init(ptr);
213 die("Cannot initialize section %s: %s", name, msg);
219 for (struct cf_item *ci=sections.cfg; ci->cls; ci++)
220 cf_init_section(ci->name, ci->u.sec, NULL);
223 static struct cf_item *
224 find_item(struct cf_section *curr_sec, byte *name, byte **msg)
227 if (name[0] == '^') // absolute name instead of relative
228 name++, curr_sec = §ions;
229 if (!curr_sec) // don't even search in an unknown section
233 byte *c = strchr(name, '.');
236 struct cf_item *ci = find_subitem(curr_sec, name);
239 if (curr_sec != §ions) // ignore silently unknown top-level sections
240 *msg = cf_printf("Unknown item %s", name);
245 if (ci->cls != CC_SECTION)
247 *msg = cf_printf("Item %s is not a section", name);
250 curr_sec = ci->u.sec;
255 /* Safe loading and reloading */
257 byte *cf_def_file = DEFAULT_CONFIG;
259 #ifndef DEFAULT_CONFIG
260 #define DEFAULT_CONFIG NULL
263 byte *cfdeffile = DEFAULT_CONFIG;
265 static byte *load_file(byte *file);
266 static byte *load_string(byte *string);
269 cf_reload(byte *file)
272 struct journal_item *oldj = journal_new_section(1);
273 byte *msg = load_file(file);
276 for (struct old_pools *p=pools; p; p=pools)
281 journal_commit_section(1, NULL);
285 journal_rollback_section(1, oldj, msg);
294 struct journal_item *oldj = journal_new_section(1);
295 byte *msg = load_file(file);
297 journal_commit_section(1, oldj);
299 journal_rollback_section(1, oldj, msg);
306 struct journal_item *oldj = journal_new_section(0);
307 byte *msg = load_string(string);
309 journal_commit_section(0, oldj);
311 journal_rollback_section(0, oldj, msg);
315 /* Parsers for standard types */
318 uns name; // one-letter name of the unit
319 uns num, den; // fraction
322 static const struct unit units[] = {
327 { 'g', 1000000000, 1 },
330 { 'G', 1073741824, 1 },
335 static const struct unit *
336 lookup_unit(byte *value, byte *end, byte **msg)
339 if (end == value || end[1] || *end >= '0' && *end <= '9')
340 *msg = "Invalid number";
342 for (const struct unit *u=units; u->name; u++)
345 *msg = "Invalid unit";
351 static char cf_rngerr[] = "Number out of range";
354 cf_parse_int(byte *str, int *ptr)
358 msg = "Missing number";
360 const struct unit *u;
363 uns x = strtoul(str, &end, 0);
366 else if (u = lookup_unit(str, end, &msg)) {
367 u64 y = (u64)x * u->num;
369 msg = "Number is not an integer";
383 cf_parse_u64(byte *str, u64 *ptr)
387 msg = "Missing number";
389 const struct unit *u;
392 u64 x = strtoull(str, &end, 0);
395 else if (u = lookup_unit(str, end, &msg)) {
396 if (x > ~(u64)0 / u->num)
397 msg = "Number out of range";
401 msg = "Number is not an integer";
412 cf_parse_double(byte *str, double *ptr)
416 msg = "Missing number";
418 const struct unit *u;
421 double x = strtoul(str, &end, 0);
424 else if (u = lookup_unit(str, end, &msg))
425 *ptr = x * u->num / u->den;
433 cf_parse_string(byte *str, byte **ptr)
435 *ptr = cf_strdup(str);
439 /* Register size of and parser for each basic type */
441 typedef byte *cf_basic_parser(byte *str, void *ptr);
446 { sizeof(int), cf_parse_int },
447 { sizeof(u64), cf_parse_u64 },
448 { sizeof(double), cf_parse_double },
449 { sizeof(byte*), cf_parse_string }
453 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type)
455 for (uns i=0; i<number; i++)
457 byte *msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
459 return cf_printf("Cannot parse item %d: %s", i+1, msg);
468 OP_SET, // basic attribute (static, dynamic, parsed), section, list
469 OP_APPEND, // dynamic array, list
470 OP_PREPEND, // dynamic array, list
473 #define OP_MASK 0x3f // only get the operation
474 #define OP_OPEN 0x40 // here we only get an opening brace
475 #define OP_RECORD 0x80 // record selectors into the mask
477 #define MAX_STACK_SIZE 100
478 static struct item_stack {
479 struct cf_section *sec; // nested section
480 void *base_ptr; // because original pointers are often relative
481 enum operation op; // it is performed when a closing brace is encountered
482 u32 mask; // bit array of selectors searching in a list
483 } stack[MAX_STACK_SIZE];
487 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
489 enum cf_type type = item->u.type;
490 cf_journal_block(ptr, sizeof(void*));
491 // boundary checks done by the caller
492 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
493 * (uns*) (*ptr - parsers[type].size) = number;
494 return cf_parse_ary(number, pars, *ptr, type);
498 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum operation op)
500 enum cf_type type = item->u.type;
502 int old_nr = * (int*) (old_p - parsers[type].size);
503 int taken = MIN(number, item->number-old_nr);
505 // stretch the dynamic array
506 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
507 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
508 cf_journal_block(ptr, sizeof(void*));
512 memcpy(new_p, old_p, old_nr * parsers[type].size);
513 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type);
515 else if (op == OP_PREPEND)
517 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
518 return cf_parse_ary(taken, pars, new_p, type);
524 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
527 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
530 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
533 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
535 return cf_printf("Item %s: %s", ci->name, msg);
539 if (!number) // stop parsing, because many parsers would complain that number==0
546 add_to_list(struct clist *list, struct cnode *node, enum operation op)
548 cf_journal_block(list, sizeof(struct clist));
550 clist_add_tail(list, node);
551 else if (op == OP_PREPEND)
552 clist_add_head(list, node);
558 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum operation op)
560 /* If the node contains any dynamic attribute at the end, we suppress
561 * repetition here and pass it inside instead. */
562 struct cf_section *sec = item->u.sec;
566 void *node = cf_malloc(sec->size);
567 cf_init_section(item->name, sec, node);
568 add_to_list(ptr, node, op);
570 byte *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
576 if (sec->flags & SEC_FLAG_DYNAMIC)
583 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
589 taken = MIN(number, item->number);
591 cf_journal_block(ptr, taken * parsers[item->u.type].size);
592 return cf_parse_ary(taken, pars, ptr, item->u.type);
595 return "Dynamic array cannot be used here";
596 taken = MIN(number, item->number);
598 return interpret_set_dynamic(item, taken, pars, ptr);
600 if (item->number < 0 && !allow_dynamic)
601 return "Parsers with variable number of parameters cannot be used here";
602 if (item->number > 0 && number < item->number)
603 return "Not enough parameters available for the parser";
604 taken = MIN(number, ABS(item->number));
606 for (int i=0; i<taken; i++)
607 pars[i] = cf_strdup(pars[i]);
608 return item->u.par(taken, pars, ptr);
610 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
613 return "Lists cannot be used here";
614 return interpret_add_list(item, number, pars, ptr, processed, OP_APPEND);
621 interpret_clear(struct cf_item *item, void *ptr)
623 if (item->cls == CC_LIST) {
624 cf_journal_block(ptr, sizeof(struct clist));
626 } else if (item->cls == CC_DYNAMIC) {
627 cf_journal_block(ptr, sizeof(void *));
628 * (void**) ptr = NULL;
630 return "The item is not a list or a dynamic array";
635 record_selector(struct cf_item *item)
637 struct cf_section *sec = stack[level].sec;
638 uns nr = sec->flags & SEC_FLAG_NUMBER;
639 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
641 uns i = item - sec->cfg;
643 return "Cannot select list nodes by this attribute";
644 stack[level].mask |= 1 << i;
650 increase_stack(struct cf_item *item, enum operation op)
652 if (level >= MAX_STACK_SIZE-1)
653 return "Too many nested sections";
655 if (item) // fill in the base pointer
657 if (item->cls == CC_SECTION)
658 stack[level].base_ptr = stack[level].base_ptr + (addr_int_t) item->ptr;
659 else if (item->cls != CC_LIST)
661 stack[level].base_ptr = cf_malloc(item->u.sec->size);
662 cf_init_section(item->name, item->u.sec, stack[level].base_ptr);
665 return "Opening brace can only be used on sections and lists";
666 stack[level].sec = item->u.sec;
668 else // unknown is also handled here, since we need to trace recursion
670 stack[level].base_ptr = NULL;
671 stack[level].sec = NULL;
673 stack[level].op = op;
674 stack[level].mask = 0;
679 interpret_line(byte *name, enum operation op, int number, byte **pars)
682 struct cf_item *item = find_item(stack[level].sec, name, &msg);
685 if (stack[level].op & OP_RECORD) {
686 msg = record_selector(item);
690 if (op & OP_OPEN) // the operation will be performed after the closing brace
691 return increase_stack(item, op);
692 if (!item) // ignored item in an unknown section
695 void *ptr = stack[level].base_ptr + (addr_int_t) item->ptr;
696 int taken; // process as many parameters as possible
699 taken = 0, msg = interpret_clear(item, ptr);
700 else if (op == OP_SET)
701 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
702 else if (item->cls == CC_DYNAMIC)
703 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
704 else if (item->cls == CC_LIST)
705 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
707 return cf_printf("Operation %d not supported for class %d", op, item->cls);
711 return cf_printf("Too many parameters: %d>%d", number, taken);