]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/config.Y
Filters now do not allow function (int arg; int arg2; ).
[thirdparty/bird.git] / filter / config.Y
1 /*
2 * BIRD - filters
3 *
4 * Copyright 1998,1999 Pavel Machek
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 *
8 FIXME: define keyword
9 FIXME: create ip.mask(x) function
10 FIXME: whole system of paths, path ~ string, path.prepend(), path.originate
11 FIXME: create community lists
12 FIXME: access to dynamic attributes
13 FIXME: local namespace for functions
14 */
15
16 CF_HDR
17
18 #include "nest/bird.h"
19 #include "lib/resource.h"
20 #include "lib/socket.h"
21 #include "lib/timer.h"
22 #include "nest/protocol.h"
23 #include "nest/iface.h"
24 #include "nest/route.h"
25
26 CF_DECLS
27
28 CF_KEYWORDS(FUNCTION, PRINT, PRINTN, CONST,
29 ACCEPT, REJECT, ERROR, QUITBIRD,
30 INT, BOOL, IP, PREFIX, PAIR, SET, STRING,
31 IF, THEN, ELSE, CASE,
32 TRUE, FALSE,
33 RTA, FROM, GW, NET, MASK, RIP_METRIC, RIP_TAG,
34 LEN,
35 IMPOSSIBLE,
36 FILTER
37 )
38
39 %type <x> term block cmds cmd function_body ifthen constant print_one print_list var_list var_listn
40 %type <f> filter filter_body
41 %type <i> type break_command pair
42 %type <e> set_item set_items switch_body
43 %type <v> set_atom prefix prefix_s ipa
44 %type <s> decls declsn one_decl function_params
45
46 CF_GRAMMAR
47
48 CF_ADDTO(conf, filter_def)
49 filter_def:
50 FILTER SYM filter_body {
51 cf_define_symbol($2, SYM_FILTER, $3);
52 $3->name = $2->name;
53 printf( "We have new filter defined (%s)\n", $2->name )
54 }
55 ;
56
57 type:
58 INT { $$ = T_INT; }
59 | BOOL { $$ = T_BOOL; }
60 | IP { $$ = T_IP; }
61 | PREFIX { $$ = T_PREFIX; }
62 | PAIR { $$ = T_PAIR; }
63 | STRING { $$ = T_STRING; }
64 | type SET {
65 switch ($1) {
66 default:
67 cf_error( "You can not create sets of this type\n" );
68 case T_INT: case T_IP: case T_PREFIX: case T_PAIR:
69 }
70 $$ = $1 | T_SET;
71 }
72 ;
73
74 one_decl:
75 type SYM {
76 cf_define_symbol($2, SYM_VARIABLE | $1, NULL);
77 printf( "New variable %s type %x\n", $2->name, $1 );
78 $2->aux = NULL;
79 {
80 struct f_val * val;
81 val = cfg_alloc(sizeof(struct f_val));
82 val->type = $1;
83 $2->aux2 = val;
84 }
85 $$=$2;
86 }
87 ;
88
89 /* Decls with ';' at the end */
90 decls: /* EMPTY */ { $$ = NULL; }
91 | one_decl ';' decls {
92 $$ = $1;
93 $$->aux = $3;
94 }
95 ;
96
97 /* Declarations that have no ';' at the end.
98 Ouch, this is responsible for 13 or so shift/reduce conflicts. */
99 declsn: one_decl { $$ = $1; }
100 | declsn ';' one_decl {
101 $$ = $3;
102 $$->aux = $1;
103 }
104 ;
105
106
107 filter_body:
108 function_body {
109 struct filter *f = cfg_alloc(sizeof(struct filter));
110 f->name = NULL;
111 f->root = $1;
112 $$ = f;
113 }
114 ;
115
116 filter:
117 SYM {
118 if ($1->class != SYM_FILTER) cf_error("No such filter");
119 $$ = $1->def;
120 }
121 | filter_body
122 ;
123
124 function_params:
125 '(' declsn ')' { printf( "Have function parameters\n" ); $$=$2; }
126 | '(' ')' { $$=NULL; }
127 ;
128
129 function_body:
130 decls '{' cmds '}' {
131 $$ = $3;
132 }
133 ;
134
135 CF_ADDTO(conf, function_def)
136 function_def:
137 FUNCTION SYM function_params function_body {
138 extern struct f_inst *startup_func;
139 cf_define_symbol($2, SYM_FUNCTION, $4);
140 if (!strcasecmp($2->name, "startup"))
141 startup_func = $4;
142 $2->aux = $3;
143 $2->aux2 = $4;
144 printf("Hmm, we've got one function here - %s\n", $2->name);
145 }
146 ;
147
148 /* Programs */
149
150 cmds: /* EMPTY */ { $$ = NULL; }
151 | cmd cmds {
152 if ($1) {
153 if ($1->next)
154 bug("Command has next already set\n");
155 $1->next = $2;
156 $$ = $1;
157 } else $$ = $2;
158 }
159 ;
160
161 block:
162 cmd {
163 $$=$1;
164 }
165 | '{' cmds '}' {
166 $$=$2;
167 }
168 ;
169
170 /*
171 * Simple types, their bison value is int
172 */
173 pair:
174 '(' NUM ',' NUM ')' { $$ = $2 << 16 | $4; }
175 ;
176
177 /*
178 * Complex types, their bison value is struct f_val
179 */
180 prefix_s:
181 IPA '/' NUM { $$.type = T_PREFIX; $$.val.px.ip = $1; $$.val.px.len = $3; printf( "ook, we have prefix here\n" ); }
182 ;
183
184 prefix:
185 prefix_s { $$ = $1; }
186 | prefix_s '+' { $$ = $1; $$.val.px.len |= LEN_PLUS; }
187 | prefix_s '-' { $$ = $1; $$.val.px.len |= LEN_MINUS; }
188 | prefix_s '{' NUM ',' NUM '}' { $$ = $1; $$.val.px.len |= LEN_RANGE | ($3 << 16) | ($5 << 8); }
189 ;
190
191 ipa:
192 IPA { $$.type = T_IP; $$.val.px.ip = $1; }
193 ;
194
195 set_atom:
196 NUM { $$.type = T_INT; $$.val.i = $1; }
197 | pair { $$.type = T_PAIR; $$.val.i = $1; }
198 | ipa { $$ = $1; }
199 | prefix { $$ = $1; }
200 ;
201
202 set_item:
203 set_atom { $$ = f_new_tree(); $$->from = $$->to = $1 }
204 | set_atom '.' '.' set_atom { $$ = f_new_tree(); $$->from = $1; $$->to = $4; }
205 ;
206
207 set_items:
208 set_item { $$ = $1; }
209 | set_items ',' set_item { $$ = $3; $$->left = $1; }
210 ;
211
212 /* 2 shift/reduce conflicts here. Curable by replacing cmds with block which breaks syntax */
213 switch_body: /* EMPTY */ { $$ = NULL; }
214 | set_item ':' cmds switch_body {
215 $$ = $1;
216 $$->data = $3;
217 $$->left = $4;
218 }
219 | ELSE ':' cmds {
220 $$ = f_new_tree();
221 $$->from.type = T_VOID;
222 $$->to.type = T_VOID;
223 $$->data = $3;
224 }
225 ;
226
227 constant:
228 CONST '(' expr ')' { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_INT; $$->a2.i = $3; }
229 | NUM { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_INT; $$->a2.i = $1; }
230 | TRUE { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_BOOL; $$->a2.i = 1; }
231 | FALSE { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_BOOL; $$->a2.i = 0; }
232 | TEXT { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_STRING; $$->a2.p = $1; }
233 | pair { $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_PAIR; $$->a2.i = $1; }
234 | ipa { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
235 /* Replace with prefix_s to get rid of shift/reduce conflicts. */
236 | prefix {NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
237 | '[' set_items ']' { printf( "We've got a set here..." ); $$ = f_new_inst(); $$->code = 'c'; $$->a1.i = T_SET; $$->a2.p = build_tree($2); printf( "ook\n" ); }
238 ;
239
240 term:
241 term '+' term { $$ = f_new_inst(); $$->code = '+'; $$->a1.p = $1; $$->a2.p = $3; }
242 | term '=' term { $$ = f_new_inst(); $$->code = '=='; $$->a1.p = $1; $$->a2.p = $3; }
243 | term '!' '=' term { $$ = f_new_inst(); $$->code = '!='; $$->a1.p = $1; $$->a2.p = $4; }
244 | term '<' term { $$ = f_new_inst(); $$->code = '<'; $$->a1.p = $1; $$->a2.p = $3; }
245 | term '<' '=' term { $$ = f_new_inst(); $$->code = '<='; $$->a1.p = $1; $$->a2.p = $4; }
246 | term '>' term { $$ = f_new_inst(); $$->code = '<'; $$->a1.p = $3; $$->a2.p = $1; }
247 | term '>' '=' term { $$ = f_new_inst(); $$->code = '<='; $$->a1.p = $4; $$->a2.p = $1; }
248 | term '~' term { $$ = f_new_inst(); $$->code = '~'; $$->a1.p = $1; $$->a2.p = $3; }
249
250 | SYM {
251 $$ = f_new_inst();
252 switch ($1->class) {
253 case SYM_VARIABLE | T_INT:
254 case SYM_VARIABLE | T_PAIR:
255 case SYM_VARIABLE | T_PREFIX:
256 case SYM_VARIABLE | T_IP:
257 $$->code = 'C';
258 $$->a1.p = $1->aux2;
259 break;
260 default:
261 cf_error("Can not use this class of symbol as variable." );
262 }
263 }
264 | constant { $$ = $1; }
265 | RTA '.' FROM { $$ = f_new_inst(); $$->code = 'a'; $$->a1.i = T_IP; $$->a2.i = OFFSETOF(struct rta, from); }
266
267 | RTA '.' GW { $$ = f_new_inst(); $$->code = 'a'; $$->a1.i = T_IP; $$->a2.i = OFFSETOF(struct rta, gw); }
268 | RTA '.' NET { $$ = f_new_inst(); $$->code = 'a'; $$->a1.i = T_PREFIX; $$->a2.i = 0x12345678; }
269
270 | RTA '.' RIP_METRIC { $$ = f_new_inst(); $$->code = 'ea'; $$->a1.i = T_INT; $$->a2.i = EA_RIP_METRIC; }
271
272 | term '.' IP { $$ = f_new_inst(); $$->code = 'cp'; $$->a1.p = $1; $$->a2.i = T_IP; }
273 | term '.' LEN { $$ = f_new_inst(); $$->code = 'cp'; $$->a1.p = $1; $$->a2.i = T_INT; }
274 | term '.' MASK '(' term ')' { $$ = f_new_inst(); $$->code = 'iM'; $$->a1.p = $1; $$->a2.p = $5; }
275 ;
276
277 break_command:
278 QUITBIRD { $$ = F_QUITBIRD }
279 | ACCEPT { $$ = F_ACCEPT }
280 | REJECT { $$ = F_REJECT }
281 | ERROR { $$ = F_ERROR }
282 | PRINT { $$ = F_NOP }
283 | PRINTN { $$ = F_NONL }
284 ;
285
286 ifthen:
287 IF term THEN block {
288 $$ = f_new_inst();
289 $$->code = '?';
290 $$->a1.p = $2;
291 $$->a2.p = $4;
292 }
293 ;
294
295 print_one:
296 term { $$ = f_new_inst(); $$->code = 'p'; $$->a1.p = $1; $$->a2.p = NULL; }
297 ;
298
299 print_list: /* EMPTY */ { $$ = NULL; }
300 | print_one print_list {
301 if ($1) {
302 $1->next = $2;
303 $$ = $1;
304 } else $$ = $2;
305 }
306 ;
307
308 var_listn: term {
309 $$ = f_new_inst();
310 $$->code = 's';
311 $$->a1.p = NULL;
312 $$->a2.p = $1;
313 $$->next = NULL;
314 }
315 | term ',' var_listn {
316 $$ = f_new_inst();
317 $$->code = 's';
318 $$->a1.p = NULL;
319 $$->a2.p = $1;
320 $$->next = $3;
321 }
322 ;
323
324 var_list: /* EMPTY */ { $$ = NULL; }
325 | var_listn { $$ = $1; }
326 ;
327
328 cmd:
329 ifthen {
330 $$ = $1;
331 }
332 /* FIXME: this leads to shift/reduce conflict. */
333 | ifthen ELSE block {
334 $$ = f_new_inst();
335 $$->code = '?';
336 $$->a1.p = $1;
337 $$->a2.p = $3;
338 }
339 | SYM '=' term ';' {
340 $$ = f_new_inst();
341 printf( "Ook, we'll set value\n" );
342 if (($1->class & ~T_MASK) != SYM_VARIABLE)
343 cf_error( "You may only set variables, and this is %x.\n", $1->class );
344 $$->code = 's';
345 $$->a1.p = $1;
346 $$->a2.p = $3;
347 }
348 | break_command print_list ';' { $$ = f_new_inst(); $$->code = 'p,'; $$->a1.p = $2; $$->a2.i = $1; }
349 | SYM '(' var_list ')' ';' {
350 struct symbol *sym;
351 struct f_inst *inst = $3;
352 if ($1->class != SYM_FUNCTION)
353 cf_error("You can not call something which is not function. Really.");
354 printf("You are calling function %s\n", $1->name);
355 $$ = f_new_inst();
356 $$->code = 'ca';
357 $$->a1.p = inst;
358 $$->a2.p = $1->aux2;
359 sym = $1->aux;
360 while (sym || inst) {
361 if (!sym || !inst)
362 cf_error("wrong number of arguments for function %s.", $1->name);
363 printf( "You should pass parameter called %s\n", sym->name);
364 inst->a1.p = sym;
365 sym = sym->aux;
366 inst = inst->next;
367 }
368 }
369 | CASE term '{' switch_body '}' {
370 $$ = f_new_inst();
371 $$->code = 'SW';
372 $$->a1.p = $2;
373 $$->a2.p = build_tree( $4 );
374 }
375 ;
376
377 CF_END