2 * UCW Library -- Configuration files: interpreter
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2012 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>
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, const char * const *t)
57 const char * const *n = 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: // editation has been done in-place
174 CF_JOURNAL_VAR(where->prev->next);
175 CF_JOURNAL_VAR(where->next->prev);
178 case OP_AFTER: // implementation dependent (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 dependent (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_PREPEND || op == OP_APPEND)
233 if (op != OP_SET && op != OP_REMOVE)
234 return cf_printf("Cannot apply operation %s on a bitmap", cf_op_names[op]);
235 else if (item->type != CT_INT && item->type != CT_LOOKUP)
236 return cf_printf("Type %s cannot be used with bitmaps", cf_type_names[item->type]);
237 cf_journal_block(ptr, sizeof(u32));
238 for (int i=0; i<number; i++) {
240 if (item->type == CT_INT)
241 TRY( cf_parse_int(pars[i], &idx) );
243 TRY( cf_parse_lookup(pars[i], &idx, item->u.lookup) );
245 return "Bitmaps only have 32 bits";
256 interpret_set_item(struct cf_item *item, int number, char **pars, int *processed, void *ptr, uns allow_dynamic)
263 return "Missing value";
264 taken = MIN(number, item->number);
266 uns size = cf_type_size(item->type, item->u.utype);
267 cf_journal_block(ptr, taken * size);
268 return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
271 return "Dynamic array cannot be used here";
272 taken = MIN(number, ABS(item->number));
274 return interpret_set_dynamic(item, taken, pars, ptr);
276 if (item->number < 0 && !allow_dynamic)
277 return "Parsers with variable number of parameters cannot be used here";
278 if (item->number > 0 && number < item->number)
279 return "Not enough parameters available for the parser";
280 taken = MIN(number, ABS(item->number));
282 for (int i=0; i<taken; i++)
283 pars[i] = cf_strdup(pars[i]);
284 return item->u.par(taken, pars, ptr);
286 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
289 return "Lists cannot be used here";
290 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
293 return "Bitmaps cannot be used here";
294 return interpret_add_bitmap(item, number, pars, processed, ptr, OP_SET);
301 interpret_set_all(struct cf_item *item, void *ptr, enum cf_operation op)
303 if (item->cls == CC_BITMAP) {
304 cf_journal_block(ptr, sizeof(u32));
308 if (item->type == CT_INT)
312 while (item->u.lookup[++nr]);
313 * (u32*) ptr = ~0u >> (32-nr);
316 } else if (op != OP_CLEAR)
317 return "The item is not a bitmap";
319 if (item->cls == CC_LIST) {
320 cf_journal_block(ptr, sizeof(clist));
322 } else if (item->cls == CC_DYNAMIC) {
323 cf_journal_block(ptr, sizeof(void *));
325 * (void**) ptr = (&zero) + 1;
326 } else if (item->cls == CC_STATIC && item->type == CT_STRING) {
327 cf_journal_block(ptr, item->number * sizeof(char*));
328 bzero(ptr, item->number * sizeof(char*));
330 return "The item is not a list, dynamic array, bitmap, or string";
335 cmp_items(void *i1, void *i2, struct cf_item *item)
337 ASSERT(item->cls == CC_STATIC);
338 i1 += (uintptr_t) item->ptr;
339 i2 += (uintptr_t) item->ptr;
340 if (item->type == CT_STRING)
341 return strcmp(* (char**) i1, * (char**) i2);
342 else // all numeric types
343 return memcmp(i1, i2, cf_type_size(item->type, item->u.utype));
347 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
349 CLIST_FOR_EACH(cnode *, n, *list)
352 for (uns i=0; i<32; i++)
354 if (cmp_items(n, query, sec->cfg+i))
366 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
368 uns nr = sec->flags & SEC_FLAG_NUMBER;
369 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
371 uns i = item - sec->cfg;
373 return "Cannot select list nodes by this attribute";
374 if (sec->cfg[i].cls != CC_STATIC)
375 return "Selection can only be done based on basic attributes";
382 opening_brace(struct cf_context *cc, struct cf_item *item, void *ptr, enum cf_operation op)
384 if (cc->stack_level >= MAX_STACK_SIZE-1)
385 return "Too many nested sections";
386 enum cf_operation pure_op = op & OP_MASK;
387 cc->stack[++cc->stack_level] = (struct item_stack) {
395 if (!item) // unknown is ignored; we just need to trace recursion
397 cc->stack[cc->stack_level].sec = item->u.sec;
398 if (item->cls == CC_SECTION)
400 if (pure_op != OP_SET)
401 return "Only SET operation can be used with a section";
402 cc->stack[cc->stack_level].base_ptr = ptr;
403 cc->stack[cc->stack_level].op = OP_EDIT | OP_2ND; // this list operation does nothing
405 else if (item->cls == CC_LIST)
407 cc->stack[cc->stack_level].base_ptr = cf_malloc(item->u.sec->size);
408 cf_init_section(item->name, item->u.sec, cc->stack[cc->stack_level].base_ptr, 1);
409 cc->stack[cc->stack_level].list = ptr;
410 cc->stack[cc->stack_level].item = item;
411 if (pure_op == OP_ALL)
412 return "Operation ALL cannot be applied on lists";
413 else if (pure_op < OP_REMOVE) {
414 add_to_list(ptr, cc->stack[cc->stack_level].base_ptr, pure_op);
415 cc->stack[cc->stack_level].op |= OP_2ND;
417 cc->stack[cc->stack_level].op |= OP_1ST;
420 return "Opening brace can only be used on sections and lists";
425 closing_brace(struct cf_context *cc, struct item_stack *st, enum cf_operation op, int number, char **pars)
427 if (st->op == OP_CLOSE) // top-level
428 return "Unmatched } parenthesis";
429 if (!st->sec) { // dummy run on unknown section
434 enum cf_operation pure_op = st->op & OP_MASK;
437 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
439 return "Cannot find a node matching the query";
440 if (pure_op != OP_REMOVE)
442 if (pure_op == OP_EDIT)
443 st->base_ptr = st->list;
444 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
445 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
446 else if (pure_op == OP_COPY) {
447 if (st->sec->flags & SEC_FLAG_CANT_COPY)
448 return cf_printf("Item %s cannot be copied", st->item->name);
449 memcpy(st->base_ptr, st->list, st->sec->size); // strings and dynamic arrays are shared
451 TRY( st->sec->copy(st->base_ptr, st->list) );
454 if (op & OP_OPEN) { // stay at the same recursion level
455 st->op = (st->op | OP_2ND) & ~OP_1ST;
456 add_to_list(st->list, st->base_ptr, pure_op);
459 int taken; // parse parameters on 1 line immediately
460 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
463 // and fall-thru to the 2nd phase
465 add_to_list(st->list, st->base_ptr, pure_op);
469 return "No parameters expected after the }";
470 else if (op & OP_OPEN)
471 return "No { is expected";
476 static struct cf_item *
477 find_item(struct cf_section *curr_sec, const char *name, char **msg, void **ptr)
479 struct cf_context *cc = cf_get_context();
481 if (name[0] == '^') // absolute name instead of relative
482 name++, curr_sec = &cc->sections, *ptr = NULL;
483 if (!curr_sec) // don't even search in an unknown section
487 if (curr_sec != &cc->sections)
488 cf_add_dirty(curr_sec, *ptr);
489 char *c = strchr(name, '.');
492 struct cf_item *ci = cf_find_subitem(curr_sec, name);
495 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
496 *msg = cf_printf("Unknown item %s", name);
499 *ptr += (uintptr_t) ci->ptr;
502 if (ci->cls != CC_SECTION)
504 *msg = cf_printf("Item %s is not a section", name);
507 curr_sec = ci->u.sec;
513 interpret_add(char *name, struct cf_item *item, int number, char **pars, int *takenp, void *ptr, enum cf_operation op)
517 return interpret_add_dynamic(item, number, pars, takenp, ptr, op);
519 return interpret_add_list(item, number, pars, takenp, ptr, op);
521 return interpret_add_bitmap(item, number, pars, takenp, ptr, op);
523 return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
528 cf_interpret_line(struct cf_context *cc, char *name, enum cf_operation op, int number, char **pars)
531 if ((op & OP_MASK) == OP_CLOSE)
532 return closing_brace(cc, cc->stack+cc->stack_level, op, number, pars);
533 void *ptr = cc->stack[cc->stack_level].base_ptr;
534 struct cf_item *item = find_item(cc->stack[cc->stack_level].sec, name, &msg, &ptr);
537 if (cc->stack[cc->stack_level].op & OP_1ST)
538 TRY( record_selector(item, cc->stack[cc->stack_level].sec, &cc->stack[cc->stack_level].mask) );
539 if (op & OP_OPEN) { // the operation will be performed after the closing brace
541 return "Cannot open a block after a parameter has been passed on a line";
542 return opening_brace(cc, item, ptr, op);
544 if (!item) // ignored item in an unknown section
548 int taken = 0; // process as many parameters as possible
552 msg = interpret_set_all(item, ptr, op);
555 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
558 msg = interpret_set_all(item, ptr, OP_CLEAR);
560 msg = interpret_add(name, item, number, pars, &taken, ptr, OP_APPEND);
563 msg = interpret_add(name, item, number, pars, &taken, ptr, op);
568 return cf_printf("Too many parameters: %d>%d", number, taken);
574 cf_find_item(const char *name, struct cf_item *item)
576 struct cf_context *cc = cf_get_context();
579 struct cf_item *ci = find_item(&cc->sections, name, &msg, &ptr);
586 bzero(item, sizeof(struct cf_item));
591 cf_modify_item(struct cf_item *item, enum cf_operation op, int number, char **pars)
597 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
601 msg = interpret_set_all(item, item->ptr, op);
607 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
610 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
613 msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
616 return "The attribute does not support append/prepend";
620 if (item->cls == CC_BITMAP)
621 msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
623 return "Only applicable on bitmaps";
626 return "Unsupported operation";
631 return "Too many parameters";
636 cf_init_stack(struct cf_context *cc)
638 if (!cc->sections_initialized++) {
639 cc->sections.flags |= SEC_FLAG_UNKNOWN;
640 cc->sections.size = 0; // size of allocated array used to be stored here
641 cf_init_section(NULL, &cc->sections, NULL, 0);
644 cc->stack[0] = (struct item_stack) {
645 .sec = &cc->sections,
655 cf_done_stack(struct cf_context *cc)
657 return (cc->stack_level > 0);