Statistics
| Branch: | Revision:

iof-bird-daemon / filter / config.Y @ b8cc390e

History | View | Annotate | Download (20.7 KB)

1
/*
2
 *	BIRD - filters
3
 *
4
 *	Copyright 1998--2000 Pavel Machek
5
 *
6
 *	Can be freely distributed and used under the terms of the GNU GPL.
7
 *
8
	FIXME: priority of ! should be lower
9
 */
10

    
11
CF_HDR
12

    
13
CF_DEFINES
14

    
15
#define P(a,b) ((a << 8) | b)
16

    
17
static inline u32 pair(u32 a, u32 b) { return (a << 16) | b; }
18
static inline u32 pair_a(u32 p) { return p >> 16; }
19
static inline u32 pair_b(u32 p) { return p & 0xFFFF; }
20

    
21

    
22
/*
23
 * Sets and their items are during parsing handled as lists, linked
24
 * through left ptr. The first item in a list also contains a pointer
25
 * to the last item in a list (right ptr). For convenience, even items
26
 * are handled as one-item lists. Lists are merged by f_merge_items().
27
 */
28

    
29
static inline struct f_tree *
30
f_new_item(struct f_val from, struct f_val to)
31
{
32
  struct f_tree *t = f_new_tree();
33
  t->right = t;
34
  t->from = from;
35
  t->to = to;
36
  return t;
37
}
38

    
39
static inline struct f_tree *
40
f_merge_items(struct f_tree *a, struct f_tree *b)
41
{
42
  if (!a) return b;
43
  a->right->left = b;
44
  a->right = b->right;
45
  b->right = NULL;
46
  return a;
47
}
48

    
49
static inline struct f_tree *
50
f_new_pair_item(int fa, int ta, int fb, int tb)
51
{
52
  struct f_tree *t = f_new_tree();
53
  t->right = t;
54
  t->from.type = t->to.type = T_PAIR;
55
  t->from.val.i = pair(fa, fb);
56
  t->to.val.i = pair(ta, tb);
57
  return t;
58
}
59

    
60
static inline struct f_tree *
61
f_new_pair_set(int fa, int ta, int fb, int tb)
62
{
63
  struct f_tree *lst = NULL;
64
  int i;
65

    
66
  if ((fa == ta) || ((fb == 0) && (tb == 0xFFFF)))
67
    return f_new_pair_item(fa, ta, fb, tb);
68
  
69
  if ((ta < fa) || (tb < fb))
70
    cf_error( "From value cannot be higher that To value in pair sets");
71

    
72
  for (i = fa; i <= ta; i++)
73
    lst = f_merge_items(lst, f_new_pair_item(i, i, fb, tb));
74

    
75
  return lst;
76
}
77

    
78
CF_DECLS
79

    
80
CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN,
81
	ACCEPT, REJECT, ERROR, QUITBIRD,
82
	INT, BOOL, IP, PREFIX, PAIR, QUAD, SET, STRING, BGPMASK, BGPPATH, CLIST,
83
	IF, THEN, ELSE, CASE,
84
	TRUE, FALSE,
85
	FROM, GW, NET, MASK, PROTO, SOURCE, SCOPE, CAST, DEST, PREFERENCE,
86
	LEN,
87
	DEFINED,
88
	ADD, DELETE, CONTAINS, RESET,
89
	PREPEND, FIRST, LAST, MATCH,
90
	EMPTY,
91
	FILTER, WHERE, EVAL)
92

    
93
%nonassoc THEN
94
%nonassoc ELSE
95

    
96
%type <x> term block cmds cmds_int cmd function_body constant print_one print_list var_list var_listn dynamic_attr static_attr function_call symbol dpair bgp_path_expr
97
%type <f> filter filter_body where_filter
98
%type <i> type break_command pair_expr
99
%type <i32> pair_atom
100
%type <e> pair_item set_item switch_item set_items switch_items switch_body
101
%type <trie> fprefix_set
102
%type <v> set_atom switch_atom fprefix fprefix_s fipa
103
%type <s> decls declsn one_decl function_params 
104
%type <h> bgp_path bgp_path_tail1 bgp_path_tail2
105

    
106
CF_GRAMMAR
107

    
108
CF_ADDTO(conf, filter_def)
109
filter_def:
110
   FILTER SYM { $2 = cf_define_symbol($2, SYM_FILTER, NULL); cf_push_scope( $2 ); }
