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