]> mj.ucw.cz Git - libucw.git/blob - lib/conf-intr.c
bugfixes
[libucw.git] / lib / conf-intr.c
1 /*
2  *      UCW Library -- Configuration files: interpreter
3  *
4  *      (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5  *      (c) 2003--2006 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/conf.h"
13 #include "lib/getopt.h"
14 #include "lib/conf-internal.h"
15 #include "lib/clists.h"
16
17 #include <string.h>
18 #include <stdio.h>
19
20 #define TRY(f)  do { byte *_msg = f; if (_msg) return _msg; } while (0)
21
22 /* Register size of and parser for each basic type */
23
24 static byte *
25 cf_parse_string(byte *str, byte **ptr)
26 {
27   *ptr = cf_strdup(str);
28   return NULL;
29 }
30
31 typedef byte *cf_basic_parser(byte *str, void *ptr);
32 static struct {
33   uns size;
34   void *parser;
35 } parsers[] = {
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
43 };
44
45 inline uns
46 cf_type_size(enum cf_type type, struct cf_user_type *utype)
47 {
48   if (type < CT_USER)
49     return parsers[type].size;
50   else
51     return utype->size;
52 }
53
54 static byte *
55 cf_parse_lookup(byte *str, int *ptr, byte **t)
56 {
57   byte **n = t;
58   uns total_len = 0;
59   while (*n && strcasecmp(*n, str)) {
60     total_len += strlen(*n) + 2;
61     n++;
62   }
63   if (*n) {
64     *ptr = n - t;
65     return NULL;
66   }
67   byte *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
68   c += sprintf(err, "Invalid value %s, possible values are: ", str);
69   for (n=t; *n; n++)
70     c+= sprintf(c, "%s, ", *n);
71   if (*t)
72     c[-2] = 0;
73   *ptr = -1;
74   return err;
75 }
76
77 static byte *
78 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_union *u)
79 {
80   for (uns i=0; i<number; i++)
81   {
82     byte *msg;
83     uns size = cf_type_size(type, u->utype);
84     if (type < CT_LOOKUP)
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);
90     else
91       ASSERT(0);
92     if (msg)
93       return number > 1 ? cf_printf("Item %d: %s", i+1, msg) : msg;
94   }
95   return NULL;
96 }
97
98 /* Interpreter */
99
100 #define T(x) #x,
101 byte *cf_op_names[] = { CF_OPERATIONS };
102 #undef T
103 byte *cf_type_names[] = { "int", "u64", "double", "ip", "string", "lookup", "user" };
104
105 #define DARY_HDR_SIZE ALIGN(sizeof(uns), CPU_STRUCT_ALIGN)
106
107 static byte *
108 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
109 {
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);
117 }
118
119 static byte *
120 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
121 {
122   enum cf_type type = item->type;
123   void *old_p = *ptr;
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);
128   *processed = taken;
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*));
133   *ptr = new_p;
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);
140   } else
141     return cf_printf("Dynamic arrays do not support operation %s", cf_op_names[op]);
142 }
143
144 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
145
146 static byte *
147 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
148 {
149   cf_add_dirty(sec, ptr);
150   *processed = 0;
151   for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
152   {
153     int taken;
154     byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
155     if (msg)
156       return cf_printf("Item %s: %s", ci->name, msg);
157     *processed += taken;
158     number -= taken;
159     pars += taken;
160     if (!number)                // stop parsing, because many parsers would otherwise complain that number==0
161       break;
162   }
163   return NULL;
164 }
165
166 static void
167 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
168 {
169   switch (op)
170   {
171     case OP_EDIT:               // edition has been done in-place
172       break;
173     case OP_REMOVE:
174       CF_JOURNAL_VAR(where->prev->next);
175       CF_JOURNAL_VAR(where->next->prev);
176       clist_remove(where);
177       break;
178     case OP_AFTER:              // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
179     case OP_PREPEND:
180     case OP_COPY:
181       CF_JOURNAL_VAR(where->next->prev);
182       CF_JOURNAL_VAR(where->next);
183       clist_insert_after(new_node, where);
184       break;
185     case OP_BEFORE:             // implementation dependend (append_tail = before(list))
186     case OP_APPEND:
187     case OP_SET:
188       CF_JOURNAL_VAR(where->prev->next);
189       CF_JOURNAL_VAR(where->prev);
190       clist_insert_before(new_node, where);
191       break;
192     default:
193       ASSERT(0);
194   }
195 }
196
197 static byte *
198 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
199 {
200   if (op >= OP_REMOVE)
201     return cf_printf("You have to open a block for operation %s", cf_op_names[op]);
202   if (!number)
203     return "Nothing to add to the list";
204   struct cf_section *sec = item->u.sec;
205   *processed = 0;
206   uns index = 0;
207   while (number > 0)
208   {
209     void *node = cf_malloc(sec->size);
210     cf_init_section(item->name, sec, node, 1);
211     add_to_list(ptr, node, op);
212     int taken;
213     /* If the node contains any dynamic attribute at the end, we suppress
214      * auto-repetition here and pass the flag inside instead.  */
215     index++;
216     byte *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
217     if (msg)
218       return sec->flags & SEC_FLAG_DYNAMIC ? msg : cf_printf("Node %d of list %s: %s", index, item->name, msg);
219     *processed += taken;
220     number -= taken;
221     pars += taken;
222     if (sec->flags & SEC_FLAG_DYNAMIC)
223       break;
224   }
225   return NULL;
226 }
227
228 static byte *
229 interpret_add_bitmap(struct cf_item *item, int number, byte **pars, int *processed, u32 *ptr, enum cf_operation op)
230 {
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++) {
237     uns idx;
238     if (item->type == CT_INT)
239       TRY( cf_parse_int(pars[i], &idx) );
240     else
241       TRY( cf_parse_lookup(pars[i], &idx, item->u.lookup) );
242     if (idx >= 32)
243       return "Bitmaps only have 32 bits";
244     if (op == OP_SET)
245       *ptr |= 1<<idx;
246     else
247       *ptr &= ~(1<<idx);
248   }
249   *processed = number;
250   return NULL;
251 }
252
253 static byte *
254 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
255 {
256   int taken;
257   switch (item->cls)
258   {
259     case CC_STATIC:
260       if (!number)
261         return "Missing value";
262       taken = MIN(number, item->number);
263       *processed = taken;
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);
267     case CC_DYNAMIC:
268       if (!allow_dynamic)
269         return "Dynamic array cannot be used here";
270       taken = MIN(number, ABS(item->number));
271       *processed = taken;
272       return interpret_set_dynamic(item, taken, pars, ptr);
273     case CC_PARSER:
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));
279       *processed = taken;
280       for (int i=0; i<taken; i++)
281         pars[i] = cf_strdup(pars[i]);
282       return item->u.par(taken, pars, ptr);
283     case CC_SECTION:
284       return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
285     case CC_LIST:
286       if (!allow_dynamic)
287         return "Lists cannot be used here";
288       return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
289     case CC_BITMAP:
290       if (!allow_dynamic)
291         return "Bitmaps cannot be used here";
292       return interpret_add_bitmap(item, number, pars, processed, ptr, OP_SET);
293     default:
294       ASSERT(0);
295   }
296 }
297
298 static byte *
299 interpret_set_all(struct cf_item *item, void *ptr, enum cf_operation op)
300 {
301   if (item->cls == CC_BITMAP) {
302     cf_journal_block(ptr, sizeof(u32));
303     if (op == OP_CLEAR)
304       * (u32*) ptr = 0;
305     else
306       if (item->type == CT_INT)
307         * (u32*) ptr = ~0u;
308       else {
309         uns nr = -1;
310         while (item->u.lookup[++nr]);
311         * (u32*) ptr = ~0u >> (32-nr);
312       }
313     return NULL;
314   } else if (op != OP_CLEAR)
315     return "The item is not a bitmap";
316
317   if (item->cls == CC_LIST) {
318     cf_journal_block(ptr, sizeof(clist));
319     clist_init(ptr);
320   } else if (item->cls == CC_DYNAMIC) {
321     cf_journal_block(ptr, sizeof(void *));
322     static uns zero = 0;
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*));
327   } else
328     return "The item is not a list, dynamic array, bitmap, or string";
329   return NULL;
330 }
331
332 static int
333 cmp_items(void *i1, void *i2, struct cf_item *item)
334 {
335   ASSERT(item->cls == CC_STATIC);
336   i1 += (addr_int_t) item->ptr;
337   i2 += (addr_int_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));
342 }
343
344 static void *
345 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
346 {
347   CLIST_FOR_EACH(cnode *, n, *list)
348   {
349     uns found = 1;
350     for (uns i=0; i<32; i++)
351       if (mask & (1<<i))
352         if (cmp_items(n, query, sec->cfg+i))
353         {
354           found = 0;
355           break;
356         }
357     if (found)
358       return n;
359   }
360   return NULL;
361 }
362
363 static byte *
364 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
365 {
366   uns nr = sec->flags & SEC_FLAG_NUMBER;
367   if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
368   {
369     uns i = item - sec->cfg;
370     if (i >= 32)
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";
374     *mask |= 1 << i;
375   }
376   return NULL;
377 }
378
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];
388 static uns level;
389
390 static byte *
391 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
392 {
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) {
397     .sec = NULL,
398     .base_ptr = NULL,
399     .op = pure_op,
400     .list = NULL,
401     .mask = 0,
402     .item = NULL,
403   };
404   if (!item)                    // unknown is ignored; we just need to trace recursion
405     return NULL;
406   stack[level].sec = item->u.sec;
407   if (item->cls == CC_SECTION)
408   {
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
413   }
414   else if (item->cls == CC_LIST)
415   {
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;
425     } else
426       stack[level].op |= OP_1ST;
427   }
428   else
429     return "Opening brace can only be used on sections and lists";
430   return NULL;
431 }
432
433 static byte *
434 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
435 {
436   if (st->op == OP_CLOSE)       // top-level
437     return "Unmatched } parenthesis";
438   if (!st->sec) {               // dummy run on unknown section
439     if (!(op & OP_OPEN))
440       level--;
441     return NULL;
442   }
443   enum cf_operation pure_op = st->op & OP_MASK;
444   if (st->op & OP_1ST)
445   {
446     st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
447     if (!st->list)
448       return "Cannot find a node matching the query";
449     if (pure_op != OP_REMOVE)
450     {
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
459         if (st->sec->copy)
460           TRY( st->sec->copy(st->base_ptr, st->list) );
461       } else
462         ASSERT(0);
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);
466         return NULL;
467       }
468       int taken;                // parse parameters on 1 line immediately
469       TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
470       number -= taken;
471       pars += taken;
472       // and fall-thru to the 2nd phase
473     }
474     add_to_list(st->list, st->base_ptr, pure_op);
475   }
476   level--;
477   if (number)
478     return "No parameters expected after the }";
479   else if (op & OP_OPEN)
480     return "No { is expected";
481   else
482     return NULL;
483 }
484
485 static struct cf_item *
486 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
487 {
488   *msg = NULL;
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
492     return NULL;
493   while (1)
494   {
495     if (curr_sec != &cf_sections)
496       cf_add_dirty(curr_sec, *ptr);
497     byte *c = strchr(name, '.');
498     if (c)
499       *c++ = 0;
500     struct cf_item *ci = cf_find_subitem(curr_sec, name);
501     if (!ci->cls)
502     {
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);
505       return NULL;
506     }
507     *ptr += (addr_int_t) ci->ptr;
508     if (!c)
509       return ci;
510     if (ci->cls != CC_SECTION)
511     {
512       *msg = cf_printf("Item %s is not a section", name);
513       return NULL;
514     }
515     curr_sec = ci->u.sec;
516     name = c;
517   }
518 }
519
520 byte *
521 cf_interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
522 {
523   byte *msg;
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);
528   if (msg)
529     return msg;
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
533     if (number)
534       return "Cannot open a block after a parameter has been passed on a line";
535     return opening_brace(item, ptr, op);
536   }
537   if (!item)                    // ignored item in an unknown section
538     return NULL;
539   op &= OP_MASK;
540
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);
552   else
553     return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
554   if (msg)
555     return msg;
556   if (taken < number)
557     return cf_printf("Too many parameters: %d>%d", number, taken);
558
559   return NULL;
560 }
561
562 byte *
563 cf_find_item(byte *name, struct cf_item *item)
564 {
565   byte *msg;
566   void *ptr = NULL;
567   struct cf_item *ci = find_item(&cf_sections, name, &msg, &ptr);
568   if (msg)
569     return msg;
570   if (ci) {
571     *item = *ci;
572     item->ptr = ptr;
573   } else
574     bzero(item, sizeof(struct cf_item));
575   return NULL;
576 }
577
578 byte *
579 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
580 {
581   byte *msg;
582   int taken = 0;
583   switch (op) {
584     case OP_SET:
585       msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
586       break;
587     case OP_CLEAR:
588     case OP_ALL:
589       msg = interpret_set_all(item, item->ptr, op);
590       break;
591     case OP_APPEND:
592     case OP_PREPEND:
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);
597       else
598         return "The attribute does not support append/prepend";
599       break;
600     case OP_REMOVE:
601       if (item->cls == CC_BITMAP)
602         msg = interpret_add_bitmap(item, number, pars, &taken, item->ptr, op);
603       else
604         return "Only applicable on bitmaps";
605       break;
606     default:
607       return "Unsupported operation";
608   }
609   if (msg)
610     return msg;
611   if (taken < number)
612     return "Too many parameters";
613   return NULL;
614 }
615
616 void
617 cf_init_stack(void)
618 {
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);
624   }
625   level = 0;
626   stack[0] = (struct item_stack) {
627     .sec = &cf_sections,
628     .base_ptr = NULL,
629     .op = OP_CLOSE,
630     .list = NULL,
631     .mask = 0,
632     .item = NULL
633   };
634 }
635
636 int
637 cf_check_stack(void)
638 {
639   if (level > 0) {
640     log(L_ERROR, "Unterminated block");
641     return 1;
642   }
643   return 0;
644 }
645