2 * UCW Library -- Reading of configuration files
4 * (c) 2001--2006 Robert Spalek <robert@ucw.cz>
5 * (c) 2003--2006 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/conf2.h"
13 #include "lib/mempool.h"
14 #include "lib/clists.h"
15 #include "lib/fastbuf.h"
16 #include "lib/chartype.h"
18 #include "lib/stkstring.h"
19 #include "lib/binsearch.h"
28 #define TRY(f) do { byte *_msg = f; if (_msg) return _msg; } while (0)
30 /* Memory allocation */
32 struct mempool *cf_pool; // current pool for loading new configuration
33 static struct old_pools {
34 struct old_pools *prev;
36 } *pools; // link-list of older cf_pool's
41 return mp_alloc(cf_pool, size);
45 cf_malloc_zero(uns size)
47 return mp_alloc_zero(cf_pool, size);
53 return mp_strdup(cf_pool, s);
57 cf_printf(char *fmt, ...)
61 byte *res = mp_vprintf(cf_pool, fmt, args);
68 uns cf_need_journal = 1; // some programs do not need journal
69 static struct journal_item {
70 struct journal_item *prev;
77 cf_journal_block(void *ptr, uns len)
81 struct journal_item *ji = cf_malloc(sizeof(struct journal_item) + len);
85 memcpy(ji->copy, ptr, len);
91 // swaps the contents of the memory and the journal, and reverses the list
93 struct journal_item *curr, *prev, *next;
94 for (next=NULL, curr=journal; curr; next=curr, curr=prev)
98 for (uns i=0; i<curr->len; i++)
100 byte x = curr->copy[i];
101 curr->copy[i] = curr->ptr[i];
108 static struct journal_item *
109 journal_new_transaction(uns new_pool)
112 cf_pool = mp_new(1<<10);
113 struct journal_item *oldj = journal;
119 journal_commit_transaction(uns new_pool, struct journal_item *oldj)
123 struct old_pools *p = cf_malloc(sizeof(struct old_pools));
130 struct journal_item **j = &journal;
138 journal_rollback_transaction(uns new_pool, struct journal_item *oldj)
140 if (!cf_need_journal)
141 die("Cannot rollback the configuration, because the journal is disabled.");
147 cf_pool = pools ? pools->pool : NULL;
153 struct dirty_section {
154 struct cf_section *sec;
157 #define GBUF_TYPE struct dirty_section
158 #define GBUF_PREFIX(x) dirtsec_##x
159 #include "lib/gbuf.h"
160 static dirtsec_t dirty;
162 static uns everything_committed; // after the 1st load, this flag is set on
165 add_dirty(struct cf_section *sec, void *ptr)
167 dirtsec_grow(&dirty, dirties+1);
168 struct dirty_section *dest = dirty.ptr + dirties;
169 if (dirties && dest[-1].sec == sec && dest[-1].ptr == ptr)
176 #define ASORT_PREFIX(x) dirtsec_##x
177 #define ASORT_KEY_TYPE struct dirty_section
178 #define ASORT_ELT(i) dirty.ptr[i]
179 #define ASORT_LT(x,y) x.sec < y.sec || x.sec == y.sec && x.ptr < y.ptr
180 #include "lib/arraysort.h"
187 dirtsec_sort(dirties);
188 struct dirty_section *read = dirty.ptr + 1, *write = dirty.ptr + 1, *limit = dirty.ptr + dirties;
189 while (read < limit) {
190 if (read->sec != read[-1].sec || read->ptr != read[-1].ptr) {
197 dirties = write - dirty.ptr;
202 #define SEC_FLAG_DYNAMIC 0x80000000 // contains a dynamic attribute
203 #define SEC_FLAG_UNKNOWN 0x40000000 // ignore unknown entriies
204 #define SEC_FLAG_NUMBER 0x0fffffff // number of entries
206 static struct cf_section sections; // root section
208 static struct cf_item *
209 find_subitem(struct cf_section *sec, byte *name)
211 struct cf_item *ci = sec->cfg;
212 for (; ci->cls; ci++)
213 if (!strcasecmp(ci->name, name))
219 inspect_section(struct cf_section *sec)
223 for (ci=sec->cfg; ci->cls; ci++)
224 if (ci->cls == CC_SECTION) {
225 inspect_section(ci->u.sec);
226 sec->flags |= ci->u.sec->flags & SEC_FLAG_DYNAMIC;
227 } else if (ci->cls == CC_LIST) {
228 inspect_section(ci->u.sec);
229 sec->flags |= SEC_FLAG_DYNAMIC;
230 } else if (ci->cls == CC_DYNAMIC || ci->cls == CC_PARSER && ci->number < 0)
231 sec->flags |= SEC_FLAG_DYNAMIC;
232 sec->flags |= ci - sec->cfg; // record the number of entries
236 cf_declare_section(byte *name, struct cf_section *sec, uns allow_unknown)
241 sections.cfg = xmalloc_zero(sections.size * sizeof(struct cf_item));
243 struct cf_item *ci = find_subitem(§ions, name);
245 die("Cannot register section %s twice", name);
246 ci->cls = CC_SECTION;
251 inspect_section(sec);
253 sec->flags |= SEC_FLAG_UNKNOWN;
255 if (ci - sections.cfg >= (int) sections.size)
257 sections.cfg = xrealloc(sections.cfg, 2*sections.size * sizeof(struct cf_item));
258 bzero(sections.cfg + sections.size, sections.size * sizeof(struct cf_item));
264 cf_init_section(byte *name, struct cf_section *sec, void *ptr, uns do_bzero)
268 bzero(ptr, sec->size);
270 for (uns i=0; sec->cfg[i].cls; i++)
271 if (sec->cfg[i].cls == CC_SECTION)
272 cf_init_section(sec->cfg[i].name, sec->cfg[i].u.sec, ptr + (addr_int_t) sec->cfg[i].ptr, 0);
273 else if (sec->cfg[i].cls == CC_LIST)
274 clist_init(ptr + (addr_int_t) sec->cfg[i].ptr);
276 byte *msg = sec->init(ptr);
278 die("Cannot initialize section %s: %s", name, msg);
285 static uns initialized = 0;
288 sections.flags |= SEC_FLAG_UNKNOWN;
289 sections.size = 0; // size of allocated array used to be stored here
290 cf_init_section("top-level", §ions, NULL, 0);
294 commit_section(byte *name, struct cf_section *sec, void *ptr, uns commit_all)
297 for (ci=sec->cfg; ci->cls; ci++)
298 if (ci->cls == CC_SECTION) {
299 if (commit_section(ci->name, ci->u.sec, ptr + (addr_int_t) ci->ptr, commit_all)) {
300 log(L_ERROR, "It happened in section %s", ci->name);
303 } else if (ci->cls == CC_LIST) {
306 CLIST_WALK(n, * (clist*) (ptr + (addr_int_t) ci->ptr))
307 if (idx++, commit_section(ci->name, ci->u.sec, n, commit_all)) {
308 log(L_ERROR, "It happened in node #%d of list %s", idx, ci->name);
313 /* We have to process the whole tree of sections even if just a few changes
314 * have been made, because there are dependencies between commit-hooks and
315 * hence we need to call them in a fixed order. */
316 #define ARY_LT_X(ary,i,x) ary[i].sec < x.sec || ary[i].sec == x.sec && ary[i].ptr < x.ptr
317 struct dirty_section comp = { sec, ptr };
318 uns pos = BIN_SEARCH_FIRST_GE_CMP(dirty.ptr, dirties, comp, ARY_LT_X);
321 || (pos < dirties && dirty.ptr[pos].sec == sec && dirty.ptr[pos].ptr == ptr)) {
322 byte *msg = sec->commit(ptr);
324 log(L_ERROR, "Cannot commit section %s: %s", name, msg);
332 static struct cf_item *
333 find_item(struct cf_section *curr_sec, byte *name, byte **msg, void **ptr)
336 if (name[0] == '^') // absolute name instead of relative
337 name++, curr_sec = §ions, *ptr = NULL;
338 if (!curr_sec) // don't even search in an unknown section
342 if (curr_sec != §ions)
343 add_dirty(curr_sec, *ptr);
344 byte *c = strchr(name, '.');
347 struct cf_item *ci = find_subitem(curr_sec, name);
350 if (!(curr_sec->flags & SEC_FLAG_UNKNOWN)) // ignore silently unknown top-level sections and unknown attributes in flagged sections
351 *msg = cf_printf("Unknown item %s", name);
354 *ptr += (addr_int_t) ci->ptr;
357 if (ci->cls != CC_SECTION)
359 *msg = cf_printf("Item %s is not a section", name);
362 curr_sec = ci->u.sec;
368 cf_find_item(byte *name, struct cf_item *item)
372 struct cf_item *ci = find_item(§ions, name, &msg, &ptr);
380 /* Safe loading and reloading */
382 static int load_file(byte *file);
383 static int load_string(byte *string);
386 cf_reload(byte *file)
389 struct journal_item *oldj = journal_new_transaction(1);
390 uns ec = everything_committed;
391 everything_committed = 0;
392 int err = load_file(file);
395 for (struct old_pools *p=pools; p; p=pools)
400 journal_commit_transaction(1, NULL);
404 everything_committed = ec;
405 journal_rollback_transaction(1, oldj);
414 struct journal_item *oldj = journal_new_transaction(1);
415 int err = load_file(file);
417 journal_commit_transaction(1, oldj);
419 journal_rollback_transaction(1, oldj);
426 struct journal_item *oldj = journal_new_transaction(0);
427 int err = load_string(string);
429 journal_commit_transaction(0, oldj);
431 journal_rollback_transaction(0, oldj);
435 /* Parsers for standard types */
438 uns name; // one-letter name of the unit
439 uns num, den; // fraction
442 static const struct unit units[] = {
447 { 'g', 1000000000, 1 },
450 { 'G', 1073741824, 1 },
455 static const struct unit *
456 lookup_unit(byte *value, byte *end, byte **msg)
459 if (end == value || end[1] || *end >= '0' && *end <= '9')
460 *msg = "Invalid number";
462 for (const struct unit *u=units; u->name; u++)
465 *msg = "Invalid unit";
471 static char cf_rngerr[] = "Number out of range";
474 cf_parse_int(byte *str, int *ptr)
478 msg = "Missing number";
480 const struct unit *u;
483 uns x = strtoul(str, &end, 0);
486 else if (u = lookup_unit(str, end, &msg)) {
487 u64 y = (u64)x * u->num;
489 msg = "Number is not an integer";
503 cf_parse_u64(byte *str, u64 *ptr)
507 msg = "Missing number";
509 const struct unit *u;
512 u64 x = strtoull(str, &end, 0);
515 else if (u = lookup_unit(str, end, &msg)) {
516 if (x > ~(u64)0 / u->num)
517 msg = "Number out of range";
521 msg = "Number is not an integer";
532 cf_parse_double(byte *str, double *ptr)
536 msg = "Missing number";
538 const struct unit *u;
541 if (sscanf(str, "%lf%n", &x, &read_chars) != 1)
542 msg = "Invalid number";
543 else if (u = lookup_unit(str, str + read_chars, &msg))
544 *ptr = x * u->num / u->den;
552 cf_parse_ip(byte *p, u32 *varp)
555 return "Missing IP address";
558 if (*p == '0' && p[1] | 32 == 'X' && Cxdigit(p[2])) {
560 x = strtoul(p, &p2, 16);
561 if (errno == ERANGE || x > 0xffffffff)
566 for (uns i = 0; i < 4; i++) {
574 uns y = strtoul(p, &p2, 10);
575 if (errno == ERANGE || p2 == (char*) p || y > 255)
581 return *p ? "Trailing characters" : NULL;
583 return "Invalid IP address";
587 cf_parse_string(byte *str, byte **ptr)
589 *ptr = cf_strdup(str);
594 cf_parse_lookup(byte *str, int *ptr, char **t)
598 while (*n && strcasecmp(*n, str)) {
599 total_len += strlen(*n) + 2;
606 byte *err = cf_malloc(total_len + strlen(str) + 60), *c = err;
607 c += sprintf(err, "Invalid value %s, possible values are: ", str);
609 c+= sprintf(c, "%s, ", *n);
614 /* Register size of and parser for each basic type */
616 typedef byte *cf_basic_parser(byte *str, void *ptr);
621 { sizeof(int), cf_parse_int },
622 { sizeof(u64), cf_parse_u64 },
623 { sizeof(double), cf_parse_double },
624 { sizeof(u32), cf_parse_ip },
625 { sizeof(byte*), cf_parse_string },
626 { sizeof(int), NULL } // lookups are parsed extra
630 cf_parse_ary(uns number, byte **pars, void *ptr, enum cf_type type, union cf_union *u)
632 for (uns i=0; i<number; i++)
635 if (type < CT_LOOKUP)
636 msg = ((cf_basic_parser*) parsers[type].parser) (pars[i], ptr + i * parsers[type].size);
637 else if (type == CT_LOOKUP)
638 msg = cf_parse_lookup(pars[i], ptr + i * sizeof(int), u->lookup);
642 return cf_printf("Cannot parse item %d: %s", i+1, msg);
650 static byte *op_names[] = { CF_OPERATIONS };
653 #define OP_MASK 0xff // only get the operation
654 #define OP_OPEN 0x100 // here we only get an opening brace instead of parameters
655 #define OP_1ST 0x200 // in the 1st phase selectors are recorded into the mask
656 #define OP_2ND 0x400 // in the 2nd phase real data are entered
659 interpret_set_dynamic(struct cf_item *item, int number, byte **pars, void **ptr)
661 enum cf_type type = item->type;
662 cf_journal_block(ptr, sizeof(void*));
663 // boundary checks done by the caller
664 *ptr = cf_malloc((number+1) * parsers[type].size) + parsers[type].size;
665 * (uns*) (*ptr - parsers[type].size) = number;
666 return cf_parse_ary(number, pars, *ptr, type, &item->u);
670 interpret_add_dynamic(struct cf_item *item, int number, byte **pars, int *processed, void **ptr, enum cf_operation op)
672 enum cf_type type = item->type;
674 int old_nr = * (int*) (old_p - parsers[type].size);
675 int taken = MIN(number, item->number-old_nr);
677 // stretch the dynamic array
678 void *new_p = cf_malloc((old_nr + taken + 1) * parsers[type].size) + parsers[type].size;
679 * (uns*) (new_p - parsers[type].size) = old_nr + taken;
680 cf_journal_block(ptr, sizeof(void*));
682 if (op == OP_APPEND) {
683 memcpy(new_p, old_p, old_nr * parsers[type].size);
684 return cf_parse_ary(taken, pars, new_p + old_nr * parsers[type].size, type, &item->u);
685 } else if (op == OP_PREPEND) {
686 memcpy(new_p + taken * parsers[type].size, old_p, old_nr * parsers[type].size);
687 return cf_parse_ary(taken, pars, new_p, type, &item->u);
689 return cf_printf("Dynamic arrays do not support operation %s", op_names[op]);
692 static byte *interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic);
695 interpret_section(struct cf_section *sec, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
699 for (struct cf_item *ci=sec->cfg; ci->cls; ci++)
702 byte *msg = interpret_set_item(ci, number, pars, &taken, ptr + (addr_int_t) ci->ptr, allow_dynamic && !ci[1].cls);
704 return cf_printf("Item %s: %s", ci->name, msg);
708 if (!number) // stop parsing, because many parsers would otherwise complain that number==0
715 add_to_list(struct cnode *where, struct cnode *new_node, enum cf_operation op)
719 case OP_EDIT: // edition has been done in-place
722 cf_journal_block(&where->prev->next, sizeof(void*));
723 cf_journal_block(&where->next->prev, sizeof(void*));
726 case OP_AFTER: // implementation dependend (prepend_head = after(list)), and where==list, see clists.h:74
728 cf_journal_block(&where->next->prev, sizeof(void*));
729 cf_journal_block(&where->next, sizeof(void*));
730 clist_insert_after(new_node, where);
732 case OP_BEFORE: // implementation dependend (append_tail = before(list))
735 cf_journal_block(&where->prev->next, sizeof(void*));
736 cf_journal_block(&where->prev, sizeof(void*));
737 clist_insert_before(new_node, where);
745 interpret_add_list(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, enum cf_operation op)
748 return cf_printf("You have to open a block for operation %s", op_names[op]);
750 return "Nothing to add to the list";
751 struct cf_section *sec = item->u.sec;
755 void *node = cf_malloc(sec->size);
756 cf_init_section(item->name, sec, node, 1);
757 add_to_list(ptr, node, op);
759 /* If the node contains any dynamic attribute at the end, we suppress
760 * auto-repetition here and pass the flag inside instead. */
761 TRY( interpret_section(sec, number, pars, &taken, node, sec->flags & SEC_FLAG_DYNAMIC) );
765 if (sec->flags & SEC_FLAG_DYNAMIC)
772 interpret_set_item(struct cf_item *item, int number, byte **pars, int *processed, void *ptr, uns allow_dynamic)
779 return "Missing value";
780 taken = MIN(number, item->number);
782 cf_journal_block(ptr, taken * parsers[item->type].size);
783 return cf_parse_ary(taken, pars, ptr, item->type, &item->u);
786 return "Dynamic array cannot be used here";
787 taken = MIN(number, item->number);
789 return interpret_set_dynamic(item, taken, pars, ptr);
791 if (item->number < 0 && !allow_dynamic)
792 return "Parsers with variable number of parameters cannot be used here";
793 if (item->number > 0 && number < item->number)
794 return "Not enough parameters available for the parser";
795 taken = MIN(number, ABS(item->number));
797 for (int i=0; i<taken; i++)
798 pars[i] = cf_strdup(pars[i]);
799 return item->u.par(taken, pars, ptr);
801 return interpret_section(item->u.sec, number, pars, processed, ptr, allow_dynamic);
804 return "Lists cannot be used here";
805 return interpret_add_list(item, number, pars, processed, ptr, OP_SET);
812 interpret_clear(struct cf_item *item, void *ptr)
814 if (item->cls == CC_LIST) {
815 cf_journal_block(ptr, sizeof(struct clist));
817 } else if (item->cls == CC_DYNAMIC) {
818 cf_journal_block(ptr, sizeof(void *));
819 * (void**) ptr = NULL;
821 return "The item is not a list or a dynamic array";
826 cmp_items(void *i1, void *i2, struct cf_item *item)
828 ASSERT(item->cls == CC_STATIC);
829 i1 += (addr_int_t) item->ptr;
830 i2 += (addr_int_t) item->ptr;
831 if (item->type == CT_STRING)
832 return strcmp(* (byte**) i1, * (byte**) i2);
833 else // all numeric types
834 return memcmp(i1, i2, parsers[item->type].size);
838 find_list_node(struct clist *list, void *query, struct cf_section *sec, u32 mask)
844 for (uns i=0; i<32; i++)
846 if (cmp_items(n, query, sec->cfg+i))
858 record_selector(struct cf_item *item, struct cf_section *sec, u32 *mask)
860 uns nr = sec->flags & SEC_FLAG_NUMBER;
861 if (item >= sec->cfg && item < sec->cfg + nr) // setting an attribute relative to this section
863 uns i = item - sec->cfg;
865 return "Cannot select list nodes by this attribute";
866 if (sec->cfg[i].cls != CC_STATIC)
867 return "Selection can only be done based on basic attributes";
873 #define MAX_STACK_SIZE 100
874 static struct item_stack {
875 struct cf_section *sec; // nested section
876 void *base_ptr; // because original pointers are often relative
877 enum cf_operation op; // it is performed when a closing brace is encountered
878 void *list; // list the operations should be done on
879 u32 mask; // bit array of selectors searching in a list
880 struct cf_item *item; // cf_item of the list
881 } stack[MAX_STACK_SIZE];
885 opening_brace(struct cf_item *item, void *ptr, enum cf_operation op)
887 if (level >= MAX_STACK_SIZE-1)
888 return "Too many nested sections";
889 stack[++level] = (struct item_stack) {
897 if (!item) // unknown is ignored; we just need to trace recursion
899 stack[level].sec = item->u.sec;
900 if (item->cls == CC_SECTION)
902 stack[level].base_ptr = ptr;
903 stack[level].op = OP_EDIT | OP_2ND; // this list operation does nothing
905 else if (item->cls == CC_LIST)
907 stack[level].base_ptr = cf_malloc(item->u.sec->size);
908 cf_init_section(item->name, item->u.sec, stack[level].base_ptr, 1);
909 stack[level].list = ptr;
910 stack[level].item = item;
911 if ((op & OP_MASK) < OP_REMOVE) {
912 add_to_list(ptr, stack[level].base_ptr, op & OP_MASK);
913 stack[level].op |= OP_2ND;
915 stack[level].op |= OP_1ST;
918 return "Opening brace can only be used on sections and lists";
923 closing_brace(struct item_stack *st, enum cf_operation op, int number, byte **pars)
925 if (st->op == OP_CLOSE) // top-level
926 return "Unmatched } parenthesis";
927 if (!st->sec) { // dummy run on unknown section
932 enum cf_operation pure_op = st->op & OP_MASK;
935 st->list = find_list_node(st->list, st->base_ptr, st->sec, st->mask);
937 return "Cannot find a node matching the query";
938 if (pure_op != OP_REMOVE)
940 if (pure_op == OP_EDIT)
941 st->base_ptr = st->list;
942 else if (pure_op == OP_AFTER || pure_op == OP_BEFORE)
943 cf_init_section(st->item->name, st->sec, st->base_ptr, 1);
946 if (op & OP_OPEN) { // stay at the same recursion level
947 st->op = (st->op | OP_2ND) & ~OP_1ST;
948 add_to_list(st->list, st->base_ptr, pure_op);
951 int taken; // parse parameters on 1 line immediately
952 TRY( interpret_section(st->sec, number, pars, &taken, st->base_ptr, 1) );
955 // and fall-thru to the 2nd phase
957 add_to_list(st->list, st->base_ptr, pure_op);
961 return "No parameters expected after the }";
962 else if (op & OP_OPEN)
963 return "No { is expected";
969 interpret_line(byte *name, enum cf_operation op, int number, byte **pars)
972 if ((op & OP_MASK) == OP_CLOSE)
973 return closing_brace(stack+level, op, number, pars);
974 void *ptr = stack[level].base_ptr;
975 struct cf_item *item = find_item(stack[level].sec, name, &msg, &ptr);
978 if (stack[level].op & OP_1ST)
979 TRY( record_selector(item, stack[level].sec, &stack[level].mask) );
980 if (op & OP_OPEN) { // the operation will be performed after the closing brace
982 return "Cannot open a block after a parameter has been passed on a line";
983 return opening_brace(item, ptr, op);
985 if (!item) // ignored item in an unknown section
989 int taken; // process as many parameters as possible
991 taken = 0, msg = interpret_clear(item, ptr);
992 else if (op == OP_SET)
993 msg = interpret_set_item(item, number, pars, &taken, ptr, 1);
994 else if (item->cls == CC_DYNAMIC)
995 msg = interpret_add_dynamic(item, number, pars, &taken, ptr, op);
996 else if (item->cls == CC_LIST)
997 msg = interpret_add_list(item, number, pars, &taken, ptr, op);
999 return cf_printf("Operation %s not supported on attribute %s", op_names[op], name);
1003 return cf_printf("Too many parameters: %d>%d", number, taken);
1009 cf_write_item(struct cf_item *item, enum cf_operation op, int number, byte **pars)
1015 msg = interpret_set_item(item, number, pars, &taken, item->ptr, 1);
1019 msg = interpret_clear(item, item->ptr);
1023 if (item->cls == CC_DYNAMIC)
1024 msg = interpret_add_dynamic(item, number, pars, &taken, item->ptr, op);
1025 else if (item->cls == CC_LIST)
1026 msg = interpret_add_list(item, number, pars, &taken, item->ptr, op);
1028 return "The attribute class does not support append/prepend";
1031 return "Unsupported operation";
1036 return "Too many parameters";
1045 stack[0] = (struct item_stack) {
1059 log(L_ERROR, "Unterminated block");
1063 if (commit_section("top-level", §ions, NULL, !everything_committed))
1065 everything_committed = 1;
1070 /* Text file parser */
1072 static byte *name_parse_fb;
1073 static struct fastbuf *parse_fb;
1074 static uns line_num;
1076 #define MAX_LINE 4096
1077 static byte line_buf[MAX_LINE];
1078 static byte *line = line_buf;
1080 #include "lib/bbuf.h"
1081 static bb_t copy_buf;
1084 #define GBUF_TYPE uns
1085 #define GBUF_PREFIX(x) split_##x
1086 #include "lib/gbuf.h"
1087 static split_t word_buf;
1089 static uns ends_by_brace; // the line is ended by "{"
1094 if (!bgets(parse_fb, line_buf, MAX_LINE))
1098 while (Cblank(*line))
1104 append(byte *start, byte *end)
1106 uns len = end - start;
1107 bb_grow(©_buf, copied + len + 1);
1108 memcpy(copy_buf.ptr + copied, start, len);
1110 copy_buf.ptr[copied-1] = 0;
1113 #define CONTROL_CHAR(x) (x == '{' || x == '}' || x == ';')
1114 // these characters separate words like blanks
1117 get_word(uns is_command_name)
1119 if (*line == '\'') {
1123 while (*line && *line != '\'')
1125 append(start, line);
1128 copy_buf.ptr[copied-1] = '\n';
1130 return "Unterminated apostrophe word at the end";
1134 } else if (*line == '"') {
1136 uns start_copy = copied;
1141 if (*line == '"' && !escape)
1143 else if (*line == '\\')
1149 append(start, line);
1153 copy_buf.ptr[copied-1] = '\n';
1154 else // merge two lines
1157 return "Unterminated quoted word at the end";
1161 byte *tmp = stk_str_unesc(copy_buf.ptr + start_copy);
1162 uns l = strlen(tmp);
1163 bb_grow(©_buf, start_copy + l + 1);
1164 strcpy(copy_buf.ptr + start_copy, tmp);
1165 copied = start_copy + l + 1;
1168 // promised that *line is non-null and non-blank
1170 while (*line && !Cblank(*line) && !CONTROL_CHAR(*line)
1171 && (*line != '=' || !is_command_name))
1173 if (*line == '=') { // nice for setting from a command-line
1175 return "Assignment without a variable";
1178 if (line == start) // already the first char is control
1180 append(start, line);
1182 while (Cblank(*line))
1188 get_token(uns is_command_name, byte **msg)
1192 if (!*line || *line == '#') {
1193 if (!is_command_name || !get_line())
1195 } else if (*line == ';') {
1197 if (!is_command_name || *msg)
1199 } else if (*line == '\\' && !line[1]) {
1201 *msg = "Last line ends by a backslash";
1204 if (!*line || *line == '#')
1205 log(L_WARN, "The line %s:%d following a backslash is empty", name_parse_fb, line_num);
1207 split_grow(&word_buf, words+1);
1209 word_buf.ptr[words++] = copied;
1210 *msg = get_word(is_command_name);
1211 return *msg ? NULL : copy_buf.ptr + start;
1219 words = copied = ends_by_brace = 0;
1220 byte *msg, *start_word;
1221 if (!(start_word = get_token(1, &msg)))
1223 if (*start_word == '{') // only one opening brace
1224 return "Unexpected opening brace";
1225 while (*line != '}') // stays for the next time
1227 if (!(start_word = get_token(0, &msg)))
1229 if (*start_word == '{') {
1230 words--; // discard the brace
1238 /* Parsing multiple files */
1241 parse_fastbuf(byte *name_fb, struct fastbuf *fb, uns depth)
1244 name_parse_fb = name_fb;
1251 msg = split_command();
1256 byte *name = copy_buf.ptr + word_buf.ptr[0];
1257 byte *pars[words-1];
1258 for (uns i=1; i<words; i++)
1259 pars[i-1] = copy_buf.ptr + word_buf.ptr[i];
1260 if (!strcasecmp(name, "include"))
1263 msg = "Expecting one filename";
1265 msg = "Too many nested files";
1266 else if (*line && *line != '#') // because the contents of line_buf is not re-entrant and will be cleared
1267 msg = "The input command must be the last one on a line";
1270 struct fastbuf *new_fb = bopen_try(pars[0], O_RDONLY, 1<<14);
1272 msg = cf_printf("Cannot open file %s: %m", pars[0]);
1276 msg = parse_fastbuf(pars[0], new_fb, depth+1);
1283 enum cf_operation op;
1284 byte *c = strchr(name, ':');
1286 op = strcmp(name, "}") ? OP_SET : OP_CLOSE;
1289 switch (Clocase(*c)) {
1290 case 's': op = OP_SET; break;
1291 case 'c': op = OP_CLEAR; break;
1292 case 'a': op = Clocase(c[1]) == 'p' ? OP_APPEND : OP_AFTER; break;
1293 case 'p': op = OP_PREPEND; break;
1294 case 'r': op = OP_REMOVE; break;
1295 case 'e': op = OP_EDIT; break;
1296 case 'b': op = OP_BEFORE; break;
1297 default: op = OP_SET; break;
1299 if (strcasecmp(c, op_names[op])) {
1300 msg = cf_printf("Unknown operation %s", c);
1306 msg = interpret_line(name, op, words-1, pars);
1311 log(L_ERROR, "File %s, line %d: %s", name_fb, line_num, msg);
1312 return "included from here";
1315 #undef DEFAULT_CONFIG /* FIXME */
1316 #define DEFAULT_CONFIG "cf/sherlock2"
1318 #ifndef DEFAULT_CONFIG
1319 #define DEFAULT_CONFIG NULL
1321 byte *cf_def_file = DEFAULT_CONFIG;
1324 load_file(byte *file)
1327 struct fastbuf *fb = bopen_try(file, O_RDONLY, 1<<14);
1329 log(L_ERROR, "Cannot open %s: %m", file);
1332 byte *msg = parse_fastbuf(file, fb, 0);
1334 int err = !!msg || done_stack();
1341 load_string(byte *string)
1345 fbbuf_init_read(&fb, string, strlen(string), 0);
1346 byte *msg = parse_fastbuf("memory string", &fb, 0);
1347 return !!msg || done_stack();
1350 /* Command-line parser */
1356 if (cf_load(cf_def_file))
1357 die("Cannot load default config %s", cf_def_file);
1361 cf_get_opt(int argc, char * const argv[], const char *short_opts, const struct option *long_opts, int *long_index)
1363 static int other_options = 0;
1365 int res = getopt_long (argc, argv, short_opts, long_opts, long_index);
1366 if (res == 'S' || res == 'C')
1369 die("The -S and -C options must precede all other arguments");
1373 die("Cannot set %s", optarg);
1375 if (cf_load(optarg))
1376 die("Cannot load config file %s", optarg);
1379 /* unhandled option or end of options */
1380 if (res != ':' && res != '?')
1391 spaces(struct fastbuf *fb, uns nr)
1393 for (uns i=0; i<nr; i++)
1398 dump_basic(struct fastbuf *fb, void *ptr, enum cf_type type, union cf_union *u)
1401 case CT_INT: bprintf(fb, "%d ", *(uns*)ptr); break;
1402 case CT_U64: bprintf(fb, "%llu ", *(u64*)ptr); break;
1403 case CT_DOUBLE: bprintf(fb, "%lg ", *(double*)ptr); break;
1404 case CT_IP: bprintf(fb, "%08x ", *(uns*)ptr); break;
1405 case CT_STRING: bprintf(fb, "'%s' ", *(byte**)ptr); break;
1406 case CT_LOOKUP: bprintf(fb, "%s ", *(int*)ptr >= 0 ? u->lookup[ *(int*)ptr ] : "???"); break;
1410 static void dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr);
1413 dump_item(struct fastbuf *fb, struct cf_item *item, int level, void *ptr)
1415 ptr += (addr_int_t) item->ptr;
1416 enum cf_type type = item->type;
1419 bprintf(fb, "%s: c%d #%d ", item->name, item->cls, item->number);
1420 if (item->cls == CC_STATIC || item->cls == CC_DYNAMIC)
1421 bprintf(fb, "t%d ", type);
1422 if (item->cls == CC_STATIC) {
1423 for (i=0; i<item->number; i++)
1424 dump_basic(fb, ptr + i * parsers[type].size, type, &item->u);
1425 } else if (item->cls == CC_DYNAMIC) {
1426 ptr = * (void**) ptr;
1428 int real_nr = * (int*) (ptr - parsers[type].size);
1429 bprintf(fb, "##%d ", real_nr);
1430 for (i=0; i<real_nr; i++)
1431 dump_basic(fb, ptr + i * parsers[type].size, type, &item->u);
1433 bprintf(fb, "NULL ");
1436 if (item->cls == CC_SECTION)
1437 dump_section(fb, item->u.sec, level+1, ptr);
1438 else if (item->cls == CC_LIST) {
1441 CLIST_WALK(n, * (clist*) ptr) {
1442 spaces(fb, level+1);
1443 bprintf(fb, "item %d\n", ++idx);
1444 dump_section(fb, item->u.sec, level+2, n);
1450 dump_section(struct fastbuf *fb, struct cf_section *sec, int level, void *ptr)
1453 bprintf(fb, "S%d F%x:\n", sec->size, sec->flags);
1454 for (struct cf_item *item=sec->cfg; item->cls; item++)
1455 dump_item(fb, item, level, ptr);
1459 cf_dump_sections(struct fastbuf *fb)
1461 dump_section(fb, §ions, 0, NULL);
1465 * - more space efficient journal