Wed Jan 8 2020 09:49:53

Asterisk developer's documentation


app_exec.c File Reference

Exec application. More...

#include "asterisk.h"
#include "asterisk/file.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/app.h"

Go to the source code of this file.

Macros

#define MAXRESULT   1024
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int exec_exec (struct ast_channel *chan, const char *data)
 
static int execif_exec (struct ast_channel *chan, const char *data)
 
static int load_module (void)
 
static int tryexec_exec (struct ast_channel *chan, const char *data)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Executes dialplan applications" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static const char app_exec [] = "Exec"
 
static const char app_execif [] = "ExecIf"
 
static const char app_tryexec [] = "TryExec"
 
static struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Macro Definition Documentation

#define MAXRESULT   1024

Definition at line 117 of file app_exec.c.

Function Documentation

static void __reg_module ( void  )
static

Definition at line 302 of file app_exec.c.

static void __unreg_module ( void  )
static

Definition at line 302 of file app_exec.c.

static int exec_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 135 of file app_exec.c.

References app, args, ast_free, ast_log(), ast_str_buffer(), ast_str_create(), ast_str_substitute_variables(), ast_strdupa, ast_strlen_zero(), LOG_WARNING, pbx_exec(), pbx_findapp(), and strsep().

Referenced by load_module().

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 }
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
#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
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'ed dynamic length string.
Definition: strings.h:420
static const char app[]
Definition: app_adsiprog.c:49
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
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
#define ast_free(a)
Definition: astmm.h:97
ast_app: A registered application
Definition: pbx.c:971
static int execif_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 204 of file app_exec.c.

References app, AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), AST_NONSTANDARD_RAW_ARGS, AST_STANDARD_APP_ARGS, ast_strdupa, ast_strlen_zero(), f, LOG_ERROR, LOG_WARNING, parse(), pbx_checkcondition(), pbx_exec(), pbx_findapp(), and S_OR.

Referenced by load_module().

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 }
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
#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
static const char app[]
Definition: app_adsiprog.c:49
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
#define LOG_ERROR
Definition: logger.h:155
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
static struct ast_format f[]
Definition: format_g726.c:181
#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
#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 AST_NONSTANDARD_RAW_ARGS(args, parse, sep)
Definition: app.h:621
static int load_module ( void  )
static

Definition at line 294 of file app_exec.c.

References ast_register_application_xml, exec_exec(), execif_exec(), and tryexec_exec().

295 {
299  return res;
300 }
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:135
static const char app_tryexec[]
Definition: app_exec.c:132
static const char app_execif[]
Definition: app_exec.c:133
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
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:437
static int tryexec_exec ( struct ast_channel chan,
const char *  data 
)
static

Definition at line 169 of file app_exec.c.

References app, args, ast_free, ast_log(), ast_str_buffer(), ast_str_create(), ast_str_substitute_variables(), ast_strdupa, ast_strlen_zero(), LOG_WARNING, pbx_builtin_setvar_helper(), pbx_exec(), pbx_findapp(), and strsep().

Referenced by load_module().

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 }
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
#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
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[]
Definition: app_adsiprog.c:49
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
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
#define ast_free(a)
Definition: astmm.h:97
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
ast_app: A registered application
Definition: pbx.c:971
static int unload_module ( void  )
static

Definition at line 283 of file app_exec.c.

References ast_unregister_application().

284 {
285  int res;
286 
290 
291  return res;
292 }
static const char app_tryexec[]
Definition: app_exec.c:132
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 const char app_exec[]
Definition: app_exec.c:131

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Executes dialplan applications" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 302 of file app_exec.c.

const char app_exec[] = "Exec"
static

Note

The key difference between these two apps is exit status. In a nutshell, Exec tries to be transparent as possible, behaving in exactly the same way as if the application it calls was directly invoked from the dialplan.

TryExec, on the other hand, provides a way to execute applications and catch any possible fatal error without actually fatally affecting the dialplan.

Definition at line 131 of file app_exec.c.

const char app_execif[] = "ExecIf"
static

Definition at line 133 of file app_exec.c.

const char app_tryexec[] = "TryExec"
static

Definition at line 132 of file app_exec.c.

Definition at line 302 of file app_exec.c.