Browse code

core: new script operators: defined, strlen, strempty

Support for 3 new operators:

defined expr - returns true if expr is defined, and false if not.
Note: only a standalone avp or pvar can be
undefined, everything else is defined.

strlen(expr) - returns the lenght of expr evaluated as string.

strempty(expr) - returns true if expr evaluates to the empty
string (equivalent to expr=="").

Example:
if (defined $v) $len=strlen($v);
else $len=0;

Andrei Pelinescu-Onciul authored on 24/04/2009 19:02:05
Showing 3 changed files
... ...
@@ -6,6 +6,14 @@ $Id$
6 6
 sip-router changes
7 7
 
8 8
 core:
9
+  - new script operators: defined, strlen, strempty
10
+      defined expr - returns true if expr is defined, and false if not.
11
+                     Note: only a standalone avp or pvar can be
12
+                     undefined, everything else is defined.
13
+      strlen(expr) - returns the lenght of expr evaluated as string.
14
+      strempty(expr) - returns true if expr evaluates to the empty
15
+                       string (equivalent to expr=="").
16
+    e.g.: if (defined $v && !strempty($v)) $len=strlen($v);
9 17
   - module search path support: loadpath takes now a list of directories
10 18
     separated by ':'. The list is searched in-order. For each directory d
11 19
     $d/${module_name}.so and $d/${module_name}/${module_name}.so are tried.
... ...
@@ -23,11 +31,13 @@ config script changes:
23 23
   - support for kamailio style pvars
24 24
   - C-like switch()/case (integer only)
25 25
   - while()
26
-  - max_while_loops - maximum iterations allowed for a while, can be changed
27
-    at runtime. Default 100.
28 26
 build system:
29 27
   - multiple modules directories are now supported (defined in Makefile.dirs)
30 28
 
29
+new config variables:
30
+  - max_while_loops - maximum iterations allowed for a while  (can be changed
31
+       at runtime). Default 100.
32
+
31 33
 
32 34
 
33 35
 
... ...
@@ -78,6 +78,7 @@
78 78
  *  2008-11-28  added support for kamailio pvars and avp/pvar guessing (andrei)
79 79
  *  2008-12-11  added support for "string1" "string2" (andrei)
80 80
  *  2009-03-10  added SET_USERPHONE action (Miklos)
81
+ *  2009-04-24  addd strlen, strempty and defined operators (andrei)
81 82
 */
82 83
 
83 84
 
... ...
@@ -244,6 +245,9 @@ LOG_OR		"or"|"||"
244 244
 BIN_OR          "|"
245 245
 PLUS	"+"
246 246
 MINUS	"-"
247
+STRLEN	"strlen"
248
+STREMPTY	"strempty"
249
+DEFINED		"defined"
247 250
 
248 251
 /* Attribute specification */
249 252
 ATTR_MARK   "%"
... ...
@@ -765,6 +769,9 @@ EAT_ABLE	[\ \t\b\r]
765 765
 <INITIAL>{BIN_OR}	{ count(); return BIN_OR;  }
766 766
 <INITIAL>{PLUS}		{ count(); return PLUS; }
767 767
 <INITIAL>{MINUS}	{ count(); return MINUS; }
768
+<INITIAL>{STRLEN}	{ count(); return STRLEN; }
769
+<INITIAL>{STREMPTY}	{ count(); return STREMPTY; }
770
+<INITIAL>{DEFINED}	{ count(); return DEFINED; }
768 771
 
769 772
 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
770 773
 <SELECT>{ID}		{ count(); addstr(&s_buf, yytext, yyleng);
... ...
@@ -489,8 +489,13 @@ static int case_check_default(struct case_stms* stms);
489 489
 %left PLUS MINUS
490 490
 %left STAR SLASH
491 491
 %right NOT
492
+%right DEFINED
492 493
 %left DOT
493 494
 
495
+/* no precedence, they use () */
496
+%token STRLEN
497
+%token STREMPTY
498
+
494 499
 /* values */
495 500
 %token <intval> NUMBER
496 501
 %token <strval> ID
... ...
@@ -2247,6 +2252,9 @@ rval_expr: rval						{ $$=$1;
2247 2247
 		| rval_expr LOG_AND rval_expr	{ $$=mk_rve2(RVE_LAND_OP, $1, $3);}
2248 2248
 		| rval_expr LOG_OR rval_expr	{ $$=mk_rve2(RVE_LOR_OP, $1, $3);}
2249 2249
 		| LPAREN rval_expr RPAREN		{ $$=$2;}
2250
+		| STRLEN LPAREN rval_expr RPAREN { $$=mk_rve1(RVE_STRLEN_OP, $3);}
2251
+		| STREMPTY LPAREN rval_expr RPAREN {$$=mk_rve1(RVE_STREMPTY_OP, $3);}
2252
+		| DEFINED rval_expr				{ $$=mk_rve1(RVE_DEFINED_OP, $2);}
2250 2253
 		| rve_un_op %prec NOT error		{ yyerror("bad expression"); }
2251 2254
 		| rval_expr PLUS error			{ yyerror("bad expression"); }
2252 2255
 		| rval_expr MINUS error			{ yyerror("bad expression"); }
... ...
@@ -2260,6 +2268,9 @@ rval_expr: rval						{ $$=$1;
2260 2260
 			{ yyerror("bad expression"); }
2261 2261
 		| rval_expr LOG_AND error		{ yyerror("bad expression"); }
2262 2262
 		| rval_expr LOG_OR error		{ yyerror("bad expression"); }
2263
+		| STRLEN LPAREN error RPAREN	{ yyerror("bad expression"); }
2264
+		| STREMPTY LPAREN error RPAREN	{ yyerror("bad expression"); }
2265
+		| DEFINED error					{ yyerror("bad expression"); }
2263 2266
 		;
2264 2267
 
2265 2268
 assign_action: lval assign_op  rval_expr	{ $$=mk_action($2, 2, LVAL_ST, $1,