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";
399 //FIXME: parsers should handle well empty strings, unwanted suffixes etc.
401 cf_parse_int(byte *str, int *ptr)
405 msg = "Missing number";
407 const struct unit *u;
410 uns x = strtoul(str, &end, 0);
413 else if (u = lookup_unit(str, end, &msg)) {
414 u64 y = (u64)x * u->num;
416 msg = "Number is not an integer";
430 cf_parse_u64(byte *str, u64 *ptr)
434 msg = "Missing number";
436 const struct unit *u;
439 u64 x = strtoull(str, &end, 0);
442 else if (u = lookup_unit(str, end, &msg)) {
443 if (x > ~(u64)0 / u->num)
444 msg = "Number out of range";
448 msg = "Number is not an integer";
459 cf_parse_double(byte *str, double *ptr)
463 msg = "Missing number";
465 const struct unit *u;
468 double x = strtoul(str, &end, 0);
471 else if (u = lookup_unit(str, end, &msg))
472 *ptr = x * u->num / u->den;
480 cf_parse_ip(byte *p, u32 *varp)
483 return "Missing IP address";
485 if (*p == '0' && p[1] | 32 == 'X') {
487 x = strtoul(p + 2, NULL, 16);
488 if (errno == ERANGE || x > 0xffffffff)
492 for (uns i = 0; i < 4; i++) {
499 uns y = strtoul(p, &p2, 10);
501 if (errno == ERANGE || y > 255)
508 return "Invalid IP address";
512 cf_parse_string(byte *str, byte **ptr)
514 *ptr = cf_strdup(str);
518 /* Register size of and parser for each basic type */
520 typedef byte *cf_basic_parser(byte *str, void *ptr);
525 { sizeof(int), cf_parse_int },
526 { sizeof(u64), cf_parse_u64 },
527 { sizeof(double), cf_parse_double },
528 { sizeof(u32), cf_parse_ip },
529 { sizeof(byte*), cf_parse_string }
533 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type)
535 for (uns i=0; i<number; i++)
537 byte *msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
539 return cf_printf("Cannot parse item %d: %s", i+1, msg);
547 static byte *op_names[] = { CF_OPERATIONS };
550 #define OP_MASK 0xff // only get the operation
551 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
552 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
553 #define OP_2ND 0x400 // in the 2nd phase real data are entered
556 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
558 enum cf_type type = item->u.type;
559 cf_journal_block(ptr, sizeof(void*));
560 // boundary checks done by the caller
561 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
562 * (uns*) (*ptr - parsers[type].size) = number;
563 return cf_parse_ary(number, pars, *ptr, type);
567 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum operation op)
569 enum cf_type type = item->u.type;
571 int old_nr = * (int*) (old_p - parsers[type].size);
572 int taken = MIN(number, item->number-old_nr);
574 // stretch the dynamic array
575 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
576 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
577 cf_journal_block(ptr, sizeof(void*));
579 if (op == OP_APPEND) {
580 memcpy(new_p, old_p, old_nr * parsers[type].size);
581 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type);
582 } else if (op == OP_PREPEND) {
583 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
584 return cf_parse_ary(taken, pars, new_p, type);
586 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
589 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
592 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
595 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
598 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
600 return cf_printf("Item %s: %s", ci->name, msg);
604 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
611 add_to_list(void *list, struct cnode *node, enum operation op)
613 //cf_journal_block(list, sizeof(struct clist)); //FIXME: we should journal the nodes of the list instead
618 clist_add_tail(list, node);
621 clist_add_head(list, node);
626 case OP_EDIT: // edition has been done in-place
628 case OP_AFTER: // here the pointer list is actually a pointer to another node
629 clist_insert_after(node, list);
632 clist_insert_before(node, list);
640 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum operation op)
643 return "Missing value";
644 ASSERT(op < OP_REMOVE);
645 struct cf_section *sec = item->u.sec;
649 void *node = cf_malloc(sec->size);
650 cf_init_section(item->name, sec, node);
651 add_to_list(ptr, node, op);
653 /* If the node contains any dynamic attribute at the end, we suppress
654 * auto-repetition here and pass the flag inside instead. */
655 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
659 if (sec->flags & SEC_FLAG_DYNAMIC)
666 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
673 return "Missing value";
674 taken = MIN(number, item->number);
676 cf_journal_block(ptr, taken * parsers[item->u.type].size);
677 return cf_parse_ary(taken, pars, ptr, item->u.type);
680 return "Dynamic array cannot be used here";
681 taken = MIN(number, item->number);
683 return interpret_set_dynamic(item, taken, pars, ptr);
685 if (item->number < 0 && !allow_dynamic)
686 return "Parsers with variable number of parameters cannot be used here";
687 if (item->number > 0 && number < item->number)
688 return "Not enough parameters available for the parser";
689 taken = MIN(number, ABS(item->number));
691 for (int i=0; i<taken; i++)
692 pars[i] = cf_strdup(pars[i]);
693 return item->u.par(taken, pars, ptr);
695 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
698 return "Lists cannot be used here";
699 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
706 interpret_clear(struct cf_item *item, void *ptr)
708 if (item->cls == CC_LIST) {
709 cf_journal_block(ptr, sizeof(struct clist));
711 } else if (item->cls == CC_DYNAMIC) {
712 cf_journal_block(ptr, sizeof(void *));
713 * (void**) ptr = NULL;
715 return "The item is not a list or a dynamic array";
720 cmp_items(void *i1, void *i2, struct cf_item *item)
722 ASSERT(item->cls == CC_STATIC);
723 i1 += (addr_int_t) item->ptr;
724 i2 += (addr_int_t) item->ptr;
725 if (item->u.type == CT_STRING)
726 return strcmp(* (byte**) i1, * (byte**) i2);
727 else // all numeric types
728 return memcmp(i1, i2, parsers[item->u.type].size);
732 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
738 for (uns i=0; i<32; i++)
740 if (cmp_items(n, query, sec->cfg+i))
752 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
754 uns nr = sec->flags & SEC_FLAG_NUMBER;
755 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
757 uns i = item - sec->cfg;
759 return "Cannot select list nodes by this attribute";
760 if (sec->cfg[i].cls != CC_STATIC)
761 return "Selection can only be done based on basic attributes";
767 #define MAX_STACK_SIZE 100
768 static struct item_stack {
769 struct cf_section *sec; // nested section
770 void *base_ptr; // because original pointers are often relative
771 enum operation op; // it is performed when a closing brace is encountered
772 void *list; // list the operations should be done on
773 u32 mask; // bit array of selectors searching in a list
774 struct cf_item *item; // cf_item of the list
775 } stack[MAX_STACK_SIZE];
779 opening_brace(struct cf_item *item, void *ptr, enum operation op)
781 if (level >= MAX_STACK_SIZE-1)
782 return "Too many nested sections";
783 stack[++level] = (struct item_stack) {
791 if (!item) // unknown is ignored; we just need to trace recursion
793 stack[level].sec = item->u.sec;
794 if (item->cls == CC_SECTION)
796 stack[level].base_ptr = ptr;
797 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
799 else if (item->cls == CC_LIST)
801 stack[level].base_ptr = cf_malloc(item->u.sec->size);
802 cf_init_section(item->name, item->u.sec, stack[level].base_ptr);
803 stack[level].list = ptr;
804 stack[level].item = item;
805 stack[level].op |= (op & OP_MASK) < OP_REMOVE ? OP_2ND : OP_1ST;
808 return "Opening brace can only be used on sections and lists";
813 closing_brace(struct item_stack *st, enum operation op, int number, byte **pars)
815 if (st->op == OP_CLOSE) // top-level
816 return "Unmatched } parenthese";
817 if (!st->sec) { // dummy run on unknown section
822 enum operation pure_op = st->op & OP_MASK;
825 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
827 return "Cannot find a node matching the query";
828 if (pure_op != OP_REMOVE)
830 if (pure_op == OP_EDIT)
831 st->base_ptr = st->list;
832 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
833 cf_init_section(st->item->name, st->sec, st->base_ptr);
836 if (op & OP_OPEN) { // stay at the same recursion level
837 st->op = (st->op | OP_2ND) & ~OP_1ST;
840 int taken; // parse parameters on 1 line immediately
841 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
844 // and fall-thru to the 2nd phase
847 add_to_list(st->list, st->base_ptr, pure_op);
850 return "No parameters expected after the }";
851 else if (op & OP_OPEN)
852 return "No { is expected";
858 interpret_line(byte *name, enum operation op, int number, byte **pars)
861 if ((op & OP_MASK) == OP_CLOSE)
862 return closing_brace(stack+level, op, number, pars);
863 void *ptr = stack[level].base_ptr;
864 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
867 if (stack[level].op & OP_1ST)
868 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
869 if (op & OP_OPEN) // the operation will be performed after the closing brace
870 return opening_brace(item, ptr, op);
871 if (!item) // ignored item in an unknown section
875 int taken; // process as many parameters as possible
877 taken = 0, msg = interpret_clear(item, ptr);
878 else if (op == OP_SET)
879 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
880 else if (item->cls == CC_DYNAMIC)
881 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
882 else if (item->cls == CC_LIST)
883 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
885 return cf_printf("Operation %s not supported on attribute class %d", op_names[op], item->cls);
889 return cf_printf("Too many parameters: %d>%d", number, taken);
895 cf_write_item(struct cf_item *item, enum operation op, int number, byte **pars)
901 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
905 msg = interpret_clear(item, item->ptr);
909 if (item->cls == CC_DYNAMIC)
910 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
911 else if (item->cls == CC_LIST)
912 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
914 return "The attribute class does not support append/prepend";
917 return "Unsupported operation";
922 return "Too many parameters";
931 stack[0] = (struct item_stack) {
945 log(L_ERROR, "Unterminated block");
948 if (commit_section("top-level", §ions, NULL))
953 /* Text file parser */
955 static struct fastbuf *parse_fb;
958 #define MAX_LINE 4096
959 static byte line_buf[MAX_LINE];
960 static byte *line = line_buf;
962 #include "lib/bbuf.h"
963 static bb_t copy_buf;
966 #define GBUF_TYPE uns
967 #define GBUF_PREFIX(x) split_##x
968 #include "lib/gbuf.h"
969 static split_t word_buf;
971 static uns ends_by_brace; // the line is ended by "{"
976 if (!bgets(parse_fb, line_buf, MAX_LINE))
980 while (Cblank(*line))
986 append(byte *start, byte *end)
988 uns len = end - start;
989 bb_grow(©_buf, copied + len + 1);
990 memcpy(copy_buf.ptr + copied, start, len);
992 copy_buf.ptr[copied-1] = 0;
995 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
996 // these characters separate words like blanks
999 get_word(uns is_command_name)
1001 if (*line == '\'') {
1005 while (*line && *line != '\'')
1007 append(start, line);
1010 copy_buf.ptr[copied-1] = '\n';
1012 return "Unterminated apostrophe word at the end";
1016 } else if (*line == '"') {
1018 uns start_copy = copied;
1023 if (*line == '"' && !escape)
1025 else if (*line == '\\')
1031 append(start, line);
1035 copy_buf.ptr[copied-1] = '\n';
1036 else // merge two lines
1039 return "Unterminated quoted word at the end";
1043 for (byte *c=copy_buf.ptr+start_copy; *c; c++)
1046 return "Formating sequences are not allowed";
1050 byte *tmp = cf_printf(copy_buf.ptr + start_copy);
1051 uns l = strlen(tmp);
1052 bb_grow(©_buf, start_copy + l + 1);
1053 strcpy(copy_buf.ptr + start_copy, tmp);
1054 copied = start_copy + l + 1;
1057 // promised that *line is non-null and non-blank
1059 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1060 && (*line != '=' || !is_command_name))
1062 if (*line == '=') { // nice for setting from a command-line
1064 return "Assignment without a variable";
1067 if (line == start) // already the first char is control
1069 append(start, line);
1071 while (Cblank(*line))
1077 get_token(uns is_command_name, byte **msg)
1081 if (!*line || *line == '#') {
1082 if (!is_command_name || !get_line())
1084 } else if (*line == ';') {
1086 if (!is_command_name || *msg)
1088 } else if (*line == '\\' && !line[1]) {
1090 *msg = "Last line ends by a backslash";
1093 if (!*line || *line == '#')
1094 log(L_WARN, "The line following the backslash is empty");
1096 split_grow(&word_buf, words+1);
1098 word_buf.ptr[words++] = copied;
1099 *msg = get_word(is_command_name);
1100 return *msg ? NULL : copy_buf.ptr + start;
1108 words = copied = ends_by_brace = 0;
1109 byte *msg, *start_word;
1110 if (!(start_word = get_token(1, &msg)))
1112 if (*start_word == '{') // only one opening brace
1113 return "Unexpected opening brace";
1114 while (*line != '}') // stays for the next time
1116 if (!(start_word = get_token(0, &msg)))
1118 if (*start_word == '{') {
1119 words--; // discard the brace
1127 /* Parsing multiple files */
1130 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1139 msg = split_command();
1144 byte *name = copy_buf.ptr + word_buf.ptr[0];
1145 byte *pars[words-1];
1146 for (uns i=1; i<words; i++)
1147 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1148 if (!strcasecmp(name, "include"))
1151 msg = "Expecting one filename";
1155 msg = "Too many nested files";
1158 struct fastbuf *new_fb = bopen(pars[0], O_RDONLY, 1<<14);
1160 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1168 byte *c = strchr(name, ':');
1170 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1173 switch (Clocase(*c)) {
1174 case 's': op = OP_SET; break;
1175 case 'c': op = OP_CLEAR; break;
1176 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1177 case 'p': op = OP_PREPEND; break;
1178 case 'r': op = OP_REMOVE; break;
1179 case 'e': op = OP_EDIT; break;
1180 case 'b': op = OP_BEFORE; break;
1181 default: op = OP_SET; break;
1183 if (strcasecmp(c, op_names[op])) {
1184 msg = cf_printf("Unknown operation %s", c);
1190 msg = interpret_line(name, op, words-1, pars);
1195 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1196 return "included from here";
1199 #ifndef DEFAULT_CONFIG
1200 #define DEFAULT_CONFIG NULL
1202 byte *cf_def_file = DEFAULT_CONFIG;
1205 load_file(byte *file)
1208 struct fastbuf *fb = bopen(file, O_RDONLY, 1<<14);
1209 byte *msg = parse_fastbuf(file, fb, 0);
1211 int err = !!msg || done_stack();
1218 load_string(byte *string)
1222 fbbuf_init_read(&fb, string, strlen(string), 0);
1223 byte *msg = parse_fastbuf("memory string", &fb, 0);
1224 return !!msg || done_stack();
1227 /* Command-line parser */
1233 if (cf_load(cf_def_file))
1234 die("Cannot load default config %s", cf_def_file);
1238 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1240 static int other_options = 0;
1242 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1243 if (res == 'S' || res == 'C')
1246 die("The -S and -C options must precede all other arguments");
1250 die("Cannot set %s", optarg);
1252 if (cf_load(optarg))
1253 die("Cannot load %s", optarg);
1256 /* unhandled option or end of options */
1266 #include "fastbuf.h"
1269 spaces(struct fastbuf *fb, uns nr)
1271 for (uns i=0; i<nr; i++)
1276 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type)
1279 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1280 case CT_U64: bprintf(fb, "%lld ", *(u64*)ptr); break;
1281 case CT_DOUBLE: bprintf(fb, "%lf ", *(double*)ptr); break;
1282 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1283 case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break;
1287 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1290 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1292 ptr += (addr_int_t) item->ptr;
1293 enum cf_type type = item->u.type;
1296 bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1297 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1298 bprintf(fb, "t%d ", type);
1299 if (item->cls == CC_STATIC) {
1300 for (i=0; i<item->number; i++)
1301 dump_basic(fb, ptr + i * parsers[type].size, type);
1302 } else if (item->cls == CC_DYNAMIC) {
1303 ptr = * (void**) ptr;
1305 int real_nr = * (int*) (ptr - parsers[type].size);
1306 bprintf(fb, "##%d ", real_nr);
1307 for (i=0; i<real_nr; i++)
1308 dump_basic(fb, ptr + i * parsers[type].size, type);
1310 bprintf(fb, "NULL ");
1313 if (item->cls == CC_SECTION)
1314 dump_section(fb, item->u.sec, level+1, ptr);
1315 else if (item->cls == CC_LIST) {
1318 CLIST_WALK(n, * (clist*) ptr) {
1319 spaces(fb, level+1);
1320 bprintf(fb, "item %d\n", ++idx);
1321 dump_section(fb, item->u.sec, level+2, n);
1327 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1330 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1331 for (struct cf_item *item=sec->cfg; item->cls; item++)
1332 dump_item(fb, item, level, ptr);
1336 cf_dump_sections(struct fastbuf *fb)
1338 dump_section(fb, §ions, 0, NULL);