Index: pbx.c
===================================================================
--- pbx.c	(revision 7635)
+++ pbx.c	(working copy)
@@ -2541,12 +2533,13 @@
  */
 int ast_context_remove_include(const char *context, const char *include, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
-	if (ast_lock_contexts()) return -1;
+	if (ast_lock_contexts())
+		return -1;
 
 	/* walk contexts and search for the right one ...*/
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (c = ast_walk_contexts(c)) ) {
 		/* we found one ... */
 		if (!strcmp(ast_get_context_name(c), context)) {
 			int ret;
@@ -2609,28 +2602,25 @@
  */
 int ast_context_remove_switch(const char *context, const char *sw, const char *data, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
+	int ret = -1; /* default error return */
 
-	if (ast_lock_contexts()) return -1;
+	if (ast_lock_contexts())
+		return -1;
 
 	/* walk contexts and search for the right one ...*/
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (c = ast_walk_contexts(c)) ) {
 		/* we found one ... */
 		if (!strcmp(ast_get_context_name(c), context)) {
-			int ret;
 			/* remove switch from this context ... */	
 			ret = ast_context_remove_switch2(c, sw, data, registrar);
-
-			ast_unlock_contexts();
-
-			/* ... return results */
-			return ret;
+			break;
 		}
 	}
 
-	/* we can't find the right one context */
+	/* found or error */
 	ast_unlock_contexts();
-	return -1;
+	return ret;
 }
 
 /*!
@@ -2677,26 +2667,25 @@
  */
 int ast_context_remove_extension(const char *context, const char *extension, int priority, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
+	int ret = -1; /* default error return */
 
-	if (ast_lock_contexts()) return -1;
+	if (ast_lock_contexts())
+		return -1;
 
 	/* walk contexts ... */
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (c = ast_walk_contexts(c)) ) {
 		/* ... search for the right one ... */
 		if (!strcmp(ast_get_context_name(c), context)) {
 			/* ... remove extension ... */
-			int ret = ast_context_remove_extension2(c, extension, priority,
+			ret = ast_context_remove_extension2(c, extension, priority,
 				registrar);
-			/* ... unlock contexts list and return */
-			ast_unlock_contexts();
-			return ret;
+			break;
 		}
 	}
-
-	/* we can't find the right context */
+	/* found or error */
 	ast_unlock_contexts();
-	return -1;
+	return ret;
 }
 
 /*!
@@ -3234,7 +3223,8 @@
 static char *complete_show_dialplan_context(char *line, char *word, int pos,
 	int state)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
+	char *ret = NULL;
 	int which = 0;
 	int wordlen;
 
@@ -3251,22 +3241,21 @@
 	wordlen = strlen(word);
 
 	/* ... walk through all contexts ... */
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (c = ast_walk_contexts(c)) ) {
 		/* ... word matches context name? yes? ... */
 		if (!strncasecmp(word, ast_get_context_name(c), wordlen)) {
 			/* ... for serve? ... */
 			if (++which > state) {
 				/* ... yes, serve this context name ... */
-				char *ret = strdup(ast_get_context_name(c));
-				ast_unlock_contexts();
-				return ret;
+				ret = strdup(ast_get_context_name(c));
+				break;
 			}
 		}
 	}
 
 	/* ... unlock and return */
 	ast_unlock_contexts();
-	return NULL;
+	return ret;
 }
 
 struct dialplan_counters {
@@ -3279,7 +3268,7 @@
 
 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;
+	struct ast_context *c = NULL;
 	int res = 0, old_total_exten = dpc->total_exten;
 
 	/* try to lock contexts */
@@ -3289,7 +3278,7 @@
 	}
 
 	/* walk all contexts ... */
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (c = ast_walk_contexts(c)) ) {
 		/* show this context? */
 		if (!context ||
 			!strcmp(ast_get_context_name(c), context)) {
@@ -3728,7 +3717,7 @@
  */
 int ast_context_add_include(const char *context, const char *include, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
 	if (ast_lock_contexts()) {
 		errno = EBUSY;
@@ -3736,7 +3725,7 @@
 	}
 
 	/* walk contexts ... */
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (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);
@@ -4164,7 +4153,7 @@
  */
 int ast_context_add_switch(const char *context, const char *sw, const char *data, int eval, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
 	if (ast_lock_contexts()) {
 		errno = EBUSY;
@@ -4172,7 +4161,7 @@
 	}
 
 	/* walk contexts ... */
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (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);
@@ -4276,14 +4265,14 @@
  */
 int ast_context_remove_ignorepat(const char *context, const char *ignorepat, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
 	if (ast_lock_contexts()) {
 		errno = EBUSY;
 		return -1;
 	}
 
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (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();
@@ -4332,14 +4321,14 @@
  */
 int ast_context_add_ignorepat(const char *con, const char *value, const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
 	if (ast_lock_contexts()) {
 		errno = EBUSY;
 		return -1;
 	}
 
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (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();
@@ -4409,14 +4398,14 @@
 int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid,
 	const char *application, void *data, void (*datad)(void *), const char *registrar)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 
 	if (ast_lock_contexts()) {
 		errno = EBUSY;
 		return -1;
 	}
 
-	for (c = ast_walk_contexts(NULL); c; c = ast_walk_contexts(c)) {
+	while ( (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);
@@ -6134,10 +6123,7 @@
  */
 struct ast_context *ast_walk_contexts(struct ast_context *con)
 {
-	if (!con)
-		return contexts;
-	else
-		return con->next;
+	return con ? con->next : contexts;
 }
 
 struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
@@ -6161,10 +6147,7 @@
 struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
 	struct ast_exten *priority)
 {
-	if (!priority)
-		return exten;
-	else
-		return priority->peer;
+	return priority ? priority->peer : exten;
 }
 
 struct ast_include *ast_walk_context_includes(struct ast_context *con,
@@ -6187,10 +6170,10 @@
 
 int ast_context_verify_includes(struct ast_context *con)
 {
-	struct ast_include *inc;
+	struct ast_include *inc = NULL;
 	int res = 0;
 
-	for (inc = ast_walk_context_includes(con, NULL); inc; inc = ast_walk_context_includes(con, inc))
+	while ( (inc = ast_walk_context_includes(con, inc)) )
 		if (!ast_context_find(inc->rname)) {
 			res = -1;
 			ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n",
