Fix colon expansion (bug #3572)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5023 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2005-02-13 20:57:52 +00:00
parent 9a43c29ca7
commit 2887217560
2 changed files with 57 additions and 21 deletions

View File

@@ -56,8 +56,8 @@ requires a variable), just write the name. To refer to the variable's value,
enclose it inside ${}. For example, SetVar takes as the first argument enclose it inside ${}. For example, SetVar takes as the first argument
(before the =) a variable name, so: (before the =) a variable name, so:
;exten => 1,2,SetVar(koko=lala) exten => 1,2,SetVar(koko=lala)
;exten => 1,3,SetVar(${koko}=blabla) exten => 1,3,SetVar(${koko}=blabla)
stores to the variable "koko" the value "lala" and to variable "lala" the stores to the variable "koko" the value "lala" and to variable "lala" the
value "blabla". value "blabla".
@@ -65,6 +65,32 @@ value "blabla".
In fact, everything contained ${here} is just replaced with the value of In fact, everything contained ${here} is just replaced with the value of
the variable "here". the variable "here".
_______________________________
REMOVING CHARACTERS FROM STRING
-------------------------------
If you want to remove the first N characters from a string, you just
add a colon and the number of characters, like
;Remove the first character of extension, save in "number" variable
exten => _9X.,1,setvar(number=${EXTEN:1})
A second colon limits the number of characters used from the original
string.
;Strip five characters from the start of string, use only two
; (character 6 and 7 in the original string)
exten => 1000,4,setvar(skrep=${STRING:5:2})
You can also count from the end of the string by giving a negative
position:
;Use the two first of the three last characters in the account code
exten => 4500,4,setvar(acc=${ACCOUNTCODE:-3:2})
Or
;Use the last three digits of the phone number
exten => 6112,4,goto(${EXTEN:-3},1)
___________________________ ___________________________
EXPRESSIONS: EXPRESSIONS:
--------------------------- ---------------------------

48
pbx.c
View File

@@ -805,6 +805,9 @@ static struct ast_exten *pbx_find_extension(struct ast_channel *chan, struct ast
return NULL; return NULL;
} }
/*--- pbx_retrieve_variable: Support for Asterisk built-in variables and
functions in the dialplan
---*/
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 *first,*second; char *first,*second;
@@ -818,7 +821,7 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
headp=&c->varshead; headp=&c->varshead;
*ret=NULL; *ret=NULL;
/* Now we have the variable name on cp3 */ /* Now we have the variable name on cp3 */
if (!strncasecmp(var,"LEN(",4)) { if (!strncasecmp(var,"LEN(",4)) { /* ${LEN(<string>)} */
int len=strlen(var); int len=strlen(var);
int len_len=4; int len_len=4;
if (strrchr(var,')')) { if (strrchr(var,')')) {
@@ -831,42 +834,49 @@ void pbx_retrieve_variable(struct ast_channel *c, const char *var, char **ret, c
/* length is zero */ /* length is zero */
*ret = "0"; *ret = "0";
} }
} else if ((first=strchr(var,':'))) { } else if ((first=strchr(var,':'))) { /* : Remove characters counting from end or start of string */
strncpy(tmpvar, var, sizeof(tmpvar) - 1); strncpy(tmpvar, var, sizeof(tmpvar) - 1);
first = strchr(tmpvar, ':'); first = strchr(tmpvar, ':');
if (!first) if (!first)
first = tmpvar + strlen(tmpvar); first = tmpvar + strlen(tmpvar);
*first='\0'; *first='\0';
pbx_retrieve_variable(c,tmpvar,ret,workspace,workspacelen - 1, headp); pbx_retrieve_variable(c,tmpvar,ret,workspace,workspacelen - 1, headp);
if (!(*ret)) return; if (!(*ret))
offset=atoi(first+1); return;
if ((second=strchr(first+1,':'))) { offset=atoi(first+1); /* The number of characters,
positive: remove # of chars from start
negative: keep # of chars from end */
if ((second=strchr(first+1,':'))) {
*second='\0'; *second='\0';
offset2=atoi(second+1); offset2 = atoi(second+1); /* Number of chars to copy */
} else } else if (offset >= 0) {
offset2=strlen(*ret)-offset; offset2 = strlen(*ret)-offset; /* Rest of string */
if (abs(offset)>strlen(*ret)) { } else {
if (offset>=0) offset2 = abs(offset);
}
if (abs(offset) > strlen(*ret)) { /* Offset beyond string */
if (offset >= 0)
offset=strlen(*ret); offset=strlen(*ret);
else else
offset=-strlen(*ret); offset=-strlen(*ret);
} }
if ((offset<0 && offset2>-offset) || (offset>=0 && offset+offset2>strlen(*ret))) { if ((offset < 0 && offset2 > -offset) || (offset >= 0 && offset+offset2 > strlen(*ret))) {
if (offset>=0) if (offset >= 0)
offset2=strlen(*ret)-offset; offset2=strlen(*ret)-offset;
else else
offset2=strlen(*ret)+offset; offset2=strlen(*ret)+offset;
} }
if (offset>=0) if (offset >= 0)
*ret+=offset; *ret += offset;
else else
*ret+=strlen(*ret)+offset; *ret += strlen(*ret)+offset;
(*ret)[offset2] = '\0'; (*ret)[offset2] = '\0'; /* Cut at offset2 position */
} else if (c && !strncmp(var, "CALL", 4)) { } else if (c && !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]) { if (!var[8]) { /* CALLERID */
/* 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);