2 * Insane tester of reading configuration files
4 * (c) 2006 Robert Spalek <robert@ucw.cz>
9 #include "lib/getopt.h"
10 #include "lib/clists.h"
11 #include "lib/fastbuf.h"
28 static struct sub_sect_1 sec1 = { {}, "Charlie", 0, "WBAFC", { 0, -1}, DARY_ALLOC(double, 3, 1e4, -1e-4, 8) };
31 init_sec_1(struct sub_sect_1 *s)
33 if (s == &sec1) // this is a static variable; skip clearing
39 // leave s->list==NULL
44 commit_sec_1(struct sub_sect_1 *s)
46 if (s->confidence[0] < 0 || s->confidence[0] > 10)
47 return "Well, this can't be";
52 time_parser(uns number, byte **pars, time_t *ptr)
54 *ptr = number ? atoi(pars[0]) : time(NULL);
58 static struct cf_section cf_sec_1 = {
59 CF_TYPE(struct sub_sect_1),
61 CF_COMMIT(commit_sec_1),
62 #define F(x) PTR_TO(struct sub_sect_1, x)
64 CF_STRING("name", F(name)),
65 //CF_PARSER("t", F(t), time_parser, 0),
66 CF_STRING("level", F(level)),
67 CF_INT_ARY("confidence", F(confidence[0]), 2), // XXX: the [0] is needed for the sake of type checking
68 CF_DOUBLE_DYN("list", F(list), 100),
75 static int *nrs1 = DARY_ALLOC(int, 5, 5, 4, 3, 2, 1);
77 static byte *str1 = "no worries";
78 static byte **str2 = DARY_ALLOC(byte *, 2, "Alice", "Bob");
79 static u64 u1 = 0xCafeBeefDeadC00ll;
80 static double d1 = -1.1;
84 static int *look = DARY_ALLOC(int, 2, 2, 1);
85 static u16 numbers[10] = { 2, 100, 1, 5 };
88 parse_u16(byte *string, u16 *ptr)
91 byte *msg = cf_parse_int(string, &a);
95 return "Come on, man, this doesn't fit to 16 bits";
101 dump_u16(struct fastbuf *fb, u16 *ptr)
103 bprintf(fb, "%d ", *ptr);
106 static struct cf_user_type u16_type = {
109 .parser = (cf_parser1*) parse_u16,
110 .dumper = (cf_dumper1*) dump_u16
114 init_top(void *ptr UNUSED)
116 for (uns i=0; i<5; i++)
118 struct sub_sect_1 *s = xmalloc(sizeof(struct sub_sect_1)); // XXX: cannot by cf_malloc(), because it's deleted when cf_reload()'ed
119 cf_init_section("slaves", &cf_sec_1, s, 1);
120 s->confidence[1] = i;
121 clist_add_tail(&secs, &s->n);
127 commit_top(void *ptr UNUSED)
130 return "Don't touch my variable!";
134 static byte *alphabet[] = { "alpha", "beta", "gamma", "delta", NULL };
135 static struct cf_section cf_top = {
137 CF_COMMIT(commit_top),
140 CF_INT_DYN("nrs1", &nrs1, 1000),
141 CF_INT_ARY("nrs2", nrs2, 5),
142 CF_STRING("str1", &str1),
143 CF_STRING_DYN("str2", &str2, 20),
145 CF_DOUBLE("d1", &d1),
146 CF_PARSER("FirstTime", &t1, time_parser, -1),
147 CF_PARSER("SecondTime", &t2, time_parser, 1),
148 CF_SECTION("master", &sec1, &cf_sec_1),
149 CF_LIST("slaves", &secs, &cf_sec_1),
151 CF_LOOKUP_DYN("look", &look, alphabet, 1000),
152 CF_USER_ARY("numbers", numbers, &u16_type, 10),
157 static byte short_opts[] = CF_SHORT_OPTS "v";
158 static struct option long_opts[] = {
160 {"verbose", 0, 0, 'v'},
164 static char *help = "\
165 Usage: conf-test <options>\n\
169 "-v\t\t\tBe verbose\n\
173 usage(byte *msg, ...)
178 vfprintf(stderr, msg, va);
184 main(int argc, char *argv[])
187 cf_declare_section("top", &cf_top, 0);
188 cf_def_file = "lib/conf-test.cf";
191 while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0)
193 case 'v': verbose++; break;
194 default: usage("unknown option %c\n", opt);
197 usage("too many parameters (%d more)\n", argc-optind);
200 cf_load("non-existent file");
201 //cf_reload("non-existent file");
202 cf_load("non-existent file");
203 cf_set("top.d1 -1.1; top.master b");
206 struct fastbuf *out = bfdopen(1, 1<<14);
207 cf_dump_sections(out);