Sat Aug 6 00:39:22 2011

Asterisk developer's documentation


app_while.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright 2004 - 2005, Anthony Minessale <anthmct@yahoo.com>
00005  *
00006  * Anthony Minessale <anthmct@yahoo.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief While Loop Implementation
00022  *
00023  * \author Anthony Minessale <anthmct@yahoo.com>
00024  * 
00025  * \ingroup applications
00026  */
00027 
00028 #include "asterisk.h"
00029 
00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 156755 $")
00031 
00032 #include <stdlib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 
00037 #include "asterisk/file.h"
00038 #include "asterisk/logger.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/utils.h"
00041 #include "asterisk/config.h"
00042 #include "asterisk/pbx.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/lock.h"
00045 #include "asterisk/options.h"
00046 
00047 #define ALL_DONE(u,ret) {ast_module_user_remove(u); return ret;}
00048 
00049 
00050 static char *start_app = "While";
00051 static char *start_desc = 
00052 "Usage:  While(<expr>)\n"
00053 "Start a While Loop.  Execution will return to this point when\n"
00054 "EndWhile is called until expr is no longer true.\n";
00055 
00056 static char *start_synopsis = "Start a while loop";
00057 
00058 
00059 static char *stop_app = "EndWhile";
00060 static char *stop_desc = 
00061 "Usage:  EndWhile()\n"
00062 "Return to the previous called While\n";
00063 
00064 static char *stop_synopsis = "End a while loop";
00065 
00066 static char *exit_app = "ExitWhile";
00067 static char *exit_desc =
00068 "Usage:  ExitWhile()\n"
00069 "Exits a While loop, whether or not the conditional has been satisfied.\n";
00070 static char *exit_synopsis = "End a While loop";
00071 
00072 static char *continue_app = "ContinueWhile";
00073 static char *continue_desc =
00074 "Usage:  ContinueWhile()\n"
00075 "Returns to the top of the while loop and re-evaluates the conditional.\n";
00076 static char *continue_synopsis = "Restart a While loop";
00077 
00078 #define VAR_SIZE 64
00079 
00080 
00081 static const char *get_index(struct ast_channel *chan, const char *prefix, int index) {
00082    char varname[VAR_SIZE];
00083 
00084    snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
00085    return pbx_builtin_getvar_helper(chan, varname);
00086 }
00087 
00088 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
00089 {
00090    struct ast_exten *e;
00091    struct ast_include *i;
00092    struct ast_context *c2;
00093 
00094    for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
00095       if (ast_extension_match(ast_get_extension_name(e), exten)) {
00096          int needmatch = ast_get_extension_matchcid(e);
00097          if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
00098             (!needmatch)) {
00099             /* This is the matching extension we want */
00100             struct ast_exten *p;
00101             for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
00102                if (priority != ast_get_extension_priority(p))
00103                   continue;
00104                return p;
00105             }
00106          }
00107       }
00108    }
00109 
00110    /* No match; run through includes */
00111    for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
00112       for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
00113          if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
00114             e = find_matching_priority(c2, exten, priority, callerid);
00115             if (e)
00116                return e;
00117          }
00118       }
00119    }
00120    return NULL;
00121 }
00122 
00123 static int find_matching_endwhile(struct ast_channel *chan)
00124 {
00125    struct ast_context *c;
00126    int res=-1;
00127 
00128    if (ast_lock_contexts()) {
00129       ast_log(LOG_ERROR, "Failed to lock contexts list\n");
00130       return -1;
00131    }
00132 
00133    for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
00134       struct ast_exten *e;
00135 
00136       if (!ast_lock_context(c)) {
00137          if (!strcmp(ast_get_context_name(c), chan->context)) {
00138             /* This is the matching context we want */
00139             int cur_priority = chan->priority + 1, level=1;
00140 
00141             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)) {
00142                if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
00143                   level++;
00144                } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
00145                   level--;
00146                }
00147 
00148                if (level == 0) {
00149                   res = cur_priority;
00150                   break;
00151                }
00152             }
00153          }
00154          ast_unlock_context(c);
00155          if (res > 0) {
00156             break;
00157          }
00158       }
00159    }
00160    ast_unlock_contexts();
00161    return res;
00162 }
00163 
00164 static int _while_exec(struct ast_channel *chan, void *data, int end)
00165 {
00166    int res=0;
00167    struct ast_module_user *u;
00168    const char *while_pri = NULL;
00169    char *my_name = NULL;
00170    const char *condition = NULL, *label = NULL;
00171    char varname[VAR_SIZE], end_varname[VAR_SIZE];
00172    const char *prefix = "WHILE";
00173    size_t size=0;
00174    int used_index_i = -1, x=0;
00175    char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
00176 
00177    if (!chan) {
00178       /* huh ? */
00179       return -1;
00180    }
00181 
00182    u = ast_module_user_add(chan);
00183 
00184 #if 0
00185    /* dont want run away loops if the chan isn't even up
00186       this is up for debate since it slows things down a tad ......
00187 
00188       Debate is over... this prevents While/EndWhile from working
00189       within the "h" extension.  Not good.
00190    */
00191    if (ast_waitfordigit(chan,1) < 0)
00192       ALL_DONE(u,-1);
00193 #endif
00194 
00195    for (x=0;;x++) {
00196       if (get_index(chan, prefix, x)) {
00197          used_index_i = x;
00198       } else 
00199          break;
00200    }
00201    
00202    snprintf(used_index, VAR_SIZE, "%d", used_index_i);
00203    snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
00204    
00205    if (!end)
00206       condition = ast_strdupa(data);
00207 
00208    size = strlen(chan->context) + strlen(chan->exten) + 32;
00209    my_name = alloca(size);
00210    memset(my_name, 0, size);
00211    snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
00212    
00213    if (ast_strlen_zero(label)) {
00214       if (end) 
00215          label = used_index;
00216       else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
00217          label = new_index;
00218          pbx_builtin_setvar_helper(chan, my_name, label);
00219       }
00220       
00221    }
00222    
00223    snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
00224    while_pri = pbx_builtin_getvar_helper(chan, varname);
00225    
00226    if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
00227       snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00228    }
00229    
00230 
00231    if ((!end && !pbx_checkcondition(condition)) || (end == 2)) {
00232       /* Condition Met (clean up helper vars) */
00233       const char *goto_str;
00234       pbx_builtin_setvar_helper(chan, varname, NULL);
00235       pbx_builtin_setvar_helper(chan, my_name, NULL);
00236       snprintf(end_varname,VAR_SIZE,"END_%s",varname);
00237       if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
00238          ast_parseable_goto(chan, goto_str);
00239          pbx_builtin_setvar_helper(chan, end_varname, NULL);
00240       } else {
00241          int pri = find_matching_endwhile(chan);
00242          if (pri > 0) {
00243             if (option_verbose > 2)
00244                ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri);
00245             chan->priority = pri;
00246          } else {
00247             ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
00248          }
00249       }
00250       ALL_DONE(u,res);
00251    }
00252 
00253    if (!end && !while_pri) {
00254       char *goto_str;
00255       size = strlen(chan->context) + strlen(chan->exten) + 32;
00256       goto_str = alloca(size);
00257       memset(goto_str, 0, size);
00258       snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
00259       pbx_builtin_setvar_helper(chan, varname, goto_str);
00260    } 
00261 
00262    else if (end && while_pri) {
00263       /* END of loop */
00264       snprintf(end_varname, VAR_SIZE, "END_%s", varname);
00265       if (! pbx_builtin_getvar_helper(chan, end_varname)) {
00266          char *goto_str;
00267          size = strlen(chan->context) + strlen(chan->exten) + 32;
00268          goto_str = alloca(size);
00269          memset(goto_str, 0, size);
00270          snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
00271          pbx_builtin_setvar_helper(chan, end_varname, goto_str);
00272       }
00273       ast_parseable_goto(chan, while_pri);
00274    }
00275    
00276 
00277 
00278 
00279    ALL_DONE(u, res);
00280 }
00281 
00282 static int while_start_exec(struct ast_channel *chan, void *data) {
00283    return _while_exec(chan, data, 0);
00284 }
00285 
00286 static int while_end_exec(struct ast_channel *chan, void *data) {
00287    return _while_exec(chan, data, 1);
00288 }
00289 
00290 static int while_exit_exec(struct ast_channel *chan, void *data) {
00291    return _while_exec(chan, data, 2);
00292 }
00293 
00294 static int while_continue_exec(struct ast_channel *chan, void *data)
00295 {
00296    int x;
00297    const char *prefix = "WHILE", *while_pri=NULL;
00298 
00299    for (x = 0; ; x++) {
00300       const char *tmp = get_index(chan, prefix, x);
00301       if (tmp)
00302          while_pri = tmp;
00303       else
00304          break;
00305    }
00306 
00307    if (while_pri)
00308       ast_parseable_goto(chan, while_pri);
00309 
00310    return 0;
00311 }
00312 
00313 static int unload_module(void)
00314 {
00315    int res;
00316    
00317    res = ast_unregister_application(start_app);
00318    res |= ast_unregister_application(stop_app);
00319    res |= ast_unregister_application(exit_app);
00320    res |= ast_unregister_application(continue_app);
00321 
00322    ast_module_user_hangup_all();
00323 
00324    return res;
00325 }
00326 
00327 static int load_module(void)
00328 {
00329    int res;
00330 
00331    res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
00332    res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
00333    res |= ast_register_application(exit_app, while_exit_exec, exit_synopsis, exit_desc);
00334    res |= ast_register_application(continue_app, while_continue_exec, continue_synopsis, continue_desc);
00335 
00336    return res;
00337 }
00338 
00339 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "While Loops and Conditional Execution");

Generated on Sat Aug 6 00:39:22 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7