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 { byte *_msg = f; if (_msg) return _msg; } while (0)
22 /* Register size of and parser for each basic type */
25 cf_parse_string(byte *str, byte **ptr)
27 *ptr = cf_strdup(str);
31 typedef byte *cf_basic_parser(byte *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(byte*), 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(byte *str, int *ptr, byte **t)
59 while (*n && strcasecmp(*n, str)) {
60 total_len += strlen(*n) + 2;
67 byte *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, byte **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 byte *cf_op_names[] = { CF_OPERATIONS };
104 #define DARY_HDR_SIZE ALIGN(sizeof(uns), CPU_STRUCT_ALIGN)
107 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
109 enum cf_type type = item->type;
110 cf_journal_block(ptr, sizeof(void*));
111 // boundary checks done by the caller
112 uns size = cf_type_size(item->type, item->u.utype);
113 *ptr = cf_malloc(DARY_HDR_SIZE + number * size) + DARY_HDR_SIZE;
114 DARY_LEN(*ptr) = number;
115 return cf_parse_ary(number, pars, *ptr, type, &item->u);
119 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
121 enum cf_type type = item->type;
123 uns size = cf_type_size(item->type, item->u.utype);
124 ASSERT(size >= sizeof(uns));
125 int old_nr = old_p ? DARY_LEN(old_p) : 0;
126 int taken = MIN(number, ABS(item->number)-old_nr);
128 // stretch the dynamic array
129 void *new_p = cf_malloc(DARY_HDR_SIZE + (old_nr + taken) * size) + DARY_HDR_SIZE;
130 DARY_LEN(new_p) = old_nr + taken;
131 cf_journal_block(ptr, sizeof(void*));
133 if (op == OP_APPEND) {
134 memcpy(new_p, old_p, old_nr * size);
135 return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
136 } else if (op == OP_PREPEND) {
137 memcpy(new_p + taken * size, old_p, old_nr * size);
138 return cf_parse_ary(taken, pars, new_p, type, &item->u);
140 return cf_printf("Dynamic arrays do not support operation %s", cf_op_names[op]);
143 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
146 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
148 cf_add_dirty(sec, ptr);
150 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
153 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
155 return cf_printf("Item %s: %s", ci->name, msg);
159 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
166 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
170 case OP_EDIT: // edition has been done in-place
173 CF_JOURNAL_VAR(where->prev->next);
174 CF_JOURNAL_VAR(where->next->prev);
177 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
180 CF_JOURNAL_VAR(where->next->prev);
181 CF_JOURNAL_VAR(where->next);
182 clist_insert_after(new_node, where);
184 case OP_BEFORE: // implementation dependend (append_tail = before(list))
187 CF_JOURNAL_VAR(where->prev->next);
188 CF_JOURNAL_VAR(where->prev);
189 clist_insert_before(new_node, where);
197 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
200 return cf_printf("You have to open a block for operation %s", cf_op_names[op]);
202 return "Nothing to add to the list";
203 struct cf_section *sec = item->u.sec;
208 void *node = cf_malloc(sec->size);
209 cf_init_section(item->name, sec, node, 1);
210 add_to_list(ptr, node, op);
212 /* If the node contains any dynamic attribute at the end, we suppress
213 * auto-repetition here and pass the flag inside instead. */
215 byte *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
217 return sec->flags & SEC_FLAG_DYNAMIC ? msg : cf_printf("Node %d of list %s: %s", index, item->name, msg);
221 if (sec->flags & SEC_FLAG_DYNAMIC)
228 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
235 return "Missing value";
236 taken = MIN(number, item->number);
238 uns size = cf_type_size(item->type, item->u.utype);
239 cf_journal_block(ptr, taken * size);
240 return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
243 return "Dynamic array cannot be used here";
244 taken = MIN(number, ABS(item->number));
246 return interpret_set_dynamic(item, taken, pars, ptr);
248 if (item->number < 0 && !allow_dynamic)
249 return "Parsers with variable number of parameters cannot be used here";
250 if (item->number > 0 && number < item->number)
251 return "Not enough parameters available for the parser";
252 taken = MIN(number, ABS(item->number));
254 for (int i=0; i<taken; i++)
255 pars[i] = cf_strdup(pars[i]);
256 return item->u.par(taken, pars, ptr);
258 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
261 return "Lists cannot be used here";
262 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
269 interpret_clear(struct cf_item *item, void *ptr)
271 if (item->cls == CC_LIST) {
272 cf_journal_block(ptr, sizeof(clist));
274 } else if (item->cls == CC_DYNAMIC) {
275 cf_journal_block(ptr, sizeof(void *));
277 * (void**) ptr = (&zero) + 1;
278 } else if (item->cls == CC_STATIC && item->type == CT_STRING) {
279 cf_journal_block(ptr, item->number * sizeof(byte*));
280 bzero(ptr, item->number * sizeof(byte*));
282 return "The item is not a list, dynamic array, or string";
287 cmp_items(void *i1, void *i2, struct cf_item *item)
289 ASSERT(item->cls == CC_STATIC);
290 i1 += (addr_int_t) item->ptr;
291 i2 += (addr_int_t) item->ptr;
292 if (item->type == CT_STRING)
293 return strcmp(* (byte**) i1, * (byte**) i2);
294 else // all numeric types
295 return memcmp(i1, i2, cf_type_size(item->type, item->u.utype));
299 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
301 CLIST_FOR_EACH(cnode *, n, *list)
304 for (uns i=0; i<32; i++)
306 if (cmp_items(n, query, sec->cfg+i))
318 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
320 uns nr = sec->flags & SEC_FLAG_NUMBER;
321 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
323 uns i = item - sec->cfg;
325 return "Cannot select list nodes by this attribute";
326 if (sec->cfg[i].cls != CC_STATIC)
327 return "Selection can only be done based on basic attributes";
333 #define MAX_STACK_SIZE 10
334 static struct item_stack {
335 struct cf_section *sec; // nested section
336 void *base_ptr; // because original pointers are often relative
337 enum cf_operation op; // it is performed when a closing brace is encountered
338 void *list; // list the operations should be done on
339 u32 mask; // bit array of selectors searching in a list
340 struct cf_item *item; // cf_item of the list
341 } stack[MAX_STACK_SIZE];
345 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
347 if (level >= MAX_STACK_SIZE-1)
348 return "Too many nested sections";
349 stack[++level] = (struct item_stack) {
357 if (!item) // unknown is ignored; we just need to trace recursion
359 stack[level].sec = item->u.sec;
360 if (item->cls == CC_SECTION)
362 stack[level].base_ptr = ptr;
363 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
365 else if (item->cls == CC_LIST)
367 stack[level].base_ptr = cf_malloc(item->u.sec->size);
368 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
369 stack[level].list = ptr;
370 stack[level].item = item;
371 if ((op & OP_MASK) < OP_REMOVE) {
372 add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
373 stack[level].op |= OP_2ND;
375 stack[level].op |= OP_1ST;
378 return "Opening brace can only be used on sections and lists";
383 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
385 if (st->op == OP_CLOSE) // top-level
386 return "Unmatched } parenthesis";
387 if (!st->sec) { // dummy run on unknown section
392 enum cf_operation pure_op = st->op & OP_MASK;
395 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
397 return "Cannot find a node matching the query";
398 if (pure_op != OP_REMOVE)
400 if (pure_op == OP_EDIT)
401 st->base_ptr = st->list;
402 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
403 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
404 else if (pure_op == OP_COPY) {
405 if (st->sec->flags & SEC_FLAG_CANT_COPY)
406 return cf_printf("Item %s cannot be copied", st->item->name);
407 memcpy(st->base_ptr, st->list, st->sec->size); // strings and dynamic arrays are shared
409 TRY( st->sec->copy(st->base_ptr, st->list) );
412 if (op & OP_OPEN) { // stay at the same recursion level
413 st->op = (st->op | OP_2ND) & ~OP_1ST;
414 add_to_list(st->list, st->base_ptr, pure_op);
417 int taken; // parse parameters on 1 line immediately
418 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
421 // and fall-thru to the 2nd phase
423 add_to_list(st->list, st->base_ptr, pure_op);
427 return "No parameters expected after the }";
428 else if (op & OP_OPEN)
429 return "No { is expected";
434 static struct cf_item *
435 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
438 if (name[0] == '^') // absolute name instead of relative
439 name++, curr_sec = &cf_sections, *ptr = NULL;
440 if (!curr_sec) // don't even search in an unknown section
444 if (curr_sec != &cf_sections)
445 cf_add_dirty(curr_sec, *ptr);
446 byte *c = strchr(name, '.');
449 struct cf_item *ci = cf_find_subitem(curr_sec, name);
452 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
453 *msg = cf_printf("Unknown item %s", name);
456 *ptr += (addr_int_t) ci->ptr;
459 if (ci->cls != CC_SECTION)
461 *msg = cf_printf("Item %s is not a section", name);
464 curr_sec = ci->u.sec;
470 cf_interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
473 if ((op & OP_MASK) == OP_CLOSE)
474 return closing_brace(stack+level, op, number, pars);
475 void *ptr = stack[level].base_ptr;
476 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
479 if (stack[level].op & OP_1ST)
480 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
481 if (op & OP_OPEN) { // the operation will be performed after the closing brace
483 return "Cannot open a block after a parameter has been passed on a line";
484 return opening_brace(item, ptr, op);
486 if (!item) // ignored item in an unknown section
490 int taken; // process as many parameters as possible
492 taken = 0, msg = interpret_clear(item, ptr);
493 else if (op == OP_SET)
494 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
495 else if (item->cls == CC_DYNAMIC)
496 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
497 else if (item->cls == CC_LIST)
498 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
500 return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
504 return cf_printf("Too many parameters: %d>%d", number, taken);
510 cf_find_item(byte *name, struct cf_item *item)
514 struct cf_item *ci = find_item(&cf_sections, name, &msg, &ptr);
521 bzero(item, sizeof(struct cf_item));
526 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
532 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
536 msg = interpret_clear(item, item->ptr);
540 if (item->cls == CC_DYNAMIC)
541 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
542 else if (item->cls == CC_LIST)
543 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
545 return "The attribute class does not support append/prepend";
548 return "Unsupported operation";
553 return "Too many parameters";
560 static uns initialized = 0;
561 if (!initialized++) {
562 cf_sections.flags |= SEC_FLAG_UNKNOWN;
563 cf_sections.size = 0; // size of allocated array used to be stored here
564 cf_init_section(NULL, &cf_sections, NULL, 0);
567 stack[0] = (struct item_stack) {
581 log(L_ERROR, "Unterminated block");