136 }
else if (*
var ==
')') {
138 }
else if (*
var ==
':' && parens == 0) {
140 sscanf(
var,
"%30d:%30d", offset, length);
158static char *
substring(
const char *
value,
int offset,
int length,
char *workspace,
size_t workspace_len)
160 char *ret = workspace;
168 if (offset == 0 && length >= lr)
172 offset = lr + offset;
182 if (length >= 0 && length < lr - offset)
184 else if (length < 0) {
185 if (lr > offset - length)
186 ret[lr + length - offset] =
'\0';
203 if (offset == 0 && length >= lr)
207 offset = lr + offset;
224 if (length >= 0 && length < lr) {
226 }
else if (length < 0) {
253 *ret = cret ? workspace :
NULL;
259 const char not_found =
'\0';
264 int i, need_substring;
298 if (!strncmp(
var,
"CALL", 4)) {
299 if (!strncmp(
var + 4,
"ING", 3)) {
300 if (!strcmp(
var + 7,
"PRES")) {
304 }
else if (!strcmp(
var + 7,
"ANI2")) {
307 }
else if (!strcmp(
var + 7,
"TON")) {
310 }
else if (!strcmp(
var + 7,
"TNS")) {
315 }
else if (!strcmp(
var,
"HINT")) {
317 }
else if (!strcmp(
var,
"HINTNAME")) {
319 }
else if (!strcmp(
var,
"EXTEN")) {
321 }
else if (!strcmp(
var,
"CONTEXT")) {
323 }
else if (!strcmp(
var,
"PRIORITY")) {
326 }
else if (!strcmp(
var,
"CHANNEL")) {
328 }
else if (!strcmp(
var,
"UNIQUEID")) {
330 }
else if (!strcmp(
var,
"HANGUPCAUSE")) {
335 if (s == ¬_found) {
336 if (!strcmp(
var,
"EPOCH")) {
339 }
else if (!strcmp(
var,
"SYSTEMNAME")) {
341 }
else if (!strcmp(
var,
"ASTCACHEDIR")) {
343 }
else if (!strcmp(
var,
"ASTETCDIR")) {
345 }
else if (!strcmp(
var,
"ASTMODDIR")) {
347 }
else if (!strcmp(
var,
"ASTVARLIBDIR")) {
349 }
else if (!strcmp(
var,
"ASTDBDIR")) {
351 }
else if (!strcmp(
var,
"ASTKEYDIR")) {
353 }
else if (!strcmp(
var,
"ASTDATADIR")) {
355 }
else if (!strcmp(
var,
"ASTAGIDIR")) {
357 }
else if (!strcmp(
var,
"ASTSPOOLDIR")) {
359 }
else if (!strcmp(
var,
"ASTRUNDIR")) {
361 }
else if (!strcmp(
var,
"ASTLOGDIR")) {
363 }
else if (!strcmp(
var,
"ASTSBINDIR")) {
365 }
else if (!strcmp(
var,
"ENTITYID")) {
371 for (i = 0; s == ¬_found && i <
ARRAY_LEN(places); i++) {
386 if (s == ¬_found || s ==
NULL) {
395 if (need_substring) {
397 ast_debug(2,
"Final result of '%s' is '%s'\n",
var, ret);
409 size_t *used,
int use_both)
412 const char *whereweare;
419 if (!substr1 || !substr3) {
430 const char *nextvar =
NULL;
431 const char *nextexp =
NULL;
432 const char *nextthing;
445 nextthing = strchr(whereweare,
'$');
447 pos = nextthing - whereweare;
448 switch (nextthing[1]) {
464 pos = strlen(whereweare);
483 vars = vare = nextvar + 2;
488 while (brackets && *vare) {
489 if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
493 }
else if (vare[0] ==
'{') {
495 }
else if (vare[0] ==
'}') {
497 }
else if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
516 ast_debug(5,
"Evaluating '%s' (from '%s' len %d)\n",
540 ast_debug(2,
"Function %s result is '%s' from channel\n",
543 if (!
c || (
c && res < 0 && use_both)) {
560 ast_log(
LOG_ERROR,
"Unable to allocate bogus channel for function value substitution.\n");
563 ast_debug(2,
"Function %s result is '%s' from headp\n",
570 ast_debug(2,
"Variable %s result is '%s' from channel\n",
573 if (!
c || (
c && !
result && use_both)) {
575 ast_debug(2,
"Variable %s result is '%s' from headp\n",
584 }
else if (nextexp) {
588 vars = vare = nextexp + 2;
593 while (brackets && *vare) {
594 if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
598 }
else if (vare[0] ==
'[') {
600 }
else if (vare[0] ==
']') {
602 }
else if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
675#define MAX_VARIABLE_SUB_RECURSE_DEPTH 15
680 const char *whereweare;
681 const char *orig_cp2 = cp2;
691 if (!recurse_depth) {
702 char *nextvar =
NULL;
703 char *nextexp =
NULL;
714 nextthing = strchr(whereweare,
'$');
716 pos = nextthing - whereweare;
717 switch (nextthing[1]) {
733 pos = strlen(whereweare);
742 memcpy(cp2, whereweare, pos);
760 vars = vare = nextvar + 2;
765 while (brackets && *vare) {
766 if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
770 }
else if (vare[0] ==
'{') {
772 }
else if (vare[0] ==
'}') {
774 }
else if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
820 ast_log(
LOG_ERROR,
"Unable to allocate bogus channel for function value substitution.\n");
824 ast_debug(2,
"Function %s result is '%s'\n", vars, cp4 ? cp4 :
"(null)");
828 if (
exten && !strcmp(vars,
"EXTEN")) {
831 }
else if (
context && !strcmp(vars,
"CONTEXT")) {
834 }
else if (pri && !strcmp(vars,
"PRIORITY")) {
844 length = strlen(cp4);
847 memcpy(cp2, cp4, length);
852 }
else if (nextexp) {
856 vars = vare = nextexp + 2;
861 while (brackets && *vare) {
862 if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
866 }
else if (vare[0] ==
'[') {
868 }
else if (vare[0] ==
']') {
870 }
else if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
900 ast_debug(1,
"Expression result is '%s'\n", cp2);
908 *used = cp2 - orig_cp2;
931 e->
command =
"dialplan show globals";
933 "Usage: dialplan show globals\n"
934 " List current global dialplan variables and their values\n";
946 ast_cli(
a->fd,
"\n -- %d variable(s)\n", i);
959 e->
command =
"dialplan show chanvar";
961 "Usage: dialplan show chanvar <channel>\n"
962 " List current channel variables and their values\n";
968 if (
a->argc != e->
args + 1) {
974 ast_cli(
a->fd,
"Channel '%s' not found\n",
a->argv[e->
args]);
999 char workspace[1024];
1004 e->
command =
"dialplan eval function";
1006 "Usage: dialplan eval function <name(args)>\n"
1007 " Evaluate a dialplan function call\n"
1008 " A dummy channel is used to evaluate\n"
1009 " the function call, so only global\n"
1010 " variables should be used.\n";
1016 if (
a->argc != e->
args + 1) {
1020 if (
a->argc != e->
args + 1 &&
a->argc != e->
args + 2) {
1031 ast_log(
LOG_WARNING,
"Failed to append a ulaw format to capabilities for channel nativeformats\n");
1036 ast_log(
LOG_WARNING,
"Failed to append an alaw format to capabilities for channel nativeformats\n");
1041 ast_log(
LOG_WARNING,
"Failed to append an h264 format to capabilities for channel nativeformats\n");
1045 c =
ast_channel_alloc(0,
AST_STATE_DOWN,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL, 0,
"CLIEval/%d", ++
cli_chan);
1047 ast_cli(
a->fd,
"Unable to allocate mock channel for application execution.\n");
1061 workspace[0] =
'\0';
1066 ast_cli(
a->fd,
"Return Value: %s (%d)\n", ret ?
"Failure" :
"Success", ret);
1067 ast_cli(
a->fd,
"Result: %s\n", workspace);
1076 e->
command =
"dialplan set global";
1078 "Usage: dialplan set global <name> <value>\n"
1079 " Set global dialplan variable <name> to <value>\n";
1085 if (
a->argc != e->
args + 2)
1089 ast_cli(
a->fd,
"\n -- Global variable '%s' set to '%s'\n",
a->argv[3],
a->argv[4]);
1097 const char *chan_name, *var_name, *var_value;
1101 e->
command =
"dialplan set chanvar";
1103 "Usage: dialplan set chanvar <channel> <varname> <value>\n"
1104 " Set channel variable <varname> to <value>\n";
1110 if (
a->argc != e->
args + 3)
1113 chan_name =
a->argv[e->
args];
1114 var_name =
a->argv[e->
args + 1];
1115 var_value =
a->argv[e->
args + 2];
1118 ast_cli(
a->fd,
"Channel '%s' not found\n", chan_name);
1126 ast_cli(
a->fd,
"\n -- Channel variable '%s' set to '%s' for '%s'\n", var_name, var_value, chan_name);
1165 const char *ret =
NULL;
1177 for (i = 0; i < 2; i++) {
1237 const char *nametail =
name;
1239 int old_value_existed = 0;
1241 if (
name[strlen(
name) - 1] ==
')') {
1256 if (*nametail ==
'_') {
1258 if (*nametail ==
'_')
1263 if (strcmp(
ast_var_name(newvariable), nametail) == 0) {
1279 }
else if (old_value_existed) {
1308 if (strchr(
name,
' ')) {
1309 ast_log(
LOG_WARNING,
"Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n",
name, mydata);
1337 for (x = 0; x <
args.argc; x++) {
1339 if (
pair.argc == 2) {
1342 ast_log(
LOG_WARNING,
"Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n",
pair.
name,
pair.value);
1363#ifdef TEST_FRAMEWORK
1371 {
"somevaluehere",
"CALLERID(num):0:25",
"somevaluehere"},
1372 {
"somevaluehere",
"CALLERID(num):0:5",
"somev"},
1373 {
"somevaluehere",
"CALLERID(num):4:5",
"value"},
1374 {
"somevaluehere",
"CALLERID(num):0:-4",
"somevalue"},
1375 {
"somevaluehere",
"CALLERID(num):-4",
"here"},
1380 info->name =
"variable_substrings";
1381 info->category =
"/main/pbx/";
1382 info->summary =
"Test variable substring resolution";
1383 info->description =
"Verify that variable substrings are calculated correctly";
1405 snprintf(tmp,
sizeof(tmp),
"${%s}",
test_strings[i][1]);
Prototypes for public functions only of internal interest,.
int ast_str_expr(struct ast_str **str, ssize_t maxlen, struct ast_channel *chan, char *expr)
Evaluate the given expression.
int ast_expr(char *expr, char *buf, int length, struct ast_channel *chan)
Evaluate the given expression.
char * strsep(char **str, const char *delims)
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
#define ast_strdupa(s)
duplicate a string in memory from the stack
const char * ast_channel_name(const struct ast_channel *chan)
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
struct varshead * ast_channel_varshead(struct ast_channel *chan)
#define ast_channel_alloc(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag,...)
Create a channel structure.
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
#define ast_channel_lock(chan)
int ast_channel_priority(const struct ast_channel *chan)
const char * ast_channel_uniqueid(const struct ast_channel *chan)
const char * ast_channel_context(const struct ast_channel *chan)
void ast_set_callerid(struct ast_channel *chan, const char *cid_num, const char *cid_name, const char *cid_ani)
Set caller ID number, name and ANI and generate AMI event.
struct ast_channel * ast_channel_get_by_name(const char *search)
Find a channel by name or uniqueid.
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
int ast_channel_hangupcause(const struct ast_channel *chan)
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
struct ast_channel * ast_channel_release(struct ast_channel *chan)
Unlink and release reference to a channel.
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#define ast_channel_unref(c)
Decrease channel reference count.
#define ast_dummy_channel_alloc()
Create a fake channel structure.
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
const char * ast_var_name(const struct ast_var_t *var)
#define ast_var_assign(name, value)
const char * ast_var_value(const struct ast_var_t *var)
void ast_var_delete(struct ast_var_t *var)
Standard Command Line Interface.
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
#define AST_CLI_DEFINE(fn, txt,...)
void ast_cli(int fd, const char *fmt,...)
char * ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos)
Command completion for the list of active channels.
#define ast_cli_register_multiple(e, len)
Register multiple commands.
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_channel_publish_varset(struct ast_channel *chan, const char *variable, const char *value)
Publish a ast_channel_publish_varset for a channel.
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_verb(level,...)
A set of macros to manage forward-linked lists.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_HEAD_NOLOCK_INIT_VALUE
Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
#define AST_LIST_INSERT_HEAD(head, elm, field)
Inserts a list entry at the head of a list.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Asterisk locking-related definitions:
#define ast_rwlock_wrlock(a)
#define ast_rwlock_rdlock(a)
#define AST_RWLOCK_DEFINE_STATIC(rwlock)
#define ast_rwlock_unlock(a)
Asterisk module definitions.
int ast_register_application2(const char *app, int(*execute)(struct ast_channel *, const char *), const char *synopsis, const char *description, void *mod)
Register an application.
int ast_unregister_application(const char *app)
Unregister an application.
Asterisk file paths, configured in asterisk.conf.
const char * ast_config_AST_KEY_DIR
const char * ast_config_AST_CACHE_DIR
const char * ast_config_AST_MODULE_DIR
const char * ast_config_AST_RUN_DIR
const char * ast_config_AST_DATA_DIR
const char * ast_config_AST_CONFIG_DIR
const char * ast_config_AST_SPOOL_DIR
const char * ast_config_AST_AGI_DIR
const char * ast_config_AST_VAR_DIR
const char * ast_config_AST_SYSTEM_NAME
const char * ast_config_AST_SBIN_DIR
const char * ast_config_AST_LOG_DIR
const char * ast_config_AST_DB
Core PBX routines and definitions.
int ast_func_read2(struct ast_channel *chan, const char *function, struct ast_str **str, ssize_t maxlen)
executes a read operation on a function
int ast_func_read(struct ast_channel *chan, const char *function, char *workspace, size_t len)
executes a read operation on a function
int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
executes a write operation on a function
int ast_str_get_hint(struct ast_str **hint, ssize_t hintsize, struct ast_str **name, ssize_t namesize, struct ast_channel *c, const char *context, const char *exten)
If an extension hint exists, return non-zero.
Private include file for pbx.
int load_pbx_variables(void)
void pbx_substitute_variables_helper_full(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count, size_t *used)
void ast_str_substitute_variables_full(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, struct varshead *headp, const char *templ, size_t *used)
int pbx_builtin_setvar(struct ast_channel *chan, const char *data)
Parse and set a single channel variable, where the name and value are separated with an '=' character...
static char * substring(const char *value, int offset, int length, char *workspace, size_t workspace_len)
takes a substring. It is ok to call with value == workspace.
void pbx_substitute_variables_varshead(struct varshead *headp, const char *cp1, char *cp2, int count)
void ast_str_substitute_variables_full2(struct ast_str **buf, ssize_t maxlen, struct ast_channel *c, struct varshead *headp, const char *templ, size_t *used, int use_both)
Perform variable/function/expression substitution on an ast_str.
static struct ast_cli_entry vars_cli[]
static ast_rwlock_t globalslock
void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
static char * handle_show_globals(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI support for listing global variables in a parseable way.
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
const char * ast_str_retrieve_variable(struct ast_str **str, ssize_t maxlen, struct ast_channel *c, struct varshead *headp, const char *var)
void ast_str_substitute_variables(struct ast_str **buf, ssize_t maxlen, struct ast_channel *chan, const char *templ)
static char * handle_set_global(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
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.
static void unload_pbx_variables(void)
void pbx_substitute_variables_helper_full_location(struct ast_channel *c, struct varshead *headp, const char *cp1, char *cp2, int count, size_t *used, const char *context, const char *exten, int pri)
Substitutes variables, similar to pbx_substitute_variables_helper_full, but allows passing the contex...
void ast_str_substitute_variables_varshead(struct ast_str **buf, ssize_t maxlen, struct varshead *headp, const char *templ)
int pbx_builtin_serialize_variables(struct ast_channel *chan, struct ast_str **buf)
Create a human-readable string, specifying all variables and their corresponding values.
void pbx_builtin_clear_globals(void)
void pbx_builtin_pushvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, without removing any previously set value.
static const char * ast_str_substring(struct ast_str *value, int offset, int length)
void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
Support for Asterisk built-in variables in the dialplan.
static const struct ast_channel_tech mock_channel_tech
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
extract offset:length from variable name.
static char * handle_show_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI support for listing chanvar's variables in a parseable way.
int pbx_builtin_setvar_multiple(struct ast_channel *chan, const char *vdata)
Parse and set multiple channel variables, where the pairs are separated by the ',' character,...
static char * handle_eval_function(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
CLI support for executing function.
static struct varshead globals
static struct ast_threadstorage varsub_recurse_level
#define MAX_VARIABLE_SUB_RECURSE_DEPTH
static char * handle_set_chanvar(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * substituted(struct ast_channel *channel, const char *string)
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
char * ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Append a non-NULL terminated substring to the end of a dynamic string.
void ast_str_reset(struct ast_str *buf)
Reset the content of a dynamic string. Useful before a series of ast_str_append.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
void ast_str_update(struct ast_str *buf)
Update the length of the buffer, after using ast_str merely as a buffer.
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
char * ast_str_set_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc)
Set a dynamic string to a non-NULL terminated substring.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Structure to describe a channel "technology", ie a channel driver See for examples:
Main Channel structure associated with a channel.
char exten[AST_MAX_EXTENSION]
descriptor for a cli entry.
int args
This gets set in ast_cli_register()
Support for dynamic strings.
struct ast_var_t::@216 entries
#define AST_TEST_REGISTER(cb)
#define ast_test_status_update(a, b, c...)
#define AST_TEST_UNREGISTER(cb)
#define AST_TEST_DEFINE(hdr)
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
struct ast_eid ast_eid_default
Global EID.