Deleted Added
full compact
grammar.y (26178) grammar.y (56891)
1%{
2/*
3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 *
1%{
2/*
3 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
17 * written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 *
22 * $FreeBSD: head/contrib/libpcap/grammar.y 56891 2000-01-30 00:43:38Z fenner $
22 */
23#ifndef lint
24static const char rcsid[] =
23 */
24#ifndef lint
25static const char rcsid[] =
25 "@(#) $Header: grammar.y,v 1.56 96/11/02 21:54:55 leres Exp $ (LBL)";
26 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.57 1999/10/19 15:18:30 itojun Exp $ (LBL)";
26#endif
27
28#include <sys/types.h>
29#include <sys/time.h>
30#include <sys/socket.h>
31
32#if __STDC__
33struct mbuf;
34struct rtentry;
35#endif
36
37#include <net/if.h>
38
39#include <netinet/in.h>
40#include <net/ethernet.h>
41
42#include <stdio.h>
43
44#include "pcap-int.h"
45
46#include "gencode.h"
47#include <pcap-namedb.h>
48
49#include "gnuc.h"
50#ifdef HAVE_OS_PROTO_H
51#include "os-proto.h"
52#endif
53
54#define QSET(q, p, d, a) (q).proto = (p),\
55 (q).dir = (d),\
56 (q).addr = (a)
57
58int n_errors = 0;
59
60static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
61
62static void
63yyerror(char *msg)
64{
65 ++n_errors;
66 bpf_error("%s", msg);
67 /* NOTREACHED */
68}
69
70#ifndef YYBISON
71int yyparse(void);
72
73int
74pcap_parse()
75{
76 return (yyparse());
77}
78#endif
79
80%}
81
82%union {
83 int i;
84 bpf_u_int32 h;
85 u_char *e;
86 char *s;
87 struct stmt *stmt;
88 struct arth *a;
89 struct {
90 struct qual q;
91 struct block *b;
92 } blk;
93 struct block *rblk;
94}
95
96%type <blk> expr id nid pid term rterm qid
97%type <blk> head
98%type <i> pqual dqual aqual ndaqual
99%type <a> arth narth
100%type <i> byteop pname pnum relop irelop
101%type <blk> and or paren not null prog
102%type <rblk> other
103
104%token DST SRC HOST GATEWAY
27#endif
28
29#include <sys/types.h>
30#include <sys/time.h>
31#include <sys/socket.h>
32
33#if __STDC__
34struct mbuf;
35struct rtentry;
36#endif
37
38#include <net/if.h>
39
40#include <netinet/in.h>
41#include <net/ethernet.h>
42
43#include <stdio.h>
44
45#include "pcap-int.h"
46
47#include "gencode.h"
48#include <pcap-namedb.h>
49
50#include "gnuc.h"
51#ifdef HAVE_OS_PROTO_H
52#include "os-proto.h"
53#endif
54
55#define QSET(q, p, d, a) (q).proto = (p),\
56 (q).dir = (d),\
57 (q).addr = (a)
58
59int n_errors = 0;
60
61static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
62
63static void
64yyerror(char *msg)
65{
66 ++n_errors;
67 bpf_error("%s", msg);
68 /* NOTREACHED */
69}
70
71#ifndef YYBISON
72int yyparse(void);
73
74int
75pcap_parse()
76{
77 return (yyparse());
78}
79#endif
80
81%}
82
83%union {
84 int i;
85 bpf_u_int32 h;
86 u_char *e;
87 char *s;
88 struct stmt *stmt;
89 struct arth *a;
90 struct {
91 struct qual q;
92 struct block *b;
93 } blk;
94 struct block *rblk;
95}
96
97%type <blk> expr id nid pid term rterm qid
98%type <blk> head
99%type <i> pqual dqual aqual ndaqual
100%type <a> arth narth
101%type <i> byteop pname pnum relop irelop
102%type <blk> and or paren not null prog
103%type <rblk> other
104
105%token DST SRC HOST GATEWAY
105%token NET MASK PORT LESS GREATER PROTO BYTE
106%token ARP RARP IP TCP UDP ICMP IGMP IGRP
106%token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
107%token ARP RARP IP TCP UDP ICMP IGMP IGRP PIM
107%token ATALK DECNET LAT SCA MOPRC MOPDL
108%token TK_BROADCAST TK_MULTICAST
109%token NUM INBOUND OUTBOUND
110%token LINK
111%token GEQ LEQ NEQ
108%token ATALK DECNET LAT SCA MOPRC MOPDL
109%token TK_BROADCAST TK_MULTICAST
110%token NUM INBOUND OUTBOUND
111%token LINK
112%token GEQ LEQ NEQ
112%token ID EID HID
113%token ID EID HID HID6
113%token LSH RSH
114%token LEN
115%token ISO ESIS ISIS
114%token LSH RSH
115%token LEN
116%token ISO ESIS ISIS
117%token IPV6 ICMPV6 AH ESP
116
117%type <s> ID
118%type <e> EID
118
119%type <s> ID
120%type <e> EID
119%type HID
121%type <s> HID HID6
120%type <i> NUM
121
122%left OR AND
123%nonassoc '!'
124%left '|'
125%left '&'
126%left LSH RSH
127%left '+' '-'
128%left '*' '/'
129%nonassoc UMINUS
130%%
131prog: null expr
132{
133 finish_parse($2.b);
134}
135 | null
136 ;
137null: /* null */ { $$.q = qerr; }
138 ;
139expr: term
140 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
141 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
142 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
143 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
144 ;
145and: AND { $$ = $<blk>0; }
146 ;
147or: OR { $$ = $<blk>0; }
148 ;
149id: nid
150 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
151 $$.q = $<blk>0.q); }
152 | paren pid ')' { $$ = $2; }
153 ;
154nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
155 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
156 $$.q = $<blk>0.q); }
157 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
158 $$.q = $<blk>0.q); }
159 | HID {
160 /* Decide how to parse HID based on proto */
161 $$.q = $<blk>0.q;
162 switch ($$.q.proto) {
163 case Q_DECNET:
164 $$.b = gen_ncode($1, 0, $$.q);
165 break;
166 default:
167 $$.b = gen_ncode($1, 0, $$.q);
168 break;
169 }
170 }
122%type <i> NUM
123
124%left OR AND
125%nonassoc '!'
126%left '|'
127%left '&'
128%left LSH RSH
129%left '+' '-'
130%left '*' '/'
131%nonassoc UMINUS
132%%
133prog: null expr
134{
135 finish_parse($2.b);
136}
137 | null
138 ;
139null: /* null */ { $$.q = qerr; }
140 ;
141expr: term
142 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
143 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
144 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
145 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
146 ;
147and: AND { $$ = $<blk>0; }
148 ;
149or: OR { $$ = $<blk>0; }
150 ;
151id: nid
152 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
153 $$.q = $<blk>0.q); }
154 | paren pid ')' { $$ = $2; }
155 ;
156nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
157 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
158 $$.q = $<blk>0.q); }
159 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
160 $$.q = $<blk>0.q); }
161 | HID {
162 /* Decide how to parse HID based on proto */
163 $$.q = $<blk>0.q;
164 switch ($$.q.proto) {
165 case Q_DECNET:
166 $$.b = gen_ncode($1, 0, $$.q);
167 break;
168 default:
169 $$.b = gen_ncode($1, 0, $$.q);
170 break;
171 }
172 }
173 | HID6 '/' NUM {
174#ifdef INET6
175 $$.b = gen_mcode6($1, NULL, $3,
176 $$.q = $<blk>0.q);
177#else
178 bpf_error("'ip6addr/prefixlen' not supported "
179 "in this configuration");
180#endif /*INET6*/
181 }
182 | HID6 {
183#ifdef INET6
184 $$.b = gen_mcode6($1, 0, 128,
185 $$.q = $<blk>0.q);
186#else
187 bpf_error("'ip6addr' not supported "
188 "in this configuration");
189#endif /*INET6*/
190 }
171 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
172 | not id { gen_not($2.b); $$ = $2; }
173 ;
174not: '!' { $$ = $<blk>0; }
175 ;
176paren: '(' { $$ = $<blk>0; }
177 ;
178pid: nid
179 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
180 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
181 ;
182qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
183 $$.q = $<blk>0.q); }
184 | pid
185 ;
186term: rterm
187 | not term { gen_not($2.b); $$ = $2; }
188 ;
189head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
190 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
191 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
192 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
191 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
192 | not id { gen_not($2.b); $$ = $2; }
193 ;
194not: '!' { $$ = $<blk>0; }
195 ;
196paren: '(' { $$ = $<blk>0; }
197 ;
198pid: nid
199 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
200 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
201 ;
202qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
203 $$.q = $<blk>0.q); }
204 | pid
205 ;
206term: rterm
207 | not term { gen_not($2.b); $$ = $2; }
208 ;
209head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
210 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
211 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
212 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
213 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
193 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
194 ;
195rterm: head id { $$ = $2; }
196 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
197 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
198 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
199 $$.q = qerr; }
200 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
201 $$.q = qerr; }
202 | other { $$.b = $1; $$.q = qerr; }
203 ;
204/* protocol level qualifiers */
205pqual: pname
206 | { $$ = Q_DEFAULT; }
207 ;
208/* 'direction' qualifiers */
209dqual: SRC { $$ = Q_SRC; }
210 | DST { $$ = Q_DST; }
211 | SRC OR DST { $$ = Q_OR; }
212 | DST OR SRC { $$ = Q_OR; }
213 | SRC AND DST { $$ = Q_AND; }
214 | DST AND SRC { $$ = Q_AND; }
215 ;
216/* address type qualifiers */
217aqual: HOST { $$ = Q_HOST; }
218 | NET { $$ = Q_NET; }
219 | PORT { $$ = Q_PORT; }
220 ;
221/* non-directional address type qualifiers */
222ndaqual: GATEWAY { $$ = Q_GATEWAY; }
223 ;
224pname: LINK { $$ = Q_LINK; }
225 | IP { $$ = Q_IP; }
226 | ARP { $$ = Q_ARP; }
227 | RARP { $$ = Q_RARP; }
228 | TCP { $$ = Q_TCP; }
229 | UDP { $$ = Q_UDP; }
230 | ICMP { $$ = Q_ICMP; }
231 | IGMP { $$ = Q_IGMP; }
232 | IGRP { $$ = Q_IGRP; }
214 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
215 ;
216rterm: head id { $$ = $2; }
217 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
218 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
219 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
220 $$.q = qerr; }
221 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
222 $$.q = qerr; }
223 | other { $$.b = $1; $$.q = qerr; }
224 ;
225/* protocol level qualifiers */
226pqual: pname
227 | { $$ = Q_DEFAULT; }
228 ;
229/* 'direction' qualifiers */
230dqual: SRC { $$ = Q_SRC; }
231 | DST { $$ = Q_DST; }
232 | SRC OR DST { $$ = Q_OR; }
233 | DST OR SRC { $$ = Q_OR; }
234 | SRC AND DST { $$ = Q_AND; }
235 | DST AND SRC { $$ = Q_AND; }
236 ;
237/* address type qualifiers */
238aqual: HOST { $$ = Q_HOST; }
239 | NET { $$ = Q_NET; }
240 | PORT { $$ = Q_PORT; }
241 ;
242/* non-directional address type qualifiers */
243ndaqual: GATEWAY { $$ = Q_GATEWAY; }
244 ;
245pname: LINK { $$ = Q_LINK; }
246 | IP { $$ = Q_IP; }
247 | ARP { $$ = Q_ARP; }
248 | RARP { $$ = Q_RARP; }
249 | TCP { $$ = Q_TCP; }
250 | UDP { $$ = Q_UDP; }
251 | ICMP { $$ = Q_ICMP; }
252 | IGMP { $$ = Q_IGMP; }
253 | IGRP { $$ = Q_IGRP; }
254 | PIM { $$ = Q_PIM; }
233 | ATALK { $$ = Q_ATALK; }
234 | DECNET { $$ = Q_DECNET; }
235 | LAT { $$ = Q_LAT; }
236 | SCA { $$ = Q_SCA; }
237 | MOPDL { $$ = Q_MOPDL; }
238 | MOPRC { $$ = Q_MOPRC; }
255 | ATALK { $$ = Q_ATALK; }
256 | DECNET { $$ = Q_DECNET; }
257 | LAT { $$ = Q_LAT; }
258 | SCA { $$ = Q_SCA; }
259 | MOPDL { $$ = Q_MOPDL; }
260 | MOPRC { $$ = Q_MOPRC; }
261 | IPV6 { $$ = Q_IPV6; }
262 | ICMPV6 { $$ = Q_ICMPV6; }
263 | AH { $$ = Q_AH; }
264 | ESP { $$ = Q_ESP; }
239 | ISO { $$ = Q_ISO; }
240 | ESIS { $$ = Q_ESIS; }
241 | ISIS { $$ = Q_ISIS; }
242 ;
243other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
244 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
245 | LESS NUM { $$ = gen_less($2); }
246 | GREATER NUM { $$ = gen_greater($2); }
247 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
248 | INBOUND { $$ = gen_inbound(0); }
249 | OUTBOUND { $$ = gen_inbound(1); }
250 ;
251relop: '>' { $$ = BPF_JGT; }
252 | GEQ { $$ = BPF_JGE; }
253 | '=' { $$ = BPF_JEQ; }
254 ;
255irelop: LEQ { $$ = BPF_JGT; }
256 | '<' { $$ = BPF_JGE; }
257 | NEQ { $$ = BPF_JEQ; }
258 ;
259arth: pnum { $$ = gen_loadi($1); }
260 | narth
261 ;
262narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
263 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
264 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
265 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
266 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
267 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
268 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
269 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
270 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
271 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
272 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
273 | paren narth ')' { $$ = $2; }
274 | LEN { $$ = gen_loadlen(); }
275 ;
276byteop: '&' { $$ = '&'; }
277 | '|' { $$ = '|'; }
278 | '<' { $$ = '<'; }
279 | '>' { $$ = '>'; }
280 | '=' { $$ = '='; }
281 ;
282pnum: NUM
283 | paren pnum ')' { $$ = $2; }
284 ;
285%%
265 | ISO { $$ = Q_ISO; }
266 | ESIS { $$ = Q_ESIS; }
267 | ISIS { $$ = Q_ISIS; }
268 ;
269other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
270 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
271 | LESS NUM { $$ = gen_less($2); }
272 | GREATER NUM { $$ = gen_greater($2); }
273 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
274 | INBOUND { $$ = gen_inbound(0); }
275 | OUTBOUND { $$ = gen_inbound(1); }
276 ;
277relop: '>' { $$ = BPF_JGT; }
278 | GEQ { $$ = BPF_JGE; }
279 | '=' { $$ = BPF_JEQ; }
280 ;
281irelop: LEQ { $$ = BPF_JGT; }
282 | '<' { $$ = BPF_JGE; }
283 | NEQ { $$ = BPF_JEQ; }
284 ;
285arth: pnum { $$ = gen_loadi($1); }
286 | narth
287 ;
288narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
289 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
290 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
291 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
292 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
293 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
294 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
295 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
296 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
297 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
298 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
299 | paren narth ')' { $$ = $2; }
300 | LEN { $$ = gen_loadlen(); }
301 ;
302byteop: '&' { $$ = '&'; }
303 | '|' { $$ = '|'; }
304 | '<' { $$ = '<'; }
305 | '>' { $$ = '>'; }
306 | '=' { $$ = '='; }
307 ;
308pnum: NUM
309 | paren pnum ')' { $$ = $2; }
310 ;
311%%