]> The Tcpdump Group git mirrors - libpcap/blob - grammar.y
use AC_DEFINE/3 to get defines into config.h.in
[libpcap] / grammar.y
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 */
23 #ifndef lint
24 static const char rcsid[] =
25 "@(#) $Header: /tcpdump/master/libpcap/grammar.y,v 1.58 2000-06-03 16:29:43 itojun Exp $ (LBL)";
26 #endif
27
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #include <sys/socket.h>
31 #include <stdlib.h>
32
33 #if __STDC__
34 struct mbuf;
35 struct rtentry;
36 #endif
37
38 #include <net/if.h>
39
40 #include <netinet/in.h>
41 #include <netinet/if_ether.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
59 int n_errors = 0;
60
61 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
62
63 static void
64 yyerror(char *msg)
65 {
66 ++n_errors;
67 bpf_error("%s", msg);
68 /* NOTREACHED */
69 }
70
71 #ifndef YYBISON
72 int yyparse(void);
73
74 int
75 pcap_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
106 %token NET MASK PORT LESS GREATER PROTO PROTOCHAIN BYTE
107 %token ARP RARP IP TCP UDP ICMP IGMP IGRP PIM
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
113 %token ID EID HID HID6
114 %token LSH RSH
115 %token LEN
116 %token IPV6 ICMPV6 AH ESP
117
118 %type <s> ID
119 %type <e> EID
120 %type <s> HID HID6
121 %type <i> NUM
122
123 %left OR AND
124 %nonassoc '!'
125 %left '|'
126 %left '&'
127 %left LSH RSH
128 %left '+' '-'
129 %left '*' '/'
130 %nonassoc UMINUS
131 %%
132 prog: null expr
133 {
134 finish_parse($2.b);
135 }
136 | null
137 ;
138 null: /* null */ { $$.q = qerr; }
139 ;
140 expr: term
141 | expr and term { gen_and($1.b, $3.b); $$ = $3; }
142 | expr and id { gen_and($1.b, $3.b); $$ = $3; }
143 | expr or term { gen_or($1.b, $3.b); $$ = $3; }
144 | expr or id { gen_or($1.b, $3.b); $$ = $3; }
145 ;
146 and: AND { $$ = $<blk>0; }
147 ;
148 or: OR { $$ = $<blk>0; }
149 ;
150 id: nid
151 | pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
152 $$.q = $<blk>0.q); }
153 | paren pid ')' { $$ = $2; }
154 ;
155 nid: ID { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
156 | HID '/' NUM { $$.b = gen_mcode($1, NULL, $3,
157 $$.q = $<blk>0.q); }
158 | HID MASK HID { $$.b = gen_mcode($1, $3, 0,
159 $$.q = $<blk>0.q); }
160 | HID {
161 /* Decide how to parse HID based on proto */
162 $$.q = $<blk>0.q;
163 switch ($$.q.proto) {
164 case Q_DECNET:
165 $$.b = gen_ncode($1, 0, $$.q);
166 break;
167 default:
168 $$.b = gen_ncode($1, 0, $$.q);
169 break;
170 }
171 }
172 | HID6 '/' NUM {
173 #ifdef INET6
174 $$.b = gen_mcode6($1, NULL, $3,
175 $$.q = $<blk>0.q);
176 #else
177 bpf_error("'ip6addr/prefixlen' not supported "
178 "in this configuration");
179 #endif /*INET6*/
180 }
181 | HID6 {
182 #ifdef INET6
183 $$.b = gen_mcode6($1, 0, 128,
184 $$.q = $<blk>0.q);
185 #else
186 bpf_error("'ip6addr' not supported "
187 "in this configuration");
188 #endif /*INET6*/
189 }
190 | EID { $$.b = gen_ecode($1, $$.q = $<blk>0.q); }
191 | not id { gen_not($2.b); $$ = $2; }
192 ;
193 not: '!' { $$ = $<blk>0; }
194 ;
195 paren: '(' { $$ = $<blk>0; }
196 ;
197 pid: nid
198 | qid and id { gen_and($1.b, $3.b); $$ = $3; }
199 | qid or id { gen_or($1.b, $3.b); $$ = $3; }
200 ;
201 qid: pnum { $$.b = gen_ncode(NULL, (bpf_u_int32)$1,
202 $$.q = $<blk>0.q); }
203 | pid
204 ;
205 term: rterm
206 | not term { gen_not($2.b); $$ = $2; }
207 ;
208 head: pqual dqual aqual { QSET($$.q, $1, $2, $3); }
209 | pqual dqual { QSET($$.q, $1, $2, Q_DEFAULT); }
210 | pqual aqual { QSET($$.q, $1, Q_DEFAULT, $2); }
211 | pqual PROTO { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
212 | pqual PROTOCHAIN { QSET($$.q, $1, Q_DEFAULT, Q_PROTOCHAIN); }
213 | pqual ndaqual { QSET($$.q, $1, Q_DEFAULT, $2); }
214 ;
215 rterm: head id { $$ = $2; }
216 | paren expr ')' { $$.b = $2.b; $$.q = $1.q; }
217 | pname { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
218 | arth relop arth { $$.b = gen_relation($2, $1, $3, 0);
219 $$.q = qerr; }
220 | arth irelop arth { $$.b = gen_relation($2, $1, $3, 1);
221 $$.q = qerr; }
222 | other { $$.b = $1; $$.q = qerr; }
223 ;
224 /* protocol level qualifiers */
225 pqual: pname
226 | { $$ = Q_DEFAULT; }
227 ;
228 /* 'direction' qualifiers */
229 dqual: SRC { $$ = Q_SRC; }
230 | DST { $$ = Q_DST; }
231 | SRC OR DST { $$ = Q_OR; }
232 | DST OR SRC { $$ = Q_OR; }
233 | SRC AND DST { $$ = Q_AND; }
234 | DST AND SRC { $$ = Q_AND; }
235 ;
236 /* address type qualifiers */
237 aqual: HOST { $$ = Q_HOST; }
238 | NET { $$ = Q_NET; }
239 | PORT { $$ = Q_PORT; }
240 ;
241 /* non-directional address type qualifiers */
242 ndaqual: GATEWAY { $$ = Q_GATEWAY; }
243 ;
244 pname: LINK { $$ = Q_LINK; }
245 | IP { $$ = Q_IP; }
246 | ARP { $$ = Q_ARP; }
247 | RARP { $$ = Q_RARP; }
248 | TCP { $$ = Q_TCP; }
249 | UDP { $$ = Q_UDP; }
250 | ICMP { $$ = Q_ICMP; }
251 | IGMP { $$ = Q_IGMP; }
252 | IGRP { $$ = Q_IGRP; }
253 | PIM { $$ = Q_PIM; }
254 | ATALK { $$ = Q_ATALK; }
255 | DECNET { $$ = Q_DECNET; }
256 | LAT { $$ = Q_LAT; }
257 | SCA { $$ = Q_SCA; }
258 | MOPDL { $$ = Q_MOPDL; }
259 | MOPRC { $$ = Q_MOPRC; }
260 | IPV6 { $$ = Q_IPV6; }
261 | ICMPV6 { $$ = Q_ICMPV6; }
262 | AH { $$ = Q_AH; }
263 | ESP { $$ = Q_ESP; }
264 ;
265 other: pqual TK_BROADCAST { $$ = gen_broadcast($1); }
266 | pqual TK_MULTICAST { $$ = gen_multicast($1); }
267 | LESS NUM { $$ = gen_less($2); }
268 | GREATER NUM { $$ = gen_greater($2); }
269 | BYTE NUM byteop NUM { $$ = gen_byteop($3, $2, $4); }
270 | INBOUND { $$ = gen_inbound(0); }
271 | OUTBOUND { $$ = gen_inbound(1); }
272 ;
273 relop: '>' { $$ = BPF_JGT; }
274 | GEQ { $$ = BPF_JGE; }
275 | '=' { $$ = BPF_JEQ; }
276 ;
277 irelop: LEQ { $$ = BPF_JGT; }
278 | '<' { $$ = BPF_JGE; }
279 | NEQ { $$ = BPF_JEQ; }
280 ;
281 arth: pnum { $$ = gen_loadi($1); }
282 | narth
283 ;
284 narth: pname '[' arth ']' { $$ = gen_load($1, $3, 1); }
285 | pname '[' arth ':' NUM ']' { $$ = gen_load($1, $3, $5); }
286 | arth '+' arth { $$ = gen_arth(BPF_ADD, $1, $3); }
287 | arth '-' arth { $$ = gen_arth(BPF_SUB, $1, $3); }
288 | arth '*' arth { $$ = gen_arth(BPF_MUL, $1, $3); }
289 | arth '/' arth { $$ = gen_arth(BPF_DIV, $1, $3); }
290 | arth '&' arth { $$ = gen_arth(BPF_AND, $1, $3); }
291 | arth '|' arth { $$ = gen_arth(BPF_OR, $1, $3); }
292 | arth LSH arth { $$ = gen_arth(BPF_LSH, $1, $3); }
293 | arth RSH arth { $$ = gen_arth(BPF_RSH, $1, $3); }
294 | '-' arth %prec UMINUS { $$ = gen_neg($2); }
295 | paren narth ')' { $$ = $2; }
296 | LEN { $$ = gen_loadlen(); }
297 ;
298 byteop: '&' { $$ = '&'; }
299 | '|' { $$ = '|'; }
300 | '<' { $$ = '<'; }
301 | '>' { $$ = '>'; }
302 | '=' { $$ = '='; }
303 ;
304 pnum: NUM
305 | paren pnum ')' { $$ = $2; }
306 ;
307 %%