Asterisk - The Open Source Telephony Project GIT-master-8924258
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
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 <since>
43 <version>1.0.0</version>
44 </since>
45 <synopsis>
46 Executes dialplan application.
47 </synopsis>
48 <syntax>
49 <parameter name="appname" required="true" hasparams="true">
50 <para>Application name and arguments of the dialplan application to execute.</para>
51 <argument name="arguments" required="true" />
52 </parameter>
53 </syntax>
54 <description>
55 <para>Allows an arbitrary application to be invoked even when not
56 hard coded into the dialplan. If the underlying application
57 terminates the dialplan, or if the application cannot be found,
58 Exec will terminate the dialplan.</para>
59 <para>To invoke external applications, see the application System.
60 If you would like to catch any error instead, see TryExec.</para>
61 </description>
62 </application>
63 <application name="TryExec" language="en_US">
64 <since>
65 <version>1.4.0</version>
66 </since>
67 <synopsis>
68 Executes dialplan application, always returning.
69 </synopsis>
70 <syntax>
71 <parameter name="appname" required="true" hasparams="true">
72 <argument name="arguments" required="true" />
73 </parameter>
74 </syntax>
75 <description>
76 <para>Allows an arbitrary application to be invoked even when not
77 hard coded into the dialplan. To invoke external applications
78 see the application System. Always returns to the dialplan.
79 The channel variable TRYSTATUS will be set to one of:
80 </para>
81 <variablelist>
82 <variable name="TRYSTATUS">
83 <value name="SUCCESS">
84 If the application returned zero.
85 </value>
86 <value name="FAILED">
87 If the application returned non-zero.
88 </value>
89 <value name="NOAPP">
90 If the application was not found or was not specified.
91 </value>
92 </variable>
93 </variablelist>
94 </description>
95 </application>
96 <application name="ExecIf" language="en_US">
97 <since>
98 <version>1.2.0</version>
99 </since>
100 <synopsis>
101 Executes dialplan application, conditionally.
102 </synopsis>
103 <syntax argsep="?">
104 <parameter name="expression" required="true" />
105 <parameter name="execapp" required="true" argsep=":">
106 <argument name="appiftrue" required="true" hasparams="true">
107 <argument name="args" required="true" />
108 </argument>
109 <argument name="appiffalse" required="false" hasparams="true">
110 <argument name="args" required="true" />
111 </argument>
112 </parameter>
113 </syntax>
114 <description>
115 <para>If <replaceable>expr</replaceable> is true, execute and return the
116 result of <replaceable>appiftrue(args)</replaceable>.</para>
117 <para>If <replaceable>expr</replaceable> is true, but <replaceable>appiftrue</replaceable> is not found,
118 then the application will return a non-zero value.</para>
119 </description>
120 </application>
121 ***/
122
123/* Maximum length of any variable */
124#define MAXRESULT 1024
125
126/*! Note
127 *
128 * The key difference between these two apps is exit status. In a
129 * nutshell, Exec tries to be transparent as possible, behaving
130 * in exactly the same way as if the application it calls was
131 * directly invoked from the dialplan.
132 *
133 * TryExec, on the other hand, provides a way to execute applications
134 * and catch any possible fatal error without actually fatally
135 * affecting the dialplan.
136 */
137
138static const char app_exec[] = "Exec";
139static const char app_tryexec[] = "TryExec";
140static const char app_execif[] = "ExecIf";
141
142static int exec_exec(struct ast_channel *chan, const char *data)
143{
144 int res = 0;
145 char *s, *appname, *endargs;
146 struct ast_app *app;
147 struct ast_str *args = NULL;
148
149 if (ast_strlen_zero(data))
150 return 0;
151
152 s = ast_strdupa(data);
153 appname = strsep(&s, "(");
154 if (s) {
155 endargs = strrchr(s, ')');
156 if (endargs)
157 *endargs = '\0';
158 if ((args = ast_str_create(16))) {
160 }
161 }
162 if (appname) {
163 app = pbx_findapp(appname);
164 if (app) {
165 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
166 } else {
167 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
168 res = -1;
169 }
170 }
171
172 ast_free(args);
173 return res;
174}
175
176static int tryexec_exec(struct ast_channel *chan, const char *data)
177{
178 int res = 0;
179 char *s, *appname, *endargs;
180 struct ast_app *app;
181 struct ast_str *args = NULL;
182
183 if (ast_strlen_zero(data))
184 return 0;
185
186 s = ast_strdupa(data);
187 appname = strsep(&s, "(");
188 if (s) {
189 endargs = strrchr(s, ')');
190 if (endargs)
191 *endargs = '\0';
192 if ((args = ast_str_create(16))) {
194 }
195 }
196 if (appname) {
197 app = pbx_findapp(appname);
198 if (app) {
199 res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
200 pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
201 } else {
202 ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
203 pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
204 }
205 }
206
207 ast_free(args);
208 return 0;
209}
210
211static int execif_exec(struct ast_channel *chan, const char *data)
212{
213 int res = 0;
214 char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
215 struct ast_app *app = NULL;
217 AST_APP_ARG(expr);
218 AST_APP_ARG(remainder);
219 );
221 AST_APP_ARG(t);
222 AST_APP_ARG(f);
223 );
224 char *parse = ast_strdupa(data);
225
226 firstcomma = strchr(parse, ',');
227 firstquestion = strchr(parse, '?');
228
229 if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
230 /* Deprecated syntax */
232 AST_APP_ARG(expr);
233 AST_APP_ARG(appname);
234 AST_APP_ARG(appargs);
235 );
236 AST_STANDARD_APP_ARGS(depr, parse);
237
238 ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
239
240 /* Make the two syntaxes look the same */
241 expr.expr = depr.expr;
242 apps.t = depr.appname;
243 apps.f = NULL;
244 truedata = depr.appargs;
245 } else {
246 /* Preferred syntax */
247
248 AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
249 if (ast_strlen_zero(expr.remainder)) {
250 ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
251 return -1;
252 }
253
254 AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
255
256 if (apps.t && (truedata = strchr(apps.t, '('))) {
257 *truedata++ = '\0';
258 if ((end = strrchr(truedata, ')'))) {
259 *end = '\0';
260 }
261 }
262
263 if (apps.f && (falsedata = strchr(apps.f, '('))) {
264 *falsedata++ = '\0';
265 if ((end = strrchr(falsedata, ')'))) {
266 *end = '\0';
267 }
268 }
269 }
270
271 if (pbx_checkcondition(expr.expr)) {
272 if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
273 res = pbx_exec(chan, app, S_OR(truedata, ""));
274 } else {
275 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
276 res = -1;
277 }
278 } else if (!ast_strlen_zero(apps.f)) {
279 if ((app = pbx_findapp(apps.f))) {
280 res = pbx_exec(chan, app, S_OR(falsedata, ""));
281 } else {
282 ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
283 res = -1;
284 }
285 }
286
287 return res;
288}
289
290static int unload_module(void)
291{
292 int res;
293
297
298 return res;
299}
300
301static int load_module(void)
302{
306 return res;
307}
308
309AST_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:176
static const char app_execif[]
Definition: app_exec.c:140
static int execif_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:211
static const char app_exec[]
Definition: app_exec.c:138
static int load_module(void)
Definition: app_exec.c:301
static int unload_module(void)
Definition: app_exec.c:290
static const char app_tryexec[]
Definition: app_exec.c:139
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:142
char * strsep(char **str, const char *delims)
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)
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:581
#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:640
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:8297
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:68
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