Bug 6099 - cleanup of parse_variable_name and pbx_retrieve_variable

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@7911 65c4cc65-6c06-0410-ace0-fbb531ad65f3
1.4
Tilghman Lesher 20 years ago
parent 335cd7d3a2
commit 8d42b1eb74

284
pbx.c

@ -821,40 +821,33 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
/* Note that it's negative -- that's important later. */ /* Note that it's negative -- that's important later. */
#define DONT_HAVE_LENGTH 0x80000000 #define DONT_HAVE_LENGTH 0x80000000
/*! \brief extract offset:length from variable name.
* Returns 1 if there is a offset:length part, which is
* trimmed off (values go into variables)
*/
static int parse_variable_name(char *var, int *offset, int *length, int *isfunc) static int parse_variable_name(char *var, int *offset, int *length, int *isfunc)
{ {
char *varchar, *offsetchar = NULL;
int parens=0; int parens=0;
*offset = 0; *offset = 0;
*length = DONT_HAVE_LENGTH; *length = DONT_HAVE_LENGTH;
*isfunc = 0; *isfunc = 0;
for (varchar = var; *varchar; varchar++) { for (; *var; var++) {
switch (*varchar) { if (*var == '(') {
case '(':
(*isfunc)++; (*isfunc)++;
parens++; parens++;
break; } else if (*var == ')') {
case ')':
parens--; parens--;
break; } else if (*var == ':' && parens == 0) {
case ':': *var++ = '\0';
if (parens == 0) { sscanf(var, "%d:%d", offset, length);
offsetchar = varchar + 1; return 1; /* offset:length valid */
*varchar = '\0';
goto pvn_endfor;
}
} }
} }
pvn_endfor:
if (offsetchar) {
sscanf(offsetchar, "%d:%d", offset, length);
return 1;
} else {
return 0; return 0;
} }
}
/*! \brief takes a substring. It is ok to call with value == workspace. */
static char *substring(char *value, int offset, int length, char *workspace, size_t workspace_len) static char *substring(char *value, int offset, int length, char *workspace, size_t workspace_len)
{ {
char *ret = workspace; char *ret = workspace;
@ -899,127 +892,118 @@ static char *substring(char *value, int offset, int length, char *workspace, siz
---*/ ---*/
void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp) void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen, struct varshead *headp)
{ {
char tmpvar[80]; const char not_found = '\0';
time_t thistime; char tmpvar[80], *deprecated = NULL;
struct tm brokentime; const char *s; /* the result */
int offset, offset2, isfunc; int offset, length;
struct ast_var_t *variables; int i, need_substring;
char *deprecated = NULL; struct varshead *places[2] = { headp, &globals }; /* list of places where we may look */
if (c) if (c) {
headp=&c->varshead; places[0] = &c->varshead;
*ret=NULL; }
ast_copy_string(tmpvar, var, sizeof(tmpvar)); /*
if (parse_variable_name(tmpvar, &offset, &offset2, &isfunc)) { * Make a copy of var because parse_variable_name() modifies the string.
pbx_retrieve_variable(c, tmpvar, ret, workspace, workspacelen, headp); * Then if called directly, we might need to run substring() on the result;
if (!(*ret)) * remember this for later in 'need_substring', 'offset' and 'length'
return; */
*ret = substring(*ret, offset, offset2, workspace, workspacelen); ast_copy_string(tmpvar, var, sizeof(tmpvar)); /* parse_variable_name modifies the string */
} else if (c && !strncmp(var, "CALL", 4)) { need_substring = parse_variable_name(tmpvar, &offset, &length, &i /* ignored */);
/*
* Look first into predefined variables, then into variable lists.
* s == &not_found (set at the beginning) means that we did not find a
* matching variable and need to look into more places.
* If s != &not_found, s is a valid result string as follows:
* s = NULL if the variable does not have a value;
* s = workspace if the result has been assembled there;
* s != workspace in case we have a string, that needs to be copied
* (the ast_copy_string is done once for all at the end).
* Deprecated variables have the replacement indicated in 'deprecated'.
*/
s = &not_found; /* default value */
if (c) { /* This group requires a valid channel */
/* Names with common parts are looked up a piece at a time using strncmp. */
if (!strncmp(var, "CALL", 4)) {
if (!strncmp(var + 4, "ER", 2)) { if (!strncmp(var + 4, "ER", 2)) {
if (!strncmp(var + 6, "ID", 2)) { if (!strncmp(var + 6, "ID", 2)) {
if (!var[8]) { /* CALLERID */ if (!var[8]) { /* CALLERID */
if (c->cid.cid_num) { if (c->cid.cid_num) {
if (c->cid.cid_name) { if (c->cid.cid_name) {
snprintf(workspace, workspacelen, "\"%s\" <%s>", c->cid.cid_name, c->cid.cid_num); snprintf(workspace, workspacelen, "\"%s\" <%s>",
c->cid.cid_name, c->cid.cid_num);
s = workspace;
} else { } else {
ast_copy_string(workspace, c->cid.cid_num, workspacelen); s = c->cid.cid_num;
} }
*ret = workspace;
} else if (c->cid.cid_name) {
ast_copy_string(workspace, c->cid.cid_name, workspacelen);
*ret = workspace;
} else } else
*ret = NULL; s = c->cid.cid_name; /* possibly empty */
deprecated = "CALLERID(all)"; deprecated = "CALLERID(all)";
} else if (!strcmp(var + 8, "NUM")) { } else if (!strcmp(var + 8, "NUM")) { /* CALLERIDNUM */
/* CALLERIDNUM */ s = c->cid.cid_num;
if (c->cid.cid_num) {
ast_copy_string(workspace, c->cid.cid_num, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(num)"; deprecated = "CALLERID(num)";
} else if (!strcmp(var + 8, "NAME")) { } else if (!strcmp(var + 8, "NAME")) { /* CALLERIDNAME */
/* CALLERIDNAME */ s = c->cid.cid_name;
if (c->cid.cid_name) {
ast_copy_string(workspace, c->cid.cid_name, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(name)"; deprecated = "CALLERID(name)";
} else }
goto icky; } else if (!strcmp(var + 6, "ANI")) { /* CALLERANI */
} else if (!strcmp(var + 6, "ANI")) { s = c->cid.cid_ani;
/* CALLERANI */
if (c->cid.cid_ani) {
ast_copy_string(workspace, c->cid.cid_ani, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(ANI)"; deprecated = "CALLERID(ANI)";
} else }
goto icky;
} else if (!strncmp(var + 4, "ING", 3)) { } else if (!strncmp(var + 4, "ING", 3)) {
if (!strcmp(var + 7, "PRES")) { if (!strcmp(var + 7, "PRES")) { /* CALLINGPRES */
/* CALLINGPRES */
snprintf(workspace, workspacelen, "%d", c->cid.cid_pres); snprintf(workspace, workspacelen, "%d", c->cid.cid_pres);
*ret = workspace; s = workspace;
} else if (!strcmp(var + 7, "ANI2")) { } else if (!strcmp(var + 7, "ANI2")) { /* CALLINGANI2 */
/* CALLINGANI2 */
snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2); snprintf(workspace, workspacelen, "%d", c->cid.cid_ani2);
*ret = workspace; s = workspace;
} else if (!strcmp(var + 7, "TON")) { } else if (!strcmp(var + 7, "TON")) { /* CALLINGTON */
/* CALLINGTON */
snprintf(workspace, workspacelen, "%d", c->cid.cid_ton); snprintf(workspace, workspacelen, "%d", c->cid.cid_ton);
*ret = workspace; s = workspace;
} else if (!strcmp(var + 7, "TNS")) { } else if (!strcmp(var + 7, "TNS")) { /* CALLINGTNS */
/* CALLINGTNS */
snprintf(workspace, workspacelen, "%d", c->cid.cid_tns); snprintf(workspace, workspacelen, "%d", c->cid.cid_tns);
*ret = workspace; s = workspace;
} else }
goto icky; }
} else } else if (!strcmp(var, "DNID")) {
goto icky; s = c->cid.cid_dnid;
} else if (c && !strcmp(var, "DNID")) {
if (c->cid.cid_dnid) {
ast_copy_string(workspace, c->cid.cid_dnid, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(DNID)"; deprecated = "CALLERID(DNID)";
} else if (c && !strcmp(var, "HINT")) { } else if (!strcmp(var, "HINT")) {
if (!ast_get_hint(workspace, workspacelen, NULL, 0, c, c->context, c->exten)) s = ast_get_hint(workspace, workspacelen, NULL, 0, c, c->context, c->exten) ? workspace : NULL;
*ret = NULL; } else if (!strcmp(var, "HINTNAME")) {
else s = ast_get_hint(NULL, 0, workspace, workspacelen, c, c->context, c->exten) ? workspace : NULL;
*ret = workspace; } else if (!strcmp(var, "EXTEN")) {
} else if (c && !strcmp(var, "HINTNAME")) { s = c->exten;
if (!ast_get_hint(NULL, 0, workspace, workspacelen, c, c->context, c->exten)) } else if (!strcmp(var, "RDNIS")) {
*ret = NULL; s = c->cid.cid_rdnis;
else
*ret = workspace;
} else if (c && !strcmp(var, "EXTEN")) {
ast_copy_string(workspace, c->exten, workspacelen);
*ret = workspace;
} else if (c && !strcmp(var, "RDNIS")) {
if (c->cid.cid_rdnis) {
ast_copy_string(workspace, c->cid.cid_rdnis, workspacelen);
*ret = workspace;
} else
*ret = NULL;
deprecated = "CALLERID(RDNIS)"; deprecated = "CALLERID(RDNIS)";
} else if (c && !strcmp(var, "CONTEXT")) { } else if (!strcmp(var, "CONTEXT")) {
ast_copy_string(workspace, c->context, workspacelen); s = c->context;
*ret = workspace; } else if (!strcmp(var, "PRIORITY")) {
} else if (c && !strcmp(var, "PRIORITY")) {
snprintf(workspace, workspacelen, "%d", c->priority); snprintf(workspace, workspacelen, "%d", c->priority);
*ret = workspace; s = workspace;
} else if (c && !strcmp(var, "CHANNEL")) { } else if (!strcmp(var, "CHANNEL")) {
ast_copy_string(workspace, c->name, workspacelen); s = c->name;
*ret = workspace; } else if (!strcmp(var, "UNIQUEID")) {
} else if (!strcmp(var, "EPOCH")) { s = c->uniqueid;
} else if (!strcmp(var, "HANGUPCAUSE")) {
snprintf(workspace, workspacelen, "%d", c->hangupcause);
s = workspace;
} else if (!strcmp(var, "ACCOUNTCODE")) {
s = c->accountcode;
deprecated = "CDR(accountcode)";
} else if (!strcmp(var, "LANGUAGE")) {
s = c->language;
deprecated = "LANGUAGE()";
}
}
if (s == &not_found) { /* look for more */
time_t thistime;
struct tm brokentime;
if (!strcmp(var, "EPOCH")) {
snprintf(workspace, workspacelen, "%u",(int)time(NULL)); snprintf(workspace, workspacelen, "%u",(int)time(NULL));
*ret = workspace; s = workspace;
} else if (!strcmp(var, "DATETIME")) { } else if (!strcmp(var, "DATETIME")) {
thistime=time(NULL); thistime=time(NULL);
localtime_r(&thistime, &brokentime); localtime_r(&thistime, &brokentime);
@ -1031,8 +1015,8 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
brokentime.tm_min, brokentime.tm_min,
brokentime.tm_sec brokentime.tm_sec
); );
*ret = workspace; s = workspace;
deprecated = "STRFTIME(${EPOCH},,\%m\%d\%Y-\%H:\%M:\%S)"; deprecated = "STRFTIME(${EPOCH},,\%d\%m\%Y-\%H:\%M:\%S)";
} else if (!strcmp(var, "TIMESTAMP")) { } else if (!strcmp(var, "TIMESTAMP")) {
thistime=time(NULL); thistime=time(NULL);
localtime_r(&thistime, &brokentime); localtime_r(&thistime, &brokentime);
@ -1045,59 +1029,35 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
brokentime.tm_min, brokentime.tm_min,
brokentime.tm_sec brokentime.tm_sec
); );
*ret = workspace; s = workspace;
deprecated = "STRFTIME(${EPOCH},,\%Y\%m\%d-\%H\%M\%S)"; deprecated = "STRFTIME(${EPOCH},,\%Y\%m\%d-\%H\%M\%S)";
} else if (c && !strcmp(var, "UNIQUEID")) {
snprintf(workspace, workspacelen, "%s", c->uniqueid);
*ret = workspace;
} else if (c && !strcmp(var, "HANGUPCAUSE")) {
snprintf(workspace, workspacelen, "%d", c->hangupcause);
*ret = workspace;
} else if (c && !strcmp(var, "ACCOUNTCODE")) {
ast_copy_string(workspace, c->accountcode, workspacelen);
*ret = workspace;
deprecated = "CDR(accountcode)";
} else if (c && !strcmp(var, "LANGUAGE")) {
ast_copy_string(workspace, c->language, workspacelen);
*ret = workspace;
deprecated = "LANGUAGE()";
} else {
icky:
if (headp) {
AST_LIST_TRAVERSE(headp,variables,entries) {
#if 0
ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
#endif
if (strcasecmp(ast_var_name(variables),var)==0) {
const char *s = ast_var_value(variables);
if (s) {
ast_copy_string(workspace, s, workspacelen);
*ret = workspace;
} }
}
/* if not found, look into chanvars or global vars */
for (i = 0; s == &not_found && i < (sizeof(places) / sizeof(places[0])); i++) {
struct ast_var_t *variables;
if (!places[i])
continue;
AST_LIST_TRAVERSE(places[i], variables, entries) {
if (strcasecmp(ast_var_name(variables), var)==0) {
s = ast_var_value(variables);
break; break;
} }
} }
} }
if (!(*ret)) { if (s == &not_found || s == NULL)
/* Try globals */ *ret = NULL;
AST_LIST_TRAVERSE(&globals,variables,entries) { else {
#if 0 if (s != workspace)
ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
#endif
if (strcasecmp(ast_var_name(variables),var)==0) {
const char *s = ast_var_value(variables);
if (s) {
ast_copy_string(workspace, s, workspacelen); ast_copy_string(workspace, s, workspacelen);
*ret = workspace; *ret = workspace;
if (need_substring)
*ret = substring(*ret, offset, length, workspace, workspacelen);
} }
}
} if (deprecated)
}
}
if (deprecated) {
ast_log(LOG_WARNING, "${%s} is deprecated. Please use ${%s} instead.\n", var, deprecated); ast_log(LOG_WARNING, "${%s} is deprecated. Please use ${%s} instead.\n", var, deprecated);
} }
}
/*! \brief CLI function to show installed custom functions /*! \brief CLI function to show installed custom functions
\addtogroup CLI_functions \addtogroup CLI_functions

Loading…
Cancel
Save