Browse code

script parsing: C style switch() & case support

- support for parsing C style switch() & case, e.g.:
switch($var){
case 1:
log(1, "1\n");
break;
case 2:
case 3:
default:
log(1, "default\n");
}
(note: this is different from kamailio/openser switch())

Andrei Pelinescu-Onciul authored on 04/02/2009 19:44:26
Showing 4 changed files
... ...
@@ -187,6 +187,9 @@ ELSE			"else"
187 187
 SET_ADV_ADDRESS	"set_advertised_address"
188 188
 SET_ADV_PORT	"set_advertised_port"
189 189
 FORCE_SEND_SOCKET	"force_send_socket"
190
+SWITCH			"switch"
191
+CASE			"case"
192
+DEFAULT			"default"
190 193
 
191 194
 /*ACTION LVALUES*/
192 195
 URIHOST			"uri:host"
... ...
@@ -495,6 +498,10 @@ EAT_ABLE	[\ \t\b\r]
495 498
 										return SET_ADV_PORT; }
496 499
 <INITIAL>{FORCE_SEND_SOCKET}	{	count(); yylval.strval=yytext;
497 500
 									return FORCE_SEND_SOCKET; }
501
+<INITIAL>{SWITCH}	{ count(); yylval.strval=yytext; return SWITCH; }
502
+<INITIAL>{CASE}	{ count(); yylval.strval=yytext; return CASE; }
503
+<INITIAL>{DEFAULT}	{ count(); yylval.strval=yytext; return DEFAULT; }
504
+
498 505
 
499 506
 <INITIAL>{URIHOST}	{ count(); yylval.strval=yytext; return URIHOST; }
500 507
 <INITIAL>{URIPORT}	{ count(); yylval.strval=yytext; return URIPORT; }
... ...
@@ -93,6 +93,7 @@
93 93
  *               lval=rval_expr, where lval=avp|pvar  (andrei)
94 94
  * 2007-12-06  expression are now evaluated in terms of rvalues;
95 95
  *             NUMBER is now always positive; cleanup (andrei)
96
+ * 2009-01-26  case/switch() support (andrei)
96 97
 */
97 98
 
98 99
 %{
... ...
@@ -109,6 +110,7 @@
109 110
 #include "route_struct.h"
110 111
 #include "globals.h"
111 112
 #include "route.h"
113
+#include "switch.h"
112 114
 #include "dprint.h"
113 115
 #include "sr_module.h"
114 116
 #include "modparam.h"
... ...
@@ -211,6 +213,8 @@ static struct socket_id* mk_listen_id2(struct name_lst*, int, int);
211 213
 static void free_name_lst(struct name_lst* lst);
212 214
 static void free_socket_id_lst(struct socket_id* i);
213 215
 
216
+static struct case_stms* mk_case_stm(struct rval_expr* ct, struct action* a);
217
+
214 218
 %}
215 219
 
