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"
26 #define TRY(f) do { byte *_msg = f; if (_msg) return _msg; } while (0)
28 /* Memory allocation */
30 struct mempool *cf_pool; // current pool for loading new configuration
31 static struct old_pools {
32 struct old_pools *prev;
34 } *pools; // link-list of older cf_pool's
39 return mp_alloc(cf_pool, size);
43 cf_malloc_zero(uns size)
45 return mp_alloc_zero(cf_pool, size);
51 return mp_strdup(cf_pool, s);
55 cf_printf(char *fmt, ...)
59 byte *res = mp_vprintf(cf_pool, fmt, args);
66 uns cf_need_journal = 1; // some programs do not need journal
67 static struct journal_item {
68 struct journal_item *prev;
75 cf_journal_block(void *ptr, uns len)
79 struct journal_item *ji = cf_malloc(sizeof(struct journal_item) + len);
83 memcpy(ji->copy, ptr, len);
89 // swaps the contents of the memory and the journal, and reverses the list
91 struct journal_item *curr, *prev, *next;
92 for (next=NULL, curr=journal; curr; next=curr, curr=prev)
96 for (uns i=0; i<curr->len; i++)
98 byte x = curr->copy[i];
99 curr->copy[i] = curr->ptr[i];
106 static struct journal_item *
107 journal_new_section(uns new_pool)
110 cf_pool = mp_new(1<<14);
111 struct journal_item *oldj = journal;
117 journal_commit_section(uns new_pool, struct journal_item *oldj)
121 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
128 struct journal_item **j = &journal;
136 journal_rollback_section(uns new_pool, struct journal_item *oldj)
138 if (!cf_need_journal)
139 die("Cannot rollback the configuration, because the journal is disabled.");
145 cf_pool = pools ? pools->pool : NULL;
151 #define SEC_FLAG_DYNAMIC 0x80000000 // contains a dynamic attribute
152 #define SEC_FLAG_UNKNOWN 0x40000000 // ignore unknown entriies
153 #define SEC_FLAG_NUMBER 0x0fffffff // number of entries
155 static struct cf_section sections; // root section
157 static struct cf_item *
158 find_subitem(struct cf_section *sec, byte *name)
160 struct cf_item *ci = sec->cfg;
161 for (; ci->cls; ci++)
162 if (!strcasecmp(ci->name, name))
168 inspect_section(struct cf_section *sec)
172 for (ci=sec->cfg; ci->cls; ci++)
173 if (ci->cls == CC_SECTION) {
174 inspect_section(ci->u.sec);
175 sec->flags |= ci->u.sec->flags & SEC_FLAG_DYNAMIC;
176 } else if (ci->cls == CC_LIST) {
177 inspect_section(ci->u.sec);
178 sec->flags |= SEC_FLAG_DYNAMIC;
179 } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_PARSER && ci->number < 0)
180 sec->flags |= SEC_FLAG_DYNAMIC;
181 sec->flags |= ci - sec->cfg; // record the number of entries
185 cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown)
190 sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
192 struct cf_item *ci = find_subitem(§ions, name);
194 die("Cannot register section %s twice", name);
195 ci->cls = CC_SECTION;
200 inspect_section(sec);
202 sec->flags |= SEC_FLAG_UNKNOWN;
204 if (ci - sections.cfg >= (int) sections.size)
206 sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
207 bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
213 cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero)
217 bzero(ptr, sec->size);
219 for (uns i=0; sec->cfg[i].cls; i++)
220 if (sec->cfg[i].cls == CC_SECTION)
221 cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr, 0);
222 else if (sec->cfg[i].cls == CC_LIST)
223 clist_init(sec->cfg[i].ptr);
225 byte *msg = sec->init(ptr);
227 die("Cannot initialize section %s: %s", name, msg);
234 static uns initialized = 0;
237 sections.flags |= SEC_FLAG_UNKNOWN;
238 sections.size = 0; // size of allocated array used to be stored here
239 cf_init_section("top-level", §ions, NULL, 0);
243 commit_section(byte *name, struct cf_section *sec, void *ptr)
246 for (ci=sec->cfg; ci->cls; ci++)
247 if (ci->cls == CC_SECTION)
248 if (commit_section(ci->name, ci->u.sec, ptr + (addr_int_t) ci->ptr))
250 else if (ci->cls == CC_LIST) {
252 CLIST_WALK(n, * (clist*) (ptr + (addr_int_t) ci->ptr))
253 if (commit_section(ci->name, ci->u.sec, n))
257 byte *msg = sec->commit(ptr);
259 log(L_ERROR, "Cannot commit section %s: %s", name, msg);
266 static struct cf_item *
267 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
270 if (name[0] == '^') // absolute name instead of relative
271 name++, curr_sec = §ions, *ptr = NULL;
272 if (!curr_sec) // don't even search in an unknown section
276 byte *c = strchr(name, '.');
279 struct cf_item *ci = find_subitem(curr_sec, name);
282 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
283 *msg = cf_printf("Unknown item %s", name);
286 *ptr += (addr_int_t) ci->ptr;
289 if (ci->cls != CC_SECTION)
291 *msg = cf_printf("Item %s is not a section", name);
294 curr_sec = ci->u.sec;
300 cf_find_item(byte *name, struct cf_item *item)
304 struct cf_item *ci = find_item(§ions, name, &msg, &ptr);
312 /* Safe loading and reloading */
314 static int load_file(byte *file);
315 static int load_string(byte *string);
318 cf_reload(byte *file)
321 struct journal_item *oldj = journal_new_section(1);
322 int err = load_file(file);
325 for (struct old_pools *p=pools; p; p=pools)
330 journal_commit_section(1, NULL);
334 journal_rollback_section(1, oldj);
343 struct journal_item *oldj = journal_new_section(1);
344 int err = load_file(file);
346 journal_commit_section(1, oldj);
348 journal_rollback_section(1, oldj);
355 struct journal_item *oldj = journal_new_section(0);
356 int err = load_string(string);
358 journal_commit_section(0, oldj);
360 journal_rollback_section(0, oldj);
364 /* Parsers for standard types */
367 uns name; // one-letter name of the unit
368 uns num, den; // fraction
371 static const struct unit units[] = {
376 { 'g', 1000000000, 1 },
379 { 'G', 1073741824, 1 },
384 static const struct unit *
385 lookup_unit(byte *value, byte *end, byte **msg)
388 if (end == value || end[1] || *end >= '0' && *end <= '9')
389 *msg = "Invalid number";
391 for (const struct unit *u=units; u->name; u++)
394 *msg = "Invalid unit";
400 static char cf_rngerr[] = "Number out of range";
403 cf_parse_int(byte *str, int *ptr)
407 msg = "Missing number";
409 const struct unit *u;
412 uns x = strtoul(str, &end, 0);
415 else if (u = lookup_unit(str, end, &msg)) {
416 u64 y = (u64)x * u->num;
418 msg = "Number is not an integer";
432 cf_parse_u64(byte *str, u64 *ptr)
436 msg = "Missing number";
438 const struct unit *u;
441 u64 x = strtoull(str, &end, 0);
444 else if (u = lookup_unit(str, end, &msg)) {
445 if (x > ~(u64)0 / u->num)
446 msg = "Number out of range";
450 msg = "Number is not an integer";
461 cf_parse_double(byte *str, double *ptr)
465 msg = "Missing number";
467 const struct unit *u;
470 if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
471 msg = "Invalid number";
472 else if (u = lookup_unit(str, str + read_chars, &msg))
473 *ptr = x * u->num / u->den;
481 cf_parse_ip(byte *p, u32 *varp)
484 return "Missing IP address";
487 if (*p == '0' && p[1] | 32 == 'X') {
489 x = strtoul(p + 2, &p2, 16);
490 if (errno == ERANGE || p2 == (char*) (p+2) || x > 0xffffffff)
495 for (uns i = 0; i < 4; i++) {
501 uns y = strtoul(p, &p2, 10);
502 if (errno == ERANGE || p2 == (char*) p || y > 255)
508 return *p ? "Trailing characters" : NULL;
510 return "Invalid IP address";
514 cf_parse_string(byte *str, byte **ptr)
516 *ptr = cf_strdup(str);
520 /* Register size of and parser for each basic type */
522 typedef byte *cf_basic_parser(byte *str, void *ptr);
527 { sizeof(int), cf_parse_int },
528 { sizeof(u64), cf_parse_u64 },
529 { sizeof(double), cf_parse_double },
530 { sizeof(u32), cf_parse_ip },
531 { sizeof(byte*), cf_parse_string }
535 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type)
537 for (uns i=0; i<number; i++)
539 byte *msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
541 return cf_printf("Cannot parse item %d: %s", i+1, msg);
549 static byte *op_names[] = { CF_OPERATIONS };
552 #define OP_MASK 0xff // only get the operation
553 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
554 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
555 #define OP_2ND 0x400 // in the 2nd phase real data are entered
558 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
560 enum cf_type type = item->u.type;
561 cf_journal_block(ptr, sizeof(void*));
562 // boundary checks done by the caller
563 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
564 * (uns*) (*ptr - parsers[type].size) = number;
565 return cf_parse_ary(number, pars, *ptr, type);
569 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum operation op)
571 enum cf_type type = item->u.type;
573 int old_nr = * (int*) (old_p - parsers[type].size);
574 int taken = MIN(number, item->number-old_nr);
576 // stretch the dynamic array
577 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
578 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
579 cf_journal_block(ptr, sizeof(void*));
581 if (op == OP_APPEND) {
582 memcpy(new_p, old_p, old_nr * parsers[type].size);
583 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type);
584 } else if (op == OP_PREPEND) {
585 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
586 return cf_parse_ary(taken, pars, new_p, type);
588 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
591 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
594 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
597 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
600 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
602 return cf_printf("Item %s: %s", ci->name, msg);
606 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
613 add_to_list(struct cnode *where, struct cnode *new_node, enum operation op)
617 case OP_EDIT: // edition has been done in-place
620 cf_journal_block(&where->prev->next, sizeof(void*));
621 cf_journal_block(&where->next->prev, sizeof(void*));
624 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
626 cf_journal_block(&where->next->prev, sizeof(void*));
627 cf_journal_block(&where->next, sizeof(void*));
628 clist_insert_after(new_node, where);
630 case OP_BEFORE: // implementation dependend (append_tail = before(list))
633 cf_journal_block(&where->prev->next, sizeof(void*));
634 cf_journal_block(&where->prev, sizeof(void*));
635 clist_insert_before(new_node, where);
643 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum operation op)
646 return cf_printf("You have to open a block for operation %s", op_names[op]);
648 return "Nothing to add to the list";
649 struct cf_section *sec = item->u.sec;
653 void *node = cf_malloc(sec->size);
654 cf_init_section(item->name, sec, node, 1);
655 add_to_list(ptr, node, op);
657 /* If the node contains any dynamic attribute at the end, we suppress
658 * auto-repetition here and pass the flag inside instead. */
659 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
663 if (sec->flags & SEC_FLAG_DYNAMIC)
670 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
677 return "Missing value";
678 taken = MIN(number, item->number);
680 cf_journal_block(ptr, taken * parsers[item->u.type].size);
681 return cf_parse_ary(taken, pars, ptr, item->u.type);
684 return "Dynamic array cannot be used here";
685 taken = MIN(number, item->number);
687 return interpret_set_dynamic(item, taken, pars, ptr);
689 if (item->number < 0 && !allow_dynamic)
690 return "Parsers with variable number of parameters cannot be used here";
691 if (item->number > 0 && number < item->number)
692 return "Not enough parameters available for the parser";
693 taken = MIN(number, ABS(item->number));
695 for (int i=0; i<taken; i++)
696 pars[i] = cf_strdup(pars[i]);
697 return item->u.par(taken, pars, ptr);
699 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
702 return "Lists cannot be used here";
703 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
710 interpret_clear(struct cf_item *item, void *ptr)
712 if (item->cls == CC_LIST) {
713 cf_journal_block(ptr, sizeof(struct clist));
715 } else if (item->cls == CC_DYNAMIC) {
716 cf_journal_block(ptr, sizeof(void *));
717 * (void**) ptr = NULL;
719 return "The item is not a list or a dynamic array";
724 cmp_items(void *i1, void *i2, struct cf_item *item)
726 ASSERT(item->cls == CC_STATIC);
727 i1 += (addr_int_t) item->ptr;
728 i2 += (addr_int_t) item->ptr;
729 if (item->u.type == CT_STRING)
730 return strcmp(* (byte**) i1, * (byte**) i2);
731 else // all numeric types
732 return memcmp(i1, i2, parsers[item->u.type].size);
736 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
742 for (uns i=0; i<32; i++)
744 if (cmp_items(n, query, sec->cfg+i))
756 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
758 uns nr = sec->flags & SEC_FLAG_NUMBER;
759 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
761 uns i = item - sec->cfg;
763 return "Cannot select list nodes by this attribute";
764 if (sec->cfg[i].cls != CC_STATIC)
765 return "Selection can only be done based on basic attributes";
771 #define MAX_STACK_SIZE 100
772 static struct item_stack {
773 struct cf_section *sec; // nested section
774 void *base_ptr; // because original pointers are often relative
775 enum operation op; // it is performed when a closing brace is encountered
776 void *list; // list the operations should be done on
777 u32 mask; // bit array of selectors searching in a list
778 struct cf_item *item; // cf_item of the list
779 } stack[MAX_STACK_SIZE];
783 opening_brace(struct cf_item *item, void *ptr, enum operation op)
785 if (level >= MAX_STACK_SIZE-1)
786 return "Too many nested sections";
787 stack[++level] = (struct item_stack) {
795 if (!item) // unknown is ignored; we just need to trace recursion
797 stack[level].sec = item->u.sec;
798 if (item->cls == CC_SECTION)
800 stack[level].base_ptr = ptr;
801 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
803 else if (item->cls == CC_LIST)
805 stack[level].base_ptr = cf_malloc(item->u.sec->size);
806 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
807 stack[level].list = ptr;
808 stack[level].item = item;
809 stack[level].op |= (op & OP_MASK) < OP_REMOVE ? OP_2ND : OP_1ST;
812 return "Opening brace can only be used on sections and lists";
817 closing_brace(struct item_stack *st, enum operation op, int number, byte **pars)
819 if (st->op == OP_CLOSE) // top-level
820 return "Unmatched } parenthese";
821 if (!st->sec) { // dummy run on unknown section
826 enum operation pure_op = st->op & OP_MASK;
829 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
831 return "Cannot find a node matching the query";
832 if (pure_op != OP_REMOVE)
834 if (pure_op == OP_EDIT)
835 st->base_ptr = st->list;
836 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
837 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
840 if (op & OP_OPEN) { // stay at the same recursion level
841 st->op = (st->op | OP_2ND) & ~OP_1ST;
844 int taken; // parse parameters on 1 line immediately
845 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
848 // and fall-thru to the 2nd phase
851 add_to_list(st->list, st->base_ptr, pure_op);
854 return "No parameters expected after the }";
855 else if (op & OP_OPEN)
856 return "No { is expected";
862 interpret_line(byte *name, enum operation op, int number, byte **pars)
865 if ((op & OP_MASK) == OP_CLOSE)
866 return closing_brace(stack+level, op, number, pars);
867 void *ptr = stack[level].base_ptr;
868 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
871 if (stack[level].op & OP_1ST)
872 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
873 if (op & OP_OPEN) // the operation will be performed after the closing brace
874 return opening_brace(item, ptr, op);
875 if (!item) // ignored item in an unknown section
879 int taken; // process as many parameters as possible
881 taken = 0, msg = interpret_clear(item, ptr);
882 else if (op == OP_SET)
883 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
884 else if (item->cls == CC_DYNAMIC)
885 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
886 else if (item->cls == CC_LIST)
887 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
889 return cf_printf("Operation %s not supported on attribute class %d", op_names[op], item->cls);
893 return cf_printf("Too many parameters: %d>%d", number, taken);
899 cf_write_item(struct cf_item *item, enum operation op, int number, byte **pars)
905 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
909 msg = interpret_clear(item, item->ptr);
913 if (item->cls == CC_DYNAMIC)
914 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
915 else if (item->cls == CC_LIST)
916 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
918 return "The attribute class does not support append/prepend";
921 return "Unsupported operation";
926 return "Too many parameters";
935 stack[0] = (struct item_stack) {
949 log(L_ERROR, "Unterminated block");
952 if (commit_section("top-level", §ions, NULL))
957 /* Text file parser */
959 static struct fastbuf *parse_fb;
962 #define MAX_LINE 4096
963 static byte line_buf[MAX_LINE];
964 static byte *line = line_buf;
966 #include "lib/bbuf.h"
967 static bb_t copy_buf;
970 #define GBUF_TYPE uns
971 #define GBUF_PREFIX(x) split_##x
972 #include "lib/gbuf.h"
973 static split_t word_buf;
975 static uns ends_by_brace; // the line is ended by "{"
980 if (!bgets(parse_fb, line_buf, MAX_LINE))
984 while (Cblank(*line))
990 append(byte *start, byte *end)
992 uns len = end - start;
993 bb_grow(©_buf, copied + len + 1);
994 memcpy(copy_buf.ptr + copied, start, len);
996 copy_buf.ptr[copied-1] = 0;
999 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1000 // these characters separate words like blanks
1003 get_word(uns is_command_name)
1005 if (*line == '\'') {
1009 while (*line && *line != '\'')
1011 append(start, line);
1014 copy_buf.ptr[copied-1] = '\n';
1016 return "Unterminated apostrophe word at the end";
1020 } else if (*line == '"') {
1022 uns start_copy = copied;
1027 if (*line == '"' && !escape)
1029 else if (*line == '\\')
1035 append(start, line);
1039 copy_buf.ptr[copied-1] = '\n';
1040 else // merge two lines
1043 return "Unterminated quoted word at the end";
1047 for (byte *c=copy_buf.ptr+start_copy; *c; c++)
1050 return "Formating sequences are not allowed";
1054 byte *tmp = cf_printf(copy_buf.ptr + start_copy);
1055 uns l = strlen(tmp);
1056 bb_grow(©_buf, start_copy + l + 1);
1057 strcpy(copy_buf.ptr + start_copy, tmp);
1058 copied = start_copy + l + 1;
1061 // promised that *line is non-null and non-blank
1063 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1064 && (*line != '=' || !is_command_name))
1066 if (*line == '=') { // nice for setting from a command-line
1068 return "Assignment without a variable";
1071 if (line == start) // already the first char is control
1073 append(start, line);
1075 while (Cblank(*line))
1081 get_token(uns is_command_name, byte **msg)
1085 if (!*line || *line == '#') {
1086 if (!is_command_name || !get_line())
1088 } else if (*line == ';') {
1090 if (!is_command_name || *msg)
1092 } else if (*line == '\\' && !line[1]) {
1094 *msg = "Last line ends by a backslash";
1097 if (!*line || *line == '#')
1098 log(L_WARN, "The line following the backslash is empty");
1100 split_grow(&word_buf, words+1);
1102 word_buf.ptr[words++] = copied;
1103 *msg = get_word(is_command_name);
1104 return *msg ? NULL : copy_buf.ptr + start;
1112 words = copied = ends_by_brace = 0;
1113 byte *msg, *start_word;
1114 if (!(start_word = get_token(1, &msg)))
1116 if (*start_word == '{') // only one opening brace
1117 return "Unexpected opening brace";
1118 while (*line != '}') // stays for the next time
1120 if (!(start_word = get_token(0, &msg)))
1122 if (*start_word == '{') {
1123 words--; // discard the brace
1131 /* Parsing multiple files */
1133 static struct fastbuf *
1134 bopen_safe(byte *name)
1136 int fd = sh_open(name, O_RDONLY);
1138 log(L_ERROR, "Cannot open %s", name);
1141 return bopen(name, O_RDONLY, 1<<14);
1145 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1154 msg = split_command();
1159 byte *name = copy_buf.ptr + word_buf.ptr[0];
1160 byte *pars[words-1];
1161 for (uns i=1; i<words; i++)
1162 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1163 if (!strcasecmp(name, "include"))
1166 msg = "Expecting one filename";
1170 msg = "Too many nested files";
1173 struct fastbuf *new_fb = bopen_safe(pars[0]);
1175 msg = "Cannot open file";
1179 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1187 byte *c = strchr(name, ':');
1189 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1192 switch (Clocase(*c)) {
1193 case 's': op = OP_SET; break;
1194 case 'c': op = OP_CLEAR; break;
1195 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1196 case 'p': op = OP_PREPEND; break;
1197 case 'r': op = OP_REMOVE; break;
1198 case 'e': op = OP_EDIT; break;
1199 case 'b': op = OP_BEFORE; break;
1200 default: op = OP_SET; break;
1202 if (strcasecmp(c, op_names[op])) {
1203 msg = cf_printf("Unknown operation %s", c);
1209 msg = interpret_line(name, op, words-1, pars);
1214 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1215 return "included from here";
1218 #ifndef DEFAULT_CONFIG
1219 #define DEFAULT_CONFIG NULL
1221 byte *cf_def_file = DEFAULT_CONFIG;
1224 load_file(byte *file)
1227 struct fastbuf *fb = bopen_safe(file);
1230 byte *msg = parse_fastbuf(file, fb, 0);
1232 int err = !!msg || done_stack();
1239 load_string(byte *string)
1243 fbbuf_init_read(&fb, string, strlen(string), 0);
1244 byte *msg = parse_fastbuf("memory string", &fb, 0);
1245 return !!msg || done_stack();
1248 /* Command-line parser */
1254 if (cf_load(cf_def_file))
1255 die("Cannot load default config %s", cf_def_file);
1259 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1261 static int other_options = 0;
1263 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1264 if (res == 'S' || res == 'C')
1267 die("The -S and -C options must precede all other arguments");
1271 die("Cannot set %s", optarg);
1273 if (cf_load(optarg))
1274 die("Cannot load %s", optarg);
1277 /* unhandled option or end of options */
1288 spaces(struct fastbuf *fb, uns nr)
1290 for (uns i=0; i<nr; i++)
1295 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type)
1298 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1299 case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break;
1300 case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break;
1301 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1302 case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break;
1306 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1309 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1311 ptr += (addr_int_t) item->ptr;
1312 enum cf_type type = item->u.type;
1315 bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1316 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1317 bprintf(fb, "t%d ", type);
1318 if (item->cls == CC_STATIC) {
1319 for (i=0; i<item->number; i++)
1320 dump_basic(fb, ptr + i * parsers[type].size, type);
1321 } else if (item->cls == CC_DYNAMIC) {
1322 ptr = * (void**) ptr;
1324 int real_nr = * (int*) (ptr - parsers[type].size);
1325 bprintf(fb, "##%d ", real_nr);
1326 for (i=0; i<real_nr; i++)
1327 dump_basic(fb, ptr + i * parsers[type].size, type);
1329 bprintf(fb, "NULL ");
1332 if (item->cls == CC_SECTION)
1333 dump_section(fb, item->u.sec, level+1, ptr);
1334 else if (item->cls == CC_LIST) {
1337 CLIST_WALK(n, * (clist*) ptr) {
1338 spaces(fb, level+1);
1339 bprintf(fb, "item %d\n", ++idx);
1340 dump_section(fb, item->u.sec, level+2, n);
1346 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1349 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1350 for (struct cf_item *item=sec->cfg; item->cls; item++)
1351 dump_item(fb, item, level, ptr);
1355 cf_dump_sections(struct fastbuf *fb)
1357 dump_section(fb, §ions, 0, NULL);