Browse code

- started to build the expresion & acion tree/list

Andrei Pelinescu-Onciul authored on 19/09/2001 02:19:09
Showing 4 changed files
... ...
@@ -149,19 +149,19 @@ EAT_ABLE	[\ \t\b\r]
149 149
 						addstr(yytext, &str);
150 150
 						if (str){
151 151
 							printf("Found string1 <%s>\n", str);
152
-							yyleng=strlen(str)+1;
153
-							memcpy(yytext, str, yyleng);
154
-							free(str);
155
-							str=0;
156 152
 						}else{
157 153
 							printf("WARNING: empty string\n");
158 154
 						}
159
-						yylval.strval=yytext; return STRING;
155
+						yylval.strval=str; str=0;
156
+						return STRING;
160 157
 					}
161 158
 <STRING2>{TICK}  { count(); state=INITIAL_S; BEGIN(INITIAL); 
162 159
 						yytext[yyleng-1]=0; yyleng--;
163 160
 						printf("Found string1 <%s>\n", yytext);
164
-						yylval.strval=yytext; return STRING;
161
+						addstr(yytext, &str);
162
+						yylval.strval=str;
163
+						str=0;
164
+						return STRING;
165 165
 					}
166 166
 <STRING2>.|{EAT_ABLE}|{CR}	{ yymore(); }
167 167
 
... ...
@@ -6,11 +6,15 @@
6 6
 
7 7
 %{
8 8
 
9
+#include "route_struct.h"
10
+
9 11
 %}
10 12
 
11 13
 %union {
12 14
 	int intval;
13 15
 	char* strval;
16
+	struct expr* expr;
17
+	struct action* action;
14 18
 }
15 19
 
16 20
 /* terminals */
... ...
@@ -46,9 +50,9 @@
46 46
 %left OR
47 47
 
48 48
 /* values */
49
-%token NUMBER
50
-%token ID
51
-%token STRING
49
+%token <intval> NUMBER
50
+%token <strval> ID
51
+%token <strval> STRING
52 52
 
53 53
 /* other */
54 54
 %token COMMA
... ...
@@ -65,6 +69,8 @@
65 65
 
66 66
 
67 67
 /*non-terminals */
68
+%type <expr> exp, condition,  exp_elem
69
+%type <action> action, actions, cmd
68 70
 
69 71
 
70 72
 
... ...
@@ -75,95 +81,181 @@ cfg:	statements
75 75
 	;
76 76
 
77 77
 statements:	statements statement {printf("got <> <>\n");}
78
-			| statement {printf("got a statement<>\n"); }
78
+		| statement {printf("got a statement<>\n"); }
79
+		| statements error { yyerror(""); }
79 80
 	;
80 81
 
81 82
 statement:	assign_stm CR
82
-			| route_stm CR
83
-			| CR	/* null statement*/
83
+		| route_stm CR
84
+		| CR	/* null statement*/
84 85
 	;
85 86
 
86
-assign_stm:	DEBUG EQUAL NUMBER |
87
-			FORK  EQUAL NUMBER |
88
-			LOGSTDERROR EQUAL NUMBER |
89
-			DNS EQUAL NUMBER |
90
-			REV_DNS EQUAL NUMBER |
91
-			LISTEN EQUAL ipv4 |
92
-			LISTEN EQUAL ID |
93
-			LISTEN EQUAL STRING 
87
+assign_stm:	DEBUG EQUAL NUMBER 
88
+		| DEBUG EQUAL error  { yyerror("number  expected"); }
89
+		| FORK  EQUAL NUMBER
90
+		| FORK  EQUAL error  { yyerror("boolean value expected"); }
91
+		| LOGSTDERROR EQUAL NUMBER 
92
+		| LOGSTDERROR EQUAL error { yyerror("boolean value expected"); }
93
+		| DNS EQUAL NUMBER
94
+		| DNS EQUAL error { yyerror("boolean value expected"); }
95
+		| REV_DNS EQUAL NUMBER 
96
+		| REV_DNS EQUAL error { yyerror("boolean value expected"); }
97
+		| LISTEN EQUAL ipv4 
98
+		| LISTEN EQUAL ID 
99
+		| LISTEN EQUAL STRING
100
+		| LISTEN EQUAL  error { yyerror("ip address or hostname"
101
+						"expected"); }
102
+		| error EQUAL { yyerror("unknown config variable"); }
94 103
 	;