111
     filter_body {
112
     $2->def = $4;
113
     $4->name = $2->name;
114
     DBG( "We have new filter defined (%s)\n", $2->name );
115
     cf_pop_scope();
116
   }
117
 ;
118

    
119
CF_ADDTO(conf, filter_eval)
120
filter_eval:
121
   EVAL term { f_eval_int($2); }
122
 ;
123

    
124
type:
125
   INT { $$ = T_INT; }
126
 | BOOL { $$ = T_BOOL; }
127
 | IP { $$ = T_IP; }
128
 | PREFIX { $$ = T_PREFIX; }
129
 | PAIR { $$ = T_PAIR; }
130
 | QUAD { $$ = T_QUAD; }
131
 | STRING { $$ = T_STRING; }
132
 | BGPMASK { $$ = T_PATH_MASK; }
133
 | BGPPATH { $$ = T_PATH; }
134
 | CLIST { $$ = T_CLIST; }
135
 | type SET { 
136
	switch ($1) {
137
	  case T_INT:
138
	  case T_PAIR:
139
	  case T_QUAD:
140
	  case T_IP:
141
	       $$ = T_SET;
142
	       break;
143

    
144
	  case T_PREFIX:
145
	       $$ = T_PREFIX_SET;
146
	    break;
147

    
148
	  default:
149
		cf_error( "You can't create sets of this type." );
150
	}
151
   }
152
 ;
153

    
154
one_decl:
155
   type SYM {
156
     struct f_val * val = cfg_alloc(sizeof(struct f_val)); 
157
     val->type = $1; 
158
     $2 = cf_define_symbol($2, SYM_VARIABLE | $1, val);
159
     DBG( "New variable %s type %x\n", $2->name, $1 );
160
     $2->aux2 = NULL;
161
     $$=$2;
162
   }
163
 ;
164

    
165
/* Decls with ';' at the end */
166
decls: /* EMPTY */ { $$ = NULL; }
167
 | one_decl ';' decls {
168
     $$ = $1;
169
     $$->aux2 = $3;
170
   }
171
 ;
172

    
173
/* Declarations that have no ';' at the end. */
174
declsn: one_decl { $$ = $1; }
175
 | declsn ';' one_decl {
176
     $$ = $1;
177
     $$->aux2 = $3;
178
   }
179
 ;
180

    
181
filter_body:
182
   function_body {
183
     struct filter *f = cfg_alloc(sizeof(struct filter));
184
     f->name = NULL;
185
     f->root = $1;
186
     $$ = f;
187
   }
188
 ;
189

    
190
filter:
191
   SYM {
192
     if ($1->class != SYM_FILTER) cf_error("No such filter.");
193
     $$ = $1->def;
194
   }
195
 | filter_body
196
 ;
197

    
198
where_filter:
199
   WHERE term {
200
     /* Construct 'IF term THEN ACCEPT; REJECT;' */
201
     struct filter *f = cfg_alloc(sizeof(struct filter));
202
     struct f_inst *i, *acc, *rej;
203
     acc = f_new_inst();		/* ACCEPT */
204
     acc->code = P('p',',');
205
     acc->a1.p = NULL;
206
     acc->a2.i = F_ACCEPT;
207
     rej = f_new_inst();		/* REJECT */
208
     rej->code = P('p',',');
209
     rej->a1.p = NULL;
210
     rej->a2.i = F_REJECT;
211
     i = f_new_inst();			/* IF */
212
     i->code = '?';
213
     i->a1.p = $2;
214
     i->a2.p = acc;
215
     i->next = rej;
216
     f->name = NULL;
217
     f->root = i;
218
     $$ = f;
219
  }
220
 ;
221

    
222
function_params:
223
   '(' declsn ')' { DBG( "Have function parameters\n" ); $$=$2; }