216 220
 %union {
... ...
@@ -219,6 +223,7 @@ static void free_socket_id_lst(struct socket_id* i);
219 223
 	char* strval;
220 224
 	struct expr* expr;
221 225
 	struct action* action;
226
+	struct case_stms* case_stms;
222 227
 	struct net* ipnet;
223 228
 	struct ip_addr* ipaddr;
224 229
 	struct socket_id* sockid;
... ...
@@ -272,6 +277,9 @@ static void free_socket_id_lst(struct socket_id* i);
272 277
 %token SET_ADV_ADDRESS
273 278
 %token SET_ADV_PORT
274 279
 %token FORCE_SEND_SOCKET
280
+%token SWITCH
281
+%token CASE
282
+%token DEFAULT
275 283
 %token URIHOST
276 284
 %token URIPORT
277 285
 %token MAX_LEN
... ...
@@ -493,6 +501,8 @@ static void free_socket_id_lst(struct socket_id* i);
493 501
 %type <intval> intno eint_op eint_op_onsend
494 502
 %type <intval> eip_op eip_op_onsend
495 503
 %type <action> action actions cmd fcmd if_cmd stm /*exp_stm*/ assign_action
504
+%type <action> switch_cmd
505
+%type <case_stms> single_case case_stms
496 506
 %type <ipaddr> ipv4 ipv6 ipv6addr ip
497 507
 %type <ipnet> ipnet
498 508
 %type <strval> host
... ...
@@ -514,7 +524,7 @@ static void free_socket_id_lst(struct socket_id* i);
514 524
 %type <attr> attr_id_any_str
515 525
 %type <pvar> pvar
516 526
 %type <lval> lval
517
-%type <rv_expr> rval rval_expr 
527
+%type <rv_expr> rval rval_expr ct_rval
518 528
 %type <lval> avp_pvar
519 529
 /* %type <intval> class_id */
520 530
 %type <intval> assign_op
... ...
@@ -1762,6 +1772,7 @@ actions:
1762 1772
 action:
1763 1773
 	fcmd SEMICOLON {$$=$1;}
1764 1774
 	| if_cmd {$$=$1;}
1775
+	| switch_cmd {$$=$1;}
1765 1776
 	| assign_action SEMICOLON {$$=$1;}
1766 1777
 	| SEMICOLON /* null action */ {$$=0;}
1767 1778
 	| fcmd error { $$=0; yyerror("bad command: missing ';'?"); }
... ...
@@ -1770,6 +1781,98 @@ if_cmd:
1770 1781
 	IF exp stm		{ $$=mk_action( IF_T, 3, EXPR_ST, $2, ACTIONS_ST, $3, NOSUBTYPE, 0); }
1771 1782
 	| IF exp stm ELSE stm	{ $$=mk_action( IF_T, 3, EXPR_ST, $2, ACTIONS_ST, $3, ACTIONS_ST, $5); }
1772 1783
 	;
1784
+
1785
+ct_rval: rval_expr {
1786
+			$$=0;
1787
+			if (!rve_is_constant($1)){
1788
+				yyerror("constant expected");
1789
+			}else if (!rve_check_type((enum rval_type*)&i_tmp, $1, 0, 0 ,0)){
1790
+				yyerror("invalid expression (bad type)");
1791
+			}else if (i_tmp!=RV_INT){
1792
+				yyerror("invalid expression type, int expected\n");
1793
+			}else
1794
+				$$=$1;
1795
+		}
1796
+;
1797
+single_case:
1798
+	CASE ct_rval COLON actions {
1799
+		$$=0;
1800
+		if ($2==0) yyerror ("bad case label");
1801
+		else if (($$=mk_case_stm($2, $4))==0){
1802
+				yyerror("internal error: memory allocation failure");
1803
+				YYABORT;
1804
+		}
1805
+	}
1806
+	| CASE ct_rval COLON {
1807
+		$$=0;
1808
+		if ($2==0) yyerror ("bad case label");
1809
+		else if (($$=mk_case_stm($2, 0))==0){
1810
+				yyerror("internal error: memory allocation failure");
1811
+				YYABORT;
1812
+		}
1813
+	}
1814
+	| DEFAULT COLON actions {
1815
+		if (($$=mk_case_stm(0, $3))==0){
1816
+				yyerror("internal error: memory allocation failure");
1817
+				YYABORT;
1818
+		}
1819
+	}
1820
+	| DEFAULT COLON {
1821
+		if (($$=mk_case_stm(0, 0))==0){
1822
+				yyerror("internal error: memory allocation failure");
1823
+				YYABORT;
1824
+		}
1825
+	}
1826
+	| CASE error { $$=0; yyerror("bad case label"); }
1827
+	| CASE ct_rval COLON error { $$=0; yyerror ("bad case body"); }
1828
+;
1829
+case_stms:
1830
+	case_stms single_case {
1831
+		$$=$1;
1832
+		if ($2==0) yyerror ("bad case");
1833
+		if ($$){
1834
+			*($$->append)=$2;
1835
+			if (*($$->append)!=0)
1836
+				$$->append=&((*($$->append))->next);
1837
+		}
1838
+	}
1839
+	| single_case {
1840
+		$$=$1;
1841
+		if ($1==0) yyerror ("bad case");
1842
+		else $$->append=&($$->next);
1843
+	}
1844
+;
1845
+switch_cmd:
1846
+	  SWITCH rval_expr LBRACE case_stms RBRACE { 
1847
+		$$=0;
1848
+		if ($2==0) yyerror("bad expression in switch(...)");
1849
+		else if ($4==0) yyerror ("bad switch body");
1850
+		else{
1851
+			$$=mk_action(SWITCH_T, 2, RVE_ST, $2, CASE_ST, $4);
1852
+			if ($$==0) {
1853
+				yyerror("internal error");
1854
+				YYABORT;
1855
+			}
1856
+		}
1857
+	}
1858
+	| SWITCH rval_expr LBRACE RBRACE {
1859
+		$$=0;
1860
+		warn("empty switch()");
1861
+		if ($2==0) yyerror("bad expression in switch(...)");
1862
+		else{
1863
+			/* it might have sideffects, so leave it for the optimizer */
1864
+			$$=mk_action(SWITCH_T, 2, RVE_ST, $2, CASE_ST, 0);
1865
+			if ($$==0) {
1866
+				yyerror("internal error");
1867
+				YYABORT;
1868
+			}
1869
+		}
1870
+	}
1871
+	| SWITCH error { $$=0; yyerror ("bad expression in switch(...)"); }
1872
+	| SWITCH rval_expr LBRACE error RBRACE 
1873
+		{$$=0; yyerror ("bad switch body"); }
1874
+;
1875
+
1773 1876
 /* class_id:
1774 1877
 	LBRACK ATTR_USER RBRACK { $$ = AVP_CLASS_USER; }
1775 1878
 	| LBRACK ATTR_DOMAIN RBRACK { $$ = AVP_CLASS_DOMAIN; }
... ...
@@ -2763,6 +2866,23 @@ static void free_socket_id_lst(struct socket_id* lst)
2763 2866
 	}
2764 2867
 }
2765 2868
 
2869
+
2870
+static struct case_stms* mk_case_stm(struct rval_expr* ct, struct action* a)
2871
+{
2872
+	struct case_stms* s;
2873
+	s=pkg_malloc(sizeof(*s));
2874
+	if (s==0) {
2875
+		LOG(L_CRIT,"ERROR: cfg. parser: out of memory.\n");
2876
+	} else {
2877
+		memset(s, 0, sizeof(*s));
2878
+		s->ct_rve=ct;
2879
+		s->actions=a;
2880
+		s->next=0;
2881
+		s->append=0;
2882
+	}
2883
+	return s;
2884
+}
2885
+
2766 2886
 /*
2767 2887
 int main(int argc, char ** argv)
2768 2888
 {
... ...
@@ -70,7 +70,9 @@ enum { METHOD_O=1, URI_O, FROM_URI_O, TO_URI_O, SRCIP_O, SRCPORT_O,
70 70
 enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T, ROUTE_T, EXEC_T,
71 71
 		SET_HOST_T, SET_HOSTPORT_T, SET_USER_T, SET_USERPASS_T,
72 72
 		SET_PORT_T, SET_URI_T, SET_HOSTPORTTRANS_T,
73
-		IF_T, MODULE_T, MODULE3_T, MODULE4_T, MODULE5_T, MODULE6_T, MODULEX_T,
73
+		IF_T, SWITCH_T /* only until fixup*/,
74
+		BLOCK_T, EVAL_T, SWITCH_JT_T, SWITCH_COND_T,
75
+		MODULE_T, MODULE3_T, MODULE4_T, MODULE5_T, MODULE6_T, MODULEX_T,
74 76
 		SETFLAG_T, RESETFLAG_T, ISFLAGSET_T ,
75 77
 		AVPFLAG_OPER_T,
76 78
 		LEN_GT_T, PREFIX_T, STRIP_T,STRIP_TAIL_T,
... ...
@@ -96,7 +98,9 @@ enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST, RE_ST, PROXY_ST,
96 98
 		MYSELF_ST, STR_ST, SOCKID_ST, SOCKETINFO_ST, ACTION_ST, AVP_ST,
97 99
 		SELECT_ST, PVAR_ST,
98 100
 		LVAL_ST,  RVE_ST,
99
-		RETCODE_ST};
101
+		RETCODE_ST, CASE_ST,
102
+		BLOCK_ST, JUMPTABLE_ST, CONDTABLE_ST
103
+};
100 104
 
