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"
25 #define TRY(f) do { byte *_msg = f; if (_msg) return _msg; } while (0)
27 /* Memory allocation */
29 struct mempool *cf_pool; // current pool for loading new configuration
30 static struct old_pools {
31 struct old_pools *prev;
33 } *pools; // link-list of older cf_pool's
38 return mp_alloc(cf_pool, size);
42 cf_malloc_zero(uns size)
44 return mp_alloc_zero(cf_pool, size);
50 return mp_strdup(cf_pool, s);
54 cf_printf(char *fmt, ...)
58 byte *res = mp_vprintf(cf_pool, fmt, args);
65 uns cf_need_journal = 1; // some programs do not need journal
66 static struct journal_item {
67 struct journal_item *prev;
74 cf_journal_block(void *ptr, uns len)
78 struct journal_item *ji = cf_malloc(sizeof(struct journal_item) + len);
82 memcpy(ji->copy, ptr, len);
88 // swaps the contents of the memory and the journal, and reverses the list
90 struct journal_item *curr, *prev, *next;
91 for (next=NULL, curr=journal; curr; next=curr, curr=prev)
95 for (uns i=0; i<curr->len; i++)
97 byte x = curr->copy[i];
98 curr->copy[i] = curr->ptr[i];
105 static struct journal_item *
106 journal_new_section(uns new_pool)
109 cf_pool = mp_new(1<<14);
110 struct journal_item *oldj = journal;
116 journal_commit_section(uns new_pool, struct journal_item *oldj)
120 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
127 struct journal_item **j = &journal;
135 journal_rollback_section(uns new_pool, struct journal_item *oldj)
137 if (!cf_need_journal)
138 die("Cannot rollback the configuration, because the journal is disabled.");
144 cf_pool = pools ? pools->pool : NULL;
150 #define SEC_FLAG_DYNAMIC 0x80000000 // contains a dynamic attribute
151 #define SEC_FLAG_UNKNOWN 0x40000000 // ignore unknown entriies
152 #define SEC_FLAG_NUMBER 0x0fffffff // number of entries
154 static struct cf_section sections; // root section
156 static struct cf_item *
157 find_subitem(struct cf_section *sec, byte *name)
159 struct cf_item *ci = sec->cfg;
160 for (; ci->cls; ci++)
161 if (!strcasecmp(ci->name, name))
167 inspect_section(struct cf_section *sec)
171 for (ci=sec->cfg; ci->cls; ci++)
172 if (ci->cls == CC_SECTION) {
173 inspect_section(ci->u.sec);
174 sec->flags |= ci->u.sec->flags & SEC_FLAG_DYNAMIC;
175 } else if (ci->cls == CC_LIST) {
176 inspect_section(ci->u.sec);
177 sec->flags |= SEC_FLAG_DYNAMIC;
178 } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_PARSER && ci->number < 0)
179 sec->flags |= SEC_FLAG_DYNAMIC;
180 sec->flags |= ci - sec->cfg; // record the number of entries
184 cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown)
189 sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
191 struct cf_item *ci = find_subitem(§ions, name);
193 die("Cannot register section %s twice", name);
194 ci->cls = CC_SECTION;
199 inspect_section(sec);
201 sec->flags |= SEC_FLAG_UNKNOWN;
203 if (ci - sections.cfg >= (int) sections.size)
205 sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
206 bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
212 cf_init_section(byte *name, struct cf_section *sec, void *ptr)
215 bzero(ptr, sec->size);
216 for (uns i=0; sec->cfg[i].cls; i++)
217 if (sec->cfg[i].cls == CC_SECTION)
218 cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr);
219 else if (sec->cfg[i].cls == CC_LIST)
220 clist_init(sec->cfg[i].ptr);
222 byte *msg = sec->init(ptr);
224 die("Cannot initialize section %s: %s", name, msg);
231 static uns initialized = 0;
234 sections.flags |= SEC_FLAG_UNKNOWN;
235 sections.size = 0; // size of allocated array used to be stored here
236 cf_init_section("top-level", §ions, NULL);
240 commit_section(byte *name, struct cf_section *sec, void *ptr)
243 for (ci=sec->cfg; ci->cls; ci++)
244 if (ci->cls == CC_SECTION)
245 if (commit_section(ci->name, ci->u.sec, ptr + (addr_int_t) ci->ptr))
247 else if (ci->cls == CC_LIST) {
249 CLIST_WALK(n, * (clist*) (ptr + (addr_int_t) ci->ptr))
250 if (commit_section(ci->name, ci->u.sec, n))
254 byte *msg = sec->commit(ptr);
256 log(L_ERROR, "Cannot commit section %s: %s", name, msg);
263 static struct cf_item *
264 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
267 if (name[0] == '^') // absolute name instead of relative
268 name++, curr_sec = §ions, *ptr = NULL;
269 if (!curr_sec) // don't even search in an unknown section
273 byte *c = strchr(name, '.');
276 struct cf_item *ci = find_subitem(curr_sec, name);
279 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
280 *msg = cf_printf("Unknown item %s", name);
283 *ptr += (addr_int_t) ci->ptr;
286 if (ci->cls != CC_SECTION)
288 *msg = cf_printf("Item %s is not a section", name);
291 curr_sec = ci->u.sec;
297 cf_find_item(byte *name, struct cf_item *item)
301 struct cf_item *ci = find_item(§ions, name, &msg, &ptr);
309 /* Safe loading and reloading */
311 static int load_file(byte *file);
312 static int load_string(byte *string);
315 cf_reload(byte *file)
318 struct journal_item *oldj = journal_new_section(1);
319 int err = load_file(file);
322 for (struct old_pools *p=pools; p; p=pools)
327 journal_commit_section(1, NULL);
331 journal_rollback_section(1, oldj);
340 struct journal_item *oldj = journal_new_section(1);
341 int err = load_file(file);
343 journal_commit_section(1, oldj);
345 journal_rollback_section(1, oldj);
352 struct journal_item *oldj = journal_new_section(0);
353 int err = load_string(string);
355 journal_commit_section(0, oldj);
357 journal_rollback_section(0, oldj);
361 /* Parsers for standard types */
364 uns name; // one-letter name of the unit
365 uns num, den; // fraction
368 static const struct unit units[] = {
373 { 'g', 1000000000, 1 },
376 { 'G', 1073741824, 1 },
381 static const struct unit *
382 lookup_unit(byte *value, byte *end, byte **msg)
385 if (end == value || end[1] || *end >= '0' && *end <= '9')
386 *msg = "Invalid number";
388 for (const struct unit *u=units; u->name; u++)
391 *msg = "Invalid unit";
397 static char cf_rngerr[] = "Number out of range";
400 cf_parse_int(byte *str, int *ptr)
404 msg = "Missing number";
406 const struct unit *u;
409 uns x = strtoul(str, &end, 0);
412 else if (u = lookup_unit(str, end, &msg)) {
413 u64 y = (u64)x * u->num;
415 msg = "Number is not an integer";
429 cf_parse_u64(byte *str, u64 *ptr)
433 msg = "Missing number";
435 const struct unit *u;
438 u64 x = strtoull(str, &end, 0);
441 else if (u = lookup_unit(str, end, &msg)) {
442 if (x > ~(u64)0 / u->num)
443 msg = "Number out of range";
447 msg = "Number is not an integer";
458 cf_parse_double(byte *str, double *ptr)
462 msg = "Missing number";
464 const struct unit *u;
467 if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
468 msg = "Invalid number";
469 else if (u = lookup_unit(str, str + read_chars, &msg))
470 *ptr = x * u->num / u->den;
478 cf_parse_ip(byte *p, u32 *varp)
481 return "Missing IP address";
484 if (*p == '0' && p[1] | 32 == 'X') {
486 x = strtoul(p + 2, &p2, 16);
487 if (errno == ERANGE || p2 == (char*) (p+2) || x > 0xffffffff)
492 for (uns i = 0; i < 4; i++) {
498 uns y = strtoul(p, &p2, 10);
499 if (errno == ERANGE || p2 == (char*) p || y > 255)
505 return *p ? "Trailing characters" : NULL;
507 return "Invalid IP address";
511 cf_parse_string(byte *str, byte **ptr)
513 *ptr = cf_strdup(str);
517 /* Register size of and parser for each basic type */
519 typedef byte *cf_basic_parser(byte *str, void *ptr);
524 { sizeof(int), cf_parse_int },
525 { sizeof(u64), cf_parse_u64 },
526 { sizeof(double), cf_parse_double },
527 { sizeof(u32), cf_parse_ip },
528 { sizeof(byte*), cf_parse_string }
532 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type)
534 for (uns i=0; i<number; i++)
536 byte *msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
538 return cf_printf("Cannot parse item %d: %s", i+1, msg);
546 static byte *op_names[] = { CF_OPERATIONS };
549 #define OP_MASK 0xff // only get the operation
550 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
551 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
552 #define OP_2ND 0x400 // in the 2nd phase real data are entered
555 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
557 enum cf_type type = item->u.type;
558 cf_journal_block(ptr, sizeof(void*));
559 // boundary checks done by the caller
560 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
561 * (uns*) (*ptr - parsers[type].size) = number;
562 return cf_parse_ary(number, pars, *ptr, type);
566 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum operation op)
568 enum cf_type type = item->u.type;
570 int old_nr = * (int*) (old_p - parsers[type].size);
571 int taken = MIN(number, item->number-old_nr);
573 // stretch the dynamic array
574 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
575 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
576 cf_journal_block(ptr, sizeof(void*));
578 if (op == OP_APPEND) {
579 memcpy(new_p, old_p, old_nr * parsers[type].size);
580 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type);
581 } else if (op == OP_PREPEND) {
582 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
583 return cf_parse_ary(taken, pars, new_p, type);
585 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
588 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
591 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
594 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
597 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
599 return cf_printf("Item %s: %s", ci->name, msg);
603 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
610 add_to_list(struct cnode *where, struct cnode *new_node, enum operation op)
614 case OP_EDIT: // edition has been done in-place
617 cf_journal_block(&where->prev->next, sizeof(void*));
618 cf_journal_block(&where->next->prev, sizeof(void*));
621 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
623 cf_journal_block(&where->next->prev, sizeof(void*));
624 cf_journal_block(&where->next, sizeof(void*));
625 clist_insert_after(new_node, where);
627 case OP_BEFORE: // implementation dependend (append_tail = before(list))
630 cf_journal_block(&where->prev->next, sizeof(void*));
631 cf_journal_block(&where->prev, sizeof(void*));
632 clist_insert_before(new_node, where);
640 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum operation op)
643 return cf_printf("You have to open a block for operation %s", op_names[op]);
645 return "Nothing to add to the list";
646 struct cf_section *sec = item->u.sec;
650 void *node = cf_malloc(sec->size);
651 cf_init_section(item->name, sec, node);
652 add_to_list(ptr, node, op);
654 /* If the node contains any dynamic attribute at the end, we suppress
655 * auto-repetition here and pass the flag inside instead. */
656 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
660 if (sec->flags & SEC_FLAG_DYNAMIC)
667 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
674 return "Missing value";
675 taken = MIN(number, item->number);
677 cf_journal_block(ptr, taken * parsers[item->u.type].size);
678 return cf_parse_ary(taken, pars, ptr, item->u.type);
681 return "Dynamic array cannot be used here";
682 taken = MIN(number, item->number);
684 return interpret_set_dynamic(item, taken, pars, ptr);
686 if (item->number < 0 && !allow_dynamic)
687 return "Parsers with variable number of parameters cannot be used here";
688 if (item->number > 0 && number < item->number)
689 return "Not enough parameters available for the parser";
690 taken = MIN(number, ABS(item->number));
692 for (int i=0; i<taken; i++)
693 pars[i] = cf_strdup(pars[i]);
694 return item->u.par(taken, pars, ptr);
696 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
699 return "Lists cannot be used here";
700 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
707 interpret_clear(struct cf_item *item, void *ptr)
709 if (item->cls == CC_LIST) {
710 cf_journal_block(ptr, sizeof(struct clist));
712 } else if (item->cls == CC_DYNAMIC) {
713 cf_journal_block(ptr, sizeof(void *));
714 * (void**) ptr = NULL;
716 return "The item is not a list or a dynamic array";
721 cmp_items(void *i1, void *i2, struct cf_item *item)
723 ASSERT(item->cls == CC_STATIC);
724 i1 += (addr_int_t) item->ptr;
725 i2 += (addr_int_t) item->ptr;
726 if (item->u.type == CT_STRING)
727 return strcmp(* (byte**) i1, * (byte**) i2);
728 else // all numeric types
729 return memcmp(i1, i2, parsers[item->u.type].size);
733 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
739 for (uns i=0; i<32; i++)
741 if (cmp_items(n, query, sec->cfg+i))
753 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
755 uns nr = sec->flags & SEC_FLAG_NUMBER;
756 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
758 uns i = item - sec->cfg;
760 return "Cannot select list nodes by this attribute";
761 if (sec->cfg[i].cls != CC_STATIC)
762 return "Selection can only be done based on basic attributes";
768 #define MAX_STACK_SIZE 100
769 static struct item_stack {
770 struct cf_section *sec; // nested section
771 void *base_ptr; // because original pointers are often relative
772 enum operation op; // it is performed when a closing brace is encountered
773 void *list; // list the operations should be done on
774 u32 mask; // bit array of selectors searching in a list
775 struct cf_item *item; // cf_item of the list
776 } stack[MAX_STACK_SIZE];
780 opening_brace(struct cf_item *item, void *ptr, enum operation op)
782 if (level >= MAX_STACK_SIZE-1)
783 return "Too many nested sections";
784 stack[++level] = (struct item_stack) {
792 if (!item) // unknown is ignored; we just need to trace recursion
794 stack[level].sec = item->u.sec;
795 if (item->cls == CC_SECTION)
797 stack[level].base_ptr = ptr;
798 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
800 else if (item->cls == CC_LIST)
802 stack[level].base_ptr = cf_malloc(item->u.sec->size);
803 cf_init_section(item->name, item->u.sec, stack[level].base_ptr);
804 stack[level].list = ptr;
805 stack[level].item = item;
806 stack[level].op |= (op & OP_MASK) < OP_REMOVE ? OP_2ND : OP_1ST;
809 return "Opening brace can only be used on sections and lists";
814 closing_brace(struct item_stack *st, enum operation op, int number, byte **pars)
816 if (st->op == OP_CLOSE) // top-level
817 return "Unmatched } parenthese";
818 if (!st->sec) { // dummy run on unknown section
823 enum operation pure_op = st->op & OP_MASK;
826 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
828 return "Cannot find a node matching the query";
829 if (pure_op != OP_REMOVE)
831 if (pure_op == OP_EDIT)
832 st->base_ptr = st->list;
833 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
834 cf_init_section(st->item->name, st->sec, st->base_ptr);
837 if (op & OP_OPEN) { // stay at the same recursion level
838 st->op = (st->op | OP_2ND) & ~OP_1ST;
841 int taken; // parse parameters on 1 line immediately
842 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
845 // and fall-thru to the 2nd phase
848 add_to_list(st->list, st->base_ptr, pure_op);
851 return "No parameters expected after the }";
852 else if (op & OP_OPEN)
853 return "No { is expected";
859 interpret_line(byte *name, enum operation op, int number, byte **pars)
862 if ((op & OP_MASK) == OP_CLOSE)
863 return closing_brace(stack+level, op, number, pars);
864 void *ptr = stack[level].base_ptr;
865 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
868 if (stack[level].op & OP_1ST)
869 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
870 if (op & OP_OPEN) // the operation will be performed after the closing brace
871 return opening_brace(item, ptr, op);
872 if (!item) // ignored item in an unknown section
876 int taken; // process as many parameters as possible
878 taken = 0, msg = interpret_clear(item, ptr);
879 else if (op == OP_SET)
880 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
881 else if (item->cls == CC_DYNAMIC)
882 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
883 else if (item->cls == CC_LIST)
884 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
886 return cf_printf("Operation %s not supported on attribute class %d", op_names[op], item->cls);
890 return cf_printf("Too many parameters: %d>%d", number, taken);
896 cf_write_item(struct cf_item *item, enum operation op, int number, byte **pars)
902 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
906 msg = interpret_clear(item, item->ptr);
910 if (item->cls == CC_DYNAMIC)
911 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
912 else if (item->cls == CC_LIST)
913 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
915 return "The attribute class does not support append/prepend";
918 return "Unsupported operation";
923 return "Too many parameters";
932 stack[0] = (struct item_stack) {
946 log(L_ERROR, "Unterminated block");
949 if (commit_section("top-level", §ions, NULL))
954 /* Text file parser */
956 static struct fastbuf *parse_fb;
959 #define MAX_LINE 4096
960 static byte line_buf[MAX_LINE];
961 static byte *line = line_buf;
963 #include "lib/bbuf.h"
964 static bb_t copy_buf;
967 #define GBUF_TYPE uns
968 #define GBUF_PREFIX(x) split_##x
969 #include "lib/gbuf.h"
970 static split_t word_buf;
972 static uns ends_by_brace; // the line is ended by "{"
977 if (!bgets(parse_fb, line_buf, MAX_LINE))
981 while (Cblank(*line))
987 append(byte *start, byte *end)
989 uns len = end - start;
990 bb_grow(©_buf, copied + len + 1);
991 memcpy(copy_buf.ptr + copied, start, len);
993 copy_buf.ptr[copied-1] = 0;
996 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
997 // these characters separate words like blanks
1000 get_word(uns is_command_name)
1002 if (*line == '\'') {
1006 while (*line && *line != '\'')
1008 append(start, line);
1011 copy_buf.ptr[copied-1] = '\n';
1013 return "Unterminated apostrophe word at the end";
1017 } else if (*line == '"') {
1019 uns start_copy = copied;
1024 if (*line == '"' && !escape)
1026 else if (*line == '\\')
1032 append(start, line);
1036 copy_buf.ptr[copied-1] = '\n';
1037 else // merge two lines
1040 return "Unterminated quoted word at the end";
1044 for (byte *c=copy_buf.ptr+start_copy; *c; c++)
1047 return "Formating sequences are not allowed";
1051 byte *tmp = cf_printf(copy_buf.ptr + start_copy);
1052 uns l = strlen(tmp);
1053 bb_grow(©_buf, start_copy + l + 1);
1054 strcpy(copy_buf.ptr + start_copy, tmp);
1055 copied = start_copy + l + 1;
1058 // promised that *line is non-null and non-blank
1060 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1061 && (*line != '=' || !is_command_name))
1063 if (*line == '=') { // nice for setting from a command-line
1065 return "Assignment without a variable";
1068 if (line == start) // already the first char is control
1070 append(start, line);
1072 while (Cblank(*line))
1078 get_token(uns is_command_name, byte **msg)
1082 if (!*line || *line == '#') {
1083 if (!is_command_name || !get_line())
1085 } else if (*line == ';') {
1087 if (!is_command_name || *msg)
1089 } else if (*line == '\\' && !line[1]) {
1091 *msg = "Last line ends by a backslash";
1094 if (!*line || *line == '#')
1095 log(L_WARN, "The line following the backslash is empty");
1097 split_grow(&word_buf, words+1);
1099 word_buf.ptr[words++] = copied;
1100 *msg = get_word(is_command_name);
1101 return *msg ? NULL : copy_buf.ptr + start;
1109 words = copied = ends_by_brace = 0;
1110 byte *msg, *start_word;
1111 if (!(start_word = get_token(1, &msg)))
1113 if (*start_word == '{') // only one opening brace
1114 return "Unexpected opening brace";
1115 while (*line != '}') // stays for the next time
1117 if (!(start_word = get_token(0, &msg)))
1119 if (*start_word == '{') {
1120 words--; // discard the brace
1128 /* Parsing multiple files */
1131 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1140 msg = split_command();
1145 byte *name = copy_buf.ptr + word_buf.ptr[0];
1146 byte *pars[words-1];
1147 for (uns i=1; i<words; i++)
1148 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1149 if (!strcasecmp(name, "include"))
1152 msg = "Expecting one filename";
1156 msg = "Too many nested files";
1159 struct fastbuf *new_fb = bopen(pars[0], O_RDONLY, 1<<14);
1161 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1169 byte *c = strchr(name, ':');
1171 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1174 switch (Clocase(*c)) {
1175 case 's': op = OP_SET; break;
1176 case 'c': op = OP_CLEAR; break;
1177 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1178 case 'p': op = OP_PREPEND; break;
1179 case 'r': op = OP_REMOVE; break;
1180 case 'e': op = OP_EDIT; break;
1181 case 'b': op = OP_BEFORE; break;
1182 default: op = OP_SET; break;
1184 if (strcasecmp(c, op_names[op])) {
1185 msg = cf_printf("Unknown operation %s", c);
1191 msg = interpret_line(name, op, words-1, pars);
1196 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1197 return "included from here";
1200 #ifndef DEFAULT_CONFIG
1201 #define DEFAULT_CONFIG NULL
1203 byte *cf_def_file = DEFAULT_CONFIG;
1206 load_file(byte *file)
1209 struct fastbuf *fb = bopen(file, O_RDONLY, 1<<14);
1210 byte *msg = parse_fastbuf(file, fb, 0);
1212 int err = !!msg || done_stack();
1219 load_string(byte *string)
1223 fbbuf_init_read(&fb, string, strlen(string), 0);
1224 byte *msg = parse_fastbuf("memory string", &fb, 0);
1225 return !!msg || done_stack();
1228 /* Command-line parser */
1234 if (cf_load(cf_def_file))
1235 die("Cannot load default config %s", cf_def_file);
1239 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1241 static int other_options = 0;
1243 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1244 if (res == 'S' || res == 'C')
1247 die("The -S and -C options must precede all other arguments");
1251 die("Cannot set %s", optarg);
1253 if (cf_load(optarg))
1254 die("Cannot load %s", optarg);
1257 /* unhandled option or end of options */
1267 #include "fastbuf.h"
1270 spaces(struct fastbuf *fb, uns nr)
1272 for (uns i=0; i<nr; i++)
1277 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type)
1280 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1281 case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break;
1282 case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break;
1283 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1284 case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break;
1288 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1291 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1293 ptr += (addr_int_t) item->ptr;
1294 enum cf_type type = item->u.type;
1297 bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1298 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1299 bprintf(fb, "t%d ", type);
1300 if (item->cls == CC_STATIC) {
1301 for (i=0; i<item->number; i++)
1302 dump_basic(fb, ptr + i * parsers[type].size, type);
1303 } else if (item->cls == CC_DYNAMIC) {
1304 ptr = * (void**) ptr;
1306 int real_nr = * (int*) (ptr - parsers[type].size);
1307 bprintf(fb, "##%d ", real_nr);
1308 for (i=0; i<real_nr; i++)
1309 dump_basic(fb, ptr + i * parsers[type].size, type);
1311 bprintf(fb, "NULL ");
1314 if (item->cls == CC_SECTION)
1315 dump_section(fb, item->u.sec, level+1, ptr);
1316 else if (item->cls == CC_LIST) {
1319 CLIST_WALK(n, * (clist*) ptr) {
1320 spaces(fb, level+1);
1321 bprintf(fb, "item %d\n", ++idx);
1322 dump_section(fb, item->u.sec, level+2, n);
1328 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1331 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1332 for (struct cf_item *item=sec->cfg; item->cls; item++)
1333 dump_item(fb, item, level, ptr);
1337 cf_dump_sections(struct fastbuf *fb)
1339 dump_section(fb, §ions, 0, NULL);