| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-01-21 07:06:25 +00:00
										 |  |  |  * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com> | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Anthony Minessale <anthmct@yahoo.com> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * See http://www.asterisk.org for more information about
 | 
					
						
							|  |  |  |  * the Asterisk project. Please do not directly contact | 
					
						
							|  |  |  |  * any of the maintainers of this project for assistance; | 
					
						
							|  |  |  |  * the project provides a web site, mailing lists and IRC | 
					
						
							|  |  |  |  * channels for your use. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2006-05-05 20:43:16 +00:00
										 |  |  |  * \brief While Loop Implementation | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \author Anthony Minessale <anthmct@yahoo.com> | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-11-06 15:09:47 +00:00
										 |  |  |  * \ingroup applications | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-07 18:54:56 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ASTERISK_FILE_VERSION(__FILE__, "$Revision$") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-21 06:02:45 +00:00
										 |  |  | #include "asterisk/pbx.h"
 | 
					
						
							|  |  |  | #include "asterisk/module.h"
 | 
					
						
							| 
									
										
										
										
											2007-11-22 03:50:04 +00:00
										 |  |  | #include "asterisk/channel.h"
 | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | /*** DOCUMENTATION
 | 
					
						
							|  |  |  | 	<application name="While" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Start a while loop. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax> | 
					
						
							|  |  |  | 			<parameter name="expr" required="true" /> | 
					
						
							|  |  |  | 		</syntax> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Start a While Loop.  Execution will return to this point when | 
					
						
							|  |  |  | 			<literal>EndWhile()</literal> is called until expr is no longer true.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 		<see-also> | 
					
						
							|  |  |  | 			<ref type="application">EndWhile</ref> | 
					
						
							|  |  |  | 			<ref type="application">ExitWhile</ref> | 
					
						
							|  |  |  | 			<ref type="application">ContinueWhile</ref> | 
					
						
							|  |  |  | 		</see-also> | 
					
						
							|  |  |  | 	</application> | 
					
						
							|  |  |  | 	<application name="EndWhile" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			End a while loop. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax /> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Return to the previous called <literal>While()</literal>.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 		<see-also> | 
					
						
							|  |  |  | 			<ref type="application">While</ref> | 
					
						
							|  |  |  | 			<ref type="application">ExitWhile</ref> | 
					
						
							|  |  |  | 			<ref type="application">ContinueWhile</ref> | 
					
						
							|  |  |  | 		</see-also> | 
					
						
							|  |  |  | 	</application> | 
					
						
							|  |  |  | 	<application name="ExitWhile" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			End a While loop. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax /> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Exits a <literal>While()</literal> loop, whether or not the conditional has been satisfied.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 		<see-also> | 
					
						
							|  |  |  | 			<ref type="application">While</ref> | 
					
						
							|  |  |  | 			<ref type="application">EndWhile</ref> | 
					
						
							|  |  |  | 			<ref type="application">ContinueWhile</ref> | 
					
						
							|  |  |  | 		</see-also> | 
					
						
							|  |  |  | 	</application> | 
					
						
							|  |  |  | 	<application name="ContinueWhile" language="en_US"> | 
					
						
							|  |  |  | 		<synopsis> | 
					
						
							|  |  |  | 			Restart a While loop. | 
					
						
							|  |  |  | 		</synopsis> | 
					
						
							|  |  |  | 		<syntax /> | 
					
						
							|  |  |  | 		<description> | 
					
						
							|  |  |  | 			<para>Returns to the top of the while loop and re-evaluates the conditional.</para> | 
					
						
							|  |  |  | 		</description> | 
					
						
							|  |  |  | 		<see-also> | 
					
						
							|  |  |  | 			<ref type="application">While</ref> | 
					
						
							|  |  |  | 			<ref type="application">EndWhile</ref> | 
					
						
							|  |  |  | 			<ref type="application">ExitWhile</ref> | 
					
						
							|  |  |  | 		</see-also> | 
					
						
							|  |  |  | 	</application> | 
					
						
							|  |  |  |  ***/ | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | static char *start_app = "While"; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | static char *stop_app = "EndWhile"; | 
					
						
							| 
									
										
										
										
											2006-04-26 18:49:07 +00:00
										 |  |  | static char *exit_app = "ExitWhile"; | 
					
						
							|  |  |  | static char *continue_app = "ContinueWhile"; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define VAR_SIZE 64
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-10 14:45:25 +00:00
										 |  |  | static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) { | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	char varname[VAR_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-10 14:45:25 +00:00
										 |  |  | 	snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	return pbx_builtin_getvar_helper(chan, varname); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-09-14 02:24:38 +00:00
										 |  |  | static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_exten *e; | 
					
						
							|  |  |  | 	struct ast_include *i; | 
					
						
							|  |  |  | 	struct ast_context *c2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) { | 
					
						
							|  |  |  | 		if (ast_extension_match(ast_get_extension_name(e), exten)) { | 
					
						
							|  |  |  | 			int needmatch = ast_get_extension_matchcid(e); | 
					
						
							|  |  |  | 			if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) || | 
					
						
							|  |  |  | 				(!needmatch)) { | 
					
						
							|  |  |  | 				/* This is the matching extension we want */ | 
					
						
							|  |  |  | 				struct ast_exten *p; | 
					
						
							|  |  |  | 				for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) { | 
					
						
							|  |  |  | 					if (priority != ast_get_extension_priority(p)) | 
					
						
							|  |  |  | 						continue; | 
					
						
							|  |  |  | 					return p; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* No match; run through includes */ | 
					
						
							|  |  |  | 	for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) { | 
					
						
							|  |  |  | 		for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) { | 
					
						
							|  |  |  | 			if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) { | 
					
						
							|  |  |  | 				e = find_matching_priority(c2, exten, priority, callerid); | 
					
						
							|  |  |  | 				if (e) | 
					
						
							|  |  |  | 					return e; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int find_matching_endwhile(struct ast_channel *chan) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct ast_context *c; | 
					
						
							|  |  |  | 	int res=-1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-28 20:46:01 +00:00
										 |  |  | 	if (ast_rdlock_contexts()) { | 
					
						
							| 
									
										
										
										
											2005-09-14 02:24:38 +00:00
										 |  |  | 		ast_log(LOG_ERROR, "Failed to lock contexts list\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) { | 
					
						
							|  |  |  | 		struct ast_exten *e; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-28 20:46:01 +00:00
										 |  |  | 		if (!ast_rdlock_context(c)) { | 
					
						
							| 
									
										
										
										
											2005-09-14 02:24:38 +00:00
										 |  |  | 			if (!strcmp(ast_get_context_name(c), chan->context)) { | 
					
						
							|  |  |  | 				/* This is the matching context we want */ | 
					
						
							|  |  |  | 				int cur_priority = chan->priority + 1, level=1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (e = find_matching_priority(c, chan->exten, cur_priority, chan->cid.cid_num); e; e = find_matching_priority(c, chan->exten, ++cur_priority, chan->cid.cid_num)) { | 
					
						
							|  |  |  | 					if (!strcasecmp(ast_get_extension_app(e), "WHILE")) { | 
					
						
							|  |  |  | 						level++; | 
					
						
							|  |  |  | 					} else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) { | 
					
						
							|  |  |  | 						level--; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (level == 0) { | 
					
						
							|  |  |  | 						res = cur_priority; | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			ast_unlock_context(c); | 
					
						
							|  |  |  | 			if (res > 0) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_unlock_contexts(); | 
					
						
							|  |  |  | 	return res; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int _while_exec(struct ast_channel *chan, void *data, int end) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int res=0; | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 	const char *while_pri = NULL; | 
					
						
							|  |  |  | 	char *my_name = NULL; | 
					
						
							|  |  |  | 	const char *condition = NULL, *label = NULL; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	char varname[VAR_SIZE], end_varname[VAR_SIZE]; | 
					
						
							|  |  |  | 	const char *prefix = "WHILE"; | 
					
						
							|  |  |  | 	size_t size=0; | 
					
						
							|  |  |  | 	int used_index_i = -1, x=0; | 
					
						
							|  |  |  | 	char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0"; | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	if (!chan) { | 
					
						
							|  |  |  | 		/* huh ? */ | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 00:43:13 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	/* dont want run away loops if the chan isn't even up
 | 
					
						
							|  |  |  | 	   this is up for debate since it slows things down a tad ...... | 
					
						
							| 
									
										
										
										
											2008-11-14 00:43:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	   Debate is over... this prevents While/EndWhile from working | 
					
						
							|  |  |  | 	   within the "h" extension.  Not good. | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	*/ | 
					
						
							|  |  |  | 	if (ast_waitfordigit(chan,1) < 0) | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 		return -1; | 
					
						
							| 
									
										
										
										
											2008-11-14 00:43:13 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (x=0;;x++) { | 
					
						
							|  |  |  | 		if (get_index(chan, prefix, x)) { | 
					
						
							|  |  |  | 			used_index_i = x; | 
					
						
							|  |  |  | 		} else  | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	snprintf(used_index, VAR_SIZE, "%d", used_index_i); | 
					
						
							|  |  |  | 	snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2006-05-10 13:22:15 +00:00
										 |  |  | 	if (!end) | 
					
						
							|  |  |  | 		condition = ast_strdupa(data); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	size = strlen(chan->context) + strlen(chan->exten) + 32; | 
					
						
							|  |  |  | 	my_name = alloca(size); | 
					
						
							|  |  |  | 	memset(my_name, 0, size); | 
					
						
							|  |  |  | 	snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 	ast_channel_lock(chan); | 
					
						
							|  |  |  | 	if (end) { | 
					
						
							|  |  |  | 		label = used_index; | 
					
						
							|  |  |  | 	} else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) { | 
					
						
							|  |  |  | 		label = new_index; | 
					
						
							|  |  |  | 		pbx_builtin_setvar_helper(chan, my_name, label); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	snprintf(varname, VAR_SIZE, "%s_%s", prefix, label); | 
					
						
							|  |  |  | 	if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) { | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 		while_pri = ast_strdupa(while_pri); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		snprintf(end_varname,VAR_SIZE,"END_%s",varname); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 	ast_channel_unlock(chan); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-05-05 16:36:43 +00:00
										 |  |  | 	if ((!end && !pbx_checkcondition(condition)) || (end == 2)) { | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		/* Condition Met (clean up helper vars) */ | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 		const char *goto_str; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, varname, NULL); | 
					
						
							|  |  |  | 		pbx_builtin_setvar_helper(chan, my_name, NULL); | 
					
						
							| 
									
										
										
										
											2006-05-05 14:47:22 +00:00
										 |  |  | 		snprintf(end_varname,VAR_SIZE,"END_%s",varname); | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 		ast_channel_lock(chan); | 
					
						
							|  |  |  | 		if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) { | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 			ast_parseable_goto(chan, goto_str); | 
					
						
							| 
									
										
										
										
											2007-01-24 00:21:32 +00:00
										 |  |  | 			pbx_builtin_setvar_helper(chan, end_varname, NULL); | 
					
						
							| 
									
										
										
										
											2005-09-14 02:24:38 +00:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			int pri = find_matching_endwhile(chan); | 
					
						
							|  |  |  | 			if (pri > 0) { | 
					
						
							| 
									
										
										
										
											2007-07-26 15:49:18 +00:00
										 |  |  | 				ast_verb(3, "Jumping to priority %d\n", pri); | 
					
						
							| 
									
										
										
										
											2005-09-14 02:24:38 +00:00
										 |  |  | 				chan->priority = pri; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 		ast_channel_unlock(chan); | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!end && !while_pri) { | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 		char *goto_str; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		size = strlen(chan->context) + strlen(chan->exten) + 32; | 
					
						
							|  |  |  | 		goto_str = alloca(size); | 
					
						
							|  |  |  | 		memset(goto_str, 0, size); | 
					
						
							| 
									
										
										
										
											2007-08-23 21:34:10 +00:00
										 |  |  | 		snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 		pbx_builtin_setvar_helper(chan, varname, goto_str); | 
					
						
							| 
									
										
										
										
											2008-04-30 19:21:04 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	else if (end && while_pri) { | 
					
						
							|  |  |  | 		/* END of loop */ | 
					
						
							|  |  |  | 		snprintf(end_varname, VAR_SIZE, "END_%s", varname); | 
					
						
							|  |  |  | 		if (! pbx_builtin_getvar_helper(chan, end_varname)) { | 
					
						
							| 
									
										
										
										
											2005-12-03 19:25:33 +00:00
										 |  |  | 			char *goto_str; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 			size = strlen(chan->context) + strlen(chan->exten) + 32; | 
					
						
							|  |  |  | 			goto_str = alloca(size); | 
					
						
							|  |  |  | 			memset(goto_str, 0, size); | 
					
						
							| 
									
										
										
										
											2007-08-23 21:34:10 +00:00
										 |  |  | 			snprintf(goto_str, size, "%s,%s,%d", chan->context, chan->exten, chan->priority+1); | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 			pbx_builtin_setvar_helper(chan, end_varname, goto_str); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ast_parseable_goto(chan, while_pri); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-07-16 14:39:29 +00:00
										 |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int while_start_exec(struct ast_channel *chan, void *data) { | 
					
						
							|  |  |  | 	return _while_exec(chan, data, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int while_end_exec(struct ast_channel *chan, void *data) { | 
					
						
							|  |  |  | 	return _while_exec(chan, data, 1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-26 18:49:07 +00:00
										 |  |  | static int while_exit_exec(struct ast_channel *chan, void *data) { | 
					
						
							|  |  |  | 	return _while_exec(chan, data, 2); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int while_continue_exec(struct ast_channel *chan, void *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	const char *prefix = "WHILE", *while_pri=NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (x = 0; ; x++) { | 
					
						
							|  |  |  | 		const char *tmp = get_index(chan, prefix, x); | 
					
						
							|  |  |  | 		if (tmp) | 
					
						
							|  |  |  | 			while_pri = tmp; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (while_pri) | 
					
						
							|  |  |  | 		ast_parseable_goto(chan, while_pri); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int unload_module(void) | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	res = ast_unregister_application(start_app); | 
					
						
							|  |  |  | 	res |= ast_unregister_application(stop_app); | 
					
						
							| 
									
										
										
										
											2006-04-26 18:49:07 +00:00
										 |  |  | 	res |= ast_unregister_application(exit_app); | 
					
						
							|  |  |  | 	res |= ast_unregister_application(continue_app); | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | static int load_module(void) | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-01 21:10:07 +00:00
										 |  |  | 	res = ast_register_application_xml(start_app, while_start_exec); | 
					
						
							|  |  |  | 	res |= ast_register_application_xml(stop_app, while_end_exec); | 
					
						
							|  |  |  | 	res |= ast_register_application_xml(exit_app, while_exit_exec); | 
					
						
							|  |  |  | 	res |= ast_register_application_xml(continue_app, while_continue_exec); | 
					
						
							| 
									
										
										
										
											2005-10-18 22:52:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return res; | 
					
						
							| 
									
										
										
										
											2004-12-24 01:18:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-08-21 02:11:39 +00:00
										 |  |  | AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution"); |