Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:49 2009

## 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 and ExecIf Implementations
    00022  *
    00023  * \ingroup applications
    00024  */
    00025
    00026 #include <stdlib.h>
    00027 #include <stdio.h>
    00028 #include <string.h>
    00029 #include <unistd.h>
    00030
    00031 #include "asterisk.h"
    00032
    00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 51828 $")
    00034
    00035 #include "asterisk/file.h"
    00036 #include "asterisk/logger.h"
    00037 #include "asterisk/channel.h"
    00038 #include "asterisk/utils.h"
    00039 #include "asterisk/config.h"
    00040 #include "asterisk/pbx.h"
    00041 #include "asterisk/module.h"
    00042 #include "asterisk/lock.h"
    00043 #include "asterisk/options.h"
    00044
    00045 #define ALL_DONE(u,ret) {LOCAL_USER_REMOVE(u); return ret;}
    00046
    00047
    00048 static char *exec_app = "ExecIf";
    00049 static char *exec_desc =
    00050 "Usage:  ExecIF (<expr>|<app>|<data>)\n"
    00051 "If <expr> is true, execute and return the result of <app>(<data>).\n"
    00052 "If <expr> is true, but <app> is not found, then the application\n"
    00053 "will return a non-zero value.";
    00054 static char *exec_synopsis = "Conditional exec";
    00055
    00056 static char *start_app = "While";
    00057 static char *start_desc =
    00058 "Usage:  While(<expr>)\n"
    00059 "Start a While Loop.  Execution will return to this point when\n"
    00060 "EndWhile is called until expr is no longer true.\n";
    00061
    00062 static char *start_synopsis = "Start A While Loop";
    00063
    00064
    00065 static char *stop_app = "EndWhile";
    00066 static char *stop_desc =
    00067 "Usage:  EndWhile()\n"
    00068 "Return to the previous called While\n\n";
    00069
    00070 static char *stop_synopsis = "End A While Loop";
    00071
    00072 static char *tdesc = "While Loops and Conditional Execution";
    00073
    00074
    00075
    00076 STANDARD_LOCAL_USER;
    00077
    00078 LOCAL_USER_DECL;
    00079
    00080 static int execif_exec(struct ast_channel *chan, void *data) {
    00081    int res=0;
    00082    struct localuser *u;
    00083    char *myapp = NULL;
    00084    char *mydata = NULL;
    00085    char *expr = NULL;
    00086    struct ast_app *app = NULL;
    00087
    00088    LOCAL_USER_ADD(u);
    00089
    00090    expr = ast_strdupa(data);
    00091    if (!expr) {
    00092       ast_log(LOG_ERROR, "Out of memory\n");
    00093       LOCAL_USER_REMOVE(u);
    00094       return -1;
    00095    }
    00096
    00097    if ((myapp = strchr(expr,'|'))) {
    00098       *myapp = '\0';
    00099       myapp++;
    00100       if ((mydata = strchr(myapp,'|'))) {
    00101          *mydata = '\0';
    00102          mydata++;
    00103       } else
    00104          mydata = "";
    00105
    00106       if (pbx_checkcondition(expr)) {
    00107          if ((app = pbx_findapp(myapp))) {
    00108             res = pbx_exec(chan, app, mydata, 1);
    00109          } else {
    00110             ast_log(LOG_WARNING, "Count not find application! (%s)\n", myapp);
    00111             res = -1;
    00112          }
    00113       }
    00114    } else {
    00115       ast_log(LOG_ERROR,"Invalid Syntax.\n");
    00116       res = -1;
    00117    }
    00118
    00119    ALL_DONE(u,res);
    00120 }
    00121
    00122 #define VAR_SIZE 64
    00123
    00124
    00125 static char *get_index(struct ast_channel *chan, const char *prefix, int index) {
    00126    char varname[VAR_SIZE];
    00127
    00128    snprintf(varname, VAR_SIZE, "%s_%d", prefix, index);
    00129    return pbx_builtin_getvar_helper(chan, varname);
    00130 }
    00131
    00132 static struct ast_exten *find_matching_priority(struct ast_context *c, const char *exten, int priority, const char *callerid)
    00133 {
    00134    struct ast_exten *e;
    00135    struct ast_include *i;
    00136    struct ast_context *c2;
    00137
    00138    for (e=ast_walk_context_extensions(c, NULL); e; e=ast_walk_context_extensions(c, e)) {
    00139       if (ast_extension_match(ast_get_extension_name(e), exten)) {
    00140          int needmatch = ast_get_extension_matchcid(e);
    00141          if ((needmatch && ast_extension_match(ast_get_extension_cidmatch(e), callerid)) ||
    00142             (!needmatch)) {
    00143             /* This is the matching extension we want */
    00144             struct ast_exten *p;
    00145             for (p=ast_walk_extension_priorities(e, NULL); p; p=ast_walk_extension_priorities(e, p)) {
    00146                if (priority != ast_get_extension_priority(p))
    00147                   continue;
    00148                return p;
    00149             }
    00150          }
    00151       }
    00152    }
    00153
    00154    /* No match; run through includes */
    00155    for (i=ast_walk_context_includes(c, NULL); i; i=ast_walk_context_includes(c, i)) {
    00156       for (c2=ast_walk_contexts(NULL); c2; c2=ast_walk_contexts(c2)) {
    00157          if (!strcmp(ast_get_context_name(c2), ast_get_include_name(i))) {
    00158             e = find_matching_priority(c2, exten, priority, callerid);
    00159             if (e)
    00160                return e;
    00161          }
    00162       }
    00163    }
    00164    return NULL;
    00165 }
    00166
    00167 static int find_matching_endwhile(struct ast_channel *chan)
    00168 {
    00169    struct ast_context *c;
    00170    int res=-1;
    00171
    00172    if (ast_lock_contexts()) {
    00173       ast_log(LOG_ERROR, "Failed to lock contexts list\n");
    00174       return -1;
    00175    }
    00176
    00177    for (c=ast_walk_contexts(NULL); c; c=ast_walk_contexts(c)) {
    00178       struct ast_exten *e;
    00179
    00180       if (!ast_lock_context(c)) {
    00181          if (!strcmp(ast_get_context_name(c), chan->context)) {
    00182             /* This is the matching context we want */
    00183             int cur_priority = chan->priority + 1, level=1;
    00184
    00185             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)) {
    00186                if (!strcasecmp(ast_get_extension_app(e), "WHILE")) {
    00187                   level++;
    00188                } else if (!strcasecmp(ast_get_extension_app(e), "ENDWHILE")) {
    00189                   level--;
    00190                }
    00191
    00192                if (level == 0) {
    00193                   res = cur_priority;
    00194                   break;
    00195                }
    00196             }
    00197          }
    00198          ast_unlock_context(c);
    00199          if (res > 0) {
    00200             break;
    00201          }
    00202       }
    00203    }
    00204    ast_unlock_contexts();
    00205    return res;
    00206 }
    00207
    00208 static int _while_exec(struct ast_channel *chan, void *data, int end)
    00209 {
    00210    int res=0;
    00211    struct localuser *u;
    00212    char *while_pri = NULL;
    00213    char *goto_str = NULL, *my_name = NULL;
    00214    char *condition = NULL, *label = NULL;
    00215    char varname[VAR_SIZE], end_varname[VAR_SIZE];
    00216    const char *prefix = "WHILE";
    00217    size_t size=0;
    00218    int used_index_i = -1, x=0;
    00219    char used_index[VAR_SIZE] = "0", new_index[VAR_SIZE] = "0";
    00220
    00221    if (!chan) {
    00222       /* huh ? */
    00223       return -1;
    00224    }
    00225
    00226    LOCAL_USER_ADD(u);
    00227
    00228    /* dont want run away loops if the chan isn't even up
    00229       this is up for debate since it slows things down a tad ......
    00230    */
    00231    if (ast_waitfordigit(chan,1) < 0)
    00232       ALL_DONE(u,-1);
    00233
    00234
    00235    for (x=0;;x++) {
    00236       if (get_index(chan, prefix, x)) {
    00237          used_index_i = x;
    00238       } else
    00239          break;
    00240    }
    00241
    00242    snprintf(used_index, VAR_SIZE, "%d", used_index_i);
    00243    snprintf(new_index, VAR_SIZE, "%d", used_index_i + 1);
    00244
    00245    if (!end) {
    00246       condition = ast_strdupa((char *) data);
    00247    }
    00248
    00249    size = strlen(chan->context) + strlen(chan->exten) + 32;
    00250    my_name = alloca(size);
    00251    memset(my_name, 0, size);
    00252    snprintf(my_name, size, "%s_%s_%d", chan->context, chan->exten, chan->priority);
    00253
    00254    if (ast_strlen_zero(label)) {
    00255       if (end)
    00256          label = used_index;
    00257       else if (!(label = pbx_builtin_getvar_helper(chan, my_name))) {
    00258          label = new_index;
    00259          pbx_builtin_setvar_helper(chan, my_name, label);
    00260       }
    00261
    00262    }
    00263
    00264    snprintf(varname, VAR_SIZE, "%s_%s", prefix, label);
    00265    while_pri = pbx_builtin_getvar_helper(chan, varname);
    00266
    00267    if ((while_pri = pbx_builtin_getvar_helper(chan, varname)) && !end) {
    00268       snprintf(end_varname,VAR_SIZE,"END_%s",varname);
    00269    }
    00270
    00271
    00272    if (!end && !pbx_checkcondition(condition)) {
    00273       /* Condition Met (clean up helper vars) */
    00274       pbx_builtin_setvar_helper(chan, varname, NULL);
    00275       pbx_builtin_setvar_helper(chan, my_name, NULL);
    00276         snprintf(end_varname,VAR_SIZE,"END_%s",varname);
    00277       if ((goto_str=pbx_builtin_getvar_helper(chan, end_varname))) {
    00278          ast_parseable_goto(chan, goto_str);
    00279          pbx_builtin_setvar_helper(chan, end_varname, NULL);
    00280       } else {
    00281          int pri = find_matching_endwhile(chan);
    00282          if (pri > 0) {
    00283             if (option_verbose > 2)
    00284                ast_verbose(VERBOSE_PREFIX_3 "Jumping to priority %d\n", pri);
    00285             chan->priority = pri;
    00286          } else {
    00287             ast_log(LOG_WARNING, "Couldn't find matching EndWhile? (While at %s@%s priority %d)\n", chan->context, chan->exten, chan->priority);
    00288          }
    00289       }
    00290       ALL_DONE(u,res);
    00291    }
    00292
    00293    if (!end && !while_pri) {
    00294       size = strlen(chan->context) + strlen(chan->exten) + 32;
    00295       goto_str = alloca(size);
    00296       memset(goto_str, 0, size);
    00297       snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority);
    00298       pbx_builtin_setvar_helper(chan, varname, goto_str);
    00299    }
    00300
    00301    else if (end && while_pri) {
    00302       /* END of loop */
    00303       snprintf(end_varname, VAR_SIZE, "END_%s", varname);
    00304       if (! pbx_builtin_getvar_helper(chan, end_varname)) {
    00305          size = strlen(chan->context) + strlen(chan->exten) + 32;
    00306          goto_str = alloca(size);
    00307          memset(goto_str, 0, size);
    00308          snprintf(goto_str, size, "%s|%s|%d", chan->context, chan->exten, chan->priority+1);
    00309          pbx_builtin_setvar_helper(chan, end_varname, goto_str);
    00310       }
    00311       ast_parseable_goto(chan, while_pri);
    00312    }
    00313
    00314
    00315
    00316
    00317    ALL_DONE(u, res);
    00318 }
    00319
    00320 static int while_start_exec(struct ast_channel *chan, void *data) {
    00321    return _while_exec(chan, data, 0);
    00322 }
    00323
    00324 static int while_end_exec(struct ast_channel *chan, void *data) {
    00325    return _while_exec(chan, data, 1);
    00326 }
    00327
    00328
    00329 int unload_module(void)
    00330 {
    00331    int res;
    00332
    00333    res = ast_unregister_application(start_app);
    00334    res |= ast_unregister_application(exec_app);
    00335    res |= ast_unregister_application(stop_app);
    00336
    00337    STANDARD_HANGUP_LOCALUSERS;
    00338
    00339    return res;
    00340 }
    00341
    00342 int load_module(void)
    00343 {
    00344    int res;
    00345
    00346    res = ast_register_application(start_app, while_start_exec, start_synopsis, start_desc);
    00347    res |= ast_register_application(exec_app, execif_exec, exec_synopsis, exec_desc);
    00348    res |= ast_register_application(stop_app, while_end_exec, stop_synopsis, stop_desc);
    00349
    00350    return res;
    00351 }
    00352
    00353 char *description(void)
    00354 {
    00355    return tdesc;
    00356 }
    00357
    00358 int usecount(void)
    00359 {
    00360    int res;
    00361    STANDARD_USECOUNT(res);
    00362    return res;
    00363 }
    00364
    00365 char *key()
    00366 {
    00367    return ASTERISK_GPL_KEY;
    00368 }
    00369