224
 | '(' ')' { $$=NULL; }
225
 ;
226

    
227
function_body:
228
   decls '{' cmds '}' {
229
     if ($1) {
230
       /* Prepend instruction to clear local variables */
231
       $$ = f_new_inst();
232
       $$->code = P('c','v');
233
       $$->a1.p = $1;
234
       $$->next = $3;
235
     } else
236
       $$ = $3;
237
   }
238
 ;
239

    
240
CF_ADDTO(conf, function_def)
241
function_def:
242
   FUNCTION SYM { DBG( "Beginning of function %s\n", $2->name );
243
     $2 = cf_define_symbol($2, SYM_FUNCTION, NULL);
244
     cf_push_scope($2);
245
   } function_params function_body {
246
     $2->def = $5;
247
     $2->aux2 = $4;
248
     DBG("Hmm, we've got one function here - %s\n", $2->name); 
249
     cf_pop_scope();
250
   }
251
 ;
252

    
253
/* Programs */
254

    
255
/* Hack: $$ of cmds_int is the last node.
256
   $$->next of cmds_int is temporary used for the first node */
257

    
258
cmds: /* EMPTY */ { $$ = NULL; }
259
 | cmds_int { $$ = $1->next; $1->next = NULL; }
260
 ;
261

    
262
cmds_int: cmd { $$ = $1; $1->next = $1; }
263
 | cmds_int cmd { $$ = $2; $2->next = $1->next ; $1->next = $2; }
264
 ;
265

    
266
block:
267
   cmd {
268
     $$=$1;
269
   }
270
 | '{' cmds '}' {
271
     $$=$2;
272
   }
273
 ;
274

    
275
/*
276
 * Complex types, their bison value is struct f_val
277
 */
278
fipa:
279
   IPA %prec PREFIX_DUMMY { $$.type = T_IP; $$.val.px.ip = $1; }
280
 ;
281

    
282

    
283

    
284
/*
285
 * Set constants. They are also used in switch cases. We use separate
286
 * nonterminals for switch (set_atom/switch_atom, set_item/switch_item ...)
287
 * to elude a collision between symbol (in expr) in set_atom and symbol
288
 * as a function call in switch case cmds.
289
 */
290

    
291
set_atom:
292
   expr  { $$.type = T_INT; $$.val.i = $1; }
293
 | RTRID { $$.type = T_QUAD; $$.val.i = $1; }
294
 | fipa  { $$ = $1; }
295
 | ENUM  { $$.type = pair_a($1); $$.val.i = pair_b($1); }
296
 ;
297

    
298
switch_atom:
299
   NUM   { $$.type = T_INT; $$.val.i = $1; }
300
 | '(' term ')' { $$.type = T_INT; $$.val.i = f_eval_int($2); }
301
 | RTRID { $$.type = T_QUAD; $$.val.i = $1; }
302
 | fipa  { $$ = $1; }
303
 | ENUM  { $$.type = pair_a($1); $$.val.i = pair_b($1); }
304
 ;
305

    
306
pair_expr:
307
   term { $$ = f_eval_int($1); check_u16($$); }
308

    
309
pair_atom:
310
   pair_expr { $$ = pair($1, $1); }
311
 | pair_expr DDOT pair_expr { $$ = pair($1, $3); }
312
 | '*' { $$ = 0xFFFF; }
313
 ;
314

    
315
pair_item:
316
   '(' pair_atom ',' pair_atom ')' {
317
     $$ = f_new_pair_set(pair_a($2), pair_b($2), pair_a($4), pair_b($4));
318
   }
319
 | '(' pair_atom ',' pair_atom ')' DDOT '(' pair_expr ',' pair_expr ')' {
320
     /* Hack: $2 and $4 should be pair_expr, but that would cause shift/reduce conflict */
321
     if ((pair_a($2) != pair_b($2)) || (pair_a($4) != pair_b($4)))
322
       cf_error("syntax error");
323
     $$ = f_new_pair_item(pair_b($2), pair_b($4), $8, $10); 
324
   }
