Thu May 24 14:21:06 2007

Asterisk developer's documentation


app_stack.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2004-2005 Tilghman Lesher <app_stack_v002@the-tilghman.com>.
00005  *
00006  * This code is released by the author with no restrictions on usage.
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 Stack applications Gosub, Return, etc.
00022  * 
00023  * \ingroup applications
00024  */
00025 
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 #include <string.h>
00030 
00031 #include "asterisk/options.h"
00032 #include "asterisk/logger.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/chanvars.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/module.h"
00037 #include "asterisk/config.h"
00038 
00039 #define STACKVAR  "~GOSUB~STACK~"
00040 
00041 static const char *tdesc = "Stack Routines";
00042 
00043 static const char *app_gosub = "Gosub";
00044 static const char *app_gosubif = "GosubIf";
00045 static const char *app_return = "Return";
00046 static const char *app_pop = "StackPop";
00047 
00048 static const char *gosub_synopsis = "Jump to label, saving return address";
00049 static const char *gosubif_synopsis = "Jump to label, saving return address";
00050 static const char *return_synopsis = "Return from gosub routine";
00051 static const char *pop_synopsis = "Remove one address from gosub stack";
00052 
00053 static const char *gosub_descrip =
00054 "Gosub([[context|]exten|]priority)\n"
00055 "  Jumps to the label specified, saving the return address.\n";
00056 static const char *gosubif_descrip =
00057 "GosubIf(condition?labeliftrue[:labeliffalse])\n"
00058 "  If the condition is true, then jump to labeliftrue.  If false, jumps to\n"
00059 "labeliffalse, if specified.  In either case, a jump saves the return point\n"
00060 "in the dialplan, to be returned to with a Return.\n";
00061 static const char *return_descrip =
00062 "Return()\n"
00063 "  Jumps to the last label on the stack, removing it.\n";
00064 static const char *pop_descrip =
00065 "StackPop()\n"
00066 "  Removes last label on the stack, discarding it.\n";
00067 
00068 STANDARD_LOCAL_USER;
00069 
00070 LOCAL_USER_DECL;
00071 
00072 static int pop_exec(struct ast_channel *chan, void *data)
00073 {
00074    pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
00075 
00076    return 0;
00077 }
00078 
00079 static int return_exec(struct ast_channel *chan, void *data)
00080 {
00081    char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
00082 
00083    if (ast_strlen_zero(label)) {
00084       ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
00085       return -1;
00086    } else if (ast_parseable_goto(chan, label)) {
00087       ast_log(LOG_WARNING, "No next statement after Gosub?\n");
00088       return -1;
00089    }
00090 
00091    pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
00092    return 0;
00093 }
00094 
00095 static int gosub_exec(struct ast_channel *chan, void *data)
00096 {
00097    char newlabel[AST_MAX_EXTENSION * 2 + 3 + 11];
00098    struct localuser *u;
00099 
00100    if (ast_strlen_zero(data)) {
00101       ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority)\n", app_gosub, app_gosub);
00102       return -1;
00103    }
00104 
00105    LOCAL_USER_ADD(u);
00106    snprintf(newlabel, sizeof(newlabel), "%s|%s|%d", chan->context, chan->exten, chan->priority + 1);
00107 
00108    if (ast_parseable_goto(chan, data)) {
00109       LOCAL_USER_REMOVE(u);
00110       return -1;
00111    }
00112 
00113    pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
00114    LOCAL_USER_REMOVE(u);
00115 
00116    return 0;
00117 }
00118 
00119 static int gosubif_exec(struct ast_channel *chan, void *data)
00120 {
00121    struct localuser *u;
00122    char *condition="", *label1, *label2, *args;
00123    int res=0;
00124 
00125    if (ast_strlen_zero(data)) {
00126       ast_log(LOG_WARNING, "GosubIf requires an argument\n");
00127       return 0;
00128    }
00129 
00130    args = ast_strdupa((char *)data);
00131    if (!args) {
00132       ast_log(LOG_ERROR, "Out of memory\n");
00133       return -1;
00134    }
00135 
00136    LOCAL_USER_ADD(u);
00137 
00138    condition = strsep(&args, "?");
00139    label1 = strsep(&args, ":");
00140    label2 = args;
00141 
00142    if (pbx_checkcondition(condition)) {
00143       if (label1) {
00144          res = gosub_exec(chan, label1);
00145       }
00146    } else if (label2) {
00147       res = gosub_exec(chan, label2);
00148    }
00149 
00150    LOCAL_USER_REMOVE(u);
00151    return res;
00152 }
00153 
00154 int unload_module(void)
00155 {
00156    ast_unregister_application(app_return);
00157    ast_unregister_application(app_pop);
00158    ast_unregister_application(app_gosubif);
00159    ast_unregister_application(app_gosub);
00160 
00161    STANDARD_HANGUP_LOCALUSERS;
00162 
00163    return 0;
00164 }
00165 
00166 int load_module(void)
00167 {
00168    ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
00169    ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
00170    ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
00171    ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
00172 
00173    return 0;
00174 }
00175 
00176 char *description(void)
00177 {
00178    return (char *) tdesc;
00179 }
00180 
00181 int usecount(void)
00182 {
00183    int res;
00184 
00185    STANDARD_USECOUNT(res);
00186 
00187    return res;
00188 }
00189 
00190 char *key()
00191 {
00192    return ASTERISK_GPL_KEY;
00193 }

Generated on Thu May 24 14:21:06 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7