Skip to content

Commit 92d408f

Browse files
InterLinked1Friendly Automation
authored andcommitted
cli: Add command to evaluate dialplan functions.
Adds the dialplan eval function commands to evaluate a dialplan function from the CLI. The return value and function result are printed out and can be used for testing or debugging. ASTERISK-29820 #close Change-Id: I833e97ea54c49336aca145330a2adeebfad05209
1 parent 0c70d49 commit 92d408f

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Subject: cli
2+
3+
A new CLI command 'dialplan eval function' has been
4+
added which allows users to test the behavior of
5+
dialplan function calls directly from the CLI.

main/pbx_variables.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,52 @@ static char *handle_show_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cl
924924
return CLI_SUCCESS;
925925
}
926926

927+
/*! \brief CLI support for executing function */
928+
static char *handle_eval_function(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
929+
{
930+
struct ast_channel *c = NULL;
931+
char *fn, *substituted;
932+
int ret;
933+
char workspace[1024];
934+
935+
switch (cmd) {
936+
case CLI_INIT:
937+
e->command = "dialplan eval function";
938+
e->usage =
939+
"Usage: dialplan eval function <name(args)>\n"
940+
" Evaluate a dialplan function call\n"
941+
" A dummy channel is used to evaluate\n"
942+
" the function call, so only global\n"
943+
" variables should be used.\n";
944+
return NULL;
945+
case CLI_GENERATE:
946+
return NULL;
947+
}
948+
949+
if (a->argc != e->args + 1) {
950+
return CLI_SHOWUSAGE;
951+
}
952+
953+
c = ast_dummy_channel_alloc();
954+
if (!c) {
955+
ast_cli(a->fd, "Unable to allocate bogus channel for function evaluation.\n");
956+
return CLI_FAILURE;
957+
}
958+
959+
fn = (char *) a->argv[3];
960+
pbx_substitute_variables_helper(c, fn, workspace, sizeof(workspace));
961+
substituted = ast_strdupa(workspace);
962+
workspace[0] = '\0';
963+
ret = ast_func_read(c, substituted, workspace, sizeof(workspace));
964+
965+
c = ast_channel_unref(c);
966+
967+
ast_cli(a->fd, "Return Value: %s (%d)\n", ret ? "Failure" : "Success", ret);
968+
ast_cli(a->fd, "Result: %s\n", workspace);
969+
970+
return CLI_SUCCESS;
971+
}
972+
927973
static char *handle_set_global(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
928974
{
929975
switch (cmd) {
@@ -1218,6 +1264,7 @@ void pbx_builtin_clear_globals(void)
12181264
static struct ast_cli_entry vars_cli[] = {
12191265
AST_CLI_DEFINE(handle_show_globals, "Show global dialplan variables"),
12201266
AST_CLI_DEFINE(handle_show_chanvar, "Show channel variables"),
1267+
AST_CLI_DEFINE(handle_eval_function, "Evaluate dialplan function"),
12211268
AST_CLI_DEFINE(handle_set_global, "Set global dialplan variable"),
12221269
AST_CLI_DEFINE(handle_set_chanvar, "Set a channel variable"),
12231270
};

0 commit comments

Comments
 (0)