-
Notifications
You must be signed in to change notification settings - Fork 1
/
ucfg.c
478 lines (398 loc) · 9.9 KB
/
ucfg.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
/* Copyright (c) 2010, Torbjörn Lönnemark <[email protected]>
*
* Permission to use, copy, modify, and/or 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ucfg.h"
/* oom handler */
static void oom(void) __attribute__((noreturn));
static void oom(void)
{
static const char msg[] = "libucfg: Not enough memory\n";
fputs(msg, stderr);
abort();
}
/* malloc that aborts on oom */
static void *ucfg_xmalloc(size_t size)
{
void *p;
if (!(p = malloc(size))) {
oom();
}
return p;
}
/* realloc that aborts on oom */
static void *ucfg_xrealloc(void *ptr, size_t size)
{
void *p;
if (!(p = realloc(ptr, size))) {
oom();
}
return p;
}
/* convert return values to strings */
const char *ucfg_strerror(int error)
{
static const char *messages[UCFG_ERR_MAX] = {
"libucfg: OK", /* UCFG_OK */
"libucfg: Error when opening file for reading", /* UCFG_ERR_FILE_READ */
"libucfg: Error when opening file for writing", /* UCFG_ERR_FILE_WRITE */
"libucfg: Syntax error in provided config", /* UCFG_ERR_SYNTAX */
"libucfg: Requested node does not exist" /* UCFG_ERR_NODE_INEXISTENT */
};
if (error >= UCFG_ERR_MAX) {
return NULL;
}
return messages[error];
}
/* initialize a config structure */
void ucfg_node_init(struct ucfg_node **conf)
{
*conf = ucfg_xmalloc(sizeof(**conf));
(*conf)->name = NULL;
(*conf)->value = NULL;
(*conf)->sub = NULL;
(*conf)->next = NULL;
}
/* destroy a config structure */
void ucfg_node_destroy(struct ucfg_node *conf)
{
if (conf != NULL) {
ucfg_node_destroy(conf->sub);
conf->sub = NULL;
ucfg_node_destroy(conf->next);
conf->next = NULL;
if (conf->name != NULL) {
free(conf->name);
conf->name = NULL;
}
if (conf->value != NULL) {
free(conf->value);
conf->value = NULL;
}
free(conf);
}
}
/* make the current node contain a new subsection */
void ucfg_node_sub_create(struct ucfg_node *conf, struct ucfg_node **created)
{
ucfg_node_init(&conf->sub);
if (created != NULL) {
*created = conf->sub;
}
}
/* append a config node onto a section */
void ucfg_node_next_append(struct ucfg_node *conf, struct ucfg_node **created)
{
struct ucfg_node *tmp;
tmp = conf;
while (tmp->next != NULL) {
tmp = tmp->next;
}
ucfg_node_init(&tmp->next);
if (created != NULL) {
*created = tmp->next;
}
}
/* set the name of a config node */
void ucfg_node_name_set(struct ucfg_node *conf, const char *name)
{
conf->name = ucfg_xmalloc(strlen(name) + 1);
strcpy(conf->name, name);
}
/* set the value of a config node */
void ucfg_node_value_set(struct ucfg_node *conf, const char *value)
{
conf->value = ucfg_xmalloc(strlen(value) + 1);
strcpy(conf->value, value);
}
/* escape double-quotes in a string */
static char *ucfg_string_escape(char *str)
{
char *tmp, *res, *old_pos;
tmp = old_pos = str;
res = ucfg_xmalloc(1);
*res = '\0';
while ((tmp = strchr(tmp, '"')) != NULL) {
int res_old_len = strlen(res) + 1;
char *res_old_end;
int new_len = res_old_len + (tmp - old_pos + 1) + 1;
res = ucfg_xrealloc(res, new_len);
res_old_end = res + strlen(res);
strncpy(res_old_end, old_pos, tmp - old_pos + 1);
*(res + new_len - 2) = '"';
*(res + new_len - 1) = '\0';
++tmp;
old_pos = tmp;
}
{
char *res_old_end;
int new_len = strlen(res) + strlen(old_pos) + 1;
res = ucfg_xrealloc(res, new_len);
res_old_end = res + strlen(res);
strcpy(res_old_end, old_pos);
}
return res;
}
/* serialize a config structure onto a file stream, with indentation */
static void ucfg_write_indented(struct ucfg_node *conf, int indent, FILE *stream)
{
fprintf(stream, "%*s", indent, "");
if (conf->name)
fprintf(stream, "%s: ", conf->name);
if (conf->value) {
char *esc = ucfg_string_escape(conf->value);
fprintf(stream, "\"%s\";\n", esc);
free(esc);
} else if (conf->sub) {
fprintf(stream, "{\n");
ucfg_write_indented(conf->sub, indent + 2, stream);
fprintf(stream, "%*s", indent, "");
fprintf(stream, "}\n");
}
if (conf->next != NULL) {
ucfg_write_indented(conf->next, indent, stream);
}
}
/* serialize a config structure onto a file stream */
void ucfg_write(struct ucfg_node *conf, FILE *stream)
{
ucfg_write_indented(conf, 0, stream);
}
/* serialize a config structure into a config file */
int ucfg_write_file(struct ucfg_node *conf, const char *filename)
{
FILE *f = fopen(filename, "w");
if (!f) {
return UCFG_ERR_FILE_WRITE;
} else {
ucfg_write(conf, f);
fflush(f);
fclose(f);
}
return UCFG_OK;
}
/* size of steps to increase strings by while reading into them */
#define ALLOC_CHUNK_SIZE 32
/* discard whitespace from a file stream */
static void parse_read_whitespace(FILE *stream)
{
int tmp;
while ((tmp = getc(stream)) == ' ' || tmp == '\t' || tmp == '\n');
if(tmp != EOF) {
ungetc(tmp, stream);
}
}
/* parse and read a value entity from a file stream */
static char *parse_read_value(FILE *stream)
{
int tmp;
int len = 0;
int max_len = ALLOC_CHUNK_SIZE;
char *ret = ucfg_xmalloc(max_len);
while (1) {
if (len == max_len) {
max_len += ALLOC_CHUNK_SIZE;
ret = ucfg_xrealloc(ret, max_len);
}
if ((tmp = getc(stream)) == EOF) {
fprintf(stderr, "parse_read_value: expected character, got EOF\n");
free(ret);
ret = NULL;
break;
} else if (tmp == '"') { /* escaped double-quote or end of value */
if ((tmp = getc(stream)) == '"') { /* escaped double-quote */
ret[len++] = '"';
} else if (tmp == ';') { /* end of value */
break;
} else {
fprintf(stderr, "parse_read_value: expected ';', got '%c'\n", tmp);
free(ret);
ret = NULL;
break;
}
} else {
ret[len++] = tmp;
}
}
if (ret != NULL) {
ret = ucfg_xrealloc(ret, len + 1);
ret[len] = '\0';
}
return ret;
}
/* parse and read a name entity from a file stream */
static char *parse_read_name(FILE *stream)
{
int len = 0;
int max_len = ALLOC_CHUNK_SIZE;
char *ret;
int tmp;
ret = ucfg_xmalloc(max_len);
while (1) {
if (len == max_len) {
max_len += ALLOC_CHUNK_SIZE;
ret = ucfg_xrealloc(ret, max_len);
}
if ((tmp = getc(stream)) == EOF) {
fprintf(stderr, "parse_read_name:"
"expected character, got EOF\n");
free(ret);
ret = NULL;
break;
} else if (tmp == ':') { /* end of name */
break;
} else {
ret[len++] = tmp;
}
}
if (ret != NULL) {
ret = ucfg_xrealloc(ret, len + 1);
ret[len] = '\0';
}
return ret;
}
/* create a config structure from a file stream */
int ucfg_read(struct ucfg_node **dest, FILE *stream)
{
struct ucfg_node *cur;
int retval = UCFG_OK;
int tmpc;
char *tmps;
ucfg_node_init(dest);
cur = *dest;
do {
parse_read_whitespace(stream);
tmpc = getc(stream);
if (tmpc != EOF) {
if (tmpc == '{') { /* read list, recurse */
if (cur->sub != NULL) {
ucfg_node_init(&cur->next);
cur = cur->next;
}
if ((retval = ucfg_read(&cur->sub, stream)) != UCFG_OK) {
break;
}
} else if (tmpc == '}') { /* end of list */
break;
} else if (tmpc == '"') {
if (cur->value != NULL) {
ucfg_node_init(&cur->next);
cur = cur->next;
}
if ((tmps = parse_read_value(stream)) != NULL) {
cur->value = tmps;
} else {
retval = UCFG_ERR_SYNTAX;
break;
}
} else { /* read name */
ungetc(tmpc, stream);
if (cur->name != NULL && cur->sub == NULL && cur->value == NULL) {
retval = UCFG_ERR_SYNTAX;
break;
} else if (cur->sub != NULL || cur->value != NULL) {
ucfg_node_init(&cur->next);
cur = cur->next;
}
if ((tmps = parse_read_name(stream)) != NULL) {
cur->name = tmps;
} else {
retval = UCFG_ERR_SYNTAX;
}
}
}
} while (retval == UCFG_OK && !feof(stream));
if (retval != UCFG_OK) {
ucfg_node_destroy(*dest);
*dest = NULL;
}
return retval;
}
/* create a config structure from a config file */
int ucfg_read_file(struct ucfg_node **dest, const char *filename)
{
int retval;
FILE *f = fopen(filename, "r");
if (!f) {
retval = UCFG_ERR_FILE_READ;
} else {
retval = ucfg_read(dest, f);
fclose(f);
}
return retval;
}
/* split a colon-delimeted string. function modifies its first argument. */
static char *strsepc(char **stringp)
{
if (*stringp == NULL) {
return NULL;
} else {
char *ret;
char *pos;
ret = *stringp;
if ((pos = strchr(*stringp, ':')) != NULL) {
*pos = '\0';
*stringp = pos + 1;
} else {
*stringp = NULL;
}
return ret;
}
}
/* lookup a section or value in a config structure */
int ucfg_lookup(struct ucfg_node **found, struct ucfg_node *root, const char *path)
{
char *lpath;
char *curpos;
char *curstr;
lpath = ucfg_xmalloc(strlen(path) + 1);
strcpy(lpath, path);
curpos = lpath;
*found = root;
curstr = strsepc(&curpos);
while (curstr != NULL) {
while (*found != NULL) {
if (strcmp(curstr, "") == 0) {
break;
} else if ((*found)->name != NULL &&
strcmp((*found)->name, curstr) == 0) {
break;
}
*found = (*found)->next;
}
if (*found == NULL) {
break;
}
curstr = strsepc(&curpos);
if (curstr != NULL) {
*found = (*found)->sub;
}
}
free(lpath);
if (*found == NULL) {
return UCFG_ERR_NODE_INEXISTENT;
}
return UCFG_OK;
}
/* convenience function for looking up a string */
char *ucfg_lookup_string(struct ucfg_node *root, const char *path)
{
struct ucfg_node *result;
if (ucfg_lookup(&result, root, path) != UCFG_OK) {
return NULL;
}
return result->value;
}