]> git.ozlabs.org Git - ppp.git/blob - pppd/grammar.y
syslog stuff back to main.c; take out npmode setting in sifup
[ppp.git] / pppd / grammar.y
1 %{
2 /*      From NetBSD: grammar.y,v 1.2 1995/03/06 11:38:27 mycroft Exp */
3
4 /*
5  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that: (1) source code distributions
10  * retain the above copyright notice and this paragraph in its entirety, (2)
11  * distributions including binary code include the above copyright notice and
12  * this paragraph in its entirety in the documentation or other materials
13  * provided with the distribution, and (3) all advertising materials mentioning
14  * features or use of this software display the following acknowledgement:
15  * ``This product includes software developed by the University of California,
16  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
17  * the University nor the names of its contributors may be used to endorse
18  * or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23  *
24  */
25 #ifndef lint
26 static char rcsid[] =
27     "@(#) Header: grammar.y,v 1.39 94/06/14 20:09:25 leres Exp (LBL)";
28 #endif
29
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #include <net/ppp_defs.h>
36 #include "bpf_compile.h"
37 #include "gencode.h"
38
39 #define QSET(q, p, d, a) (q).proto = (p),\
40                          (q).dir = (d),\
41                          (q).addr = (a)
42
43 int n_errors = 0;
44
45 static struct qual qerr = { Q_UNDEF, Q_UNDEF, Q_UNDEF, Q_UNDEF };
46
47 static void
48 yyerror(char *msg)
49 {
50         ++n_errors;
51         bpf_error(msg);
52         /* NOTREACHED */
53 }
54
55 #ifndef YYBISON
56 int
57 pcap_parse()
58 {
59         extern int yyparse();
60         return (yyparse());
61 }
62 #endif
63
64 %}
65
66 %union {
67         int i;
68         unsigned long h;
69         char *s;
70         struct stmt *stmt;
71         struct arth *a;
72         struct {
73                 struct qual q;
74                 struct block *b;
75         } blk;
76         struct block *rblk;
77 }
78
79 %type   <blk>   expr id nid pid term rterm qid
80 %type   <blk>   head
81 %type   <i>     pqual dqual aqual
82 %type   <a>     arth narth
83 %type   <i>     byteop pname pnum relop irelop
84 %type   <blk>   and or paren not null prog
85 %type   <rblk>  other
86
87 %token  DST SRC HOST
88 %token  NET PORT LESS GREATER PROTO BYTE
89 %token  IP TCP UDP ICMP
90 %token  TK_BROADCAST TK_MULTICAST
91 %token  NUM INBOUND OUTBOUND
92 %token  LINK
93 %token  GEQ LEQ NEQ
94 %token  ID HID
95 %token  LSH RSH
96 %token  LEN
97
98 %type   <s> ID
99 %type   <h> HID
100 %type   <i> NUM
101
102 %left OR AND
103 %nonassoc  '!'
104 %left '|'
105 %left '&'
106 %left LSH RSH
107 %left '+' '-'
108 %left '*' '/'
109 %nonassoc UMINUS
110 %%
111 prog:     null expr
112 {
113         finish_parse($2.b);
114 }
115         | null
116         ;
117 null:     /* null */            { $$.q = qerr; }
118         ;
119 expr:     term
120         | expr and term         { gen_and($1.b, $3.b); $$ = $3; }
121         | expr and id           { gen_and($1.b, $3.b); $$ = $3; }
122         | expr or term          { gen_or($1.b, $3.b); $$ = $3; }
123         | expr or id            { gen_or($1.b, $3.b); $$ = $3; }
124         ;
125 and:      AND                   { $$ = $<blk>0; }
126         ;
127 or:       OR                    { $$ = $<blk>0; }
128         ;
129 id:       nid
130         | pnum                  { $$.b = gen_ncode((unsigned long)$1,
131                                                    $$.q = $<blk>0.q); }
132         | paren pid ')'         { $$ = $2; }
133         ;
134 nid:      ID                    { $$.b = gen_scode($1, $$.q = $<blk>0.q); }
135         | HID                   { $$.b = gen_ncode(__pcap_atoin((char *)$1),
136                                                    $$.q); }
137         | not id                { gen_not($2.b); $$ = $2; }
138         ;
139 not:      '!'                   { $$ = $<blk>0; }
140         ;
141 paren:    '('                   { $$ = $<blk>0; }
142         ;
143 pid:      nid
144         | qid and id            { gen_and($1.b, $3.b); $$ = $3; }
145         | qid or id             { gen_or($1.b, $3.b); $$ = $3; }
146         ;
147 qid:      pnum                  { $$.b = gen_ncode((unsigned long)$1,
148                                                    $$.q = $<blk>0.q); }
149         | pid
150         ;
151 term:     rterm
152         | not term              { gen_not($2.b); $$ = $2; }
153         ;
154 head:     pqual dqual aqual     { QSET($$.q, $1, $2, $3); }
155         | pqual dqual           { QSET($$.q, $1, $2, Q_DEFAULT); }
156         | pqual aqual           { QSET($$.q, $1, Q_DEFAULT, $2); }
157         | pqual PROTO           { QSET($$.q, $1, Q_DEFAULT, Q_PROTO); }
158         ;
159 rterm:    head id               { $$ = $2; }
160         | paren expr ')'        { $$.b = $2.b; $$.q = $1.q; }
161         | pname                 { $$.b = gen_proto_abbrev($1); $$.q = qerr; }
162         | arth relop arth       { $$.b = gen_relation($2, $1, $3, 0);
163                                   $$.q = qerr; }
164         | arth irelop arth      { $$.b = gen_relation($2, $1, $3, 1);
165                                   $$.q = qerr; }
166         | other                 { $$.b = $1; $$.q = qerr; }
167         ;
168 /* protocol level qualifiers */
169 pqual:    pname
170         |                       { $$ = Q_DEFAULT; }
171         ;
172 /* 'direction' qualifiers */
173 dqual:    SRC                   { $$ = Q_SRC; }
174         | DST                   { $$ = Q_DST; }
175         | SRC OR DST            { $$ = Q_OR; }
176         | DST OR SRC            { $$ = Q_OR; }
177         | SRC AND DST           { $$ = Q_AND; }
178         | DST AND SRC           { $$ = Q_AND; }
179         ;
180 /* address type qualifiers */
181 aqual:    HOST                  { $$ = Q_HOST; }
182         | NET                   { $$ = Q_NET; }
183         | PORT                  { $$ = Q_PORT; }
184         ;
185 pname:    LINK                  { $$ = Q_LINK; }
186         | IP                    { $$ = Q_IP; }
187         | TCP                   { $$ = Q_TCP; }
188         | UDP                   { $$ = Q_UDP; }
189         | ICMP                  { $$ = Q_ICMP; }
190         ;
191 other:    pqual TK_BROADCAST    { $$ = gen_broadcast($1); }
192         | pqual TK_MULTICAST    { $$ = gen_multicast($1); }
193         | LESS NUM              { $$ = gen_less($2); }
194         | GREATER NUM           { $$ = gen_greater($2); }
195         | BYTE NUM byteop NUM   { $$ = gen_byteop($3, $2, $4); }
196         | INBOUND               { $$ = gen_inbound(0); }
197         | OUTBOUND              { $$ = gen_inbound(1); }
198         ;
199 relop:    '>'                   { $$ = BPF_JGT; }
200         | GEQ                   { $$ = BPF_JGE; }
201         | '='                   { $$ = BPF_JEQ; }
202         ;
203 irelop:   LEQ                   { $$ = BPF_JGT; }
204         | '<'                   { $$ = BPF_JGE; }
205         | NEQ                   { $$ = BPF_JEQ; }
206         ;
207 arth:     pnum                  { $$ = gen_loadi($1); }
208         | narth
209         ;
210 narth:    pname '[' arth ']'            { $$ = gen_load($1, $3, 1); }
211         | pname '[' arth ':' NUM ']'    { $$ = gen_load($1, $3, $5); }
212         | arth '+' arth                 { $$ = gen_arth(BPF_ADD, $1, $3); }
213         | arth '-' arth                 { $$ = gen_arth(BPF_SUB, $1, $3); }
214         | arth '*' arth                 { $$ = gen_arth(BPF_MUL, $1, $3); }
215         | arth '/' arth                 { $$ = gen_arth(BPF_DIV, $1, $3); }
216         | arth '&' arth                 { $$ = gen_arth(BPF_AND, $1, $3); }
217         | arth '|' arth                 { $$ = gen_arth(BPF_OR, $1, $3); }
218         | arth LSH arth                 { $$ = gen_arth(BPF_LSH, $1, $3); }
219         | arth RSH arth                 { $$ = gen_arth(BPF_RSH, $1, $3); }
220         | '-' arth %prec UMINUS         { $$ = gen_neg($2); }
221         | paren narth ')'               { $$ = $2; }
222         | LEN                           { $$ = gen_loadlen(); }
223         ;
224 byteop:   '&'                   { $$ = '&'; }
225         | '|'                   { $$ = '|'; }
226         | '<'                   { $$ = '<'; }
227         | '>'                   { $$ = '>'; }
228         | '='                   { $$ = '='; }
229         ;
230 pnum:     NUM
231         | paren pnum ')'        { $$ = $2; }
232         ;
233 %%