document a nice technique to exit from a block in case of errors.
[asterisk/asterisk.git] / main / config.c
index 1dc16fd..1f29ee1 100644 (file)
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+#include "asterisk/paths.h"    /* use ast_config_AST_CONFIG_DIR */
+#include "asterisk/network.h"  /* we do some sockaddr manipulation here */
 #include <time.h>
 #include <sys/stat.h>
-#include <sys/socket.h>                /* for AF_INET */
 #define AST_INCLUDE_GLOB 1
 #ifdef AST_INCLUDE_GLOB
 #if defined(__Darwin__) || defined(__CYGWIN__)
@@ -49,8 +45,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/config.h"
 #include "asterisk/cli.h"
 #include "asterisk/lock.h"
-#include "asterisk/options.h"
-#include "asterisk/logger.h"
 #include "asterisk/utils.h"
 #include "asterisk/channel.h"
 #include "asterisk/app.h"
@@ -212,12 +206,19 @@ static struct ast_config_engine *config_engine_list;
 
 #define MAX_INCLUDE_LEVEL 10
 
+struct ast_category_template_instance {
+       char name[80]; /* redundant? */
+       const struct ast_category *inst;
+       AST_LIST_ENTRY(ast_category_template_instance) next;
+};
+
 struct ast_category {
        char name[80];
-       int ignored;                    /*!< do not let user of the config see this category */
+       int ignored;                    /*!< do not let user of the config see this category -- set by (!) after the category decl; a template */
        int include_level;
        char *file;                /*!< the file name from whence this declaration was read */
        int lineno;
+       AST_LIST_HEAD_NOLOCK(template_instance_list, ast_category_template_instance) template_instances;
        struct ast_comment *precomments;
        struct ast_comment *sameline;
        struct ast_comment *trailing; /*!< the last object in the list will get assigned any trailing comments when EOF is hit */
@@ -252,14 +253,16 @@ struct ast_variable *ast_variable_new(const char *name, const char *value, const
 {
        struct ast_variable *variable;
        int name_len = strlen(name) + 1;        
-
-       if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + strlen(filename) + 1 + sizeof(*variable)))) {
-               variable->name = variable->stuff;
-               variable->value = variable->stuff + name_len;           
-               variable->file = variable->stuff + name_len + strlen(value) + 1;
-               strcpy(variable->name,name);
-               strcpy(variable->value,value);
-               strcpy(variable->file,filename);
+       int val_len = strlen(value) + 1;        
+       int fn_len = strlen(filename) + 1;      
+
+       if ((variable = ast_calloc(1, name_len + val_len + fn_len + sizeof(*variable)))) {
+               char *dst = variable->stuff;    /* writable space starts here */
+               variable->name = strcpy(dst, name);
+               dst += name_len;
+               variable->value = strcpy(dst, value);
+               dst += val_len;
+               variable->file = strcpy(dst, filename);
        }
        return variable;
 }
@@ -515,12 +518,43 @@ void ast_category_append(struct ast_config *config, struct ast_category *categor
        config->current = category;
 }
 
+static void ast_destroy_comments(struct ast_category *cat)
+{
+       struct ast_comment *n, *p;
+       for (p=cat->precomments; p; p=n) {
+               n = p->next;
+               free(p);
+       }
+       for (p=cat->sameline; p; p=n) {
+               n = p->next;
+               free(p);
+       }
+       for (p=cat->trailing; p; p=n) {
+               n = p->next;
+               free(p);
+       }
+       cat->precomments = NULL;
+       cat->sameline = NULL;
+       cat->trailing = NULL;
+}
+
+static void ast_destroy_template_list(struct ast_category *cat)
+{
+       struct ast_category_template_instance *x;
+
+       while ((x = AST_LIST_REMOVE_HEAD(&cat->template_instances, next)))
+               free(x);
+}
+
 void ast_category_destroy(struct ast_category *cat)
 {
        ast_variables_destroy(cat->root);
-       if (cat->file)
+       if (cat->file) {
                free(cat->file);
-       
+               cat->file = 0;
+       }
+       ast_destroy_comments(cat);
+       ast_destroy_template_list(cat);
        ast_free(cat);
 }
 
@@ -547,6 +581,12 @@ static struct ast_category *next_available_category(struct ast_category *cat)
        return cat;
 }
 