101 105
 /* run flags */
102 106
 #define EXIT_R_F   1
103 107
new file mode 100644
... ...
@@ -0,0 +1,60 @@
1
+/* 
2
+ * $Id$
3
+ * 
4
+ * Copyright (C) 2009 iptelorg GmbH
5
+ *
6
+ * Permission to use, copy, modify, and distribute this software for any
7
+ * purpose with or without fee is hereby granted, provided that the above
8
+ * copyright notice and this permission notice appear in all copies.
9
+ *
10
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+/*
19
+ * /home/andrei/sr.git/switch.h
20
+ */
21
+/*
22
+ * History:
23
+ * --------
24
+ *  2009-02-02  initial version (andrei)
25
+*/
26
+
27
+#ifndef __switch_h
28
+#define __switch_h
29
+
30
+#include "route_struct.h"
31
+
32
+struct case_stms{
33
+	struct rval_expr* ct_rve;
34
+	struct action* actions;
35
+	struct case_stms* next;
36
+	struct case_stms** append;
37
+	int int_label;
38
+	int is_default;
39
+};
40
+
41
+
42
+struct switch_cond_table{
43
+	int n;                  /**< size */
44
+	int* cond;              /**< int labels array */
45
+	struct action** jump;   /**< jump points array */
46
+	struct action* def; /**< default jump  */
47
+};
48
+
49
+
50
+struct switch_jmp_table{
51
+	int first;              /**< first int label in the jump table */
52
+	int last;               /**< last int label in the jump table */
53
+	struct action** tbl;    /**< jmp table [v-first] iff first<=v<=last */
54
+	struct switch_cond_table rest; /**< normal cond. table for the rest */
55
+};
56
+
57
+
58
+#endif /*__switch_h*/
59
+
60
+/* vi: set ts=4 sw=4 tw=79:ai:cindent: */