]> mj.ucw.cz Git - libucw.git/blob - lib/conf2.c
conf2: dynamic arrays allow negative maximal length
[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   *ptr = -1;
611   return err;
612 }
613
614 /* Register size of and parser for each basic type */
615
616 typedef byte *cf_basic_parser(byte *str, void *ptr);
617 static struct {
618   uns size;
619   void *parser;
620 } parsers[] = {
621   { sizeof(int), cf_parse_int },
622   { sizeof(u64), cf_parse_u64 },
623   { sizeof(double), cf_parse_double },
624   { sizeof(u32), cf_parse_ip },
625   { sizeof(byte*), cf_parse_string },
626   { sizeof(int), NULL },                        // lookups are parsed extra
627   { 0, NULL },                                  // user-defined types are parsed extra
628 };
629
630 static inline uns
631 type_size(enum cf_type type, struct cf_user_type *utype)
632 {
633   if (type < CT_USER)
634     return parsers[type].size;
635   else
636     return utype->size;
637 }
638
639 static byte *
640 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_union *u)
641 {
642   for (uns i=0; i<number; i++)
643   {
644     byte *msg;
645     uns size = type_size(type, u->utype);
646     if (type < CT_LOOKUP)
647       msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * size);
648     else if (type == CT_LOOKUP)
649       msg = cf_parse_lookup(pars[i], ptr + i * size, u->lookup);
650     else if (type == CT_USER)
651       msg = u->utype->parser(pars[i], ptr + i * size);
652     else
653       ASSERT(0);
654     if (msg)
655       return cf_printf("Cannot parse item %d: %s", i+1, msg);
656   }
657   return NULL;
658 }
659
660 /* Interpreter */
661
662 #define T(x) #x,
663 static byte *op_names[] = { CF_OPERATIONS };
664 #undef T
665
666 #define OP_MASK 0xff            // only get the operation
667 #define OP_OPEN 0x100           // here we only get an opening brace instead of parameters
668 #define OP_1ST 0x200            // in the 1st phase selectors are recorded into the mask
669 #define OP_2ND 0x400            // in the 2nd phase real data are entered
670
671 static byte *
672 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
673 {
674   enum cf_type type = item->type;
675   cf_journal_block(ptr, sizeof(void*));
676   // boundary checks done by the caller
677   uns size = type_size(item->type, item->u.utype);
678   ASSERT(size >= sizeof(uns));
679   *ptr = cf_malloc((number+1) * size) + size;
680   * (uns*) (*ptr - size) = number;
681   return cf_parse_ary(number, pars, *ptr, type, &item->u);
682 }
683
684 static byte *
685 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
686 {
687   enum cf_type type = item->type;
688   void *old_p = *ptr;
689   uns size = type_size(item->type, item->u.utype);
690   ASSERT(size >= sizeof(uns));
691   int old_nr = * (int*) (old_p - size);
692   int taken = MIN(number, ABS(item->number)-old_nr);
693   *processed = taken;
694   // stretch the dynamic array
695   void *new_p = cf_malloc((old_nr + taken + 1) * size) + size;
696   * (uns*) (new_p - size) = old_nr + taken;
697   cf_journal_block(ptr, sizeof(void*));
698   *ptr = new_p;
699   if (op == OP_APPEND) {
700     memcpy(new_p, old_p, old_nr * size);
701     return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
702   } else if (op == OP_PREPEND) {
703     memcpy(new_p + taken * size, old_p, old_nr * size);
704     return cf_parse_ary(taken, pars, new_p, type, &item->u);
705   } else
706     return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
707 }
708
709 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
710
711 static byte *
712 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
713 {
714   add_dirty(sec, ptr);
715   *processed = 0;
716   for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
717   {
718     int taken;
719     byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
720     if (msg)
721       return cf_printf("Item %s: %s", ci->name, msg);
722     *processed += taken;
723     number -= taken;
724     pars += taken;
725     if (!number)                // stop parsing, because many parsers would otherwise complain that number==0
726       break;
727   }
728   return NULL;
729 }
730
731 static void
732 add_to_list(struct cnode *where, struct cnode *new_node, enum cf_operation op)
733 {
734   switch (op)
735   {
736     case OP_EDIT:               // edition has been done in-place
737       break;
738     case OP_REMOVE:
739       cf_journal_block(&where->prev->next, sizeof(void*));
740       cf_journal_block(&where->next->prev, sizeof(void*));
741       clist_remove(where);
742       break;
743     case OP_AFTER:              // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
744     case OP_PREPEND:
745       cf_journal_block(&where->next->prev, sizeof(void*));
746       cf_journal_block(&where->next, sizeof(void*));
747       clist_insert_after(new_node, where);
748       break;
749     case OP_BEFORE:             // implementation dependend (append_tail = before(list))
750     case OP_APPEND:
751     case OP_SET:
752       cf_journal_block(&where->prev->next, sizeof(void*));
753       cf_journal_block(&where->prev, sizeof(void*));
754       clist_insert_before(new_node, where);
755       break;
756     default:
757       ASSERT(0);
758   }
759 }
760
761 static byte *
762 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
763 {
764   if (op >= OP_REMOVE)
765     return cf_printf("You have to open a block for operation %s", op_names[op]);
766   if (!number)
767     return "Nothing to add to the list";
768   struct cf_section *sec = item->u.sec;
769   *processed = 0;
770   while (number > 0)
771   {
772     void *node = cf_malloc(sec->size);
773     cf_init_section(item->name, sec, node, 1);
774     add_to_list(ptr, node, op);
775     int taken;
776     /* If the node contains any dynamic attribute at the end, we suppress
777      * auto-repetition here and pass the flag inside instead.  */
778     TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
779     *processed += taken;
780     number -= taken;
781     pars += taken;
782     if (sec->flags & SEC_FLAG_DYNAMIC)
783       break;
784   }
785   return NULL;
786 }
787
788 static byte *
789 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
790 {
791   int taken;
792   switch (item->cls)
793   {
794     case CC_STATIC:
795       if (!number)
796         return "Missing value";
797       taken = MIN(number, item->number);
798       *processed = taken;
799       uns size = type_size(item->type, item->u.utype);
800       cf_journal_block(ptr, taken * size);
801       return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
802     case CC_DYNAMIC:
803       if (!allow_dynamic)
804         return "Dynamic array cannot be used here";
805       taken = MIN(number, ABS(item->number));
806       *processed = taken;
807       return interpret_set_dynamic(item, taken, pars, ptr);
808     case CC_PARSER:
809       if (item->number < 0 && !allow_dynamic)
810         return "Parsers with variable number of parameters cannot be used here";
811       if (item->number > 0 && number < item->number)
812         return "Not enough parameters available for the parser";
813       taken = MIN(number, ABS(item->number));
814       *processed = taken;
815       for (int i=0; i<taken; i++)
816         pars[i] = cf_strdup(pars[i]);
817       return item->u.par(taken, pars, ptr);
818     case CC_SECTION:
819       return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
820     case CC_LIST:
821       if (!allow_dynamic)
822         return "Lists cannot be used here";
823       return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
824     default:
825       ASSERT(0);
826   }
827 }
828
829 static byte *
830 interpret_clear(struct cf_item *item, void *ptr)
831 {
832   if (item->cls == CC_LIST) {
833     cf_journal_block(ptr, sizeof(struct clist));
834     clist_init(ptr);
835   } else if (item->cls == CC_DYNAMIC) {
836     cf_journal_block(ptr, sizeof(void *));
837     * (void**) ptr = NULL;
838   } else
839     return "The item is not a list or a dynamic array";
840   return NULL;
841 }
842
843 static int
844 cmp_items(void *i1, void *i2, struct cf_item *item)
845 {
846   ASSERT(item->cls == CC_STATIC);
847   i1 += (addr_int_t) item->ptr;
848   i2 += (addr_int_t) item->ptr;
849   if (item->type == CT_STRING)
850     return strcmp(* (byte**) i1, * (byte**) i2);
851   else                          // all numeric types
852     return memcmp(i1, i2, type_size(item->type, item->u.utype));
853 }
854
855 static void *
856 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
857 {
858   struct cnode *n;
859   CLIST_WALK(n, *list)
860   {
861     uns found = 1;
862     for (uns i=0; i<32; i++)
863       if (mask & (1<<i))
864         if (cmp_items(n, query, sec->cfg+i))
865         {
866           found = 0;
867           break;
868         }
869     if (found)
870       return n;
871   }
872   return NULL;
873 }
874
875 static byte *
876 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
877 {
878   uns nr = sec->flags & SEC_FLAG_NUMBER;
879   if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
880   {
881     uns i = item - sec->cfg;
882     if (i >= 32)
883       return "Cannot select list nodes by this attribute";
884     if (sec->cfg[i].cls != CC_STATIC)
885       return "Selection can only be done based on basic attributes";
886     *mask |= 1 << i;
887   }
888   return NULL;
889 }
890
891 #define MAX_STACK_SIZE  100
892 static struct item_stack {
893   struct cf_section *sec;       // nested section
894   void *base_ptr;               // because original pointers are often relative
895   enum cf_operation op;         // it is performed when a closing brace is encountered
896   void *list;                   // list the operations should be done on
897   u32 mask;                     // bit array of selectors searching in a list
898   struct cf_item *item;         // cf_item of the list
899 } stack[MAX_STACK_SIZE];
900 static uns level;
901
902 static byte *
903 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
904 {
905   if (level >= MAX_STACK_SIZE-1)
906     return "Too many nested sections";
907   stack[++level] = (struct item_stack) {
908     .sec = NULL,
909     .base_ptr = NULL,
910     .op = op & OP_MASK,
911     .list = NULL,
912     .mask = 0,
913     .item = NULL,
914   };
915   if (!item)                    // unknown is ignored; we just need to trace recursion
916     return NULL;
917   stack[level].sec = item->u.sec;
918   if (item->cls == CC_SECTION)
919   {
920     stack[level].base_ptr = ptr;
921     stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
922   }
923   else if (item->cls == CC_LIST)
924   {
925     stack[level].base_ptr = cf_malloc(item->u.sec->size);
926     cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
927     stack[level].list = ptr;
928     stack[level].item = item;
929     if ((op & OP_MASK) < OP_REMOVE) {
930       add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
931       stack[level].op |= OP_2ND;
932     } else
933       stack[level].op |= OP_1ST;
934   }
935   else
936     return "Opening brace can only be used on sections and lists";
937   return NULL;
938 }
939
940 static byte *
941 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
942 {
943   if (st->op == OP_CLOSE)       // top-level
944     return "Unmatched } parenthesis";
945   if (!st->sec) {               // dummy run on unknown section
946     if (!(op & OP_OPEN))
947       level--;
948     return NULL;
949   }
950   enum cf_operation pure_op = st->op & OP_MASK;
951   if (st->op & OP_1ST)
952   {
953     st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
954     if (!st->list)
955       return "Cannot find a node matching the query";
956     if (pure_op != OP_REMOVE)
957     {
958       if (pure_op == OP_EDIT)
959         st->base_ptr = st->list;
960       else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
961         cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
962       else
963         ASSERT(0);
964       if (op & OP_OPEN) {       // stay at the same recursion level
965         st->op = (st->op | OP_2ND) & ~OP_1ST;
966         add_to_list(st->list, st->base_ptr, pure_op);
967         return NULL;
968       }
969       int taken;                // parse parameters on 1 line immediately
970       TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
971       number -= taken;
972       pars += taken;
973       // and fall-thru to the 2nd phase
974     }
975     add_to_list(st->list, st->base_ptr, pure_op);
976   }
977   level--;
978   if (number)
979     return "No parameters expected after the }";
980   else if (op & OP_OPEN)
981     return "No { is expected";
982   else
983     return NULL;
984 }
985
986 static byte *
987 interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
988 {
989   byte *msg;
990   if ((op & OP_MASK) == OP_CLOSE)
991     return closing_brace(stack+level, op, number, pars);
992   void *ptr = stack[level].base_ptr;
993   struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
994   if (msg)
995     return msg;
996   if (stack[level].op & OP_1ST)
997     TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
998   if (op & OP_OPEN) {           // the operation will be performed after the closing brace
999     if (number)
1000       return "Cannot open a block after a parameter has been passed on a line";
1001     return opening_brace(item, ptr, op);
1002   }
1003   if (!item)                    // ignored item in an unknown section
1004     return NULL;
1005   op &= OP_MASK;
1006
1007   int taken;                    // process as many parameters as possible
1008   if (op == OP_CLEAR)
1009     taken = 0, msg = interpret_clear(item, ptr);
1010   else if (op == OP_SET)
1011     msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
1012   else if (item->cls == CC_DYNAMIC)
1013     msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
1014   else if (item->cls == CC_LIST)
1015     msg = interpret_add_list(item, number, pars, &taken, ptr, op);
1016   else
1017     return cf_printf("Operation %s not supported on attribute %s", op_names[op], name);
1018   if (msg)
1019     return msg;
1020   if (taken < number)
1021     return cf_printf("Too many parameters: %d>%d", number, taken);
1022
1023   return NULL;
1024 }
1025
1026 byte *
1027 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
1028 {
1029   byte *msg;
1030   int taken;
1031   switch (op) {
1032     case OP_SET:
1033       msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
1034       break;
1035     case OP_CLEAR:
1036       taken = 0;
1037       msg = interpret_clear(item, item->ptr);
1038       break;
1039     case OP_APPEND:
1040     case OP_PREPEND:
1041       if (item->cls == CC_DYNAMIC)
1042         msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
1043       else if (item->cls == CC_LIST)
1044         msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
1045       else
1046         return "The attribute class does not support append/prepend";
1047       break;
1048     default:
1049       return "Unsupported operation";
1050   }
1051   if (msg)
1052     return msg;
1053   if (taken < number)
1054     return "Too many parameters";
1055   return NULL;
1056 }
1057
1058 static void
1059 init_stack(void)
1060 {
1061   global_init();
1062   level = 0;
1063   stack[0] = (struct item_stack) {
1064     .sec = &sections,
1065     .base_ptr = NULL,
1066     .op = OP_CLOSE,
1067     .list = NULL,
1068     .mask = 0,
1069     .item = NULL
1070   };
1071 }
1072
1073 static int
1074 done_stack(void)
1075 {
1076   if (level > 0) {
1077     log(L_ERROR, "Unterminated block");
1078     return 1;
1079   }
1080   sort_dirty();
1081   if (commit_section("top-level", &sections, NULL, !everything_committed))
1082     return 1;
1083   everything_committed = 1;
1084   dirties = 0;
1085   return 0;
1086 }
1087
1088 /* Text file parser */
1089
1090 static byte *name_parse_fb;
1091 static struct fastbuf *parse_fb;
1092 static uns line_num;
1093
1094 #define MAX_LINE        4096
1095 static byte line_buf[MAX_LINE];
1096 static byte *line = line_buf;
1097
1098 #include "lib/bbuf.h"
1099 static bb_t copy_buf;
1100 static uns copied;
1101
1102 #define GBUF_TYPE       uns
1103 #define GBUF_PREFIX(x)  split_##x
1104 #include "lib/gbuf.h"
1105 static split_t word_buf;
1106 static uns words;
1107 static uns ends_by_brace;               // the line is ended by "{"
1108
1109 static int
1110 get_line(void)
1111 {
1112   if (!bgets(parse_fb, line_buf, MAX_LINE))
1113     return 0;
1114   line_num++;
1115   line = line_buf;
1116   while (Cblank(*line))
1117     line++;
1118   return 1;
1119 }
1120
1121 static void
1122 append(byte *start, byte *end)
1123 {
1124   uns len = end - start;
1125   bb_grow(&copy_buf, copied + len + 1);
1126   memcpy(copy_buf.ptr + copied, start, len);
1127   copied += len + 1;
1128   copy_buf.ptr[copied-1] = 0;
1129 }
1130
1131 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1132   // these characters separate words like blanks
1133
1134 static byte *
1135 get_word(uns is_command_name)
1136 {
1137   if (*line == '\'') {
1138     line++;
1139     while (1) {
1140       byte *start = line;
1141       while (*line && *line != '\'')
1142         line++;
1143       append(start, line);
1144       if (*line)
1145         break;
1146       copy_buf.ptr[copied-1] = '\n';
1147       if (!get_line())
1148         return "Unterminated apostrophe word at the end";
1149     }
1150     line++;
1151
1152   } else if (*line == '"') {
1153     line++;
1154     uns start_copy = copied;
1155     while (1) {
1156       byte *start = line;
1157       uns escape = 0;
1158       while (*line) {
1159         if (*line == '"' && !escape)
1160           break;
1161         else if (*line == '\\')
1162           escape ^= 1;
1163         else
1164           escape = 0;
1165         line++;
1166       }
1167       append(start, line);
1168       if (*line)
1169         break;
1170       if (!escape)
1171         copy_buf.ptr[copied-1] = '\n';
1172       else // merge two lines
1173         copied -= 2;
1174       if (!get_line())
1175         return "Unterminated quoted word at the end";
1176     }
1177     line++;
1178
1179     byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
1180     uns l = strlen(tmp);
1181     bb_grow(&copy_buf, start_copy + l + 1);
1182     strcpy(copy_buf.ptr + start_copy, tmp);
1183     copied = start_copy + l + 1;
1184
1185   } else {
1186     // promised that *line is non-null and non-blank
1187     byte *start = line;
1188     while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1189         && (*line != '=' || !is_command_name))
1190       line++;
1191     if (*line == '=') {                         // nice for setting from a command-line
1192       if (line == start)
1193         return "Assignment without a variable";
1194       *line = ' ';
1195     }
1196     if (line == start)                          // already the first char is control
1197       line++;
1198     append(start, line);
1199   }
1200   while (Cblank(*line))
1201     line++;
1202   return NULL;
1203 }
1204
1205 static byte *
1206 get_token(uns is_command_name, byte **msg)
1207 {
1208   *msg = NULL;
1209   while (1) {
1210     if (!*line || *line == '#') {
1211       if (!is_command_name || !get_line())
1212         return NULL;
1213     } else if (*line == ';') {
1214       *msg = get_word(0);
1215       if (!is_command_name || *msg)
1216         return NULL;
1217     } else if (*line == '\\' && !line[1]) {
1218       if (!get_line()) {
1219         *msg = "Last line ends by a backslash";
1220         return NULL;
1221       }
1222       if (!*line || *line == '#')
1223         log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
1224     } else {
1225       split_grow(&word_buf, words+1);
1226       uns start = copied;
1227       word_buf.ptr[words++] = copied;
1228       *msg = get_word(is_command_name);
1229       return *msg ? NULL : copy_buf.ptr + start;
1230     }
1231   }
1232 }
1233
1234 static byte *
1235 split_command(void)
1236 {
1237   words = copied = ends_by_brace = 0;
1238   byte *msg, *start_word;
1239   if (!(start_word = get_token(1, &msg)))
1240     return msg;
1241   if (*start_word == '{')                       // only one opening brace
1242     return "Unexpected opening brace";
1243   while (*line != '}')                          // stays for the next time
1244   {
1245     if (!(start_word = get_token(0, &msg)))
1246       return msg;
1247     if (*start_word == '{') {
1248       words--;                                  // discard the brace
1249       ends_by_brace = 1;
1250       break;
1251     }
1252   }
1253   return NULL;
1254 }
1255
1256 /* Parsing multiple files */
1257
1258 static byte *
1259 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1260 {
1261   byte *msg;
1262   name_parse_fb = name_fb;
1263   parse_fb = fb;
1264   line_num = 0;
1265   line = line_buf;
1266   *line = 0;
1267   while (1)
1268   {
1269     msg = split_command();
1270     if (msg)
1271       goto error;
1272     if (!words)
1273       return NULL;
1274     byte *name = copy_buf.ptr + word_buf.ptr[0];
1275     byte *pars[words-1];
1276     for (uns i=1; i<words; i++)
1277       pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1278     if (!strcasecmp(name, "include"))
1279     {
1280       if (words != 2)
1281         msg = "Expecting one filename";
1282       else if (depth > 8)
1283         msg = "Too many nested files";
1284       else if (*line && *line != '#')           // because the contents of line_buf is not re-entrant and will be cleared
1285         msg = "The input command must be the last one on a line";
1286       if (msg)
1287         goto error;
1288       struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
1289       if (!new_fb) {
1290         msg = cf_printf("Cannot open file %s: %m", pars[0]);
1291         goto error;
1292       }
1293       uns ll = line_num;
1294       msg = parse_fastbuf(pars[0], new_fb, depth+1);
1295       bclose(new_fb);
1296       if (msg)
1297         goto error;
1298       line_num = ll;
1299       parse_fb = fb;
1300     }
1301     enum cf_operation op;
1302     byte *c = strchr(name, ':');
1303     if (!c)
1304       op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1305     else {
1306       *c++ = 0;
1307       switch (Clocase(*c)) {
1308         case 's': op = OP_SET; break;
1309         case 'c': op = OP_CLEAR; break;
1310         case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1311         case 'p': op = OP_PREPEND; break;
1312         case 'r': op = OP_REMOVE; break;
1313         case 'e': op = OP_EDIT; break;
1314         case 'b': op = OP_BEFORE; break;
1315         default: op = OP_SET; break;
1316       }
1317       if (strcasecmp(c, op_names[op])) {
1318         msg = cf_printf("Unknown operation %s", c);
1319         goto error;
1320       }
1321     }
1322     if (ends_by_brace)
1323       op |= OP_OPEN;
1324     msg = interpret_line(name, op, words-1, pars);
1325     if (msg)
1326       goto error;
1327   }
1328 error:
1329   log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1330   return "included from here";
1331 }
1332
1333 #undef DEFAULT_CONFIG                   /* FIXME */
1334 #define DEFAULT_CONFIG "cf/sherlock2"
1335
1336 #ifndef DEFAULT_CONFIG
1337 #define DEFAULT_CONFIG NULL
1338 #endif
1339 byte *cf_def_file = DEFAULT_CONFIG;
1340
1341 static int
1342 load_file(byte *file)
1343 {
1344   init_stack();
1345   struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
1346   if (!fb) {
1347     log(L_ERROR, "Cannot open %s: %m", file);
1348     return 1;
1349   }
1350   byte *msg = parse_fastbuf(file, fb, 0);
1351   bclose(fb);
1352   int err = !!msg || done_stack();
1353   if (!err)
1354     cf_def_file = NULL;
1355   return err;
1356 }
1357
1358 static int
1359 load_string(byte *string)
1360 {
1361   init_stack();
1362   struct fastbuf fb;
1363   fbbuf_init_read(&fb, string, strlen(string), 0);
1364   byte *msg = parse_fastbuf("memory string", &fb, 0);
1365   return !!msg || done_stack();
1366 }
1367
1368 /* Command-line parser */
1369
1370 static void
1371 load_default(void)
1372 {
1373   if (cf_def_file)
1374     if (cf_load(cf_def_file))
1375       die("Cannot load default config %s", cf_def_file);
1376 }
1377
1378 int
1379 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1380 {
1381   static int other_options = 0;
1382   while (1) {
1383     int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1384     if (res == 'S' || res == 'C')
1385     {
1386       if (other_options)
1387         die("The -S and -C options must precede all other arguments");
1388       if (res == 'S') {
1389         load_default();
1390         if (cf_set(optarg))
1391           die("Cannot set %s", optarg);
1392       } else {
1393         if (cf_load(optarg))
1394           die("Cannot load config file %s", optarg);
1395       }
1396     } else {
1397       /* unhandled option or end of options */
1398       if (res != ':' && res != '?')
1399         load_default();
1400       other_options++;
1401       return res;
1402     }
1403   }
1404 }
1405
1406 /* Debug dumping */
1407
1408 static void
1409 spaces(struct fastbuf *fb, uns nr)
1410 {
1411   for (uns i=0; i<nr; i++)
1412     bputs(fb, "  ");
1413 }
1414
1415 static void
1416 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type, union cf_union *u)
1417 {
1418   switch (type) {
1419     case CT_INT:        bprintf(fb, "%d ", *(uns*)ptr); break;
1420     case CT_U64:        bprintf(fb, "%llu ", *(u64*)ptr); break;
1421     case CT_DOUBLE:     bprintf(fb, "%lg ", *(double*)ptr); break;
1422     case CT_IP:         bprintf(fb, "%08x ", *(uns*)ptr); break;
1423     case CT_STRING:     bprintf(fb, "'%s' ", *(byte**)ptr); break;
1424     case CT_LOOKUP:     bprintf(fb, "%s ", *(int*)ptr >= 0 ? u->lookup[ *(int*)ptr ] : "???"); break;
1425     case CT_USER:
1426       if (u->utype->dumper)
1427         u->utype->dumper(fb, ptr);
1428       else
1429         bprintf(fb, "??? ");
1430       break;
1431   }
1432 }
1433
1434 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1435
1436 static void
1437 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1438 {
1439   ptr += (addr_int_t) item->ptr;
1440   enum cf_type type = item->type;
1441   uns size = type_size(item->type, item->u.utype);
1442   int i;
1443   spaces(fb, level);
1444   bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1445   if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1446     bprintf(fb, "t%d ", type);
1447   if (item->cls == CC_STATIC) {
1448     if (item->type == CT_USER)
1449       bprintf(fb, "S%d ", size);
1450     for (i=0; i<item->number; i++)
1451       dump_basic(fb, ptr + i * size, type, &item->u);
1452   } else if (item->cls == CC_DYNAMIC) {
1453     if (item->type == CT_USER)
1454       bprintf(fb, "S%d ", size);
1455     ptr = * (void**) ptr;
1456     if (ptr) {
1457       int real_nr = * (int*) (ptr - size);
1458       bprintf(fb, "##%d ", real_nr);
1459       for (i=0; i<real_nr; i++)
1460         dump_basic(fb, ptr + i * size, type, &item->u);
1461     } else
1462       bprintf(fb, "NULL ");
1463   }
1464   bputc(fb, '\n');
1465   if (item->cls == CC_SECTION)
1466     dump_section(fb, item->u.sec, level+1, ptr);
1467   else if (item->cls == CC_LIST) {
1468     uns idx = 0;
1469     struct cnode *n;
1470     CLIST_WALK(n, * (clist*) ptr) {
1471       spaces(fb, level+1);
1472       bprintf(fb, "item %d\n", ++idx);
1473       dump_section(fb, item->u.sec, level+2, n);
1474     }
1475   }
1476 }
1477
1478 static void
1479 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1480 {
1481   spaces(fb, level);
1482   bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1483   for (struct cf_item *item=sec->cfg; item->cls; item++)
1484     dump_item(fb, item, level, ptr);
1485 }
1486
1487 void
1488 cf_dump_sections(struct fastbuf *fb)
1489 {
1490   dump_section(fb, &sections, 0, NULL);
1491 }
1492
1493 /* TODO
1494  * - more space efficient journal
1495  */