Wed Jan 8 2020 09:49:39

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
35 
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/app.h"
41 
42 /*** DOCUMENTATION
43  <application name="Exec" language="en_US">
44  <synopsis>
45  Executes dialplan application.
46  </synopsis>
47  <syntax>
48  <parameter name="appname" required="true" hasparams="true">
49  <para>Application name and arguments of the dialplan application to execute.</para>
50  <argument name="arguments" required="true" />
51  </parameter>
52  </syntax>
53  <description>
54  <para>Allows an arbitrary application to be invoked even when not
55  hard coded into the dialplan. If the underlying application
56  terminates the dialplan, or if the application cannot be found,
57  Exec will terminate the dialplan.</para>
58  <para>To invoke external applications, see the application System.
59  If you would like to catch any error instead, see TryExec.</para>
60  </description>
61  </application>
62  <application name="TryExec" language="en_US">
63  <synopsis>
64  Executes dialplan application, always returning.
65  </synopsis>
66  <syntax>
67  <parameter name="appname" required="true" hasparams="true">
68  <argument name="arguments" required="true" />
69  </parameter>
70  </syntax>
71  <description>
72  <para>Allows an arbitrary application to be invoked even when not
73  hard coded into the dialplan. To invoke external applications
74  see the application System. Always returns to the dialplan.
75  The channel variable TRYSTATUS will be set to one of:
76  </para>
77  <variablelist>
78  <variable name="TRYSTATUS">
79  <value name="SUCCESS">
80  If the application returned zero.
81  </value>
82  <value name="FAILED">
83  If the application returned non-zero.
84  </value>
85  <value name="NOAPP">
86  If the application was not found or was not specified.
87  </value>
88  </variable>
89  </variablelist>
90  </description>
91  </application>
92  <application name="ExecIf" language="en_US">
93  <synopsis>
94  Executes dialplan application, conditionally.
95  </synopsis>
96  <syntax argsep="?">
97  <parameter name="expression" required="true" />
98  <parameter name="execapp" required="true" argsep=":">
99  <argument name="appiftrue" required="true" hasparams="true">
100  <argument name="args" required="true" />
101  </argument>
102  <argument name="appiffalse" required="false" hasparams="true">
103  <argument name="args" required="true" />
104  </argument>
105  </parameter>
106  </syntax>
107  <description>
108  <para>If <replaceable>expr</replaceable> is true, execute and return the
109  result of <replaceable>appiftrue(args)</replaceable>.</para>
110  <para>If <replaceable>expr</replaceable> is true, but <replaceable>appiftrue</replaceable> is not found,
111  then the application will return a non-zero value.</para>
112  </description>
113  </application>
114  ***/
115 
116 /* Maximum length of any variable */
117 #define MAXRESULT 1024
118 
119 /*! Note
120  *
121  * The key difference between these two apps is exit status. In a
122  * nutshell, Exec tries to be transparent as possible, behaving
123  * in exactly the same way as if the application it calls was
124  * directly invoked from the dialplan.
125  *
126  * TryExec, on the other hand, provides a way to execute applications
127  * and catch any possible fatal error without actually fatally
128  * affecting the dialplan.
129  */
130 
131 static const char app_exec[] = "Exec";
132 static const char app_tryexec[] = "TryExec";
133 static const char app_execif[] = "ExecIf";
134 
135 static int exec_exec(struct ast_channel *chan, const char *data)
136 {
137  int res = 0;
138  char *s, *appname, *endargs;
139  struct ast_app *app;
140  struct ast_str *args = NULL;
141 
142  if (ast_strlen_zero(data))
143  return 0;
144 
145  s = ast_strdupa(data);
146  appname = strsep(&s, "(");
147  if (s) {
148  endargs = strrchr(s, ')');
149  if (endargs)
150  *endargs = '\0';
151  if ((args = ast_str_create(16))) {
152  ast_str_substitute_variables(&args, 0, chan, s);
153  }
154  }
155  if (appname) {
156  app = pbx_findapp(appname);
157  if (app) {
158  res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
159  } else {
160  ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
161  res = -1;
162  }
163  }
164 
165  ast_free(args);
166  return res;
167 }
168 
169 static int tryexec_exec(struct ast_channel *chan, const char *data)
170 {
171  int res = 0;
172  char *s, *appname, *endargs;
173  struct ast_app *app;
174  struct ast_str *args = NULL;
175 
176  if (ast_strlen_zero(data))
177  return 0;
178 
179  s = ast_strdupa(data);
180  appname = strsep(&s, "(");
181  if (s) {
182  endargs = strrchr(s, ')');
183  if (endargs)
184  *endargs = '\0';
185  if ((args = ast_str_create(16))) {
186  ast_str_substitute_variables(&args, 0, chan, s);
187  }
188  }
189  if (appname) {
190  app = pbx_findapp(appname);
191  if (app) {
192  res = pbx_exec(chan, app, args ? ast_str_buffer(args) : NULL);
193  pbx_builtin_setvar_helper(chan, "TRYSTATUS", res ? "FAILED" : "SUCCESS");
194  } else {
195  ast_log(LOG_WARNING, "Could not find application (%s)\n", appname);
196  pbx_builtin_setvar_helper(chan, "TRYSTATUS", "NOAPP");
197  }
198  }
199 
200  ast_free(args);
201  return 0;
202 }
203 
204 static int execif_exec(struct ast_channel *chan, const char *data)
205 {
206  int res = 0;
207  char *truedata = NULL, *falsedata = NULL, *end, *firstcomma, *firstquestion;
208  struct ast_app *app = NULL;
210  AST_APP_ARG(expr);
211  AST_APP_ARG(remainder);
212  );
214  AST_APP_ARG(t);
215  AST_APP_ARG(f);
216  );
217  char *parse = ast_strdupa(data);
218 
219  firstcomma = strchr(parse, ',');
220  firstquestion = strchr(parse, '?');
221 
222  if ((firstcomma != NULL && firstquestion != NULL && firstcomma < firstquestion) || (firstquestion == NULL)) {
223  /* Deprecated syntax */
225  AST_APP_ARG(expr);
226  AST_APP_ARG(appname);
227  AST_APP_ARG(appargs);
228  );
229  AST_STANDARD_APP_ARGS(depr, parse);
230 
231  ast_log(LOG_WARNING, "Deprecated syntax found. Please upgrade to using ExecIf(<expr>?%s(%s))\n", depr.appname, depr.appargs);
232 
233  /* Make the two syntaxes look the same */
234  expr.expr = depr.expr;
235  apps.t = depr.appname;
236  apps.f = NULL;
237  truedata = depr.appargs;
238  } else {
239  /* Preferred syntax */
240 
241  AST_NONSTANDARD_RAW_ARGS(expr, parse, '?');
242  if (ast_strlen_zero(expr.remainder)) {
243  ast_log(LOG_ERROR, "Usage: ExecIf(<expr>?<appiftrue>(<args>)[:<appiffalse>(<args)])\n");
244  return -1;
245  }
246 
247  AST_NONSTANDARD_RAW_ARGS(apps, expr.remainder, ':');
248 
249  if (apps.t && (truedata = strchr(apps.t, '('))) {
250  *truedata++ = '\0';
251  if ((end = strrchr(truedata, ')'))) {
252  *end = '\0';
253  }
254  }
255 
256  if (apps.f && (falsedata = strchr(apps.f, '('))) {
257  *falsedata++ = '\0';
258  if ((end = strrchr(falsedata, ')'))) {
259  *end = '\0';
260  }
261  }
262  }
263 
264  if (pbx_checkcondition(expr.expr)) {
265  if (!ast_strlen_zero(apps.t) && (app = pbx_findapp(apps.t))) {
266  res = pbx_exec(chan, app, S_OR(truedata, ""));
267  } else {
268  ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.t);
269  res = -1;
270  }
271  } else if (!ast_strlen_zero(apps.f)) {
272  if ((app = pbx_findapp(apps.f))) {
273  res = pbx_exec(chan, app, S_OR(falsedata, ""));
274  } else {
275  ast_log(LOG_WARNING, "Could not find application! (%s)\n", apps.f);
276  res = -1;
277  }
278  }
279 
280  return res;
281 }
282 
283 static int unload_module(void)
284 {
285  int res;
286 
287  res = ast_unregister_application(app_exec);
288  res |= ast_unregister_application(app_tryexec);
289  res |= ast_unregister_application(app_execif);
290 
291  return res;
292 }
293 
294 static int load_module(void)
295 {
296  int res = ast_register_application_xml(app_exec, exec_exec);
297  res |= ast_register_application_xml(app_tryexec, tryexec_exec);
298  res |= ast_register_application_xml(app_execif, execif_exec);
299  return res;
300 }
301 
302 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Executes dialplan applications");
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:135
static int load_module(void)
Definition: app_exec.c:294
static const char app_tryexec[]
Definition: app_exec.c:132
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
static int unload_module(void)
Definition: app_exec.c:283
char * strsep(char **str, const char *delims)
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: pbx.c:1537
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx.c:1497
Definition: pbx.c:1301
#define LOG_WARNING
Definition: logger.h:144
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:10719
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
Definition: pbx.c:4468
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
static const char app_execif[]
Definition: app_exec.c:133
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx.c:7705
static int execif_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:204
static int tryexec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:169
static const char app_exec[]
Definition: app_exec.c:131
static const char app[]
Definition: app_adsiprog.c:49
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Core PBX routines and definitions.
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
static struct @350 args
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
#define ast_free(a)
Definition: astmm.h:97
static struct ast_format f[]
Definition: format_g726.c:181
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...
Definition: pbx.c:10546
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:77
ast_app: A registered application
Definition: pbx.c:971
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
union ast_frame::@172 data
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
#define AST_NONSTANDARD_RAW_ARGS(args, parse, sep)
Definition: app.h:621
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180