95 104
 
96 105
 
97 106
 ipv4:	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
98 107
 	;
99 108
 
100
-route_stm:	ROUTE LBRACE rules RBRACE |
101
-			ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE
109
+route_stm:	ROUTE LBRACE rules RBRACE 
110
+		| ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE
111
+		| ROUTE error { yyerror("invalid  route  statement"); }
102 112
 	;
103 113
 
104
-rules:	rules rule |
105
-		rule
114
+rules:	rules rule
115
+	| rule
116
+	| rules error { yyerror("invalid rule"); }
106 117
 	 ;
107 118
 
108 119
 rule:	condition	actions CR
109
-	 	| CR  /* null rule */
120
+	| CR  /* null rule */
121
+	| condition error { yyerror("bad actions in rule"); }
110 122
 	;
111 123
 
112
-condition:	exp_elem |
113
-			LPAREN exp RPAREN
124
+condition:	exp
114 125
 	;
115 126
 
116
-exp:	exp AND condition |
117
-		exp OR  condition |
118
-		NOT condition |
119
-		condition
127
+exp:	exp AND exp 
128
+	| exp OR  exp 
129
+	| NOT exp 
130
+	| LPAREN exp RPAREN
131
+	| exp_elem
120 132
 	;
121 133
 
122
-exp_elem:	METHOD EQUAL_T STRING |
123
-			METHOD EQUAL_T ID |
124
-			METHOD MATCH STRING |
125
-			METHOD MATCH ID |
126
-			URI EQUAL_T STRING |
127
-			URI EQUAL_T ID |
128
-			URI MATCH STRING |
129
-			URI MATCH ID |
130
-			SRCIP EQUAL_T net4 |
131
-			SRCIP EQUAL_T STRING |
132
-			SRCIP EQUAL_T host |
133
-			SRCIP MATCH STRING 
134
-			DSTIP EQUAL_T net4 |
135
-			DSTIP EQUAL_T STRING |
136
-			DSTIP MATCH STRING
134
+exp_elem:	METHOD EQUAL_T STRING	{$$= mk_elem(	EQUAL_OP,
135
+							STRING_ST, 
136
+							METHOD_O,
137
+							$3);
138
+					}
139
+		| METHOD EQUAL_T ID	{$$ = mk_elem(	EQUAL_OP,
140
+							STRING_ST,
141
+							METHOD_O,
142
+							$3); 
143
+				 	}
144
+		| METHOD EQUAL_T error { $$=0; yyerror("string expected"); }
145
+		| METHOD MATCH STRING 	{$$ = mk_elem(	MATCH_OP,
146
+							STRING_ST,
147
+							METHOD_O,
148
+							$3); 
149
+				 	}
150
+		| METHOD MATCH ID	{$$ = mk_elem(	MATCH_OP,
151
+							STRING_ST,
152
+							METHOD_O,
153
+							$3); 
154
+				 	}
155
+		| METHOD MATCH error { $$=0; yyerror("string expected"); }
156
+		| METHOD error	{ $$=0; 
157
+				  yyerror("invalid operator,"
158
+				  		"== or ~= expected");
159
+				}
160
+		| URI EQUAL_T STRING 	{$$ = mk_elem(	EQUAL_OP,
161
+							STRING_ST,
162
+							URI_O,
163
+							$3); 
164
+				 	}
165
+		| URI EQUAL_T ID 	{$$ = mk_elem(	EQUAL_OP,
166
+							STRING_ST,
167
+							URI_O,
168
+							$3); 
169
+				 	}
170
+		| URI EQUAL_T error { $$=0; yyerror("string expected"); }
171
+		| URI MATCH STRING 
172
+		| URI MATCH ID
173
+		| URI MATCH error {  $$=0; yyerror("string expected"); }
174
+		| URI error	{ $$=0; 
175
+				  yyerror("invalid operator,"
176
+				  		" == or ~= expected");
177
+				}
178
+		| SRCIP EQUAL_T net4 
179
+		| SRCIP EQUAL_T STRING 
180
+		| SRCIP EQUAL_T host
181
+		| SRCIP EQUAL_T error { yyerror( "ip address or hostname"
182
+						 "expected" ); }
183
+		| SRCIP MATCH STRING
184
+		| SRCIP MATCH ID
185
+		| SRCIP MATCH error  { yyerror( "hostname expected"); }
186
+		| SRCIP error  {yyerror("invalid operator, == or ~= expected");}
187
+		| DSTIP EQUAL_T net4 
188
+		| DSTIP EQUAL_T STRING
189
+		| DSTIP EQUAL_T host
190
+		| DSTIP EQUAL_T error { yyerror( "ip address or hostname"
191
+						 "expected" ); }
192
+		| DSTIP MATCH STRING
193
+		| DSTIP MATCH ID
194
+		| DSTIP MATCH error  { yyerror ( "hostname  expected" ); }
195
+		| DSTIP error {yyerror("invalid operator, == or ~= expected");}
137 196
 	;