325
 ;
326

    
327
set_item:
328
   pair_item
329
 | set_atom { $$ = f_new_item($1, $1); }
330
 | set_atom DDOT set_atom { $$ = f_new_item($1, $3); }
331
 ;
332

    
333
switch_item:
334
   pair_item
335
 | switch_atom { $$ = f_new_item($1, $1); }
336
 | switch_atom DDOT switch_atom { $$ = f_new_item($1, $3); }
337
 ;
338

    
339
set_items:
340
   set_item
341
 | set_items ',' set_item { $$ = f_merge_items($1, $3); }
342
 ;
343

    
344
switch_items:
345
   switch_item
346
 | switch_items ',' switch_item { $$ = f_merge_items($1, $3); }
347
 ;
348

    
349
fprefix_s:
350
   IPA '/' NUM %prec '/' {
351
     if (($3 < 0) || ($3 > MAX_PREFIX_LENGTH) || !ip_is_prefix($1, $3)) cf_error("Invalid network prefix: %I/%d.", $1, $3);
352
     $$.type = T_PREFIX; $$.val.px.ip = $1; $$.val.px.len = $3;
353
   }
354
 ;
355

    
356
fprefix:
357
   fprefix_s { $$ = $1; }
358
 | fprefix_s '+' { $$ = $1; $$.val.px.len |= LEN_PLUS; }
359
 | fprefix_s '-' { $$ = $1; $$.val.px.len |= LEN_MINUS; }
360
 | fprefix_s '{' NUM ',' NUM '}' { 
361
     if (! ((0 <= $3) && ($3 <= $5) && ($5 <= MAX_PREFIX_LENGTH))) cf_error("Invalid prefix pattern range: {%d, %d}.", $3, $5);
362
     $$ = $1; $$.val.px.len |= LEN_RANGE | ($3 << 16) | ($5 << 8);
363
   }
364
 ;
365

    
366
fprefix_set:
367
   fprefix { $$ = f_new_trie(cfg_mem); trie_add_fprefix($$, &($1.val.px)); }
368
 | fprefix_set ',' fprefix { $$ = $1; trie_add_fprefix($$, &($3.val.px)); }
369
 ;
370

    
371
switch_body: /* EMPTY */ { $$ = NULL; }
372
 | switch_body switch_items ':' cmds  {
373
     /* Fill data fields */
374
     struct f_tree *t;
375
     for (t = $2; t; t = t->left)
376
       t->data = $4;
377
     $$ = f_merge_items($1, $2);
378
   }
379
 | switch_body ELSECOL cmds { 
380
     struct f_tree *t = f_new_tree();
381
     t->from.type = t->to.type = T_VOID;
382
     t->right = t;
383
     t->data = $3;
384
     $$ = f_merge_items($1, t);
385
 }
386
 ;
387

    
388
/* CONST '(' expr ')' { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_INT; $$->a2.i = $3; } */
389

    
390
bgp_path_expr:
391
   symbol       { $$ = $1; }   
392
 | '(' term ')' { $$ = $2; }
393
 ;
394

    
395
bgp_path:
396
   PO  bgp_path_tail1 PC  { $$ = $2; }
397
 | '/' bgp_path_tail2 '/' { $$ = $2; }
398
 ;
399

    
400
bgp_path_tail1:
401
   NUM bgp_path_tail1 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_ASN;      $$->val = $1; }
402
 | '*' bgp_path_tail1 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_ASTERISK; $$->val  = 0; }
403
 | '?' bgp_path_tail1 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_QUESTION; $$->val  = 0; }
404
 | bgp_path_expr bgp_path_tail1 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_ASN_EXPR; $$->val = (uintptr_t) $1; }
405
 |  		      { $$ = NULL; }
406
 ;
407

    
408
bgp_path_tail2:
409
   NUM bgp_path_tail2 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_ASN;      $$->val = $1; }
410
 | '?' bgp_path_tail2 { $$ = cfg_alloc(sizeof(struct f_path_mask)); $$->next = $2; $$->kind = PM_ASTERISK; $$->val  = 0; }
