aboutsummaryrefslogtreecommitdiffstats
path: root/config.c
blob: 21c8fb9a5305f76aeaacaa2d8c0d08145834f2fc (plain)
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Configuration File Parser
 * 
 * Copyright (C) 1999, Mark Spencer
 *
 * Mark Spencer <markster@linux-support.net>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <asterisk/config.h>
#include <asterisk/options.h>
#include <asterisk/logger.h>
#include "asterisk.h"
#include "astconf.h"

#define MAX_INCLUDE_LEVEL 10

struct ast_category {
	char name[80];
	struct ast_variable *root;
	struct ast_category *next;
	struct ast_comment *precomments;
	struct ast_comment *sameline;
};

struct ast_config {
	/* Maybe this structure isn't necessary but we'll keep it
	   for now */
	struct ast_category *root;
	struct ast_category *prev;
	struct ast_comment *trailingcomments;
};

struct ast_comment_struct
{
	struct ast_comment *root;
	struct ast_comment *prev;
};

static char *strip(char *buf)
{
	char *start;
	/* Strip off trailing whitespace, returns, etc */
	while(strlen(buf) && (buf[strlen(buf)-1]<33))
		buf[strlen(buf)-1] = '\0';
	start = buf;
	/* Strip off leading whitespace, returns, etc */
	while(*start && (*start < 33))
		*start++ = '\0';
	return start;
}

static void free_comments(struct ast_comment *com)
{
	struct ast_comment *l;
	while (com) {
		l = com;
		com = com->next;
		free(l->comment);
		free(l);
	}
}

void ast_destroy(struct ast_config *ast)
{
	struct ast_category *cat, *catn;
	struct ast_variable *v, *vn;

	if (!ast)
		return;

	cat = ast->root;
	while(cat) {
		v = cat->root;
		while(v) {
			vn = v;
			free(v->name);
			free(v->value);
			free_comments(v->precomments);
			free_comments(v->sameline);
			v = v->next;
			free(vn);
		}
		catn = cat;
		free_comments(cat->precomments);
		free_comments(cat->sameline);
		cat = cat->next;
		free(catn);
	}
	free_comments(ast->trailingcomments);
	free(ast);
}

int ast_true(char *s)
{
	if (!s)
		return 0;
	/* Determine if this is a true value */
	if (!strcasecmp(s, "yes") ||
	    !strcasecmp(s, "true") ||
		!strcasecmp(s, "y") ||
		!strcasecmp(s, "t") ||
		!strcasecmp(s, "1"))
			return -1;
	return 0;
}

struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
{
	struct ast_category *cat;
	cat = config->root;
	while(cat) {
		if (cat->name == category)
			return cat->root;
		cat = cat->next;
	}
	cat = config->root;
	while(cat) {
		if (!strcasecmp(cat->name, category))
			return cat->root;
		cat = cat->next;
	}
	return NULL;
}

char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
{
	struct ast_variable *v;
	if (category) {
		v = ast_variable_browse(config, category);
		while (v) {
			if (value == v->name)
				return v->value;
			v=v->next;
		}
		v = ast_variable_browse(config, category);
		while (v) {
			if (!strcasecmp(value, v->name))
				return v->value;
			v=v->next;
		}
	} else {
		struct ast_category *cat;
		cat = config->root;
		while(cat) {
			v = cat->root;
			while (v) {
				if (!strcasecmp(value, v->name))
					return v->value;
				v=v->next;
			}
			cat = cat->next;
		}
	}
	return NULL;
}

int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
{
	struct ast_variable *v, *pv, *bv, *bpv;
	struct ast_category *cat;
	cat = cfg->root;
	while(cat) {
		if (cat->name == category) {
			break;
		}
		cat = cat->next;
	}
	if (!cat) {
		cat = cfg->root;
		while(cat) {
			if (!strcasecmp(cat->name, category)) {
				break;
			}
			cat = cat->next;
		}
	}
	if (!cat)
		return -1;
	v = cat->root;
	pv = NULL;
	while (v) {
		if ((variable == v->name) && (!value || !strcmp(v->value, value)))
			break;
		pv = v;
		v=v->next;
	}
	if (!v) {
		/* Get the last one that looks like it */
		bv = NULL;
		bpv = NULL;
		v = cat->root;
		pv = NULL;
		while (v) {
			if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
				bv = v;
				bpv = pv;
			}
			pv = v;
			v=v->next;
		}
		v = bv;
	}

	if (v) {
		/* Unlink from original position */
		if (pv) 
			pv->next = v->next;
		else
			cat->root = v->next;
		v->next = NULL;
		free(v->name);
		if (v->value)
			free(v->value);
		free_comments(v->sameline);
		free_comments(v->precomments);
		return 0;
	}
	return -1;
}

