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