-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
458 lines (350 loc) · 9.48 KB
/
tree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "io.h"
#include "vink-arena.h"
#include "vink-internal.h"
#include "vink-tree.h"
#include "vink.h"
struct vink_tree_node
{
char* path;
char* value;
};
struct vink_tree
{
struct vink_arena arena;
char* name;
struct vink_tree_node* nodes;
size_t node_count;
size_t node_alloc;
};
struct vink_tree*
vink_tree_create (const char* name)
{
struct vink_tree* result;
struct vink_arena arena;
vink_arena_init (&arena);
result = vink_arena_calloc (&arena, sizeof (*result));
if (!result)
errx(EX_OSERR, "Arena allocation failed: %s", vink_last_error());
result->arena = arena;
result->name = vink_arena_strdup (&result->arena, name);
return result;
}
void
vink_tree_destroy (struct vink_tree* t)
{
free (t->nodes);
vink_arena_free (&t->arena);
}
void
vink_tree_create_node (struct vink_tree* t, const char* path, const char* value)
{
size_t i;
if(t->node_count == t->node_alloc)
{
t->node_alloc = t->node_alloc * 4 / 3 + 16;
t->nodes = realloc(t->nodes, sizeof(*t->nodes) * t->node_alloc);
if(!t->nodes)
errx(EX_OSERR, "failed to allocate memory for tree nodes");
}
i = t->node_count++;
t->nodes[i].path = vink_arena_strdup (&t->arena, path);
t->nodes[i].value = vink_arena_strdup (&t->arena, value);
}
long long int
vink_tree_get_integer (const struct vink_tree* t, const char* path)
{
char* tmp;
long long int result;
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
{
result = strtoll(t->nodes[i].value, &tmp, 0);
if(*tmp)
errx(EX_DATAERR, "%s: expected integer value in '%s', found '%s'",
t->name, path, t->nodes[i].value);
return result;
}
}
errx(EX_DATAERR, "%s: could not find symbol '%s'", t->name, path);
}
long long int
vink_tree_get_integer_default (const struct vink_tree* t, const char* path, long long int def)
{
char* tmp;
long long int result;
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
{
result = strtoll(t->nodes[i].value, &tmp, 0);
if(*tmp)
{
fprintf(stderr, "%s: expected integer value in '%s', found '%s'\n",
t->name, path, t->nodes[i].value);
return def;
}
return result;
}
}
return def;
}
int
vink_tree_get_bool (const struct vink_tree* t, const char* path)
{
const char* value;
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
{
value = t->nodes[i].value;
if(!strcmp(value, "0")
|| !strcasecmp(value, "false")
|| !strcasecmp(value, "no"))
return 0;
if(!strcmp(value, "1")
|| !strcasecmp(value, "true")
|| !strcasecmp(value, "yes"))
return 1;
errx(EX_DATAERR, "%s: expected boolean value in '%s', found '%s'",
t->name, path, t->nodes[i].value);
}
}
errx(EX_DATAERR, "%s: could not find symbol '%s'", t->name, path);
}
int
vink_tree_get_bool_default (const struct vink_tree* t, const char* path, int def)
{
const char* value;
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
{
value = t->nodes[i].value;
if(!strcmp(value, "0")
|| !strcasecmp(value, "false")
|| !strcasecmp(value, "no"))
return 0;
if(!strcmp(value, "1")
|| !strcasecmp(value, "true")
|| !strcasecmp(value, "yes"))
return 1;
fprintf(stderr, "%s: expected boolean value in '%s', found '%s'\n",
t->name, path, t->nodes[i].value);
return def;
}
}
return def;
}
const char*
vink_tree_get_string (const struct vink_tree* t, const char* path)
{
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
return t->nodes[i].value;
}
errx(EX_DATAERR, "%s: could not find symbol '%s'", t->name, path);
}
size_t
vink_tree_get_strings (const struct vink_tree* t, const char* path, char*** result)
{
size_t i, count = 0;
*result = 0;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
{
*result = realloc(*result, sizeof(*result) * (count + 1));
(*result)[count++] = t->nodes[i].value;
}
}
return count;
}
const char*
vink_tree_get_string_default (const struct vink_tree* t, const char* path, const char* def)
{
size_t i;
for(i = 0; i < t->node_count; ++i)
{
if(!strcmp(t->nodes[i].path, path))
return t->nodes[i].value;
}
return def;
}
static int
is_symbol_char(int ch)
{
return isalnum(ch) || ch == '-' || ch == '_' || ch == '!';
}
struct vink_tree*
vink_tree_load_cfg (const char* path)
{
struct vink_tree* result;
char* data;
off_t size;
int fd;
char symbol[4096];
size_t symbol_len = 0;
size_t section_stack[32];
size_t section_stackp = 0;
int expecting_symbol = 1;
char* c;
int lineno = 1;
result = vink_tree_create (path);
if(-1 == (fd = open(path, O_RDONLY)))
return result;
if(-1 == (size = lseek(fd, 0, SEEK_END)))
err(EX_OSERR, "%s: failed to seek to end of file", path);
if(-1 == lseek(fd, 0, SEEK_SET))
err(EX_OSERR, "%s: failed to seek to start of file", path);
if(0 == (data = malloc(size + 1)))
err(EX_OSERR, "%s: failed to allocate %zu bytes for parsing", path,
(size_t) (size + 1));
read_all(fd, data, size, path);
data[size] = 0;
close(fd);
c = data;
while(*c)
{
while(isspace(*c))
{
if(*c++ == '\n')
++lineno;
}
if(!*c)
break;
if(*c == '#')
{
while(*c && *c != '\n')
++c;
++lineno;
continue;
}
if(*c == '}')
{
if(!section_stackp)
errx(EX_DATAERR, "%s:%d: unexpected '}'", path, lineno);
if(!--section_stackp)
symbol_len = 0;
else
symbol_len = section_stack[section_stackp - 1];
++c;
continue;
}
if(expecting_symbol)
{
if(!is_symbol_char(*c))
{
if(isprint(*c))
errx(EX_DATAERR, "%s:%d: unexpected '%c' while looking for symbol",
path, lineno, *c);
else
errx(EX_DATAERR, "%s:%d: unexpected 0x%02x while looking for symbol",
path, lineno, *c);
}
if(symbol_len)
{
if(symbol_len + 1 == ARRAY_SIZE(symbol))
errx(EX_DATAERR, "%s:%d: symbol stack overflow", path, lineno);
symbol[symbol_len++] = '.';
}
while(is_symbol_char(*c))
{
if(symbol_len + 1 == ARRAY_SIZE(symbol))
errx(EX_DATAERR, "%s:%d: symbol stack overflow", path, lineno);
symbol[symbol_len++] = *c++;
}
if(isspace(*c))
{
*c++ = 0;
while(isspace(*c))
++c;
}
switch(*c)
{
case 0:
errx(EX_DATAERR, "%s:%d: unexpected end-of-file after symbol",
path, lineno);
case '.':
expecting_symbol = 1;
*c++ = 0;
break;
case '{':
if(section_stackp == ARRAY_SIZE(section_stack))
errx(EX_DATAERR, "%s:%d: too many nested sections", path,
lineno);
section_stack[section_stackp++] = symbol_len;
expecting_symbol = 1;
*c++ = 0;
break;
case '}':
errx(EX_DATAERR, "%s:%d: unexpected '%c' after symbol", path,
lineno, *c);
default:
expecting_symbol = 0;
}
}
else /* !expecting_symbol */
{
char* value = c;
if(*c == '"')
{
char* o;
o = value = ++c;
for(;;)
{
if(!*c)
{
errx(EX_DATAERR, "%s:%d: unexpected end-of-file in "
"string", path, lineno);
}
if(*c == '\\')
{
if(!*(c + 1))
errx(EX_DATAERR, "%s:%d: unexpected end-of-file in "
"string", path, lineno);
++c;
*o++ = *c++;
}
else if(*c == '"')
break;
else
*o++ = *c++;
}
*c++ = 0;
}
else
{
while(*c && !isspace(*c))
++c;
if(*c)
*c++ = 0;
}
symbol[symbol_len] = 0;
vink_tree_create_node (result, symbol, value);
if(section_stackp)
symbol_len = section_stack[section_stackp - 1];
else
symbol_len = 0;
expecting_symbol = 1;
}
}
free(data);
return result;
}