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