minor cleanups ...
authorRussell Bryant <russell@russellbryant.com>
Sat, 24 Dec 2005 06:23:36 +0000 (06:23 +0000)
committerRussell Bryant <russell@russellbryant.com>
Sat, 24 Dec 2005 06:23:36 +0000 (06:23 +0000)
- use for loops instead of while loops for basic list traversals
- only calculate word length one time in CLI complete functions
- use calloc instead of malloc + memset
- remove some unnecessary casts
- formatting tweaks

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7618 65c4cc65-6c06-0410-ace0-fbb531ad65f3

include/asterisk/pbx.h
pbx.c

index 9332b9e..82df57a 100644 (file)
@@ -626,7 +626,7 @@ int ast_parseable_goto(struct ast_channel *chan, const char *goto_string);
 int ast_explicit_goto(struct ast_channel *chan, const char *context, const char *exten, int priority);
 int ast_async_goto_if_exists(struct ast_channel *chan, const char *context, const char *exten, int priority);
 
-struct ast_custom_function* ast_custom_function_find(char *name);
+struct ast_custom_function* ast_custom_function_find(const char *name);
 int ast_custom_function_unregister(struct ast_custom_function *acf);
 int ast_custom_function_register(struct ast_custom_function *acf);
 
diff --git a/pbx.c b/pbx.c
index 82b49a2..17dadbd 100644 (file)
--- a/pbx.c
+++ b/pbx.c
@@ -102,45 +102,46 @@ AST_APP_OPTIONS(waitexten_opts, {
 
 struct ast_context;
 
-/*!\brief ast_exten: An extension 
+/*!
+   \brief ast_exten: An extension 
        The dialplan is saved as a linked list with each context
        having it's own linked list of extensions - one item per
        priority.
 */
 struct ast_exten {
-       char *exten;                    /* Extension name */
-       int matchcid;                   /* Match caller id ? */
-       char *cidmatch;                 /* Caller id to match for this extension */
-       int priority;                   /* Priority */
-       char *label;                    /* Label */
-       struct ast_context *parent;     /* The context this extension belongs to  */
-       char *app;                      /* Application to execute */
-       void *data;                     /* Data to use (arguments) */
-       void (*datad)(void *);          /* Data destructor */
-       struct ast_exten *peer;         /* Next higher priority with our extension */
-       const char *registrar;          /* Registrar */
-       struct ast_exten *next;         /* Extension with a greater ID */
+       char *exten;                    /*!< Extension name */
+       int matchcid;                   /*!< Match caller id ? */
+       char *cidmatch;                 /*!< Caller id to match for this extension */
+       int priority;                   /*!< Priority */
+       char *label;                    /*!< Label */
+       struct ast_context *parent;     /*!< The context this extension belongs to  */
+       char *app;                      /*!< Application to execute */
+       void *data;                     /*!< Data to use (arguments) */
+       void (*datad)(void *);          /*!< Data destructor */
+       struct ast_exten *peer;         /*!< Next higher priority with our extension */
+       const char *registrar;          /*!< Registrar */
+       struct ast_exten *next;         /*!< Extension with a greater ID */
        char stuff[0];
 };
 
 /*! \brief ast_include: include= support in extensions.conf */
 struct ast_include {
        char *name;             
-       char *rname;            /* Context to include */
-       const char *registrar;                  /* Registrar */
-       int hastime;                            /* If time construct exists */
-       struct ast_timing timing;               /* time construct */
-       struct ast_include *next;               /* Link them together */
+       char *rname;                            /*!< Context to include */
+       const char *registrar;                  /*!< Registrar */
+       int hastime;                            /*!< If time construct exists */
+       struct ast_timing timing;               /*!< time construct */
+       struct ast_include *next;               /*!< Link them together */
        char stuff[0];
 };
 
 /*! \brief ast_sw: Switch statement in extensions.conf */
 struct ast_sw {
        char *name;
-       const char *registrar;                  /* Registrar */
-       char *data;                             /* Data load */
+       const char *registrar;                  /*!< Registrar */
+       char *data;                             /*!< Data load */
        int eval;
-       struct ast_sw *next;                    /* Link them together */
+       struct ast_sw *next;                    /*!< Link them together */
        char *tmpdata;
        char stuff[0];
 };
@@ -168,10 +169,10 @@ struct ast_context {
 /*! \brief ast_app: A registered application */
 struct ast_app {
        int (*execute)(struct ast_channel *chan, void *data);
-       const char *synopsis;                   /* Synopsis text for 'show applications' */
-       const char *description;                /* Description (help text) for 'show application <name>' */
-       struct ast_app *next;                   /* Next app in list */
-       char name[0];                           /* Name of the application */
+       const char *synopsis;                   /*!< Synopsis text for 'show applications' */
+       const char *description;                /*!< Description (help text) for 'show application <name>' */
+       struct ast_app *next;                   /*!< Next app in list */
+       char name[0];                           /*!< Name of the application */
 };
 
 /*! \brief ast_state_cb: An extension state notify register item */
@@ -443,14 +444,14 @@ static struct pbx_builtin {
 };
 
 static struct ast_context *contexts = NULL;
-AST_MUTEX_DEFINE_STATIC(conlock);              /* Lock for the ast_context list */
+AST_MUTEX_DEFINE_STATIC(conlock);              /*!< Lock for the ast_context list */
 static struct ast_app *apps = NULL;
-AST_MUTEX_DEFINE_STATIC(applock);              /* Lock for the application list */
+AST_MUTEX_DEFINE_STATIC(applock);              /*!< Lock for the application list */
 
 struct ast_switch *switches = NULL;
-AST_MUTEX_DEFINE_STATIC(switchlock);           /* Lock for switches */
+AST_MUTEX_DEFINE_STATIC(switchlock);           /*!< Lock for switches */
 
-AST_MUTEX_DEFINE_STATIC(hintlock);             /* Lock for extension state notifys */
+AST_MUTEX_DEFINE_STATIC(hintlock);             /*!< Lock for extension state notifys */
 static int stateid = 1;
 struct ast_hint *hints = NULL;
 struct ast_state_cb *statecbs = NULL;
@@ -511,11 +512,9 @@ struct ast_app *pbx_findapp(const char *app)
                ast_log(LOG_WARNING, "Unable to obtain application lock\n");
                return NULL;
        }
-       tmp = apps;
-       while(tmp) {
+       for (tmp = apps; tmp; tmp = tmp->next) {
                if (!strcasecmp(tmp->name, app))
                        break;
-               tmp = tmp->next;
        }
        ast_mutex_unlock(&applock);
        return tmp;
@@ -529,11 +528,9 @@ static struct ast_switch *pbx_findswitch(const char *sw)
                ast_log(LOG_WARNING, "Unable to obtain application lock\n");
                return NULL;
        }
-       asw = switches;
-       while(asw) {
+       for (asw = switches; asw; asw = asw->next) {
                if (!strcasecmp(asw->name, sw))
                        break;
-               asw = asw->next;
        }
        ast_mutex_unlock(&switchlock);
        return asw;
@@ -668,11 +665,9 @@ struct ast_context *ast_context_find(const char *name)
        struct ast_context *tmp;
        ast_mutex_lock(&conlock);
        if (name) {
-               tmp = contexts;
-               while(tmp) {
+               for (tmp = contexts; tmp; tmp = tmp->next) {
                        if (!strcasecmp(name, tmp->name))
                                break;
-                       tmp = tmp->next;
                }
        } else
                tmp = contexts;
@@ -725,7 +720,7 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
                return NULL;
        }
        /* Check first to see if we've already been checked */
-       for (x=0; x<*stacklen; x++) {
+       for (x = 0; x < *stacklen; x++) {
                if (!strcasecmp(incstack[x], context))
                        return NULL;
        }
@@ -733,30 +728,29 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
                tmp = bypass;
        else
                tmp = contexts;
-       while(tmp) {
+       for (; tmp; tmp = tmp->next) {
                /* Match context */
                if (bypass || !strcmp(tmp->name, context)) {
                        struct ast_exten *earlymatch = NULL;
 
                        if (*status < STATUS_NO_EXTENSION)
                                *status = STATUS_NO_EXTENSION;
-                       for (eroot = tmp->root; eroot; eroot=eroot->next) {
+                       for (eroot = tmp->root; eroot; eroot = eroot->next) {
                                int match = 0;
                                /* Match extension */
                                if ((((action != HELPER_MATCHMORE) && ast_extension_match(eroot->exten, exten)) ||
                                     ((action == HELPER_CANMATCH) && (ast_extension_close(eroot->exten, exten, 0))) ||
                                     ((action == HELPER_MATCHMORE) && (match = ast_extension_close(eroot->exten, exten, 1)))) &&
-                                   (!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
+                                    (!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
 
                                        if (action == HELPER_MATCHMORE && match == 2 && !earlymatch) {
                                                /* It matched an extension ending in a '!' wildcard
                                                   So ignore it for now, unless there's a better match */
                                                earlymatch = eroot;
                                        } else {
-                                               e = eroot;
                                                if (*status < STATUS_NO_PRIORITY)
                                                        *status = STATUS_NO_PRIORITY;
-                                               while(e) {
+                                               for (e = eroot; e; e = e->peer) {
                                                        /* Match priority */
                                                        if (action == HELPER_FINDLABEL) {
                                                                if (*status < STATUS_NO_LABEL)
@@ -771,7 +765,6 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
                                                                *foundcontext = context;
                                                                return e;
                                                        }
-                                                       e = e->peer;
                                                }
                                        }
                                }
@@ -784,8 +777,7 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
                                return NULL;
                        }
                        /* Check alternative switches */
-                       sw = tmp->alts;
-                       while(sw) {
+                       for (sw = tmp->alts; sw; sw = sw->next) {
                                if ((asw = pbx_findswitch(sw->name))) {
                                        /* Substitute variables now */
                                        if (sw->eval) 
@@ -806,25 +798,21 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
                                } else {
                                        ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
                                }
-                               sw = sw->next;
                        }
                        /* Setup the stack */
                        incstack[*stacklen] = tmp->name;
                        (*stacklen)++;
                        /* Now try any includes we have in this context */
-                       i = tmp->includes;
-                       while(i) {
+                       for (i = tmp->includes; i; i = i->next) {
                                if (include_valid(i)) {
                                        if ((e = pbx_find_extension(chan, bypass, i->rname, exten, priority, label, callerid, action, incstack, stacklen, status, swo, data, foundcontext))) 
                                                return e;
                                        if (*swo) 
                                                return NULL;
                                }
-                               i = i->next;
                        }
                        break;
                }
-               tmp = tmp->next;
        }
        return NULL;
 }
@@ -840,7 +828,7 @@ static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
        *offset = 0;
        *length = DONT_HAVE_LENGTH;
        *isfunc = 0;
-       for (varchar=var; *varchar; varchar++) {
+       for (varchar = var; *varchar; varchar++) {
                switch (*varchar) {
                case '(':
                        (*isfunc)++;
@@ -1194,6 +1182,9 @@ static char *complete_show_function(char *line, char *word, int pos, int state)
 {
        struct ast_custom_function *acf;
        int which = 0;
+       int wordlen;
+
+       wordlen = strlen(word);
 
        /* try to lock functions list ... */
        if (ast_mutex_lock(&acflock)) {
@@ -1201,23 +1192,21 @@ static char *complete_show_function(char *line, char *word, int pos, int state)
                return NULL;
        }
 
-       acf = acf_root;
-       while (acf) {
-               if (!strncasecmp(word, acf->name, strlen(word))) {
+       for (acf = acf_root; acf; acf = acf->next) {
+               if (!strncasecmp(word, acf->name, wordlen)) {
                        if (++which > state) {
                                char *ret = strdup(acf->name);
                                ast_mutex_unlock(&acflock);
                                return ret;
                        }
                }
-               acf = acf->next; 
        }
 
        ast_mutex_unlock(&acflock);
        return NULL; 
 }
 
-struct ast_custom_function* ast_custom_function_find(char *name) 
+struct ast_custom_function* ast_custom_function_find(const char *name) 
 {
        struct ast_custom_function *acfptr;
 
@@ -1677,7 +1666,8 @@ static int pbx_extension_helper(struct ast_channel *c, struct ast_context *con,
                                return -1;
                        }
                default:
-                       ast_log(LOG_WARNING, "Huh (%d)?\n", action);                    return -1;
+                       ast_log(LOG_WARNING, "Huh (%d)?\n", action);
+                       return -1;
                }
        } else if (sw) {
                switch(action) {
@@ -1913,23 +1903,20 @@ int ast_extension_state_add(const char *context, const char *exten,
        if (!context && !exten) {
                ast_mutex_lock(&hintlock);
 
-               cblist = statecbs;
-               while (cblist) {
+               for (cblist = statecbs; cblist; cblist = cblist->next) {
                        if (cblist->callback == callback) {
                                cblist->data = data;
                                ast_mutex_unlock(&hintlock);
                                return 0;
                        }
-                       cblist = cblist->next;
                }
        
                /* Now insert the callback */
-               cblist = malloc(sizeof(struct ast_state_cb));
+               cblist = calloc(1, sizeof(struct ast_state_cb));
                if (!cblist) {
                        ast_mutex_unlock(&hintlock);
                        return -1;
                }
-               memset(cblist, 0, sizeof(struct ast_state_cb));
                cblist->id = 0;
                cblist->callback = callback;
                cblist->data = data;
@@ -1952,12 +1939,10 @@ int ast_extension_state_add(const char *context, const char *exten,
 
        /* Find the hint in the list of hints */
        ast_mutex_lock(&hintlock);
-       list = hints;        
-
-       while (list) {
+       
+       for (list = hints; list; list = list->next) {
                if (list->exten == e)
                        break;      
-               list = list->next;    
        }
 
        if (!list) {
@@ -1967,12 +1952,11 @@ int ast_extension_state_add(const char *context, const char *exten,
        }
 
        /* Now insert the callback in the callback list  */
-       cblist = malloc(sizeof(struct ast_state_cb));
+       cblist = calloc(1, sizeof(struct ast_state_cb));
        if (!cblist) {
                ast_mutex_unlock(&hintlock);
                return -1;
        }
-       memset(cblist, 0, sizeof(struct ast_state_cb));
        cblist->id = stateid++;         /* Unique ID for this callback */
        cblist->callback = callback;    /* Pointer to callback routine */
        cblist->data = data;            /* Data for the callback */
@@ -1998,8 +1982,7 @@ int ast_extension_state_del(int id, ast_state_cb_type callback)
        /* id is zero is a callback without extension */
        if (!id) {
                cbprev = NULL;
-               cblist = statecbs;
-               while (cblist) {
+               for (cblist = statecbs; cblist; cblist = cblist->next) {
                        if (cblist->callback == callback) {
                                if (!cbprev)
                                        statecbs = cblist->next;
@@ -2012,7 +1995,6 @@ int ast_extension_state_del(int id, ast_state_cb_type callback)
                                return 0;
                        }
                        cbprev = cblist;
-                       cblist = cblist->next;
                }
 
                ast_mutex_lock(&hintlock);
@@ -2021,11 +2003,9 @@ int ast_extension_state_del(int id, ast_state_cb_type callback)
 
        /* id greater than zero is a callback with extension */
        /* Find the callback based on ID */
-       list = hints;
-       while (list) {
-               cblist = list->callbacks;
+       for (list = hints; list; list = list->next) {
                cbprev = NULL;
-               while (cblist) {
+               for (cblist = list->callbacks; cblist; cblist = cblist->next) {
                        if (cblist->id==id) {
                                if (!cbprev)
                                        list->callbacks = cblist->next;         
@@ -2038,9 +2018,7 @@ int ast_extension_state_del(int id, ast_state_cb_type callback)
                                return 0;               
                        }               
                        cbprev = cblist;                                
-                       cblist = cblist->next;
                }
-               list = list->next;
        }
 
        ast_mutex_unlock(&hintlock);
@@ -2056,23 +2034,21 @@ static int ast_add_hint(struct ast_exten *e)
                return -1;
 
        ast_mutex_lock(&hintlock);
-       list = hints;        
 
        /* Search if hint exists, do nothing */
-       while (list) {
+       for (list = hints; list; list = list->next) {
                if (list->exten == e) {
                        ast_mutex_unlock(&hintlock);
                        if (option_debug > 1)
                                ast_log(LOG_DEBUG, "HINTS: Not re-adding existing hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
                        return -1;
                }
-               list = list->next;    
        }
 
        if (option_debug > 1)
                ast_log(LOG_DEBUG, "HINTS: Adding hint %s: %s\n", ast_get_extension_name(e), ast_get_extension_app(e));
 
-       list = malloc(sizeof(struct ast_hint));
+       list = calloc(1, sizeof(struct ast_hint));
        if (!list) {
                ast_mutex_unlock(&hintlock);
                if (option_debug > 1)
@@ -2080,7 +2056,6 @@ static int ast_add_hint(struct ast_exten *e)
                return -1;
        }
        /* Initialize and insert new item at the top */
-       memset(list, 0, sizeof(struct ast_hint));
        list->exten = e;
        list->laststate = ast_extension_state2(e);
        list->next = hints;
@@ -2096,16 +2071,15 @@ static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
        struct ast_hint *list;
 
        ast_mutex_lock(&hintlock);
-       list = hints;
 
-       while(list) {
+       for (list = hints; list; list = list->next) {
                if (list->exten == oe) {
                        list->exten = ne;
                        ast_mutex_unlock(&hintlock);    
                        return 0;
                }
-               list = list->next;
        }
+
        ast_mutex_unlock(&hintlock);
 
        return -1;
@@ -2123,9 +2097,8 @@ static int ast_remove_hint(struct ast_exten *e)
 
        ast_mutex_lock(&hintlock);
 
-       list = hints;    
-       while(list) {
-               if (list->exten==e) {
+       for (list = hints; list; list = list->next) {
+               if (list->exten == e) {
                        cbprev = NULL;
                        cblist = list->callbacks;
                        while (cblist) {
@@ -2145,10 +2118,8 @@ static int ast_remove_hint(struct ast_exten *e)
            
                        ast_mutex_unlock(&hintlock);
                        return 0;
-               } else {
-                       prev = list;
-                       list = list->next;    
                }
+               prev = list;
        }
 
        ast_mutex_unlock(&hintlock);
@@ -2171,7 +2142,7 @@ int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_
                        if (tmp)
                                ast_copy_string(name, (char *) tmp, namesize);
                }
-           return -1;
+               return -1;
        }
        return 0;       
 }
@@ -2224,7 +2195,7 @@ static int __ast_pbx_run(struct ast_channel *c)
        /* A little initial setup here */
        if (c->pbx)
                ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
-       c->pbx = malloc(sizeof(struct ast_pbx));
+       c->pbx = calloc(1, sizeof(struct ast_pbx));
        if (!c->pbx) {
                ast_log(LOG_ERROR, "Out of memory\n");
                return -1;
@@ -2240,7 +2211,6 @@ static int __ast_pbx_run(struct ast_channel *c)
                        ast_cdr_init(c->cdr, c);
                }
        }
-       memset(c->pbx, 0, sizeof(struct ast_pbx));
        /* Set reasonable defaults */
        c->pbx->rtimeout = 10;
        c->pbx->dtimeout = 5;
@@ -2264,10 +2234,10 @@ static int __ast_pbx_run(struct ast_channel *c)
        }
        if (c->cdr && !c->cdr->start.tv_sec && !c->cdr->start.tv_usec)
                ast_cdr_start(c->cdr);
-       for(;;) {
+       for (;;) {
                pos = 0;
                digit = 0;
-               while(ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
+               while (ast_exists_extension(c, c->context, c->exten, c->priority, c->cid.cid_num)) {
                        memset(exten, 0, sizeof(exten));
                        if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->cid.cid_num))) {
                                /* Something bad happened, or a hangup has been requested. */
@@ -2561,8 +2531,7 @@ int ast_context_remove_include(const char *context, const char *include, const c
        if (ast_lock_contexts()) return -1;
 
        /* walk contexts and search for the right one ...*/
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* we found one ... */
                if (!strcmp(ast_get_context_name(c), context)) {
                        int ret;
@@ -2574,7 +2543,6 @@ int ast_context_remove_include(const char *context, const char *include, const c
                        /* ... return results */
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        /* we can't find the right one context */
@@ -2597,8 +2565,7 @@ int ast_context_remove_include2(struct ast_context *con, const char *include, co
        if (ast_mutex_lock(&con->lock)) return -1;
 
        /* walk includes */
-       i = con->includes;
-       while (i) {
+       for (i = con->includes; i; i = i->next) {
                /* find our include */
                if (!strcmp(i->name, include) && 
                        (!registrar || !strcmp(i->registrar, registrar))) {
@@ -2613,7 +2580,6 @@ int ast_context_remove_include2(struct ast_context *con, const char *include, co
                        return 0;
                }
                pi = i;
-               i = i->next;
        }
 
        /* we can't find the right include */
@@ -2633,8 +2599,7 @@ int ast_context_remove_switch(const char *context, const char *sw, const char *d
        if (ast_lock_contexts()) return -1;
 
        /* walk contexts and search for the right one ...*/
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* we found one ... */
                if (!strcmp(ast_get_context_name(c), context)) {
                        int ret;
@@ -2646,7 +2611,6 @@ int ast_context_remove_switch(const char *context, const char *sw, const char *d
                        /* ... return results */
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        /* we can't find the right one context */
@@ -2669,8 +2633,7 @@ int ast_context_remove_switch2(struct ast_context *con, const char *sw, const ch
        if (ast_mutex_lock(&con->lock)) return -1;
 
        /* walk switchs */
-       i = con->alts;
-       while (i) {
+       for (i = con->alts; i; i = i->next) {
                /* find our switch */
                if (!strcmp(i->name, sw) && !strcmp(i->data, data) && 
                        (!registrar || !strcmp(i->registrar, registrar))) {
@@ -2685,7 +2648,6 @@ int ast_context_remove_switch2(struct ast_context *con, const char *sw, const ch
                        return 0;
                }
                pi = i;
-               i = i->next;
        }
 
        /* we can't find the right switch */
@@ -2705,8 +2667,7 @@ int ast_context_remove_extension(const char *context, const char *extension, int
        if (ast_lock_contexts()) return -1;
 
        /* walk contexts ... */
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* ... search for the right one ... */
                if (!strcmp(ast_get_context_name(c), context)) {
                        /* ... remove extension ... */
@@ -2716,7 +2677,6 @@ int ast_context_remove_extension(const char *context, const char *extension, int
                        ast_unlock_contexts();
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        /* we can't find the right context */
@@ -2850,43 +2810,40 @@ int ast_register_application(const char *app, int (*execute)(struct ast_channel
                ast_log(LOG_ERROR, "Unable to lock application list\n");
                return -1;
        }
-       tmp = apps;
-       while(tmp) {
+       for (tmp = apps; tmp; tmp = tmp->next) {
                if (!strcasecmp(app, tmp->name)) {
                        ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
                        ast_mutex_unlock(&applock);
                        return -1;
                }
-               tmp = tmp->next;
        }
-       tmp = malloc(length);
-       if (tmp) {
-               memset(tmp, 0, length);
-               strcpy(tmp->name, app);
-               tmp->execute = execute;
-               tmp->synopsis = synopsis;
-               tmp->description = description;
-               /* Store in alphabetical order */
-               cur = apps;
-               prev = NULL;
-               while(cur) {
-                       if (strcasecmp(tmp->name, cur->name) < 0)
-                               break;
-                       prev = cur;
-                       cur = cur->next;
-               }
-               if (prev) {
-                       tmp->next = prev->next;
-                       prev->next = tmp;
-               } else {
-                       tmp->next = apps;
-                       apps = tmp;
-               }
-       } else {
+       
+       tmp = calloc(1, length);
+       if (!tmp) {
                ast_log(LOG_ERROR, "Out of memory\n");
                ast_mutex_unlock(&applock);
                return -1;
        }
+
+       strcpy(tmp->name, app);
+       tmp->execute = execute;
+       tmp->synopsis = synopsis;
+       tmp->description = description;
+       /* Store in alphabetical order */
+       prev = NULL;
+       for (cur = apps; cur; cur = cur->next) {
+               if (strcasecmp(tmp->name, cur->name) < 0)
+                       break;
+               prev = cur;
+       }
+       if (prev) {
+               tmp->next = prev->next;
+               prev->next = tmp;
+       } else {
+               tmp->next = apps;
+               apps = tmp;
+       }
+       
        if (option_verbose > 1)
                ast_verbose( VERBOSE_PREFIX_2 "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
        ast_mutex_unlock(&applock);
@@ -2900,12 +2857,10 @@ int ast_register_switch(struct ast_switch *sw)
                ast_log(LOG_ERROR, "Unable to lock switch lock\n");
                return -1;
        }
-       tmp = switches;
-       while(tmp) {
+       for (tmp = switches; tmp; tmp = tmp->next) {
                if (!strcasecmp(tmp->name, sw->name))
                        break;
                prev = tmp;
-               tmp = tmp->next;
        }
        if (tmp) {      
                ast_mutex_unlock(&switchlock);
@@ -2928,8 +2883,7 @@ void ast_unregister_switch(struct ast_switch *sw)
                ast_log(LOG_ERROR, "Unable to lock switch lock\n");
                return;
        }
-       tmp = switches;
-       while(tmp) {
+       for (tmp = switches; tmp; tmp = tmp->next) {
                if (tmp == sw) {
                        if (prev)
                                prev->next = tmp->next;
@@ -2939,7 +2893,6 @@ void ast_unregister_switch(struct ast_switch *sw)
                        break;                  
                }
                prev = tmp;
-               tmp = tmp->next;
        }
        ast_mutex_unlock(&switchlock);
 }
@@ -3005,8 +2958,7 @@ static char *complete_show_application(char *line, char *word,
        }
 
        /* ... walk all applications ... */
-       a = apps; 
-       while (a) {
+       for (a = apps; a; a = a->next) {
                /* ... check if word matches this application ... */
                if (!strncasecmp(word, a->name, strlen(word))) {
                        /* ... if this is right app serve it ... */
@@ -3016,7 +2968,6 @@ static char *complete_show_application(char *line, char *word,
                                return ret;
                        }
                }
-               a = a->next; 
        }
 
        /* no application match */
@@ -3038,8 +2989,7 @@ static int handle_show_application(int fd, int argc, char *argv[])
        }
 
        /* ... go through all applications ... */
-       a = apps; 
-       while (a) {
+       for (a = apps; a; a = a->next) {
                /* ... compare this application name with all arguments given
                 * to 'show application' command ... */
                for (app = 2; app < argc; app++) {
@@ -3087,7 +3037,6 @@ static int handle_show_application(int fd, int argc, char *argv[])
                                }
                        }
                }
-               a = a->next; 
        }
 
        ast_mutex_unlock(&applock);
@@ -3119,8 +3068,7 @@ static int handle_show_hints(int fd, int argc, char *argv[])
                ast_log(LOG_ERROR, "Unable to lock hints\n");
                return -1;
        }
-       hint = hints;
-       while (hint) {
+       for (hint = hints; hint; hint = hint->next) {
                watchers = 0;
                for (watcher = hint->callbacks; watcher; watcher = watcher->next)
                        watchers++;
@@ -3128,7 +3076,6 @@ static int handle_show_hints(int fd, int argc, char *argv[])
                        ast_get_extension_name(hint->exten), ast_get_extension_app(hint->exten),
                        ast_extension_state2str(hint->laststate), watchers);
                num++;
-               hint = hint->next;
        }
        ast_cli(fd, "----------------\n");
        ast_cli(fd, "- %d hints registered\n", num);
@@ -3150,10 +3097,8 @@ static int handle_show_switches(int fd, int argc, char *argv[])
                ast_log(LOG_ERROR, "Unable to lock switches\n");
                return -1;
        }
-       sw = switches;
-       while (sw) {
+       for (sw = switches; sw; sw = sw->next) {
                ast_cli(fd, "%s: %s\n", sw->name, sw->description);
-               sw = sw->next;
        }
        ast_mutex_unlock(&switchlock);
        return RESULT_SUCCESS;
@@ -3165,7 +3110,7 @@ static int handle_show_switches(int fd, int argc, char *argv[])
 static int handle_show_applications(int fd, int argc, char *argv[])
 {
        struct ast_app *a;
-       int like=0, describing=0;
+       int like = 0, describing = 0;
        int total_match = 0;    /* Number of matches in like clause */
        int total_apps = 0;     /* Number of apps registered */
        
@@ -3211,7 +3156,7 @@ static int handle_show_applications(int fd, int argc, char *argv[])
                                /* Match all words on command line */
                                int i;
                                printapp = 1;
-                               for (i=3; i<argc; i++) {
+                               for (i = 3; i < argc; i++) {
                                        if (!strcasestr(a->description, argv[i])) {
                                                printapp = 0;
                                        } else {
@@ -3276,9 +3221,11 @@ static char *complete_show_dialplan_context(char *line, char *word, int pos,
 {
        struct ast_context *c;
        int which = 0;
+       int wordlen;
 
        /* we are do completion of [exten@]context on second position only */
-       if (pos != 2) return NULL;
+       if (pos != 2)
+               return NULL;
 
        /* try to lock contexts list ... */
        if (ast_lock_contexts()) {
@@ -3286,11 +3233,12 @@ static char *complete_show_dialplan_context(char *line, char *word, int pos,
                return NULL;
        }
 
+       wordlen = strlen(word);
+
        /* ... walk through all contexts ... */
-       c = ast_walk_contexts(NULL);
-       while(c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* ... word matches context name? yes? ... */
-               if (!strncasecmp(word, ast_get_context_name(c), strlen(word))) {
+               if (!strncasecmp(word, ast_get_context_name(c), wordlen)) {
                        /* ... for serve? ... */
                        if (++which > state) {
                                /* ... yes, serve this context name ... */
@@ -3299,7 +3247,6 @@ static char *complete_show_dialplan_context(char *line, char *word, int pos,
                                return ret;
                        }
                }
-               c = ast_walk_contexts(c);
        }
 
        /* ... unlock and return */
@@ -3318,7 +3265,7 @@ struct dialplan_counters {
 static int show_dialplan_helper(int fd, char *context, char *exten, struct dialplan_counters *dpc, struct ast_include *rinclude, int includecount, char *includes[])
 {
        struct ast_context *c;
-       int res=0, old_total_exten = dpc->total_exten;
+       int res = 0, old_total_exten = dpc->total_exten;
 
        /* try to lock contexts */
        if (ast_lock_contexts()) {
@@ -3327,7 +3274,7 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
        }
 
        /* walk all contexts ... */
-       for (c = ast_walk_contexts(NULL); c ; c = ast_walk_contexts(c)) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* show this context? */
                if (!context ||
                        !strcmp(ast_get_context_name(c), context)) {
@@ -3465,7 +3412,7 @@ static int show_dialplan_helper(int fd, char *context, char *exten, struct dialp
                                }
 
                                /* walk ignore patterns and write info ... */
-                               for (ip=ast_walk_context_ignorepats(c, NULL); ip; ip=ast_walk_context_ignorepats(c, ip)) {
+                               for (ip = ast_walk_context_ignorepats(c, NULL); ip; ip = ast_walk_context_ignorepats(c, ip)) {
                                        const char *ipname = ast_get_ignorepat_name(ip);
                                        char ignorepat[AST_MAX_EXTENSION];
                                        snprintf(buf, sizeof(buf), "'%s'", ipname);
@@ -3593,8 +3540,7 @@ int ast_unregister_application(const char *app)
                ast_log(LOG_ERROR, "Unable to lock application list\n");
                return -1;
        }
-       tmp = apps;
-       while(tmp) {
+       for (tmp = apps; tmp; tmp = tmp->next) {
                if (!strcasecmp(app, tmp->name)) {
                        if (tmpl)
                                tmpl->next = tmp->next;
@@ -3607,7 +3553,6 @@ int ast_unregister_application(const char *app)
                        return 0;
                }
                tmpl = tmp;
-               tmp = tmp->next;
        }
        ast_mutex_unlock(&applock);
        return -1;
@@ -3625,8 +3570,7 @@ struct ast_context *ast_context_create(struct ast_context **extcontexts, const c
        } else
                local_contexts = extcontexts;
 
-       tmp = *local_contexts;
-       while(tmp) {
+       for (tmp = *local_contexts; tmp; tmp = tmp->next) {
                if (!strcasecmp(tmp->name, name)) {
                        ast_mutex_unlock(&conlock);
                        ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
@@ -3634,11 +3578,9 @@ struct ast_context *ast_context_create(struct ast_context **extcontexts, const c
                                ast_mutex_unlock(&conlock);
                        return NULL;
                }
-               tmp = tmp->next;
        }
-       tmp = malloc(length);
+       tmp = calloc(1, length);
        if (tmp) {
-               memset(tmp, 0, length);
                ast_mutex_init(&tmp->lock);
                strcpy(tmp->name, name);
                tmp->root = NULL;
@@ -3779,8 +3721,7 @@ int ast_context_add_include(const char *context, const char *include, const char
        }
 
        /* walk contexts ... */
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* ... search for the right one ... */
                if (!strcmp(ast_get_context_name(c), context)) {
                        int ret = ast_context_add_include2(c, include, registrar);
@@ -3788,7 +3729,6 @@ int ast_context_add_include(const char *context, const char *include, const char
                        ast_unlock_contexts();
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        /* we can't find the right context */
@@ -3829,7 +3769,7 @@ static void get_timerange(struct ast_timing *i, char *times)
        }
        *e = '\0';
        e++;
-       while(*e && !isdigit(*e)) 
+       while (*e && !isdigit(*e)) 
                e++;
        if (!*e) {
                ast_log(LOG_WARNING, "Invalid time range.  Assuming no restrictions based on time.\n");
@@ -4147,14 +4087,13 @@ int ast_context_add_include2(struct ast_context *con, const char *value,
        length += 2 * (strlen(value) + 1);
 
        /* allocate new include structure ... */
-       if (!(new_include = malloc(length))) {
+       if (!(new_include = calloc(1, length))) {
                ast_log(LOG_ERROR, "Out of memory\n");
                errno = ENOMEM;
                return -1;
        }
        
        /* ... fill in this structure ... */
-       memset(new_include, 0, length);
        p = new_include->stuff;
        new_include->name = p;
        strcpy(new_include->name, value);
@@ -4181,8 +4120,7 @@ int ast_context_add_include2(struct ast_context *con, const char *value,
        }
 
        /* ... go to last include and check if context is already included too... */
-       i = con->includes;
-       while (i) {
+       for (i = con->includes; i; i = i->next) {
                if (!strcasecmp(i->name, new_include->name)) {
                        free(new_include);
                        ast_mutex_unlock(&con->lock);
@@ -4190,7 +4128,6 @@ int ast_context_add_include2(struct ast_context *con, const char *value,
                        return -1;
                }
                il = i;
-               i = i->next;
        }
 
        /* ... include new context into context list, unlock, return */
@@ -4220,8 +4157,7 @@ int ast_context_add_switch(const char *context, const char *sw, const char *data
        }
 
        /* walk contexts ... */
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                /* ... search for the right one ... */
                if (!strcmp(ast_get_context_name(c), context)) {
                        int ret = ast_context_add_switch2(c, sw, data, eval, registrar);
@@ -4229,7 +4165,6 @@ int ast_context_add_switch(const char *context, const char *sw, const char *data
                        ast_unlock_contexts();
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        /* we can't find the right context */
@@ -4265,14 +4200,13 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
        }
 
        /* allocate new sw structure ... */
-       if (!(new_sw = malloc(length))) {
+       if (!(new_sw = calloc(1, length))) {
                ast_log(LOG_ERROR, "Out of memory\n");
                errno = ENOMEM;
                return -1;
        }
        
        /* ... fill in this structure ... */
-       memset(new_sw, 0, length);
        p = new_sw->stuff;
        new_sw->name = p;
        strcpy(new_sw->name, value);
@@ -4299,8 +4233,7 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
        }
 
        /* ... go to last sw and check if context is already swd too... */
-       i = con->alts;
-       while (i) {
+       for (i = con->alts; i; i = i->next) {
                if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
                        free(new_sw);
                        ast_mutex_unlock(&con->lock);
@@ -4308,7 +4241,6 @@ int ast_context_add_switch2(struct ast_context *con, const char *value,
                        return -1;
                }
                il = i;
-               i = i->next;
        }
 
        /* ... sw new context into context list, unlock, return */
@@ -4336,14 +4268,12 @@ int ast_context_remove_ignorepat(const char *context, const char *ignorepat, con
                return -1;
        }
 
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                if (!strcmp(ast_get_context_name(c), context)) {
                        int ret = ast_context_remove_ignorepat2(c, ignorepat, registrar);
                        ast_unlock_contexts();
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        ast_unlock_contexts();
@@ -4360,8 +4290,7 @@ int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat
                return -1;
        }
 
-       ip = con->ignorepats;
-       while (ip) {
+       for (ip = con->ignorepats; ip; ip = ip->next) {
                if (!strcmp(ip->pattern, ignorepat) &&
                        (!registrar || (registrar == ip->registrar))) {
                        if (ipl) {
@@ -4374,7 +4303,7 @@ int ast_context_remove_ignorepat2(struct ast_context *con, const char *ignorepat
                        ast_mutex_unlock(&con->lock);
                        return 0;
                }
-               ipl = ip; ip = ip->next;
+               ipl = ip;
        }
 
        ast_mutex_unlock(&con->lock);
@@ -4395,14 +4324,12 @@ int ast_context_add_ignorepat(const char *con, const char *value, const char *re
                return -1;
        }
 
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                if (!strcmp(ast_get_context_name(c), con)) {
                        int ret = ast_context_add_ignorepat2(c, value, registrar);
                        ast_unlock_contexts();
                        return ret;
                } 
-               c = ast_walk_contexts(c);
        }
 
        ast_unlock_contexts();
@@ -4416,19 +4343,17 @@ int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const
        int length;
        length = sizeof(struct ast_ignorepat);
        length += strlen(value) + 1;
-       ignorepat = malloc(length);
+       ignorepat = calloc(1, length);
        if (!ignorepat) {
                ast_log(LOG_ERROR, "Out of memory\n");
                errno = ENOMEM;
                return -1;
        }
-       memset(ignorepat, 0, length);
        strcpy(ignorepat->pattern, value);
        ignorepat->next = NULL;
        ignorepat->registrar = registrar;
        ast_mutex_lock(&con->lock);
-       ignorepatc = con->ignorepats;
-       while(ignorepatc) {
+       for (ignorepatc = con->ignorepats; ignorepatc; ignorepatc = ignorepatc->next) {
                ignorepatl = ignorepatc;
                if (!strcasecmp(ignorepatc->pattern, value)) {
                        /* Already there */
@@ -4436,7 +4361,6 @@ int ast_context_add_ignorepat2(struct ast_context *con, const char *value, const
                        errno = EEXIST;
                        return -1;
                }
-               ignorepatc = ignorepatc->next;
        }
        if (ignorepatl) 
                ignorepatl->next = ignorepat;
@@ -4454,11 +4378,9 @@ int ast_ignore_pattern(const char *context, const char *pattern)
 
        con = ast_context_find(context);
        if (con) {
-               pat = con->ignorepats;
-               while (pat) {
+               for (pat = con->ignorepats; pat; pat = pat->next) {
                        if (ast_extension_match(pat->pattern, pattern))
                                return 1;
-                       pat = pat->next;
                }
        } 
        return 0;
@@ -4479,15 +4401,13 @@ int ast_add_extension(const char *context, int replace, const char *extension, i
                return -1;
        }
 
-       c = ast_walk_contexts(NULL);
-       while (c) {
+       for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
                if (!strcmp(context, ast_get_context_name(c))) {
                        int ret = ast_add_extension2(c, replace, extension, priority, label, callerid,
                                application, data, datad, registrar);
                        ast_unlock_contexts();
                        return ret;
                }
-               c = ast_walk_contexts(c);
        }
 
        ast_unlock_contexts();
@@ -4580,7 +4500,7 @@ static int ext_strncpy(char *dst, const char *src, int len)
 {
        int count=0;
 
-       while(*src && (count < len - 1)) {
+       while (*src && (count < len - 1)) {
                switch(*src) {
                case ' ':
                        /*      otherwise exten => [a-b],1,... doesn't work */
@@ -4661,9 +4581,8 @@ int ast_add_extension2(struct ast_context *con,
        /* Be optimistic:  Build the extension structure first */
        if (datad == NULL)
                datad = null_datad;
-       tmp = malloc(length);
+       tmp = calloc(1, length);
        if (tmp) {
-               memset(tmp, 0, length);
                p = tmp->stuff;
                if (label) {
                        tmp->label = p;
@@ -4703,8 +4622,7 @@ int ast_add_extension2(struct ast_context *con,
                errno = EBUSY;
                return -1;
        }
-       e = con->root;
-       while(e) {
+       for (e = con->root; e; e = e->next) {
                /* Make sure patterns are always last! */
                if ((e->exten[0] != '_') && (extension[0] == '_'))
                        res = -1;
@@ -4827,7 +4745,6 @@ int ast_add_extension2(struct ast_context *con,
                }                       
                        
                el = e;
-               e = e->next;
        }
        /* If we fall all the way through to here, then we need to be on the end. */
        if (el)
@@ -4861,7 +4778,7 @@ static void *async_wait(void *data)
        struct ast_frame *f;
        struct ast_app *app;
        
-       while(timeout && (chan->_state != AST_STATE_UP)) {
+       while (timeout && (chan->_state != AST_STATE_UP)) {
                res = ast_waitfor(chan, timeout);
                if (res < 1) 
                        break;
@@ -4919,7 +4836,7 @@ int ast_pbx_outgoing_cdr_failed(void)
 {
        /* allocate a channel */
        struct ast_channel *chan = ast_channel_alloc(0);
-       if(!chan) {
+       if (!chan) {
                /* allocation of the channel failed, let some peeps know */
                ast_log(LOG_WARNING, "Unable to allocate channel structure for CDR record\n");
                return -1;  /* failure */
@@ -4927,7 +4844,7 @@ int ast_pbx_outgoing_cdr_failed(void)
 
        chan->cdr = ast_cdr_alloc();   /* allocate a cdr for the channel */
 
-       if(!chan->cdr) {
+       if (!chan->cdr) {
                /* allocation of the cdr failed */
                ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
                ast_channel_free(chan);   /* free the channel */
@@ -5455,7 +5372,7 @@ static int pbx_builtin_setamaflags(struct ast_channel *chan, void *data)
 {
        /* Copy the AMA Flags as specified */
        if (data)
-               ast_cdr_setamaflags(chan, (char *)data);
+               ast_cdr_setamaflags(chan, data);
        else
                ast_cdr_setamaflags(chan, "");
        return 0;
@@ -5486,7 +5403,7 @@ static int pbx_builtin_gotoiftime(struct ast_channel *chan, void *data)
                return -1;
        }
 
-       if ((s = ast_strdupa((char *) data))) {
+       if ((s = ast_strdupa(data))) {
                ts = s;
 
                /* Separate the Goto path */