2 * UCW Library -- Configuration files: interpreter
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2014 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.
13 #include <ucw/getopt.h>
14 #include <ucw/conf-internal.h>
15 #include <ucw/clists.h>
17 #include <ucw/mempool.h>
22 #define TRY(f) do { char *_msg = f; if (_msg) return _msg; } while (0)
24 /* Register size of and parser for each basic type */
27 cf_parse_string(char *str, char **ptr)
29 *ptr = cf_strdup(str);
33 typedef char *cf_basic_parser(char *str, void *ptr);
38 { sizeof(int), cf_parse_int },
39 { sizeof(u64), cf_parse_u64 },
40 { sizeof(double), cf_parse_double },
41 { sizeof(u32), cf_parse_ip },
42 { sizeof(char*), cf_parse_string },
43 { sizeof(int), NULL }, // lookups are parsed extra
44 { 0, NULL }, // user-defined types are parsed extra
48 cf_type_size(enum cf_type type, struct cf_user_type *utype)
51 return parsers[type].size;
57 cf_parse_lookup(char *str, int *ptr, const char * const *t)
59 const char * const *n = t;
61 while (*n && strcasecmp(*n, str)) {
62 total_len += strlen(*n) + 2;
69 char *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
70 c += sprintf(err, "Invalid value %s, possible values are: ", str);
72 c+= sprintf(c, "%s, ", *n);
80 cf_parse_ary(uint number, char **pars, void *ptr, enum cf_type type, union cf_union *u)
82 for (uint i=0; i<number; i++)
85 uint size = cf_type_size(type, u->utype);
87 msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * size);
88 else if (type == CT_LOOKUP)
89 msg = cf_parse_lookup(pars[i], ptr + i * size, u->lookup);
90 else if (type == CT_USER)
91 msg = u->utype->parser(pars[i], ptr + i * size);
95 return number > 1 ? cf_printf("Item %d: %s", i+1, msg) : msg;
103 char *cf_op_names[] = { CF_OPERATIONS };
105 char *cf_type_names[] = { "int", "u64", "double", "ip", "string", "lookup", "user" };
108 interpret_set_dynamic(struct cf_item *item, int number, char **pars, void **ptr)
110 enum cf_type type = item->type;
111 uint size = cf_type_size(type, item->u.utype);
112 cf_journal_block(ptr, sizeof(void*));
113 // boundary checks done by the caller
114 *ptr = gary_init(size, number, mp_get_allocator(cf_get_pool()));
115 return cf_parse_ary(number, pars, *ptr, type, &item->u);
119 interpret_add_dynamic(struct cf_item *item, int number, char **pars, int *processed, void **ptr, enum cf_operation op)
121 enum cf_type type = item->type;
123 uint size = cf_type_size(item->type, item->u.utype);
124 ASSERT(size >= sizeof(uint));
125 int old_nr = old_p ? GARY_SIZE(old_p) : 0;
126 int taken = MIN(number, ABS(item->number)-old_nr);
128 // stretch the dynamic array
129 void *new_p = gary_init(size, old_nr + taken, mp_get_allocator(cf_get_pool()));
130 cf_journal_block(ptr, sizeof(void*));
132 if (op == OP_APPEND) {
133 memcpy(new_p, old_p, old_nr * size);
134 return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
135 } else if (op == OP_PREPEND) {
136 memcpy(new_p + taken * size, old_p, old_nr * size);
137 return cf_parse_ary(taken, pars, new_p, type, &item->u);
139 return cf_printf("Dynamic arrays do not support operation %s", cf_op_names[op]);
142 static char *interpret_set_item(struct cf_item *item, int number, char **pars, int *processed, void *ptr, uint allow_dynamic);
145 interpret_section(struct cf_section *sec, int number, char **pars, int *processed, void *ptr, uint allow_dynamic)
147 cf_add_dirty(sec, ptr);
149 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
152 char *msg = interpret_set_item(ci, number, pars, &taken, ptr + (uintptr_t) ci->ptr, allow_dynamic && !ci[1].cls);
154 return cf_printf("Item %s: %s", ci->name, msg);
158 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
165 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
169 case OP_EDIT: // editation has been done in-place
172 CF_JOURNAL_VAR(where->prev->next);
173 CF_JOURNAL_VAR(where->next->prev);
176 case OP_AFTER: // implementation dependent (prepend_head = after(list)), and where==list, see clists.h:74
179 CF_JOURNAL_VAR(where->next->prev);
180 CF_JOURNAL_VAR(where->next);
181 clist_insert_after(new_node, where);
183 case OP_BEFORE: // implementation dependent (append_tail = before(list))
186 CF_JOURNAL_VAR(where->prev->next);
187 CF_JOURNAL_VAR(where->prev);
188 clist_insert_before(new_node, where);
196 interpret_add_list(struct cf_item *item, int number, char **pars, int *processed, void *ptr, enum cf_operation op)
199 return cf_printf("You have to open a block for operation %s", cf_op_names[op]);
201 return "Nothing to add to the list";
202 struct cf_section *sec = item->u.sec;
207 void *node = cf_malloc(sec->size);
208 cf_init_section(item->name, sec, node, 1);
209 add_to_list(ptr, node, op);
211 /* If the node contains any dynamic attribute at the end, we suppress
212 * auto-repetition here and pass the flag inside instead. */
214 char *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
216 return sec->flags & SEC_FLAG_DYNAMIC ? msg : cf_printf("Node %d of list %s: %s", index, item->name, msg);
220 if (sec->flags & SEC_FLAG_DYNAMIC)
227 interpret_add_bitmap(struct cf_item *item, int number, char **pars, int *processed, u32 *ptr, enum cf_operation op)
229 if (op == OP_PREPEND || op == OP_APPEND)
231 if (op != OP_SET && op != OP_REMOVE)
232 return cf_printf("Cannot apply operation %s on a bitmap", cf_op_names[op]);
233 else if (item->type != CT_INT && item->type != CT_LOOKUP)
234 return cf_printf("Type %s cannot be used with bitmaps", cf_type_names[item->type]);
235 cf_journal_block(ptr, sizeof(u32));
236 for (int i=0; i<number; i++) {
238 if (item->type == CT_INT)
239 TRY( cf_parse_int(pars[i], &idx) );
241 TRY( cf_parse_lookup(pars[i], &idx, item->u.lookup) );
243 return "Bitmaps only have 32 bits";
254 interpret_set_item(struct cf_item *item, int number, char **pars, int *processed, void *ptr, uint allow_dynamic)
261 return "Missing value";
262 taken = MIN(number, item->number);
264 uint size = cf_type_size(item->type, item->u.utype);
265 cf_journal_block(ptr, taken * size);
266 return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
269 return "Dynamic array cannot be used here";
270 taken = MIN(number, ABS(item->number));
272 return interpret_set_dynamic(item, taken, pars, ptr);
274 if (item->number < 0 && !allow_dynamic)
275 return "Parsers with variable number of parameters cannot be used here";
276 if (item->number > 0 && number < item->number)
277 return "Not enough parameters available for the parser";
278 taken = MIN(number, ABS(item->number));
280 for (int i=0; i<taken; i++)
281 pars[i] = cf_strdup(pars[i]);
282 return item->u.par(taken, pars, ptr);
284 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
287 return "Lists cannot be used here";
288 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
291 return "Bitmaps cannot be used here";
292 return interpret_add_bitmap(item, number, pars, processed, ptr, OP_SET);
299 interpret_set_all(struct cf_item *item, void *ptr, enum cf_operation op)
301 if (item->cls == CC_BITMAP) {
302 cf_journal_block(ptr, sizeof(u32));
306 if (item->type == CT_INT)
310 while (item->u.lookup[++nr]);
311 * (u32*) ptr = ~0u >> (32-nr);
314 } else if (op != OP_CLEAR)
315 return "The item is not a bitmap";
317 if (item->cls == CC_LIST) {
318 cf_journal_block(ptr, sizeof(clist));
320 } else if (item->cls == CC_DYNAMIC) {
321 cf_journal_block(ptr, sizeof(void *));
322 * (void**) ptr = GARY_FOREVER_EMPTY;
323 } else if (item->cls == CC_STATIC && item->type == CT_STRING) {
324 cf_journal_block(ptr, item->number * sizeof(char*));
325 bzero(ptr, item->number * sizeof(char*));
327 return "The item is not a list, dynamic array, bitmap, or string";
332 cmp_items(void *i1, void *i2, struct cf_item *item)
334 ASSERT(item->cls == CC_STATIC);
335 i1 += (uintptr_t) item->ptr;
336 i2 += (uintptr_t) item->ptr;
337 if (item->type == CT_STRING)
338 return strcmp(* (char**) i1, * (char**) i2);
339 else // all numeric types
340 return memcmp(i1, i2, cf_type_size(item->type, item->u.utype));
344 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
346 CLIST_FOR_EACH(cnode *, n, *list)
349 for (uint i=0; i<32; i++)
351 if (cmp_items(n, query, sec->cfg+i))
363 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
365 uint nr = sec->flags & SEC_FLAG_NUMBER;
366 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
368 uint i = item - sec->cfg;
370 return "Cannot select list nodes by this attribute";
371 if (sec->cfg[i].cls != CC_STATIC)
372 return "Selection can only be done based on basic attributes";
379 opening_brace(struct cf_context *cc, struct cf_item *item, void *ptr, enum cf_operation op)
381 if (cc->stack_level >= MAX_STACK_SIZE-1)
382 return "Too many nested sections";
383 enum cf_operation pure_op = op & OP_MASK;
384 cc->stack[++cc->stack_level] = (struct item_stack) {
392 if (!item) // unknown is ignored; we just need to trace recursion
394 cc->stack[cc->stack_level].sec = item->u.sec;
395 if (item->cls == CC_SECTION)
397 if (pure_op != OP_SET)
398 return "Only SET operation can be used with a section";
399 cc->stack[cc->stack_level].base_ptr = ptr;
400 cc->stack[cc->stack_level].op = OP_EDIT | OP_2ND; // this list operation does nothing
402 else if (item->cls == CC_LIST)
404 cc->stack[cc->stack_level].base_ptr = cf_malloc(item->u.sec->size);
405 cf_init_section(item->name, item->u.sec, cc->stack[cc->stack_level].base_ptr, 1);
406 cc->stack[cc->stack_level].list = ptr;
407 cc->stack[cc->stack_level].item = item;
408 if (pure_op == OP_ALL)
409 return "Operation ALL cannot be applied on lists";
410 else if (pure_op < OP_REMOVE) {
411 add_to_list(ptr, cc->stack[cc->stack_level].base_ptr, pure_op);
412 cc->stack[cc->stack_level].op |= OP_2ND;
414 cc->stack[cc->stack_level].op |= OP_1ST;
417 return "Opening brace can only be used on sections and lists";
422 closing_brace(struct cf_context *cc, struct item_stack *st, enum cf_operation op, int number, char **pars)
424 if (st->op == OP_CLOSE) // top-level
425 return "Unmatched } parenthesis";
426 if (!st->sec) { // dummy run on unknown section
431 enum cf_operation pure_op = st->op & OP_MASK;
434 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
436 return "Cannot find a node matching the query";
437 if (pure_op != OP_REMOVE)
439 if (pure_op == OP_EDIT)
440 st->base_ptr = st->list;
441 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
442 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
443 else if (pure_op == OP_COPY) {
444 if (st->sec->flags & SEC_FLAG_CANT_COPY)
445 return cf_printf("Item %s cannot be copied", st->item->name);
446 memcpy(st->base_ptr, st->list, st->sec->size); // strings and dynamic arrays are shared
448 TRY( st->sec->copy(st->base_ptr, st->list) );
451 if (op & OP_OPEN) { // stay at the same recursion level
452 st->op = (st->op | OP_2ND) & ~OP_1ST;
453 add_to_list(st->list, st->base_ptr, pure_op);
456 int taken; // parse parameters on 1 line immediately
457 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
460 // and fall-thru to the 2nd phase
462 add_to_list(st->list, st->base_ptr, pure_op);
466 return "No parameters expected after the }";
467 else if (op & OP_OPEN)
468 return "No { is expected";
473 static struct cf_item *
474 find_item(struct cf_section *curr_sec, const char *name, char **msg, void **ptr)
476 struct cf_context *cc = cf_get_context();
478 if (name[0] == '^') // absolute name instead of relative
479 name++, curr_sec = &cc->sections, *ptr = NULL;
480 if (!curr_sec) // don't even search in an unknown section
484 if (curr_sec != &cc->sections)
485 cf_add_dirty(curr_sec, *ptr);
486 char *c = strchr(name, '.');
489 struct cf_item *ci = cf_find_subitem(curr_sec, name);
492 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
493 *msg = cf_printf("Unknown item %s", name);
496 *ptr += (uintptr_t) ci->ptr;
499 if (ci->cls != CC_SECTION)
501 *msg = cf_printf("Item %s is not a section", name);
504 curr_sec = ci->u.sec;
510 interpret_add(char *name, struct cf_item *item, int number, char **pars, int *takenp, void *ptr, enum cf_operation op)
514 return interpret_add_dynamic(item, number, pars, takenp, ptr, op);
516 return interpret_add_list(item, number, pars, takenp, ptr, op);
518 return interpret_add_bitmap(item, number, pars, takenp, ptr, op);
520 return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
525 cf_interpret_line(struct cf_context *cc, char *name, enum cf_operation op, int number, char **pars)
528 if ((op & OP_MASK) == OP_CLOSE)
529 return closing_brace(cc, cc->stack+cc->stack_level, op, number, pars);
530 void *ptr = cc->stack[cc->stack_level].base_ptr;
531 struct cf_item *item = find_item(cc->stack[cc->stack_level].sec, name, &msg, &ptr);
534 if (cc->stack[cc->stack_level].op & OP_1ST)
535 TRY( record_selector(item, cc->stack[cc->stack_level].sec, &cc->stack[cc->stack_level].mask) );
536 if (op & OP_OPEN) { // the operation will be performed after the closing brace
538 return "Cannot open a block after a parameter has been passed on a line";
539 return opening_brace(cc, item, ptr, op);
541 if (!item) // ignored item in an unknown section
545 int taken = 0; // process as many parameters as possible
549 msg = interpret_set_all(item, ptr, op);
552 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
555 msg = interpret_set_all(item, ptr, OP_CLEAR);
557 msg = interpret_add(name, item, number, pars, &taken, ptr, OP_APPEND);
560 msg = interpret_add(name, item, number, pars, &taken, ptr, op);
565 return cf_printf("Too many parameters: %d>%d", number, taken);
571 cf_find_item(const char *name, struct cf_item *item)
573 struct cf_context *cc = cf_get_context();
576 struct cf_item *ci = find_item(&cc->sections, name, &msg, &ptr);
583 bzero(item, sizeof(struct cf_item));
588 cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars)
594 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
598 msg = interpret_set_all(item, item->ptr, op);
604 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
607 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
610 msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
613 return "The attribute does not support append/prepend";
617 if (item->cls == CC_BITMAP)
618 msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
620 return "Only applicable on bitmaps";
623 return "Unsupported operation";
628 return "Too many parameters";
633 cf_init_stack(struct cf_context *cc)
635 if (!cc->sections_initialized++) {
636 cc->sections.flags |= SEC_FLAG_UNKNOWN;
637 cc->sections.size = 0; // size of allocated array used to be stored here
638 cf_init_section(NULL, &cc->sections, NULL, 0);
641 cc->stack[0] = (struct item_stack) {
642 .sec = &cc->sections,
652 cf_done_stack(struct cf_context *cc)
654 return (cc->stack_level > 0);