]> mj.ucw.cz Git - libucw.git/blob - lib/conf-intr.c
5a5f7eaab0e2c0d60b21fb556f957f400549ea06
[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_bitmap(struct cf_item *item, int number, byte **pars, int *processed, u32 *ptr, enum cf_operation op)
230 {
231   if (item->cls != CC_BITMAP)
232     return cf_printf("Expecting a bitmap");
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_bitmap(item, number, pars, processed, ptr, OP_SET);
293     default:
294       ASSERT(0);
295   }
296 }
297
298 static byte *
299 interpret_clear(struct cf_item *item, void *ptr)
300 {
301   if (item->cls == CC_LIST) {
302     cf_journal_block(ptr, sizeof(clist));
303     clist_init(ptr);
304   } else if (item->cls == CC_DYNAMIC) {
305     cf_journal_block(ptr, sizeof(void *));
306     static uns zero = 0;
307     * (void**) ptr = (&zero) + 1;
308   } else if (item->cls == CC_STATIC && item->type == CT_STRING) {
309     cf_journal_block(ptr, item->number * sizeof(byte*));
310     bzero(ptr, item->number * sizeof(byte*));
311   } else if (item->cls == CC_BITMAP) {
312     cf_journal_block(ptr, sizeof(u32));
313     * (u32*) ptr = 0;
314   } else
315     return "The item is not a list, dynamic array, bitmap, or string";
316   return NULL;
317 }
318
319 static byte *
320 interpret_set_all(struct cf_item *item, void *ptr)
321 {
322   if (item->cls == CC_BITMAP) {
323     cf_journal_block(ptr, sizeof(u32));
324     if (item->type == CT_INT)
325       * (u32*) ptr = ~0u;
326     else {
327       uns nr = -1;
328       while (item->u.lookup[++nr]);
329       * (u32*) ptr = ~0u >> (32-nr);
330     }
331     return NULL;
332   } else
333     return "The item is not a bitmap";
334 }
335
336 static int
337 cmp_items(void *i1, void *i2, struct cf_item *item)
338 {
339   ASSERT(item->cls == CC_STATIC);
340   i1 += (addr_int_t) item->ptr;
341   i2 += (addr_int_t) item->ptr;
342   if (item->type == CT_STRING)
343     return strcmp(* (byte**) i1, * (byte**) i2);
344   else                          // all numeric types
345     return memcmp(i1, i2, cf_type_size(item->type, item->u.utype));
346 }
347
348 static void *
349 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
350 {
351   CLIST_FOR_EACH(cnode *, n, *list)
352   {
353     uns found = 1;
354     for (uns i=0; i<32; i++)
355       if (mask & (1<<i))
356         if (cmp_items(n, query, sec->cfg+i))
357         {
358           found = 0;
359           break;
360         }
361     if (found)
362       return n;
363   }
364   return NULL;
365 }
366
367 static byte *
368 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
369 {
370   uns nr = sec->flags & SEC_FLAG_NUMBER;
371   if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
372   {
373     uns i = item - sec->cfg;
374     if (i >= 32)
375       return "Cannot select list nodes by this attribute";
376     if (sec->cfg[i].cls != CC_STATIC)
377       return "Selection can only be done based on basic attributes";
378     *mask |= 1 << i;
379   }
380   return NULL;
381 }
382
383 #define MAX_STACK_SIZE  10
384 static struct item_stack {
385   struct cf_section *sec;       // nested section
386   void *base_ptr;               // because original pointers are often relative
387   enum cf_operation op;         // it is performed when a closing brace is encountered
388   void *list;                   // list the operations should be done on
389   u32 mask;                     // bit array of selectors searching in a list
390   struct cf_item *item;         // cf_item of the list
391 } stack[MAX_STACK_SIZE];
392 static uns level;
393
394 static byte *
395 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
396 {
397   if (level >= MAX_STACK_SIZE-1)
398     return "Too many nested sections";
399   enum cf_operation pure_op = op & OP_MASK;
400   stack[++level] = (struct item_stack) {
401     .sec = NULL,
402     .base_ptr = NULL,
403     .op = pure_op,
404     .list = NULL,
405     .mask = 0,
406     .item = NULL,
407   };
408   if (!item)                    // unknown is ignored; we just need to trace recursion
409     return NULL;
410   stack[level].sec = item->u.sec;
411   if (item->cls == CC_SECTION)
412   {
413     if (pure_op != OP_SET)
414       return "Only SET operation can be used with a section";
415     stack[level].base_ptr = ptr;
416     stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
417   }
418   else if (item->cls == CC_LIST)
419   {
420     stack[level].base_ptr = cf_malloc(item->u.sec->size);
421     cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
422     stack[level].list = ptr;
423     stack[level].item = item;
424     if (pure_op == OP_UNSET || pure_op == OP_ALL)
425       return "Bitmap operations are not supported for lists";
426     else if (pure_op < OP_REMOVE) {
427       add_to_list(ptr, stack[level].base_ptr, pure_op);
428       stack[level].op |= OP_2ND;
429     } else
430       stack[level].op |= OP_1ST;
431   }
432   else
433     return "Opening brace can only be used on sections and lists";
434   return NULL;
435 }
436
437 static byte *
438 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
439 {
440   if (st->op == OP_CLOSE)       // top-level
441     return "Unmatched } parenthesis";
442   if (!st->sec) {               // dummy run on unknown section
443     if (!(op & OP_OPEN))
444       level--;
445     return NULL;
446   }
447   enum cf_operation pure_op = st->op & OP_MASK;
448   if (st->op & OP_1ST)
449   {
450     st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
451     if (!st->list)
452       return "Cannot find a node matching the query";
453     if (pure_op != OP_REMOVE)
454     {
455       if (pure_op == OP_EDIT)
456         st->base_ptr = st->list;
457       else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
458         cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
459       else if (pure_op == OP_COPY) {
460         if (st->sec->flags & SEC_FLAG_CANT_COPY)
461           return cf_printf("Item %s cannot be copied", st->item->name);
462         memcpy(st->base_ptr, st->list, st->sec->size);  // strings and dynamic arrays are shared
463         if (st->sec->copy)
464           TRY( st->sec->copy(st->base_ptr, st->list) );
465       } else
466         ASSERT(0);
467       if (op & OP_OPEN) {       // stay at the same recursion level
468         st->op = (st->op | OP_2ND) & ~OP_1ST;
469         add_to_list(st->list, st->base_ptr, pure_op);
470         return NULL;
471       }
472       int taken;                // parse parameters on 1 line immediately
473       TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
474       number -= taken;
475       pars += taken;
476       // and fall-thru to the 2nd phase
477     }
478     add_to_list(st->list, st->base_ptr, pure_op);
479   }
480   level--;
481   if (number)
482     return "No parameters expected after the }";
483   else if (op & OP_OPEN)
484     return "No { is expected";
485   else
486     return NULL;
487 }
488
489 static struct cf_item *
490 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
491 {
492   *msg = NULL;
493   if (name[0] == '^')                           // absolute name instead of relative
494     name++, curr_sec = &cf_sections, *ptr = NULL;
495   if (!curr_sec)                                // don't even search in an unknown section
496     return NULL;
497   while (1)
498   {
499     if (curr_sec != &cf_sections)
500       cf_add_dirty(curr_sec, *ptr);
501     byte *c = strchr(name, '.');
502     if (c)
503       *c++ = 0;
504     struct cf_item *ci = cf_find_subitem(curr_sec, name);
505     if (!ci->cls)
506     {
507       if (!(curr_sec->flags & SEC_FLAG_UNKNOWN))        // ignore silently unknown top-level sections and unknown attributes in flagged sections
508         *msg = cf_printf("Unknown item %s", name);
509       return NULL;
510     }
511     *ptr += (addr_int_t) ci->ptr;
512     if (!c)
513       return ci;
514     if (ci->cls != CC_SECTION)
515     {
516       *msg = cf_printf("Item %s is not a section", name);
517       return NULL;
518     }
519     curr_sec = ci->u.sec;
520     name = c;
521   }
522 }
523
524 byte *
525 cf_interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
526 {
527   byte *msg;
528   if ((op & OP_MASK) == OP_CLOSE)
529     return closing_brace(stack+level, op, number, pars);
530   void *ptr = stack[level].base_ptr;
531   struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
532   if (msg)
533     return msg;
534   if (stack[level].op & OP_1ST)
535     TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
536   if (op & OP_OPEN) {           // the operation will be performed after the closing brace
537     if (number)
538       return "Cannot open a block after a parameter has been passed on a line";
539     return opening_brace(item, ptr, op);
540   }
541   if (!item)                    // ignored item in an unknown section
542     return NULL;
543   op &= OP_MASK;
544
545   int taken = 0;                // process as many parameters as possible
546   if (op == OP_CLEAR)
547     msg = interpret_clear(item, ptr);
548   else if (op == OP_ALL)
549     msg = interpret_set_all(item, ptr);
550   else if (op == OP_SET)
551     msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
552   else if (op == OP_UNSET)
553     msg = interpret_bitmap(item, number, pars, &taken, ptr, OP_UNSET);
554   else if (item->cls == CC_DYNAMIC)
555     msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
556   else if (item->cls == CC_LIST)
557     msg = interpret_add_list(item, number, pars, &taken, ptr, op);
558   else
559     return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
560   if (msg)
561     return msg;
562   if (taken < number)
563     return cf_printf("Too many parameters: %d>%d", number, taken);
564
565   return NULL;
566 }
567
568 byte *
569 cf_find_item(byte *name, struct cf_item *item)
570 {
571   byte *msg;
572   void *ptr = NULL;
573   struct cf_item *ci = find_item(&cf_sections, name, &msg, &ptr);
574   if (msg)
575     return msg;
576   if (ci) {
577     *item = *ci;
578     item->ptr = ptr;
579   } else
580     bzero(item, sizeof(struct cf_item));
581   return NULL;
582 }
583
584 byte *
585 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
586 {
587   byte *msg;
588   int taken = 0;
589   switch (op) {
590     case OP_SET:
591       msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
592       break;
593     case OP_CLEAR:
594       msg = interpret_clear(item, item->ptr);
595       break;
596     case OP_UNSET:
597       msg = interpret_bitmap(item, number, pars, &taken, item->ptr, OP_UNSET);
598       break;
599     case OP_ALL:
600       msg = interpret_set_all(item, item->ptr);
601       break;
602     case OP_APPEND:
603     case OP_PREPEND:
604       if (item->cls == CC_DYNAMIC)
605         msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
606       else if (item->cls == CC_LIST)
607         msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
608       else
609         return "The attribute class does not support append/prepend";
610       break;
611     default:
612       return "Unsupported operation";
613   }
614   if (msg)
615     return msg;
616   if (taken < number)
617     return "Too many parameters";
618   return NULL;
619 }
620
621 void
622 cf_init_stack(void)
623 {
624   static uns initialized = 0;
625   if (!initialized++) {
626     cf_sections.flags |= SEC_FLAG_UNKNOWN;
627     cf_sections.size = 0;                       // size of allocated array used to be stored here
628     cf_init_section(NULL, &cf_sections, NULL, 0);
629   }
630   level = 0;
631   stack[0] = (struct item_stack) {
632     .sec = &cf_sections,
633     .base_ptr = NULL,
634     .op = OP_CLOSE,
635     .list = NULL,
636     .mask = 0,
637     .item = NULL
638   };
639 }
640
641 int
642 cf_check_stack(void)
643 {
644   if (level > 0) {
645     log(L_ERROR, "Unterminated block");
646     return 1;
647   }
648   return 0;
649 }
650