]> mj.ucw.cz Git - libucw.git/blob - lib/conf2.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git#dev-config
[libucw.git] / lib / conf2.c
1 /*
2  *      UCW Library -- Reading of configuration files
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/conf2.h"
13 #include "lib/mempool.h"
14 #include "lib/clists.h"
15 #include "lib/fastbuf.h"
16 #include "lib/chartype.h"
17 #include "lib/lfs.h"
18 #include "lib/stkstring.h"
19 #include "lib/binsearch.h"
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <stdarg.h>
25 #include <fcntl.h>
26 #include <getopt.h>
27
28 #define TRY(f)  do { byte *_msg = f; if (_msg) return _msg; } while (0)
29
30 /* Memory allocation */
31
32 struct mempool *cf_pool;        // current pool for loading new configuration
33 static struct old_pools {
34   struct old_pools *prev;
35   struct mempool *pool;
36 } *pools;                       // link-list of older cf_pool's
37
38 void *
39 cf_malloc(uns size)
40 {
41   return mp_alloc(cf_pool, size);
42 }
43
44 void *
45 cf_malloc_zero(uns size)
46 {
47   return mp_alloc_zero(cf_pool, size);
48 }
49
50 byte *
51 cf_strdup(byte *s)
52 {
53   return mp_strdup(cf_pool, s);
54 }
55
56 byte *
57 cf_printf(char *fmt, ...)
58 {
59   va_list args;
60   va_start(args, fmt);
61   byte *res = mp_vprintf(cf_pool, fmt, args);
62   va_end(args);
63   return res;
64 }
65
66 /* Undo journal */
67
68 uns cf_need_journal = 1;        // some programs do not need journal
69 static struct journal_item {
70   struct journal_item *prev;
71   byte *ptr;
72   uns len;
73   byte copy[0];
74 } *journal;
75
76 void
77 cf_journal_block(void *ptr, uns len)
78 {
79   if (!cf_need_journal)
80     return;
81   struct journal_item *ji = cf_malloc(sizeof(struct journal_item) + len);
82   ji->prev = journal;
83   ji->ptr = ptr;
84   ji->len = len;
85   memcpy(ji->copy, ptr, len);
86   journal = ji;
87 }
88
89 static void
90 journal_swap(void)
91   // swaps the contents of the memory and the journal, and reverses the list
92 {
93   struct journal_item *curr, *prev, *next;
94   for (next=NULL, curr=journal; curr; next=curr, curr=prev)
95   {
96     prev = curr->prev;
97     curr->prev = next;
98     for (uns i=0; i<curr->len; i++)
99     {
100       byte x = curr->copy[i];
101       curr->copy[i] = curr->ptr[i];
102       curr->ptr[i] = x;
103     }
104   }
105   journal = next;
106 }
107
108 static struct journal_item *
109 journal_new_transaction(uns new_pool)
110 {
111   if (new_pool)
112     cf_pool = mp_new(1<<10);
113   struct journal_item *oldj = journal;
114   journal = NULL;
115   return oldj;
116 }
117
118 static void
119 journal_commit_transaction(uns new_pool, struct journal_item *oldj)
120 {
121   if (new_pool)
122   {
123     struct old_pools *p = cf_malloc(sizeof(struct old_pools));
124     p->prev = pools;
125     p->pool = cf_pool;
126     pools = p;
127   }
128   if (oldj)
129   {
130     struct journal_item **j = &journal;
131     while (*j)
132       j = &(*j)->prev;
133     *j = oldj;
134   }
135 }
136
137 static void
138 journal_rollback_transaction(uns new_pool, struct journal_item *oldj)
139 {
140   if (!cf_need_journal)
141     die("Cannot rollback the configuration, because the journal is disabled.");
142   journal_swap();
143   journal = oldj;
144   if (new_pool)
145   {
146     mp_delete(cf_pool);
147     cf_pool = pools ? pools->pool : NULL;
148   }
149 }
150
151 /* Dirty sections */
152
153 struct dirty_section {
154   struct cf_section *sec;
155   void *ptr;
156 };
157 #define GBUF_TYPE       struct dirty_section
158 #define GBUF_PREFIX(x)  dirtsec_##x
159 #include "lib/gbuf.h"
160 static dirtsec_t dirty;
161 static uns dirties;
162 static uns everything_committed;                // after the 1st load, this flag is set on
163
164 static void
165 add_dirty(struct cf_section *sec, void *ptr)
166 {
167   dirtsec_grow(&dirty, dirties+1);
168   struct dirty_section *dest = dirty.ptr + dirties;
169   if (dirties && dest[-1].sec == sec && dest[-1].ptr == ptr)
170     return;
171   dest->sec = sec;
172   dest->ptr = ptr;
173   dirties++;
174 }
175
176 #define ASORT_PREFIX(x) dirtsec_##x
177 #define ASORT_KEY_TYPE  struct dirty_section
178 #define ASORT_ELT(i)    dirty.ptr[i]
179 #define ASORT_LT(x,y)   x.sec < y.sec || x.sec == y.sec && x.ptr < y.ptr
180 #include "lib/arraysort.h"
181
182 static void
183 sort_dirty(void)
184 {
185   if (dirties <= 1)
186     return;
187   dirtsec_sort(dirties);
188   struct dirty_section *read = dirty.ptr + 1, *write = dirty.ptr + 1, *limit = dirty.ptr + dirties;
189   while (read < limit) {
190     if (read->sec != read[-1].sec || read->ptr != read[-1].ptr) {
191       if (read != write)
192         *write = *read;
193       write++;
194     }
195     read++;
196   }
197   dirties = write - dirty.ptr;
198 }
199
200 /* Initialization */
201
202 #define SEC_FLAG_DYNAMIC        0x80000000      // contains a dynamic attribute
203 #define SEC_FLAG_UNKNOWN        0x40000000      // ignore unknown entriies
204 #define SEC_FLAG_NUMBER         0x0fffffff      // number of entries
205
206 static struct cf_section sections;      // root section
207
208 static struct cf_item *
209 find_subitem(struct cf_section *sec, byte *name)
210 {
211   struct cf_item *ci = sec->cfg;
212   for (; ci->cls; ci++)
213     if (!strcasecmp(ci->name, name))
214       return ci;
215   return ci;
216 }
217
218 static void
219 inspect_section(struct cf_section *sec)
220 {
221   sec->flags = 0;
222   struct cf_item *ci;
223   for (ci=sec->cfg; ci->cls; ci++)
224     if (ci->cls == CC_SECTION) {
225       inspect_section(ci->u.sec);
226       sec->flags |= ci->u.sec->flags & SEC_FLAG_DYNAMIC;
227     } else if (ci->cls == CC_LIST) {
228       inspect_section(ci->u.sec);
229       sec->flags |= SEC_FLAG_DYNAMIC;
230     } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_PARSER && ci->number < 0)
231       sec->flags |= SEC_FLAG_DYNAMIC;
232   sec->flags |= ci - sec->cfg;          // record the number of entries
233 }
234
235 void
236 cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown)
237 {
238   if (!sections.cfg)
239   {
240     sections.size = 50;
241     sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
242   }
243   struct cf_item *ci = find_subitem(&sections, name);
244   if (ci->cls)
245     die("Cannot register section %s twice", name);
246   ci->cls = CC_SECTION;
247   ci->name = name;
248   ci->number = 1;
249   ci->ptr = NULL;
250   ci->u.sec = sec;
251   inspect_section(sec);
252   if (allow_unknown)
253     sec->flags |= SEC_FLAG_UNKNOWN;
254   ci++;
255   if (ci - sections.cfg >= (int) sections.size)
256   {
257     sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
258     bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
259     sections.size *= 2;
260   }
261 }
262
263 void
264 cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero)
265 {
266   if (do_bzero) {
267     ASSERT(sec->size);
268     bzero(ptr, sec->size);
269   }
270   for (uns i=0; sec->cfg[i].cls; i++)
271     if (sec->cfg[i].cls == CC_SECTION)
272       cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr, 0);
273     else if (sec->cfg[i].cls == CC_LIST)
274       clist_init(ptr + (addr_int_t) sec->cfg[i].ptr);
275   if (sec->init) {
276     byte *msg = sec->init(ptr);
277     if (msg)
278       die("Cannot initialize section %s: %s", name, msg);
279   }
280 }
281
282 static void
283 global_init(void)
284 {
285   static uns initialized = 0;
286   if (initialized++)
287     return;
288   sections.flags |= SEC_FLAG_UNKNOWN;
289   sections.size = 0;                    // size of allocated array used to be stored here
290   cf_init_section("top-level", &sections, NULL, 0);
291 }
292
293 static int
294 commit_section(byte *name, struct cf_section *sec, void *ptr, uns commit_all)
295 {
296   struct cf_item *ci;
297   for (ci=sec->cfg; ci->cls; ci++)
298     if (ci->cls == CC_SECTION) {
299       if (commit_section(ci->name, ci->u.sec, ptr + (addr_int_t) ci->ptr, commit_all)) {
300         log(L_ERROR, "It happened in section %s", ci->name);
301         return 1;
302       }
303     } else if (ci->cls == CC_LIST) {
304       struct cnode *n;
305       uns idx = 0;
306       CLIST_WALK(n, * (clist*) (ptr + (addr_int_t) ci->ptr))
307         if (idx++, commit_section(ci->name, ci->u.sec, n, commit_all)) {
308           log(L_ERROR, "It happened in node #%d of list %s", idx, ci->name);
309           return 1;
310         }
311     }
312   if (sec->commit) {
313     /* We have to process the whole tree of sections even if just a few changes
314      * have been made, because there are dependencies between commit-hooks and
315      * hence we need to call them in a fixed order.  */
316 #define ARY_LT_X(ary,i,x) ary[i].sec < x.sec || ary[i].sec == x.sec && ary[i].ptr < x.ptr
317     struct dirty_section comp = { sec, ptr };
318     uns pos = BIN_SEARCH_FIRST_GE_CMP(dirty.ptr, dirties, comp, ARY_LT_X);
319
320     if (commit_all
321         || (pos < dirties && dirty.ptr[pos].sec == sec && dirty.ptr[pos].ptr == ptr)) {
322       byte *msg = sec->commit(ptr);
323       if (msg) {
324         log(L_ERROR, "Cannot commit section %s: %s", name, msg);
325         return 1;
326       }
327     }
328   }
329   return 0;
330 }
331
332 static struct cf_item *
333 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
334 {
335   *msg = NULL;
336   if (name[0] == '^')                           // absolute name instead of relative
337     name++, curr_sec = &sections, *ptr = NULL;
338   if (!curr_sec)                                // don't even search in an unknown section
339     return NULL;
340   while (1)
341   {
342     if (curr_sec != &sections)
343       add_dirty(curr_sec, *ptr);
344     byte *c = strchr(name, '.');
345     if (c)
346       *c++ = 0;
347     struct cf_item *ci = find_subitem(curr_sec, name);
348     if (!ci->cls)
349     {
350       if (!(curr_sec->flags & SEC_FLAG_UNKNOWN))        // ignore silently unknown top-level sections and unknown attributes in flagged sections
351         *msg = cf_printf("Unknown item %s", name);
352       return NULL;
353     }
354     *ptr += (addr_int_t) ci->ptr;
355     if (!c)
356       return ci;
357     if (ci->cls != CC_SECTION)
358     {
359       *msg = cf_printf("Item %s is not a section", name);
360       return NULL;
361     }
362     curr_sec = ci->u.sec;
363     name = c;
364   }
365 }
366
367 byte *
368 cf_find_item(byte *name, struct cf_item *item)
369 {
370   byte *msg;
371   void *ptr;
372   struct cf_item *ci = find_item(&sections, name, &msg, &ptr);
373   if (msg)
374     return msg;
375   *item = *ci;
376   item->ptr = ptr;
377   return NULL;
378 }
379
380 /* Safe loading and reloading */
381
382 static int load_file(byte *file);
383 static int load_string(byte *string);
384
385 int
386 cf_reload(byte *file)
387 {
388   journal_swap();
389   struct journal_item *oldj = journal_new_transaction(1);
390   uns ec = everything_committed;
391   everything_committed = 0;
392   int err = load_file(file);
393   if (!err)
394   {
395     for (struct old_pools *p=pools; p; p=pools)
396     {
397       pools = p->prev;
398       mp_delete(p->pool);
399     }
400     journal_commit_transaction(1, NULL);
401   }
402   else
403   {
404     everything_committed = ec;
405     journal_rollback_transaction(1, oldj);
406     journal_swap();
407   }
408   return err;
409 }
410
411 int
412 cf_load(byte *file)
413 {
414   struct journal_item *oldj = journal_new_transaction(1);
415   int err = load_file(file);
416   if (!err)
417     journal_commit_transaction(1, oldj);
418   else
419     journal_rollback_transaction(1, oldj);
420   return err;
421 }
422
423 int
424 cf_set(byte *string)
425 {
426   struct journal_item *oldj = journal_new_transaction(0);
427   int err = load_string(string);
428   if (!err)
429     journal_commit_transaction(0, oldj);
430   else
431     journal_rollback_transaction(0, oldj);
432   return err;
433 }
434
435 /* Parsers for standard types */
436
437 struct unit {
438   uns name;                     // one-letter name of the unit
439   uns num, den;                 // fraction
440 };
441
442 static const struct unit units[] = {
443   { 'd', 86400, 1 },
444   { 'h', 3600, 1 },
445   { 'k', 1000, 1 },
446   { 'm', 1000000, 1 },
447   { 'g', 1000000000, 1 },
448   { 'K', 1024, 1 },
449   { 'M', 1048576, 1 },
450   { 'G', 1073741824, 1 },
451   { '%', 1, 100 },
452   { 0, 0, 0 }
453 };
454
455 static const struct unit *
456 lookup_unit(byte *value, byte *end, byte **msg)
457 {
458   if (end && *end) {
459     if (end == value || end[1] || *end >= '0' && *end <= '9')
460       *msg = "Invalid number";
461     else {
462       for (const struct unit *u=units; u->name; u++)
463         if (u->name == *end)
464           return u;
465       *msg = "Invalid unit";
466     }
467   }
468   return NULL;
469 }
470
471 static char cf_rngerr[] = "Number out of range";
472
473 byte *
474 cf_parse_int(byte *str, int *ptr)
475 {
476   byte *msg = NULL;
477   if (!*str)
478     msg = "Missing number";
479   else {
480     const struct unit *u;
481     char *end;
482     errno = 0;
483     uns x = strtoul(str, &end, 0);
484     if (errno == ERANGE)
485       msg = cf_rngerr;
486     else if (u = lookup_unit(str, end, &msg)) {
487       u64 y = (u64)x * u->num;
488       if (y % u->den)
489         msg = "Number is not an integer";
490       else {
491         y /= u->den;
492         if (y > 0xffffffff)
493           msg = cf_rngerr;
494         *ptr = y;
495       }
496     } else
497       *ptr = x;
498   }
499   return msg;
500 }
501
502 byte *
503 cf_parse_u64(byte *str, u64 *ptr)
504 {
505   byte *msg = NULL;
506   if (!*str)
507     msg = "Missing number";
508   else {
509     const struct unit *u;
510     char *end;
511     errno = 0;
512     u64 x = strtoull(str, &end, 0);
513     if (errno == ERANGE)
514       msg = cf_rngerr;
515     else if (u = lookup_unit(str, end, &msg)) {
516       if (x > ~(u64)0 / u->num)
517         msg = "Number out of range";
518       else {
519         x *= u->num;
520         if (x % u->den)
521           msg = "Number is not an integer";
522         else
523           *ptr = x / u->den;
524       }
525     } else
526       *ptr = x;
527   }
528   return msg;
529 }
530
531 byte *
532 cf_parse_double(byte *str, double *ptr)
533 {
534   byte *msg = NULL;
535   if (!*str)
536     msg = "Missing number";
537   else {
538     const struct unit *u;
539     double x;
540     uns read_chars;
541     if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
542       msg = "Invalid number";
543     else if (u = lookup_unit(str, str + read_chars, &msg))
544       *ptr = x * u->num / u->den;
545     else
546       *ptr = x;
547   }
548   return msg;
549 }
550
551 byte *
552 cf_parse_ip(byte *p, u32 *varp)
553 {
554   if (!*p)
555     return "Missing IP address";
556   uns x = 0;
557   char *p2;
558   if (*p == '0' && (p[1] | 32) == 'x' && Cxdigit(p[2])) {
559     errno = 0;
560     x = strtoul(p, &p2, 16);
561     if (errno == ERANGE || x > 0xffffffff)
562       goto error;
563     p = p2;
564   }
565   else
566     for (uns i = 0; i < 4; i++) {
567       if (i) {
568         if (*p++ != '.')
569           goto error;
570       }
571       if (!Cdigit(*p))
572         goto error;
573       errno = 0;
574       uns y = strtoul(p, &p2, 10);
575       if (errno == ERANGE || p2 == (char*) p || y > 255)
576         goto error;
577       p = p2;
578       x = (x << 8) + y;
579     }
580   *varp = x;
581   return *p ? "Trailing characters" : NULL;
582 error:
583   return "Invalid IP address";
584 }
585
586 static byte *
587 cf_parse_string(byte *str, byte **ptr)
588 {
589   *ptr = cf_strdup(str);
590   return NULL;
591 }
592
593 static byte *
594 cf_parse_lookup(byte *str, int *ptr, char **t)
595 {
596   char **n = t;
597   uns total_len = 0;
598   while (*n && strcasecmp(*n, str)) {
599     total_len += strlen(*n) + 2;
600     n++;
601   }
602   if (*n) {
603     *ptr = n - t;
604     return NULL;
605   }
606   byte *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
607   c += sprintf(err, "Invalid value %s, possible values are: ", str);
608   for (n=t; *n; n++)
609     c+= sprintf(c, "%s, ", *n);
610   if (*t)
611     c[-2] = 0;
612   *ptr = -1;
613   return err;
614 }
615
616 /* Register size of and parser for each basic type */
617
618 typedef byte *cf_basic_parser(byte *str, void *ptr);
619 static struct {
620   uns size;
621   void *parser;
622 } parsers[] = {
623   { sizeof(int), cf_parse_int },
624   { sizeof(u64), cf_parse_u64 },
625   { sizeof(double), cf_parse_double },
626   { sizeof(u32), cf_parse_ip },
627   { sizeof(byte*), cf_parse_string },
628   { sizeof(int), NULL },                        // lookups are parsed extra
629   { 0, NULL },                                  // user-defined types are parsed extra
630 };
631
632 static inline uns
633 type_size(enum cf_type type, struct cf_user_type *utype)
634 {
635   if (type < CT_USER)
636     return parsers[type].size;
637   else
638     return utype->size;
639 }
640
641 static byte *
642 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_union *u)
643 {
644   for (uns i=0; i<number; i++)
645   {
646     byte *msg;
647     uns size = type_size(type, u->utype);
648     if (type < CT_LOOKUP)
649       msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * size);
650     else if (type == CT_LOOKUP)
651       msg = cf_parse_lookup(pars[i], ptr + i * size, u->lookup);
652     else if (type == CT_USER)
653       msg = u->utype->parser(pars[i], ptr + i * size);
654     else
655       ASSERT(0);
656     if (msg)
657       return cf_printf("Cannot parse item %d: %s", i+1, msg);
658   }
659   return NULL;
660 }
661
662 /* Interpreter */
663
664 #define T(x) #x,
665 static byte *op_names[] = { CF_OPERATIONS };
666 #undef T
667
668 #define OP_MASK 0xff            // only get the operation
669 #define OP_OPEN 0x100           // here we only get an opening brace instead of parameters
670 #define OP_1ST 0x200            // in the 1st phase selectors are recorded into the mask
671 #define OP_2ND 0x400            // in the 2nd phase real data are entered
672
673 static byte *
674 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
675 {
676   enum cf_type type = item->type;
677   cf_journal_block(ptr, sizeof(void*));
678   // boundary checks done by the caller
679   uns size = type_size(item->type, item->u.utype);
680   ASSERT(size >= sizeof(uns));
681   *ptr = cf_malloc((number+1) * size) + size;
682   * (uns*) (*ptr - size) = number;
683   return cf_parse_ary(number, pars, *ptr, type, &item->u);
684 }
685
686 static byte *
687 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
688 {
689   enum cf_type type = item->type;
690   void *old_p = *ptr;
691   uns size = type_size(item->type, item->u.utype);
692   ASSERT(size >= sizeof(uns));
693   int old_nr = * (int*) (old_p - size);
694   int taken = MIN(number, ABS(item->number)-old_nr);
695   *processed = taken;
696   // stretch the dynamic array
697   void *new_p = cf_malloc((old_nr + taken + 1) * size) + size;
698   * (uns*) (new_p - size) = old_nr + taken;
699   cf_journal_block(ptr, sizeof(void*));
700   *ptr = new_p;
701   if (op == OP_APPEND) {
702     memcpy(new_p, old_p, old_nr * size);
703     return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
704   } else if (op == OP_PREPEND) {
705     memcpy(new_p + taken * size, old_p, old_nr * size);
706     return cf_parse_ary(taken, pars, new_p, type, &item->u);
707   } else
708     return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
709 }
710
711 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
712
713 static byte *
714 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
715 {
716   add_dirty(sec, ptr);
717   *processed = 0;
718   for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
719   {
720     int taken;
721     byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
722     if (msg)
723       return cf_printf("Item %s: %s", ci->name, msg);
724     *processed += taken;
725     number -= taken;
726     pars += taken;
727     if (!number)                // stop parsing, because many parsers would otherwise complain that number==0
728       break;
729   }
730   return NULL;
731 }
732
733 static void
734 add_to_list(struct cnode *where, struct cnode *new_node, enum cf_operation op)
735 {
736   switch (op)
737   {
738     case OP_EDIT:               // edition has been done in-place
739       break;
740     case OP_REMOVE:
741       cf_journal_block(&where->prev->next, sizeof(void*));
742       cf_journal_block(&where->next->prev, sizeof(void*));
743       clist_remove(where);
744       break;
745     case OP_AFTER:              // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
746     case OP_PREPEND:
747       cf_journal_block(&where->next->prev, sizeof(void*));
748       cf_journal_block(&where->next, sizeof(void*));
749       clist_insert_after(new_node, where);
750       break;
751     case OP_BEFORE:             // implementation dependend (append_tail = before(list))
752     case OP_APPEND:
753     case OP_SET:
754       cf_journal_block(&where->prev->next, sizeof(void*));
755       cf_journal_block(&where->prev, sizeof(void*));
756       clist_insert_before(new_node, where);
757       break;
758     default:
759       ASSERT(0);
760   }
761 }
762
763 static byte *
764 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
765 {
766   if (op >= OP_REMOVE)
767     return cf_printf("You have to open a block for operation %s", op_names[op]);
768   if (!number)
769     return "Nothing to add to the list";
770   struct cf_section *sec = item->u.sec;
771   *processed = 0;
772   while (number > 0)
773   {
774     void *node = cf_malloc(sec->size);
775     cf_init_section(item->name, sec, node, 1);
776     add_to_list(ptr, node, op);
777     int taken;
778     /* If the node contains any dynamic attribute at the end, we suppress
779      * auto-repetition here and pass the flag inside instead.  */
780     TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
781     *processed += taken;
782     number -= taken;
783     pars += taken;
784     if (sec->flags & SEC_FLAG_DYNAMIC)
785       break;
786   }
787   return NULL;
788 }
789
790 static byte *
791 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
792 {
793   int taken;
794   switch (item->cls)
795   {
796     case CC_STATIC:
797       if (!number)
798         return "Missing value";
799       taken = MIN(number, item->number);
800       *processed = taken;
801       uns size = type_size(item->type, item->u.utype);
802       cf_journal_block(ptr, taken * size);
803       return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
804     case CC_DYNAMIC:
805       if (!allow_dynamic)
806         return "Dynamic array cannot be used here";
807       taken = MIN(number, ABS(item->number));
808       *processed = taken;
809       return interpret_set_dynamic(item, taken, pars, ptr);
810     case CC_PARSER:
811       if (item->number < 0 && !allow_dynamic)
812         return "Parsers with variable number of parameters cannot be used here";
813       if (item->number > 0 && number < item->number)
814         return "Not enough parameters available for the parser";
815       taken = MIN(number, ABS(item->number));
816       *processed = taken;
817       for (int i=0; i<taken; i++)
818         pars[i] = cf_strdup(pars[i]);
819       return item->u.par(taken, pars, ptr);
820     case CC_SECTION:
821       return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
822     case CC_LIST:
823       if (!allow_dynamic)
824         return "Lists cannot be used here";
825       return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
826     default:
827       ASSERT(0);
828   }
829 }
830
831 static byte *
832 interpret_clear(struct cf_item *item, void *ptr)
833 {
834   if (item->cls == CC_LIST) {
835     cf_journal_block(ptr, sizeof(struct clist));
836     clist_init(ptr);
837   } else if (item->cls == CC_DYNAMIC) {
838     cf_journal_block(ptr, sizeof(void *));
839     * (void**) ptr = NULL;
840   } else
841     return "The item is not a list or a dynamic array";
842   return NULL;
843 }
844
845 static int
846 cmp_items(void *i1, void *i2, struct cf_item *item)
847 {
848   ASSERT(item->cls == CC_STATIC);
849   i1 += (addr_int_t) item->ptr;
850   i2 += (addr_int_t) item->ptr;
851   if (item->type == CT_STRING)
852     return strcmp(* (byte**) i1, * (byte**) i2);
853   else                          // all numeric types
854     return memcmp(i1, i2, type_size(item->type, item->u.utype));
855 }
856
857 static void *
858 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
859 {
860   struct cnode *n;
861   CLIST_WALK(n, *list)
862   {
863     uns found = 1;
864     for (uns i=0; i<32; i++)
865       if (mask & (1<<i))
866         if (cmp_items(n, query, sec->cfg+i))
867         {
868           found = 0;
869           break;
870         }
871     if (found)
872       return n;
873   }
874   return NULL;
875 }
876
877 static byte *
878 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
879 {
880   uns nr = sec->flags & SEC_FLAG_NUMBER;
881   if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
882   {
883     uns i = item - sec->cfg;
884     if (i >= 32)
885       return "Cannot select list nodes by this attribute";
886     if (sec->cfg[i].cls != CC_STATIC)
887       return "Selection can only be done based on basic attributes";
888     *mask |= 1 << i;
889   }
890   return NULL;
891 }
892
893 #define MAX_STACK_SIZE  100
894 static struct item_stack {
895   struct cf_section *sec;       // nested section
896   void *base_ptr;               // because original pointers are often relative
897   enum cf_operation op;         // it is performed when a closing brace is encountered
898   void *list;                   // list the operations should be done on
899   u32 mask;                     // bit array of selectors searching in a list
900   struct cf_item *item;         // cf_item of the list
901 } stack[MAX_STACK_SIZE];
902 static uns level;
903
904 static byte *
905 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
906 {
907   if (level >= MAX_STACK_SIZE-1)
908     return "Too many nested sections";
909   stack[++level] = (struct item_stack) {
910     .sec = NULL,
911     .base_ptr = NULL,
912     .op = op & OP_MASK,
913     .list = NULL,
914     .mask = 0,
915     .item = NULL,
916   };
917   if (!item)                    // unknown is ignored; we just need to trace recursion
918     return NULL;
919   stack[level].sec = item->u.sec;
920   if (item->cls == CC_SECTION)
921   {
922     stack[level].base_ptr = ptr;
923     stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
924   }
925   else if (item->cls == CC_LIST)
926   {
927     stack[level].base_ptr = cf_malloc(item->u.sec->size);
928     cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
929     stack[level].list = ptr;
930     stack[level].item = item;
931     if ((op & OP_MASK) < OP_REMOVE) {
932       add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
933       stack[level].op |= OP_2ND;
934     } else
935       stack[level].op |= OP_1ST;
936   }
937   else
938     return "Opening brace can only be used on sections and lists";
939   return NULL;
940 }
941
942 static byte *
943 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
944 {
945   if (st->op == OP_CLOSE)       // top-level
946     return "Unmatched } parenthesis";
947   if (!st->sec) {               // dummy run on unknown section
948     if (!(op & OP_OPEN))
949       level--;
950     return NULL;
951   }
952   enum cf_operation pure_op = st->op & OP_MASK;
953   if (st->op & OP_1ST)
954   {
955     st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
956     if (!st->list)
957       return "Cannot find a node matching the query";
958     if (pure_op != OP_REMOVE)
959     {
960       if (pure_op == OP_EDIT)
961         st->base_ptr = st->list;
962       else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
963         cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
964       else
965         ASSERT(0);
966       if (op & OP_OPEN) {       // stay at the same recursion level
967         st->op = (st->op | OP_2ND) & ~OP_1ST;
968         add_to_list(st->list, st->base_ptr, pure_op);
969         return NULL;
970       }
971       int taken;                // parse parameters on 1 line immediately
972       TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
973       number -= taken;
974       pars += taken;
975       // and fall-thru to the 2nd phase
976     }
977     add_to_list(st->list, st->base_ptr, pure_op);
978   }
979   level--;
980   if (number)
981     return "No parameters expected after the }";
982   else if (op & OP_OPEN)
983     return "No { is expected";
984   else
985     return NULL;
986 }
987
988 static byte *
989 interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
990 {
991   byte *msg;
992   if ((op & OP_MASK) == OP_CLOSE)
993     return closing_brace(stack+level, op, number, pars);
994   void *ptr = stack[level].base_ptr;
995   struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
996   if (msg)
997     return msg;
998   if (stack[level].op & OP_1ST)
999     TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
1000   if (op & OP_OPEN) {           // the operation will be performed after the closing brace
1001     if (number)
1002       return "Cannot open a block after a parameter has been passed on a line";
1003     return opening_brace(item, ptr, op);
1004   }
1005   if (!item)                    // ignored item in an unknown section
1006     return NULL;
1007   op &= OP_MASK;
1008
1009   int taken;                    // process as many parameters as possible
1010   if (op == OP_CLEAR)
1011     taken = 0, msg = interpret_clear(item, ptr);
1012   else if (op == OP_SET)
1013     msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
1014   else if (item->cls == CC_DYNAMIC)
1015     msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
1016   else if (item->cls == CC_LIST)
1017     msg = interpret_add_list(item, number, pars, &taken, ptr, op);
1018   else
1019     return cf_printf("Operation %s not supported on attribute %s", op_names[op], name);
1020   if (msg)
1021     return msg;
1022   if (taken < number)
1023     return cf_printf("Too many parameters: %d>%d", number, taken);
1024
1025   return NULL;
1026 }
1027
1028 byte *
1029 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
1030 {
1031   byte *msg;
1032   int taken;
1033   switch (op) {
1034     case OP_SET:
1035       msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
1036       break;
1037     case OP_CLEAR:
1038       taken = 0;
1039       msg = interpret_clear(item, item->ptr);
1040       break;
1041     case OP_APPEND:
1042     case OP_PREPEND:
1043       if (item->cls == CC_DYNAMIC)
1044         msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
1045       else if (item->cls == CC_LIST)
1046         msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
1047       else
1048         return "The attribute class does not support append/prepend";
1049       break;
1050     default:
1051       return "Unsupported operation";
1052   }
1053   if (msg)
1054     return msg;
1055   if (taken < number)
1056     return "Too many parameters";
1057   return NULL;
1058 }
1059
1060 static void
1061 init_stack(void)
1062 {
1063   global_init();
1064   level = 0;
1065   stack[0] = (struct item_stack) {
1066     .sec = &sections,
1067     .base_ptr = NULL,
1068     .op = OP_CLOSE,
1069     .list = NULL,
1070     .mask = 0,
1071     .item = NULL
1072   };
1073 }
1074
1075 static int
1076 done_stack(void)
1077 {
1078   if (level > 0) {
1079     log(L_ERROR, "Unterminated block");
1080     return 1;
1081   }
1082   sort_dirty();
1083   if (commit_section("top-level", &sections, NULL, !everything_committed))
1084     return 1;
1085   everything_committed = 1;
1086   dirties = 0;
1087   return 0;
1088 }
1089
1090 /* Text file parser */
1091
1092 static byte *name_parse_fb;
1093 static struct fastbuf *parse_fb;
1094 static uns line_num;
1095
1096 #define MAX_LINE        4096
1097 static byte line_buf[MAX_LINE];
1098 static byte *line = line_buf;
1099
1100 #include "lib/bbuf.h"
1101 static bb_t copy_buf;
1102 static uns copied;
1103
1104 #define GBUF_TYPE       uns
1105 #define GBUF_PREFIX(x)  split_##x
1106 #include "lib/gbuf.h"
1107 static split_t word_buf;
1108 static uns words;
1109 static uns ends_by_brace;               // the line is ended by "{"
1110
1111 static int
1112 get_line(void)
1113 {
1114   if (!bgets(parse_fb, line_buf, MAX_LINE))
1115     return 0;
1116   line_num++;
1117   line = line_buf;
1118   while (Cblank(*line))
1119     line++;
1120   return 1;
1121 }
1122
1123 static void
1124 append(byte *start, byte *end)
1125 {
1126   uns len = end - start;
1127   bb_grow(&copy_buf, copied + len + 1);
1128   memcpy(copy_buf.ptr + copied, start, len);
1129   copied += len + 1;
1130   copy_buf.ptr[copied-1] = 0;
1131 }
1132
1133 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1134   // these characters separate words like blanks
1135
1136 static byte *
1137 get_word(uns is_command_name)
1138 {
1139   if (*line == '\'') {
1140     line++;
1141     while (1) {
1142       byte *start = line;
1143       while (*line && *line != '\'')
1144         line++;
1145       append(start, line);
1146       if (*line)
1147         break;
1148       copy_buf.ptr[copied-1] = '\n';
1149       if (!get_line())
1150         return "Unterminated apostrophe word at the end";
1151     }
1152     line++;
1153
1154   } else if (*line == '"') {
1155     line++;
1156     uns start_copy = copied;
1157     while (1) {
1158       byte *start = line;
1159       uns escape = 0;
1160       while (*line) {
1161         if (*line == '"' && !escape)
1162           break;
1163         else if (*line == '\\')
1164           escape ^= 1;
1165         else
1166           escape = 0;
1167         line++;
1168       }
1169       append(start, line);
1170       if (*line)
1171         break;
1172       if (!escape)
1173         copy_buf.ptr[copied-1] = '\n';
1174       else // merge two lines
1175         copied -= 2;
1176       if (!get_line())
1177         return "Unterminated quoted word at the end";
1178     }
1179     line++;
1180
1181     byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
1182     uns l = strlen(tmp);
1183     bb_grow(&copy_buf, start_copy + l + 1);
1184     strcpy(copy_buf.ptr + start_copy, tmp);
1185     copied = start_copy + l + 1;
1186
1187   } else {
1188     // promised that *line is non-null and non-blank
1189     byte *start = line;
1190     while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1191         && (*line != '=' || !is_command_name))
1192       line++;
1193     if (*line == '=') {                         // nice for setting from a command-line
1194       if (line == start)
1195         return "Assignment without a variable";
1196       *line = ' ';
1197     }
1198     if (line == start)                          // already the first char is control
1199       line++;
1200     append(start, line);
1201   }
1202   while (Cblank(*line))
1203     line++;
1204   return NULL;
1205 }
1206
1207 static byte *
1208 get_token(uns is_command_name, byte **msg)
1209 {
1210   *msg = NULL;
1211   while (1) {
1212     if (!*line || *line == '#') {
1213       if (!is_command_name || !get_line())
1214         return NULL;
1215     } else if (*line == ';') {
1216       *msg = get_word(0);
1217       if (!is_command_name || *msg)
1218         return NULL;
1219     } else if (*line == '\\' && !line[1]) {
1220       if (!get_line()) {
1221         *msg = "Last line ends by a backslash";
1222         return NULL;
1223       }
1224       if (!*line || *line == '#')
1225         log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
1226     } else {
1227       split_grow(&word_buf, words+1);
1228       uns start = copied;
1229       word_buf.ptr[words++] = copied;
1230       *msg = get_word(is_command_name);
1231       return *msg ? NULL : copy_buf.ptr + start;
1232     }
1233   }
1234 }
1235
1236 static byte *
1237 split_command(void)
1238 {
1239   words = copied = ends_by_brace = 0;
1240   byte *msg, *start_word;
1241   if (!(start_word = get_token(1, &msg)))
1242     return msg;
1243   if (*start_word == '{')                       // only one opening brace
1244     return "Unexpected opening brace";
1245   while (*line != '}')                          // stays for the next time
1246   {
1247     if (!(start_word = get_token(0, &msg)))
1248       return msg;
1249     if (*start_word == '{') {
1250       words--;                                  // discard the brace
1251       ends_by_brace = 1;
1252       break;
1253     }
1254   }
1255   return NULL;
1256 }
1257
1258 /* Parsing multiple files */
1259
1260 static byte *
1261 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1262 {
1263   byte *msg;
1264   name_parse_fb = name_fb;
1265   parse_fb = fb;
1266   line_num = 0;
1267   line = line_buf;
1268   *line = 0;
1269   while (1)
1270   {
1271     msg = split_command();
1272     if (msg)
1273       goto error;
1274     if (!words)
1275       return NULL;
1276     byte *name = copy_buf.ptr + word_buf.ptr[0];
1277     byte *pars[words-1];
1278     for (uns i=1; i<words; i++)
1279       pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1280     if (!strcasecmp(name, "include"))
1281     {
1282       if (words != 2)
1283         msg = "Expecting one filename";
1284       else if (depth > 8)
1285         msg = "Too many nested files";
1286       else if (*line && *line != '#')           // because the contents of line_buf is not re-entrant and will be cleared
1287         msg = "The input command must be the last one on a line";
1288       if (msg)
1289         goto error;
1290       struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
1291       if (!new_fb) {
1292         msg = cf_printf("Cannot open file %s: %m", pars[0]);
1293         goto error;
1294       }
1295       uns ll = line_num;
1296       msg = parse_fastbuf(pars[0], new_fb, depth+1);
1297       bclose(new_fb);
1298       if (msg)
1299         goto error;
1300       line_num = ll;
1301       parse_fb = fb;
1302     }
1303     enum cf_operation op;
1304     byte *c = strchr(name, ':');
1305     if (!c)
1306       op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1307     else {
1308       *c++ = 0;
1309       switch (Clocase(*c)) {
1310         case 's': op = OP_SET; break;
1311         case 'c': op = OP_CLEAR; break;
1312         case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1313         case 'p': op = OP_PREPEND; break;
1314         case 'r': op = OP_REMOVE; break;
1315         case 'e': op = OP_EDIT; break;
1316         case 'b': op = OP_BEFORE; break;
1317         default: op = OP_SET; break;
1318       }
1319       if (strcasecmp(c, op_names[op])) {
1320         msg = cf_printf("Unknown operation %s", c);
1321         goto error;
1322       }
1323     }
1324     if (ends_by_brace)
1325       op |= OP_OPEN;
1326     msg = interpret_line(name, op, words-1, pars);
1327     if (msg)
1328       goto error;
1329   }
1330 error:
1331   log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1332   return "included from here";
1333 }
1334
1335 #undef DEFAULT_CONFIG                   /* FIXME */
1336 #define DEFAULT_CONFIG "cf/sherlock2"
1337
1338 #ifndef DEFAULT_CONFIG
1339 #define DEFAULT_CONFIG NULL
1340 #endif
1341 byte *cf_def_file = DEFAULT_CONFIG;
1342
1343 static int
1344 load_file(byte *file)
1345 {
1346   init_stack();
1347   struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
1348   if (!fb) {
1349     log(L_ERROR, "Cannot open %s: %m", file);
1350     return 1;
1351   }
1352   byte *msg = parse_fastbuf(file, fb, 0);
1353   bclose(fb);
1354   int err = !!msg || done_stack();
1355   if (!err)
1356     cf_def_file = NULL;
1357   return err;
1358 }
1359
1360 static int
1361 load_string(byte *string)
1362 {
1363   init_stack();
1364   struct fastbuf fb;
1365   fbbuf_init_read(&fb, string, strlen(string), 0);
1366   byte *msg = parse_fastbuf("memory string", &fb, 0);
1367   return !!msg || done_stack();
1368 }
1369
1370 /* Command-line parser */
1371
1372 static void
1373 load_default(void)
1374 {
1375   if (cf_def_file)
1376     if (cf_load(cf_def_file))
1377       die("Cannot load default config %s", cf_def_file);
1378 }
1379
1380 int
1381 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1382 {
1383   static int other_options = 0;
1384   while (1) {
1385     int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1386     if (res == 'S' || res == 'C')
1387     {
1388       if (other_options)
1389         die("The -S and -C options must precede all other arguments");
1390       if (res == 'S') {
1391         load_default();
1392         if (cf_set(optarg))
1393           die("Cannot set %s", optarg);
1394       } else {
1395         if (cf_load(optarg))
1396           die("Cannot load config file %s", optarg);
1397       }
1398     } else {
1399       /* unhandled option or end of options */
1400       if (res != ':' && res != '?')
1401         load_default();
1402       other_options++;
1403       return res;
1404     }
1405   }
1406 }
1407
1408 /* Debug dumping */
1409
1410 static void
1411 spaces(struct fastbuf *fb, uns nr)
1412 {
1413   for (uns i=0; i<nr; i++)
1414     bputs(fb, "  ");
1415 }
1416
1417 static void
1418 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type, union cf_union *u)
1419 {
1420   switch (type) {
1421     case CT_INT:        bprintf(fb, "%d ", *(uns*)ptr); break;
1422     case CT_U64:        bprintf(fb, "%llu ", *(u64*)ptr); break;
1423     case CT_DOUBLE:     bprintf(fb, "%lg ", *(double*)ptr); break;
1424     case CT_IP:         bprintf(fb, "%08x ", *(uns*)ptr); break;
1425     case CT_STRING:     bprintf(fb, "'%s' ", *(byte**)ptr); break;
1426     case CT_LOOKUP:     bprintf(fb, "%s ", *(int*)ptr >= 0 ? u->lookup[ *(int*)ptr ] : "???"); break;
1427     case CT_USER:
1428       if (u->utype->dumper)
1429         u->utype->dumper(fb, ptr);
1430       else
1431         bprintf(fb, "??? ");
1432       break;
1433   }
1434 }
1435
1436 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1437
1438 static void
1439 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1440 {
1441   ptr += (addr_int_t) item->ptr;
1442   enum cf_type type = item->type;
1443   uns size = type_size(item->type, item->u.utype);
1444   int i;
1445   spaces(fb, level);
1446   bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1447   if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1448     bprintf(fb, "t%d ", type);
1449   if (item->cls == CC_STATIC) {
1450     if (item->type == CT_USER)
1451       bprintf(fb, "S%d ", size);
1452     for (i=0; i<item->number; i++)
1453       dump_basic(fb, ptr + i * size, type, &item->u);
1454   } else if (item->cls == CC_DYNAMIC) {
1455     if (item->type == CT_USER)
1456       bprintf(fb, "S%d ", size);
1457     ptr = * (void**) ptr;
1458     if (ptr) {
1459       int real_nr = * (int*) (ptr - size);
1460       bprintf(fb, "##%d ", real_nr);
1461       for (i=0; i<real_nr; i++)
1462         dump_basic(fb, ptr + i * size, type, &item->u);
1463     } else
1464       bprintf(fb, "NULL ");
1465   }
1466   bputc(fb, '\n');
1467   if (item->cls == CC_SECTION)
1468     dump_section(fb, item->u.sec, level+1, ptr);
1469   else if (item->cls == CC_LIST) {
1470     uns idx = 0;
1471     struct cnode *n;
1472     CLIST_WALK(n, * (clist*) ptr) {
1473       spaces(fb, level+1);
1474       bprintf(fb, "item %d\n", ++idx);
1475       dump_section(fb, item->u.sec, level+2, n);
1476     }
1477   }
1478 }
1479
1480 static void
1481 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1482 {
1483   spaces(fb, level);
1484   bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1485   for (struct cf_item *item=sec->cfg; item->cls; item++)
1486     dump_item(fb, item, level, ptr);
1487 }
1488
1489 void
1490 cf_dump_sections(struct fastbuf *fb)
1491 {
1492   dump_section(fb, &sections, 0, NULL);
1493 }
1494
1495 /* TODO
1496  * - more space efficient journal
1497  */