scanner.l revision 1.4
1%{ 2/* $OpenBSD */ 3/* $NetBSD: scanner.l,v 1.2 1995/03/06 11:39:12 mycroft Exp $ */ 4 5/* 6 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994 7 * The Regents of the University of California. All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that: (1) source code distributions 11 * retain the above copyright notice and this paragraph in its entirety, (2) 12 * distributions including binary code include the above copyright notice and 13 * this paragraph in its entirety in the documentation or other materials 14 * provided with the distribution, and (3) all advertising materials mentioning 15 * features or use of this software display the following acknowledgement: 16 * ``This product includes software developed by the University of California, 17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 18 * the University nor the names of its contributors may be used to endorse 19 * or promote products derived from this software without specific prior 20 * written permission. 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 24 */ 25 26#ifndef lint 27static char rcsid[] = 28 "@(#) Header: scanner.l,v 1.40 94/06/10 17:21:44 mccanne Exp (LBL)"; 29#endif 30 31#include <sys/types.h> 32#include <sys/time.h> 33 34#include <ctype.h> 35#include <pcap.h> 36#include <pcap-namedb.h> 37 38#include "gencode.h" 39#include "grammar.tab.h" 40 41#ifndef __GNUC__ 42#define inline 43#endif 44 45static int stoi(char *); 46static 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 69extern YYSTYPE yylval; 70 71static char *in_buffer; 72 73%} 74 75N ([0-9]+|(0X|0x)[0-9A-Fa-f]+) 76B ([0-9A-Fa-f][0-9A-Fa-f]?) 77 78%a 3000 79 80%% 81dst return DST; 82src return SRC; 83 84link|ether|ppp|slip return LINK; 85fddi return LINK; 86arp return ARP; 87rarp return RARP; 88ip return IP; 89tcp return TCP; 90udp return UDP; 91icmp return ICMP; 92 93decnet return DECNET; 94lat return LAT; 95moprc return MOPRC; 96mopdl return MOPDL; 97 98host return HOST; 99net return NET; 100port return PORT; 101proto return PROTO; 102 103gateway return GATEWAY; 104 105less return LESS; 106greater return GREATER; 107byte return BYTE; 108broadcast return TK_BROADCAST; 109multicast return TK_MULTICAST; 110 111and|"&&" return AND; 112or|"||" return OR; 113not return '!'; 114 115len|length return LEN; 116inbound return INBOUND; 117outbound return OUTBOUND; 118 119[ \n\t] ; 120[+\-*/:\[\]!<>()&|=] return yytext[0]; 121">=" return GEQ; 122"<=" return LEQ; 123"!=" return NEQ; 124"==" return '='; 125"<<" return LSH; 126">>" return RSH; 127{N} { yylval.i = stoi((char *)yytext); return NUM; } 128({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) { 129 yylval.s = sdup((char *)yytext); return HID; 130} 131{B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext); 132 return EID; } 133{B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); } 134[A-Za-z][-_.A-Za-z0-9]* { yylval.s = sdup((char *)yytext); return ID; } 135"\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; } 136[^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ { bpf_error("illegal token: %s\n", yytext); } 137. { bpf_error("illegal char '%c'", *yytext); } 138%% 139void 140lex_init(buf) 141 char *buf; 142{ 143 in_buffer = buf; 144} 145 146/* 147 * Also define a yywrap. Note that if we're using flex, it will 148 * define a macro to map this identifier to pcap_wrap. 149 */ 150int 151yywrap() 152{ 153 return 1; 154} 155 156/* Hex digit to integer. */ 157static inline int 158xdtoi(c) 159 register int c; 160{ 161 if (isdigit(c)) 162 return c - '0'; 163 else if (islower(c)) 164 return c - 'a' + 10; 165 else 166 return c - 'A' + 10; 167} 168 169/* 170 * Convert string to integer. Just like atoi(), but checks for 171 * preceding 0x or 0 and uses hex or octal instead of decimal. 172 */ 173static int 174stoi(s) 175 char *s; 176{ 177 int base = 10; 178 int n = 0; 179 180 if (*s == '0') { 181 if (s[1] == 'x' || s[1] == 'X') { 182 s += 2; 183 base = 16; 184 } 185 else { 186 base = 8; 187 s += 1; 188 } 189 } 190 while (*s) 191 n = n * base + xdtoi(*s++); 192 193 return n; 194} 195 196