[SR-Dev] git:master: core: new script operators: defined, strlen, strempty

Andrei Pelinescu-Onciul andrei at iptel.org
Fri Apr 24 21:52:44 CEST 2009


Module: sip-router
Branch: master
Commit: d47990275e5f604560d7ef6d93e8f2056110d7c8
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=d47990275e5f604560d7ef6d93e8f2056110d7c8

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Fri Apr 24 21:02:05 2009 +0200

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;

---

 NEWS    |   14 ++++++++++++--
 cfg.lex |    7 +++++++
 cfg.y   |   11 +++++++++++
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index ba832bf..fade123 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,14 @@ $Id$
 sip-router changes
 
 core:
+  - new script operators: defined, strlen, strempty
+      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=="").
+    e.g.: if (defined $v && !strempty($v)) $len=strlen($v);
   - module search path support: loadpath takes now a list of directories
     separated by ':'. The list is searched in-order. For each directory d
     $d/${module_name}.so and $d/${module_name}/${module_name}.so are tried.
@@ -23,11 +31,13 @@ config script changes:
   - support for kamailio style pvars
   - C-like switch()/case (integer only)
   - while()
-  - max_while_loops - maximum iterations allowed for a while, can be changed
-    at runtime. Default 100.
 build system:
   - multiple modules directories are now supported (defined in Makefile.dirs)
 
+new config variables:
+  - max_while_loops - maximum iterations allowed for a while  (can be changed
+       at runtime). Default 100.
+
 
 
 
diff --git a/cfg.lex b/cfg.lex
index 9f42697..b1f9c55 100644
--- a/cfg.lex
+++ b/cfg.lex
@@ -78,6 +78,7 @@
  *  2008-11-28  added support for kamailio pvars and avp/pvar guessing (andrei)
  *  2008-12-11  added support for "string1" "string2" (andrei)
  *  2009-03-10  added SET_USERPHONE action (Miklos)
+ *  2009-04-24  addd strlen, strempty and defined operators (andrei)
 */
 
 
@@ -244,6 +245,9 @@ LOG_OR		"or"|"||"
 BIN_OR          "|"
 PLUS	"+"
 MINUS	"-"
+STRLEN	"strlen"
+STREMPTY	"strempty"
+DEFINED		"defined"
 
 /* Attribute specification */
 ATTR_MARK   "%"
@@ -765,6 +769,9 @@ EAT_ABLE	[\ \t\b\r]
 <INITIAL>{BIN_OR}	{ count(); return BIN_OR;  }
 <INITIAL>{PLUS}		{ count(); return PLUS; }
 <INITIAL>{MINUS}	{ count(); return MINUS; }
+<INITIAL>{STRLEN}	{ count(); return STRLEN; }
+<INITIAL>{STREMPTY}	{ count(); return STREMPTY; }
+<INITIAL>{DEFINED}	{ count(); return DEFINED; }
 
 <INITIAL>{SELECT_MARK}  { count(); state = SELECT_S; BEGIN(SELECT); return SELECT_MARK; }
 <SELECT>{ID}		{ count(); addstr(&s_buf, yytext, yyleng);
diff --git a/cfg.y b/cfg.y
index de7643d..a281052 100644
--- a/cfg.y
+++ b/cfg.y
@@ -489,8 +489,13 @@ static int case_check_default(struct case_stms* stms);
 %left PLUS MINUS
 %left STAR SLASH
 %right NOT
+%right DEFINED
 %left DOT
 
+/* no precedence, they use () */
+%token STRLEN
+%token STREMPTY
+
 /* values */
 %token <intval> NUMBER
 %token <strval> ID
@@ -2247,6 +2252,9 @@ rval_expr: rval						{ $$=$1;
 		| rval_expr LOG_AND rval_expr	{ $$=mk_rve2(RVE_LAND_OP, $1, $3);}
 		| rval_expr LOG_OR rval_expr	{ $$=mk_rve2(RVE_LOR_OP, $1, $3);}
 		| LPAREN rval_expr RPAREN		{ $$=$2;}
+		| STRLEN LPAREN rval_expr RPAREN { $$=mk_rve1(RVE_STRLEN_OP, $3);}
+		| STREMPTY LPAREN rval_expr RPAREN {$$=mk_rve1(RVE_STREMPTY_OP, $3);}
+		| DEFINED rval_expr				{ $$=mk_rve1(RVE_DEFINED_OP, $2);}
 		| rve_un_op %prec NOT error		{ yyerror("bad expression"); }
 		| rval_expr PLUS error			{ yyerror("bad expression"); }
 		| rval_expr MINUS error			{ yyerror("bad expression"); }
@@ -2260,6 +2268,9 @@ rval_expr: rval						{ $$=$1;
 			{ yyerror("bad expression"); }
 		| rval_expr LOG_AND error		{ yyerror("bad expression"); }
 		| rval_expr LOG_OR error		{ yyerror("bad expression"); }
+		| STRLEN LPAREN error RPAREN	{ yyerror("bad expression"); }
+		| STREMPTY LPAREN error RPAREN	{ yyerror("bad expression"); }
+		| DEFINED error					{ yyerror("bad expression"); }
 		;
 
 assign_action: lval assign_op  rval_expr	{ $$=mk_action($2, 2, LVAL_ST, $1, 




More information about the sr-dev mailing list