]> mj.ucw.cz Git - libucw.git/blob - lib/conf-intr.c
acbe702f2446def422ab06ac3dd97fc3a588693f
[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
104 #define DARY_HDR_SIZE ALIGN(sizeof(uns), CPU_STRUCT_ALIGN)
105
106 static byte *
107 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
108 {
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);
116 }
117
118 static byte *
119 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
120 {
121   enum cf_type type = item->type;
122   void *old_p = *ptr;
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);
127   *processed = taken;
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*));
132   *ptr = new_p;
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);
139   } else
140     return cf_printf("Dynamic arrays do not support operation %s", cf_op_names[op]);
141 }
142
143 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
144
145 static byte *
146 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
147 {
148   cf_add_dirty(sec, ptr);
149   *processed = 0;
150   for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
151   {
152     int taken;
153     byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
154     if (msg)
155       return cf_printf("Item %s: %s", ci->name, msg);
156     *processed += taken;
157     number -= taken;
158     pars += taken;
159     if (!number)                // stop parsing, because many parsers would otherwise complain that number==0
160       break;
161   }
162   return NULL;
163 }
164
165 static void
166 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
167 {
168   switch (op)
169   {
170     case OP_EDIT:               // edition has been done in-place
171       break;
172     case OP_REMOVE:
173       CF_JOURNAL_VAR(where->prev->next);
174       CF_JOURNAL_VAR(where->next->prev);
175       clist_remove(where);
176       break;
177     case OP_AFTER:              // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
178     case OP_PREPEND:
179     case OP_COPY:
180       CF_JOURNAL_VAR(where->next->prev);
181       CF_JOURNAL_VAR(where->next);
182       clist_insert_after(new_node, where);
183       break;
184     case OP_BEFORE:             // implementation dependend (append_tail = before(list))
185     case OP_APPEND:
186     case OP_SET:
187       CF_JOURNAL_VAR(where->prev->next);
188       CF_JOURNAL_VAR(where->prev);
189       clist_insert_before(new_node, where);
190       break;
191     default:
192       ASSERT(0);
193   }
194 }
195
196 static byte *
197 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
198 {
199   if (op >= OP_REMOVE)
200     return cf_printf("You have to open a block for operation %s", cf_op_names[op]);
201   if (!number)
202     return "Nothing to add to the list";
203   struct cf_section *sec = item->u.sec;
204   *processed = 0;
205   uns index = 0;
206   while (number > 0)
207   {
208     void *node = cf_malloc(sec->size);
209     cf_init_section(item->name, sec, node, 1);
210     add_to_list(ptr, node, op);
211     int taken;
212     /* If the node contains any dynamic attribute at the end, we suppress
213      * auto-repetition here and pass the flag inside instead.  */
214     index++;
215     byte *msg = interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC);
216     if (msg)
217       return sec->flags & SEC_FLAG_DYNAMIC ? msg : cf_printf("Node %d of list %s: %s", index, item->name, msg);
218     *processed += taken;
219     number -= taken;
220     pars += taken;
221     if (sec->flags & SEC_FLAG_DYNAMIC)
222       break;
223   }
224   return NULL;
225 }
226
227 static byte *
228 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
229 {
230   int taken;
231   switch (item->cls)
232   {
233     case CC_STATIC:
234       if (!number)
235         return "Missing value";
236       taken = MIN(number, item->number);
237       *processed = taken;
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);
241     case CC_DYNAMIC:
242       if (!allow_dynamic)
243         return "Dynamic array cannot be used here";
244       taken = MIN(number, ABS(item->number));
245       *processed = taken;
246       return interpret_set_dynamic(item, taken, pars, ptr);
247     case CC_PARSER:
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));
253       *processed = taken;
254       for (int i=0; i<taken; i++)
255         pars[i] = cf_strdup(pars[i]);
256       return item->u.par(taken, pars, ptr);
257     case CC_SECTION:
258       return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
259     case CC_LIST:
260       if (!allow_dynamic)
261         return "Lists cannot be used here";
262       return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
263     default:
264       ASSERT(0);
265   }
266 }
267
268 static byte *
269 interpret_clear(struct cf_item *item, void *ptr)
270 {
271   if (item->cls == CC_LIST) {
272     cf_journal_block(ptr, sizeof(clist));
273     clist_init(ptr);
274   } else if (item->cls == CC_DYNAMIC) {
275     cf_journal_block(ptr, sizeof(void *));
276     static uns zero = 0;
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*));
281   } else
282     return "The item is not a list, dynamic array, or string";
283   return NULL;
284 }
285
286 static int
287 cmp_items(void *i1, void *i2, struct cf_item *item)
288 {
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));
296 }
297
298 static void *
299 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
300 {
301   CLIST_FOR_EACH(cnode *, n, *list)
302   {
303     uns found = 1;
304     for (uns i=0; i<32; i++)
305       if (mask & (1<<i))
306         if (cmp_items(n, query, sec->cfg+i))
307         {
308           found = 0;
309           break;
310         }
311     if (found)
312       return n;
313   }
314   return NULL;
315 }
316
317 static byte *
318 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
319 {
320   uns nr = sec->flags & SEC_FLAG_NUMBER;
321   if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
322   {
323     uns i = item - sec->cfg;
324     if (i >= 32)
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";
328     *mask |= 1 << i;
329   }
330   return NULL;
331 }
332
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];
342 static uns level;
343
344 static byte *
345 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
346 {
347   if (level >= MAX_STACK_SIZE-1)
348     return "Too many nested sections";
349   stack[++level] = (struct item_stack) {
350     .sec = NULL,
351     .base_ptr = NULL,
352     .op = op & OP_MASK,
353     .list = NULL,
354     .mask = 0,
355     .item = NULL,
356   };
357   if (!item)                    // unknown is ignored; we just need to trace recursion
358     return NULL;
359   stack[level].sec = item->u.sec;
360   if (item->cls == CC_SECTION)
361   {
362     stack[level].base_ptr = ptr;
363     stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
364   }
365   else if (item->cls == CC_LIST)
366   {
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;
374     } else
375       stack[level].op |= OP_1ST;
376   }
377   else
378     return "Opening brace can only be used on sections and lists";
379   return NULL;
380 }
381
382 static byte *
383 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
384 {
385   if (st->op == OP_CLOSE)       // top-level
386     return "Unmatched } parenthesis";
387   if (!st->sec) {               // dummy run on unknown section
388     if (!(op & OP_OPEN))
389       level--;
390     return NULL;
391   }
392   enum cf_operation pure_op = st->op & OP_MASK;
393   if (st->op & OP_1ST)
394   {
395     st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
396     if (!st->list)
397       return "Cannot find a node matching the query";
398     if (pure_op != OP_REMOVE)
399     {
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
408         if (st->sec->copy)
409           TRY( st->sec->copy(st->base_ptr, st->list) );
410       } else
411         ASSERT(0);
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);
415         return NULL;
416       }
417       int taken;                // parse parameters on 1 line immediately
418       TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
419       number -= taken;
420       pars += taken;
421       // and fall-thru to the 2nd phase
422     }
423     add_to_list(st->list, st->base_ptr, pure_op);
424   }
425   level--;
426   if (number)
427     return "No parameters expected after the }";
428   else if (op & OP_OPEN)
429     return "No { is expected";
430   else
431     return NULL;
432 }
433
434 static struct cf_item *
435 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
436 {
437   *msg = NULL;
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
441     return NULL;
442   while (1)
443   {
444     if (curr_sec != &cf_sections)
445       cf_add_dirty(curr_sec, *ptr);
446     byte *c = strchr(name, '.');
447     if (c)
448       *c++ = 0;
449     struct cf_item *ci = cf_find_subitem(curr_sec, name);
450     if (!ci->cls)
451     {
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);
454       return NULL;
455     }
456     *ptr += (addr_int_t) ci->ptr;
457     if (!c)
458       return ci;
459     if (ci->cls != CC_SECTION)
460     {
461       *msg = cf_printf("Item %s is not a section", name);
462       return NULL;
463     }
464     curr_sec = ci->u.sec;
465     name = c;
466   }
467 }
468
469 byte *
470 cf_interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
471 {
472   byte *msg;
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);
477   if (msg)
478     return msg;
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
482     if (number)
483       return "Cannot open a block after a parameter has been passed on a line";
484     return opening_brace(item, ptr, op);
485   }
486   if (!item)                    // ignored item in an unknown section
487     return NULL;
488   op &= OP_MASK;
489
490   int taken;                    // process as many parameters as possible
491   if (op == OP_CLEAR)
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);
499   else
500     return cf_printf("Operation %s not supported on attribute %s", cf_op_names[op], name);
501   if (msg)
502     return msg;
503   if (taken < number)
504     return cf_printf("Too many parameters: %d>%d", number, taken);
505
506   return NULL;
507 }
508
509 byte *
510 cf_find_item(byte *name, struct cf_item *item)
511 {
512   byte *msg;
513   void *ptr = NULL;
514   struct cf_item *ci = find_item(&cf_sections, name, &msg, &ptr);
515   if (msg)
516     return msg;
517   if (ci) {
518     *item = *ci;
519     item->ptr = ptr;
520   } else
521     bzero(item, sizeof(struct cf_item));
522   return NULL;
523 }
524
525 byte *
526 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
527 {
528   byte *msg;
529   int taken;
530   switch (op) {
531     case OP_SET:
532       msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
533       break;
534     case OP_CLEAR:
535       taken = 0;
536       msg = interpret_clear(item, item->ptr);
537       break;
538     case OP_APPEND:
539     case OP_PREPEND:
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);
544       else
545         return "The attribute class does not support append/prepend";
546       break;
547     default:
548       return "Unsupported operation";
549   }
550   if (msg)
551     return msg;
552   if (taken < number)
553     return "Too many parameters";
554   return NULL;
555 }
556
557 void
558 cf_init_stack(void)
559 {
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);
565   }
566   level = 0;
567   stack[0] = (struct item_stack) {
568     .sec = &cf_sections,
569     .base_ptr = NULL,
570     .op = OP_CLOSE,
571     .list = NULL,
572     .mask = 0,
573     .item = NULL
574   };
575 }
576
577 int
578 cf_check_stack(void)
579 {
580   if (level > 0) {
581     log(L_ERROR, "Unterminated block");
582     return 1;
583   }
584   return 0;
585 }
586