411
 | 		      { $$ = NULL; }
412
 ;
413

    
414
dpair:
415
   '(' term ',' term ')' {
416
        if (($2->code == 'c') && ($4->code == 'c'))
417
          { 
418
            if (($2->aux != T_INT) || ($4->aux != T_INT))
419
              cf_error( "Can't operate with value of non-integer type in pair constructor" );
420
	    check_u16($2->a2.i); check_u16($4->a2.i);
421
            $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_PAIR;  $$->a2.i = pair($2->a2.i, $4->a2.i);
422
          }
423
	else
424
	  { $$ = f_new_inst(); $$->code = P('m', 'p'); $$->a1.p = $2; $$->a2.p = $4; }
425
    }
426
 ;
427

    
428
constant:
429
   NUM    { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_INT;  $$->a2.i = $1; }
430
 | TRUE   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 1;  }
431
 | FALSE  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_BOOL; $$->a2.i = 0;  }
432
 | TEXT   { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_STRING; $$->a2.p = $1; }
433
 | fipa	   { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
434
 | fprefix_s {NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; *val = $1; }
435
 | RTRID  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_QUAD;  $$->a2.i = $1; }
436
 | '[' set_items ']' { DBG( "We've got a set here..." ); $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_SET; $$->a2.p = build_tree($2); DBG( "ook\n" ); }
437
 | '[' fprefix_set ']' { $$ = f_new_inst(); $$->code = 'c'; $$->aux = T_PREFIX_SET;  $$->a2.p = $2; }
438
 | ENUM	  { $$ = f_new_inst(); $$->code = 'c'; $$->aux = $1 >> 16; $$->a2.i = $1 & 0xffff; }
439
 | bgp_path { NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; val->type = T_PATH_MASK; val->val.path_mask = $1; $$->a1.p = val; }
440
 ;
441

    
442

    
443
/*
444
 *  Maybe there are no dynamic attributes defined by protocols.
445
 *  For such cases, we force the dynamic_attr list to contain
446
 *  at least an invalid token, so it is syntantically correct.
447
 */
448
CF_ADDTO(dynamic_attr, INVALID_TOKEN { $$ = NULL; })
449

    
450
rtadot: /* EMPTY, we are not permitted RTA. prefix */
451
 ;
452

    
453
function_call:
454
   SYM '(' var_list ')' {
455
     struct symbol *sym;
456
     struct f_inst *inst = $3;
457
     if ($1->class != SYM_FUNCTION)
458
       cf_error("You can't call something which is not a function. Really.");
459
     DBG("You are calling function %s\n", $1->name);
460
     $$ = f_new_inst();
461
     $$->code = P('c','a');
462
     $$->a1.p = inst;
463
     $$->a2.p = $1->def;
464
     sym = $1->aux2;
465
     while (sym || inst) {
466
       if (!sym || !inst)
467
	 cf_error("Wrong number of arguments for function %s.", $1->name);
468
       DBG( "You should pass parameter called %s\n", sym->name);
469
       inst->a1.p = sym;
470
       sym = sym->aux2;
471
       inst = inst->next;
472
     }
473
   }
474
 ;
475

    
476
symbol:
477
   SYM {
478
     $$ = f_new_inst();
479
     switch ($1->class) {
480
       case SYM_NUMBER:
481
	$$ = f_new_inst();
482
	$$->code = 'c'; 
483
	$$->aux = T_INT; 
484
	$$->a2.i = $1->aux;
485
	break;
486
       case SYM_IPA:
487
	{ NEW_F_VAL; $$ = f_new_inst(); $$->code = 'C'; $$->a1.p = val; val->type = T_IP; val->val.px.ip = * (ip_addr *) ($1->def); }
488
	break;
489
       case SYM_VARIABLE | T_BOOL:
490
       case SYM_VARIABLE | T_INT:
491
       case SYM_VARIABLE | T_PAIR:
492
       case SYM_VARIABLE | T_QUAD:
493
       case SYM_VARIABLE | T_STRING:
494
       case SYM_VARIABLE | T_IP:
495
       case SYM_VARIABLE | T_PREFIX:
496
       case SYM_VARIABLE | T_PREFIX_SET:
497
       case SYM_VARIABLE | T_SET:
498
       case SYM_VARIABLE | T_PATH:
499
       case SYM_VARIABLE | T_PATH_MASK:
500
       case SYM_VARIABLE | T_CLIST:
501
	 $$->code = 'V';
502
	 $$->a1.p = $1->def;
503
	 $$->a2.p = $1->name;
504
	 break;
505
       default:
506
	 cf_error("%s: variable expected.", $1->name );
507
     }
508
   }
