Sat Aug 6 00:39:21 2011

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

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