xref: /freebsd/contrib/libpcap/scanner.l (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 %{
2 /*
3  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
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: scanner.l,v 1.56 97/07/21 13:31:50 leres Exp $ (LBL)";
26 #endif
27 
28 #include <sys/types.h>
29 #include <sys/time.h>
30 
31 #include <ctype.h>
32 #include <unistd.h>
33 
34 #include "pcap-int.h"
35 
36 #include "gencode.h"
37 #include <pcap-namedb.h>
38 #include "tokdefs.h"
39 
40 #include "gnuc.h"
41 #ifdef HAVE_OS_PROTO_H
42 #include "os-proto.h"
43 #endif
44 
45 static int stoi(char *);
46 static inline int xdtoi(int);
47 
48 #ifdef FLEX_SCANNER
49 #define YY_NO_UNPUT
50 #undef YY_INPUT
51 #define YY_INPUT(buf, result, max)\
52  {\
53 	char *src = in_buffer;\
54 	int i;\
55 \
56 	if (*src == 0)\
57 		result = YY_NULL;\
58 	else {\
59 		for (i = 0; *src && i < max; ++i)\
60 			buf[i] = *src++;\
61 		in_buffer += i;\
62 		result = i;\
63 	}\
64  }
65 #else
66 #undef getc
67 #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
68 #endif
69 
70 #define yylval pcap_lval
71 extern YYSTYPE yylval;
72 
73 static char *in_buffer;
74 
75 %}
76 
77 N		([0-9]+|(0X|0x)[0-9A-Fa-f]+)
78 B		([0-9A-Fa-f][0-9A-Fa-f]?)
79 
80 %a 3000
81 
82 %%
83 dst		return DST;
84 src		return SRC;
85 
86 link|ether|ppp|slip  return LINK;
87 fddi		return LINK;
88 arp		return ARP;
89 rarp		return RARP;
90 ip		return IP;
91 tcp		return TCP;
92 udp		return UDP;
93 icmp		return ICMP;
94 igmp		return IGMP;
95 igrp		return IGRP;
96 
97 atalk		return ATALK;
98 decnet		return DECNET;
99 lat		return LAT;
100 sca		return SCA;
101 moprc		return MOPRC;
102 mopdl		return MOPDL;
103 
104 iso		return ISO;
105 esis		return ESIS;
106 es-is		return ESIS;
107 isis		return ISIS;
108 is-is		return ISIS;
109 
110 host		return HOST;
111 net		return NET;
112 mask		return MASK;
113 port		return PORT;
114 proto		return PROTO;
115 
116 gateway		return GATEWAY;
117 
118 less		return LESS;
119 greater		return GREATER;
120 byte		return BYTE;
121 broadcast	return TK_BROADCAST;
122 multicast	return TK_MULTICAST;
123 
124 and|"&&"	return AND;
125 or|"||"		return OR;
126 not		return '!';
127 
128 len|length	return LEN;
129 inbound		return INBOUND;
130 outbound	return OUTBOUND;
131 
132 [ \n\t]			;
133 [+\-*/:\[\]!<>()&|=]	return yytext[0];
134 ">="			return GEQ;
135 "<="			return LEQ;
136 "!="			return NEQ;
137 "=="			return '=';
138 "<<"			return LSH;
139 ">>"			return RSH;
140 {N}			{ yylval.i = stoi((char *)yytext); return NUM; }
141 ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})	{
142 			yylval.s = sdup((char *)yytext); return HID; }
143 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
144 			  return EID; }
145 {B}:+({B}:+)+		{ bpf_error("bogus ethernet address %s", yytext); }
146 [A-Za-z0-9][-_.A-Za-z0-9]*[.A-Za-z0-9] {
147 			 yylval.s = sdup((char *)yytext); return ID; }
148 "\\"[^ !()\n\t]+	{ yylval.s = sdup((char *)yytext + 1); return ID; }
149 [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+i {
150 			bpf_error("illegal token: %s\n", yytext); }
151 .			{ bpf_error("illegal char '%c'", *yytext); }
152 %%
153 void
154 lex_init(buf)
155 	char *buf;
156 {
157 	in_buffer = buf;
158 }
159 
160 /*
161  * Also define a yywrap.  Note that if we're using flex, it will
162  * define a macro to map this identifier to pcap_wrap.
163  */
164 int
165 yywrap()
166 {
167 	return 1;
168 }
169 
170 /* Hex digit to integer. */
171 static inline int
172 xdtoi(c)
173 	register int c;
174 {
175 	if (isdigit(c))
176 		return c - '0';
177 	else if (islower(c))
178 		return c - 'a' + 10;
179 	else
180 		return c - 'A' + 10;
181 }
182 
183 /*
184  * Convert string to integer.  Just like atoi(), but checks for
185  * preceding 0x or 0 and uses hex or octal instead of decimal.
186  */
187 static int
188 stoi(s)
189 	char *s;
190 {
191 	int base = 10;
192 	int n = 0;
193 
194 	if (*s == '0') {
195 		if (s[1] == 'x' || s[1] == 'X') {
196 			s += 2;
197 			base = 16;
198 		}
199 		else {
200 			base = 8;
201 			s += 1;
202 		}
203 	}
204 	while (*s)
205 		n = n * base + xdtoi(*s++);
206 
207 	return n;
208 }
209 
210