Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 2009

## Asterisk developer's documentation

# app_exec.c

Go to the documentation of this file.

    00001 /*
    00002  * Asterisk -- An open source telephony toolkit.
    00003  *
    00004  * Copyright (c) 2004 - 2005, Tilghman Lesher.  All rights reserved.
    00005  *
    00006  * Tilghman Lesher <app_exec__v001@the-tilghman.com>
    00007  *
    00008  * This code is released by the author with no restrictions on usage.
    00009  *
    00010  * See http://www.asterisk.org for more information about
    00011  * the Asterisk project. Please do not directly contact
    00012  * any of the maintainers of this project for assistance;
    00013  * the project provides a web site, mailing lists and IRC
    00014  * channels for your use.
    00015  *
    00016  */
    00017
    00018 /*! \file
    00019  *
    00020  * \brief Exec application
    00021  *
    00022  * \author Tilghman Lesher <app_exec__v001@the-tilghman.com>
    00023  *
    00024  * \ingroup applications
    00025  */
    00026
    00027 #include <stdio.h>
    00028 #include <stdlib.h>
    00029 #include <unistd.h>
    00030 #include <string.h>
    00031
    00032 #include "asterisk.h"
    00033
    00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00035
    00036 #include "asterisk/file.h"
    00037 #include "asterisk/logger.h"
    00038 #include "asterisk/options.h"
    00039 #include "asterisk/channel.h"
    00040 #include "asterisk/pbx.h"
    00041 #include "asterisk/module.h"
    00042
    00043 /* Maximum length of any variable */
    00044 #define MAXRESULT 1024
    00045
    00046 static char *tdesc = "Executes applications";
    00047
    00048 static char *app_exec = "Exec";
    00049
    00050 static char *exec_synopsis = "Executes internal application";
    00051
    00052 static char *exec_descrip =
    00053 "Usage: Exec(appname(arguments))\n"
    00054 "  Allows an arbitrary application to be invoked even when not\n"
    00055 "hardcoded into the dialplan. To invoke external applications\n"
    00056 "see the application System. Returns whatever value the\n"
    00057 "app returns or a non-zero value if the app cannot be found.\n";
    00058
    00059 STANDARD_LOCAL_USER;
    00060
    00061 LOCAL_USER_DECL;
    00062
    00063 static int exec_exec(struct ast_channel *chan, void *data)
    00064 {
    00065    int res=0;
    00066    struct localuser *u;
    00067    char *s, *appname, *endargs, args[MAXRESULT];
    00068    struct ast_app *app;
    00069
    00070    LOCAL_USER_ADD(u);
    00071
    00072    memset(args, 0, MAXRESULT);
    00073
    00074    /* Check and parse arguments */
    00075    if (data) {
    00076       s = ast_strdupa((char *)data);
    00077       if (s) {
    00078          appname = strsep(&s, "(");
    00079          if (s) {
    00080             endargs = strrchr(s, ')');
    00081             if (endargs)
    00082                *endargs = '\0';
    00083             pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
    00084          }
    00085          if (appname) {
    00086             app = pbx_findapp(appname);
    00087             if (app) {
    00088                res = pbx_exec(chan, app, args, 1);
    00089             } else {
    00090                ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
    00091                res = -1;
    00092             }
    00093          }
    00094       } else {
    00095          ast_log(LOG_ERROR, "Out of memory\n");
    00096          res = -1;
    00097       }
    00098    }
    00099
    00100    LOCAL_USER_REMOVE(u);
    00101    return res;
    00102 }
    00103
    00104 int unload_module(void)
    00105 {
    00106    int res;
    00107
    00108    res = ast_unregister_application(app_exec);
    00109
    00110    STANDARD_HANGUP_LOCALUSERS;
    00111
    00112    return res;
    00113 }
    00114
    00115 int load_module(void)
    00116 {
    00117    return ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
    00118 }
    00119
    00120 char *description(void)
    00121 {
    00122    return tdesc;
    00123 }
    00124
    00125 int usecount(void)
    00126 {
    00127    int res;
    00128    STANDARD_USECOUNT(res);
    00129    return res;
    00130 }
    00131
    00132 char *key()
    00133 {
    00134    return ASTERISK_GPL_KEY;
    00135 }