forked from meetecho/janus-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
463 lines (438 loc) · 12.3 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
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
/*! \file config.c
* \author Lorenzo Miniero <[email protected]>
* \copyright GNU General Public License v3
* \brief Configuration files parsing
* \details Implementation of a parser of INI configuration files.
*
* \ingroup core
* \ref core
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "config.h"
#include "debug.h"
#include "utils.h"
/* Filename helper */
static char *get_filename(const char *path) {
char *filename = NULL;
if(path)
filename = strrchr(path, '/')+1;
return filename;
}
/* Trimming helper */
static char *ltrim(char *s) {
if(strlen(s) == 0)
return s;
while(isspace(*s))
s++;
return s;
}
static char *rtrim(char *s) {
if(strlen(s) == 0)
return s;
char *back = s + strlen(s);
while(isspace(*--back));
*(back+1) = '\0';
return s;
}
static char *trim(char *s) {
if(strlen(s) == 0)
return s;
return rtrim(ltrim(s));
}
/* Memory management helpers */
static void janus_config_free_item(gpointer data) {
janus_config_item *i = (janus_config_item *)data;
if(i) {
if(i->name)
g_free((gpointer)i->name);
if(i->value)
g_free((gpointer)i->value);
g_free(i);
}
}
static void janus_config_free_category(gpointer data) {
janus_config_category *c = (janus_config_category *)data;
if(c) {
if(c->name)
g_free((gpointer)c->name);
if(c->items)
g_list_free_full(c->items, janus_config_free_item);
g_free(c);
}
}
/* Public methods */
janus_config *janus_config_parse(const char *config_file) {
if(config_file == NULL)
return NULL;
char *filename = get_filename(config_file);
if(filename == NULL) {
JANUS_LOG(LOG_ERR, "Invalid filename %s\n", config_file);
return NULL;
}
/* Open file */
FILE *file = fopen(config_file, "rt");
if(!file) {
JANUS_LOG(LOG_ERR, " -- Error reading configuration file '%s'... error %d (%s)\n", filename, errno, strerror(errno));
return NULL;
}
/* Create configuration instance */
janus_config *jc = g_malloc0(sizeof(janus_config));
jc->name = g_strdup(filename);
/* Traverse and parse it */
int line_number = 0;
char line_buffer[BUFSIZ];
janus_config_category *cg = NULL;
while(fgets(line_buffer, sizeof(line_buffer), file)) {
line_number++;
if(strlen(line_buffer) == 0)
continue;
/* Strip comments */
char *line = line_buffer, *sc = line, *c = NULL;
while((c = strchr(sc, ';')) != NULL) {
if(c == line || *(c-1) != '\\') {
/* Comment starts here */
*c = '\0';
break;
}
/* Escaped semicolon, remove the slash */
sc = c-1;
/* length will be at least 2: ';' '\0' */
memmove(sc, c, strlen(c)+1);
/* Go on */
sc++;
}
/* Trim (will remove newline characters too) */
line = trim(line);
if(strlen(line) == 0)
continue;
/* Parse */
if(line[0] == '[') {
/* Category */
line++;
char *end = strchr(line, ']');
if(end == NULL) {
JANUS_LOG(LOG_ERR, "Error parsing category at line %d: syntax error (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
*end = '\0';
line = trim(line);
if(strlen(line) == 0) {
JANUS_LOG(LOG_ERR, "Error parsing category at line %d: no name (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
cg = janus_config_add_category(jc, line);
if(cg == NULL) {
JANUS_LOG(LOG_ERR, "Error adding category %s (%s)\n", line, filename);
janus_config_destroy(jc);
return NULL;
}
} else {
/* Item */
char *name = line, *value = strchr(line, '=');
if(value == NULL || value == line) {
JANUS_LOG(LOG_ERR, "Error parsing item at line %d (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
*value = '\0';
name = trim(name);
if(strlen(name) == 0) {
JANUS_LOG(LOG_ERR, "Error parsing item at line %d: no name (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
value++;
value = trim(value);
if(strlen(value) == 0) {
JANUS_LOG(LOG_ERR, "Error parsing item at line %d: no value (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
if(*value == '>') {
value++;
value = trim(value);
if(strlen(value) == 0) {
JANUS_LOG(LOG_ERR, "Error parsing item at line %d: no value (%s)\n", line_number, filename);
janus_config_destroy(jc);
return NULL;
}
}
if(janus_config_add_item(jc, cg ? cg->name : NULL, name, value) == NULL) {
if(cg == NULL)
JANUS_LOG(LOG_ERR, "Error adding item %s (%s)\n", name, filename);
else
JANUS_LOG(LOG_ERR, "Error adding item %s to category %s (%s)\n", name, cg->name, filename);
janus_config_destroy(jc);
return NULL;
}
}
}
fclose(file);
return jc;
}
janus_config *janus_config_create(const char *name) {
janus_config *jc = g_malloc0(sizeof(janus_config));
if(jc == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
if(name != NULL) {
jc->name = g_strdup(name);
}
return jc;
}
GList *janus_config_get_categories(janus_config *config) {
if(config == NULL)
return NULL;
return config->categories;
}
janus_config_category *janus_config_get_category(janus_config *config, const char *name) {
if(config == NULL || name == NULL)
return NULL;
if(config->categories == NULL)
return NULL;
GList *l = config->categories;
while(l) {
janus_config_category *c = (janus_config_category *)l->data;
if(c && c->name && !strcasecmp(name, c->name))
return c;
l = l->next;
}
return NULL;
}
GList *janus_config_get_items(janus_config_category *category) {
if(category == NULL)
return NULL;
return category->items;
}
janus_config_item *janus_config_get_item(janus_config_category *category, const char *name) {
if(category == NULL || name == NULL)
return NULL;
if(category->items == NULL)
return NULL;
GList *l = category->items;
while(l) {
janus_config_item *i = (janus_config_item *)l->data;
if(i && i->name && !strcasecmp(name, i->name))
return i;
l = l->next;
}
return NULL;
}
janus_config_item *janus_config_get_item_drilldown(janus_config *config, const char *category, const char *name) {
if(config == NULL || category == NULL || name == NULL)
return NULL;
janus_config_category *c = janus_config_get_category(config, category);
if(c == NULL)
return NULL;
return janus_config_get_item(c, name);
}
janus_config_category *janus_config_add_category(janus_config *config, const char *category) {
if(config == NULL || category == NULL)
return NULL;
janus_config_category *c = janus_config_get_category(config, category);
if(c != NULL) {
/* Category exists, return this */
return c;
}
c = g_malloc0(sizeof(janus_config_category));
if(c == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
c->name = g_strdup(category);
config->categories = g_list_append(config->categories, c);
return c;
}
int janus_config_remove_category(janus_config *config, const char *category) {
if(config == NULL || category == NULL)
return -1;
janus_config_category *c = janus_config_get_category(config, category);
if(c) {
config->categories = g_list_remove(config->categories, c);
janus_config_free_category(c);
return 0;
}
return -2;
}
janus_config_item *janus_config_add_item(janus_config *config, const char *category, const char *name, const char *value) {
if(config == NULL || name == NULL || value == NULL)
return NULL;
/* This will return the existing category, if it exists already */
janus_config_category *c = category ? janus_config_add_category(config, category) : NULL;
if(category != NULL && c == NULL) {
/* Create it */
JANUS_LOG(LOG_FATAL, "Category error!\n");
return NULL;
}
janus_config_item *item = c ? janus_config_get_item(c, name) : NULL;
if(item == NULL) {
/* Create it */
item = g_malloc0(sizeof(janus_config_item));
if(item == NULL) {
JANUS_LOG(LOG_FATAL, "Memory error!\n");
return NULL;
}
item->name = g_strdup(name);
item->value = g_strdup(value);
if(c != NULL) {
/* Add to category */
c->items = g_list_append(c->items, item);
} else {
/* Uncategorized item */
config->items = g_list_append(config->items, item);
}
} else {
/* Update it */
char *item_value = g_strdup(value);
if(item->value)
g_free((gpointer)item->value);
item->value = item_value;
}
return item;
}
int janus_config_remove_item(janus_config *config, const char *category, const char *name) {
if(config == NULL || category == NULL || name == NULL)
return -1;
janus_config_category *c = janus_config_add_category(config, category);
if(c == NULL)
return -2;
janus_config_item *item = janus_config_get_item(c, name);
if(item == NULL)
return -3;
c->items = g_list_remove(c->items, item);
janus_config_free_item(item);
return 0;
}
void janus_config_print(janus_config *config) {
if(config == NULL)
return;
JANUS_LOG(LOG_VERB, "[%s]\n", config->name ? config->name : "??");
if(config->items) {
GList *l = config->items;
while(l) {
janus_config_item *i = (janus_config_item *)l->data;
JANUS_LOG(LOG_VERB, " %s: %s\n", i->name ? i->name : "??", i->value ? i->value : "??");
l = l->next;
}
}
if(config->categories) {
GList *l = config->categories;
while(l) {
janus_config_category *c = (janus_config_category *)l->data;
JANUS_LOG(LOG_VERB, " [%s]\n", c->name ? c->name : "??");
if(c->items) {
GList *li = c->items;
while(li) {
janus_config_item *i = (janus_config_item *)li->data;
JANUS_LOG(LOG_VERB, " %s: %s\n", i->name ? i->name : "??", i->value ? i->value : "??");
li = li->next;
}
}
l = l->next;
}
}
}
gboolean janus_config_save(janus_config *config, const char *folder, const char *filename) {
if(config == NULL)
return -1;
FILE *file = NULL;
char path[1024];
if(folder != NULL) {
/* Create folder, if needed */
if(janus_mkdir(folder, 0755) < 0) {
JANUS_LOG(LOG_ERR, "Couldn't save configuration file, error creating folder '%s'...\n", folder);
return -2;
}
g_snprintf(path, 1024, "%s/%s.cfg", folder, filename);
} else {
g_snprintf(path, 1024, "%s.cfg", filename);
}
file = fopen(path, "wt");
if(file == NULL) {
JANUS_LOG(LOG_ERR, "Couldn't save configuration file, error opening file '%s'...\n", path);
return -3;
}
/* Print a header */
char date[64], header[256];
struct tm tmresult;
time_t ltime = time(NULL);
localtime_r(<ime, &tmresult);
strftime(date, sizeof(date), "%a %b %e %T %Y", &tmresult);
g_snprintf(header, 256, ";\n; File automatically generated on %s\n;\n\n", date);
fwrite(header, sizeof(char), strlen(header), file);
/* Go on with the configuration */
if(config->items) {
GList *l = config->items;
while(l) {
janus_config_item *i = (janus_config_item *)l->data;
if(i->name && i->value) {
fwrite(i->name, sizeof(char), strlen(i->name), file);
fwrite(" = ", sizeof(char), 3, file);
fwrite(i->value, sizeof(char), strlen(i->value), file);
fwrite("\n", sizeof(char), 1, file);
}
l = l->next;
}
}
if(config->categories) {
GList *l = config->categories;
while(l) {
janus_config_category *c = (janus_config_category *)l->data;
if(c->name) {
fwrite("[", sizeof(char), 1, file);
fwrite(c->name, sizeof(char), strlen(c->name), file);
fwrite("]\n", sizeof(char), 2, file);
if(c->items) {
GList *li = c->items;
while(li) {
janus_config_item *i = (janus_config_item *)li->data;
if(i->name && i->value) {
fwrite(i->name, sizeof(char), strlen(i->name), file);
fwrite(" = ", sizeof(char), 3, file);
/* If the value contains a semicolon, escape it */
if(strchr(i->value, ';')) {
char *value = g_strdup(i->value);
value = janus_string_replace((char *)value, ";", "\\;");
fwrite(value, sizeof(char), strlen(value), file);
fwrite("\n", sizeof(char), 1, file);
g_free(value);
} else {
/* No need to escape */
fwrite(i->value, sizeof(char), strlen(i->value), file);
fwrite("\n", sizeof(char), 1, file);
}
}
li = li->next;
}
}
}
fwrite("\r\n", sizeof(char), 2, file);
l = l->next;
}
}
fclose(file);
return 0;
}
void janus_config_destroy(janus_config *config) {
if(config == NULL)
return;
if(config->items) {
g_list_free_full(config->items, janus_config_free_item);
config->items = NULL;
}
if(config->categories) {
g_list_free_full(config->categories, janus_config_free_category);
config->items = NULL;
}
if(config->name)
g_free((gpointer)config->name);
g_free((gpointer)config);
config = NULL;
}