Sun Mar 9 07:00:01 CET 2003
[asterisk/asterisk.git] / config.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Configuration File Parser
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <time.h>
20 #include <asterisk/config.h>
21 #include <asterisk/options.h>
22 #include <asterisk/logger.h>
23 #include "asterisk.h"
24 #include "astconf.h"
25
26 #define MAX_INCLUDE_LEVEL 10
27
28 struct ast_category {
29         char name[80];
30         struct ast_variable *root;
31         struct ast_category *next;
32         struct ast_comment *precomments;
33         struct ast_comment *sameline;
34 };
35
36 struct ast_config {
37         /* Maybe this structure isn't necessary but we'll keep it
38            for now */
39         struct ast_category *root;
40         struct ast_category *prev;
41         struct ast_comment *trailingcomments;
42 };
43
44 struct ast_comment_struct
45 {
46         struct ast_comment *root;
47         struct ast_comment *prev;
48 };
49
50 static char *strip(char *buf)
51 {
52         char *start;
53         /* Strip off trailing whitespace, returns, etc */
54         while(strlen(buf) && (buf[strlen(buf)-1]<33))
55                 buf[strlen(buf)-1] = '\0';
56         start = buf;
57         /* Strip off leading whitespace, returns, etc */
58         while(*start && (*start < 33))
59                 *start++ = '\0';
60         return start;
61 }
62
63 static void free_comments(struct ast_comment *com)
64 {
65         struct ast_comment *l;
66         while (com) {
67                 l = com;
68                 com = com->next;
69                 free(l);
70         }
71 }
72
73 void ast_destroy(struct ast_config *ast)
74 {
75         struct ast_category *cat, *catn;
76         struct ast_variable *v, *vn;
77
78         if (!ast)
79                 return;
80
81         cat = ast->root;
82         while(cat) {
83                 v = cat->root;
84                 while(v) {
85                         vn = v;
86                         free(v->name);
87                         free(v->value);
88                         free_comments(v->precomments);
89                         free_comments(v->sameline);
90                         v = v->next;
91                         free(vn);
92                 }
93                 catn = cat;
94                 free_comments(cat->precomments);
95                 free_comments(cat->sameline);
96                 cat = cat->next;
97                 free(catn);
98         }
99         free_comments(ast->trailingcomments);
100         free(ast);
101 }
102
103 int ast_true(char *s)
104 {
105         if (!s)
106                 return 0;
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") ||
112                 !strcasecmp(s, "1"))
113                         return -1;
114         return 0;
115 }
116
117 struct ast_variable *ast_variable_browse(struct ast_config *config, char *category)
118 {
119         struct ast_category *cat;
120         cat = config->root;
121         while(cat) {
122                 if (cat->name == category)
123                         return cat->root;
124                 cat = cat->next;
125         }
126         cat = config->root;
127         while(cat) {
128                 if (!strcasecmp(cat->name, category))
129                         return cat->root;
130                 cat = cat->next;
131         }
132         return NULL;
133 }
134
135 char *ast_variable_retrieve(struct ast_config *config, char *category, char *value)
136 {
137         struct ast_variable *v;
138         if (category) {
139                 v = ast_variable_browse(config, category);
140                 while (v) {
141                         if (value == v->name)
142                                 return v->value;
143                         v=v->next;
144                 }
145                 v = ast_variable_browse(config, category);
146                 while (v) {
147                         if (!strcasecmp(value, v->name))
148                                 return v->value;
149                         v=v->next;
150                 }
151         } else {
152                 struct ast_category *cat;
153                 cat = config->root;
154                 while(cat) {
155                         v = cat->root;
156                         while (v) {
157                                 if (!strcasecmp(value, v->name))
158                                         return v->value;
159                                 v=v->next;
160                         }
161                         cat = cat->next;
162                 }
163         }
164         return NULL;
165 }
166
167 int ast_variable_delete(struct ast_config *cfg, char *category, char *variable, char *value)
168 {
169         struct ast_variable *v, *pv, *bv, *bpv;
170         struct ast_category *cat;
171         cat = cfg->root;
172         while(cat) {
173                 if (cat->name == category) {
174                         break;
175                 }
176                 cat = cat->next;
177         }
178         if (!cat) {
179                 cat = cfg->root;
180                 while(cat) {
181                         if (!strcasecmp(cat->name, category)) {
182                                 break;
183                         }
184                         cat = cat->next;
185                 }
186         }
187         if (!cat)
188                 return -1;
189         v = cat->root;
190         pv = NULL;
191         while (v) {
192                 if ((variable == v->name) && (!value || !strcmp(v->value, value)))
193                         break;
194                 pv = v;
195                 v=v->next;
196         }
197         if (!v) {
198                 /* Get the last one that looks like it */
199                 bv = NULL;
200                 bpv = NULL;
201                 v = cat->root;
202                 pv = NULL;
203                 while (v) {
204                         if (!strcasecmp(variable, v->name) && (!value || !strcmp(v->value, value))) {
205                                 bv = v;
206                                 bpv = pv;
207                         }
208                         pv = v;
209                         v=v->next;
210                 }
211                 v = bv;
212         }
213
214         if (v) {
215                 /* Unlink from original position */
216                 if (pv) 
217                         pv->next = v->next;
218                 else
219                         cat->root = v->next;
220                 v->next = NULL;
221                 free(v->name);
222                 if (v->value)
223                         free(v->value);
224                 free_comments(v->sameline);
225                 free_comments(v->precomments);
226                 return 0;
227         }
228         return -1;
229 }
230
231 int ast_category_delete(struct ast_config *cfg, char *category)
232 {
233         struct ast_variable *v, *pv;
234         struct ast_category *cat, *cprev;
235         cat = cfg->root;
236         cprev = NULL;
237         while(cat) {
238                 if (cat->name == category) {
239                         break;
240                 }
241                 cprev = cat;
242                 cat = cat->next;
243         }
244         if (!cat) {
245                 cat = cfg->root;
246                 cprev = NULL;
247                 while(cat) {
248                         if (!strcasecmp(cat->name, category)) {
249                                 break;
250                         }
251                         cprev = cat;
252                         cat = cat->next;
253                 }
254         }
255         if (!cat)
256                 return -1;
257         /* Unlink it */
258         if (cprev)
259                 cprev->next = cat->next;
260         else
261                 cfg->root = cat->next;
262         v = cat->root;
263         while (v) {
264                 pv = v;
265                 v=v->next;
266                 if (pv->value)
267                         free(pv->value);
268                 if (pv->name)
269                         free(pv->name);
270                 free_comments(pv->sameline);
271                 free_comments(pv->precomments);
272                 free(pv);
273         }
274         free_comments(cat->sameline);
275         free_comments(cat->precomments);
276         free(cat);
277         return 0;
278 }
279
280 struct ast_variable *ast_variable_append_modify(struct ast_config *config, char *category, char *variable, char *value, int newcat, int newvar, int move)
281 {
282         struct ast_variable *v, *pv, *bv, *bpv;
283         struct ast_category *cat, *pcat;
284         cat = config->root;
285         if (!newcat) {
286                 while(cat) {
287                         if (cat->name == category) {
288                                 break;
289                         }
290                         cat = cat->next;
291                 }
292                 if (!cat) {
293                         cat = config->root;
294                         while(cat) {
295                                 if (!strcasecmp(cat->name, category)) {
296                                         break;
297                                 }
298                                 cat = cat->next;
299                         }
300                 }
301         }
302         if (!cat) {
303                 cat = malloc(sizeof(struct ast_category));
304                 if (!cat)
305                         return NULL;
306                 memset(cat, 0, sizeof(struct ast_category));
307                 strncpy(cat->name, category, sizeof(cat->name));
308                 if (config->root) {
309                         /* Put us at the end */
310                         pcat = config->root;
311                         while(pcat->next)
312                                 pcat = pcat->next;
313                         pcat->next = cat;
314                 } else {
315                         /* We're the first one */
316                         config->root = cat;
317                 }
318                         
319         }
320         if (!newvar) {
321                 v = cat->root;
322                 pv = NULL;
323                 while (v) {
324                         if (variable == v->name)
325                                 break;
326                         pv = v;
327                         v=v->next;
328                 }
329                 if (!v) {
330                         /* Get the last one that looks like it */
331                         bv = NULL;
332                         bpv = NULL;
333                         v = cat->root;
334                         pv = NULL;
335                         while (v) {
336                                 if (!strcasecmp(variable, v->name)) {
337                                         bv = v;
338                                         bpv = pv;
339                                 }
340                                 pv = v;
341                                 v=v->next;
342                         }
343                         v = bv;
344                 }
345         } else v = NULL;
346         if (v && move) {
347                 /* Unlink from original position */
348                 if (pv) 
349                         pv->next = v->next;
350                 else
351                         cat->root = v->next;
352                 v->next = NULL;
353         }
354         if (!v) {
355                 v = malloc(sizeof(struct ast_variable));
356                 if (!v)
357                         return NULL;
358                 memset(v, 0, sizeof(struct ast_variable));
359                 v->name = strdup(variable);
360                 move = 1;
361         }
362         if (v->value)
363                 free(v->value);
364         if (value)
365                 v->value = strdup(value);
366         else
367                 v->value = strdup("");
368         if (move) {
369                 if (cat->root) {
370                         pv = cat->root;
371                         while (pv->next) 
372                                 pv = pv->next;
373                         pv->next = v;
374                 } else {
375                         cat->root = v;
376                 }
377         }
378         return v;
379 }
380
381 int ast_category_exist(struct ast_config *config, char *category_name)
382 {
383         struct ast_category *category = NULL;
384
385         category = config->root;
386
387         while(category) {
388                 if (!strcasecmp(category->name,category_name)) 
389                         return 1;
390                 category = category->next;
391         } 
392
393         return 0;
394 }
395
396 static struct ast_comment *build_comment(char *cmt)
397 {
398         struct ast_comment *c;
399         c = malloc(sizeof(struct ast_comment));
400         if (c) {
401                 memset(c, 0, sizeof(struct ast_comment));
402                 c->comment = strdup(cmt);
403         }
404         return c;
405 }
406
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)
409 {
410         char *c;
411         char *cur;
412         struct ast_variable *v;
413         struct ast_comment *com = NULL;
414         int object;
415         /* Strip off lines using ; as comment */
416         c = strchr(buf, ';');
417         if (c) {
418                 *c = '\0';
419                 c++;
420                 if (*c != '!')
421                         com = build_comment(c);
422         }
423         cur = strip(buf);
424         if (strlen(cur)) {
425                 /* Actually parse the entry */
426                 if (cur[0] == '[') {
427                         /* A category header */
428                         c = strchr(cur, ']');
429                         if (c) {
430                                 *c = 0;
431                                 *_tmpc = malloc(sizeof(struct ast_category));
432                                 if (!*_tmpc) {
433                                         ast_destroy(tmp);
434                                         ast_log(LOG_WARNING,
435                                                 "Out of memory, line %d\n", lineno);
436                                         return -1;
437                                 }
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;
443                                 if (!tmp->prev)
444                                         tmp->root = *_tmpc;
445                                 else
446                                         tmp->prev->next = *_tmpc;
447
448                                 tmp->prev = *_tmpc;
449                                 acs->root = NULL;
450                                 acs->prev = NULL;
451                                 *_last =  NULL;
452                         } else {
453                                 ast_log(LOG_WARNING, 
454                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
455                         }
456                 } else if (cur[0] == '#') {
457                         /* A directive */
458                         cur++;
459                         c = cur;
460                         while(*c && (*c > 32)) c++;
461                         if (*c) {
462                                 *c = '\0';
463                                 c++;
464                                 /* Find real argument */
465                                 while(*c  && (*c < 33)) c++;
466                                 if (!*c)
467                                         c = NULL;
468                         } else 
469                                 c = NULL;
470                         if (!strcasecmp(cur, "include")) {
471                                 /* A #include */
472                                 if (c) {
473                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
474                                         /* Get rid of leading mess */
475                                         cur = c;
476                                         while(strlen(cur)) {
477                                                 c = cur + strlen(cur) - 1;
478                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
479                                                         *c = '\0';
480                                                 else
481                                                         break;
482                                         }
483                                         if (includelevel < MAX_INCLUDE_LEVEL) {
484                                                 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1, acs);
485                                         } else 
486                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
487                                 } else
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 */
490                         } else 
491                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
492                 } else {
493                         /* Just a line (variable = value) */
494                         if (!*_tmpc) {
495                                 ast_log(LOG_WARNING,
496                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
497                                 ast_destroy(tmp);
498                                 return -1;
499                         }
500                         c = strchr(cur, '=');
501                         if (c) {
502                                 *c = 0;
503                                 c++;
504                                 /* Ignore > in => */
505                                 if (*c== '>') {
506                                         object = 1;
507                                         c++;
508                                 } else
509                                         object = 0;
510                                 v = malloc(sizeof(struct ast_variable));
511                                 if (v) {
512                                         memset(v, 0, sizeof(struct ast_variable));
513                                         v->next = NULL;
514                                         v->name = strdup(strip(cur));
515                                         v->value = strdup(strip(c));
516                                         v->lineno = lineno;
517                                         v->object = object;
518                                         /* Put and reset comments */
519                                         v->precomments = acs->root;
520                                         v->blanklines = 0;
521                                         acs->prev = NULL;
522                                         acs->root = NULL;
523                                         v->sameline = com;
524                                         if (*_last)
525                                                 (*_last)->next = v;
526                                         else
527                                                 (*_tmpc)->root = v;
528                                         *_last = v;
529                                 } else {
530                                         ast_destroy(tmp);
531                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
532                                         return -1;
533                                 }
534                         } else {
535                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
536                         }
537                                                                                                                 
538                 }
539         } else {
540                 /* store any comments if there are any */
541                 if (com) {
542                         if (acs->prev)
543                                 acs->prev->next = com;
544                         else
545                                 acs->root = com;
546                         acs->prev = com;
547                 } else {
548                 if (*_last) 
549                         (*_last)->blanklines++;
550
551                 }
552         }
553         return 0;
554 }
555
556 static void dump_comments(FILE *f, struct ast_comment *comment)
557 {
558         while (comment) {
559                 fprintf(f, ";%s", comment->comment);
560                 comment = comment->next;
561         }
562 }
563
564 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
565 {
566         FILE *f;
567         char fn[256];
568         char date[256];
569         time_t t;
570         struct ast_variable *var;
571         struct ast_category *cat;
572         int blanklines = 0;
573         if (configfile[0] == '/') {
574                 strncpy(fn, configfile, sizeof(fn)-1);
575         } else {
576                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
577         }
578         time(&t);
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);
583                 fprintf(f, ";!\n");
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);
588                 fprintf(f, ";!\n");
589                 cat = cfg->root;
590                 while(cat) {
591                         /* Dump any precomments */
592                         dump_comments(f, cat->precomments);
593                         /* Dump section with any appropriate comment */
594                         if (cat->sameline) 
595                                 fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->comment);
596                         else
597                                 fprintf(f, "[%s]\n", cat->name);
598                         var = cat->root;
599                         while(var) {
600                                 dump_comments(f, var->precomments);
601                                 if (var->sameline) 
602                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->comment);
603                                 else    
604                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
605                                 if (var->blanklines) {
606                                         blanklines = var->blanklines;
607                                         while (blanklines) {
608                                                 fprintf(f, "\n");
609                                                 blanklines--;
610                                         }
611                                 }
612                                         
613                                 var = var->next;
614                         }
615 #if 0
616                         /* Put an empty line */
617                         fprintf(f, "\n");
618 #endif
619                         cat = cat->next;
620                 }
621                 dump_comments(f, cfg->trailingcomments);
622         } else {
623                 if (option_debug)
624                         printf("Unable to open for writing: %s\n", fn);
625                 else if (option_verbose > 1)
626                         printf( "Unable to write (%s)", strerror(errno));
627                 return -1;
628         }
629         fclose(f);
630         return 0;
631 }
632
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)
634 {
635         char fn[256];
636         char buf[256];
637         FILE *f;
638         int lineno=0;
639         int master=0;
640
641         if (configfile[0] == '/') {
642                 strncpy(fn, configfile, sizeof(fn)-1);
643         } else {
644                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
645         }
646         if ((option_verbose > 1) && !option_debug) {
647                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
648                 fflush(stdout);
649         }
650         if ((f = fopen(fn, "r"))) {
651                 if (option_debug)
652                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
653                 else if (option_verbose > 1)
654                         ast_verbose( "Found\n");
655                 if (!tmp) {
656                         tmp = malloc(sizeof(struct ast_config));
657                         if (tmp)
658                                 memset(tmp, 0, sizeof(struct ast_config));
659
660                         master = 1;
661                 }
662                 if (!tmp) {
663                         ast_log(LOG_WARNING, "Out of memory\n");
664                         fclose(f);
665                         return NULL;
666                 }
667                 while(!feof(f)) {
668                         fgets(buf, sizeof(buf), f);
669                         lineno++;
670                         if (!feof(f)) {
671                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel, acs)) {
672                                         fclose(f);
673                                         return NULL;
674                                 }
675                         }
676                 }
677                 fclose(f);              
678         } else {
679                 if (option_debug)
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));
683         }
684         if (master) {
685                 /* Keep trailing comments */
686                 tmp->trailingcomments = acs->root;
687                 acs->root = NULL;
688                 acs->prev = NULL;
689         }
690         return tmp;
691 }
692
693 struct ast_config *ast_load(char *configfile)
694 {
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);
699 }
700
701 char *ast_category_browse(struct ast_config *config, char *prev)
702 {       
703         struct ast_category *cat;
704         if (!prev) {
705                 if (config->root)
706                         return config->root->name;
707                 else
708                         return NULL;
709         }
710         cat = config->root;
711         while(cat) {
712                 if (cat->name == prev) {
713                         if (cat->next)
714                                 return cat->next->name;
715                         else
716                                 return NULL;
717                 }
718                 cat = cat->next;
719         }
720         cat = config->root;
721         while(cat) {
722                 if (!strcasecmp(cat->name, prev)) {
723                         if (cat->next)
724                                 return cat->next->name;
725                         else
726                                 return NULL;
727                 }
728                 cat = cat->next;
729         }
730         return NULL;
731 }