This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scfg.y
528 lines (433 loc) · 10.1 KB
/
scfg.y
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright (c) 2018, 2019 Tim Kuijsten
*
* Permission to use, copy, modify, and distribute this software for any purpose
* with or without fee is hereby granted, provided that the above copyright
* notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
%{
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include "parseconfig.h"
int yydebug = 0;
typedef struct {
union {
struct scfge *scfge;
char *str;
char **strv;
};
int lineno;
int colno;
} YYSTYPE;
extern int verbose;
/* Should be assigned externally. */
int yyd = -1;
/* Use a stream internally for easy look-ahead using ungetc. */
static FILE *yyfp = NULL;
static int curcol;
static int curline;
/*
* Global config vector that holds entries.
*/
static struct scfge *cfgroot, *cfgparent, *cfglast;
static int allocated = 0;
static void addentry(struct scfge *, struct scfge *);
static void addstr(struct scfge *, const char *str);
static struct scfge *allocentry(void);
static void freeentry(struct scfge **);
static char *getstr(FILE *);
static char *appendc(char *, char, size_t *);
void
yyerror(char *msg)
{
warnx("%s at line %d, column %d", msg, curline, curcol);
}
%}
%start grammar
%token <str> STRING
%token OBRACE CBRACE EOS
%type <scfge> grammar entryv
%%
grammar: /* new */ {
if (cfgroot == NULL)
scfg = cfgroot = cfgparent = allocentry();
}
| grammar {
$$ = cfglast = allocentry();
} strv entryv EOS {
if ($2->strvsize == 0 && $2->entryvsize == 0) {
freeentry(&$2);
$$ = NULL;
} else {
addentry(cfgparent, $2);
$$ = $2;
}
}
;
strv: /* optional */
| strv STRING {
addstr(cfglast, $2);
}
;
entryv: /* optional */ { $$ = NULL; }
| OBRACE {
/* descend */
$$ = cfgparent;
cfgparent = cfglast;
} grammar CBRACE {
/* ascend */
cfgparent = $2;
/* return entryv or null */
$$ = $3;
}
;
%%
int
yylex(void)
{
static int prevtoken = 0, nexttoken = 0;
if (yyd == -1) {
yyerror("no descriptor open");
return -1;
}
/* Init on first call. */
if (yyfp == NULL) {
curcol = 0;
curline = 1;
if ((yyfp = fdopen(yyd, "r")) == NULL)
err(1, "%s: fdopen", __func__);
}
/*
* Maybe the next token was cached because of an implicit
* end-of-statement.
*/
if (nexttoken > 0) {
prevtoken = nexttoken;
nexttoken = 0;
return prevtoken;
}
/* Expect yylval be either NULL or initialized in a previous call. */
free(yylval.str);
while ((yylval.str = getstr(yyfp)) != NULL) {
yylval.lineno = curline;
yylval.colno = curcol;
if (strcmp(yylval.str, "\n") == 0) {
prevtoken = EOS;
return EOS;
} else if (strcmp(yylval.str, ";") == 0) {
prevtoken = EOS;
return EOS;
} else if (strcmp(yylval.str, "{") == 0) {
prevtoken = OBRACE;
return OBRACE;
} else if (strcmp(yylval.str, "}") == 0) {
/*
* Return end-of-statement if not set explicitly after
* the last entry in a entryv.
*/
if (prevtoken != EOS) {
nexttoken = CBRACE;
prevtoken = EOS;
return EOS;
}
prevtoken = CBRACE;
return CBRACE;
} else {
prevtoken = STRING;
return STRING;
}
}
if (ferror(yyfp) != 0)
err(1, "yyfp error");
if (feof(yyfp) == 0)
errx(1, "yyfp still open");
/* Cleanup and signal the end. */
yyfp = NULL;
return 0;
}
/*
* Parse input for strings.
*
* A string is a concatenation of non-null and non-control characters. Strings
* are expected to be separated by blanks, newlines or ';'. A string may
* only contain spaces if it is enclosed in double quotes or if every space is
* escaped using a '\' character.
*
* The following special characters outside a string are returned as null-
* terminated character strings:
* '{'
* '}'
* '\n'
* ';'
*
* A '#' outside of a string ignores all characters up to the first newline.
*
* Any control characters or '\0' outside of a string, except for '\t' and '\n'
* are considered illegal.
*
* Return a pointer to a new string on success or NULL on end-of-file or error.
*/
static char *
getstr(FILE *stream)
{
enum states { S, STR, QSTR, ESCQ, ESCS, COMMENT };
size_t len;
int c, state;
char *r;
state = S;
r = NULL;
len = 0;
while ((c = fgetc(stream)) != EOF) {
/* Track position in file for debugging. */
if (c == '\n') {
curline++;
curcol = 0;
}
curcol++;
switch (state) {
case S:
if (isblank(c)) {
/* Swallow any preceding blanks. */
} else if (c == ';' || c == '\n' || c == '{'
|| c == '}') {
/* These characters are strings by themselves. */
r = appendc(r, c, &len);
goto end;
} else if (c == '\\') {
state = ESCS;
} else if (c == '"') {
state = QSTR;
} else if (c == '#') {
state = COMMENT;
} else if (c != '\0' && !iscntrl(c)) {
r = appendc(r, c, &len);
state = STR;
} else {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
break;
case STR:
if (isblank(c)) {
/* End of string. */
goto end;
} else if (c == ';' || c == '\n' || c == '{'
|| c == '}') {
/*
* End of string. Finish this string and leave
* the newline or curly brace for the next run
* because these are special strings by
* themselves.
*/
ungetc(c, stream);
/* don't count the characater twice */
if (c == '\n')
curline--;
else
curcol--;
goto end;
} else if (c == '\\') {
state = ESCS;
} else if (c != '\0' && !iscntrl(c)) {
r = appendc(r, c, &len);
} else {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
break;
case QSTR:
if (c == '\\') {
state = ESCQ;
} else if (c == '"') {
goto end;
} else if (c != '\0' && !iscntrl(c)) {
r = appendc(r, c, &len);
} else {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
break;
case ESCS:
if (c == '\0' || iscntrl(c)) {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
r = appendc(r, c, &len);
state = STR;
break;
case ESCQ:
if (c == '\0' || iscntrl(c)) {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
r = appendc(r, c, &len);
state = QSTR;
break;
case COMMENT:
if (c == '\t') {
/* swallow the tab control-character */
} else if (c == '\n') {
/*
* End of comment, a newline is a string by
* itself.
*/
r = appendc(r, c, &len);
goto end;
} else if (c == '\0' || iscntrl(c)) {
warnx("unexpected char %d at %d,%d", c, curline, curcol);
goto err;
}
break;
}
}
err:
free(r);
r = NULL;
end:
if (verbose > 3)
warnx("returning %lu \"%s\"", r == NULL ? 0 : strlen(r), r == NULL ? "NULL" : r);
return r;
}
/*
* Append a character to a dynamically allocated string.
*
* Return a pointer to the possibly relocated string on success, or exit on
* error.
*/
static char *
appendc(char *str, char c, size_t *len)
{
/* account for terminating null byte */
str = realloc(str, *len + 2);
if (str == NULL)
err(1, "%s: realloc", __func__);
str[*len] = c;
str[*len + 1] = 0;
(*len)++;
return str;
}
/* Add an entry to an entry vector. */
static void
addentry(struct scfge *parent, struct scfge *child)
{
parent->entryv = reallocarray(parent->entryv, parent->entryvsize + 1,
sizeof(*parent->entryv));
if (parent->entryv == NULL)
err(1, "%s: reallocarray", __func__);
parent->entryv[parent->entryvsize] = child;
parent->entryvsize++;
}
static void
addstr(struct scfge *scfge, const char *str)
{
scfge->strv = reallocarray(scfge->strv, scfge->strvsize + 1,
sizeof(*scfge->strv));
if (scfge->strv == NULL)
err(1, "%s: reallocarray", __func__);
if ((scfge->strv[scfge->strvsize] = strdup(str)) == NULL)
err(1, "%s: strdup", __func__);
scfge->strvsize++;
}
/* Allocate a new entry. */
static struct scfge *
allocentry(void)
{
struct scfge *scfge;
if ((scfge = calloc(1, sizeof(*scfge))) == NULL)
err(1, "%s: calloc", __func__);
allocated++;
if (verbose > 3)
warnx("%s: allocated %p %d", __func__, (void *)scfge,
allocated);
scfge->strv = NULL;
scfge->entryv = NULL;
return scfge;
}
static void
freeentry(struct scfge **scfge)
{
size_t n;
if (*scfge == NULL)
return;
if (verbose > 3)
warnx("%s: deallocating %p %d", __func__, (void *)*scfge, allocated);
for (n = 0; n < (*scfge)->strvsize; n++)
free((*scfge)->strv[n]);
free((*scfge)->strv);
(*scfge)->strv = NULL;
(*scfge)->strvsize = 0;
for (n = 0; n < (*scfge)->entryvsize; n++)
freeentry(&(*scfge)->entryv[n]);
free((*scfge)->entryv);
(*scfge)->entryv = NULL;
(*scfge)->entryvsize = 0;
allocated--;
if (verbose > 3)
warnx("%s: deallocated %p %d", __func__, (void *)*scfge, allocated);
free(*scfge);
*scfge = NULL;
}
/*
* Recursively print all strings of all configuration entries.
*/
static void
printr(struct scfge *scfge, int depth)
{
size_t n;
if (scfge == NULL)
return;
fprintf(stdout, "%d: ", depth);
if (scfge->strvsize == 0) {
printf("%p\n", (void *)scfge);
} else {
for (n = 0; n < scfge->strvsize; n++)
printf("%s ", scfge->strv[n]);
printf("\n");
}
for (n = 0; n < scfge->entryvsize; n++)
printr(scfge->entryv[n], depth + 1);
}
/*
* Recursively print all strings of all configuration entries.
*/
void
scfg_printr(void)
{
if (cfgroot == NULL)
return;
printr(cfgroot, 0);
}
/*
* Remove the complete config from memory and check for leaks.
*
* Return 0 on success, -1 if an error occurred.
*/
int
scfg_clear(void)
{
if (verbose > 3)
warnx("%s", __func__);
if (cfgroot == NULL) {
if (allocated > 0) {
warnx("no cfgroot while %d entries allocated",
allocated);
return -1;
}
return 0;
}
freeentry(&cfgroot);
if (allocated > 0) {
warnx("memleak: still %d entries allocated", allocated);
return -1;
}
return 0;
}