int ast_category_delete(struct ast_config *cfg, char *category)
{
	struct ast_variable *v, *pv;
	struct ast_category *cat, *cprev;
	cat = cfg->root;
	cprev = NULL;
	while(cat) {
		if (cat->name == category) {
			break;
		}
		cprev = cat;
		cat = cat->next;
	}
	if (!cat) {
		cat = cfg->root;
		cprev = NULL;
		while(cat) {
			if (!strcasecmp(cat->name, category)) {
				break;
			}
			cprev = cat;
			cat = cat->next;
		}
	}
	if (!cat)
		return -1;
	/* Unlink it */
	if (cprev)
		cprev->next = cat->next;
	else
		cfg->root = cat->next;
	v = cat->root;
	while (v) {
		pv = v;
		v=v->next;
		if (pv->value)
			free(pv->value);
		if (pv->name)
			free(pv->name);
		free_comments(pv->sameline);
		free_comments(pv->precomments);
		free(pv);
	}
	free_comments(cat->sameline);
	free_comments(cat->precomments);
	free(cat);
	return 0;
}

struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
{
	struct ast_variable *v, *pv, *bv, *bpv;
	struct ast_category *cat, *pcat;
	cat = config->root;
	if (!newcat) {
		while(cat) {
			if (cat->name == category) {
				break;
			}
			cat = cat->next;
		}
		if (!cat) {
			cat = config->root;
			while(cat) {
				if (!strcasecmp(cat->name, category)) {
					break;
				}
				cat = cat->next;
			}
		}
	}
	if (!cat) {
		cat = malloc(sizeof(struct ast_category));
		if (!cat)
			return NULL;
		memset(cat, 0, sizeof(struct ast_category));
		strncpy(cat->name, category, sizeof(cat->name));
		if (config->root) {
			/* Put us at the end */
			pcat = config->root;
			while(pcat->next)
				pcat = pcat->next;
			pcat->next = cat;
		} else {
			/* We're the first one */
			config->root = cat;
		}
			
	}
	if (!newvar) {
		v = cat->root;
		pv = NULL;
		while (v) {
			if (variable == v->name)
				break;
			pv = v;
			v=v->next;
		}
		if (!v) {
			/* Get the last one that looks like it */
			bv = NULL;
			bpv = NULL;
			v = cat->root;
			pv = NULL;
			while (v) {
				if (!strcasecmp(variable, v->name)) {
					bv = v;
					bpv = pv;
				}
				pv = v;
				v=v->next;
			}
			v = bv;
		}
	} else v = NULL;
	if (v && move) {
		/* Unlink from original position */
		if (pv) 
			pv->next = v->next;
		else
			cat->root = v->next;
		v->next = NULL;
	}
	if (!v) {
		v = malloc(sizeof(struct ast_variable));
		if (!v)
			return NULL;
		memset(v, 0, sizeof(struct ast_variable));
		v->name = strdup(variable);
		move = 1;
	}
	if (v->value)
		free(v->value);
	if (value)
		v->value = strdup(value);
	else
		v->value = strdup("");
	if (move) {
		if (cat->root) {
			pv = cat->root;
			while (pv->next) 
				pv = pv->next;
			pv->next = v;
		} else {
			cat->root = v;
		}
	}
	return v;
}

int ast_category_exist(struct ast_config *config, char *category_name)
{
	struct ast_category *category = NULL;

	category = config->root;

	while(category) {
		if (!strcasecmp(category->name,category_name)) 
			return 1;
		category = category->next;
	} 

	return 0;
}

