Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / apps / app_exec.c
blob285b8b2f02ddca8053a7a3a887fc79d080480c58
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
5 * Portions copyright (c) 2006, Philipp Dunkel.
7 * Tilghman Lesher <app_exec__v002@the-tilghman.com>
9 * This code is released by the author with no restrictions on usage.
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
19 /*! \file
21 * \brief Exec application
23 * \author Tilghman Lesher <app_exec__v002@the-tilghman.com>
24 * \author Philipp Dunkel <philipp.dunkel@ebox.at>
26 * \ingroup applications
29 #include "asterisk.h"
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/file.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/module.h"
37 #include "asterisk/app.h"
39 /* Maximum length of any variable */
40 #define MAXRESULT 1024
42 /*! Note
44 * The key difference between these two apps is exit status. In a
45 * nutshell, Exec tries to be transparent as possible, behaving
46 * in exactly the same way as if the application it calls was
47 * directly invoked from the dialplan.
49 * TryExec, on the other hand, provides a way to execute applications
50 * and catch any possible fatal error without actually fatally
51 * affecting the dialplan.
54 static char *app_exec = "Exec";
55 static char *exec_synopsis = "Executes dialplan application";
56 static char *exec_descrip =
57 " Exec(appname(arguments)):\n"
58 "Allows an arbitrary application to be invoked even when not\n"
59 "hardcoded into the dialplan. If the underlying application\n"
60 "terminates the dialplan, or if the application cannot be found,\n"
61 "Exec will terminate the dialplan.\n"
62 " To invoke external applications, see the application System.\n"
63 " If you would like to catch any error instead, see TryExec.\n";
65 static char *app_tryexec = "TryExec";
66 static char *tryexec_synopsis = "Executes dialplan application, always returning";
67 static char *tryexec_descrip =
68 " TryExec(appname(arguments)):\n"
69 "Allows an arbitrary application to be invoked even when not\n"
70 "hardcoded into the dialplan. To invoke external applications\n"
71 "see the application System. Always returns to the dialplan.\n"
72 "The channel variable TRYSTATUS will be set to one of:\n"
73 " SUCCESS if the application returned zero\n"
74 " FAILED if the application returned non-zero\n"
75 " NOAPP if the application was not found or was not specified\n";
77 static char *app_execif = "ExecIf";
78 static char *execif_synopsis = "Executes dialplan application, conditionally";
79 static char *execif_descrip =
80 " ExecIF (<expr>?<appiftrue>(<args>)[:<appiffalse>(<args>)])\n"
81 "If <expr> is true, execute and return the result of <appiftrue>(<args>).\n"
82 "If <expr> is true, but <appiftrue> is not found, then the application\n"
83 "will return a non-zero value.\n";
85 static int exec_exec(struct ast_channel *chan, void *data)
87 int res = 0;
88 char *s, *appname, *endargs, args[MAXRESULT];
89 struct ast_app *app;
91 if (ast_strlen_zero(data))
92 return 0;
94 s = ast_strdupa(data);
95 args[0] = 0;
96 appname = strsep(&s, "(");
97 if (s) {
98 endargs = strrchr(s, ')');
99 if (endargs)
100 *endargs = '\0';
101 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
103 if (appname) {
104 app = pbx_findapp(appname);
105 if (app) {
106 res = pbx_exec(chan, app, args);
107 } else {
108 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
109 res = -1;
113 return res;
116 static int tryexec_exec(struct ast_channel *chan, void *data)
118 int res = 0;
119 char *s, *appname, *endargs, args[MAXRESULT];
120 struct ast_app *app;
122 if (ast_strlen_zero(data))
123 return 0;
125 s = ast_strdupa(data);
126 args[0] = 0;
127 appname = strsep(&s, "(");
128 if (s) {
129 endargs = strrchr(s, ')');
130 if (endargs)
131 *endargs = '\0';
132 pbx_substitute_variables_helper(chan, s, args, MAXRESULT - 1);
134 if (appname) {
135 app = pbx_findapp(appname);
136 if (app) {
137 res = pbx_exec(chan, app, args);
138 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
139 } else {
140 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
141 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
145 return 0;
148 static int execif_exec(struct ast_channel *chan, void *data)
150 int res = 0;
151 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
152 struct ast_app *app = NULL;
153 AST_DECLARE_APP_ARGS(expr,
154 AST_APP_ARG(expr);
155 AST_APP_ARG(remainder);
157 AST_DECLARE_APP_ARGS(apps,
158 AST_APP_ARG(t);
159 AST_APP_ARG(f);
161 char *parse = ast_strdupa(data);
163 firstcomma = strchr(parse, ',');
164 firstquestion = strchr(parse, '?');
166 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
167 /* Deprecated syntax */
168 AST_DECLARE_APP_ARGS(depr,
169 AST_APP_ARG(expr);
170 AST_APP_ARG(appname);
171 AST_APP_ARG(appargs);
173 AST_STANDARD_APP_ARGS(depr, parse);
175 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
177 /* Make the two syntaxes look the same */
178 expr.expr = depr.expr;
179 apps.t = depr.appname;
180 apps.f = NULL;
181 truedata = depr.appargs;
182 } else {
183 /* Preferred syntax */
185 AST_NONSTANDARD_APP_ARGS(expr, parse, '?');
186 if (ast_strlen_zero(expr.remainder)) {
187 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
188 return -1;
191 AST_NONSTANDARD_APP_ARGS(apps, expr.remainder, ':');
193 if (apps.t && (truedata = strchr(apps.t, '('))) {
194 *truedata++ = '\0';
195 if ((end = strrchr(truedata, ')'))) {
196 *end = '\0';
200 if (apps.f && (falsedata = strchr(apps.f, '('))) {
201 *falsedata++ = '\0';
202 if ((end = strrchr(falsedata, ')'))) {
203 *end = '\0';
208 if (pbx_checkcondition(expr.expr)) {
209 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
210 res = pbx_exec(chan, app, S_OR(truedata, ""));
211 } else {
212 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
213 res = -1;
215 } else if (!ast_strlen_zero(apps.f)) {
216 if ((app = pbx_findapp(apps.f))) {
217 res = pbx_exec(chan, app, S_OR(falsedata, ""));
218 } else {
219 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
220 res = -1;
224 return res;
227 static int unload_module(void)
229 int res;
231 res = ast_unregister_application(app_exec);
232 res |= ast_unregister_application(app_tryexec);
233 res |= ast_unregister_application(app_execif);
235 return res;
238 static int load_module(void)
240 int res = ast_register_application(app_exec, exec_exec, exec_synopsis, exec_descrip);
241 res |= ast_register_application(app_tryexec, tryexec_exec, tryexec_synopsis, tryexec_descrip);
242 res |= ast_register_application(app_execif, execif_exec, execif_synopsis, execif_descrip);
243 return res;
246 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");