[asterisk-commits] trunk - r7618 in /trunk: include/asterisk/pbx.h pbx.c

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Dec 24 00:23:38 CST 2005


Author: russell
Date: Sat Dec 24 00:23:36 2005
New Revision: 7618

URL: http://svn.digium.com/view/asterisk?rev=7618&view=rev
Log:
minor cleanups ...
- 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 

Modified:
    trunk/include/asterisk/pbx.h
    trunk/pbx.c

Modified: trunk/include/asterisk/pbx.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/pbx.h?rev=7618&r1=7617&r2=7618&view=diff
==============================================================================
--- trunk/include/asterisk/pbx.h (original)
+++ trunk/include/asterisk/pbx.h Sat Dec 24 00:23:36 2005
@@ -626,7 +626,7 @@
 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);
 

Modified: trunk/pbx.c
URL: http://svn.digium.com/view/asterisk/trunk/pbx.c?rev=7618&r1=7617&r2=7618&view=diff
==============================================================================
--- trunk/pbx.c (original)
+++ trunk/pbx.c Sat Dec 24 00:23:36 2005
@@ -102,45 +102,46 @@
 
 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 @@
 /*! \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 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(hintlock);		/* Lock for extension state notifys */
+AST_MUTEX_DEFINE_STATIC(switchlock);		/*!< Lock for switches */
+
+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 @@
 		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 @@
 		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 *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 @@
 		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 @@
 		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 @@
 								*foundcontext = context;
 								return e;
 							}
-							e = e->peer;
 						}
 					}
 				}
@@ -784,8 +777,7 @@
 				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 @@
 				} 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 @@
 	*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 @@
 {
 	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 @@
 		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 @@
 				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 @@
 	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 @@
 
 	/* 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 @@
 	}
 
 	/* 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 @@
 	/* 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 @@
 				return 0;
 	    		}
 	    		cbprev = cblist;
-	    		cblist = cblist->next;
 		}
 
 		ast_mutex_lock(&hintlock);
@@ -2021,11 +2003,9 @@
 
 	/* 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 @@
 				return 0;		
 	    		}		
 	    		cbprev = cblist;				
-	    		cblist = cblist->next;
-		}
-		list = list->next;
+		}
 	}
 
 	ast_mutex_unlock(&hintlock);
@@ -2056,23 +2034,21 @@
 		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 @@
 		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 @@
 	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 @@
 
 	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 @@
 	    
 			ast_mutex_unlock(&hintlock);
 			return 0;
-		} else {
-			prev = list;
-			list = list->next;    
-		}
+		}
+		prev = list;
 	}
 
 	ast_mutex_unlock(&hintlock);
@@ -2171,7 +2142,7 @@
 			if (tmp)
 				ast_copy_string(name, (char *) tmp, namesize);
 		}
-	    return -1;
+		return -1;
 	}
 	return 0;	
 }
@@ -2224,7 +2195,7 @@
 	/* 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 @@
 			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 @@
 	}
 	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 @@
 	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 @@
 			/* ... return results */
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right one context */
@@ -2597,8 +2565,7 @@
 	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 @@
 			return 0;
 		}
 		pi = i;
-		i = i->next;
 	}
 
 	/* we can't find the right include */
@@ -2633,8 +2599,7 @@
 	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 @@
 			/* ... return results */
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right one context */
@@ -2669,8 +2633,7 @@
 	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 @@
 			return 0;
 		}
 		pi = i;
-		i = i->next;
 	}
 
 	/* we can't find the right switch */
@@ -2705,8 +2667,7 @@
 	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 @@
 			ast_unlock_contexts();
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right context */
@@ -2850,43 +2810,40 @@
 		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 @@
 		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 @@
 		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 @@
 			break;			
 		}
 		prev = tmp;
-		tmp = tmp->next;
 	}
 	ast_mutex_unlock(&switchlock);
 }
@@ -3005,8 +2958,7 @@
 	}
 
 	/* ... 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 @@
 				return ret;
 			}
 		}
-		a = a->next; 
 	}
 
 	/* no application match */
@@ -3038,8 +2989,7 @@
 	}
 
 	/* ... 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 @@
 				}
 			}
 		}
-		a = a->next; 
 	}
 
 	ast_mutex_unlock(&applock);
@@ -3119,8 +3068,7 @@
 		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 @@
 			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 @@
 		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_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 @@
 				/* 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 @@
 {
 	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 @@
 		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 @@
 				return ret;
 			}
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* ... unlock and return */
@@ -3318,7 +3265,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;
-	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 @@
 	}
 
 	/* 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 @@
 				}
 
 				/* 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 @@
 		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 @@
 			return 0;
 		}
 		tmpl = tmp;
-		tmp = tmp->next;
 	}
 	ast_mutex_unlock(&applock);
 	return -1;
@@ -3625,8 +3570,7 @@
 	} 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 @@
 				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 @@
 	}
 
 	/* 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 @@
 			ast_unlock_contexts();
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right context */
@@ -3829,7 +3769,7 @@
 	}
 	*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 @@
 	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 @@
 	}
 
 	/* ... 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 @@
 			return -1;
 		}
 		il = i;
-		i = i->next;
 	}
 
 	/* ... include new context into context list, unlock, return */
@@ -4220,8 +4157,7 @@
 	}
 
 	/* 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 @@
 			ast_unlock_contexts();
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right context */
@@ -4265,14 +4200,13 @@
 	}
 
 	/* 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 @@
 	}
 
 	/* ... 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 @@
 			return -1;
 		}
 		il = i;
-		i = i->next;
 	}
 
 	/* ... sw new context into context list, unlock, return */
@@ -4336,14 +4268,12 @@
 		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 @@
 		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 @@
 			ast_mutex_unlock(&con->lock);
 			return 0;
 		}
-		ipl = ip; ip = ip->next;
+		ipl = ip;
 	}
 
 	ast_mutex_unlock(&con->lock);
@@ -4395,14 +4324,12 @@
 		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 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 @@
 			errno = EEXIST;
 			return -1;
 		}
-		ignorepatc = ignorepatc->next;
 	}
 	if (ignorepatl) 
 		ignorepatl->next = ignorepat;
@@ -4454,11 +4378,9 @@
 
 	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 @@
 		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 @@
 {
 	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 @@
 	/* 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 @@
 		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 @@
 		}			
 			
 		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 @@
 	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 @@
 {
 	/* 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 @@
 
 	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 @@
 {
 	/* 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 @@
 		return -1;
 	}
 
-	if ((s = ast_strdupa((char *) data))) {
+	if ((s = ast_strdupa(data))) {
 		ts = s;
 
 		/* Separate the Goto path */



More information about the asterisk-commits mailing list