static struct ast_comment *build_comment(char *cmt)
{
	struct ast_comment *c;
	c = malloc(sizeof(struct ast_comment));
	if (c) {
		memset(c, 0, sizeof(struct ast_comment));
		c->comment = strdup(cmt);
	}
	return c;
}

static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel, struct ast_comment_struct *acs);
static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel, struct ast_comment_struct *acs)
{
	char *c;
	char *cur;
	struct ast_variable *v;
	struct ast_comment *com = NULL;
	int object;
	/* Strip off lines using ; as comment */
	c = strchr(buf, ';');
	if (c) {
		*c = '\0';
		c++;
		if (*c != '!')
			com = build_comment(c);
	}
	cur = strip(buf);
	if (strlen(cur)) {
		/* Actually parse the entry */
		if (cur[0] == '[') {
			/* A category header */
			c = strchr(cur, ']');
			if (c) {
				*c = 0;
				*_tmpc = malloc(sizeof(struct ast_category));
				if (!*_tmpc) {
					ast_destroy(tmp);
					ast_log(LOG_WARNING,
						"Out of memory, line %d\n", lineno);
					return -1;
				}
				memset(*_tmpc, 0, sizeof(struct ast_category));
				strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
				(*_tmpc)->root =  NULL;
				(*_tmpc)->precomments = acs->root;
				(*_tmpc)->sameline = com;
				if (!tmp->prev)
					tmp->root = *_tmpc;
				else
					tmp->prev->next = *_tmpc;

				tmp->prev = *_tmpc;
				acs->root = NULL;
				acs->prev = NULL;
				*_last =  NULL;
			} else {
				ast_log(LOG_WARNING, 
					"parse error: no closing ']', line %d of %s\n", lineno, configfile);
			}
		} else if (cur[0] == '#') {
			/* A directive */
			cur++;
			c = cur;
			while(*c && (*c > 32)) c++;
			if (*c) {
				*c = '\0';
				c++;
				/* Find real argument */
				while(*c  && (*c < 33)) c++;
				if (!*c)
					c = NULL;
			} else 
				c = NULL;
			if (!strcasecmp(cur, "include")) {
				/* A #include */
				if (c) {
					while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
					/* Get rid of leading mess */
					cur = c;
					while(strlen(cur)) {
						c = cur + strlen(cur) - 1;
						if ((*c == '>') || (*c == '<') || (*c == '\"'))
							*c = '\0';
						else
							break;
					}
					if (includelevel < MAX_INCLUDE_LEVEL) {
						__ast_load(cur, tmp, _tmpc, _last, includelevel + 1, acs);
					} else 
						ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
				} else
					ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
				/* Strip off leading and trailing "'s and <>'s */
			} else 
				ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
		} else {
			/* Just a line (variable = value) */
			if (!*_tmpc) {
				ast_log(LOG_WARNING,
					"parse error: No category context for line %d of %s\n", lineno, configfile);
				ast_destroy(tmp);
				return -1;
			}
			c = strchr(cur, '=');
			if (c) {
				*c = 0;
				c++;
				/* Ignore > in => */
				if (*c== '>') {
					object = 1;
					c++;
				} else
					object = 0;
				v = malloc(sizeof(struct ast_variable));
				if (v) {
					memset(v, 0, sizeof(struct ast_variable));
					v->next = NULL;
					v->name = strdup(strip(cur));
					v->value = strdup(strip(c));
					v->lineno = lineno;
					v->object = object;
					/* Put and reset comments */
					v->precomments = acs->root;
					v->blanklines = 0;
					acs->prev = NULL;
					acs->root = NULL;
					v->sameline = com;
					if (*_last)
						(*_last)->next = v;
					else
						(*_tmpc)->root = v;
					*_last = v;
				} else {
					ast_destroy(tmp);
					ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
					return -1;
				}
			} else {
				ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
			}
														
		}
	} else {
		/* store any comments if there are any */
		if (com) {
			if (acs->prev)
				acs->prev->next = com;
			else
				acs->root = com;
			acs->prev = com;
		} else {
		if (*_last) 
			(*_last)->blanklines++;

		}
	}
	return 0;
}

static void dump_comments(FILE *f, struct ast_comment *comment)
{
	while (comment) {
		fprintf(f, ";%s", comment->comment);
		comment = comment->next;
	}
}

