2 * UCW Library -- Configuration files: interpreter
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.
13 #include "lib/getopt.h"
14 #include "lib/conf-internal.h"
15 #include "lib/clists.h"
20 #define TRY(f) do { char *_msg = f; if (_msg) return _msg; } while (0)
22 /* Register size of and parser for each basic type */
25 cf_parse_string(char *str, char **ptr)
27 *ptr = cf_strdup(str);
31 typedef char *cf_basic_parser(char *str, void *ptr);
36 { sizeof(int), cf_parse_int },
37 { sizeof(u64), cf_parse_u64 },
38 { sizeof(double), cf_parse_double },
39 { sizeof(u32), cf_parse_ip },
40 { sizeof(char*), cf_parse_string },
41 { sizeof(int), NULL }, // lookups are parsed extra
42 { 0, NULL }, // user-defined types are parsed extra
46 cf_type_size(enum cf_type type, struct cf_user_type *utype)
49 return parsers[type].size;
55 cf_parse_lookup(char *str, int *ptr, char **t)
59 while (*n && strcasecmp(*n, str)) {
60 total_len += strlen(*n) + 2;
67 char *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
68 c += sprintf(err, "Invalid value %s, possible values are: ", str);
70 c+= sprintf(c, "%s, ", *n);
78 cf_parse_ary(uns number, char **pars, void *ptr, enum cf_type type, union cf_union *u)
80 for (uns i=0; i<number; i++)
83 uns size = cf_type_size(type, u->utype);
85 msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * size);
86 else if (type == CT_LOOKUP)
87 msg = cf_parse_lookup(pars[i], ptr + i * size, u->lookup);
88 else if (type == CT_USER)
89 msg = u->utype->parser(pars[i], ptr + i * size);
93 return number > 1 ? cf_printf("Item %d: %s", i+1, msg) : msg;
101 char *cf_op_names[] = { CF_OPERATIONS };
103 char *cf_type_names[] = { "int", "u64", "double", "ip", "string", "lookup", "user" };
105 #define DARY_HDR_SIZE ALIGN_TO(sizeof(uns), CPU_STRUCT_ALIGN)
108 interpret_set_dynamic(struct cf_item *item, int number, char **pars, void **ptr)
110 enum cf_type type = item->type;
111 cf_journal_block(ptr, sizeof(void*));
112 // boundary checks done by the caller
113 uns size = cf_type_size(item->type, item->u.utype);
114 *ptr = cf_malloc(DARY_HDR_SIZE + number * size) + DARY_HDR_SIZE;
115 DARY_LEN(*ptr) = number;
116 return cf_parse_ary(number, pars, *ptr, type, &item->u);
120 interpret_add_dynamic(struct cf_item *item, int number, char **pars, int *processed, void **ptr, enum cf_operation op)
122 enum cf_type type = item->type;
124 uns size = cf_type_size(item->type, item->u.utype);
125 ASSERT(size >= sizeof(uns));
126 int old_nr = old_p ? DARY_LEN(old_p) : 0;
127 int taken = MIN(number, ABS(item->number)-old_nr);
129 // stretch the dynamic array
130 void *new_p = cf_malloc(DARY_HDR_SIZE + (old_nr + taken) * size) + DARY_HDR_SIZE;
131 DARY_LEN(new_p) = old_nr + taken;
132 cf_journal_block(ptr, sizeof(void*));
134 if (op == OP_APPEND) {
135 memcpy(new_p, old_p, old_nr * size);
136 return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
137 } else if (op == OP_PREPEND) {
138 memcpy(new_p + taken * size, old_p, old_nr * size);
139 return cf_parse_ary(taken, pars, new_p, type, &item->u);
141 return cf_printf("Dynamic arrays do not support operation %s", cf_op_names[op]);
144 static char *interpret_set_item(struct cf_item *item, int number, char **pars, int *processed, void *ptr, uns allow_dynamic);
147 interpret_section(struct cf_section *sec, int number, char **pars, int *processed, void *ptr, uns allow_dynamic)
149 cf_add_dirty(sec, ptr);
151 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
154 char *msg = interpret_set_item(ci, number, pars, &taken, ptr + (uintptr_t) ci->ptr, allow_dynamic && !ci[1].cls);
156 return cf_printf("Item %s: %s", ci->name, msg);
160 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
167 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
171 case OP_EDIT: // edition has been done in-place
174 CF_JOURNAL_VAR(where->prev->next);
175 CF_JOURNAL_VAR(where->next->prev);
178 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
181 CF_JOURNAL_VAR(where->next->prev);
182 CF_JOURNAL_VAR(where->next);
183 clist_insert_after(new_node, where);
185 case OP_BEFORE: // implementation dependend (append_tail = before(list))
188 CF_JOURNAL_VAR(where->prev->next);
189 CF_JOURNAL_VAR(where->prev);
190 clist_insert_before(new_node, where);
198 interpret_add_list(struct cf_item *item, int number, char **pars, int *processed, void *ptr, enum cf_operation op)
201 return cf_printf("You have to open a block for operation %s", cf_op_names[op]);
203 return "Nothing to add to the list";
204 struct cf_section *sec = item->u.sec;
209 void *node = cf_malloc(sec->size);
210 cf_init_section(item->name, sec, node, 1);
211 add_to_list(ptr, node, op);
213 /* If the node contains any dynamic attribute at the end, we suppress
214 * auto-repetition here and pass the flag inside instead. */
216 char *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
218 return sec->flags & SEC_FLAG_DYNAMIC ? msg : cf_printf("Node %d of list %s: %s", index, item->name, msg);
222 if (sec->flags & SEC_FLAG_DYNAMIC)
229 interpret_add_bitmap(struct cf_item *item, int number, char **pars, int *processed, u32 *ptr, enum cf_operation op)
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, uns allow_dynamic)
261 return "Missing value";
262 taken = MIN(number, item->number);
264 uns 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 *));
323 * (void**) ptr = (&zero) + 1;
324 } else if (item->cls == CC_STATIC && item->type == CT_STRING) {
325 cf_journal_block(ptr, item->number * sizeof(byte*));
326 bzero(ptr, item->number * sizeof(byte*));
328 return "The item is not a list, dynamic array, bitmap, or string";
333 cmp_items(void *i1, void *i2, struct cf_item *item)
335 ASSERT(item->cls == CC_STATIC);
336 i1 += (uintptr_t) item->ptr;
337 i2 += (uintptr_t) item->ptr;
338 if (item->type == CT_STRING)
339 return strcmp(* (byte**) i1, * (byte**) i2);
340 else // all numeric types
341 return memcmp(i1, i2, cf_type_size(item->type, item->u.utype));
345 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
347 CLIST_FOR_EACH(cnode *, n, *list)
350 for (uns i=0; i<32; i++)
352 if (cmp_items(n, query, sec->cfg+i))
364 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
366 uns nr = sec->flags & SEC_FLAG_NUMBER;
367 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
369 uns i = item - sec->cfg;
371 return "Cannot select list nodes by this attribute";
372 if (sec->cfg[i].cls != CC_STATIC)
373 return "Selection can only be done based on basic attributes";
379 #define MAX_STACK_SIZE 10
380 static struct item_stack {
381 struct cf_section *sec; // nested section
382 void *base_ptr; // because original pointers are often relative
383 enum cf_operation op; // it is performed when a closing brace is encountered
384 void *list; // list the operations should be done on
385 u32 mask; // bit array of selectors searching in a list
386 struct cf_item *item; // cf_item of the list
387 } stack[MAX_STACK_SIZE];
391 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
393 if (level >= MAX_STACK_SIZE-1)
394 return "Too many nested sections";
395 enum cf_operation pure_op = op & OP_MASK;
396 stack[++level] = (struct item_stack) {
404 if (!item) // unknown is ignored; we just need to trace recursion
406 stack[level].sec = item->u.sec;
407 if (item->cls == CC_SECTION)
409 if (pure_op != OP_SET)
410 return "Only SET operation can be used with a section";
411 stack[level].base_ptr = ptr;
412 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
414 else if (item->cls == CC_LIST)
416 stack[level].base_ptr = cf_malloc(item->u.sec->size);
417 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
418 stack[level].list = ptr;
419 stack[level].item = item;
420 if (pure_op == OP_ALL)
421 return "Operation ALL cannot be applied on lists";
422 else if (pure_op < OP_REMOVE) {
423 add_to_list(ptr, stack[level].base_ptr, pure_op);
424 stack[level].op |= OP_2ND;
426 stack[level].op |= OP_1ST;
429 return "Opening brace can only be used on sections and lists";
434 closing_brace(struct item_stack *st, enum cf_operation op, int number, char **pars)
436 if (st->op == OP_CLOSE) // top-level
437 return "Unmatched } parenthesis";
438 if (!st->sec) { // dummy run on unknown section
443 enum cf_operation pure_op = st->op & OP_MASK;
446 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
448 return "Cannot find a node matching the query";
449 if (pure_op != OP_REMOVE)
451 if (pure_op == OP_EDIT)
452 st->base_ptr = st->list;
453 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
454 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
455 else if (pure_op == OP_COPY) {
456 if (st->sec->flags & SEC_FLAG_CANT_COPY)
457 return cf_printf("Item %s cannot be copied", st->item->name);
458 memcpy(st->base_ptr, st->list, st->sec->size); // strings and dynamic arrays are shared
460 TRY( st->sec->copy(st->base_ptr, st->list) );
463 if (op & OP_OPEN) { // stay at the same recursion level
464 st->op = (st->op | OP_2ND) & ~OP_1ST;
465 add_to_list(st->list, st->base_ptr, pure_op);
468 int taken; // parse parameters on 1 line immediately
469 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
472 // and fall-thru to the 2nd phase
474 add_to_list(st->list, st->base_ptr, pure_op);
478 return "No parameters expected after the }";
479 else if (op & OP_OPEN)
480 return "No { is expected";
485 static struct cf_item *
486 find_item(struct cf_section *curr_sec, const char *name, char **msg, void **ptr)
489 if (name[0] == '^') // absolute name instead of relative
490 name++, curr_sec = &cf_sections, *ptr = NULL;
491 if (!curr_sec) // don't even search in an unknown section
495 if (curr_sec != &cf_sections)
496 cf_add_dirty(curr_sec, *ptr);
497 char *c = strchr(name, '.');
500 struct cf_item *ci = cf_find_subitem(curr_sec, name);
503 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
504 *msg = cf_printf("Unknown item %s", name);
507 *ptr += (uintptr_t) ci->ptr;
510 if (ci->cls != CC_SECTION)
512 *msg = cf_printf("Item %s is not a section", name);
515 curr_sec = ci->u.sec;
521 cf_interpret_line(char *name, enum cf_operation op, int number, char **pars)
524 if ((op & OP_MASK) == OP_CLOSE)
525 return closing_brace(stack+level, op, number, pars);
526 void *ptr = stack[level].base_ptr;
527 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
530 if (stack[level].op & OP_1ST)
531 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
532 if (op & OP_OPEN) { // the operation will be performed after the closing brace
534 return "Cannot open a block after a parameter has been passed on a line";
535 return opening_brace(item, ptr, op);
537 if (!item) // ignored item in an unknown section
541 int taken = 0; // process as many parameters as possible
542 if (op == OP_CLEAR || op == OP_ALL)
543 msg = interpret_set_all(item, ptr, op);
544 else if (op == OP_SET)
545 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
546 else if (item->cls == CC_DYNAMIC)
547 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
548 else if (item->cls == CC_LIST)
549 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
550 else if (item->cls == CC_BITMAP)
551 msg = interpret_add_bitmap(item, number, pars, &taken, ptr, op);
553 return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
557 return cf_printf("Too many parameters: %d>%d", number, taken);
563 cf_find_item(const char *name, struct cf_item *item)
567 struct cf_item *ci = find_item(&cf_sections, name, &msg, &ptr);
574 bzero(item, sizeof(struct cf_item));
579 cf_write_item(struct cf_item *item, enum cf_operation op, int number, char **pars)
585 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
589 msg = interpret_set_all(item, item->ptr, op);
593 if (item->cls == CC_DYNAMIC)
594 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
595 else if (item->cls == CC_LIST)
596 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
598 return "The attribute does not support append/prepend";
601 if (item->cls == CC_BITMAP)
602 msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
604 return "Only applicable on bitmaps";
607 return "Unsupported operation";
612 return "Too many parameters";
619 static uns initialized = 0;
620 if (!initialized++) {
621 cf_sections.flags |= SEC_FLAG_UNKNOWN;
622 cf_sections.size = 0; // size of allocated array used to be stored here
623 cf_init_section(NULL, &cf_sections, NULL, 0);
626 stack[0] = (struct item_stack) {
640 msg(L_ERROR, "Unterminated block");