4 * Copyright 1998,1999 Pavel Machek
6 * Can be freely distributed and used under the terms of the GNU GPL.
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
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"
28 CF_KEYWORDS(FUNCTION, PRINT, PRINTN, CONST,
29 ACCEPT, REJECT, ERROR, QUITBIRD,
30 INT, BOOL, IP, PREFIX, PAIR, SET, STRING,
33 RTA, FROM, GW, NET, MASK, RIP_METRIC, RIP_TAG,
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
48 CF_ADDTO(conf, filter_def)
50 FILTER SYM filter_body {
51 cf_define_symbol($2, SYM_FILTER, $3);
53 printf( "We have new filter defined (%s)\n", $2->name )
59 | BOOL { $$ = T_BOOL; }
61 | PREFIX { $$ = T_PREFIX; }
62 | PAIR { $$ = T_PAIR; }
63 | STRING { $$ = T_STRING; }
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:
76 cf_define_symbol($2, SYM_VARIABLE | $1, NULL);
77 printf( "New variable %s type %x\n", $2->name, $1 );
81 val = cfg_alloc(sizeof(struct f_val));
89 /* Decls with ';' at the end */
90 decls: /* EMPTY */ { $$ = NULL; }
91 | one_decl ';' decls {
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 {
109 struct filter *f = cfg_alloc(sizeof(struct filter));
118 if ($1->class != SYM_FILTER) cf_error("No such filter");
125 '(' declsn ')' { printf( "Have function parameters\n" ); $$=$2; }
126 | '(' ')' { $$=NULL; }
135 CF_ADDTO(conf, 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"))
144 printf("Hmm, we've got one function here - %s\n", $2->name);
150 cmds: /* EMPTY */ { $$ = NULL; }
154 bug("Command has next already set\n");
171 * Simple types, their bison value is int
174 '(' NUM ',' NUM ')' { $$ = $2 << 16 | $4; }
178 * Complex types, their bison value is struct f_val
181 IPA '/' NUM { $$.type = T_PREFIX; $$.val.px.ip = $1; $$.val.px.len = $3; printf( "ook, we have prefix here\n" ); }
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); }
192 IPA { $$.type = T_IP; $$.val.px.ip = $1; }
196 NUM { $$.type = T_INT; $$.val.i = $1; }
197 | pair { $$.type = T_PAIR; $$.val.i = $1; }
199 | prefix { $$ = $1; }
203 set_atom { $$ = f_new_tree(); $$->from = $$->to = $1 }
204 | set_atom '.' '.' set_atom { $$ = f_new_tree(); $$->from = $1; $$->to = $4; }
208 set_item { $$ = $1; }
209 | set_items ',' set_item { $$ = $3; $$->left = $1; }
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 {
221 $$->from.type = T_VOID;
222 $$->to.type = T_VOID;
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" ); }
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; }
253 case SYM_VARIABLE | T_INT:
254 case SYM_VARIABLE | T_PAIR:
255 case SYM_VARIABLE | T_PREFIX:
256 case SYM_VARIABLE | T_IP:
261 cf_error("Can not use this class of symbol as variable." );
264 | constant { $$ = $1; }
265 | RTA '.' FROM { $$ = f_new_inst(); $$->code = 'a'; $$->a1.i = T_IP; $$->a2.i = OFFSETOF(struct rta, from); }
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; }
270 | RTA '.' RIP_METRIC { $$ = f_new_inst(); $$->code = 'ea'; $$->a1.i = T_INT; $$->a2.i = EA_RIP_METRIC; }
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; }
278 QUITBIRD { $$ = F_QUITBIRD }
279 | ACCEPT { $$ = F_ACCEPT }
280 | REJECT { $$ = F_REJECT }
281 | ERROR { $$ = F_ERROR }
282 | PRINT { $$ = F_NOP }
283 | PRINTN { $$ = F_NONL }
296 term { $$ = f_new_inst(); $$->code = 'p'; $$->a1.p = $1; $$->a2.p = NULL; }
299 print_list: /* EMPTY */ { $$ = NULL; }
300 | print_one print_list {
315 | term ',' var_listn {
324 var_list: /* EMPTY */ { $$ = NULL; }
325 | var_listn { $$ = $1; }
332 /* FIXME: this leads to shift/reduce conflict. */
333 | ifthen ELSE block {
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 );
348 | break_command print_list ';' { $$ = f_new_inst(); $$->code = 'p,'; $$->a1.p = $2; $$->a2.i = $1; }
349 | SYM '(' var_list ')' ';' {
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);
360 while (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);
369 | CASE term '{' switch_body '}' {
373 $$->a2.p = build_tree( $4 );