138 197
 
139
-net4:	ipv4 SLASH ipv4 |
140
-		ipv4 SLASH NUMBER |
141
-		ipv4
198
+net4:	ipv4 SLASH ipv4 
199
+	| ipv4 SLASH NUMBER 
200
+	| ipv4
201
+	| ipv4 SLASH error {yyerror("netmask (eg:255.0.0.0 or 8) expected");}
142 202
 	;
143 203
 
144
-host:	ID |
145
-		host DOT ID
204
+host:	ID
205
+	| host DOT ID
206
+	| host DOT error { yyerror("invalid hostname"); }
146 207
 	;
147 208
 
148 209
 
149
-actions:	actions action |
150
-			action
210
+actions:	actions action 
211
+		| action
212
+		| actions error { yyerror("bad command"); }
213
+	;
214
+
215
+action:		cmd SEMICOLON
216
+		| SEMICOLON /* null action */
217
+		| cmd error { yyerror("bad command: missing ';'?"); }
151 218
 	;
152 219
 
153
-action:		cmd SEMICOLON |
154
-			SEMICOLON /* null action */
155
-
156
-cmd:		FORWARD LPAREN host RPAREN |
157
-			FORWARD LPAREN STRING RPAREN |
158
-			FORWARD LPAREN ipv4 RPAREN |
159
-			SEND LPAREN host RPAREN |
160
-			SEND LPAREN STRING RPAREN |
161
-			SEND LPAREN ipv4 RPAREN |
162
-			DROP LPAREN RPAREN |
163
-			DROP |
164
-			LOG LPAREN STRING RPAREN |
165
-			ERROR LPAREN STRING COMMA STRING RPAREN |
166
-			ROUTE LPAREN NUMBER RPAREN
220
+cmd:		FORWARD LPAREN host RPAREN 
221
+		| FORWARD LPAREN STRING RPAREN 
222
+		| FORWARD LPAREN ipv4 RPAREN 
223
+		| FORWARD LPAREN host COMMA NUMBER RPAREN
224
+		| FORWARD LPAREN STRING COMMA NUMBER RPAREN
225
+		| FORWARD LPAREN ipv4 COMMA NUMBER RPAREN
226
+		| FORWARD error { yyerror("missing '(' or ')' ?"); }
227
+		| FORWARD LPAREN error RPAREN { yyerror("bad forward"
228
+						"argument"); }
229
+		| SEND LPAREN host RPAREN 
230
+		| SEND LPAREN STRING RPAREN 
231
+		| SEND LPAREN ipv4 RPAREN
232
+		| SEND LPAREN host COMMA NUMBER RPAREN
233
+		| SEND LPAREN STRING COMMA NUMBER RPAREN
234
+		| SEND LPAREN ipv4 COMMA NUMBER RPAREN
235
+		| SEND error { yyerror("missing '(' or ')' ?"); }
236
+		| SEND LPAREN error RPAREN { yyerror("bad send"
237
+						"argument"); }
238
+		| DROP LPAREN RPAREN 
239
+		| DROP 
240
+		| LOG LPAREN STRING RPAREN
241
+		| LOG LPAREN NUMBER COMMA STRING RPAREN
242
+		| LOG error { yyerror("missing '(' or ')' ?"); }
243
+		| LOG LPAREN error RPAREN { yyerror("bad log"
244
+						"argument"); }
245
+		| ERROR LPAREN STRING COMMA STRING RPAREN 
246
+		| ERROR error { yyerror("missing '(' or ')' ?"); }
247
+		| ERROR LPAREN error RPAREN { yyerror("bad error"
248
+						"argument"); }
249
+		| ROUTE LPAREN NUMBER RPAREN
250
+		| ROUTE error { yyerror("missing '(' or ')' ?"); }
251
+		| ROUTE LPAREN error RPAREN { yyerror("bad route"
252
+						"argument"); }
167 253
 	;
