Browse code

- new config parser (not finished yet)

Andrei Pelinescu-Onciul authored on 18/09/2001 04:56:43
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,252 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ * scanner for cfg files
4
+ */
5
+
6
+
7
+%{
8
+	#include "cfg.tab.h"
9
+	#include <string.h>
10
+
11
+	/* states */
12
+	#define INITIAL_S		0
13
+	#define COMMENT_S		1
14
+	#define COMMENT_LN_S	2
15
+	#define STRING_S		3
16
+
17
+	
18
+	static int comment_nest=0;
19
+	static int state=0;
20
+	static char* str=0;
21
+	int line=0;
22
+	int column=0;
23
+
24
+	static char* addstr(char*, char**);
25
+	static void count();
26
+
27
+
28
+%}
29
+
30
+/* start conditions */
31
+%x STRING1 STRING2 COMMENT COMMENT_LN
32
+
33
+/* action keywords */
34
+FORWARD	forward
35
+DROP	drop
36
+SEND	send
37
+LOG		log
38
+ERROR	error
39
+ROUTE	route
40
+/* condition keywords */
41
+METHOD	method
42
+URI		uri
43
+SRCIP	src_ip
44
+DSTIP	dst_ip
45
+/* operators */
46
+EQUAL	=
47
+EQUAL_T	==
48
+MATCH	~=
49
+NOT		!
50
+AND		"and"|"&&"|"&"
51
+OR		"or"|"||"|"|"
52
+
53
+/* config vars. */
54
+DEBUG	debug
55
+FORK	fork
56
+LOGSTDERROR	log_stderror
57
+LISTEN		listen
58
+DNS		 dns
59
+REV_DNS	 rev_dns
60
+
61
+/* values */
62
+YES			"yes"|"true"|"on"|"enable"
63
+NO			"no"|"false"|"off"|"disable"
64
+
65
+LETTER		[a-zA-Z]
66
+DIGIT		[0-9]
67
+ALPHANUM	{LETTER}|{DIGIT}
68
+NUMBER		{DIGIT}+
69
+ID			{LETTER}{ALPHANUM}*
70
+QUOTES		\"
71
+TICK		\'
72
+SLASH		"/"
73
+SEMICOLON	;
74
+RPAREN		\)
75
+LPAREN		\(
76
+LBRACE		\{
77
+RBRACE		\}
78
+LBRACK		\[
79
+RBRACK		\]
80
+COMMA		,
81
+DOT			\.
82
+CR			\n
83
+
84
+
85
+
86
+COM_LINE	#
87
+COM_START	"/\*"
88
+COM_END		"\*/"
89
+
90
+EAT_ABLE	[\ \t\b\r]
91
+
92
+%%
93
+
94
+
95
+<INITIAL>{EAT_ABLE}	{ count(); }
96
+
97
+<INITIAL>{FORWARD}	{count(); yylval.strval=yytext; return FORWARD; }
98
+<INITIAL>{DROP}	{ count(); yylval.strval=yytext; return DROP; }
99
+<INITIAL>{SEND}	{ count(); yylval.strval=yytext; return SEND; }
100
+<INITIAL>{LOG}	{ count(); yylval.strval=yytext; return LOG; }
101
+<INITIAL>{ERROR}	{ count(); yylval.strval=yytext; return ERROR; }
102
+<INITIAL>{ROUTE}	{ count(); yylval.strval=yytext; return ROUTE; }
103
+
104
+<INITIAL>{METHOD}	{ count(); yylval.strval=yytext; return METHOD; }
105
+<INITIAL>{URI}	{ count(); yylval.strval=yytext; return URI; }
106
+<INITIAL>{SRCIP}	{ count(); yylval.strval=yytext; return SRCIP; }
107
+<INITIAL>{DSTIP}	{ count(); yylval.strval=yytext; return DSTIP; }
108
+
109
+<INITIAL>{DEBUG}	{ count(); yylval.strval=yytext; return DEBUG; }
110
+<INITIAL>{FORK}		{ count(); yylval.strval=yytext; return FORK; }
111
+<INITIAL>{LOGSTDERROR}	{ yylval.strval=yytext; return LOGSTDERROR; }
112
+<INITIAL>{LISTEN}	{ count(); yylval.strval=yytext; return LISTEN; }
113
+<INITIAL>{DNS}	{ count(); yylval.strval=yytext; return DNS; }
114
+<INITIAL>{REV_DNS}	{ count(); yylval.strval=yytext; return REV_DNS; }
115
+
116
+<INITIAL>{EQUAL}	{ count(); return EQUAL; }
117
+<INITIAL>{EQUAL_T}	{ count(); return EQUAL_T; }
118
+<INITIAL>{MATCH}	{ count(); return MATCH; }
119
+<INITIAL>{NOT}		{ count(); return NOT; }
120
+<INITIAL>{AND}		{ count(); return AND; }
121
+<INITIAL>{OR}		{ count(); return OR;  }
122
+
123
+<INITIAL>{NUMBER}		{ count(); yylval.intval=atoi(yytext);
124
+							return NUMBER; }
125
+<INITIAL>{YES}			{ count(); yylval.intval=1; return NUMBER; }
126
+<INITIAL>{NO}			{ count(); yylval.intval=0; return NUMBER; }
127
+
128
+<INITIAL>{COMMA}		{ count(); return COMMA; }
129
+<INITIAL>{SEMICOLON}	{ count(); return SEMICOLON; }
130
+<INITIAL>{RPAREN}	{ count(); return RPAREN; }
131
+<INITIAL>{LPAREN}	{ count(); return LPAREN; }
132
+<INITIAL>{LBRACE}	{ count(); return LBRACE; }
133
+<INITIAL>{RBRACE}	{ count(); return RBRACE; }
134
+<INITIAL>{LBRACK}	{ count(); return LBRACK; }
135
+<INITIAL>{RBRACK}	{ count(); return RBRACK; }
136
+<INITIAL>{SLASH}	{ count(); return SLASH; }
137
+<INITIAL>{DOT}		{ count(); return DOT; }
138
+<INITIAL>\\{CR}		{count(); } /* eat the escaped CR */
139
+<INITIAL>{CR}		{ count(); return CR; }
140
+
141
+
142
+<INITIAL>{QUOTES} { count(); state=STRING_S; BEGIN(STRING1); }
143
+<INITIAL>{TICK} { count(); state=STRING_S; BEGIN(STRING2); }
144
+
145
+
146
+<STRING1>{QUOTES} { count(); state=INITIAL_S; BEGIN(INITIAL); 
147
+						yytext[yyleng-1]=0; yyleng--;
148
+						addstr(yytext, &str);
149
+						if (str){
150
+							printf("Found string1 <%s>\n", str);
151
+							yyleng=strlen(str)+1;
152
+							memcpy(yytext, str, yyleng);
153
+							free(str);
154
+							str=0;
155
+						}else{
156
+							printf("WARNING: empty string\n");
157
+						}
158
+						yylval.strval=yytext; return STRING;
159
+					}
160
+<STRING2>{TICK}  { count(); state=INITIAL_S; BEGIN(INITIAL); 
161
+						yytext[yyleng-1]=0; yyleng--;
162
+						printf("Found string1 <%s>\n", yytext);
163
+						yylval.strval=yytext; return STRING;
164
+					}
165
+<STRING2>.|{EAT_ABLE}|{CR}	{ yymore(); }
166
+
167
+<STRING1>\\n		{ count(); yytext[yyleng-2]='\n';yytext[yyleng-1]=0; 
168
+						yyleng--; addstr(yytext, &str); }
169
+<STRING1>\\t		{ count(); yytext[yyleng-2]='\t';yytext[yyleng-1]=0; 
170
+						yyleng--; addstr(yytext, &str); }
171
+<STRING1>\\\\		{ count(); yytext[yyleng-2]='\\';yytext[yyleng-1]=0; 
172
+						yyleng--; addstr(yytext, &str); } 
173
+<STRING1>.|{EAT_ABLE}|{CR}	{ yymore(); }
174
+
175
+
176
+<INITIAL,COMMENT>{COM_START}	{ count(); comment_nest++; state=COMMENT_S;
177
+										BEGIN(COMMENT); }
178
+<COMMENT>{COM_END}				{ count(); comment_nest--;
179
+										if (comment_nest==0){
180
+											state=INITIAL_S;
181
+											BEGIN(INITIAL);
182
+										}
183
+								}
184
+<COMMENT>.|{EAT_ABLE}|{CR}				{ count(); };
185
+
186
+<INITIAL>{COM_LINE}.*{CR}	{ count(); } 
187
+
188
+<INITIAL>{ID}			{ count(); yylval.strval=yytext; return ID; }
189
+
190
+
191
+<<EOF>>							{
192
+									switch(state){
193
+										case STRING_S: 
194
+											printf("Unexpected EOF: closed string\n");
195
+											if (str) {free(str); str=0;}
196
+											break;
197
+										case COMMENT_S:
198
+											printf("Unexpected EOF:%d comments open\n", comment_nest);
199
+											break;
200
+										case COMMENT_LN_S:
201
+											printf("Unexpected EOF: comment line open\n");
202
+											break;
203
+									}
204
+									return 0;
205
+								}
206
+			
207
+%%
208
+
209
+static char* addstr(char * src, char ** dest)
210
+{
211
+	char *tmp;
212
+	int len1, len2;
213
+	
214
+	if (*dest==0){
215
+		*dest=strdup(src);
216
+	}else{
217
+		len1=strlen(*dest);
218
+		len2=strlen(src);
219
+		tmp=malloc(len1+len2+1);
220
+		if (tmp==0) goto error;
221
+		memcpy(tmp, *dest, len1);
222
+		memcpy(tmp+len1, src, len2);
223
+		tmp[len1+len2]=0;
224
+		free(*dest);
225
+		*dest=tmp;
226
+	}
227
+	return *dest;
228
+error:
229
+	fprintf(stderr, "lex:addstr: memory allocation error\n");
230
+	return 0;
231
+}
232
+
233
+
234
+
235
+static void count()
236
+{
237
+	int i;
238
+	
239
+	for (i=0; i<yyleng;i++){
240
+		if (yytext[i]=='\n'){
241
+			line++;
242
+			column=0;
243
+		}else if (yytext[i]=='\t'){
244
+			column+=8 -(column%8);
245
+		}else{
246
+			column++;
247
+		}
248
+	}
249
+}
250
+
251
+
0 252
new file mode 100644
... ...
@@ -0,0 +1,183 @@
0
+/*
1
+ * $Id$
2
+ *
3
+ *  cfg grammar
4
+ */
5
+
6
+%{
7
+
8
+%}
9
+
10
+%union {
11
+	int intval;
12
+	char* strval;
13
+}
14
+
15
+/* terminals */
16
+
17
+
18
+/* keywors */
19
+%token FORWARD
20
+%token SEND
21
+%token DROP
22
+%token LOG
23
+%token ERROR
24
+%token ROUTE
25
+%token METHOD
26
+%token URI
27
+%token SRCIP
28
+%token DSTIP
29
+
30
+/* config vars. */
31
+%token DEBUG
32
+%token FORK
33
+%token LOGSTDERROR
34
+%token LISTEN
35
+%token DNS
36
+%token REV_DNS
37
+
38
+
39
+/* operators */
40
+%nonassoc EQUAL
41
+%nonassoc EQUAL_T
42
+%nonassoc MATCH
43
+%left NOT
44
+%left AND
45
+%left OR
46
+
47
+/* values */
48
+%token NUMBER
49
+%token ID
50
+%token STRING
51
+
52
+/* other */
53
+%token COMMA
54
+%token SEMICOLON
55
+%token RPAREN
56
+%token LPAREN
57
+%token LBRACE
58
+%token RBRACE
59
+%token LBRACK
60
+%token RBRACK
61
+%token SLASH
62
+%token DOT
63
+%token CR
64
+
65
+
66
+/*non-terminals */
67
+
68
+
69
+
70
+%%
71
+
72
+
73
+cfg:	statements
74
+	;
75
+
76
+statements:	statements statement {printf("got <> <>\n");}
77
+			| statement {printf("got a statement<>\n"); }
78
+	;
79
+
80
+statement:	assign_stm CR
81
+			| route_stm CR
82
+			| CR	/* null statement*/
83
+	;
84
+
85
+assign_stm:	DEBUG EQUAL NUMBER |
86
+			FORK  EQUAL NUMBER |
87
+			LOGSTDERROR EQUAL NUMBER |
88
+			DNS EQUAL NUMBER |
89
+			REV_DNS EQUAL NUMBER |
90
+			LISTEN EQUAL ipv4 |
91
+			LISTEN EQUAL ID |
92
+			LISTEN EQUAL STRING 
93
+	;
94
+
95
+
96
+ipv4:	NUMBER DOT NUMBER DOT NUMBER DOT NUMBER
97
+	;
98
+
99
+route_stm:	ROUTE LBRACE rules RBRACE |
100
+			ROUTE LBRACK NUMBER RBRACK LBRACE rules RBRACE
101
+	;
102
+
103
+rules:	rules rule |
104
+		rule
105
+	 ;
106
+
107
+rule:	condition	actions CR
108
+	 	| CR  /* null rule */
109
+	;
110
+
111
+condition:	exp_elem |
112
+			LPAREN exp RPAREN
113
+	;
114
+
115
+exp:	exp AND condition |
116
+		exp OR  condition |
117
+		NOT condition |
118
+		condition
119
+	;
120
+
121
+exp_elem:	METHOD EQUAL_T STRING |
122
+			METHOD EQUAL_T ID |
123
+			METHOD MATCH STRING |
124
+			METHOD MATCH ID |
125
+			URI EQUAL_T STRING |
126
+			URI EQUAL_T ID |
127
+			URI MATCH STRING |
128
+			URI MATCH ID |
129
+			SRCIP EQUAL_T net4 |
130
+			SRCIP EQUAL_T STRING |
131
+			SRCIP EQUAL_T host |
132
+			SRCIP MATCH STRING 
133
+			DSTIP EQUAL_T net4 |
134
+			DSTIP EQUAL_T STRING |
135
+			DSTIP MATCH STRING
136
+	;
137
+
138
+net4:	ipv4 SLASH ipv4 |
139
+		ipv4 SLASH NUMBER |
140
+		ipv4
141
+	;
142
+
143
+host:	ID |
144
+		host DOT ID
145
+	;
146
+
147
+
148
+actions:	actions action |
149
+			action
150
+	;
151
+
152
+action:		cmd SEMICOLON |
153
+			SEMICOLON /* null action */
154
+
155
+cmd:		FORWARD LPAREN host RPAREN |
156
+			FORWARD LPAREN STRING RPAREN |
157
+			FORWARD LPAREN ipv4 RPAREN |
158
+			SEND LPAREN host RPAREN |
159
+			SEND LPAREN STRING RPAREN |
160
+			SEND LPAREN ipv4 RPAREN |
161
+			DROP LPAREN RPAREN |
162
+			DROP |
163
+			LOG LPAREN STRING RPAREN |
164
+			ERROR LPAREN STRING COMMA STRING RPAREN |
165
+			ROUTE LPAREN NUMBER RPAREN
166
+	;
167
+
168
+
169
+%%
170
+
171
+extern int line;
172
+extern int column;
173
+yyerror(char* s)
174
+{
175
+	fprintf(stderr, "parse error (%d,%d): %s\n", line, column, s);
176
+}
177
+
178
+int main(int argc, char ** argv)
179
+{
180
+	if (yyparse()!=0)
181
+		fprintf(stderr, "parsing error\n");
182
+}