2 * UCW Library -- Reading of configuration files
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2006 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
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"
18 #include "lib/stkstring.h"
19 #include "lib/binsearch.h"
28 #define TRY(f) do { byte *_msg = f; if (_msg) return _msg; } while (0)
30 /* Memory allocation */
32 struct mempool *cf_pool; // current pool for loading new configuration
33 static struct old_pools {
34 struct old_pools *prev;
36 } *pools; // link-list of older cf_pool's
41 return mp_alloc(cf_pool, size);
45 cf_malloc_zero(uns size)
47 return mp_alloc_zero(cf_pool, size);
53 return mp_strdup(cf_pool, s);
57 cf_printf(char *fmt, ...)
61 byte *res = mp_vprintf(cf_pool, fmt, args);
68 uns cf_need_journal = 1; // some programs do not need journal
69 static struct cf_journal_item {
70 struct cf_journal_item *prev;
77 cf_journal_block(void *ptr, uns len)
81 struct cf_journal_item *ji = cf_malloc(sizeof(struct cf_journal_item) + len);
85 memcpy(ji->copy, ptr, len);
91 // swaps the contents of the memory and the journal, and reverses the list
93 struct cf_journal_item *curr, *prev, *next;
94 for (next=NULL, curr=journal; curr; next=curr, curr=prev)
98 for (uns i=0; i<curr->len; i++)
100 byte x = curr->copy[i];
101 curr->copy[i] = curr->ptr[i];
108 struct cf_journal_item *
109 cf_journal_new_transaction(uns new_pool)
112 cf_pool = mp_new(1<<10);
113 struct cf_journal_item *oldj = journal;
119 cf_journal_commit_transaction(uns new_pool, struct cf_journal_item *oldj)
123 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
130 struct cf_journal_item **j = &journal;
138 cf_journal_rollback_transaction(uns new_pool, struct cf_journal_item *oldj)
140 if (!cf_need_journal)
141 die("Cannot rollback the configuration, because the journal is disabled.");
147 cf_pool = pools ? pools->pool : NULL;
153 struct dirty_section {
154 struct cf_section *sec;
157 #define GBUF_TYPE struct dirty_section
158 #define GBUF_PREFIX(x) dirtsec_##x
159 #include "lib/gbuf.h"
160 static dirtsec_t dirty;
162 static uns everything_committed; // after the 1st load, this flag is set on
165 add_dirty(struct cf_section *sec, void *ptr)
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)
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"
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) {
197 dirties = write - dirty.ptr;
202 #define SEC_FLAG_DYNAMIC 0x80000000 // contains a dynamic attribute
203 #define SEC_FLAG_UNKNOWN 0x40000000 // ignore unknown entriies
204 #define SEC_FLAG_CANT_COPY 0x20000000 // contains lists or parsers
205 #define SEC_FLAG_NUMBER 0x0fffffff // number of entries
207 static struct cf_section sections; // root section
209 static struct cf_item *
210 find_subitem(struct cf_section *sec, byte *name)
212 struct cf_item *ci = sec->cfg;
213 for (; ci->cls; ci++)
214 if (!strcasecmp(ci->name, name))
220 inspect_section(struct cf_section *sec)
224 for (ci=sec->cfg; ci->cls; ci++)
225 if (ci->cls == CC_SECTION) {
226 inspect_section(ci->u.sec);
227 sec->flags |= ci->u.sec->flags & (SEC_FLAG_DYNAMIC | SEC_FLAG_CANT_COPY);
228 } else if (ci->cls == CC_LIST) {
229 inspect_section(ci->u.sec);
230 sec->flags |= SEC_FLAG_DYNAMIC | SEC_FLAG_CANT_COPY;
231 } else if (ci->cls == CC_DYNAMIC)
232 sec->flags |= SEC_FLAG_DYNAMIC;
233 else if (ci->cls == CC_PARSER) {
234 sec->flags |= SEC_FLAG_CANT_COPY;
236 sec->flags |= SEC_FLAG_DYNAMIC;
239 sec->flags &= ~SEC_FLAG_CANT_COPY;
240 sec->flags |= ci - sec->cfg; // record the number of entries
244 cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown)
249 sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
251 struct cf_item *ci = find_subitem(§ions, name);
253 die("Cannot register section %s twice", name);
254 ci->cls = CC_SECTION;
259 inspect_section(sec);
261 sec->flags |= SEC_FLAG_UNKNOWN;
263 if (ci - sections.cfg >= (int) sections.size)
265 sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
266 bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
272 cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero)
276 bzero(ptr, sec->size);
278 for (uns i=0; sec->cfg[i].cls; i++)
279 if (sec->cfg[i].cls == CC_SECTION)
280 cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr, 0);
281 else if (sec->cfg[i].cls == CC_LIST)
282 clist_init(ptr + (addr_int_t) sec->cfg[i].ptr);
284 byte *msg = sec->init(ptr);
286 die("Cannot initialize section %s: %s", name, msg);
293 static uns initialized = 0;
296 sections.flags |= SEC_FLAG_UNKNOWN;
297 sections.size = 0; // size of allocated array used to be stored here
298 cf_init_section("top-level", §ions, NULL, 0);
302 commit_section(byte *name, struct cf_section *sec, void *ptr, uns commit_all)
305 for (ci=sec->cfg; ci->cls; ci++)
306 if (ci->cls == CC_SECTION) {
307 if (commit_section(ci->name, ci->u.sec, ptr + (addr_int_t) ci->ptr, commit_all)) {
308 log(L_ERROR, "It happened in section %s", ci->name);
311 } else if (ci->cls == CC_LIST) {
313 CLIST_FOR_EACH(cnode *, n, * (clist*) (ptr + (addr_int_t) ci->ptr))
314 if (idx++, commit_section(ci->name, ci->u.sec, n, commit_all)) {
315 log(L_ERROR, "It happened in node #%d of list %s", idx, ci->name);
320 /* We have to process the whole tree of sections even if just a few changes
321 * have been made, because there are dependencies between commit-hooks and
322 * hence we need to call them in a fixed order. */
323 #define ARY_LT_X(ary,i,x) ary[i].sec < x.sec || ary[i].sec == x.sec && ary[i].ptr < x.ptr
324 struct dirty_section comp = { sec, ptr };
325 uns pos = BIN_SEARCH_FIRST_GE_CMP(dirty.ptr, dirties, comp, ARY_LT_X);
328 || (pos < dirties && dirty.ptr[pos].sec == sec && dirty.ptr[pos].ptr == ptr)) {
329 byte *msg = sec->commit(ptr);
331 log(L_ERROR, "Cannot commit section %s: %s", name, msg);
339 static struct cf_item *
340 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
343 if (name[0] == '^') // absolute name instead of relative
344 name++, curr_sec = §ions, *ptr = NULL;
345 if (!curr_sec) // don't even search in an unknown section
349 if (curr_sec != §ions)
350 add_dirty(curr_sec, *ptr);
351 byte *c = strchr(name, '.');
354 struct cf_item *ci = find_subitem(curr_sec, name);
357 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
358 *msg = cf_printf("Unknown item %s", name);
361 *ptr += (addr_int_t) ci->ptr;
364 if (ci->cls != CC_SECTION)
366 *msg = cf_printf("Item %s is not a section", name);
369 curr_sec = ci->u.sec;
375 cf_find_item(byte *name, struct cf_item *item)
379 struct cf_item *ci = find_item(§ions, name, &msg, &ptr);
386 bzero(item, sizeof(struct cf_item));
390 /* Safe loading and reloading */
392 static int load_file(byte *file);
393 static int load_string(byte *string);
396 cf_reload(byte *file)
399 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
400 uns ec = everything_committed;
401 everything_committed = 0;
402 int err = load_file(file);
405 for (struct old_pools *p=pools; p; p=pools)
410 cf_journal_commit_transaction(1, NULL);
414 everything_committed = ec;
415 cf_journal_rollback_transaction(1, oldj);
424 struct cf_journal_item *oldj = cf_journal_new_transaction(1);
425 int err = load_file(file);
427 cf_journal_commit_transaction(1, oldj);
429 cf_journal_rollback_transaction(1, oldj);
436 struct cf_journal_item *oldj = cf_journal_new_transaction(0);
437 int err = load_string(string);
439 cf_journal_commit_transaction(0, oldj);
441 cf_journal_rollback_transaction(0, oldj);
445 /* Parsers for standard types */
448 uns name; // one-letter name of the unit
449 uns num, den; // fraction
452 static const struct unit units[] = {
457 { 'g', 1000000000, 1 },
460 { 'G', 1073741824, 1 },
465 static const struct unit *
466 lookup_unit(byte *value, byte *end, byte **msg)
469 if (end == value || end[1] || *end >= '0' && *end <= '9')
470 *msg = "Invalid number";
472 for (const struct unit *u=units; u->name; u++)
475 *msg = "Invalid unit";
481 static char cf_rngerr[] = "Number out of range";
484 cf_parse_int(byte *str, int *ptr)
488 msg = "Missing number";
490 const struct unit *u;
493 uns x = strtoul(str, &end, 0);
496 else if (u = lookup_unit(str, end, &msg)) {
497 u64 y = (u64)x * u->num;
499 msg = "Number is not an integer";
513 cf_parse_u64(byte *str, u64 *ptr)
517 msg = "Missing number";
519 const struct unit *u;
522 u64 x = strtoull(str, &end, 0);
525 else if (u = lookup_unit(str, end, &msg)) {
526 if (x > ~(u64)0 / u->num)
527 msg = "Number out of range";
531 msg = "Number is not an integer";
542 cf_parse_double(byte *str, double *ptr)
546 msg = "Missing number";
548 const struct unit *u;
551 if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
552 msg = "Invalid number";
553 else if (u = lookup_unit(str, str + read_chars, &msg))
554 *ptr = x * u->num / u->den;
562 cf_parse_ip(byte *p, u32 *varp)
565 return "Missing IP address";
568 if (*p == '0' && (p[1] | 32) == 'x' && Cxdigit(p[2])) {
570 x = strtoul(p, &p2, 16);
571 if (errno == ERANGE || x > 0xffffffff)
576 for (uns i = 0; i < 4; i++) {
584 uns y = strtoul(p, &p2, 10);
585 if (errno == ERANGE || p2 == (char*) p || y > 255)
591 return *p ? "Trailing characters" : NULL;
593 return "Invalid IP address";
597 cf_parse_string(byte *str, byte **ptr)
599 *ptr = cf_strdup(str);
604 cf_parse_lookup(byte *str, int *ptr, byte **t)
608 while (*n && strcasecmp(*n, str)) {
609 total_len += strlen(*n) + 2;
616 byte *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
617 c += sprintf(err, "Invalid value %s, possible values are: ", str);
619 c+= sprintf(c, "%s, ", *n);
626 /* Register size of and parser for each basic type */
628 typedef byte *cf_basic_parser(byte *str, void *ptr);
633 { sizeof(int), cf_parse_int },
634 { sizeof(u64), cf_parse_u64 },
635 { sizeof(double), cf_parse_double },
636 { sizeof(u32), cf_parse_ip },
637 { sizeof(byte*), cf_parse_string },
638 { sizeof(int), NULL }, // lookups are parsed extra
639 { 0, NULL }, // user-defined types are parsed extra
643 type_size(enum cf_type type, struct cf_user_type *utype)
646 return parsers[type].size;
652 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_union *u)
654 for (uns i=0; i<number; i++)
657 uns size = type_size(type, u->utype);
658 if (type < CT_LOOKUP)
659 msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * size);
660 else if (type == CT_LOOKUP)
661 msg = cf_parse_lookup(pars[i], ptr + i * size, u->lookup);
662 else if (type == CT_USER)
663 msg = u->utype->parser(pars[i], ptr + i * size);
667 return cf_printf("Cannot parse item %d: %s", i+1, msg);
675 static byte *op_names[] = { CF_OPERATIONS };
678 #define OP_MASK 0xff // only get the operation
679 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
680 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
681 #define OP_2ND 0x400 // in the 2nd phase real data are entered
684 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
686 enum cf_type type = item->type;
687 cf_journal_block(ptr, sizeof(void*));
688 // boundary checks done by the caller
689 uns size = type_size(item->type, item->u.utype);
690 ASSERT(size >= sizeof(uns));
691 *ptr = cf_malloc((number+1) * size) + size;
692 * (uns*) (*ptr - size) = number;
693 return cf_parse_ary(number, pars, *ptr, type, &item->u);
697 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
699 enum cf_type type = item->type;
701 uns size = type_size(item->type, item->u.utype);
702 ASSERT(size >= sizeof(uns));
703 int old_nr = * (int*) (old_p - size);
704 int taken = MIN(number, ABS(item->number)-old_nr);
706 // stretch the dynamic array
707 void *new_p = cf_malloc((old_nr + taken + 1) * size) + size;
708 * (uns*) (new_p - size) = old_nr + taken;
709 cf_journal_block(ptr, sizeof(void*));
711 if (op == OP_APPEND) {
712 memcpy(new_p, old_p, old_nr * size);
713 return cf_parse_ary(taken, pars, new_p + old_nr * size, type, &item->u);
714 } else if (op == OP_PREPEND) {
715 memcpy(new_p + taken * size, old_p, old_nr * size);
716 return cf_parse_ary(taken, pars, new_p, type, &item->u);
718 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
721 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
724 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
728 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
731 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
733 return cf_printf("Item %s: %s", ci->name, msg);
737 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
744 add_to_list(cnode *where, cnode *new_node, enum cf_operation op)
748 case OP_EDIT: // edition has been done in-place
751 CF_JOURNAL_VAR(where->prev->next);
752 CF_JOURNAL_VAR(where->next->prev);
755 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
758 CF_JOURNAL_VAR(where->next->prev);
759 CF_JOURNAL_VAR(where->next);
760 clist_insert_after(new_node, where);
762 case OP_BEFORE: // implementation dependend (append_tail = before(list))
765 CF_JOURNAL_VAR(where->prev->next);
766 CF_JOURNAL_VAR(where->prev);
767 clist_insert_before(new_node, where);
775 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
778 return cf_printf("You have to open a block for operation %s", op_names[op]);
780 return "Nothing to add to the list";
781 struct cf_section *sec = item->u.sec;
785 void *node = cf_malloc(sec->size);
786 cf_init_section(item->name, sec, node, 1);
787 add_to_list(ptr, node, op);
789 /* If the node contains any dynamic attribute at the end, we suppress
790 * auto-repetition here and pass the flag inside instead. */
791 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
795 if (sec->flags & SEC_FLAG_DYNAMIC)
802 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
809 return "Missing value";
810 taken = MIN(number, item->number);
812 uns size = type_size(item->type, item->u.utype);
813 cf_journal_block(ptr, taken * size);
814 return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
817 return "Dynamic array cannot be used here";
818 taken = MIN(number, ABS(item->number));
820 return interpret_set_dynamic(item, taken, pars, ptr);
822 if (item->number < 0 && !allow_dynamic)
823 return "Parsers with variable number of parameters cannot be used here";
824 if (item->number > 0 && number < item->number)
825 return "Not enough parameters available for the parser";
826 taken = MIN(number, ABS(item->number));
828 for (int i=0; i<taken; i++)
829 pars[i] = cf_strdup(pars[i]);
830 return item->u.par(taken, pars, ptr);
832 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
835 return "Lists cannot be used here";
836 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
843 interpret_clear(struct cf_item *item, void *ptr)
845 if (item->cls == CC_LIST) {
846 cf_journal_block(ptr, sizeof(clist));
848 } else if (item->cls == CC_DYNAMIC) {
849 cf_journal_block(ptr, sizeof(void *));
850 * (void**) ptr = NULL;
852 return "The item is not a list or a dynamic array";
857 cmp_items(void *i1, void *i2, struct cf_item *item)
859 ASSERT(item->cls == CC_STATIC);
860 i1 += (addr_int_t) item->ptr;
861 i2 += (addr_int_t) item->ptr;
862 if (item->type == CT_STRING)
863 return strcmp(* (byte**) i1, * (byte**) i2);
864 else // all numeric types
865 return memcmp(i1, i2, type_size(item->type, item->u.utype));
869 find_list_node(clist *list, void *query, struct cf_section *sec, u32 mask)
871 CLIST_FOR_EACH(cnode *, n, *list)
874 for (uns i=0; i<32; i++)
876 if (cmp_items(n, query, sec->cfg+i))
888 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
890 uns nr = sec->flags & SEC_FLAG_NUMBER;
891 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
893 uns i = item - sec->cfg;
895 return "Cannot select list nodes by this attribute";
896 if (sec->cfg[i].cls != CC_STATIC)
897 return "Selection can only be done based on basic attributes";
903 #define MAX_STACK_SIZE 100
904 static struct item_stack {
905 struct cf_section *sec; // nested section
906 void *base_ptr; // because original pointers are often relative
907 enum cf_operation op; // it is performed when a closing brace is encountered
908 void *list; // list the operations should be done on
909 u32 mask; // bit array of selectors searching in a list
910 struct cf_item *item; // cf_item of the list
911 } stack[MAX_STACK_SIZE];
915 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
917 if (level >= MAX_STACK_SIZE-1)
918 return "Too many nested sections";
919 stack[++level] = (struct item_stack) {
927 if (!item) // unknown is ignored; we just need to trace recursion
929 stack[level].sec = item->u.sec;
930 if (item->cls == CC_SECTION)
932 stack[level].base_ptr = ptr;
933 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
935 else if (item->cls == CC_LIST)
937 stack[level].base_ptr = cf_malloc(item->u.sec->size);
938 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
939 stack[level].list = ptr;
940 stack[level].item = item;
941 if ((op & OP_MASK) < OP_REMOVE) {
942 add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
943 stack[level].op |= OP_2ND;
945 stack[level].op |= OP_1ST;
948 return "Opening brace can only be used on sections and lists";
953 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
955 if (st->op == OP_CLOSE) // top-level
956 return "Unmatched } parenthesis";
957 if (!st->sec) { // dummy run on unknown section
962 enum cf_operation pure_op = st->op & OP_MASK;
965 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
967 return "Cannot find a node matching the query";
968 if (pure_op != OP_REMOVE)
970 if (pure_op == OP_EDIT)
971 st->base_ptr = st->list;
972 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
973 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
974 else if (pure_op == OP_COPY) {
975 if (st->sec->flags & SEC_FLAG_CANT_COPY)
976 return cf_printf("Item %s cannot be copied", st->item->name);
977 memcpy(st->base_ptr, st->list, st->sec->size); // strings and dynamic arrays are shared
979 TRY( st->sec->copy(st->base_ptr, st->list) );
982 if (op & OP_OPEN) { // stay at the same recursion level
983 st->op = (st->op | OP_2ND) & ~OP_1ST;
984 add_to_list(st->list, st->base_ptr, pure_op);
987 int taken; // parse parameters on 1 line immediately
988 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
991 // and fall-thru to the 2nd phase
993 add_to_list(st->list, st->base_ptr, pure_op);
997 return "No parameters expected after the }";
998 else if (op & OP_OPEN)
999 return "No { is expected";
1005 interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
1008 if ((op & OP_MASK) == OP_CLOSE)
1009 return closing_brace(stack+level, op, number, pars);
1010 void *ptr = stack[level].base_ptr;
1011 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
1014 if (stack[level].op & OP_1ST)
1015 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
1016 if (op & OP_OPEN) { // the operation will be performed after the closing brace
1018 return "Cannot open a block after a parameter has been passed on a line";
1019 return opening_brace(item, ptr, op);
1021 if (!item) // ignored item in an unknown section
1025 int taken; // process as many parameters as possible
1027 taken = 0, msg = interpret_clear(item, ptr);
1028 else if (op == OP_SET)
1029 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
1030 else if (item->cls == CC_DYNAMIC)
1031 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
1032 else if (item->cls == CC_LIST)
1033 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
1035 return cf_printf("Operation %s not supported on attribute %s", op_names[op], name);
1039 return cf_printf("Too many parameters: %d>%d", number, taken);
1045 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
1051 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
1055 msg = interpret_clear(item, item->ptr);
1059 if (item->cls == CC_DYNAMIC)
1060 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
1061 else if (item->cls == CC_LIST)
1062 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
1064 return "The attribute class does not support append/prepend";
1067 return "Unsupported operation";
1072 return "Too many parameters";
1081 stack[0] = (struct item_stack) {
1095 log(L_ERROR, "Unterminated block");
1099 if (commit_section("top-level", §ions, NULL, !everything_committed))
1101 everything_committed = 1;
1106 /* Text file parser */
1108 static byte *name_parse_fb;
1109 static struct fastbuf *parse_fb;
1110 static uns line_num;
1112 #define MAX_LINE 4096
1113 static byte line_buf[MAX_LINE];
1114 static byte *line = line_buf;
1116 #include "lib/bbuf.h"
1117 static bb_t copy_buf;
1120 #define GBUF_TYPE uns
1121 #define GBUF_PREFIX(x) split_##x
1122 #include "lib/gbuf.h"
1123 static split_t word_buf;
1125 static uns ends_by_brace; // the line is ended by "{"
1130 if (!bgets(parse_fb, line_buf, MAX_LINE))
1134 while (Cblank(*line))
1140 append(byte *start, byte *end)
1142 uns len = end - start;
1143 bb_grow(©_buf, copied + len + 1);
1144 memcpy(copy_buf.ptr + copied, start, len);
1146 copy_buf.ptr[copied-1] = 0;
1149 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1150 // these characters separate words like blanks
1153 get_word(uns is_command_name)
1155 if (*line == '\'') {
1159 while (*line && *line != '\'')
1161 append(start, line);
1164 copy_buf.ptr[copied-1] = '\n';
1166 return "Unterminated apostrophe word at the end";
1170 } else if (*line == '"') {
1172 uns start_copy = copied;
1177 if (*line == '"' && !escape)
1179 else if (*line == '\\')
1185 append(start, line);
1189 copy_buf.ptr[copied-1] = '\n';
1190 else // merge two lines
1193 return "Unterminated quoted word at the end";
1197 byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
1198 uns l = strlen(tmp);
1199 bb_grow(©_buf, start_copy + l + 1);
1200 strcpy(copy_buf.ptr + start_copy, tmp);
1201 copied = start_copy + l + 1;
1204 // promised that *line is non-null and non-blank
1206 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1207 && (*line != '=' || !is_command_name))
1209 if (*line == '=') { // nice for setting from a command-line
1211 return "Assignment without a variable";
1214 if (line == start) // already the first char is control
1216 append(start, line);
1218 while (Cblank(*line))
1224 get_token(uns is_command_name, byte **msg)
1228 if (!*line || *line == '#') {
1229 if (!is_command_name || !get_line())
1231 } else if (*line == ';') {
1233 if (!is_command_name || *msg)
1235 } else if (*line == '\\' && !line[1]) {
1237 *msg = "Last line ends by a backslash";
1240 if (!*line || *line == '#')
1241 log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
1243 split_grow(&word_buf, words+1);
1245 word_buf.ptr[words++] = copied;
1246 *msg = get_word(is_command_name);
1247 return *msg ? NULL : copy_buf.ptr + start;
1255 words = copied = ends_by_brace = 0;
1256 byte *msg, *start_word;
1257 if (!(start_word = get_token(1, &msg)))
1259 if (*start_word == '{') // only one opening brace
1260 return "Unexpected opening brace";
1261 while (*line != '}') // stays for the next time
1263 if (!(start_word = get_token(0, &msg)))
1265 if (*start_word == '{') {
1266 words--; // discard the brace
1274 /* Parsing multiple files */
1277 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1280 name_parse_fb = name_fb;
1287 msg = split_command();
1292 byte *name = copy_buf.ptr + word_buf.ptr[0];
1293 byte *pars[words-1];
1294 for (uns i=1; i<words; i++)
1295 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1296 if (!strcasecmp(name, "include"))
1299 msg = "Expecting one filename";
1301 msg = "Too many nested files";
1302 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
1303 msg = "The input command must be the last one on a line";
1306 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
1308 msg = cf_printf("Cannot open file %s: %m", pars[0]);
1312 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1319 enum cf_operation op;
1320 byte *c = strchr(name, ':');
1322 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1325 switch (Clocase(*c)) {
1326 case 's': op = OP_SET; break;
1327 case 'c': op = Clocase(c[1]) == 'l' ? OP_CLEAR: OP_COPY; break;
1328 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1329 case 'p': op = OP_PREPEND; break;
1330 case 'r': op = OP_REMOVE; break;
1331 case 'e': op = OP_EDIT; break;
1332 case 'b': op = OP_BEFORE; break;
1333 default: op = OP_SET; break;
1335 if (strcasecmp(c, op_names[op])) {
1336 msg = cf_printf("Unknown operation %s", c);
1342 msg = interpret_line(name, op, words-1, pars);
1347 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1348 return "included from here";
1351 #undef DEFAULT_CONFIG /* FIXME */
1352 #define DEFAULT_CONFIG "cf/sherlock2"
1354 #ifndef DEFAULT_CONFIG
1355 #define DEFAULT_CONFIG NULL
1357 byte *cf_def_file = DEFAULT_CONFIG;
1360 load_file(byte *file)
1363 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
1365 log(L_ERROR, "Cannot open %s: %m", file);
1368 byte *msg = parse_fastbuf(file, fb, 0);
1370 int err = !!msg || done_stack();
1377 load_string(byte *string)
1381 fbbuf_init_read(&fb, string, strlen(string), 0);
1382 byte *msg = parse_fastbuf("memory string", &fb, 0);
1383 return !!msg || done_stack();
1386 /* Command-line parser */
1392 if (cf_load(cf_def_file))
1393 die("Cannot load default config %s", cf_def_file);
1397 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1399 static int other_options = 0;
1401 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1402 if (res == 'S' || res == 'C' || res == 0x64436667)
1405 die("The -S and -C options must precede all other arguments");
1409 die("Cannot set %s", optarg);
1410 } else if (res == 'C') {
1411 if (cf_load(optarg))
1412 die("Cannot load config file %s", optarg);
1415 else { /* --dumpconfig */
1417 struct fastbuf *b = bfdopen(1, 4096);
1418 cf_dump_sections(b);
1423 /* unhandled option or end of options */
1424 if (res != ':' && res != '?')
1435 spaces(struct fastbuf *fb, uns nr)
1437 for (uns i=0; i<nr; i++)
1442 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type, union cf_union *u)
1445 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1446 case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break;
1447 case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break;
1448 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1451 bprintf(fb, "'%s' ", *(byte**)ptr);
1453 bprintf(fb, "NULL ");
1455 case CT_LOOKUP: bprintf(fb, "%s ", *(int*)ptr >= 0 ? u->lookup[ *(int*)ptr ] : (byte*) "???"); break;
1457 if (u->utype->dumper)
1458 u->utype->dumper(fb, ptr);
1460 bprintf(fb, "??? ");
1465 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1467 static byte *class_names[] = { "end", "static", "dynamic", "parser", "section", "list" };
1468 static byte *type_names[] = { "int", "u64", "double", "ip", "string", "lookup", "user" };
1471 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1473 ptr += (addr_int_t) item->ptr;
1474 enum cf_type type = item->type;
1475 uns size = type_size(item->type, item->u.utype);
1478 bprintf(fb, "%s: C%s #", item->name, class_names[item->cls]);
1479 if (item->number == CF_ANY_NUM)
1482 bprintf(fb, "%d ", item->number);
1483 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC) {
1484 bprintf(fb, "T%s ", type_names[type]);
1485 if (item->type == CT_USER)
1486 bprintf(fb, "U%s S%d ", item->u.utype->name, size);
1488 if (item->cls == CC_STATIC) {
1489 for (i=0; i<item->number; i++)
1490 dump_basic(fb, ptr + i * size, type, &item->u);
1491 } else if (item->cls == CC_DYNAMIC) {
1492 ptr = * (void**) ptr;
1494 int real_nr = * (int*) (ptr - size);
1495 bprintf(fb, "N%d ", real_nr);
1496 for (i=0; i<real_nr; i++)
1497 dump_basic(fb, ptr + i * size, type, &item->u);
1499 bprintf(fb, "NULL ");
1502 if (item->cls == CC_SECTION)
1503 dump_section(fb, item->u.sec, level+1, ptr);
1504 else if (item->cls == CC_LIST) {
1506 CLIST_FOR_EACH(cnode *, n, * (clist*) ptr) {
1507 spaces(fb, level+1);
1508 bprintf(fb, "item %d\n", ++idx);
1509 dump_section(fb, item->u.sec, level+2, n);
1515 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1518 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1519 for (struct cf_item *item=sec->cfg; item->cls; item++)
1520 dump_item(fb, item, level, ptr);
1524 cf_dump_sections(struct fastbuf *fb)
1526 dump_section(fb, §ions, 0, NULL);
1530 * - more space efficient journal