168 254
 
169 255
 
170 256
new file mode 100644
... ...
@@ -0,0 +1,210 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * route structures helping functions
4
+ */
5
+
6
+
7
+#include  "route_struct.h"
8
+
9
+#include <stdio.h>
10
+
11
+struct expr* mk_exp(int op, struct expr* left, struct expr* right)
12
+{
13
+	struct expr * e;
14
+	e=(struct expr*)malloc(sizeof (struct expr));
15
+	if (e==0) goto error;
16
+	e->type=EXP_T;
17
+	e->op=op;
18
+	e->l.expr=left;
19
+	e->r.expr=right;
20
+	return e;
21
+error:
22
+	fprintf(stderr, "ERROR: mk_exp: memory allocation failure\n");
23
+	return 0;
24
+}
25
+
26
+
27
+struct expr* mk_elem(int op, int subtype, int operand, void* param)
28
+{
29
+	struct expr * e;
30
+	e=(struct expr*)malloc(sizeof (struct expr));
31
+	if (e==0) goto error;
32
+	e->type=ELEM_T;
33
+	e->op=op;
34
+	e->subtype=subtype;
35
+	e->l.operand=operand;
36
+	e->r.param=param;
37
+	return e;
38
+error:
39
+	fprintf(stderr, "ERROR: mk_elem: memory allocation failure\n");
40
+	return 0;
41
+}
42
+
43
+
44
+
45
+struct action* mk_action(int type, int p1_type, int p2_type, void* p1, void* p2)
46
+{
47
+	struct action* a;
48
+	a=(struct action*)malloc(sizeof(struct action));
49
+	if (a==0) goto  error;
50
+	a->type=type;
51
+	a->p1_type=p1_type;
52
+	a->p2_type=p2_type;
53
+	a->p1.string=(char*) p1;
54
+	a->p2.string=(char*) p2;
55
+	a->next=0;
56
+	return a;
57
+	
58
+error:
59
+	fprintf(stderr, "ERROR: mk_action: memory allocation failure\n");
60
+	return 0;
61
+
62
+}
63
+
64
+
65
+struct action* append_action(struct action* a, struct action* b)
66
+{
67
+	struct action *t;
68
+	if (b==0) return a;
69
+	if (a==0) return b;
70
+	
71
+	for(t=a;t->next;t=t->next);
72
+	t->next=b;
73
+	return a;
74
+}
75
+
76
+	
77
+	
78
+void print_expr(struct expr* exp)
79
+{
80
+	if (exp==0){
81
+		fprintf(stderr, "ERROR: print_expr: null expression!\n");
82
+		return;
83
+	}
84
+	if (exp->type==ELEM_T){
85
+		switch(exp->l.operand){
86
+			case METHOD_O:
87
+				printf("method");
88
+				break;
89
+			case URI_O:
90
+				printf("uri");
91
+				break;
92
+			case SRCIP_O:
93
+				printf("srcip");
94
+				break;
95
+			case DSTIP_O:
96
+				printf("dstip");
97
+				break;
98
+			default:
99
+				printf("UNKNOWN");
100
+		}
101
+		switch(exp->op){
102
+			case EQUAL_OP:
103
+				printf("==");
104
+				break;
105
+			case MATCH_OP:
106
+				printf("~=");
107
+			default:
108
+				priint("<UNKNOWN>");
109
+		}
110
+		switch(exp->subtype){
111
+			case NOSUBTYPE: 
112
+					printf("N/A");
113
+					break;
114
+			case STRING_ST:
115
+					printf("\"%s\"", (char*)exp->r.param);
116
+					break;
117
+			case NET_ST:
118
+					printf("");
119
+					break;
120
+			default:
121
+					prinf("UNKNOWN");
122
+		}
123
+	}else if (exp->type==EXP_T){
124
+		switch(exp->op){
125
+			case AND_OP:
126
+					printf("AND( ");
127
+					print_expr(exp->l.expr);
128
+					printf(", ");
129
+					print_expr(exp->r.expr);
130
+					printf(" )");
131
+					break;
132
+			case OR_OP:
133
+					printf("OR( ");
134
+					print_expr(exp->l.expr);
135
+					printf(", ");
136
+					print_expr(exp->r.expr);
137
+					printf(" )");
138
+					break;
139
+			case NOT_OP:	
140
+					printf("NOT( ");
141
+					print_expr(exp->l.expr);
142
+					printf(" )");
143
+					break;
144
+			default:
145
+					printf("UNKNOWN_EXP ");
146
+		}
147
+					
148
+	}else{
149
+		printf("ERROR:print_expr: unknown type\n");
150
+	}
151
+}
152
+					
153
+
154
+					
155
+
156
+void print_action(struct action* a)
157
+{
158
+	struct action* t;
159
+	for(t=a; t!=0;t=t->next){
160
+		switch(t->type){
161
+			case FORWARD_T:
162
+					printf("forward(");
163
+					break;
164
+			case SEND_T:
165
+					printf("send(");
166
+					break;
167
+			case DROP_T:
168
+					printf("drop(");
169
+					break;
170
+			case LOG_T:
171
+					printf("log(");
172
+					break;
173
+			case ERROR_T:
174
+					printf("error(");
175
+					break;
176
+			default:
177
+					printf("UNKNOWN(");
178
+		}
179
+		switch(t->p1_type){
180
+			case STRING_ST:
181
+					printf("\"%s\"", t->p1.string);
182
+					break;
183
+			case NUMBER_ST:
184
+					printf("%d",t->p1.number);
185
+					break;
186
+			default:
187
+					printf("type<%d>", t->p1_type);
188
+		}
189
+		switch(t->p2_type){
190
+			case NOSUBTYPE:
191
+					break;
192
+			case STRING_ST:
193
+					printf(", \"%s\"", t->p2.string);
194
+					break;
195
+			case NUMBER_ST:
196
+					printf(", %d",t->p2.number);
197
+					break;
198
+			default:
199
+					printf(", type<%d>", t->p2_type);
200
+		}
201
+		printf("); ");
202
+	}
203
+}
204
+			
205
+	
206
+
207
+	
208
+	
209
+
0 210
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ */
4
+
5
+#ifndef route_struct_h
6
+#define route_struct_h
7
+
8
+enum { EXP_T=1, ELEM_T };
9
+enum { AND_OP=1, OR_OP, NOT_OP };
10
+enum { EQUAL_OP=10, MATCH_OP };
11
+enum { METHOD_O=1, URI_O, SRCIP_O, DSTIP_O };
12
+
13
+enum { FORWARD_T=1, SEND_T, DROP_T, LOG_T, ERROR_T};
14
+enum { NOSUBTYPE=0, STRING_ST, NET_ST, NUMBER_ST, IP_ST };
15
+
16
+	
17
+struct expr{
18
+	int type; /* exp, exp_elem */
19
+	int op; /* and, or, not | ==,  ~= */
20
+	int  subtype;
21
+	union {
22
+		struct expr* expr;
23
+		int operand;
24
+	}l;
25
+	union {
26
+		struct expr* expr;
27
+		void* param;
28
+	}r;
29
+};
30
+
31
+
32
+struct action{
33
+	int type;  /* forward, drop, log, send ...*/
34
+	int p1_type;
35
+	int p2_type;
36
+	union {
37
+		int number;
38
+		char* string;
39
+		void* data;
40
+	}p1, p2;
41
+	struct action* next;
42
+};
43
+
44
+struct expr* mk_exp(int op, struct expr* left, struct expr* right);
45
+struct expr* mk_elem(int op, int subtype, int operand, void* param);
46
+struct action* mk_action(int type, int p1_type, int p2_type, void* p1, void* p2);
47
+
48
+void print_action(struct action* a);
49
+void print_expr(struct expr* exp);
50
+
51
+
52
+
53
+
54
+
55
+#endif
56
+