Asterisk - The Open Source Telephony Project GIT-master-abe0018
Functions | Variables
res_cliexec.c File Reference

Execute dialplan applications from the CLI. More...

#include "asterisk.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h"
#include "asterisk/frame.h"
#include "asterisk/format_cache.h"
Include dependency graph for res_cliexec.c:

Go to the source code of this file.

Functions

 AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Simple dialplan execution from the CLI")
 
static char * handle_exec (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 CLI support for executing application. More...
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static int cli_chan = 0
 
static struct ast_cli_entry cli_cliorig []
 
static const struct ast_channel_tech mock_channel_tech
 

Detailed Description

Execute dialplan applications from the CLI.

Author
Naveen Albert aster.nosp@m.isk@.nosp@m.phrea.nosp@m.knet.nosp@m..org

Definition in file res_cliexec.c.

Function Documentation

◆ AST_MODULE_INFO_STANDARD_EXTENDED()

AST_MODULE_INFO_STANDARD_EXTENDED ( ASTERISK_GPL_KEY  ,
"Simple dialplan execution from the CLI"   
)

◆ handle_exec()

static char * handle_exec ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

CLI support for executing application.

Definition at line 48 of file res_cliexec.c.

49{
50 struct ast_channel *c = NULL;
51 RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
52 char *app_name, *app_args;
53 int ret = 0;
54 struct ast_app *app;
55
56 switch (cmd) {
57 case CLI_INIT:
58 e->command = "dialplan exec application";
59 e->usage =
60 "Usage: dialplan exec application <appname> [<args>]\n"
61 " Execute a single dialplan application call for\n"
62 " testing. A mock channel is used to execute\n"
63 " the application, so it may not make\n"
64 " sense to use all applications, and only\n"
65 " global variables should be used.\n"
66 " The ulaw, alaw, and h264 codecs are available.\n";
67 return NULL;
68 case CLI_GENERATE:
69 return NULL;
70 }
71
72 if (a->argc != e->args + 1 && a->argc != e->args + 2) {
73 return CLI_SHOWUSAGE;
74 }
75
76 app_name = (char *) a->argv[3];
77 app_args = a->argc == e->args + 2 ? (char *) a->argv[4] : NULL;
78
79 if (!app_name) {
80 return CLI_FAILURE;
81 }
82
84 if (!caps) {
85 ast_log(LOG_WARNING, "Could not allocate an empty format capabilities structure\n");
86 return CLI_FAILURE;
87 }
88
90 ast_log(LOG_WARNING, "Failed to append a ulaw format to capabilities for channel nativeformats\n");
91 return CLI_FAILURE;
92 }
93
95 ast_log(LOG_WARNING, "Failed to append an alaw format to capabilities for channel nativeformats\n");
96 return CLI_FAILURE;
97 }
98
100 ast_log(LOG_WARNING, "Failed to append an h264 format to capabilities for channel nativeformats\n");
101 return CLI_FAILURE;
102 }
103
104 c = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "CLIExec/%d", ++cli_chan);
105 if (!c) {
106 ast_cli(a->fd, "Unable to allocate mock channel for application execution.\n");
107 return CLI_FAILURE;
108 }
116
118 if (!app) {
119 ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
120 ast_hangup(c);
121 return CLI_FAILURE;
122 } else {
123 struct ast_str *substituted_args = ast_str_create(16);
124
125 if (substituted_args) {
126 ast_str_substitute_variables(&substituted_args, 0, c, app_args);
127 ast_cli(a->fd, "Executing: %s(%s)\n", app_name, ast_str_buffer(substituted_args));
128 ret = pbx_exec(c, app, ast_str_buffer(substituted_args));
129 ast_free(substituted_args);
130 } else {
131 ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
132 ast_cli(a->fd, "Executing: %s(%s)\n", app_name, app_args);
133 ret = pbx_exec(c, app, app_args);
134 }
135 }
136
137 ast_hangup(c); /* no need to unref separately */
138
139 ast_cli(a->fd, "Return Value: %s (%d)\n", ret ? "Failure" : "Success", ret);
140
141 return CLI_SUCCESS;
142}
static const char app[]
Definition: app_adsiprog.c:56
#define ast_free(a)
Definition: astmm.h:180
#define ast_log
Definition: astobj2.c:42
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
Definition: channel.h:1258
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
#define ast_channel_unlock(chan)
Definition: channel.h:2923
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
@ AST_STATE_DOWN
Definition: channelstate.h:36
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153
#define CLI_FAILURE
Definition: cli.h:46
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:176
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
struct ast_format * ast_format_slin
Built-in cached signed linear 8kHz format.
Definition: format_cache.c:41
struct ast_format * ast_format_alaw
Built-in cached alaw format.
Definition: format_cache.c:91
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition: format_cap.h:38
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
#define LOG_WARNING
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
struct ast_app * pbx_findapp(const char *app)
Look up an application.
Definition: ael_main.c:165
const char * app_name(struct ast_app *app)
Definition: pbx_app.c:463
static int cli_chan
Definition: res_cliexec.c:45
static const struct ast_channel_tech mock_channel_tech
Definition: res_cliexec.c:42
#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 ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
ast_app: A registered application
Definition: pbx_app.c:45
Main Channel structure associated with a channel.
int args
This gets set in ast_cli_register()
Definition: cli.h:185
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
Support for dynamic strings.
Definition: strings.h:623
static struct test_val a
static struct test_val c
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References a, ao2_cleanup, app, app_name(), ast_cli_entry::args, ast_channel_alloc, ast_channel_nativeformats_set(), ast_channel_set_rawreadformat(), ast_channel_set_rawwriteformat(), ast_channel_set_readformat(), ast_channel_set_writeformat(), ast_channel_tech_set(), ast_channel_unlock, ast_cli(), ast_format_alaw, ast_format_cap_alloc, ast_format_cap_append, AST_FORMAT_CAP_FLAG_DEFAULT, ast_format_h264, ast_format_slin, ast_format_ulaw, ast_free, ast_hangup(), ast_log, AST_STATE_DOWN, ast_str_buffer(), ast_str_create, ast_str_substitute_variables(), c, cli_chan, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, LOG_WARNING, mock_channel_tech, NULL, pbx_exec(), pbx_findapp(), RAII_VAR, and ast_cli_entry::usage.

◆ load_module()

static int load_module ( void  )
static

Definition at line 153 of file res_cliexec.c.

154{
155 int res;
158}
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_cli_entry cli_cliorig[]
Definition: res_cliexec.c:144
#define ARRAY_LEN(a)
Definition: utils.h:666

References ARRAY_LEN, ast_cli_register_multiple, AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and cli_cliorig.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 148 of file res_cliexec.c.

149{
151}
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: clicompat.c:30

References ARRAY_LEN, ast_cli_unregister_multiple(), and cli_cliorig.

Variable Documentation

◆ cli_chan

int cli_chan = 0
static

Definition at line 45 of file res_cliexec.c.

Referenced by handle_exec().

◆ cli_cliorig

struct ast_cli_entry cli_cliorig[]
static
Initial value:
= {
{ .handler = handle_exec , .summary = "Execute a dialplan application" ,},
}
static char * handle_exec(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI support for executing application.
Definition: res_cliexec.c:48

Definition at line 144 of file res_cliexec.c.

Referenced by load_module(), and unload_module().

◆ mock_channel_tech

const struct ast_channel_tech mock_channel_tech
static
Initial value:
= {
}

Definition at line 42 of file res_cliexec.c.

Referenced by handle_exec().