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