509

    
510
static_attr:
511
   FROM    { $$ = f_new_inst(); $$->aux = T_IP;         $$->a2.i = OFFSETOF(struct rta, from);   $$->a1.i = 1; }
512

    
513
 | GW      { $$ = f_new_inst(); $$->aux = T_IP;         $$->a2.i = OFFSETOF(struct rta, gw);     $$->a1.i = 1; }
514
 | NET     { $$ = f_new_inst(); $$->aux = T_PREFIX;     $$->a2.i = 0x12345678; /* This is actually ok - T_PREFIX is special-cased. */ }
515
 | PROTO   { $$ = f_new_inst(); $$->aux = T_STRING;     $$->a2.i = 0x12345678; /* T_STRING is also special-cased. */ }
516
 | SOURCE  { $$ = f_new_inst(); $$->aux = T_ENUM_RTS;   $$->a2.i = OFFSETOF(struct rta, source); }
517
 | SCOPE   { $$ = f_new_inst(); $$->aux = T_ENUM_SCOPE; $$->a2.i = OFFSETOF(struct rta, scope);  $$->a1.i = 1; }
518
 | CAST    { $$ = f_new_inst(); $$->aux = T_ENUM_RTC;   $$->a2.i = OFFSETOF(struct rta, cast); }
519
 | DEST    { $$ = f_new_inst(); $$->aux = T_ENUM_RTD;   $$->a2.i = OFFSETOF(struct rta, dest); }
520
 ;
521

    
522
term:
523
   '(' term ')'      { $$ = $2; }
524
 | term '+' term     { $$ = f_new_inst(); $$->code = '+';        $$->a1.p = $1; $$->a2.p = $3; }
525
 | term '-' term     { $$ = f_new_inst(); $$->code = '-';        $$->a1.p = $1; $$->a2.p = $3; }
526
 | term '*' term     { $$ = f_new_inst(); $$->code = '*';        $$->a1.p = $1; $$->a2.p = $3; }
527
 | term '/' term     { $$ = f_new_inst(); $$->code = '/';        $$->a1.p = $1; $$->a2.p = $3; }
528
 | term AND term     { $$ = f_new_inst(); $$->code = '&';        $$->a1.p = $1; $$->a2.p = $3; }
529
 | term OR  term     { $$ = f_new_inst(); $$->code = '|';        $$->a1.p = $1; $$->a2.p = $3; }
530
 | term '=' term     { $$ = f_new_inst(); $$->code = P('=','='); $$->a1.p = $1; $$->a2.p = $3; }
531
 | term NEQ term { $$ = f_new_inst(); $$->code = P('!','=');     $$->a1.p = $1; $$->a2.p = $3; }
532
 | term '<' term     { $$ = f_new_inst(); $$->code = '<';        $$->a1.p = $1; $$->a2.p = $3; }
533
 | term LEQ term { $$ = f_new_inst(); $$->code = P('<','=');     $$->a1.p = $1; $$->a2.p = $3; }
534
 | term '>' term     { $$ = f_new_inst(); $$->code = '<';        $$->a1.p = $3; $$->a2.p = $1; }
535
 | term GEQ term { $$ = f_new_inst(); $$->code = P('<','=');     $$->a1.p = $3; $$->a2.p = $1; }
536
 | term '~' term     { $$ = f_new_inst(); $$->code = '~';        $$->a1.p = $1; $$->a2.p = $3; }
537
 | '!' term { $$ = f_new_inst(); $$->code = '!'; $$->a1.p = $2; }
