Browse code

script parsing: expression type checking on startup

- check expressions type and report errors at script parsing time
- yyerror() uses now a printf style format

Andrei Pelinescu-Onciul authored on 15/12/2008 20:24:47
Showing 1 changed files
... ...
@@ -99,6 +99,7 @@
99 99
 
100 100
 #include <stdlib.h>
101 101
 #include <stdio.h>
102
+#include <stdarg.h>
102 103
 #include <sys/types.h>
103 104
 #include <sys/socket.h>
104 105
 #include <netinet/in.h>
... ...
@@ -180,7 +181,7 @@
180 180
 
181 181
 
182 182
 extern int yylex();
183
-static void yyerror(char* s);
183
+static void yyerror(char* s, ...);
184 184
 static char* tmp;
185 185
 static int i_tmp;
186 186
 static unsigned u_tmp;
... ...
@@ -199,6 +200,10 @@ static struct lvalue* lval_tmp;
199 199
 static struct rvalue* rval_tmp;
200 200
 
201 201
 static void warn(char* s);
202
+static struct rval_expr* mk_rve_rval(enum rval_type, void* v);
203
+static struct rval_expr* mk_rve1(enum rval_expr_op op, struct rval_expr* rve1);
204
+static struct rval_expr* mk_rve2(enum rval_expr_op op, struct rval_expr* rve1,
205
+									struct rval_expr* rve2);
202 206
 static struct socket_id* mk_listen_id(char*, int, int);
203 207
 static struct name_lst* mk_name_lst(char* name, int flags);
204 208
 static struct socket_id* mk_listen_id2(struct name_lst*, int, int);
