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