2 * Asterisk -- A telephony toolkit for Linux.
4 * Configuration File Parser
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
20 #include <asterisk/config.h>
21 #include <asterisk/options.h>
22 #include <asterisk/logger.h>
26 #define MAX_INCLUDE_LEVEL 10
30 struct ast_variable *root;
31 struct ast_category *next;
32 struct ast_comment *precomments;
33 struct ast_comment *sameline;
37 /* Maybe this structure isn't necessary but we'll keep it
39 struct ast_category *root;
40 struct ast_category *prev;
41 struct ast_comment *trailingcomments;
44 struct ast_comment_struct
46 struct ast_comment *root;
47 struct ast_comment *prev;
50 static char *strip(char *buf)
53 /* Strip off trailing whitespace, returns, etc */
54 while(strlen(buf) && (buf[strlen(buf)-1]<33))
55 buf[strlen(buf)-1] = '\0';
57 /* Strip off leading whitespace, returns, etc */
58 while(*start && (*start < 33))
63 static void free_comments(struct ast_comment *com)
65 struct ast_comment *l;
73 void ast_destroy(struct ast_config *ast)
75 struct ast_category *cat, *catn;
76 struct ast_variable *v, *vn;
88 free_comments(v->precomments);
89 free_comments(v->sameline);
94 free_comments(cat->precomments);
95 free_comments(cat->sameline);
99 free_comments(ast->trailingcomments);
103 int ast_true(char *s)
107 /* Determine if this is a true value */
108 if (!strcasecmp(s, "yes") ||
109 !strcasecmp(s, "true") ||
110 !strcasecmp(s, "y") ||
111 !strcasecmp(s, "t") ||
117 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
119 struct ast_category *cat;
122 if (cat->name == category)
128 if (!strcasecmp(cat->name, category))
135 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
137 struct ast_variable *v;
139 v = ast_variable_browse(config, category);
141 if (value == v->name)
145 v = ast_variable_browse(config, category);
147 if (!strcasecmp(value, v->name))
152 struct ast_category *cat;
157 if (!strcasecmp(value, v->name))
167 int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
169 struct ast_variable *v, *pv, *bv, *bpv;
170 struct ast_category *cat;
173 if (cat->name == category) {
181 if (!strcasecmp(cat->name, category)) {
192 if ((variable == v->name) && (!value || !strcmp(v->value, value)))
198 /* Get the last one that looks like it */
204 if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
215 /* Unlink from original position */
224 free_comments(v->sameline);
225 free_comments(v->precomments);
231 int ast_category_delete(struct ast_config *cfg, char *category)
233 struct ast_variable *v, *pv;
234 struct ast_category *cat, *cprev;
238 if (cat->name == category) {
248 if (!strcasecmp(cat->name, category)) {
259 cprev->next = cat->next;
261 cfg->root = cat->next;
270 free_comments(pv->sameline);
271 free_comments(pv->precomments);
274 free_comments(cat->sameline);
275 free_comments(cat->precomments);
280 struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
282 struct ast_variable *v, *pv, *bv, *bpv;
283 struct ast_category *cat, *pcat;
287 if (cat->name == category) {
295 if (!strcasecmp(cat->name, category)) {
303 cat = malloc(sizeof(struct ast_category));
306 memset(cat, 0, sizeof(struct ast_category));
307 strncpy(cat->name, category, sizeof(cat->name));
309 /* Put us at the end */
315 /* We're the first one */
324 if (variable == v->name)
330 /* Get the last one that looks like it */
336 if (!strcasecmp(variable, v->name)) {
347 /* Unlink from original position */
355 v = malloc(sizeof(struct ast_variable));
358 memset(v, 0, sizeof(struct ast_variable));
359 v->name = strdup(variable);
365 v->value = strdup(value);
367 v->value = strdup("");
381 int ast_category_exist(struct ast_config *config, char *category_name)
383 struct ast_category *category = NULL;
385 category = config->root;
388 if (!strcasecmp(category->name,category_name))
390 category = category->next;
396 static struct ast_comment *build_comment(char *cmt)
398 struct ast_comment *c;
399 c = malloc(sizeof(struct ast_comment));
401 memset(c, 0, sizeof(struct ast_comment));
402 c->comment = strdup(cmt);
407 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);
408 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)
412 struct ast_variable *v;
413 struct ast_comment *com = NULL;
415 /* Strip off lines using ; as comment */
416 c = strchr(buf, ';');
421 com = build_comment(c);
425 /* Actually parse the entry */
427 /* A category header */
428 c = strchr(cur, ']');
431 *_tmpc = malloc(sizeof(struct ast_category));
435 "Out of memory, line %d\n", lineno);
438 memset(*_tmpc, 0, sizeof(struct ast_category));
439 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
440 (*_tmpc)->root = NULL;
441 (*_tmpc)->precomments = acs->root;
442 (*_tmpc)->sameline = com;
446 tmp->prev->next = *_tmpc;
454 "parse error: no closing ']', line %d of %s\n", lineno, configfile);
456 } else if (cur[0] == '#') {
460 while(*c && (*c > 32)) c++;
464 /* Find real argument */
465 while(*c && (*c < 33)) c++;
470 if (!strcasecmp(cur, "include")) {
473 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
474 /* Get rid of leading mess */
477 c = cur + strlen(cur) - 1;
478 if ((*c == '>') || (*c == '<') || (*c == '\"'))
483 if (includelevel < MAX_INCLUDE_LEVEL) {
484 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1, acs);
486 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
488 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
489 /* Strip off leading and trailing "'s and <>'s */
491 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
493 /* Just a line (variable = value) */
496 "parse error: No category context for line %d of %s\n", lineno, configfile);
500 c = strchr(cur, '=');
510 v = malloc(sizeof(struct ast_variable));
512 memset(v, 0, sizeof(struct ast_variable));
514 v->name = strdup(strip(cur));
515 v->value = strdup(strip(c));
518 /* Put and reset comments */
519 v->precomments = acs->root;
531 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
535 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
540 /* store any comments if there are any */
543 acs->prev->next = com;
549 (*_last)->blanklines++;
556 static void dump_comments(FILE *f, struct ast_comment *comment)
559 fprintf(f, ";%s", comment->comment);
560 comment = comment->next;
564 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
570 struct ast_variable *var;
571 struct ast_category *cat;
573 if (configfile[0] == '/') {
574 strncpy(fn, configfile, sizeof(fn)-1);
576 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
579 strncpy(date, ctime(&t), sizeof(date));
580 if ((f = fopen(fn, "w"))) {
581 if ((option_verbose > 1) && !option_debug)
582 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
584 fprintf(f, ";! Automatically generated configuration file\n");
585 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
586 fprintf(f, ";! Generator: %s\n", generator);
587 fprintf(f, ";! Creation Date: %s", date);
591 /* Dump any precomments */
592 dump_comments(f, cat->precomments);
593 /* Dump section with any appropriate comment */
595 fprintf(f, "[%s] ; %s\n", cat->name, cat->sameline->comment);
597 fprintf(f, "[%s]\n", cat->name);
600 dump_comments(f, var->precomments);
602 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->comment);
604 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
605 if (var->blanklines) {
606 blanklines = var->blanklines;
616 /* Put an empty line */
621 dump_comments(f, cfg->trailingcomments);
624 printf("Unable to open for writing: %s\n", fn);
625 else if (option_verbose > 1)
626 printf( "Unable to write (%s)", strerror(errno));
633 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)
641 if (configfile[0] == '/') {
642 strncpy(fn, configfile, sizeof(fn)-1);
644 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
646 if ((option_verbose > 1) && !option_debug) {
647 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
650 if ((f = fopen(fn, "r"))) {
652 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
653 else if (option_verbose > 1)
654 ast_verbose( "Found\n");
656 tmp = malloc(sizeof(struct ast_config));
658 memset(tmp, 0, sizeof(struct ast_config));
663 ast_log(LOG_WARNING, "Out of memory\n");
668 fgets(buf, sizeof(buf), f);
671 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel, acs)) {
680 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
681 else if (option_verbose > 1)
682 ast_verbose( "Not found (%s)", strerror(errno));
685 /* Keep trailing comments */
686 tmp->trailingcomments = acs->root;
693 struct ast_config *ast_load(char *configfile)
695 struct ast_category *tmpc=NULL;
696 struct ast_variable *last = NULL;
697 struct ast_comment_struct acs = { NULL, NULL };
698 return __ast_load(configfile, NULL, &tmpc, &last, 0, &acs);
701 char *ast_category_browse(struct ast_config *config, char *prev)
703 struct ast_category *cat;
706 return config->root->name;
712 if (cat->name == prev) {
714 return cat->next->name;
722 if (!strcasecmp(cat->name, prev)) {
724 return cat->next->name;