Asterisk - The Open Source Telephony Project GIT-master-a358458
app_exec.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (c) 2004 - 2005, Tilghman Lesher. All rights reserved.
5 * Portions copyright (c) 2006, Philipp Dunkel.
6 *
7 * Tilghman Lesher <app_exec__v002@the-tilghman.com>
8 *
9 * This code is released by the author with no restrictions on usage.
10 *
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.
16 *
17 */
18
19/*! \file
20 *
21 * \brief Exec application
22 *
23 * \author Tilghman Lesher <app_exec__v002@the-tilghman.com>
24 * \author Philipp Dunkel <philipp.dunkel@ebox.at>
25 *
26 * \ingroup applications
27 */
28
29/*** MODULEINFO
30 <support_level>core</support_level>
31 ***/
32#include "asterisk.h"
33
34#include "asterisk/file.h"
35#include "asterisk/channel.h"
36#include "asterisk/pbx.h"
37#include "asterisk/module.h"
38#include "asterisk/app.h"
39
40/*** DOCUMENTATION
41 <application name="Exec" language="en_US">
42 <synopsis>
43 Executes dialplan application.
44 </synopsis>
45 <syntax>
46 <parameter name="appname" required="true" hasparams="true">
47 <para>Application name and arguments of the dialplan application to execute.</para>
48 <argument name="arguments" required="true" />
49 </parameter>
50 </syntax>
51 <description>
52 <para>Allows an arbitrary application to be invoked even when not
53 hard coded into the dialplan. If the underlying application
54 terminates the dialplan, or if the application cannot be found,
55 Exec will terminate the dialplan.</para>
56 <para>To invoke external applications, see the application System.
57 If you would like to catch any error instead, see TryExec.</para>
58 </description>
59 </application>
60 <application name="TryExec" language="en_US">
61 <synopsis>
62 Executes dialplan application, always returning.
63 </synopsis>
64 <syntax>
65 <parameter name="appname" required="true" hasparams="true">
66 <argument name="arguments" required="true" />
67 </parameter>
68 </syntax>
69 <description>
70 <para>Allows an arbitrary application to be invoked even when not
71 hard coded into the dialplan. To invoke external applications
72 see the application System. Always returns to the dialplan.
73 The channel variable TRYSTATUS will be set to one of:
74 </para>
75 <variablelist>
76 <variable name="TRYSTATUS">
77 <value name="SUCCESS">
78 If the application returned zero.
79 </value>
80 <value name="FAILED">
81 If the application returned non-zero.
82 </value>
83 <value name="NOAPP">
84 If the application was not found or was not specified.
85 </value>
86 </variable>
87 </variablelist>
88 </description>
89 </application>
90 <application name="ExecIf" language="en_US">
91 <synopsis>
92 Executes dialplan application, conditionally.
93 </synopsis>
94 <syntax argsep="?">
95 <parameter name="expression" required="true" />
96 <parameter name="execapp" required="true" argsep=":">
97 <argument name="appiftrue" required="true" hasparams="true">
98 <argument name="args" required="true" />
99 </argument>
100 <argument name="appiffalse" required="false" hasparams="true">
101 <argument name="args" required="true" />
102 </argument>
103 </parameter>
104 </syntax>
105 <description>
106 <para>If <replaceable>expr</replaceable> is true, execute and return the
107 result of <replaceable>appiftrue(args)</replaceable>.</para>
108 <para>If <replaceable>expr</replaceable> is true, but <replaceable>appiftrue</replaceable> is not found,
109 then the application will return a non-zero value.</para>
110 </description>
111 </application>
112 ***/
113
114/* Maximum length of any variable */
115#define MAXRESULT 1024
116
117/*! Note
118 *
119 * The key difference between these two apps is exit status. In a
120 * nutshell, Exec tries to be transparent as possible, behaving
121 * in exactly the same way as if the application it calls was
122 * directly invoked from the dialplan.
123 *
124 * TryExec, on the other hand, provides a way to execute applications
125 * and catch any possible fatal error without actually fatally
126 * affecting the dialplan.
127 */
128
129static const char app_exec[] = "Exec";
130static const char app_tryexec[] = "TryExec";
131static const char app_execif[] = "ExecIf";
132
133static int exec_exec(struct ast_channel *chan, const char *data)
134{
135 int res = 0;
136 char *s, *appname, *endargs;
137 struct ast_app *app;
138 struct ast_str *args = NULL;
139
140 if (ast_strlen_zero(data))
141 return 0;
142
143 s = ast_strdupa(data);
144 appname = strsep(&s, "(");
145 if (s) {
146 endargs = strrchr(s, ')');
147 if (endargs)
148 *endargs = '\0';
149 if ((args = ast_str_create(16))) {
151 }
152 }
153 if (appname) {
154 app = pbx_findapp(appname);
155 if (app) {
156 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
157 } else {
158 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
159 res = -1;
160 }
161 }
162
163 ast_free(args);
164 return res;
165}
166
167static int tryexec_exec(struct ast_channel *chan, const char *data)
168{
169 int res = 0;
170 char *s, *appname, *endargs;
171 struct ast_app *app;
172 struct ast_str *args = NULL;
173
174 if (ast_strlen_zero(data))
175 return 0;
176
177 s = ast_strdupa(data);
178 appname = strsep(&s, "(");
179 if (s) {
180 endargs = strrchr(s, ')');
181 if (endargs)
182 *endargs = '\0';
183 if ((args = ast_str_create(16))) {
185 }
186 }
187 if (appname) {
188 app = pbx_findapp(appname);
189 if (app) {
190 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
191 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
192 } else {
193 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
194 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
195 }
196 }
197
198 ast_free(args);
199 return 0;
200}
201
202static int execif_exec(struct ast_channel *chan, const char *data)
203{
204 int res = 0;
205 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
206 struct ast_app *app = NULL;
208 AST_APP_ARG(expr);
209 AST_APP_ARG(remainder);
210 );
212 AST_APP_ARG(t);
213 AST_APP_ARG(f);
214 );
215 char *parse = ast_strdupa(data);
216
217 firstcomma = strchr(parse, ',');
218 firstquestion = strchr(parse, '?');
219
220 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
221 /* Deprecated syntax */
223 AST_APP_ARG(expr);
224 AST_APP_ARG(appname);
225 AST_APP_ARG(appargs);
226 );
227 AST_STANDARD_APP_ARGS(depr, parse);
228
229 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
230
231 /* Make the two syntaxes look the same */
232 expr.expr = depr.expr;
233 apps.t = depr.appname;
234 apps.f = NULL;
235 truedata = depr.appargs;
236 } else {
237 /* Preferred syntax */
238
239 AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
240 if (ast_strlen_zero(expr.remainder)) {
241 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
242 return -1;
243 }
244
245 AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
246
247 if (apps.t && (truedata = strchr(apps.t, '('))) {
248 *truedata++ = '\0';
249 if ((end = strrchr(truedata, ')'))) {
250 *end = '\0';
251 }
252 }
253
254 if (apps.f && (falsedata = strchr(apps.f, '('))) {
255 *falsedata++ = '\0';
256 if ((end = strrchr(falsedata, ')'))) {
257 *end = '\0';
258 }
259 }
260 }
261
262 if (pbx_checkcondition(expr.expr)) {
263 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
264 res = pbx_exec(chan, app, S_OR(truedata, ""));
265 } else {
266 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
267 res = -1;
268 }
269 } else if (!ast_strlen_zero(apps.f)) {
270 if ((app = pbx_findapp(apps.f))) {
271 res = pbx_exec(chan, app, S_OR(falsedata, ""));
272 } else {
273 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
274 res = -1;
275 }
276 }
277
278 return res;
279}
280
281static int unload_module(void)
282{
283 int res;
284
288
289 return res;
290}
291
292static int load_module(void)
293{
297 return res;
298}
299
300AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");
static const char app[]
Definition: app_adsiprog.c:56
static int tryexec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:167
static const char app_execif[]
Definition: app_exec.c:131
static int execif_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:202
static const char app_exec[]
Definition: app_exec.c:129
static int load_module(void)
Definition: app_exec.c:292
static int unload_module(void)
Definition: app_exec.c:281
static const char app_tryexec[]
Definition: app_exec.c:130
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:133
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
char * end
Definition: eagi_proxy.c:73
Generic File Format Support. Should be included by clients of the file handling routines....
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_NONSTANDARD_RAW_ARGS(args, parse, sep)
char * strsep(char **str, const char *delims)
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626
Core PBX routines and definitions.
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name.
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8282
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
#define NULL
Definition: resample.c:96
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:761
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
Definition: strings.h:80
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
Registered applications container.
Definition: pbx_app.c:67
ast_app: A registered application
Definition: pbx_app.c:45
Main Channel structure associated with a channel.
Support for dynamic strings.
Definition: strings.h:623
const char * args