135 }
else if (*
var ==
')') {
137 }
else if (*
var ==
':' && parens == 0) {
139 sscanf(
var,
"%30d:%30d", offset, length);
157static char *
substring(
const char *
value,
int offset,
int length,
char *workspace,
size_t workspace_len)
159 char *ret = workspace;
167 if (offset == 0 && length >= lr)
171 offset = lr + offset;
181 if (length >= 0 && length < lr - offset)
183 else if (length < 0) {
184 if (lr > offset - length)
185 ret[lr + length - offset] =
'\0';
202 if (offset == 0 && length >= lr)
206 offset = lr + offset;
223 if (length >= 0 && length < lr) {
225 }
else if (length < 0) {
252 *ret = cret ? workspace :
NULL;
258 const char not_found =
'\0';
263 int i, need_substring;
297 if (!strncmp(
var,
"CALL", 4)) {
298 if (!strncmp(
var + 4,
"ING", 3)) {
299 if (!strcmp(
var + 7,
"PRES")) {
303 }
else if (!strcmp(
var + 7,
"ANI2")) {
306 }
else if (!strcmp(
var + 7,
"TON")) {
309 }
else if (!strcmp(
var + 7,
"TNS")) {
314 }
else if (!strcmp(
var,
"HINT")) {
316 }
else if (!strcmp(
var,
"HINTNAME")) {
318 }
else if (!strcmp(
var,
"EXTEN")) {
320 }
else if (!strcmp(
var,
"CONTEXT")) {
322 }
else if (!strcmp(
var,
"PRIORITY")) {
325 }
else if (!strcmp(
var,
"CHANNEL")) {
327 }
else if (!strcmp(
var,
"UNIQUEID")) {
329 }
else if (!strcmp(
var,
"HANGUPCAUSE")) {
334 if (s == ¬_found) {
335 if (!strcmp(
var,
"EPOCH")) {
338 }
else if (!strcmp(
var,
"SYSTEMNAME")) {
340 }
else if (!strcmp(
var,
"ASTCACHEDIR")) {
342 }
else if (!strcmp(
var,
"ASTETCDIR")) {
344 }
else if (!strcmp(
var,
"ASTMODDIR")) {
346 }
else if (!strcmp(
var,
"ASTVARLIBDIR")) {
348 }
else if (!strcmp(
var,
"ASTDBDIR")) {
350 }
else if (!strcmp(
var,
"ASTKEYDIR")) {
352 }
else if (!strcmp(
var,
"ASTDATADIR")) {
354 }
else if (!strcmp(
var,
"ASTAGIDIR")) {
356 }
else if (!strcmp(
var,
"ASTSPOOLDIR")) {
358 }
else if (!strcmp(
var,
"ASTRUNDIR")) {
360 }
else if (!strcmp(
var,
"ASTLOGDIR")) {
362 }
else if (!strcmp(
var,
"ASTSBINDIR")) {
364 }
else if (!strcmp(
var,
"ENTITYID")) {
370 for (i = 0; s == ¬_found && i <
ARRAY_LEN(places); i++) {
385 if (s == ¬_found || s ==
NULL) {
394 if (need_substring) {
396 ast_debug(2,
"Final result of '%s' is '%s'\n",
var, ret);
408 size_t *used,
int use_both)
411 const char *whereweare;
418 if (!substr1 || !substr3) {
429 const char *nextvar =
NULL;
430 const char *nextexp =
NULL;
431 const char *nextthing;
444 nextthing = strchr(whereweare,
'$');
446 pos = nextthing - whereweare;
447 switch (nextthing[1]) {
463 pos = strlen(whereweare);
482 vars = vare = nextvar + 2;
487 while (brackets && *vare) {
488 if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
492 }
else if (vare[0] ==
'{') {
494 }
else if (vare[0] ==
'}') {
496 }
else if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
515 ast_debug(5,
"Evaluating '%s' (from '%s' len %d)\n",
539 ast_debug(2,
"Function %s result is '%s' from channel\n",
542 if (!
c || (
c && res < 0 && use_both)) {
559 ast_log(
LOG_ERROR,
"Unable to allocate bogus channel for function value substitution.\n");
562 ast_debug(2,
"Function %s result is '%s' from headp\n",
569 ast_debug(2,
"Variable %s result is '%s' from channel\n",
572 if (!
c || (
c && !
result && use_both)) {
574 ast_debug(2,
"Variable %s result is '%s' from headp\n",
583 }
else if (nextexp) {
587 vars = vare = nextexp + 2;
592 while (brackets && *vare) {
593 if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
597 }
else if (vare[0] ==
'[') {
599 }
else if (vare[0] ==
']') {
601 }
else if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
674#define MAX_VARIABLE_SUB_RECURSE_DEPTH 15
679 const char *whereweare;
680 const char *orig_cp2 = cp2;
690 if (!recurse_depth) {
701 char *nextvar =
NULL;
702 char *nextexp =
NULL;
713 nextthing = strchr(whereweare,
'$');
715 pos = nextthing - whereweare;
716 switch (nextthing[1]) {
732 pos = strlen(whereweare);
741 memcpy(cp2, whereweare, pos);
759 vars = vare = nextvar + 2;
764 while (brackets && *vare) {
765 if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
769 }
else if (vare[0] ==
'{') {
771 }
else if (vare[0] ==
'}') {
773 }
else if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
819 ast_log(
LOG_ERROR,
"Unable to allocate bogus channel for function value substitution.\n");
823 ast_debug(2,
"Function %s result is '%s'\n", vars, cp4 ? cp4 :
"(null)");
827 if (
exten && !strcmp(vars,
"EXTEN")) {
830 }
else if (
context && !strcmp(vars,
"CONTEXT")) {
833 }
else if (pri && !strcmp(vars,
"PRIORITY")) {
843 length = strlen(cp4);
846 memcpy(cp2, cp4, length);
851 }
else if (nextexp) {
855 vars = vare = nextexp + 2;
860 while (brackets && *vare) {
861 if ((vare[0] ==
'$') && (vare[1] ==
'[')) {
865 }
else if (vare[0] ==
'[') {
867 }
else if (vare[0] ==
']') {
869 }
else if ((vare[0] ==
'$') && (vare[1] ==
'{')) {
899 ast_debug(1,
"Expression result is '%s'\n", cp2);
907 *used = cp2 - orig_cp2;
930 e->
command =
"dialplan show globals";
932 "Usage: dialplan show globals\n"
933 " List current global dialplan variables and their values\n";
945 ast_cli(
a->fd,
"\n -- %d variable(s)\n", i);
958 e->
command =
"dialplan show chanvar";
960 "Usage: dialplan show chanvar <channel>\n"
961 " List current channel variables and their values\n";
967 if (
a->argc != e->
args + 1) {
973 ast_cli(
a->fd,
"Channel '%s' not found\n",
a->argv[e->
args]);
993 char workspace[1024];
997 e->
command =
"dialplan eval function";
999 "Usage: dialplan eval function <name(args)>\n"
1000 " Evaluate a dialplan function call\n"
1001 " A dummy channel is used to evaluate\n"
1002 " the function call, so only global\n"
1003 " variables should be used.\n";
1009 if (
a->argc != e->
args + 1) {
1015 ast_cli(
a->fd,
"Unable to allocate bogus channel for function evaluation.\n");
1019 fn = (
char *)
a->argv[3];
1022 workspace[0] =
'\0';
1027 ast_cli(
a->fd,
"Return Value: %s (%d)\n", ret ?
"Failure" :
"Success", ret);
1028 ast_cli(
a->fd,
"Result: %s\n", workspace);
1037 e->
command =
"dialplan set global";
1039 "Usage: dialplan set global <name> <value>\n"
1040 " Set global dialplan variable <name> to <value>\n";
1046 if (
a->argc != e->
args + 2)
1050 ast_cli(
a->fd,
"\n -- Global variable '%s' set to '%s'\n",
a->argv[3],
a->argv[4]);
1058 const char *chan_name, *var_name, *var_value;
1062 e->
command =
"dialplan set chanvar";
1064 "Usage: dialplan set chanvar <channel> <varname> <value>\n"
1065 " Set channel variable <varname> to <value>\n";
1071 if (
a->argc != e->
args + 3)
1074 chan_name =
a->argv[e->
args];
1075 var_name =
a->argv[e->
args + 1];
1076 var_value =
a->argv[e->
args + 2];
1079 ast_cli(
a->fd,
"Channel '%s' not found\n", chan_name);
1087 ast_cli(
a->fd,
"\n -- Channel variable '%s' set to '%s' for '%s'\n", var_name, var_value, chan_name);
1126 const char *ret =
NULL;
1138 for (i = 0; i < 2; i++) {
1198 const char *nametail =
name;
1200 int old_value_existed = 0;
1202 if (
name[strlen(
name) - 1] ==
')') {
1217 if (*nametail ==
'_') {
1219 if (*nametail ==
'_')
1224 if (strcmp(
ast_var_name(newvariable), nametail) == 0) {
1240 }
else if (old_value_existed) {
1269 if (strchr(
name,
' ')) {
1270 ast_log(
LOG_WARNING,
"Please avoid unnecessary spaces on variables as it may lead to unexpected results ('%s' set to '%s').\n",
name, mydata);
1298 for (x = 0; x <
args.argc; x++) {
1300 if (
pair.argc == 2) {
1303 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);
1324#ifdef TEST_FRAMEWORK
1332 {
"somevaluehere",
"CALLERID(num):0:25",
"somevaluehere"},
1333 {
"somevaluehere",
"CALLERID(num):0:5",
"somev"},
1334 {
"somevaluehere",
"CALLERID(num):4:5",
"value"},
1335 {
"somevaluehere",
"CALLERID(num):0:-4",
"somevalue"},
1336 {
"somevaluehere",
"CALLERID(num):-4",
"here"},
1341 info->name =
"variable_substrings";
1342 info->category =
"/main/pbx/";
1343 info->summary =
"Test variable substring resolution";
1344 info->description =
"Verify that variable substrings are calculated correctly";
1366 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)
struct varshead * ast_channel_varshead(struct ast_channel *chan)
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
#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.
int ast_channel_hangupcause(const struct ast_channel *chan)
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.
#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)
struct ast_channel * ast_channel_get_by_name(const char *name)
Find a channel by name.
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
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 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.
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::@213 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.
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.