538
 | DEFINED '(' term ')' { $$ = f_new_inst(); $$->code = P('d','e');  $$->a1.p = $3; }
539

    
540
 | symbol   { $$ = $1; }
541
 | constant { $$ = $1; }
542
 | dpair    { $$ = $1; }
543

    
544
 | PREFERENCE { $$ = f_new_inst(); $$->code = 'P'; }
545

    
546
 | rtadot static_attr { $$ = $2; $$->code = 'a'; }
547

    
548
 | rtadot dynamic_attr { $$ = $2; $$->code = P('e','a'); }
549

    
550
 | term '.' IP { $$ = f_new_inst(); $$->code = P('c','p'); $$->a1.p = $1; $$->aux = T_IP; }
551
 | term '.' LEN { $$ = f_new_inst(); $$->code = 'L'; $$->a1.p = $1; }
552
 | term '.' MASK '(' term ')' { $$ = f_new_inst(); $$->code = P('i','M'); $$->a1.p = $1; $$->a2.p = $5; }
553
 | term '.' FIRST { $$ = f_new_inst(); $$->code = P('a','f'); $$->a1.p = $1; }
554
 | term '.' LAST  { $$ = f_new_inst(); $$->code = P('a','l'); $$->a1.p = $1; }
555

    
556
/* Communities */
557
/* This causes one shift/reduce conflict
558
 | rtadot dynamic_attr '.' ADD '(' term ')' { }
559
 | rtadot dynamic_attr '.' DELETE '(' term ')' { }
560
 | rtadot dynamic_attr '.' CONTAINS '(' term ')' { }
561
 | rtadot dynamic_attr '.' RESET{ }
562
*/
563

    
564
 | '+' EMPTY '+' { $$ = f_new_inst(); $$->code = 'E'; $$->aux = T_PATH; }
565
 | '-' EMPTY '-' { $$ = f_new_inst(); $$->code = 'E'; $$->aux = T_CLIST; }
566
 | PREPEND '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('A','p'); $$->a1.p = $3; $$->a2.p = $5; } 
567
 | ADD '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('C','a'); $$->a1.p = $3; $$->a2.p = $5; $$->aux = 'a'; } 
568
 | DELETE '(' term ',' term ')' { $$ = f_new_inst(); $$->code = P('C','a'); $$->a1.p = $3; $$->a2.p = $5; $$->aux = 'd'; }
569

    
570
/* | term '.' LEN { $$->code = P('P','l'); } */
571

    
572
/* function_call is inlined here */
573
 | SYM '(' var_list ')' {
574
     struct symbol *sym;
575
     struct f_inst *inst = $3;
576
     if ($1->class != SYM_FUNCTION)
577
       cf_error("You can't call something which is not a function. Really.");
578
     DBG("You are calling function %s\n", $1->name);
579
     $$ = f_new_inst();
580
     $$->code = P('c','a');
581
     $$->a1.p = inst;
582
     $$->a2.p = $1->def;
583
     sym = $1->aux2;
584
     while (sym || inst) {
585
       if (!sym || !inst)
586
	 cf_error("Wrong number of arguments for function %s.", $1->name);
587
       DBG( "You should pass parameter called %s\n", sym->name);
588
       inst->a1.p = sym;
589
       sym = sym->aux2;
590
       inst = inst->next;
591
     }
592
   }
593
 ;
594

    
595
break_command:
596
   QUITBIRD { $$ = F_QUITBIRD; }
597
 | ACCEPT { $$ = F_ACCEPT; }
598
 | REJECT { $$ = F_REJECT; }
599
 | ERROR { $$ = F_ERROR; }
600
 | PRINT { $$ = F_NOP; }
601
 | PRINTN { $$ = F_NONL; }
602
 ;
603

    
604
print_one:
605
   term { $$ = f_new_inst(); $$->code = 'p'; $$->a1.p = $1; $$->a2.p = NULL; }
606
 ;
607

    
608
print_list: /* EMPTY */ { $$ = NULL; }
609
 | print_one { $$ = $1; }
