2 * Insane tester of reading configuration files
4 * (c) 2006 Robert Spalek <robert@ucw.cz>
9 #include <ucw/getopt.h>
10 #include <ucw/clists.h>
11 #include <ucw/fastbuf.h>
18 static int new_context;
30 static struct sub_sect_1 sec1 = { {}, "Charlie", 0, "WBAFC", { 0, -1}, DARY_ALLOC(double, 3, 1e4, -1e-4, 8) };
33 init_sec_1(struct sub_sect_1 *s)
35 if (s == &sec1) // this is a static variable; skip clearing
41 // leave s->list==NULL
46 commit_sec_1(struct sub_sect_1 *s)
48 if (s->confidence[0] < 0 || s->confidence[0] > 10)
49 return "Well, this can't be";
54 time_parser(uns number, char **pars, time_t *ptr)
56 *ptr = number ? atoi(pars[0]) : time(NULL);
60 static struct cf_section cf_sec_1 = {
61 CF_TYPE(struct sub_sect_1),
63 CF_COMMIT(commit_sec_1),
64 #define F(x) PTR_TO(struct sub_sect_1, x)
66 CF_STRING("name", F(name)),
67 //CF_PARSER("t", F(t), time_parser, 0),
68 CF_STRING("level", F(level)),
69 CF_INT_ARY("confidence", F(confidence[0]), 2), // XXX: the [0] is needed for the sake of type checking
70 CF_DOUBLE_DYN("list", F(list), 100),
77 static int *nrs1 = DARY_ALLOC(int, 5, 5, 4, 3, 2, 1);
79 static char *str1 = "no worries";
80 static char **str2 = DARY_ALLOC(char *, 2, "Alice", "Bob");
81 static u64 u1 = 0xCafeBeefDeadC00ll;
82 static double d1 = -1.1;
86 static int *look = DARY_ALLOC(int, 2, 2, 1);
87 static u16 numbers[10] = { 2, 100, 1, 5 };
88 static u32 bitmap1 = 0xff;
89 static u32 bitmap2 = 3;
92 parse_u16(char *string, u16 *ptr)
95 char *msg = cf_parse_int(string, &a);
99 return "Come on, man, this doesn't fit to 16 bits";
105 dump_u16(struct fastbuf *fb, u16 *ptr)
107 bprintf(fb, "%d ", *ptr);
110 static struct cf_user_type u16_type = {
113 .parser = (cf_parser1*) parse_u16,
114 .dumper = (cf_dumper1*) dump_u16
118 init_top(void *ptr UNUSED)
120 for (uns i=0; i<5; i++)
122 struct sub_sect_1 *s = xmalloc(sizeof(struct sub_sect_1)); // XXX: cannot by cf_malloc(), because it's deleted when cf_reload()'ed
123 cf_init_section("slaves", &cf_sec_1, s, 1);
124 s->confidence[1] = i;
125 clist_add_tail(&secs, &s->n);
131 commit_top(void *ptr UNUSED)
134 return "Don't touch my variable!";
138 static const char * const alphabet[] = { "alpha", "beta", "gamma", "delta", NULL };
139 static struct cf_section cf_top = {
141 CF_COMMIT(commit_top),
144 CF_INT_DYN("nrs1", &nrs1, 1000),
145 CF_INT_ARY("nrs2", nrs2, 5),
146 CF_STRING("str1", &str1),
147 CF_STRING_DYN("str2", &str2, 20),
149 CF_DOUBLE("d1", &d1),
150 CF_PARSER("FirstTime", &t1, time_parser, -1),
151 CF_PARSER("SecondTime", &t2, time_parser, 1),
152 CF_SECTION("master", &sec1, &cf_sec_1),
153 CF_LIST("slaves", &secs, &cf_sec_1),
155 CF_LOOKUP_DYN("look", &look, alphabet, 1000),
156 CF_USER_ARY("numbers", numbers, &u16_type, 10),
157 CF_BITMAP_INT("bitmap1", &bitmap1),
158 CF_BITMAP_LOOKUP("bitmap2", &bitmap2, ((const char* const[]) {
159 "one", "two", "three", "four", "five", "six", "seven", "eight",
160 "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "seventeen",
161 "eighteen", "nineteen", "twenty", NULL // hidden joke here
167 static char short_opts[] = CF_SHORT_OPTS "rv";
168 static struct option long_opts[] = {
170 {"reload", 0, 0, 'r'},
171 {"verbose", 0, 0, 'v'},
175 static char *help = "\
176 Usage: conf-test [ctxt] <options>\n\
178 Options:\n" CF_USAGE "\
179 -r, --reload\t\tReload configuration\n\
180 -v, --verbose\t\tBe verbose\n\
184 usage(char *msg, ...)
189 vfprintf(stderr, msg, va);
195 main(int argc, char *argv[])
199 struct cf_context *cc = NULL, *prev = NULL;
200 if (argc > 1 && !strcmp(argv[1], "ctxt")) {
201 cc = cf_new_context();
202 prev = cf_switch_context(cc);
206 cf_declare_section("top", &cf_top, 0);
207 cf_def_file = "ucw/conf-test.cf";
210 while ((opt = cf_getopt(argc, argv, short_opts, long_opts, NULL)) >= 0)
212 case 'n': new_context++; break;
213 case 'r': reload++; break;
214 case 'v': verbose++; break;
215 default: usage("unknown option %c\n", opt);
218 usage("too many parameters (%d more)\n", argc-optind);
226 struct fastbuf *out = bfdopen(1, 1<<14);
227 cf_dump_sections(out);
232 cf_switch_context(prev);