... ...
@@ -1489,7 +1494,7 @@ send_route_stm: ROUTE_SEND LBRACE actions RBRACE {
1489 1489
 
1490 1490
 exp:	rval_expr
1491 1491
 		{
1492
-			if (!rve_check_type((enum rval_type*)&i_tmp, $1)){
1492
+			if (!rve_check_type((enum rval_type*)&i_tmp, $1, 0, 0 ,0)){
1493 1493
 				yyerror("invalid expression");
1494 1494
 				$$=0;
1495 1495
 			}else if (i_tmp!=RV_INT && i_tmp!=RV_NONE){
... ...
@@ -1992,18 +1997,18 @@ lval: attr_id_ass {
1992 1992
 				}
1993 1993
 	;
1994 1994
 
1995
-rval: intno			{$$=mk_rval_expr_v(RV_INT, (void*)$1); }
1995
+rval: intno			{$$=mk_rve_rval(RV_INT, (void*)$1); }
1996 1996
 	| STRING			{	s_tmp.s=$1; s_tmp.len=strlen($1);
1997
-							$$=mk_rval_expr_v(RV_STR, &s_tmp); }
1998
-	| attr_id_any		{$$=mk_rval_expr_v(RV_AVP, $1); pkg_free($1); }
1999
-	| pvar				{$$=mk_rval_expr_v(RV_PVAR, $1); pkg_free($1); }
1997
+							$$=mk_rve_rval(RV_STR, &s_tmp); }
1998
+	| attr_id_any		{$$=mk_rve_rval(RV_AVP, $1); pkg_free($1); }
1999
+	| pvar				{$$=mk_rve_rval(RV_PVAR, $1); pkg_free($1); }
2000 2000
 	| avp_pvar			{
2001 2001
 							switch($1->type){
2002 2002
 								case LV_AVP:
2003
-									$$=mk_rval_expr_v(RV_AVP, &$1->lv.avps);
2003
+									$$=mk_rve_rval(RV_AVP, &$1->lv.avps);
2004 2004
 									break;
2005 2005
 								case LV_PVAR:
2006
-									$$=mk_rval_expr_v(RV_PVAR, &$1->lv.pvs);
2006
+									$$=mk_rve_rval(RV_PVAR, &$1->lv.pvs);
2007 2007
 									break;
2008 2008
 								default:
2009 2009
 									yyerror("BUG: invalid lvalue type ");
... ...
@@ -2011,11 +2016,12 @@ rval: intno			{$$=mk_rval_expr_v(RV_INT, (void*)$1); }
2011 2011
 							}
2012 2012
 							pkg_free($1); /* not needed anymore */
2013 2013
 						}
2014
-	| select_id			{$$=mk_rval_expr_v(RV_SEL, $1); pkg_free($1); }
2015
-	| fcmd				{$$=mk_rval_expr_v(RV_ACTION_ST, $1); }
2016
-	| LBRACE actions RBRACE	{$$=mk_rval_expr_v(RV_ACTION_ST, $2); }
2014
+	| select_id			{$$=mk_rve_rval(RV_SEL, $1); pkg_free($1); }
2015
+	| fcmd				{$$=mk_rve_rval(RV_ACTION_ST, $1); }
2016
+	| exp_elem { $$=mk_rve_rval(RV_BEXPR, $1); }
2017
+	| LBRACE actions RBRACE	{$$=mk_rve_rval(RV_ACTION_ST, $2); }
2017 2018
 	| LBRACE error RBRACE	{ yyerror("bad command block"); }
2018
-	| LPAREN assign_action RPAREN	{$$=mk_rval_expr_v(RV_ACTION_ST, $2); }
2019
+	| LPAREN assign_action RPAREN	{$$=mk_rve_rval(RV_ACTION_ST, $2); }
2019 2020
 	| LPAREN error RPAREN	{ yyerror("bad expression"); }
2020 2021
 	;
2021 2022
 
... ...
@@ -2039,36 +2045,32 @@ rval_expr: rval						{ $$=$1;
2039 2039
 												YYABORT;
2040 2040
 											}
2041 2041
 									}
2042
-		| rve_un_op %prec NOT rval_expr 	{$$=mk_rval_expr1($1, $2); }
2043
-		| rval_expr PLUS rval_expr	{$$=mk_rval_expr2(RVE_PLUS_OP, $1, $3); }
2044
-		| rval_expr MINUS rval_expr	{$$=mk_rval_expr2(RVE_MINUS_OP, $1, $3); }
2045
-		| rval_expr STAR rval_expr	{$$=mk_rval_expr2(RVE_MUL_OP, $1, $3); }
2046
-		| rval_expr SLASH rval_expr	{$$=mk_rval_expr2(RVE_DIV_OP, $1, $3); }
2047
-		| rval_expr BIN_OR rval_expr {$$=mk_rval_expr2(RVE_BOR_OP, $1,  $3); }
2048
-		| rval_expr BIN_AND rval_expr {$$=mk_rval_expr2(RVE_BAND_OP, $1,  $3);}
2049
-		| rval_expr rve_cmpop %prec GT rval_expr 
2050
-			{ $$=mk_rval_expr2( $2, $1, $3);}
2042
+		| rve_un_op %prec NOT rval_expr	{$$=mk_rve1($1, $2); }
2043
+		| rval_expr PLUS rval_expr		{$$=mk_rve2(RVE_PLUS_OP, $1, $3); }
2044
+		| rval_expr MINUS rval_expr		{$$=mk_rve2(RVE_MINUS_OP, $1, $3); }
2045
+		| rval_expr STAR rval_expr		{$$=mk_rve2(RVE_MUL_OP, $1, $3); }
2046
+		| rval_expr SLASH rval_expr		{$$=mk_rve2(RVE_DIV_OP, $1, $3); }
2047
+		| rval_expr BIN_OR rval_expr	{$$=mk_rve2(RVE_BOR_OP, $1,  $3); }
2048
+		| rval_expr BIN_AND rval_expr	{$$=mk_rve2(RVE_BAND_OP, $1,  $3);}
2049
+		| rval_expr rve_cmpop %prec GT rval_expr { $$=mk_rve2( $2, $1, $3);}
2051 2050
 		| rval_expr rve_equalop %prec EQUAL_T rval_expr
2052
-			{ $$=mk_rval_expr2( $2, $1, $3);}
2053
-		| rval_expr LOG_AND rval_expr
2054
-			{ $$=mk_rval_expr2(RVE_LAND_OP, $1, $3);}
2055
-		| rval_expr LOG_OR rval_expr
2056
-			{ $$=mk_rval_expr2(RVE_LOR_OP, $1, $3);}
2057
-		| LPAREN rval_expr RPAREN	{ $$=$2; }
2058
-		| exp_elem { $$=mk_rval_expr_v(RV_BEXPR, $1); }
2059
-		| rve_un_op %prec NOT error { yyerror("bad expression"); }
2060
-		| rval_expr PLUS error		{ yyerror("bad expression"); }
2061
-		| rval_expr MINUS error		{ yyerror("bad expression"); }
2062
-		| rval_expr STAR error		{ yyerror("bad expression"); }
2063
-		| rval_expr SLASH error		{ yyerror("bad expression"); }
2064
-		| rval_expr BIN_OR error	{ yyerror("bad expression"); }
2065
-		| rval_expr BIN_AND error	{ yyerror("bad expression"); }
2051
+			{ $$=mk_rve2( $2, $1, $3);}
2052
+		| rval_expr LOG_AND rval_expr	{ $$=mk_rve2(RVE_LAND_OP, $1, $3);}
2053
+		| rval_expr LOG_OR rval_expr	{ $$=mk_rve2(RVE_LOR_OP, $1, $3);}
2054
+		| LPAREN rval_expr RPAREN		{ $$=$2; }
2055
+		| rve_un_op %prec NOT error		{ yyerror("bad expression"); }
2056
+		| rval_expr PLUS error			{ yyerror("bad expression"); }
2057
+		| rval_expr MINUS error			{ yyerror("bad expression"); }
2058
+		| rval_expr STAR error			{ yyerror("bad expression"); }
2059
+		| rval_expr SLASH error			{ yyerror("bad expression"); }
2060
+		| rval_expr BIN_OR error		{ yyerror("bad expression"); }
2061
+		| rval_expr BIN_AND error		{ yyerror("bad expression"); }
2066 2062
 		| rval_expr rve_cmpop %prec GT error
2067 2063
 			{ yyerror("bad expression"); }
2068 2064
 		| rval_expr rve_equalop %prec EQUAL_T error
2069 2065
 			{ yyerror("bad expression"); }
2070
-		| rval_expr LOG_AND error	{ yyerror("bad expression"); }
2071
-		| rval_expr LOG_OR error	{ yyerror("bad expression"); }
2066
+		| rval_expr LOG_AND error		{ yyerror("bad expression"); }
2067
+		| rval_expr LOG_OR error		{ yyerror("bad expression"); }
2072 2068
 		;
2073 2069
 
2074 2070
 assign_action: lval assign_op  rval_expr	{ $$=mk_action($2, 2, LVAL_ST, $1, 
... ...
@@ -2562,8 +2564,14 @@ static void warn(char* s)
2562 2562
 	cfg_warnings++;
2563 2563
 }
2564 2564
 
2565
-static void yyerror(char* s)
2565
+static void yyerror(char* format, ...)
2566 2566
 {
2567
+	va_list ap;
2568
+	char s[256];
2569
+	
2570
+	va_start(ap, format);
2571
+	vsnprintf(s, sizeof(s), format, ap);
2572
+	va_end(ap);
2567 2573
 	if (line!=startline)
2568 2574
 		LOG(L_CRIT, "*** PARSE ERROR *** (%d,%d-%d,%d): %s\n", 
2569 2575
 					startline, startcolumn, line, column-1, s);
... ...
@@ -2577,6 +2585,72 @@ static void yyerror(char* s)
2577 2577
 }
2578 2578
 
2579 2579
 
2580
+/** mk_rval_expr_v wrapper.
2581
+ *  checks mk_rval_expr_v return value and sets the cfg. pos
2582
+ *  (line and column numbers)
2583
+ *  @return rval_expr* on success, 0 on error (@see mk_rval_expr_v)
2584
+ */
2585
+static struct rval_expr* mk_rve_rval(enum rval_type type, void* v)
2586
+{
2587
+	struct rval_expr* ret;
2588
+
2589
+	ret=mk_rval_expr_v(type, v);
2590
+	if (ret==0){
2591
+		yyerror("internal error: failed to create rval expr");
2592
+		/* YYABORT; */
2593
+	}
2594
+	return ret;
2595
+}
2596
+
2597
+
2598
+/** mk_rval_expr1 wrapper.
2599
+ *  checks mk_rval_expr1 return value (!=0 and type checking)
2600
+ *  @return rval_expr* on success, 0 on error (@see mk_rval_expr1)
2601
+ */
2602
+static struct rval_expr* mk_rve1(enum rval_expr_op op, struct rval_expr* rve1)
2603
+{
2604
+	struct rval_expr* ret;
2605
+	struct rval_expr* bad_rve;
2606
+	enum rval_type type, bad_t, exp_t;
2607
+	
2608
+	ret=mk_rval_expr1(op, rve1);
2609
+	if (ret && (rve_check_type(&type, ret, &bad_rve, &bad_t, &exp_t)!=1)){
2610
+		yyerror("bad expression: type mismatch (%s instead of %s)",
2611
+					rval_type_name(bad_t), rval_type_name(exp_t));
2612
+	}
2613
+	return ret;
2614
+}
2615
+
2616
+
2617
+/** mk_rval_expr2 wrapper.
2618
+ *  checks mk_rval_expr2 return value (!=0 and type checking)
2619
+ *  @return rval_expr* on success, 0 on error (@see mk_rval_expr2)
2620
+ */
2621
+static struct rval_expr* mk_rve2(enum rval_expr_op op, struct rval_expr* rve1,
2622
+									struct rval_expr* rve2)
2623
+{
2624
+	struct rval_expr* ret;
2625
+	struct rval_expr* bad_rve;
2626
+	enum rval_type type, type1, type2, bad_t, exp_t;
2627
+	
2628
+	ret=mk_rval_expr2(op, rve1, rve2);
2629
+	if (ret && (rve_check_type(&type, ret, &bad_rve, &bad_t, &exp_t)!=1)){
2630
+		if (rve_check_type(&type1, rve1, &bad_rve, &bad_t, &exp_t)!=1)
2631
+			yyerror("bad expression: left side type mismatch"
2632
+					" (%s instead of %s)",
2633
+					rval_type_name(bad_t), rval_type_name(exp_t));
2634
+		else if (rve_check_type(&type2, rve2, &bad_rve, &bad_t, &exp_t)!=1)
2635
+			yyerror("bad expression: right side type mismatch"
2636
+					" (%s instead of %s)",
2637
+					rval_type_name(bad_t), rval_type_name(exp_t));
2638
+		else
2639
+			yyerror("bad expression: type mismatch (%s instead of %s)",
2640
+					rval_type_name(bad_t), rval_type_name(exp_t));
2641
+	}
2642
+	return ret;
2643
+}
2644
+
2645
+
2580 2646
 static struct name_lst* mk_name_lst(char* host, int flags)
2581 2647
 {
2582 2648
 	struct name_lst* l;