610
 | print_one ',' print_list {
611
     if ($1) {
612
       $1->next = $3;
613
       $$ = $1;
614
     } else $$ = $3;
615
   }
616
 
617
 ;
618

    
619
var_listn: term { 
620
     $$ = f_new_inst();
621
     $$->code = 's';
622
     $$->a1.p = NULL;
623
     $$->a2.p = $1;
624
     $$->next = NULL;
625
   }
626
 | term ',' var_listn {
627
     $$ = f_new_inst();
628
     $$->code = 's';
629
     $$->a1.p = NULL;
630
     $$->a2.p = $1;
631
     $$->next = $3;
632
   }
633
 ;
634

    
635
var_list: /* EMPTY */ { $$ = NULL; }
636
 | var_listn { $$ = $1; }
637
 ;
638

    
639
cmd:
640
   IF term THEN block {
641
     $$ = f_new_inst();
642
     $$->code = '?';
643
     $$->a1.p = $2;
644
     $$->a2.p = $4;
645
   }
646
 | IF term THEN block ELSE block {
647
     struct f_inst *i = f_new_inst();
648
     i->code = '?';
649
     i->a1.p = $2;
650
     i->a2.p = $4;
651
     $$ = f_new_inst();
652
     $$->code = '?';
653
     $$->a1.p = i;
654
     $$->a2.p = $6;
655
   }
656
 | SYM '=' term ';' {
657
     $$ = f_new_inst();
658
     DBG( "Ook, we'll set value\n" );
659
     if (($1->class & ~T_MASK) != SYM_VARIABLE)
660
       cf_error( "You may set only variables." );
661
     $$->code = 's';
662
     $$->a1.p = $1;
663
     $$->a2.p = $3;
664
   }
665
 | RETURN term ';' {
666
     $$ = f_new_inst();
667
     DBG( "Ook, we'll return the value\n" );
668
     $$->code = 'r';
669
     $$->a1.p = $2;
670
   }
671
 | rtadot dynamic_attr '=' term ';' {
672
     $$ = $2;
673
     $$->code = P('e','S');
674
     $$->a1.p = $4;
675
   }
676
 | rtadot static_attr '=' term ';' {
677
     $$ = $2;
678
     if (!$$->a1.i)
679
       cf_error( "This static attribute is read-only.");
680
     $$->code = P('a','S');
681
     $$->a1.p = $4;
682
   }
683
 | PREFERENCE '=' term ';' {
684
     $$ = f_new_inst();
685
     $$->code = P('P','S');
686
     $$->a1.p = $3;
687
   } 
688
 | UNSET '(' rtadot dynamic_attr ')' ';' {
689
     $$ = $4;
690
     $$->aux = EAF_TYPE_UNDEF | EAF_TEMP;
691
     $$->code = P('e','S');
692
     $$->a1.p = NULL;
693
   }
694
 | break_command print_list ';' { $$ = f_new_inst(); $$->code = P('p',','); $$->a1.p = $2; $$->a2.i = $1; }
695
 | function_call ';' { $$ = $1; }
696
 | CASE term '{' switch_body '}' {
697
      $$ = f_new_inst();
698
      $$->code = P('S','W');
699
      $$->a1.p = $2;
700
      $$->a2.p = build_tree( $4 );
701
   }
702

    
703

    
704
 | rtadot dynamic_attr '.' EMPTY ';' 
705
  { struct f_inst *i = f_new_inst(); i->code = 'E'; i->aux = T_CLIST; $$ = $2; $$->code = P('e','S'); $$->a1.p = i; }
706
 | rtadot dynamic_attr '.' PREPEND '(' term ')' ';'   { $$ = f_generate_complex( P('A','p'), 'x', $2, $6 ); }
707
 | rtadot dynamic_attr '.' ADD '(' term ')' ';'       { $$ = f_generate_complex( P('C','a'), 'a', $2, $6 ); } 
708
 | rtadot dynamic_attr '.' DELETE '(' term ')' ';'    { $$ = f_generate_complex( P('C','a'), 'd', $2, $6 ); } 
709
 ;
710

    
711
CF_END