+/*! return the first var of a category */
+struct ast_variable *ast_category_first(struct ast_category *cat)
+{
+       return (cat) ? cat->root : NULL;
+}
+
 struct ast_variable *ast_category_root(struct ast_config *config, char *cat)
 {
        struct ast_category *category = ast_category_get(config, cat);
@@ -606,7 +646,10 @@ void ast_category_rename(struct ast_category *cat, const char *name)
 static void inherit_category(struct ast_category *new, const struct ast_category *base)
 {
        struct ast_variable *var;
-
+       struct ast_category_template_instance *x = ast_calloc(1,sizeof(struct ast_category_template_instance));
+       strcpy(x->name, base->name);
+       x->inst = base;
+       AST_LIST_INSERT_TAIL(&new->template_instances, x, next);
        for (var = base->root; var; var = var->next)
                ast_variable_append(new, variable_clone(var));
 }
@@ -711,7 +754,6 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
        cat = cfg->root;
        while (cat) {
                if (cat->name == category) {
-                       ast_variables_destroy(cat->root);
                        if (prev) {
                                prev->next = cat->next;
                                if (cat == cfg->last)
@@ -721,7 +763,7 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
                                if (cat == cfg->last)
                                        cfg->last = NULL;
                        }
-                       ast_free(cat);
+                       ast_category_destroy(cat);
                        return 0;
                }
                prev = cat;
@@ -732,7 +774,6 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
        cat = cfg->root;
        while (cat) {
                if (!strcasecmp(cat->name, category)) {
-                       ast_variables_destroy(cat->root);
                        if (prev) {
                                prev->next = cat->next;
                                if (cat == cfg->last)
@@ -742,7 +783,7 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
                                if (cat == cfg->last)
                                        cfg->last = NULL;
                        }
-                       ast_free(cat);
+                       ast_category_destroy(cat);
                        return 0;
                }
                prev = cat;
@@ -762,11 +803,9 @@ void ast_config_destroy(struct ast_config *cfg)
 
        cat = cfg->root;
        while (cat) {
-               ast_variables_destroy(cat->root);
                catn = cat;
                cat = cat->next;
-               ast_free(catn->file);
-               ast_free(catn);
+               ast_category_destroy(catn);
        }
        ast_free(cfg);
 }
@@ -818,6 +857,12 @@ static void config_cache_attribute(const char *configfile, enum config_cache_att
 
        switch (attrtype) {
        case ATTRIBUTE_INCLUDE:
+               AST_LIST_TRAVERSE(&cfmtime->includes, cfinclude, list) {
+                       if (!strcmp(cfinclude->include, filename)) {
+                               AST_LIST_UNLOCK(&cfmtime_head);
+                               return;
+                       }
+               }
                cfinclude = ast_calloc(1, sizeof(*cfinclude) + strlen(filename) + 1);
                if (!cfinclude) {
                        AST_LIST_UNLOCK(&cfmtime_head);
@@ -833,21 +878,36 @@ static void config_cache_attribute(const char *configfile, enum config_cache_att
        AST_LIST_UNLOCK(&cfmtime_head);
 }
 
-static int process_text_line(struct ast_config *cfg, struct ast_category **cat, char *buf, int lineno, const char *configfile, struct ast_flags flags,
-                                                        char **comment_buffer, int *comment_buffer_size, char **lline_buffer, int *lline_buffer_size, const char *suggested_include_file, struct ast_category **last_cat, struct ast_variable **last_var)
+/*! \brief parse one line in the configuration.
+ * We can have a category header       [foo](...)
+ * a directive                         #include / #exec
+ * or a regular line                   name = value
+ */
+static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
+       char *buf, int lineno, const char *configfile, struct ast_flags flags,
+       char **comment_buffer, int *comment_buffer_size,
+       char **lline_buffer, int *lline_buffer_size,
+       const char *suggested_include_file,
+       struct ast_category **last_cat, struct ast_variable **last_var)
 {
        char *c;
        char *cur = buf;
        struct ast_variable *v;
        char cmd[512], exec_file[512];
-       int object, do_exec, do_include;
 
        /* Actually parse the entry */
-       if (cur[0] == '[') {
+       if (cur[0] == '[') { /* A category header */
+               /* format is one of the following:
+                * [foo]        define a new category named 'foo'
+                * [foo](!)     define a new template category named 'foo'
+                * [foo](+)     append to category 'foo', error if foo does not exist.
+                * [foo](a)     define a new category and inherit from template a.
+                *              You can put a comma-separated list of templates and '!' and '+'
+                *              between parentheses, with obvious meaning.
+                */
                struct ast_category *newcat = NULL;
                char *catname;
 
-               /* A category header */
                c = strchr(cur, ']');
                if (!c) {
                        ast_log(LOG_WARNING, "parse error: no closing ']', line %d of %s\n", lineno, configfile);
@@ -912,8 +972,9 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                }
                if (newcat)
                        ast_category_append(cfg, *cat);
-       } else if (cur[0] == '#') {
-               /* A directive */
+       } else if (cur[0] == '#') { /* A directive - #include or #exec */
+               int do_exec, do_include;
+
                cur++;
                c = cur;
                while (*c && (*c > 32)) c++;
@@ -995,6 +1056,7 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                }
                c = strchr(cur, '=');
                if (c) {
+                       int object;
                        *c = 0;
                        c++;
                        /* Ignore > in => */
@@ -1091,6 +1153,11 @@ static struct ast_config *config_text_file_load(const char *database, const char
                        for (i=0; i<globbuf.gl_pathc; i++) {
                                ast_copy_string(fn, globbuf.gl_pathv[i], sizeof(fn));
 #endif
+       /*
+        * The following is not a loop, but just a convenient way to define a block
+        * (using do { } while(0) ), and be able to exit from it with 'continue'
+        * or 'break' in case of errors. Nice trick.
+        */
        do {
                if (stat(fn, &statbuf))
                        continue;
@@ -1386,7 +1453,6 @@ static void set_fn(char *fn, int fn_size, const char *file, const char *configfi
                fx->lineno = 1;
                *fi = fx;
                ao2_link(fileset, fx);
-               ao2_ref(fx,1); /* bump the ref count, so it looks like we just got the ref from find */
        }
 }
 
@@ -1522,7 +1588,20 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg,
                        if (!cat->precomments)
                                fprintf(f,"\n");
                        fprintf(f, "[%s]", cat->name);
-                       for (cmt = cat->sameline; cmt; cmt=cmt->next) {
+                       if (cat->ignored)
+                               fprintf(f, "(!)");
+                       if (!AST_LIST_EMPTY(&cat->template_instances)) {
+                               struct ast_category_template_instance *x;
+                               fprintf(f, "(");
+                               AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
+                                       fprintf(f,"%s",x->name);
+                                       if (x != AST_LIST_LAST(&cat->template_instances))
+                                               fprintf(f,",");
+                               }
+                               fprintf(f, ")");
+                       }
+                       for(cmt = cat->sameline; cmt; cmt=cmt->next)
+                       {
                                fprintf(f,"%s", cmt->cmt);
                        }
                        if (!cat->sameline)
@@ -1537,6 +1616,23 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg,
                        
                        var = cat->root;
                        while (var) {
+                               struct ast_category_template_instance *x;
+                               int found = 0;
+                               AST_LIST_TRAVERSE(&cat->template_instances, x, next) {
+                                       struct ast_variable *v;
+                                       for (v = x->inst->root; v; v = v->next) {
+                                               if (!strcasecmp(var->name, v->name) && !strcmp(var->value, v->value)) {
+                                                       found = 1;
+                                                       break;
+                                               }
+                                       }
+                                       if (found)
+                                               break;
+                               }
+                               if (found) {
+                                       var = var->next;
+                                       continue;
+                               }
                                set_fn(fn, sizeof(fn), var->file, configfile, fileset, &fi);
                                f = fopen(fn, "a");
                                if (!f)
@@ -1648,7 +1744,7 @@ static void clear_config_maps(void)
        ast_mutex_unlock(&config_lock);
 }
 
-static int append_mapping(char *name, char *driver, char *database, char *table)
+static int append_mapping(const char *name, const char *driver, const char *database, const char *table)
 {
        struct ast_config_map *map;
        int length;
@@ -1699,7 +1795,9 @@ int read_config_maps(void)
        }
 
        for (v = ast_variable_browse(config, "settings"); v; v = v->next) {
-               stringp = v->value;
+               char buf[512];
+               ast_copy_string(buf, v->value, sizeof(buf));
+               stringp = buf;
                driver = strsep(&stringp, ",");
 
                if ((tmp = strchr(stringp, '\"')))
@@ -2143,37 +2241,42 @@ int ast_parse_arg(const char *arg, enum ast_parse_flags flags,
        return error;
 }
 
-static int config_command(int fd, int argc, char **argv) 
+static char *handle_cli_core_show_config_mappings(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct ast_config_engine *eng;
        struct ast_config_map *map;
+
+       switch (cmd) {
+       case CLI_INIT:
+               e->command = "core show config mappings";
+               e->usage =
+                       "Usage: core show config mappings\n"
+                       "       Shows the filenames to config engines.\n";
+               return NULL;
+       case CLI_GENERATE:
+               return NULL;
+       }
        
        ast_mutex_lock(&config_lock);
 
-       ast_cli(fd, "\n\n");
+       ast_cli(a->fd, "\n\n");
        for (eng = config_engine_list; eng; eng = eng->next) {
-               ast_cli(fd, "\nConfig Engine: %s\n", eng->name);
+               ast_cli(a->fd, "\nConfig Engine: %s\n", eng->name);
                for (map = config_maps; map; map = map->next)
                        if (!strcasecmp(map->driver, eng->name)) {
-                               ast_cli(fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
+                               ast_cli(a->fd, "===> %s (db=%s, table=%s)\n", map->name, map->database,
                                        map->table ? map->table : map->name);
                        }
        }
-       ast_cli(fd,"\n\n");
+       ast_cli(a->fd,"\n\n");
        
        ast_mutex_unlock(&config_lock);
 
-       return 0;
+       return CLI_SUCCESS;
 }
 
-static char show_config_help[] =
-       "Usage: core show config mappings\n"
-       "       Shows the filenames to config engines.\n";
-
 static struct ast_cli_entry cli_config[] = {
-       { { "core", "show", "config", "mappings", NULL },
-       config_command, "Display config mappings (file names to config engines)",
-       show_config_help },
+       AST_CLI_DEFINE(handle_cli_core_show_config_mappings, "Display config mappings (file names to config engines)"),
 };
 
 int register_config_cli()