Index: pbx.c
===================================================================
RCS file: /usr/cvsroot/asterisk/pbx.c,v
retrieving revision 1.256
diff -u -p -r1.256 pbx.c
--- pbx.c	20 Jun 2005 23:37:01 -0000	1.256
+++ pbx.c	27 Jun 2005 18:26:29 -0000
@@ -591,6 +591,19 @@ struct ast_app *pbx_findapp(const char *
 	return tmp;
 }
 
+/*
+ * copy the string at src into *dst, returns the original *dst and updates *dst
+ * to point after the copy.
+ * We assume that *dst has sufficient space for the string and terminator.
+ */
+static char *str_add(char **dst, const char *src)
+{
+	char *ret = *dst;
+	strcpy(ret, src);
+	*dst += strlen(ret) + 1;
+	return ret;
+}
+
 static struct ast_switch *pbx_findswitch(const char *sw)
 {
 	struct ast_switch *asw;
@@ -916,43 +929,37 @@ void pbx_retrieve_variable(struct ast_ch
 		headp=&c->varshead;
 	*ret=NULL;
 	if ((first=strchr(var,':'))) {	/* : Remove characters counting from end or start of string */
+		int lr;	/* cache repeated calls to strlen(*ret) */
+
 		ast_copy_string(tmpvar, var, sizeof(tmpvar));
+		/* XXX don't understand this */
 		first = strchr(tmpvar, ':');
 		if (!first)
-			first = tmpvar + strlen(tmpvar);
+			first = tmpvar + strlen(tmpvar);	/* basically empty string */
 		*first='\0';
 		pbx_retrieve_variable(c,tmpvar,ret,workspace,workspacelen - 1, headp);
-		if (!(*ret)) 
+		if (*ret == NULL) 
 			return;
+		lr = strlen(*ret);
 		offset=atoi(first+1);	/* The number of characters, 
 					   positive: remove # of chars from start
 					   negative: keep # of chars from end */
-						
 	 	if ((second=strchr(first+1,':'))) {	
 			*second='\0';
 			offset2 = atoi(second+1);		/* Number of chars to copy */
 		} else if (offset >= 0) {
-			offset2 = strlen(*ret)-offset;	/* Rest of string */
+			offset2 = lr-offset;	/* Rest of string */
 		} else {
 			offset2 = abs(offset);
 		}
 
-		if (abs(offset) > strlen(*ret)) {	/* Offset beyond string */
-			if (offset >= 0) 
-				offset=strlen(*ret);
-			else 
-				offset=-strlen(*ret);	
-		}
-		if ((offset < 0 && offset2 > -offset) || (offset >= 0 && offset+offset2 > strlen(*ret))) {
-			if (offset >= 0) 
-				offset2=strlen(*ret)-offset;
-			else 
-				offset2=strlen(*ret)+offset;
+		if (abs(offset) > lr) {	/* Offset beyond string */
+			offset = (offset >= 0) ? lr : -lr;
 		}
-		if (offset >= 0)
-			*ret += offset;
-		else
-			*ret += strlen(*ret)+offset;
+		if ((offset < 0 && offset2 > -offset) || (offset >= 0 && offset+offset2 > lr)) {
+			offset = lr + ( (offset >= 0) ? -offset : offset );
+		}
+		*ret += offset + ( (offset >= 0) ? 0 : lr);
 		(*ret)[offset2] = '\0';		/* Cut at offset2 position */
 	} else if (c && !strncmp(var, "CALL", 4)) {
 		if (!strncmp(var + 4, "ER", 2)) {
@@ -1136,6 +1143,16 @@ static int handle_show_functions(int fd,
 	return 0;
 }
 
+/* returns the size of a GUI string */
+static const char *gui_string(const char *s)
+{
+	return (s ? s : "Not available");
+}
+static int gui_size(const char *s)
+{
+	return 23 + gui_size(s);
+}
+
 static int handle_show_function(int fd, int argc, char *argv[])
 {
 	struct ast_custom_function *acf;
@@ -1153,22 +1170,13 @@ static int handle_show_function(int fd, 
 
 	}
 
-	if (acf->synopsis)
-		synopsis_size = strlen(acf->synopsis) + 23;
-	else
-		synopsis_size = strlen("Not available") + 23;
+	synopsis_size = gui_size(acf->synopsis);
 	synopsis = alloca(synopsis_size);
 	
-	if (acf->desc)
-		description_size = strlen(acf->desc) + 23;
-	else
-		description_size = strlen("Not available") + 23;
+	description_size = gui_size(acf->desc);
 	description = alloca(description_size);
 
-	if (acf->syntax)
-		syntax_size = strlen(acf->syntax) + 23;
-	else
-		syntax_size = strlen("Not available") + 23;
+	syntax_size = gui_size(acf->syntax);
 	syntax = alloca(syntax_size);
 
 	snprintf(info, 64 + AST_MAX_APP, "\n  -= Info about function '%s' =- \n\n", acf->name);
@@ -1176,15 +1184,9 @@ static int handle_show_function(int fd, 
 	term_color(stxtitle, "[Syntax]\n", COLOR_MAGENTA, 0, 40);
 	term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
 	term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
-	term_color(syntax,
-		   acf->syntax ? acf->syntax : "Not available",
-		   COLOR_CYAN, 0, syntax_size);
-	term_color(synopsis,
-		   acf->synopsis ? acf->synopsis : "Not available",
-		   COLOR_CYAN, 0, synopsis_size);
-	term_color(description,
-		   acf->desc ? acf->desc : "Not available",
-		   COLOR_CYAN, 0, description_size);
+	term_color(syntax, gui_string(acf->syntax), COLOR_CYAN, 0, syntax_size);
+	term_color(synopsis, gui_string(acf->synopsis), COLOR_CYAN, 0, synopsis_size);
+	term_color(description, gui_string(acf->desc), COLOR_CYAN, 0, description_size);
 	
 	ast_cli(fd,"%s%s%s\n\n%s%s\n\n%s%s\n", infotitle, stxtitle, syntax, syntitle, synopsis, destitle, description);
 
@@ -1195,6 +1197,7 @@ static char *complete_show_function(char
 {
 	struct ast_custom_function *acf;
 	int which = 0;
+	char *ret = NULL;
 
 	/* try to lock functions list ... */
 	if (ast_mutex_lock(&acflock)) {
@@ -1204,18 +1207,16 @@ static char *complete_show_function(char
 
 	acf = acf_root;
 	while (acf) {
-		if (!strncasecmp(word, acf->name, strlen(word))) {
-			if (++which > state) {
-				char *ret = strdup(acf->name);
-				ast_mutex_unlock(&acflock);
-				return ret;
-			}
+		if (!strncasecmp(word, acf->name, strlen(word)) &&
+			++which > state) {
+				ret = strdup(acf->name);
+				break;
 		}
 		acf = acf->next; 
 	}
 
 	ast_mutex_unlock(&acflock);
-	return NULL; 
+	return ret; 
 }
 
 struct ast_custom_function* ast_custom_function_find(char *name) 
@@ -1311,8 +1312,7 @@ char *ast_func_read(struct ast_channel *
 	function = ast_strdupa(in);
 	if (function) {
 		if ((args = strchr(function, '('))) {
-			*args = '\0';
-			args++;
+			*args++ = '\0';
 			if ((p = strrchr(args, ')'))) {
 				*p = '\0';
 			} else {
@@ -1346,8 +1346,7 @@ void ast_func_write(struct ast_channel *
 	function = ast_strdupa(in);
 	if (function) {
 		if ((args = strchr(function, '('))) {
-			*args = '\0';
-			args++;
+			*args++ = '\0';
 			if ((p = strrchr(args, ')'))) {
 				*p = '\0';
 			} else {
@@ -1446,7 +1445,6 @@ static void pbx_substitute_variables_hel
 			whereweare += ( len + 3);
 
 			/* Store variable name (and truncate) */
-			memset(var, 0, sizeof(var));
 			ast_copy_string(var, vars, sizeof(var));
 			var[len] = '\0';
 
@@ -1510,7 +1508,6 @@ static void pbx_substitute_variables_hel
 			whereweare += ( len + 3);
 			
 			/* Store variable name (and truncate) */
-			memset(var, 0, sizeof(var));
 			ast_copy_string(var, vars, sizeof(var));
 			var[len] = '\0';
 			
@@ -1746,14 +1743,10 @@ static int ast_extension_state2(struct a
 
 	ast_copy_string(hint, ast_get_extension_app(e), sizeof(hint));
 
-	cur = hint;    	/* On or more devices separated with a & character */
-	do {
-		rest = strchr(cur, '&');
-		if (rest) {
-	    		*rest = 0;
-			rest++;
-		}
-	
+	rest = hint;    	/* On or more devices separated with a & character */
+	while (rest) {
+		cur = strsep(&rest, "&");
+
 		res = ast_device_state(cur);
 		switch (res) {
 		case AST_DEVICE_NOT_INUSE:
@@ -1777,8 +1770,7 @@ static int ast_extension_state2(struct a
 			allbusy = 0;
 			allfree = 0;
 		}
-		cur = rest;
-	} while (cur);
+	}
 
 	if (allfree)			
 		return AST_EXTENSION_NOT_INUSE;
@@ -1823,11 +1815,8 @@ int ast_device_state_changed(const char 
 	vsnprintf(device, sizeof(device), fmt, ap);
 	va_end(ap);
 
-	rest = strchr(device, '-');
-	if (rest) {
-		*rest = 0;
-	}
-
+	rest = device;
+	strsep(&rest, "-");
 
 	state = ast_device_state(device);
 	if (option_debug > 2)
@@ -1849,13 +1838,9 @@ int ast_device_state_changed(const char 
 	while (list) {
 
 		ast_copy_string(hint, ast_get_extension_app(list->exten), sizeof(hint));
-		cur = hint;
-		do {
-			rest = strchr(cur, '&');
-			if (rest) {
-				*rest = 0;
-				rest++;
-			}
+		rest = hint;
+		while (rest) {
+			cur = strsep(&rest, "&");
 			
 			if (!strcmp(cur, device)) {	/* Is this device referred to in this hint? */
 
@@ -1883,8 +1868,7 @@ int ast_device_state_changed(const char 
 				}
 				break;
 			}
-			cur = rest;
-		} while (cur);
+		}
 		list = list->next;
 	}
 	ast_mutex_unlock(&hintlock);
@@ -2124,19 +2108,14 @@ static int ast_change_hint(struct ast_ex
 	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;
+			break;
 		}
-		list = list->next;
-	}
 	ast_mutex_unlock(&hintlock);
 
-	return -1;
+	return (list ? 0 /* match */ : -1);
 }
 
 /*--- ast_remove_hint: Remove hint from extension */
@@ -2673,8 +2652,7 @@ int ast_context_remove_switch2(struct as
 				con->alts = i->next;
 			/* free switch and return */
 			free(i);
-			ast_mutex_unlock(&con->lock);
-			return 0;
+			break; /* can't dereference i but it is still non-null on success */
 		}
 		pi = i;
 		i = i->next;
@@ -2682,6 +2660,7 @@ int ast_context_remove_switch2(struct as
 
 	/* we can't find the right switch */
 	ast_mutex_unlock(&con->lock);
+	return (i ? 0 : -1 );
 	return -1;
 }
 
@@ -2692,28 +2671,24 @@ int ast_context_remove_switch2(struct as
  */
 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;
 
 	if (ast_lock_contexts()) return -1;
 
 	/* walk contexts ... */
-	c = ast_walk_contexts(NULL);
-	while (c) {
+	while ( (c = ast_walk_contexts(c)) != NULL) {
 		/* ... 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;
 		}
-		c = ast_walk_contexts(c);
 	}
-
-	/* we can't find the right context */
 	ast_unlock_contexts();
-	return -1;
+	return ret;
 }
 
 /*
@@ -2988,6 +2963,7 @@ static char *complete_show_application(c
 {
 	struct ast_app *a;
 	int which = 0;
+	char *ret = NULL;
 
 	/* try to lock applications list ... */
 	if (ast_mutex_lock(&applock)) {
@@ -2999,20 +2975,17 @@ static char *complete_show_application(c
 	a = apps; 
 	while (a) {
 		/* ... check if word matches this application ... */
-		if (!strncasecmp(word, a->name, strlen(word))) {
+		if (!strncasecmp(word, a->name, strlen(word)) &&
 			/* ... if this is right app serve it ... */
-			if (++which > state) {
-				char *ret = strdup(a->name);
-				ast_mutex_unlock(&applock);
-				return ret;
-			}
+				++which > state) {
+			ret = strdup(a->name);
+			break;
 		}
 		a = a->next; 
 	}
 
-	/* no application match */
 	ast_mutex_unlock(&applock);
-	return NULL; 
+	return ret; 
 }
 
 static int handle_show_application(int fd, int argc, char *argv[])
@@ -3042,16 +3015,10 @@ static int handle_show_application(int f
 
 				no_registered_app = 0;
 
-				if (a->synopsis)
-					synopsis_size = strlen(a->synopsis) + 23;
-				else
-					synopsis_size = strlen("Not available") + 23;
+				synopsis_size = gui_size(a->synopsis);
 				synopsis = alloca(synopsis_size);
 
-				if (a->description)
-					description_size = strlen(a->description) + 23;
-				else
-					description_size = strlen("Not available") + 23;
+				description_size = gui_size(a->description);
 				description = alloca(description_size);
 
 				if (synopsis && description) {
@@ -3059,12 +3026,8 @@ static int handle_show_application(int f
 					term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
 					term_color(syntitle, "[Synopsis]\n", COLOR_MAGENTA, 0, 40);
 					term_color(destitle, "[Description]\n", COLOR_MAGENTA, 0, 40);
-					term_color(synopsis,
-									a->synopsis ? a->synopsis : "Not available",
-									COLOR_CYAN, 0, synopsis_size);
-					term_color(description,
-									a->description ? a->description : "Not available",
-									COLOR_CYAN, 0, description_size);
+					term_color(synopsis, gui_string(a->synopsis), COLOR_CYAN, 0, synopsis_size);
+					term_color(description, gui_string(a->description), COLOR_CYAN, 0, description_size);
 
 					ast_cli(fd,"%s%s%s\n\n%s%s\n", infotitle, syntitle, synopsis, destitle, description);
 				} else {
@@ -3073,8 +3036,8 @@ static int handle_show_application(int f
 						"[Synopsis]\n  %s\n\n"
 						"[Description]\n%s\n",
 						a->name,
-						a->synopsis ? a->synopsis : "Not available",
-						a->description ? a->description : "Not available");
+						gui_string(a->synopsis),
+						gui_string(a->description));
 				}
 			}
 		}
@@ -3258,8 +3221,9 @@ static char *complete_show_applications(
 static char *complete_show_dialplan_context(char *line, char *word, int pos,
 	int state)
 {
-	struct ast_context *c;
+	struct ast_context *c = NULL;
 	int which = 0;
+	char *ret = NULL;
 
 	/* we are do completion of [exten@]context on second position only */
 	if (pos != 2) return NULL;
@@ -3271,24 +3235,17 @@ static char *complete_show_dialplan_cont
 	}
 
 	/* ... walk through all contexts ... */
-	c = ast_walk_contexts(NULL);
-	while(c) {
+	while ( (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), strlen(word)) &&
 			/* ... for serve? ... */
-			if (++which > state) {
-				/* ... yes, serve this context name ... */
-				char *ret = strdup(ast_get_context_name(c));
-				ast_unlock_contexts();
-				return ret;
-			}
+				++which > state) {
+			/* ... yes, serve this context name ... */
+			ret = strdup(ast_get_context_name(c));
+			break;
 		}
-		c = ast_walk_contexts(c);
-	}
-
-	/* ... unlock and return */
 	ast_unlock_contexts();
-	return NULL;
+	return ret;
 }
 
 struct dialplan_counters {
@@ -3553,6 +3510,7 @@ static struct ast_cli_entry pbx_cli[] = 
 
 int ast_unregister_application(const char *app) {
 	struct ast_app *tmp, *tmpl = NULL;
+
 	if (ast_mutex_lock(&applock)) {
 		ast_log(LOG_ERROR, "Unable to lock application list\n");
 		return -1;
@@ -3567,14 +3525,13 @@ int ast_unregister_application(const cha
 			if (option_verbose > 1)
 				ast_verbose( VERBOSE_PREFIX_2 "Unregistered application '%s'\n", tmp->name);
 			free(tmp);
-			ast_mutex_unlock(&applock);
-			return 0;
+			break;
 		}
 		tmpl = tmp;
 		tmp = tmp->next;
 	}
 	ast_mutex_unlock(&applock);
-	return -1;
+	return tmp ? 0 : -1;
 }
 
 struct ast_context *ast_context_create(struct ast_context **extcontexts, const char *name, const char *registrar)
@@ -3710,13 +3667,12 @@ static void get_timerange(struct ast_tim
 		return;
 	}
 	/* Otherwise expect a range */
-	e = strchr(times, '-');
+	e = times;
+	strsep(&e, "-");
 	if (!e) {
 		ast_log(LOG_WARNING, "Time range is not valid. Assuming no restrictions based on time.\n");
 		return;
 	}
-	*e = '\0';
-	e++;
 	while(*e && !isdigit(*e)) 
 		e++;
 	if (!*e) {
@@ -3799,12 +3755,8 @@ static unsigned int get_dow(char *dow)
 	if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
 		return (1 << 7) - 1;
 	/* Get start and ending days */
-	c = strchr(dow, '-');
-	if (c) {
-		*c = '\0';
-		c++;
-	} else
-		c = NULL;
+	c = dow;
+	strsep(&c, "-");
 	/* Find the start */
 	s = 0;
 	while((s < 7) && strcasecmp(dow, days[s])) s++;
@@ -3843,11 +3795,8 @@ static unsigned int get_day(char *day)
 		return mask;
 	}
 	/* Get start and ending days */
-	c = strchr(day, '-');
-	if (c) {
-		*c = '\0';
-		c++;
-	}
+	c = day;
+	strsep(&c, "-");
 	/* Find the start */
 	if (sscanf(day, "%d", &s) != 1) {
 		ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", day);
@@ -3905,11 +3854,8 @@ static unsigned int get_month(char *mon)
 	if (ast_strlen_zero(mon) || !strcmp(mon, "*")) 
 		return (1 << 12) - 1;
 	/* Get start and ending days */
-	c = strchr(mon, '-');
-	if (c) {
-		*c = '\0';
-		c++;
-	}
+	c = mon;
+	strsep(&c, "-");
 	/* Find the start */
 	s = 0;
 	while((s < 12) && strcasecmp(mon, months[s])) s++;
@@ -4044,11 +3990,8 @@ int ast_context_add_include2(struct ast_
 	/* ... fill in this structure ... */
 	memset(new_include, 0, length);
 	p = new_include->stuff;
-	new_include->name = p;
-	strcpy(new_include->name, value);
-	p += strlen(value) + 1;
-	new_include->rname = p;
-	strcpy(new_include->rname, value);
+	new_include->name = str_add(&p, value);
+	new_include->rname = str_add(&p, value);
 	c = new_include->rname;
 	/* Strip off timing info */
 	while(*c && (*c != '|')) 
@@ -4100,7 +4043,7 @@ int ast_context_add_include2(struct ast_
  */
 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;
@@ -4108,8 +4051,7 @@ int ast_context_add_switch(const char *c
 	}
 
 	/* walk contexts ... */
-	c = ast_walk_contexts(NULL);
-	while (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);
@@ -4117,7 +4059,6 @@ int ast_context_add_switch(const char *c
 			ast_unlock_contexts();
 			return ret;
 		}
-		c = ast_walk_contexts(c);
 	}
 
 	/* we can't find the right context */
@@ -4162,17 +4103,8 @@ int ast_context_add_switch2(struct ast_c
 	/* ... fill in this structure ... */
 	memset(new_sw, 0, length);
 	p = new_sw->stuff;
-	new_sw->name = p;
-	strcpy(new_sw->name, value);
-	p += strlen(value) + 1;
-	new_sw->data = p;
-	if (data) {
-		strcpy(new_sw->data, data);
-		p += strlen(data) + 1;
-	} else {
-		strcpy(new_sw->data, "");
-		p++;
-	}
+	new_sw->name = str_add(&p, value);
+	new_sw->data = str_add(&p, data ? data : "");
 	if (eval) 
 		new_sw->tmpdata = p;
 	new_sw->next      = NULL;
@@ -4464,27 +4396,19 @@ int ast_async_goto_by_name(const char *c
 	return res;
 }
 
-static int ext_strncpy(char *dst, const char *src, int len)
+/*
+ * copy src to dst skipping blanks, return len including the final '\0'
+ * src is guaranteed to fit in dst
+ */
+static int ext_strcpy(char *dst, const char *src)
 {
-	int count=0;
+	char *base = dst;
 
-	while(*src && (count < len - 1)) {
-		switch(*src) {
-		case ' ':
-			/*	otherwise exten => [a-b],1,... doesn't work */
-			/*		case '-': */
-			/* Ignore */
-			break;
-		default:
-			*dst = *src;
-			dst++;
-		}
-		src++;
-		count++;
-	}
-	*dst = '\0';
-
-	return count;
+	for (; *src; src++)
+		if (*src != ' ')
+			*dst++ = *src;
+	*dst++ = '\0';
+	return dst - base;
 }
 
 static void null_datad(void *foo)
@@ -4543,17 +4467,14 @@ int ast_add_extension2(struct ast_contex
 	if (tmp) {
 		memset(tmp, 0, length);
 		p = tmp->stuff;
-		if (label) {
-			tmp->label = p;
-			strcpy(tmp->label, label);
-			p += strlen(label) + 1;
-		}
+		if (label)
+			tmp->label = str_add(&p, label);
 		tmp->exten = p;
-		p += ext_strncpy(tmp->exten, extension, strlen(extension) + 1) + 1;
+		p += ext_strcpy(tmp->exten, extension);
 		tmp->priority = priority;
 		tmp->cidmatch = p;
 		if (callerid) {
-			p += ext_strncpy(tmp->cidmatch, callerid, strlen(callerid) + 1) + 1;
+			p += ext_strcpy(tmp->cidmatch, callerid);
 			tmp->matchcid = 1;
 		} else {
 			tmp->cidmatch[0] = '\0';
@@ -5312,20 +5233,14 @@ static int pbx_builtin_resetcdr(struct a
 static int pbx_builtin_setaccount(struct ast_channel *chan, void *data)
 {
 	/* Copy the account code  as specified */
-	if (data)
-		ast_cdr_setaccount(chan, (char *)data);
-	else
-		ast_cdr_setaccount(chan, "");
+	ast_cdr_setaccount(chan, data ? (char *)data : "");
 	return 0;
 }
 
 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);
-	else
-		ast_cdr_setamaflags(chan, "");
+	ast_cdr_setamaflags(chan, data ? (char *)data : "");
 	return 0;
 }
 
@@ -5432,10 +5347,8 @@ static int pbx_builtin_execiftime(struct
 				/* ptr2 is now the app name 
 				   we're done with ptr1 now so recycle it and use it to point to the app args*/
 				struct ast_app *app;
-				if((ptr1 = strchr(ptr2, '|'))) {
-					*ptr1 = '\0';
-					ptr1++;
-				}
+				ptr1 = ptr2;
+				strsep(&ptr1, "|");
 				if ((app = pbx_findapp(ptr2))) {
 					res = pbx_exec(chan, app, ptr1 ? ptr1 : "", 1);
 				} else {
@@ -5576,12 +5489,9 @@ static int pbx_builtin_background(struct
 		/* Stop anything playing */
 		ast_stopstream(chan);
 		/* Stream a file */
-		front = filename;
-		while(!res && front) {
-			if((back = strchr(front, '&'))) {
-				*back = '\0';
-				back++;
-			}
+		back = filename;
+		while(!res && back) {
+			front = strsep(&back, "&");
 			res = ast_streamfile(chan, front, lang);
 			if (!res) {
 				res = ast_waitstream(chan, AST_DIGIT_ANY);
@@ -5591,7 +5501,6 @@ static int pbx_builtin_background(struct
 				res = 0;
 				break;
 			}
-			front = back;
 		}
 	}
 	return res;
@@ -5782,12 +5691,11 @@ int pbx_builtin_setvar(struct ast_channe
 	}
 
 	for (x = 0; x < argc; x++) {
-		name = argv[x];
-		if ((value = strchr(name, '='))) {
-			*value = '\0';
-			value++;
+		value = name = argv[x];
+		strsep(&value, "=");
+		if (value != NULL)
 			pbx_builtin_setvar_helper((global) ? NULL : chan, name, value);
-		} else
+		else
 			ast_log(LOG_WARNING, "Ignoring entry '%s' with no = (and not last 'options' entry)\n", name);
 	}
 
@@ -5833,14 +5741,13 @@ static int pbx_builtin_setglobalvar(stru
 {
 	char *name;
 	char *value;
-	char *stringp = NULL;
+	char *stringp = data;
 
 	if (!data || ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "Ignoring, since there is no variable to set\n");
 		return 0;
 	}
 
-	stringp = data;
 	name = strsep(&stringp, "=");
 	value = strsep(&stringp, "\0"); 
 
@@ -5872,17 +5779,15 @@ static int pbx_checkcondition(char *cond
 static int pbx_builtin_gotoif(struct ast_channel *chan, void *data)
 {
 	char *condition,*branch1,*branch2,*branch;
-	char *s;
 	int rc;
-	char *stringp=NULL;
+	char *stringp;
 
 	if (!data || ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "Ignoring, since there is no variable to check\n");
 		return 0;
 	}
 	
-	s=ast_strdupa(data);
-	stringp=s;
+	stringp=ast_strdupa(data);
 	condition=strsep(&stringp,"?");
 	branch1=strsep(&stringp,":");
 	branch2=strsep(&stringp,"");
@@ -5902,8 +5807,8 @@ static int pbx_builtin_saynumber(struct 
 {
 	int res = 0;
 	char tmp[256];
-	char *number = (char *) NULL;
-	char *options = (char *) NULL;
+	char *number;
+	char *options;
 
 	
 	if (!data || ast_strlen_zero((char *)data)) {
@@ -6150,10 +6055,10 @@ struct ast_ignorepat *ast_walk_context_i
 
 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)) != NULL)
 		if (!ast_context_find(inc->rname)) {
 			res = -1;
 			ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n",
@@ -6190,9 +6095,8 @@ int ast_async_goto_if_exists(struct ast_
 
 int ast_parseable_goto(struct ast_channel *chan, const char *goto_string) 
 {
-	char *s;
 	char *exten, *pri, *context;
-	char *stringp=NULL;
+	char *stringp;
 	int ipri;
 	int mode = 0;
 
@@ -6200,8 +6104,7 @@ int ast_parseable_goto(struct ast_channe
 		ast_log(LOG_WARNING, "Goto requires an argument (optional context|optional extension|priority)\n");
 		return -1;
 	}
-	s = ast_strdupa(goto_string);
-	stringp=s;
+	stringp = ast_strdupa(goto_string);
 	context = strsep(&stringp, "|");
 	exten = strsep(&stringp, "|");
 	if (!exten) {
