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 #ifdef PRESERVE_COMMENTS
33 struct ast_comment *precomments;
34 struct ast_comment *sameline;
39 /* Maybe this structure isn't necessary but we'll keep it
41 struct ast_category *root;
42 struct ast_category *prev;
43 #ifdef PRESERVE_COMMENTS
44 struct ast_comment *trailingcomments;
48 #ifdef PRESERVE_COMMENTS
49 struct ast_comment_struct
51 struct ast_comment *root;
52 struct ast_comment *prev;
56 static char *strip(char *buf)
59 /* Strip off trailing whitespace, returns, etc */
60 while(strlen(buf) && (buf[strlen(buf)-1]<33))
61 buf[strlen(buf)-1] = '\0';
63 /* Strip off leading whitespace, returns, etc */
64 while(*start && (*start < 33))
69 #ifdef PRESERVE_COMMENTS
70 static void free_comments(struct ast_comment *com)
72 struct ast_comment *l;
81 void ast_destroy(struct ast_config *ast)
83 struct ast_category *cat, *catn;
84 struct ast_variable *v, *vn;
96 #ifdef PRESERVE_COMMENTS
97 free_comments(v->precomments);
98 free_comments(v->sameline);
104 #ifdef PRESERVE_COMMENTS
105 free_comments(cat->precomments);
106 free_comments(cat->sameline);
111 #ifdef PRESERVE_COMMENTS
112 free_comments(ast->trailingcomments);
117 int ast_true(char *s)
121 /* Determine if this is a true value */
122 if (!strcasecmp(s, "yes") ||
123 !strcasecmp(s, "true") ||
124 !strcasecmp(s, "y") ||
125 !strcasecmp(s, "t") ||
131 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
133 struct ast_category *cat;
136 if (cat->name == category)
142 if (!strcasecmp(cat->name, category))
149 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
151 struct ast_variable *v;
153 v = ast_variable_browse(config, category);
155 if (value == v->name)
159 v = ast_variable_browse(config, category);
161 if (!strcasecmp(value, v->name))
166 struct ast_category *cat;
171 if (!strcasecmp(value, v->name))
181 #ifdef PRESERVE_COMMENTS
182 int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
184 struct ast_variable *v, *pv, *bv, *bpv;
185 struct ast_category *cat;
188 if (cat->name == category) {
196 if (!strcasecmp(cat->name, category)) {
207 if ((variable == v->name) && (!value || !strcmp(v->value, value)))
213 /* Get the last one that looks like it */
219 if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
230 /* Unlink from original position */
239 free_comments(v->sameline);
240 free_comments(v->precomments);
246 int ast_category_delete(struct ast_config *cfg, char *category)
248 struct ast_variable *v, *pv;
249 struct ast_category *cat, *cprev;
253 if (cat->name == category) {
263 if (!strcasecmp(cat->name, category)) {
274 cprev->next = cat->next;
276 cfg->root = cat->next;
285 free_comments(pv->sameline);
286 free_comments(pv->precomments);
289 free_comments(cat->sameline);
290 free_comments(cat->precomments);
295 struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
297 struct ast_variable *v, *pv=NULL, *bv, *bpv;
298 struct ast_category *cat, *pcat;
302 if (cat->name == category) {
310 if (!strcasecmp(cat->name, category)) {
318 cat = malloc(sizeof(struct ast_category));
321 memset(cat, 0, sizeof(struct ast_category));
322 strncpy(cat->name, category, sizeof(cat->name));
324 /* Put us at the end */
330 /* We're the first one */
339 if (variable == v->name)
345 /* Get the last one that looks like it */
351 if (!strcasecmp(variable, v->name)) {
362 /* Unlink from original position */
370 v = malloc(sizeof(struct ast_variable));
373 memset(v, 0, sizeof(struct ast_variable));
374 v->name = strdup(variable);
380 v->value = strdup(value);
382 v->value = strdup("");
397 int ast_category_exist(struct ast_config *config, char *category_name)
399 struct ast_category *category = NULL;
401 category = config->root;
404 if (!strcasecmp(category->name,category_name))
406 category = category->next;
412 #ifdef PRESERVE_COMMENTS
413 static struct ast_comment *build_comment(char *cmt)
415 struct ast_comment *c;
416 int len = strlen(cmt) + 1;
417 c = malloc(sizeof(struct ast_comment) + len);
419 /* Memset the header */
420 memset(c, 0, sizeof(struct ast_comment));
428 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
429 #ifdef PRESERVE_COMMENTS
430 , struct ast_comment_struct *acs
434 static int cfg_process(struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, char *buf, int lineno, char *configfile, int includelevel
435 #ifdef PRESERVE_COMMENTS
436 ,struct ast_comment_struct *acs
442 struct ast_variable *v;
443 #ifdef PRESERVE_COMMENTS
444 struct ast_comment *com = NULL;
447 /* Strip off lines using ; as comment */
448 c = strchr(buf, ';');
451 #ifdef PRESERVE_COMMENTS
454 com = build_comment(c);
459 /* Actually parse the entry */
461 /* A category header */
462 c = strchr(cur, ']');
465 *_tmpc = malloc(sizeof(struct ast_category));
469 "Out of memory, line %d\n", lineno);
472 memset(*_tmpc, 0, sizeof(struct ast_category));
473 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
474 (*_tmpc)->root = NULL;
475 #ifdef PRESERVE_COMMENTS
476 (*_tmpc)->precomments = acs->root;
477 (*_tmpc)->sameline = com;
482 tmp->prev->next = *_tmpc;
485 #ifdef PRESERVE_COMMENTS
492 "parse error: no closing ']', line %d of %s\n", lineno, configfile);
494 } else if (cur[0] == '#') {
498 while(*c && (*c > 32)) c++;
502 /* Find real argument */
503 while(*c && (*c < 33)) c++;
508 if (!strcasecmp(cur, "include")) {
511 while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
512 /* Get rid of leading mess */
515 c = cur + strlen(cur) - 1;
516 if ((*c == '>') || (*c == '<') || (*c == '\"'))
521 if (includelevel < MAX_INCLUDE_LEVEL) {
522 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1
523 #ifdef PRESERVE_COMMENTS
528 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
530 ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
531 /* Strip off leading and trailing "'s and <>'s */
533 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
535 /* Just a line (variable = value) */
538 "parse error: No category context for line %d of %s\n", lineno, configfile);
542 c = strchr(cur, '=');
552 v = malloc(sizeof(struct ast_variable));
554 memset(v, 0, sizeof(struct ast_variable));
556 v->name = strdup(strip(cur));
557 v->value = strdup(strip(c));
560 /* Put and reset comments */
561 #ifdef PRESERVE_COMMENTS
562 v->precomments = acs->root;
575 ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
579 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
584 /* store any comments if there are any */
585 #ifdef PRESERVE_COMMENTS
588 acs->prev->next = com;
594 (*_last)->blanklines++;
602 #ifdef PRESERVE_COMMENTS
603 static void dump_comments(FILE *f, struct ast_comment *comment)
606 fprintf(f, ";%s", comment->cmt);
607 comment = comment->next;
612 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
618 struct ast_variable *var;
619 struct ast_category *cat;
621 if (configfile[0] == '/') {
622 strncpy(fn, configfile, sizeof(fn)-1);
624 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
627 strncpy(date, ctime(&t), sizeof(date));
628 if ((f = fopen(fn, "w"))) {
629 if ((option_verbose > 1) && !option_debug)
630 ast_verbose( VERBOSE_PREFIX_2 "Saving '%s': ", fn);
632 fprintf(f, ";! Automatically generated configuration file\n");
633 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
634 fprintf(f, ";! Generator: %s\n", generator);
635 fprintf(f, ";! Creation Date: %s", date);
639 #ifdef PRESERVE_COMMENTS
640 /* Dump any precomments */
641 dump_comments(f, cat->precomments);
643 /* Dump section with any appropriate comment */
644 #ifdef PRESERVE_COMMENTS
646 fprintf(f, "[%s] ; %s\n", cat->name, cat->sameline->cmt);
649 fprintf(f, "[%s]\n", cat->name);
652 #ifdef PRESERVE_COMMENTS
653 dump_comments(f, var->precomments);
656 fprintf(f, "%s %s %s ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
658 fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
659 if (var->blanklines) {
660 blanklines = var->blanklines;
670 /* Put an empty line */
675 #ifdef PRESERVE_COMMENTS
676 dump_comments(f, cfg->trailingcomments);
680 printf("Unable to open for writing: %s\n", fn);
681 else if (option_verbose > 1)
682 printf( "Unable to write (%s)", strerror(errno));
689 static struct ast_config *__ast_load(char *configfile, struct ast_config *tmp, struct ast_category **_tmpc, struct ast_variable **_last, int includelevel
690 #ifdef PRESERVE_COMMENTS
691 , struct ast_comment_struct *acs
701 if (configfile[0] == '/') {
702 strncpy(fn, configfile, sizeof(fn)-1);
704 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
706 if ((option_verbose > 1) && !option_debug) {
707 ast_verbose( VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
710 if ((f = fopen(fn, "r"))) {
712 ast_log(LOG_DEBUG, "Parsing %s\n", fn);
713 else if (option_verbose > 1)
714 ast_verbose( "Found\n");
716 tmp = malloc(sizeof(struct ast_config));
718 memset(tmp, 0, sizeof(struct ast_config));
723 ast_log(LOG_WARNING, "Out of memory\n");
728 fgets(buf, sizeof(buf), f);
731 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel
732 #ifdef PRESERVE_COMMENTS
744 ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
745 else if (option_verbose > 1)
746 ast_verbose( "Not found (%s)", strerror(errno));
748 #ifdef PRESERVE_COMMENTS
750 /* Keep trailing comments */
751 tmp->trailingcomments = acs->root;
759 struct ast_config *ast_load(char *configfile)
761 struct ast_category *tmpc=NULL;
762 struct ast_variable *last = NULL;
763 #ifdef PRESERVE_COMMENTS
764 struct ast_comment_struct acs = { NULL, NULL };
766 return __ast_load(configfile, NULL, &tmpc, &last, 0
767 #ifdef PRESERVE_COMMENTS
773 char *ast_category_browse(struct ast_config *config, char *prev)
775 struct ast_category *cat;
778 return config->root->name;
784 if (cat->name == prev) {
786 return cat->next->name;
794 if (!strcasecmp(cat->name, prev)) {
796 return cat->next->name;