Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
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"
Include dependency graph for app_exec.c:

Go to the source code of this file.

Macros

#define MAXRESULT   1024
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const char app_exec [] = "Exec"
 
static const char app_execif [] = "ExecIf"
 
static const char app_tryexec [] = "TryExec"
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Exec application.

Author
Tilghman Lesher app_e.nosp@m.xec_.nosp@m._v002.nosp@m.@the.nosp@m.-tilg.nosp@m.hman.nosp@m..com
Philipp Dunkel phili.nosp@m.pp.d.nosp@m.unkel.nosp@m.@ebo.nosp@m.x.at

Definition in file app_exec.c.

Macro Definition Documentation

◆ MAXRESULT

#define MAXRESULT   1024

Definition at line 115 of file app_exec.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 300 of file app_exec.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 300 of file app_exec.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 300 of file app_exec.c.

◆ exec_exec()

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

Definition at line 133 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, NULL, pbx_exec(), pbx_findapp(), and strsep().

Referenced by load_module().

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))) {
150  ast_str_substitute_variables(&args, 0, chan, s);
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 }
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * args
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ast_free(a)
Definition: astmm.h:182
char * strsep(char **str, const char *delims)
ast_app: A registered application
Definition: pbx_app.c:45
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
static const char app[]
Definition: app_mysql.c:62
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:620

◆ execif_exec()

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

Definition at line 202 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, end, LOG_ERROR, LOG_WARNING, NULL, parse(), pbx_checkcondition(), pbx_exec(), pbx_findapp(), and S_OR.

Referenced by load_module().

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 }
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
Registered applications container.
Definition: pbx_app.c:67
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
int pbx_checkcondition(const char *condition)
Evaluate a condition.
Definition: pbx.c:8321
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
#define AST_NONSTANDARD_RAW_ARGS(args, parse, sep)
#define LOG_ERROR
Definition: logger.h:285
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1872
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
ast_app: A registered application
Definition: pbx_app.c:45
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
static const char app[]
Definition: app_mysql.c:62
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 292 of file app_exec.c.

References app_exec, app_execif, app_tryexec, ast_register_application_xml, exec_exec(), execif_exec(), and tryexec_exec().

293 {
297  return res;
298 }
static int exec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:133
static const char app_tryexec[]
Definition: app_exec.c:130
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 int tryexec_exec(struct ast_channel *chan, const char *data)
Definition: app_exec.c:167
static const char app_exec[]
Definition: app_exec.c:129
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626

◆ tryexec_exec()

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

Definition at line 167 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, NULL, pbx_builtin_setvar_helper(), pbx_exec(), pbx_findapp(), and strsep().

Referenced by load_module().

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))) {
184  ast_str_substitute_variables(&args, 0, chan, s);
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 }
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data)
Execute an application.
Definition: pbx_app.c:471
#define LOG_WARNING
Definition: logger.h:274
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
const char * args
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
#define ast_free(a)
Definition: astmm.h:182
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...
char * strsep(char **str, const char *delims)
ast_app: A registered application
Definition: pbx_app.c:45
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
static const char app[]
Definition: app_mysql.c:62
#define ast_str_create(init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:620

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 281 of file app_exec.c.

References app_exec, app_execif, app_tryexec, and ast_unregister_application().

282 {
283  int res;
284 
288 
289  return res;
290 }
static const char app_tryexec[]
Definition: app_exec.c:130
static const char app_execif[]
Definition: app_exec.c:131
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static const char app_exec[]
Definition: app_exec.c:129

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 300 of file app_exec.c.

◆ app_exec

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 129 of file app_exec.c.

Referenced by load_module(), and unload_module().

◆ app_execif

const char app_execif[] = "ExecIf"
static

Definition at line 131 of file app_exec.c.

Referenced by load_module(), and unload_module().

◆ app_tryexec

const char app_tryexec[] = "TryExec"
static

Definition at line 130 of file app_exec.c.

Referenced by load_module(), and unload_module().

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 300 of file app_exec.c.