-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_conf.c
508 lines (392 loc) · 9.83 KB
/
parse_conf.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
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
/*
* parse_conf.c
*
* Routines to read and parse a configuration file.
*
* $Id: parse_conf.c,v 1.2 2012/05/16 12:16:16 kouril Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "parse_conf.h"
#define BUFSIZE 256
#define DEFAULT_DELIM " \n\t"
struct _pconf_context {
FILE *file;
char *filename;
int linenum;
char *delim;
int buffer_read;
int whole_line;
};
typedef struct _pconf_context pconf_context;
char pconf_error[BUFSIZE] = "No error";
static pconf_entry *new_pconf_entry();
static pconf_entry *parse_list(pconf_context *, int, int *);
static pconf_entry *parse_entry(pconf_context *, char **, int *);
static char **read_list(pconf_context *, char **, int *, int);
static char *get_token(pconf_context *);
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE !FALSE
#endif
#define EXPECT_EOL TRUE
#define DONT_EXPECT_EOL FALSE
#define IS_LVALUE TRUE
/*
* Externally available routines
*/
/*
* Given a configuration filename, parse the file returning a linked
* list of conf_entrys. Returns NULL on error and sets pconf_error.
*/
pconf_entry *
parse_conf(char *filename, char *delim)
{
pconf_context pcontext;
pconf_entry *entry;
int error;
pcontext.file = fopen(filename, "r");
if (pcontext.file == NULL) {
sprintf(pconf_error, "Couldn't open file %s: %s", filename, strerror(errno));
return NULL;
}
pcontext.linenum = 0;
pcontext.filename = filename;
pcontext.delim = (delim ? delim : DEFAULT_DELIM);
pcontext.buffer_read = FALSE;
pcontext.whole_line = FALSE;
entry = parse_list(&pcontext, DONT_EXPECT_EOL, &error);
return entry;
}
/*
* Given a linked list of pconf_enteries, free them all.
*/
void
free_pconf_enteries(pconf_entry * entry)
{
while (entry) {
pconf_entry *next = entry->next;
if (entry->strings) {
char **string = entry->strings;
while (*string) {
free(*string);
string++;
}
free(entry->strings);
}
if (entry->values) {
char **string = entry->values;
while (*string) {
free(*string);
string++;
}
free(entry->values);
}
if (entry->list)
free_pconf_enteries(entry->list);
free(entry);
entry = next;
}
}
/*
* Internal Routines
*/
/*
* Allocate and initialize and new pconf_entry structure.
*/
static pconf_entry *
new_pconf_entry()
{
pconf_entry *entry;
entry = (pconf_entry *) malloc(sizeof(*entry));
if (entry == NULL) {
sprintf(pconf_error, "malloc() failed");
return NULL;
}
entry->strings = NULL;
entry->type = CONF_TYPE_NONE;
entry->values = NULL;
entry->list = NULL;
entry->next = NULL;
return entry;
}
/*
* Parse a configuration file. This reoutine is recursive in that if it
* descents to a sublist (e.g. "sublist = { <sublist> }") it calls itself
* on the sublist.
*
* If expect_eol is true, then this routine expects a "}" to end this
* list, otherwise it expects EOF.
*/
static pconf_entry *
parse_list(pconf_context * pcontext, int expect_eol, int *error)
{
pconf_entry *head = NULL;
pconf_entry *current = NULL;
char *string;
while (TRUE) {
if (current == NULL) {
/* First time through loop */
current = parse_entry(pcontext, &string, error);
head = current;
} else {
current->next = parse_entry(pcontext, &string, error);
current = current->next;
}
if (*error)
goto error_return;
if (string == NULL) { /* EOF */
if (expect_eol) {
sprintf(pconf_error, "Unexpected EOF on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
break;
}
if (strcmp(string, "}") == 0) {
if (!expect_eol) {
sprintf(pconf_error, "Unexpected } on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
break;
}
if (strcmp(string, ";") == 0)
continue;
/* XXX Anything else to check for here ? */
}
return head;
error_return:
/* Clean up all of our allocations and return NULL */
free_pconf_enteries(head);
return NULL;
}
/*
* Read an entry and allocate a structure to hold it. Returns null if
* there wasn't an entry to read (we're at EOL or EOF).
*
* terminator is set to point at the string that terminated the list
* (will be NULL if it was terminated by EOF).
*
* Sets error if an error is encountered.
*/
static pconf_entry *
parse_entry(pconf_context * pcontext, char **terminator, int *error)
{
pconf_entry *entry;
char *string;
entry = new_pconf_entry();
if (entry == NULL)
goto error_return;
/* Read Lvalues */
entry->strings = read_list(pcontext, &string, error, IS_LVALUE);
if (*error)
goto error_return;
if (string == NULL) /* EOF */
goto done;
if (strcmp(string, ";") == 0) {
if (entry->strings && *entry->strings && strcmp(*entry->strings, "include") == 0) {
pconf_entry *included;
if (entry->strings + 1 == NULL || *(entry->strings + 1) == NULL) {
sprintf(pconf_error, "Missing file name on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
included = parse_conf(*(entry->strings + 1), NULL);
free_pconf_enteries(entry);
entry = NULL;
if (included == NULL)
goto error_return;
entry = included;
}
goto done;
}
if (strcmp(string, "}") == 0)
goto done; /* Must be an empty list */
/* Must have hit at "=" */
entry->values = read_list(pcontext, &string, error, !IS_LVALUE);
if (*error)
goto error_return;
/* Is it the start of another entry list? */
if (strcmp(string, "{") == 0) {
entry->type = CONF_TYPE_LIST;
entry->list = parse_list(pcontext, EXPECT_EOL, error);
if (*error)
goto error_return;
} else {
entry->type = CONF_TYPE_VALUE;
}
done:
*terminator = string;
*error = FALSE;
/*
* Check and see if we actually read anything, and if not return null.
*/
if (entry->strings == NULL) {
free(entry);
entry = NULL;
}
return entry;
error_return:
if (entry)
free_pconf_enteries(entry);
*error = TRUE;
return NULL;
}
/*
* Read a list of strings stopping if a special character (;={}) or EOF
* is reached (which is an error). The string with the specical character
* is returned in terminator. If an error is encounted, NULL is returned
* and error is set to 1, else 0.
*/
static char **
read_list(pconf_context * pcontext, char **terminator, int *error, int is_lvalue)
{
char **list;
char *string;
int size = 10; /* Initial size of array */
int num_items = 0;
list = (char **)malloc(sizeof(char *) * size);
if (list == NULL) {
sprintf(pconf_error, "malloc() failed");
goto error_return;
}
list[0] = NULL;
while ((string = get_token(pcontext))) {
/* ; is always a legal terminator */
if (strcmp(string, ";") == 0)
break;
/* = is legal if this is a lvalue and we're read something */
if (strcmp(string, "=") == 0) {
if (is_lvalue && (num_items != 0))
break;
sprintf(pconf_error, "Unexpected = on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
/*
* } is only legal if this is an lvalue and
* we haven't read anything
*/
if (strcmp(string, "}") == 0) {
if (is_lvalue && (num_items == 0))
break;
sprintf(pconf_error, "Unexpected } on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
/*
* { is legal if this is not an lvalue and we haven't read
* anything.
*/
if (strcmp(string, "{") == 0) {
if (!is_lvalue && (num_items == 0))
break;
sprintf(pconf_error, "Unexpected { on line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
/*
* Add this string to the list, making sure we have enough space
* allocated.
*/
if ((num_items + 2) > size) {
size += 10;
list = (char **)realloc(list, sizeof(char *) * size);
if (list == NULL) {
sprintf(pconf_error, "realloc() failed");
goto error_return;
}
}
list[num_items] = strdup(string);
num_items++;
list[num_items] = NULL;
}
/* EOF is only legal if this is a lvalue and we haven't read anything */
if (string == NULL)
if (!(is_lvalue && (num_items == 0))) {
sprintf(pconf_error, "Unexpected EOF at line %d of file %s",
pcontext->linenum, pcontext->filename);
goto error_return;
}
if (num_items) {
/* Trim array down to size, leaving room for terminating NULL */
list = (char **)realloc(list, sizeof(char *) * (num_items + 1));
if (list == NULL) {
sprintf(pconf_error, "realloc() failed");
goto error_return;
}
list[num_items] = NULL;
} else {
/* Nothing read */
free(list);
list = NULL;
}
*terminator = string;
*error = FALSE;
return list;
error_return:
if (list) {
char **string = list;
while (*string) {
free(*string);
string++;
}
free(list);
}
*error = TRUE;
return NULL;
}
/*
* Get the next token from the currently being parsed file. Handles
* getting new lines.
*/
static char *
get_token(pconf_context * pcontext)
{
static char buffer[BUFSIZE];
char *token = NULL;
int in_comment = FALSE;
if (pcontext->buffer_read) {
token = strtok(NULL, pcontext->delim);
if (token && (*token == '#'))
in_comment = TRUE;
}
while ((token == NULL) || in_comment) {
/*
* If we're in a comment then just read the rest of the
* line, if we haven't read the whole this, and discard it
*/
if (in_comment && (pcontext->whole_line == FALSE)) {
while (1) {
if (fgets(buffer, BUFSIZE, pcontext->file) == NULL)
return NULL;
if (buffer[strlen(buffer) - 1] == '\n')
break;
}
}
in_comment = FALSE;
/* Read our next line to parse */
if (fgets(buffer, BUFSIZE, pcontext->file) == NULL)
return NULL;
pcontext->buffer_read = TRUE;
/* Did we get a CR? */
if (buffer[strlen(buffer) - 1] == '\n') {
(pcontext->linenum)++;
pcontext->whole_line = TRUE;
} else {
pcontext->whole_line = FALSE;
}
token = strtok(buffer, pcontext->delim);
if (token && (*token == '#'))
in_comment = TRUE;
}
return token;
}