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