Allocate comments
[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=NULL, *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         int len = strlen(cmt) + 1;
400         c = malloc(sizeof(struct ast_comment) + len);
401         if (c) {
402                 /* Memset the header */
403                 memset(c, 0, sizeof(struct ast_comment));
404                 /* Copy the rest */
405                 strcpy(c->cmt, cmt);
406         }
407         return c;
408 }
409
410 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);
411 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 {
413         char *c;
414         char *cur;
415         struct ast_variable *v;
416         struct ast_comment *com = NULL;
417         int object;
418         /* Strip off lines using ; as comment */
419         c = strchr(buf, ';');
420         if (c) {
421                 *c = '\0';
422                 c++;
423                 if (*c != '!')
424                         com = build_comment(c);
425         }
426         cur = strip(buf);
427         if (strlen(cur)) {
428                 /* Actually parse the entry */
429                 if (cur[0] == '[') {
430                         /* A category header */
431                         c = strchr(cur, ']');
432                         if (c) {
433                                 *c = 0;
434                                 *_tmpc = malloc(sizeof(struct ast_category));
435                                 if (!*_tmpc) {
436                                         ast_destroy(tmp);
437                                         ast_log(LOG_WARNING,
438                                                 "Out of memory, line %d\n", lineno);
439                                         return -1;
440                                 }
441                                 memset(*_tmpc, 0, sizeof(struct ast_category));
442                                 strncpy((*_tmpc)->name, cur+1, sizeof((*_tmpc)->name) - 1);
443                                 (*_tmpc)->root =  NULL;
444                                 (*_tmpc)->precomments = acs->root;
445                                 (*_tmpc)->sameline = com;
446                                 if (!tmp->prev)
447                                         tmp->root = *_tmpc;
448                                 else
449                                         tmp->prev->next = *_tmpc;
450
451                                 tmp->prev = *_tmpc;
452                                 acs->root = NULL;
453                                 acs->prev = NULL;
454                                 *_last =  NULL;
455                         } else {
456                                 ast_log(LOG_WARNING, 
457                                         "parse error: no closing ']', line %d of %s\n", lineno, configfile);
458                         }
459                 } else if (cur[0] == '#') {
460                         /* A directive */
461                         cur++;
462                         c = cur;
463                         while(*c && (*c > 32)) c++;
464                         if (*c) {
465                                 *c = '\0';
466                                 c++;
467                                 /* Find real argument */
468                                 while(*c  && (*c < 33)) c++;
469                                 if (!*c)
470                                         c = NULL;
471                         } else 
472                                 c = NULL;
473                         if (!strcasecmp(cur, "include")) {
474                                 /* A #include */
475                                 if (c) {
476                                         while((*c == '<') || (*c == '>') || (*c == '\"')) c++;
477                                         /* Get rid of leading mess */
478                                         cur = c;
479                                         while(strlen(cur)) {
480                                                 c = cur + strlen(cur) - 1;
481                                                 if ((*c == '>') || (*c == '<') || (*c == '\"'))
482                                                         *c = '\0';
483                                                 else
484                                                         break;
485                                         }
486                                         if (includelevel < MAX_INCLUDE_LEVEL) {
487                                                 __ast_load(cur, tmp, _tmpc, _last, includelevel + 1, acs);
488                                         } else 
489                                                 ast_log(LOG_WARNING, "Maximum Include level (%d) exceeded\n", includelevel);
490                                 } else
491                                         ast_log(LOG_WARNING, "Directive '#include' needs an argument (filename) at line %d of %s\n", lineno, configfile);
492                                 /* Strip off leading and trailing "'s and <>'s */
493                         } else 
494                                 ast_log(LOG_WARNING, "Unknown directive '%s' at line %d of %s\n", cur, lineno, configfile);
495                 } else {
496                         /* Just a line (variable = value) */
497                         if (!*_tmpc) {
498                                 ast_log(LOG_WARNING,
499                                         "parse error: No category context for line %d of %s\n", lineno, configfile);
500                                 ast_destroy(tmp);
501                                 return -1;
502                         }
503                         c = strchr(cur, '=');
504                         if (c) {
505                                 *c = 0;
506                                 c++;
507                                 /* Ignore > in => */
508                                 if (*c== '>') {
509                                         object = 1;
510                                         c++;
511                                 } else
512                                         object = 0;
513                                 v = malloc(sizeof(struct ast_variable));
514                                 if (v) {
515                                         memset(v, 0, sizeof(struct ast_variable));
516                                         v->next = NULL;
517                                         v->name = strdup(strip(cur));
518                                         v->value = strdup(strip(c));
519                                         v->lineno = lineno;
520                                         v->object = object;
521                                         /* Put and reset comments */
522                                         v->precomments = acs->root;
523                                         v->blanklines = 0;
524                                         acs->prev = NULL;
525                                         acs->root = NULL;
526                                         v->sameline = com;
527                                         if (*_last)
528                                                 (*_last)->next = v;
529                                         else
530                                                 (*_tmpc)->root = v;
531                                         *_last = v;
532                                 } else {
533                                         ast_destroy(tmp);
534                                         ast_log(LOG_WARNING, "Out of memory, line %d\n", lineno);
535                                         return -1;
536                                 }
537                         } else {
538                                 ast_log(LOG_WARNING, "No '=' (equal sign) in line %d of %s\n", lineno, configfile);
539                         }
540                                                                                                                 
541                 }
542         } else {
543                 /* store any comments if there are any */
544                 if (com) {
545                         if (acs->prev)
546                                 acs->prev->next = com;
547                         else
548                                 acs->root = com;
549                         acs->prev = com;
550                 } else {
551                 if (*_last) 
552                         (*_last)->blanklines++;
553
554                 }
555         }
556         return 0;
557 }
558
559 static void dump_comments(FILE *f, struct ast_comment *comment)
560 {
561         while (comment) {
562                 fprintf(f, ";%s", comment->cmt);
563                 comment = comment->next;
564         }
565 }
566
567 int ast_save(char *configfile, struct ast_config *cfg, char *generator)
568 {
569         FILE *f;
570         char fn[256];
571         char date[256];
572         time_t t;
573         struct ast_variable *var;
574         struct ast_category *cat;
575         int blanklines = 0;
576         if (configfile[0] == '/') {
577                 strncpy(fn, configfile, sizeof(fn)-1);
578         } else {
579                 snprintf(fn, sizeof(fn), "%s/%s", AST_CONFIG_DIR, configfile);
580         }
581         time(&t);
582         strncpy(date, ctime(&t), sizeof(date));
583         if ((f = fopen(fn, "w"))) {
584                 if ((option_verbose > 1) && !option_debug)
585                         ast_verbose(  VERBOSE_PREFIX_2 "Saving '%s': ", fn);
586                 fprintf(f, ";!\n");
587                 fprintf(f, ";! Automatically generated configuration file\n");
588                 fprintf(f, ";! Filename: %s (%s)\n", configfile, fn);
589                 fprintf(f, ";! Generator: %s\n", generator);
590                 fprintf(f, ";! Creation Date: %s", date);
591                 fprintf(f, ";!\n");
592                 cat = cfg->root;
593                 while(cat) {
594                         /* Dump any precomments */
595                         dump_comments(f, cat->precomments);
596                         /* Dump section with any appropriate comment */
597                         if (cat->sameline) 
598                                 fprintf(f, "[%s]  ; %s\n", cat->name, cat->sameline->cmt);
599                         else
600                                 fprintf(f, "[%s]\n", cat->name);
601                         var = cat->root;
602                         while(var) {
603                                 dump_comments(f, var->precomments);
604                                 if (var->sameline) 
605                                         fprintf(f, "%s %s %s  ; %s\n", var->name, (var->object ? "=>" : "="), var->value, var->sameline->cmt);
606                                 else    
607                                         fprintf(f, "%s %s %s\n", var->name, (var->object ? "=>" : "="), var->value);
608                                 if (var->blanklines) {
609                                         blanklines = var->blanklines;
610                                         while (blanklines) {
611                                                 fprintf(f, "\n");
612                                                 blanklines--;
613                                         }
614                                 }
615                                         
616                                 var = var->next;
617                         }
618 #if 0
619                         /* Put an empty line */
620                         fprintf(f, "\n");
621 #endif
622                         cat = cat->next;
623                 }
624                 dump_comments(f, cfg->trailingcomments);
625         } else {
626                 if (option_debug)
627                         printf("Unable to open for writing: %s\n", fn);
628                 else if (option_verbose > 1)
629                         printf( "Unable to write (%s)", strerror(errno));
630                 return -1;
631         }
632         fclose(f);
633         return 0;
634 }
635
636 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)
637 {
638         char fn[256];
639         char buf[256];
640         FILE *f;
641         int lineno=0;
642         int master=0;
643
644         if (configfile[0] == '/') {
645                 strncpy(fn, configfile, sizeof(fn)-1);
646         } else {
647                 snprintf(fn, sizeof(fn), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, configfile);
648         }
649         if ((option_verbose > 1) && !option_debug) {
650                 ast_verbose(  VERBOSE_PREFIX_2 "Parsing '%s': ", fn);
651                 fflush(stdout);
652         }
653         if ((f = fopen(fn, "r"))) {
654                 if (option_debug)
655                         ast_log(LOG_DEBUG, "Parsing %s\n", fn);
656                 else if (option_verbose > 1)
657                         ast_verbose( "Found\n");
658                 if (!tmp) {
659                         tmp = malloc(sizeof(struct ast_config));
660                         if (tmp)
661                                 memset(tmp, 0, sizeof(struct ast_config));
662
663                         master = 1;
664                 }
665                 if (!tmp) {
666                         ast_log(LOG_WARNING, "Out of memory\n");
667                         fclose(f);
668                         return NULL;
669                 }
670                 while(!feof(f)) {
671                         fgets(buf, sizeof(buf), f);
672                         lineno++;
673                         if (!feof(f)) {
674                                 if (cfg_process(tmp, _tmpc, _last, buf, lineno, configfile, includelevel, acs)) {
675                                         fclose(f);
676                                         return NULL;
677                                 }
678                         }
679                 }
680                 fclose(f);              
681         } else {
682                 if (option_debug)
683                         ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
684                 else if (option_verbose > 1)
685                         ast_verbose( "Not found (%s)", strerror(errno));
686         }
687         if (master) {
688                 /* Keep trailing comments */
689                 tmp->trailingcomments = acs->root;
690                 acs->root = NULL;
691                 acs->prev = NULL;
692         }
693         return tmp;
694 }
695
696 struct ast_config *ast_load(char *configfile)
697 {
698         struct ast_category *tmpc=NULL;
699         struct ast_variable *last = NULL;
700         struct ast_comment_struct acs = { NULL, NULL };
701         return __ast_load(configfile, NULL, &tmpc, &last, 0, &acs);
702 }
703
704 char *ast_category_browse(struct ast_config *config, char *prev)
705 {       
706         struct ast_category *cat;
707         if (!prev) {
708                 if (config->root)
709                         return config->root->name;
710                 else
711                         return NULL;
712         }
713         cat = config->root;
714         while(cat) {
715                 if (cat->name == prev) {
716                         if (cat->next)
717                                 return cat->next->name;
718                         else
719                                 return NULL;
720                 }
721                 cat = cat->next;
722         }
723         cat = config->root;
724         while(cat) {
725                 if (!strcasecmp(cat->name, prev)) {
726                         if (cat->next)
727                                 return cat->next->name;
728                         else
729                                 return NULL;
730                 }
731                 cat = cat->next;
732         }
733         return NULL;
734 }