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_transaction(uns new_pool)
110 cf_pool = mp_new(1<<10);
111 struct journal_item *oldj = journal;
117 journal_commit_transaction(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_transaction(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(ptr + (addr_int_t) 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)) {
249 log(L_ERROR, "It happened in section %s", ci->name);
252 } else if (ci->cls == CC_LIST) {
255 CLIST_WALK(n, * (clist*) (ptr + (addr_int_t) ci->ptr))
256 if (idx++, commit_section(ci->name, ci->u.sec, n)) {
257 log(L_ERROR, "It happened in node #%d of list %s", idx, ci->name);
262 byte *msg = sec->commit(ptr);
264 log(L_ERROR, "Cannot commit section %s: %s", name, msg);
271 static struct cf_item *
272 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
275 if (name[0] == '^') // absolute name instead of relative
276 name++, curr_sec = §ions, *ptr = NULL;
277 if (!curr_sec) // don't even search in an unknown section
281 byte *c = strchr(name, '.');
284 struct cf_item *ci = find_subitem(curr_sec, name);
287 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
288 *msg = cf_printf("Unknown item %s", name);
291 *ptr += (addr_int_t) ci->ptr;
294 if (ci->cls != CC_SECTION)
296 *msg = cf_printf("Item %s is not a section", name);
299 curr_sec = ci->u.sec;
305 cf_find_item(byte *name, struct cf_item *item)
309 struct cf_item *ci = find_item(§ions, name, &msg, &ptr);
317 /* Safe loading and reloading */
319 static int load_file(byte *file);
320 static int load_string(byte *string);
323 cf_reload(byte *file)
326 struct journal_item *oldj = journal_new_transaction(1);
327 int err = load_file(file);
330 for (struct old_pools *p=pools; p; p=pools)
335 journal_commit_transaction(1, NULL);
339 journal_rollback_transaction(1, oldj);
348 struct journal_item *oldj = journal_new_transaction(1);
349 int err = load_file(file);
351 journal_commit_transaction(1, oldj);
353 journal_rollback_transaction(1, oldj);
360 struct journal_item *oldj = journal_new_transaction(0);
361 int err = load_string(string);
363 journal_commit_transaction(0, oldj);
365 journal_rollback_transaction(0, oldj);
369 /* Parsers for standard types */
372 uns name; // one-letter name of the unit
373 uns num, den; // fraction
376 static const struct unit units[] = {
381 { 'g', 1000000000, 1 },
384 { 'G', 1073741824, 1 },
389 static const struct unit *
390 lookup_unit(byte *value, byte *end, byte **msg)
393 if (end == value || end[1] || *end >= '0' && *end <= '9')
394 *msg = "Invalid number";
396 for (const struct unit *u=units; u->name; u++)
399 *msg = "Invalid unit";
405 static char cf_rngerr[] = "Number out of range";
408 cf_parse_int(byte *str, int *ptr)
412 msg = "Missing number";
414 const struct unit *u;
417 uns x = strtoul(str, &end, 0);
420 else if (u = lookup_unit(str, end, &msg)) {
421 u64 y = (u64)x * u->num;
423 msg = "Number is not an integer";
437 cf_parse_u64(byte *str, u64 *ptr)
441 msg = "Missing number";
443 const struct unit *u;
446 u64 x = strtoull(str, &end, 0);
449 else if (u = lookup_unit(str, end, &msg)) {
450 if (x > ~(u64)0 / u->num)
451 msg = "Number out of range";
455 msg = "Number is not an integer";
466 cf_parse_double(byte *str, double *ptr)
470 msg = "Missing number";
472 const struct unit *u;
475 if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
476 msg = "Invalid number";
477 else if (u = lookup_unit(str, str + read_chars, &msg))
478 *ptr = x * u->num / u->den;
486 cf_parse_ip(byte *p, u32 *varp)
489 return "Missing IP address";
492 if (*p == '0' && p[1] | 32 == 'X') {
494 x = strtoul(p + 2, &p2, 16);
495 if (errno == ERANGE || p2 == (char*) (p+2) || x > 0xffffffff)
500 for (uns i = 0; i < 4; i++) {
506 uns y = strtoul(p, &p2, 10);
507 if (errno == ERANGE || p2 == (char*) p || y > 255)
513 return *p ? "Trailing characters" : NULL;
515 return "Invalid IP address";
519 cf_parse_string(byte *str, byte **ptr)
521 *ptr = cf_strdup(str);
525 /* Register size of and parser for each basic type */
527 typedef byte *cf_basic_parser(byte *str, void *ptr);
532 { sizeof(int), cf_parse_int },
533 { sizeof(u64), cf_parse_u64 },
534 { sizeof(double), cf_parse_double },
535 { sizeof(u32), cf_parse_ip },
536 { sizeof(byte*), cf_parse_string }
540 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type)
542 for (uns i=0; i<number; i++)
544 byte *msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
546 return cf_printf("Cannot parse item %d: %s", i+1, msg);
554 static byte *op_names[] = { CF_OPERATIONS };
557 #define OP_MASK 0xff // only get the operation
558 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
559 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
560 #define OP_2ND 0x400 // in the 2nd phase real data are entered
563 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
565 enum cf_type type = item->u.type;
566 cf_journal_block(ptr, sizeof(void*));
567 // boundary checks done by the caller
568 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
569 * (uns*) (*ptr - parsers[type].size) = number;
570 return cf_parse_ary(number, pars, *ptr, type);
574 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
576 enum cf_type type = item->u.type;
578 int old_nr = * (int*) (old_p - parsers[type].size);
579 int taken = MIN(number, item->number-old_nr);
581 // stretch the dynamic array
582 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
583 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
584 cf_journal_block(ptr, sizeof(void*));
586 if (op == OP_APPEND) {
587 memcpy(new_p, old_p, old_nr * parsers[type].size);
588 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type);
589 } else if (op == OP_PREPEND) {
590 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
591 return cf_parse_ary(taken, pars, new_p, type);
593 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
596 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
599 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
602 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
605 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
607 return cf_printf("Item %s: %s", ci->name, msg);
611 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
618 add_to_list(struct cnode *where, struct cnode *new_node, enum cf_operation op)
622 case OP_EDIT: // edition has been done in-place
625 cf_journal_block(&where->prev->next, sizeof(void*));
626 cf_journal_block(&where->next->prev, sizeof(void*));
629 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
631 cf_journal_block(&where->next->prev, sizeof(void*));
632 cf_journal_block(&where->next, sizeof(void*));
633 clist_insert_after(new_node, where);
635 case OP_BEFORE: // implementation dependend (append_tail = before(list))
638 cf_journal_block(&where->prev->next, sizeof(void*));
639 cf_journal_block(&where->prev, sizeof(void*));
640 clist_insert_before(new_node, where);
648 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
651 return cf_printf("You have to open a block for operation %s", op_names[op]);
653 return "Nothing to add to the list";
654 struct cf_section *sec = item->u.sec;
658 void *node = cf_malloc(sec->size);
659 cf_init_section(item->name, sec, node, 1);
660 add_to_list(ptr, node, op);
662 /* If the node contains any dynamic attribute at the end, we suppress
663 * auto-repetition here and pass the flag inside instead. */
664 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
668 if (sec->flags & SEC_FLAG_DYNAMIC)
675 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
682 return "Missing value";
683 taken = MIN(number, item->number);
685 cf_journal_block(ptr, taken * parsers[item->u.type].size);
686 return cf_parse_ary(taken, pars, ptr, item->u.type);
689 return "Dynamic array cannot be used here";
690 taken = MIN(number, item->number);
692 return interpret_set_dynamic(item, taken, pars, ptr);
694 if (item->number < 0 && !allow_dynamic)
695 return "Parsers with variable number of parameters cannot be used here";
696 if (item->number > 0 && number < item->number)
697 return "Not enough parameters available for the parser";
698 taken = MIN(number, ABS(item->number));
700 for (int i=0; i<taken; i++)
701 pars[i] = cf_strdup(pars[i]);
702 return item->u.par(taken, pars, ptr);
704 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
707 return "Lists cannot be used here";
708 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
715 interpret_clear(struct cf_item *item, void *ptr)
717 if (item->cls == CC_LIST) {
718 cf_journal_block(ptr, sizeof(struct clist));
720 } else if (item->cls == CC_DYNAMIC) {
721 cf_journal_block(ptr, sizeof(void *));
722 * (void**) ptr = NULL;
724 return "The item is not a list or a dynamic array";
729 cmp_items(void *i1, void *i2, struct cf_item *item)
731 ASSERT(item->cls == CC_STATIC);
732 i1 += (addr_int_t) item->ptr;
733 i2 += (addr_int_t) item->ptr;
734 if (item->u.type == CT_STRING)
735 return strcmp(* (byte**) i1, * (byte**) i2);
736 else // all numeric types
737 return memcmp(i1, i2, parsers[item->u.type].size);
741 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
747 for (uns i=0; i<32; i++)
749 if (cmp_items(n, query, sec->cfg+i))
761 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
763 uns nr = sec->flags & SEC_FLAG_NUMBER;
764 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
766 uns i = item - sec->cfg;
768 return "Cannot select list nodes by this attribute";
769 if (sec->cfg[i].cls != CC_STATIC)
770 return "Selection can only be done based on basic attributes";
776 #define MAX_STACK_SIZE 100
777 static struct item_stack {
778 struct cf_section *sec; // nested section
779 void *base_ptr; // because original pointers are often relative
780 enum cf_operation op; // it is performed when a closing brace is encountered
781 void *list; // list the operations should be done on
782 u32 mask; // bit array of selectors searching in a list
783 struct cf_item *item; // cf_item of the list
784 } stack[MAX_STACK_SIZE];
788 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
790 if (level >= MAX_STACK_SIZE-1)
791 return "Too many nested sections";
792 stack[++level] = (struct item_stack) {
800 if (!item) // unknown is ignored; we just need to trace recursion
802 stack[level].sec = item->u.sec;
803 if (item->cls == CC_SECTION)
805 stack[level].base_ptr = ptr;
806 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
808 else if (item->cls == CC_LIST)
810 stack[level].base_ptr = cf_malloc(item->u.sec->size);
811 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
812 stack[level].list = ptr;
813 stack[level].item = item;
814 if ((op & OP_MASK) < OP_REMOVE) {
815 add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
816 stack[level].op |= OP_2ND;
818 stack[level].op |= OP_1ST;
821 return "Opening brace can only be used on sections and lists";
826 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
828 if (st->op == OP_CLOSE) // top-level
829 return "Unmatched } parenthesis";
830 if (!st->sec) { // dummy run on unknown section
835 enum cf_operation pure_op = st->op & OP_MASK;
838 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
840 return "Cannot find a node matching the query";
841 if (pure_op != OP_REMOVE)
843 if (pure_op == OP_EDIT)
844 st->base_ptr = st->list;
845 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
846 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
849 if (op & OP_OPEN) { // stay at the same recursion level
850 st->op = (st->op | OP_2ND) & ~OP_1ST;
851 add_to_list(st->list, st->base_ptr, pure_op);
854 int taken; // parse parameters on 1 line immediately
855 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
858 // and fall-thru to the 2nd phase
860 add_to_list(st->list, st->base_ptr, pure_op);
864 return "No parameters expected after the }";
865 else if (op & OP_OPEN)
866 return "No { is expected";
872 interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
875 if ((op & OP_MASK) == OP_CLOSE)
876 return closing_brace(stack+level, op, number, pars);
877 void *ptr = stack[level].base_ptr;
878 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
881 if (stack[level].op & OP_1ST)
882 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
883 if (op & OP_OPEN) // the operation will be performed after the closing brace
884 return opening_brace(item, ptr, op);
885 if (!item) // ignored item in an unknown section
889 int taken; // process as many parameters as possible
891 taken = 0, msg = interpret_clear(item, ptr);
892 else if (op == OP_SET)
893 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
894 else if (item->cls == CC_DYNAMIC)
895 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
896 else if (item->cls == CC_LIST)
897 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
899 return cf_printf("Operation %s not supported on attribute %s", op_names[op], name);
903 return cf_printf("Too many parameters: %d>%d", number, taken);
909 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
915 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
919 msg = interpret_clear(item, item->ptr);
923 if (item->cls == CC_DYNAMIC)
924 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
925 else if (item->cls == CC_LIST)
926 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
928 return "The attribute class does not support append/prepend";
931 return "Unsupported operation";
936 return "Too many parameters";
945 stack[0] = (struct item_stack) {
959 log(L_ERROR, "Unterminated block");
962 if (commit_section("top-level", §ions, NULL))
967 /* Text file parser */
969 static byte *name_parse_fb;
970 static struct fastbuf *parse_fb;
973 #define MAX_LINE 4096
974 static byte line_buf[MAX_LINE];
975 static byte *line = line_buf;
977 #include "lib/bbuf.h"
978 static bb_t copy_buf;
981 #define GBUF_TYPE uns
982 #define GBUF_PREFIX(x) split_##x
983 #include "lib/gbuf.h"
984 static split_t word_buf;
986 static uns ends_by_brace; // the line is ended by "{"
991 if (!bgets(parse_fb, line_buf, MAX_LINE))
995 while (Cblank(*line))
1001 append(byte *start, byte *end)
1003 uns len = end - start;
1004 bb_grow(©_buf, copied + len + 1);
1005 memcpy(copy_buf.ptr + copied, start, len);
1007 copy_buf.ptr[copied-1] = 0;
1010 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1011 // these characters separate words like blanks
1014 get_word(uns is_command_name)
1016 if (*line == '\'') {
1020 while (*line && *line != '\'')
1022 append(start, line);
1025 copy_buf.ptr[copied-1] = '\n';
1027 return "Unterminated apostrophe word at the end";
1031 } else if (*line == '"') {
1033 uns start_copy = copied;
1038 if (*line == '"' && !escape)
1040 else if (*line == '\\')
1046 append(start, line);
1050 copy_buf.ptr[copied-1] = '\n';
1051 else // merge two lines
1054 return "Unterminated quoted word at the end";
1058 // FIXME: this is utterly bogus, since printf() only expands these
1059 // percents, and the \n, \x1b codes are actually expanded by the compiler
1060 for (byte *c=copy_buf.ptr+start_copy; *c; c++)
1063 return "Formating sequences are not allowed";
1067 byte *tmp = cf_printf(copy_buf.ptr + start_copy);
1068 uns l = strlen(tmp);
1069 bb_grow(©_buf, start_copy + l + 1);
1070 strcpy(copy_buf.ptr + start_copy, tmp);
1071 copied = start_copy + l + 1;
1074 // promised that *line is non-null and non-blank
1076 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1077 && (*line != '=' || !is_command_name))
1079 if (*line == '=') { // nice for setting from a command-line
1081 return "Assignment without a variable";
1084 if (line == start) // already the first char is control
1086 append(start, line);
1088 while (Cblank(*line))
1094 get_token(uns is_command_name, byte **msg)
1098 if (!*line || *line == '#') {
1099 if (!is_command_name || !get_line())
1101 } else if (*line == ';') {
1103 if (!is_command_name || *msg)
1105 } else if (*line == '\\' && !line[1]) {
1107 *msg = "Last line ends by a backslash";
1110 if (!*line || *line == '#')
1111 log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
1113 split_grow(&word_buf, words+1);
1115 word_buf.ptr[words++] = copied;
1116 *msg = get_word(is_command_name);
1117 return *msg ? NULL : copy_buf.ptr + start;
1125 words = copied = ends_by_brace = 0;
1126 byte *msg, *start_word;
1127 if (!(start_word = get_token(1, &msg)))
1129 if (*start_word == '{') // only one opening brace
1130 return "Unexpected opening brace";
1131 while (*line != '}') // stays for the next time
1133 if (!(start_word = get_token(0, &msg)))
1135 if (*start_word == '{') {
1136 words--; // discard the brace
1144 /* Parsing multiple files */
1146 static struct fastbuf *
1147 bopen_safe(byte *name)
1149 int fd = sh_open(name, O_RDONLY);
1151 log(L_ERROR, "Cannot open %s", name);
1154 return bopen(name, O_RDONLY, 1<<14);
1158 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1161 name_parse_fb = name_fb;
1168 msg = split_command();
1173 byte *name = copy_buf.ptr + word_buf.ptr[0];
1174 byte *pars[words-1];
1175 for (uns i=1; i<words; i++)
1176 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1177 if (!strcasecmp(name, "include"))
1180 msg = "Expecting one filename";
1182 msg = "Too many nested files";
1183 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
1184 msg = "The input command must be the last one on a line";
1187 struct fastbuf *new_fb = bopen_safe(pars[0]);
1189 msg = "Cannot open file";
1193 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1200 enum cf_operation op;
1201 byte *c = strchr(name, ':');
1203 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1206 switch (Clocase(*c)) {
1207 case 's': op = OP_SET; break;
1208 case 'c': op = OP_CLEAR; break;
1209 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1210 case 'p': op = OP_PREPEND; break;
1211 case 'r': op = OP_REMOVE; break;
1212 case 'e': op = OP_EDIT; break;
1213 case 'b': op = OP_BEFORE; break;
1214 default: op = OP_SET; break;
1216 if (strcasecmp(c, op_names[op])) {
1217 msg = cf_printf("Unknown operation %s", c);
1223 msg = interpret_line(name, op, words-1, pars);
1228 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1229 return "included from here";
1232 #ifndef DEFAULT_CONFIG
1233 #define DEFAULT_CONFIG NULL
1235 byte *cf_def_file = DEFAULT_CONFIG;
1238 load_file(byte *file)
1241 struct fastbuf *fb = bopen_safe(file);
1244 byte *msg = parse_fastbuf(file, fb, 0);
1246 int err = !!msg || done_stack();
1253 load_string(byte *string)
1257 fbbuf_init_read(&fb, string, strlen(string), 0);
1258 byte *msg = parse_fastbuf("memory string", &fb, 0);
1259 return !!msg || done_stack();
1262 /* Command-line parser */
1268 if (cf_load(cf_def_file))
1269 die("Cannot load default config %s", cf_def_file);
1273 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1275 static int other_options = 0;
1277 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1278 if (res == 'S' || res == 'C')
1281 die("The -S and -C options must precede all other arguments");
1285 die("Cannot set %s", optarg);
1287 if (cf_load(optarg))
1288 die("Cannot load %s", optarg);
1291 /* unhandled option or end of options */
1302 spaces(struct fastbuf *fb, uns nr)
1304 for (uns i=0; i<nr; i++)
1309 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type)
1312 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1313 case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break;
1314 case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break;
1315 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1316 case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break;
1320 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1323 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1325 ptr += (addr_int_t) item->ptr;
1326 enum cf_type type = item->u.type;
1329 bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1330 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1331 bprintf(fb, "t%d ", type);
1332 if (item->cls == CC_STATIC) {
1333 for (i=0; i<item->number; i++)
1334 dump_basic(fb, ptr + i * parsers[type].size, type);
1335 } else if (item->cls == CC_DYNAMIC) {
1336 ptr = * (void**) ptr;
1338 int real_nr = * (int*) (ptr - parsers[type].size);
1339 bprintf(fb, "##%d ", real_nr);
1340 for (i=0; i<real_nr; i++)
1341 dump_basic(fb, ptr + i * parsers[type].size, type);
1343 bprintf(fb, "NULL ");
1346 if (item->cls == CC_SECTION)
1347 dump_section(fb, item->u.sec, level+1, ptr);
1348 else if (item->cls == CC_LIST) {
1351 CLIST_WALK(n, * (clist*) ptr) {
1352 spaces(fb, level+1);
1353 bprintf(fb, "item %d\n", ++idx);
1354 dump_section(fb, item->u.sec, level+2, n);
1360 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1363 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1364 for (struct cf_item *item=sec->cfg; item->cls; item++)
1365 dump_item(fb, item, level, ptr);
1369 cf_dump_sections(struct fastbuf *fb)
1371 dump_section(fb, §ions, 0, NULL);
1375 * - more space efficient journal
1376 * - don't commit all, but recognize from the journal which sections have been changed