Skip to content
Snippets Groups Projects
app_stack.c 7.27 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    /*
     * Asterisk -- An open source telephony toolkit.
     *
    
     * Copyright (c) 2004-2006 Tilghman Lesher <app_stack_v003@the-tilghman.com>.
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
     *
     * This code is released by the author with no restrictions on usage.
     *
     * 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 Stack applications Gosub, Return, etc.
    
     * \author Tilghman Lesher <app_stack_v003@the-tilghman.com>
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
     * 
     * \ingroup applications
     */
    
    
    #include "asterisk.h"
     
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    #include "asterisk/options.h"
    #include "asterisk/logger.h"
    #include "asterisk/channel.h"
    #include "asterisk/chanvars.h"
    #include "asterisk/pbx.h"
    #include "asterisk/module.h"
    #include "asterisk/config.h"
    
    #include "asterisk/app.h"
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    #define STACKVAR	"~GOSUB~STACK~"
    
    
    static const char *app_gosub = "Gosub";
    static const char *app_gosubif = "GosubIf";
    static const char *app_return = "Return";
    static const char *app_pop = "StackPop";
    
    static const char *gosub_synopsis = "Jump to label, saving return address";
    
    static const char *gosubif_synopsis = "Conditionally jump to label, saving return address";
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const char *return_synopsis = "Return from gosub routine";
    static const char *pop_synopsis = "Remove one address from gosub stack";
    
    static const char *gosub_descrip =
    
    "Gosub([[context|]exten|]priority[(arg1[|...][|argN])])\n"
    
    "  Jumps to the label specified, saving the return address.\n";
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const char *gosubif_descrip =
    
    "GosubIf(condition?labeliftrue[(arg1[|...])][:labeliffalse[(arg1[|...])]])\n"
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    "  If the condition is true, then jump to labeliftrue.  If false, jumps to\n"
    "labeliffalse, if specified.  In either case, a jump saves the return point\n"
    
    "in the dialplan, to be returned to with a Return.\n";
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const char *return_descrip =
    
    "Return([return-value])\n"
    "  Jumps to the last label on the stack, removing it.  The return value, if\n"
    "any, is saved in the channel variable GOSUB_RETVAL.\n";
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    static const char *pop_descrip =
    "StackPop()\n"
    
    "  Removes last label on the stack, discarding it.\n";
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    
    static int pop_exec(struct ast_channel *chan, void *data)
    {
    
    	const char *frame = pbx_builtin_getvar_helper(chan, STACKVAR);
    	int numargs = 0, i;
    	char argname[15];
    
    	/* Pop any arguments for this stack frame off the variable stack */
    	if (frame) {
    		numargs = atoi(frame);
    		for (i = 1; i <= numargs; i++) {
    			snprintf(argname, sizeof(argname), "ARG%d", i);
    			pbx_builtin_setvar_helper(chan, argname, NULL);
    		}
    	}
    
    	/* Remove the last frame from the Gosub stack */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
    
    	return 0;
    }
    
    static int return_exec(struct ast_channel *chan, void *data)
    {
    
    	const char *label = pbx_builtin_getvar_helper(chan, STACKVAR);
    
    	char argname[15], *retval = data;
    	int numargs, i;
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    	if (ast_strlen_zero(label)) {
    		ast_log(LOG_ERROR, "Return without Gosub: stack is empty\n");
    		return -1;
    
    	}
    
    	/* Pop any arguments for this stack frame off the variable stack */
    	numargs = atoi(label);
    	for (i = 1; i <= numargs; i++) {
    		snprintf(argname, sizeof(argname), "ARG%d", i);
    		pbx_builtin_setvar_helper(chan, argname, NULL);
    	}
    
    	/* If the label exists, it will always have a ':' */
    	label = strchr(label, ':') + 1;
    
    	if (ast_parseable_goto(chan, label)) {
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    		ast_log(LOG_WARNING, "No next statement after Gosub?\n");
    		return -1;
    	}
    
    
    	/* Remove the current frame from the Gosub stack */
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	pbx_builtin_setvar_helper(chan, STACKVAR, NULL);
    
    
    	/* Set a return value, if any */
    	pbx_builtin_setvar_helper(chan, "GOSUB_RETVAL", S_OR(retval, ""));
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	return 0;
    }
    
    static int gosub_exec(struct ast_channel *chan, void *data)
    {
    
    	char newlabel[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 11 + 11 + 4];
    
    	char argname[15], *tmp = ast_strdupa(data), *label, *endparen;
    
    	AST_DECLARE_APP_ARGS(args2,
    		AST_APP_ARG(argval)[100];
    	);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    	if (ast_strlen_zero(data)) {
    
    		ast_log(LOG_ERROR, "%s requires an argument: %s([[context|]exten|]priority[(arg1[|...][|argN])])\n", app_gosub, app_gosub);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    		return -1;
    	}
    
    
    	u = ast_module_user_add(chan);
    
    	/* NOTE:  you cannot use ast_app_separate_args for this, because '(' cannot be used as a delimiter. */
    	label = strsep(&tmp, "(");
    	if (tmp) {
    		endparen = strrchr(tmp, ')');
    
    		else
    			ast_log(LOG_WARNING, "Ouch.  No closing paren: '%s'?\n", (char *)data);
    		AST_STANDARD_APP_ARGS(args2, tmp);
    	} else
    		args2.argc = 0;
    
    	/* Create the return address, but don't save it until we know that the Gosub destination exists */
    
    	snprintf(newlabel, sizeof(newlabel), "%d:%s|%s|%d", args2.argc, chan->context, chan->exten, chan->priority + 1);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    
    	if (ast_parseable_goto(chan, label)) {
    		ast_log(LOG_ERROR, "Gosub address is invalid: '%s'\n", (char *)data);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    		return -1;
    	}
    
    
    	/* Now that we know for certain that we're going to a new location, set our arguments */
    
    		snprintf(argname, sizeof(argname), "ARG%d", i + 1);
    		pbx_builtin_pushvar_helper(chan, argname, args2.argval[i]);
    
    		ast_log(LOG_DEBUG, "Setting '%s' to '%s'\n", argname, args2.argval[i]);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	pbx_builtin_pushvar_helper(chan, STACKVAR, newlabel);
    
    	ast_log(LOG_DEBUG, "Setting gosub return address to '%s'\n", newlabel);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    	return 0;
    }
    
    static int gosubif_exec(struct ast_channel *chan, void *data)
    {
    
    	char *args;
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	int res=0;
    
    	AST_DECLARE_APP_ARGS(cond,
    		AST_APP_ARG(ition);
    		AST_APP_ARG(labels);
    	);
    	AST_DECLARE_APP_ARGS(label,
    		AST_APP_ARG(iftrue);
    		AST_APP_ARG(iffalse);
    	);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    	if (ast_strlen_zero(data)) {
    
    		ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    		return 0;
    	}
    
    
    	u = ast_module_user_add(chan);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    
    	args = ast_strdupa(data);
    	AST_NONSTANDARD_APP_ARGS(cond, args, '?');
    	if (cond.argc != 2) {
    		ast_log(LOG_WARNING, "GosubIf requires an argument: GosubIf(cond?label1(args):label2(args)\n");
    		ast_module_user_remove(u);
    		return 0;
    	}
    
    	AST_NONSTANDARD_APP_ARGS(label, cond.labels, ':');
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    
    	if (pbx_checkcondition(cond.ition)) {
    		if (!ast_strlen_zero(label.iftrue))
    			res = gosub_exec(chan, label.iftrue);
    	} else if (!ast_strlen_zero(label.iffalse)) {
    		res = gosub_exec(chan, label.iffalse);
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	}
    
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	return res;
    }
    
    
    static int unload_module(void)
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    {
    	ast_unregister_application(app_return);
    	ast_unregister_application(app_pop);
    	ast_unregister_application(app_gosubif);
    	ast_unregister_application(app_gosub);
    
    
    	ast_module_user_hangup_all();
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    
    	return 0;
    }
    
    
    static int load_module(void)
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    {
    	ast_register_application(app_pop, pop_exec, pop_synopsis, pop_descrip);
    	ast_register_application(app_return, return_exec, return_synopsis, return_descrip);
    	ast_register_application(app_gosubif, gosubif_exec, gosubif_synopsis, gosubif_descrip);
    	ast_register_application(app_gosub, gosub_exec, gosub_synopsis, gosub_descrip);
    
    	return 0;
    }
    
    
    AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Stack Routines");