1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
*
* Anthony Minessale <anthmct@yahoo.com>
*
* 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.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief While Loop Implementation
*
* \author Anthony Minessale <anthmct@yahoo.com>
*
* \ingroup applications
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 370655 $")
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
/*** 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>
***/
static char *start_app = "While";
static char *stop_app = "EndWhile";
static char *exit_app = "ExitWhile";
static char *continue_app = "ContinueWhile";
#define VAR_SIZE 64
static const char *get_index(struct ast_channel *chan, const char *prefix, int idx) {
char varname[VAR_SIZE];
snprintf(varname, VAR_SIZE, "%s_%d", prefix, idx);
return pbx_builtin_getvar_helper(chan, varname);
}
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;
if (ast_rdlock_contexts()) {
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;
if (!ast_rdlock_context(c)) {
if (!strcmp(ast_get_context_name(c), ast_channel_context(chan))) {
/* This is the matching context we want */
int cur_priority = ast_channel_priority(chan) + 1, level=1;
for (e = find_matching_priority(c, ast_channel_exten(chan), cur_priority,
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL));
e;
e = find_matching_priority(c, ast_channel_exten(chan), ++cur_priority,
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
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;
}
static int _while_exec(struct ast_channel *chan, const char *data, int end)
{
int res=0;
const char *while_pri = NULL;
char *my_name = NULL;
const char *condition = NULL, *label = NULL;
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";
if (!chan) {
/* huh ? */
return -1;
}
#if 0
/* don't want run away loops if the chan isn't even up
this is up for debate since it slows things down a tad ......
Debate is over... this prevents While/EndWhile from working
within the "h" extension. Not good.
*/
if (ast_waitfordigit(chan,1) < 0)
return -1;
#endif
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);
if (!end)
condition = ast_strdupa(data);
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
my_name = ast_alloca(size);
memset(my_name, 0, size);
snprintf(my_name, size, "%s_%s_%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
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);
}
snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
while_pri = ast_strdupa(while_pri);
snprintf(end_varname,VAR_SIZE,"END_%s",varname);
}
ast_channel_unlock(chan);
if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
/* Condition Met (clean up helper vars) */
const char *goto_str;
pbx_builtin_setvar_helper(chan, varname, NULL);
pbx_builtin_setvar_helper(chan, my_name, NULL);
snprintf(end_varname,VAR_SIZE,"END_%s",varname);
ast_channel_lock(chan);
if ((goto_str = pbx_builtin_getvar_helper(chan, end_varname))) {
ast_parseable_goto(chan, goto_str);
pbx_builtin_setvar_helper(chan, end_varname, NULL);
} else {
int pri = find_matching_endwhile(chan);
if (pri > 0) {
ast_verb(3, "Jumping to priority %d\n", pri);
ast_channel_priority_set(chan, pri);
} else {
ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
}
}
ast_channel_unlock(chan);
return res;
}
if (!end && !while_pri) {
char *goto_str;
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
goto_str = ast_alloca(size);
memset(goto_str, 0, size);
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
pbx_builtin_setvar_helper(chan, varname, goto_str);
}
else if (end && while_pri) {
/* END of loop */
snprintf(end_varname, VAR_SIZE, "END_%s", varname);
if (! pbx_builtin_getvar_helper(chan, end_varname)) {
char *goto_str;
size = strlen(ast_channel_context(chan)) + strlen(ast_channel_exten(chan)) + 32;
goto_str = ast_alloca(size);
memset(goto_str, 0, size);
snprintf(goto_str, size, "%s,%s,%d", ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan)+1);
pbx_builtin_setvar_helper(chan, end_varname, goto_str);
}
ast_parseable_goto(chan, while_pri);
}
return res;
}
static int while_start_exec(struct ast_channel *chan, const char *data) {
return _while_exec(chan, data, 0);
}
static int while_end_exec(struct ast_channel *chan, const char *data) {
return _while_exec(chan, data, 1);
}
static int while_exit_exec(struct ast_channel *chan, const char *data) {
return _while_exec(chan, data, 2);
}
static int while_continue_exec(struct ast_channel *chan, const char *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;
}
static int unload_module(void)
{
int res;
res = ast_unregister_application(start_app);
res |= ast_unregister_application(stop_app);
res |= ast_unregister_application(exit_app);
res |= ast_unregister_application(continue_app);
return res;
}
static int load_module(void)
{
int res;
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);
return res;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");
|