-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.c
382 lines (350 loc) · 9.22 KB
/
config.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
/*
* Copyright (C) 2010 Luigi Rizzo, Universita' di Pisa
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* a config file has a number of section marked by [section-name]
* followed by 'key = value' lines.
* A \\ protects the next character, while strings in ' and " are
* also protected.
* A ';' anywhere, or a # in the left hand side start a comment.
* Spaces are allowed both in key and value.
*/
#define _GNU_SOURCE /* asprintf */
#include <fcntl.h>
#include <time.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include "config.h"
extern int verbose;
#ifndef DBG
#define DBG(level, format, ...) do { \
if (verbose >= level) \
fprintf(stderr, "[%-14.14s %4d] " format, \
__FUNCTION__, __LINE__, ##__VA_ARGS__); \
} while(0)
#endif
struct section {
struct section *next;
char *name;
struct entry *keys; /* children */
};
/*
* A buffer contains inline the storage for the backing file,
* and in case we read from multiple files we need to link
* the buffers together.
* Individual entries (section and entry) are allocated
* individually.
*/
struct config {
struct config *next; /* chain pointer */
char *pbuf ; /* pointer to the config file buffer (inline) */
struct section *sections ; /* first section */
};
/* skip leading whitespace */
char *skipws(char *p)
{
return p ? p + strspn(p, " \t\r\n") : p;
}
/* trim trailing unescaped whitespace, including comments.
* 'end' points to the first character after our string. If NULL, it is computed.
* Unfortunately, to understand escape sequences one must process left to right.
*/
char *trimws(char *s, char *end)
{
int in_quote = 0;
char *start = s, *last_ws = NULL;
if (end == NULL)
end = s + strlen(s);
if (end == s)
return start;
for (; s < end; s++) {
if (*s == '\\') {
if (s+1 < end)
s++;
last_ws = NULL;
} else if (in_quote) {
in_quote = (*s != '"' && *s != '\'');
} else if (*s == '"' || *s == '\'') {
in_quote = 1;
last_ws = NULL;
} else if (*s == ';' || (s == start && *s == '#')) {
if (!last_ws)
last_ws = s;
break;
} else if (index(" \t\r\n", *s)) {
if (!last_ws)
last_ws = s;
} else
last_ws = NULL;
}
if (last_ws)
s = last_ws;
*s = '\0';
return start;
}
/*
* Parse a name in key = value pair. If sep[0] == '='
* and '=' is not found return an error.
* Trim left and right whitespace.
*/
static char *parse_name(char **p, const char *sep)
{
char c, *q;
int l;
if (!sep || !p || !*p || !**p)
return NULL;
q = *p = skipws(*p);
for (l=0; q[l] && !index(sep, q[l]); l++) {
if (q[l] == '\\' && q[l+1])
l++;
}
if (sep[0] == '=' && q[l] != '=') /* looking for a key, missing = */
return NULL;
*p += l;
c = q[l];
q[l] = '\0';
if (c)
(*p)++;
trimws(q, q+l);
return q;
}
/* parse the content of a config file. We expect the buffer to be
* persistent and writable.
*/
static int cfg_parse(struct config *db, char *p, const char *basedir)
{
struct section *cur;
struct entry *kcur;
DBG(3, "start, db %p content\n%.50s\n...\n", db, p);
cur = NULL;
kcur = NULL;
while (p && *p) {
char c;
char *start;
char *key, *val;
start = strsep(&p, "\n"); /* to end of line */
start = skipws(start); /* skip whitespace */
trimws(start, p - 1);
switch (*start) {
case '\0': /* comment line */
case ';': /* comment line */
case '#': /* comment line */
break ;
case '[': /* section delimiter */
key = ++start; /* skip it */
while (isalnum(*key) || index("-_", *key))
key++;
c = *key;
*key = '\0';
if (c)
key++;
if (c != ']') {
DBG(0, "invalid section name %s %c\n",
start, c);
break;
}
DBG(1, "start section %s\n", start);
cur = cfg_find_section(db, start);
if (!cur) { /* allocate new */
cur = calloc(1, sizeof(struct section));
if (cur == NULL) {
DBG(0, "cannot allocate section %s\n", start);
return -1;
}
cur->next = db->sections;
db->sections = cur;
cur->name = start;
}
break;
default: /* it a a key/value string then */
DBG(3, "key name pair\n");
key = parse_name(&start, "=\r\n");
val = key ? parse_name(&start, "\r\n") : NULL;
DBG(2, "after parse name next p %p %d\n", p, *p);
if (!val) {
if (key) {
DBG(0, "cannot parse name %s\n", start);
return -1;
}
break;
}
DBG(1, "key [%s] val [%s]\n", key, val);
if (!strcmp(key, "include")) {
DBG(1, "processing include %s\n", val);
cfg_read(val, basedir, db);
break;
}
if (!cur) {
DBG(0, "key val outside section, ignore\n");
break;
}
kcur = (struct entry *)cfg_find_entry(cur, start);
if (kcur) {
DBG(0, "replace val %s\n", kcur->value);
// XXX should we add ?
} else {
kcur = calloc(1, sizeof(struct entry));
if (kcur == NULL) {
DBG(0, "cannot allocate key %s\n", start);
return -1;
}
kcur->next = cur->keys;
cur->keys = kcur;
kcur->key = key;
}
kcur->value = val;
break ;
}
}
DBG(1, "END db %p\n", db);
return 0;
}
/*
* Creates a database for file 'path', optionally prepending 'base' to
* the file name. If 'path' starts with newline, then this is an
* immediate string with the content of the file.
* If the db argument is non null then it appends to the existing db.
*/
struct config *cfg_read(const char *path, const char *base,
struct config *old)
{
struct config *db = NULL;
int n, len, fd = -1;
char *buf;
DBG(1, "%s\n", path);
if (path == NULL)
return NULL;
if (path[0] == '\n') {
len = strlen(path);
goto immediate;
}
if ( (fd = open(path, O_RDONLY)) >= 0)
goto good;
if (path[0] != '.' && path[0] != '/') { // try alternate location
char *p;
asprintf(&p, "%s/%s", base, path);
fd = open(p, O_RDONLY);
if ( fd >= 0)
goto good;
}
DBG(0, "error opening %s\n", path);
return old;
good:
len = lseek(fd, 0, SEEK_END) ;
lseek(fd, 0, SEEK_SET) ;
immediate:
// allocate memory for descriptor + data + trailing '\0'
db = calloc(1, sizeof(struct config) + len + 1);
if (db == NULL)
goto error;
buf = db->pbuf = (char *)(db + 1); // data are after descriptor
// read the file
if (fd < 0) {
memcpy(db->pbuf, path, len);
} else {
n = read(fd, db->pbuf, len);
if (n != len) {
DBG(0, "cannot read file %s size %d\n", path, len) ;
goto error;
}
close(fd) ; /* don't need the file open anymore .. */
fd = -1;
}
if (old) { /* link the db to the next one */
db->next = old;
old->next = db;
db = old;
}
/* do not abort if we are extending another file */
if (!cfg_parse(db, buf, base) || old)
return db ;
DBG(0, "can't create db structure\n") ;
error:
if (db)
free(db);
if (fd >= 0)
close(fd) ;
return old ;
}
/*
* destructor for the data structure
*/
void cfg_free(struct config *p)
{
struct section *s;
struct entry *k;
if (!p)
return;
while ( (s = p->sections) ) {
p->sections = s->next;
while ( (k = s->keys) ) {
s->keys = k->next;
free(k);
}
free(s);
}
free(p) ;
}
const char *cfg_section_name(const struct section *sec)
{
return sec ? sec->name : NULL;
}
struct section *cfg_find_section(struct config *db, const char *name)
{
struct section *s;
if (db == NULL) /* iterate, use name as current ptr */
return name ? ((struct section *)name)->next : NULL;
s = db->sections;
if (name == NULL)
return s;
for (; s; s = s->next)
if (!strcasecmp(name, s->name))
return s;
return NULL ;
}
const struct entry * cfg_find_entry(const struct section *s, const char *name)
{
const struct entry *k ;
if (!s)
return NULL;
k = s->keys;
if (name == NULL)
return k;
for (; k; k = k->next)
if (strcasecmp(name, k->key) == 0)
return k;
return NULL ;
}
const char *cfg_find_val(struct config *db, const char *sec, const char *name)
{
const struct section *s = db ? cfg_find_section(db, sec) :
(const struct section *)sec;
const struct entry *k = cfg_find_entry(s, name);
return k ? k->value : NULL;
}