Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:47 2009

## Asterisk developer's documentation

# app_eval.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_eval__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  * \brief Eval application
    00020  *
    00021  * \author Tilghman Lesher <app_eval__v001@the-tilghman.com>
    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.h"
    00032
    00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
    00034
    00035 #include "asterisk/file.h"
    00036 #include "asterisk/logger.h"
    00037 #include "asterisk/options.h"
    00038 #include "asterisk/channel.h"
    00039 #include "asterisk/pbx.h"
    00040 #include "asterisk/module.h"
    00041
    00042 /* Maximum length of any variable */
    00043 #define MAXRESULT 1024
    00044
    00045 static char *tdesc = "Reevaluates strings";
    00046
    00047 static char *app_eval = "Eval";
    00048
    00049 static char *eval_synopsis = "Evaluates a string";
    00050
    00051 static char *eval_descrip =
    00052 "Usage: Eval(newvar=somestring)\n"
    00053 "  Normally Asterisk evaluates variables inline.  But what if you want to\n"
    00054 "store variable offsets in a database, to be evaluated later?  Eval is\n"
    00055 "the answer, by allowing a string to be evaluated twice in the dialplan,\n"
    00056 "the first time as part of the normal dialplan, and the second using Eval.\n";
    00057
    00058 STANDARD_LOCAL_USER;
    00059
    00060 LOCAL_USER_DECL;
    00061
    00062 static int eval_exec(struct ast_channel *chan, void *data)
    00063 {
    00064    int res=0;
    00065    struct localuser *u;
    00066    char *s, *newvar=NULL, tmp[MAXRESULT];
    00067    static int dep_warning = 0;
    00068
    00069    LOCAL_USER_ADD(u);
    00070
    00071    if (!dep_warning) {
    00072       ast_log(LOG_WARNING, "This application has been deprecated in favor of the dialplan function, EVAL\n");
    00073       dep_warning = 1;
    00074    }
    00075
    00076    /* Check and parse arguments */
    00077    if (data) {
    00078       s = ast_strdupa((char *)data);
    00079       if (s) {
    00080          newvar = strsep(&s, "=");
    00081          if (newvar && (newvar[0] != '\0')) {
    00082             memset(tmp, 0, MAXRESULT);
    00083             pbx_substitute_variables_helper(chan, s, tmp, MAXRESULT - 1);
    00084             pbx_builtin_setvar_helper(chan, newvar, tmp);
    00085          }
    00086       } else {
    00087          ast_log(LOG_ERROR, "Out of memory\n");
    00088          res = -1;
    00089       }
    00090    }
    00091
    00092    LOCAL_USER_REMOVE(u);
    00093    return res;
    00094 }
    00095
    00096 int unload_module(void)
    00097 {
    00098    int res;
    00099
    00100    res = ast_unregister_application(app_eval);
    00101
    00102    STANDARD_HANGUP_LOCALUSERS;
    00103
    00104    return res;
    00105 }
    00106
    00107 int load_module(void)
    00108 {
    00109    return ast_register_application(app_eval, eval_exec, eval_synopsis, eval_descrip);
    00110 }
    00111
    00112 char *description(void)
    00113 {
    00114    return tdesc;
    00115 }
    00116
    00117 int usecount(void)
    00118 {
    00119    int res;
    00120    STANDARD_USECOUNT(res);
    00121    return res;
    00122 }
    00123
    00124 char *key()
    00125 {
    00126    return ASTERISK_GPL_KEY;
    00127 }