int ast_save(char *configfile, struct ast_config *cfg, char *generator)
{
	FILE *f;
	char fn[256];
	char date[256];
	time_t t;
	struct ast_variable *var;
	struct ast_category *cat;
	int blanklines = 0;
	if (configfile[0] == '/') {
		strncpy(fn, configfile, sizeof(fn)-1);
	} else {
		snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
	}
	time(&t);
	strncpy(date, ctime(&t), sizeof(date));
	if ((f = fopen(fn, "w"))) {
		if ((option_verbose > 1) && !option_debug)
			ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
		fprintf(f, ";!\n");
		fprintf(f, ";! Automatically generated configuration file\n");
		fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
		fprintf(f, ";! Generator: %s\n", generator);
		fprintf(f, ";! Creation Date: %s", date);
		fprintf(f, ";!\n");
		cat = cfg->root;
		while(cat) {
			/* Dump any precomments */
			dump_comments(f, cat->precomments);
			/* Dump section with any appropriate comment */
			if (cat->sameline) 
				fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->comment);
			else
				fprintf(f, "[%s]\n", cat->name);
			var = cat->root;
			while(var) {
				dump_comments(f, var->precomments);
				if (var->sameline) 
					fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->comment);
				else	
					fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
				if (var->blanklines) {
					blanklines = var->blanklines;
					while (blanklines) {
						fprintf(f, "\n");
						blanklines--;
					}
				}
					
				var = var->next;
			}
#if 0
			/* Put an empty line */
			fprintf(f, "\n");
#endif
			cat = cat->next;
		}
		dump_comments(f, cfg->trailingcomments);
	} else {
		if (option_debug)
			printf("Unable to open for writing: %s\n", fn);
		else if (option_verbose > 1)
			printf( "Unable to write (%s)", strerror(errno));
		return -1;
	}
	fclose(f);
	return 0;
}

static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel, struct ast_comment_struct *acs)
{
	char fn[256];
	char buf[256];
	FILE *f;
	int lineno=0;
	int master=0;

	if (configfile[0] == '/') {
		strncpy(fn, configfile, sizeof(fn)-1);
	} else {
		snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
	}
	if ((option_verbose > 1) && !option_debug) {
		ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
		fflush(stdout);
	}
	if ((f = fopen(fn, "r"))) {
		if (option_debug)
			ast_log(LOG_DEBUG, "Parsing %s\n", fn);
		else if (option_verbose > 1)
			ast_verbose( "Found\n");
		if (!tmp) {
			tmp = malloc(sizeof(struct ast_config));
			if (tmp)
				memset(tmp, 0, sizeof(struct ast_config));

			master = 1;
		}
		if (!tmp) {
			ast_log(LOG_WARNING, "Out of memory\n");
			fclose(f);
			return NULL;
		}
		while(!feof(f)) {
			fgets(buf, sizeof(buf), f);
			lineno++;
			if (!feof(f)) {
				if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel, acs)) {
					fclose(f);
					return NULL;
				}
			}
		}
		fclose(f);		
	} else {
		if (option_debug)
			ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
		else if (option_verbose > 1)
			ast_verbose( "Not found (%s)", strerror(errno));
	}
	if (master) {
		/* Keep trailing comments */
		tmp->trailingcomments = acs->root;
		acs->root = NULL;
		acs->prev = NULL;
	}
	return tmp;
}

struct ast_config *ast_load(char *configfile)
{
	struct ast_category *tmpc=NULL;
	struct ast_variable *last = NULL;
	struct ast_comment_struct acs = { NULL, NULL };
	return __ast_load(configfile, NULL, &tmpc, &last, 0, &acs);
}

char *ast_category_browse(struct ast_config *config, char *prev)
{	
	struct ast_category *cat;
	if (!prev) {
		if (config->root)
			return config->root->name;
		else
			return NULL;
	}
	cat = config->root;
	while(cat) {
		if (cat->name == prev) {
			if (cat->next)
				return cat->next->name;
			else
				return NULL;
		}
		cat = cat->next;
	}
	cat = config->root;
	while(cat) {
		if (!strcasecmp(cat->name, prev)) {
			if (cat->next)
				return cat->next->name;
			else
				return NULL;
		}
		cat = cat->next;
	}
	return NULL;
}