Add a massive set of changes for converting to use the ast_debug() macro.
[asterisk/asterisk.git] / main / config.c
index 9bc97ea..ad71384 100644 (file)
@@ -140,7 +140,8 @@ static void CB_RESET(char **comment_buffer, char **lline_buffer)
 
 static struct ast_comment *ALLOC_COMMENT(const char *buffer)
 { 
-       struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
+       struct ast_comment *x;
+       x = ast_calloc(1, sizeof(*x)+strlen(buffer)+1);
        strcpy(x->cmt, buffer);
        return x;
 }
@@ -215,7 +216,7 @@ void ast_variables_destroy(struct ast_variable *v)
        while (v) {
                vn = v;
                v = v->next;
-               free(vn);
+               ast_free(vn);
        }
 }
 
@@ -344,7 +345,7 @@ void ast_category_append(struct ast_config *config, struct ast_category *categor
 void ast_category_destroy(struct ast_category *cat)
 {
        ast_variables_destroy(cat->root);
-       free(cat);
+       ast_free(cat);
 }
 
 static struct ast_category *next_available_category(struct ast_category *cat)
@@ -468,54 +469,41 @@ int ast_variable_delete(struct ast_category *category, const char *variable, con
        return res;
 }
 
-int ast_variable_update(struct ast_category *category, const char *variable, const char *value, const char *match)
+int ast_variable_update(struct ast_category *category, const char *variable, 
+       const char *value, const char *match, unsigned int object)
 {
        struct ast_variable *cur, *prev=NULL, *newer;
-       newer = ast_variable_new(variable, value);
-       if (!newer)
+
+       if (!(newer = ast_variable_new(variable, value)))
                return -1;
-       cur = category->root;
-       while (cur) {
-               if (cur->name == variable) {
-                       newer->next = cur->next;
-                       newer->object = cur->object;
-                       if (prev)
-                               prev->next = newer;
-                       else
-                               category->root = newer;
-                       if (category->last == cur)
-                               category->last = newer;
-                       cur->next = NULL;
-                       ast_variables_destroy(cur);
-                       return 0;
-               }
-               prev = cur;
-               cur = cur->next;
-       }
+       
+       newer->object = object;
 
-       prev = NULL;
-       cur = category->root;
-       while (cur) {
-               if (!strcasecmp(cur->name, variable) && (ast_strlen_zero(match) || !strcasecmp(cur->value, match))) {
-                       newer->next = cur->next;
-                       newer->object = cur->object;
-                       if (prev)
-                               prev->next = newer;
-                       else
-                               category->root = newer;
-                       if (category->last == cur)
-                               category->last = newer;
-                       cur->next = NULL;
-                       ast_variables_destroy(cur);
-                       return 0;
-               }
-               prev = cur;
-               cur = cur->next;
+       for (cur = category->root; cur; prev = cur, cur = cur->next) {
+               if (strcasecmp(cur->name, variable) ||
+                       (!ast_strlen_zero(match) && strcasecmp(cur->value, match)))
+                       continue;
+
+               newer->next = cur->next;
+               newer->object = cur->object || object;
+               if (prev)
+                       prev->next = newer;
+               else
+                       category->root = newer;
+               if (category->last == cur)
+                       category->last = newer;
+
+               cur->next = NULL;
+               ast_variables_destroy(cur);
+
+               return 0;
        }
+
        if (prev)
                prev->next = newer;
        else
                category->root = newer;
+
        return 0;
 }
 
@@ -535,7 +523,7 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
                                if (cat == cfg->last)
                                        cfg->last = NULL;
                        }
-                       free(cat);
+                       ast_free(cat);
                        return 0;
                }
                prev = cat;
@@ -556,7 +544,7 @@ int ast_category_delete(struct ast_config *cfg, const char *category)
                                if (cat == cfg->last)
                                        cfg->last = NULL;
                        }
-                       free(cat);
+                       ast_free(cat);
                        return 0;
                }
                prev = cat;
@@ -577,9 +565,9 @@ void ast_config_destroy(struct ast_config *cfg)
                ast_variables_destroy(cat->root);
                catn = cat;
                cat = cat->next;
-               free(catn);
+               ast_free(catn);
        }
-       free(cfg);
+       ast_free(cfg);
 }
 
 struct ast_category *ast_config_get_current_category(const struct ast_config *cfg)
@@ -644,7 +632,6 @@ static int process_text_line(struct ast_config *cfg, struct ast_category **cat,
                                } else if (!strcasecmp(cur, "+")) {
                                        *cat = category_get(cfg, catname, 1);
                                        if (!(*cat)) {
-                                               ast_config_destroy(cfg);
                                                if (newcat)
                                                        ast_category_destroy(newcat);
                                                ast_log(LOG_WARNING, "Category addition requested, but category '%s' does not exist, line %d of %s\n", catname, lineno, configfile);
@@ -842,15 +829,13 @@ static struct ast_config *config_text_file_load(const char *database, const char
                        fflush(stdout);
                }
                if (!(f = fopen(fn, "r"))) {
-                       if (option_debug)
-                               ast_log(LOG_DEBUG, "No file to parse: %s\n", fn);
+                       ast_debug(1, "No file to parse: %s\n", fn);
                        if (option_verbose > 1)
                                ast_verbose( "Not found (%s)\n", strerror(errno));
                        continue;
                }
                count++;
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Parsing %s\n", fn);
+               ast_debug(1, "Parsing %s\n", fn);
                if (option_verbose > 1)
                        ast_verbose("Found\n");
                while (!feof(f)) {
@@ -948,8 +933,8 @@ static struct ast_config *config_text_file_load(const char *database, const char
 #endif
 
        if (cfg && cfg->include_level == 1 && withcomments && comment_buffer) {
-               free(comment_buffer);
-               free(lline_buffer);
+               ast_free(comment_buffer);
+               ast_free(lline_buffer);
                comment_buffer = NULL;
                lline_buffer = NULL;
                comment_buffer_size = 0;
@@ -1041,8 +1026,7 @@ int config_text_file_save(const char *configfile, const struct ast_config *cfg,
                if ((option_verbose > 1) && !option_debug)
                        ast_verbose("Saved\n");
        } else {
-               if (option_debug)
-                       ast_log(LOG_DEBUG, "Unable to open for writing: %s\n", fn);
+               ast_debug(1, "Unable to open for writing: %s\n", fn);
                if (option_verbose > 1)
                        ast_verbose(VERBOSE_PREFIX_2 "Unable to write (%s)", strerror(errno));
                return -1;
@@ -1060,7 +1044,7 @@ static void clear_config_maps(void)
        while (config_maps) {
                map = config_maps;
                config_maps = config_maps->next;
-               free(map);
+               ast_free(map);
        }
                
        ast_mutex_unlock(&config_lock);
@@ -1283,6 +1267,8 @@ struct ast_config *ast_config_internal_load(const char *filename, struct ast_con
 
        if (result)
                result->include_level--;
+       else
+               cfg->include_level--;
 
        return result;
 }
@@ -1357,7 +1343,7 @@ struct ast_variable *ast_load_realtime(const char *family, ...)
        /* Eliminate blank entries */
        for (cur = res; cur; cur = cur->next) {
                if (freeme) {
-                       free(freeme);
+                       ast_free(freeme);
                        freeme = NULL;
                }