scanner.l revision 26178
117683Spst%{ 217683Spst/* 317683Spst * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996 417683Spst * The Regents of the University of California. All rights reserved. 517683Spst * 617683Spst * Redistribution and use in source and binary forms, with or without 717683Spst * modification, are permitted provided that: (1) source code distributions 817683Spst * retain the above copyright notice and this paragraph in its entirety, (2) 917683Spst * distributions including binary code include the above copyright notice and 1017683Spst * this paragraph in its entirety in the documentation or other materials 1117683Spst * provided with the distribution, and (3) all advertising materials mentioning 1217683Spst * features or use of this software display the following acknowledgement: 1317683Spst * ``This product includes software developed by the University of California, 1417683Spst * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 1517683Spst * the University nor the names of its contributors may be used to endorse 1617683Spst * or promote products derived from this software without specific prior 1717683Spst * written permission. 1817683Spst * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 1917683Spst * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 2017683Spst * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2117683Spst */ 2217683Spst 2317683Spst#ifndef lint 2426178Sfennerstatic const char rcsid[] = 2526178Sfenner "@(#) $Header: scanner.l,v 1.55 96/09/27 22:34:45 leres Exp $ (LBL)"; 2617683Spst#endif 2717683Spst 2817683Spst#include <sys/types.h> 2917683Spst#include <sys/time.h> 3017683Spst 3117683Spst#include <ctype.h> 3217683Spst#include <unistd.h> 3317683Spst 3417683Spst#include "pcap-int.h" 3517683Spst 3617683Spst#include "gencode.h" 3717683Spst#include <pcap-namedb.h> 3817683Spst#include "tokdefs.h" 3917683Spst 4017683Spst#include "gnuc.h" 4117683Spst#ifdef HAVE_OS_PROTO_H 4217683Spst#include "os-proto.h" 4317683Spst#endif 4417683Spst 4517683Spststatic int stoi(char *); 4617683Spststatic inline int xdtoi(int); 4717683Spst 4817683Spst#ifdef FLEX_SCANNER 4926178Sfenner#define YY_NO_UNPUT 5017683Spst#undef YY_INPUT 5117683Spst#define YY_INPUT(buf, result, max)\ 5217683Spst {\ 5317683Spst char *src = in_buffer;\ 5417683Spst int i;\ 5517683Spst\ 5617683Spst if (*src == 0)\ 5717683Spst result = YY_NULL;\ 5817683Spst else {\ 5917683Spst for (i = 0; *src && i < max; ++i)\ 6017683Spst buf[i] = *src++;\ 6117683Spst in_buffer += i;\ 6217683Spst result = i;\ 6317683Spst }\ 6417683Spst } 6517683Spst#else 6617683Spst#undef getc 6717683Spst#define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++) 6817683Spst#endif 6917683Spst 7017683Spst#define yylval pcap_lval 7117683Spstextern YYSTYPE yylval; 7217683Spst 7317683Spststatic char *in_buffer; 7417683Spst 7517683Spst%} 7617683Spst 7717683SpstN ([0-9]+|(0X|0x)[0-9A-Fa-f]+) 7817683SpstB ([0-9A-Fa-f][0-9A-Fa-f]?) 7917683Spst 8017683Spst%a 3000 8117683Spst 8217683Spst%% 8317683Spstdst return DST; 8417683Spstsrc return SRC; 8517683Spst 8617683Spstlink|ether|ppp|slip return LINK; 8717683Spstfddi return LINK; 8817683Spstarp return ARP; 8917683Spstrarp return RARP; 9017683Spstip return IP; 9117683Spsttcp return TCP; 9217683Spstudp return UDP; 9317683Spsticmp return ICMP; 9417683Spstigmp return IGMP; 9517683Spstigrp return IGRP; 9617683Spst 9717683Spstatalk return ATALK; 9817683Spstdecnet return DECNET; 9917683Spstlat return LAT; 10017683Spstsca return SCA; 10117683Spstmoprc return MOPRC; 10217683Spstmopdl return MOPDL; 10317683Spst 10417749Spstiso return ISO; 10517749Spstesis return ESIS; 10617749Spstes-is return ESIS; 10717749Spstisis return ISIS; 10817749Spstis-is return ISIS; 10917749Spst 11017683Spsthost return HOST; 11117683Spstnet return NET; 11217683Spstmask return MASK; 11317683Spstport return PORT; 11417683Spstproto return PROTO; 11517683Spst 11617683Spstgateway return GATEWAY; 11717683Spst 11817683Spstless return LESS; 11917683Spstgreater return GREATER; 12017683Spstbyte return BYTE; 12117683Spstbroadcast return TK_BROADCAST; 12217683Spstmulticast return TK_MULTICAST; 12317683Spst 12417683Spstand|"&&" return AND; 12517683Spstor|"||" return OR; 12617683Spstnot return '!'; 12717683Spst 12817683Spstlen|length return LEN; 12917683Spstinbound return INBOUND; 13017683Spstoutbound return OUTBOUND; 13117683Spst 13217683Spst[ \n\t] ; 13317683Spst[+\-*/:\[\]!<>()&|=] return yytext[0]; 13417683Spst">=" return GEQ; 13517683Spst"<=" return LEQ; 13617683Spst"!=" return NEQ; 13717683Spst"==" return '='; 13817683Spst"<<" return LSH; 13917683Spst">>" return RSH; 14017683Spst{N} { yylval.i = stoi((char *)yytext); return NUM; } 14117683Spst({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N}) { 14217683Spst yylval.s = sdup((char *)yytext); return HID; 14317683Spst} 14417683Spst{B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext); 14517683Spst return EID; } 14617683Spst{B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); } 14717683Spst[A-Za-z][-_.A-Za-z0-9]* { yylval.s = sdup((char *)yytext); return ID; } 14817683Spst"\\"[^ !()\n\t]+ { yylval.s = sdup((char *)yytext + 1); return ID; } 14917683Spst[^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+ { bpf_error("illegal token: %s\n", yytext); } 15017683Spst. { bpf_error("illegal char '%c'", *yytext); } 15117683Spst%% 15217683Spstvoid 15317683Spstlex_init(buf) 15417683Spst char *buf; 15517683Spst{ 15617683Spst in_buffer = buf; 15717683Spst} 15817683Spst 15917683Spst/* 16017683Spst * Also define a yywrap. Note that if we're using flex, it will 16117683Spst * define a macro to map this identifier to pcap_wrap. 16217683Spst */ 16317683Spstint 16417683Spstyywrap() 16517683Spst{ 16617683Spst return 1; 16717683Spst} 16817683Spst 16917683Spst/* Hex digit to integer. */ 17017683Spststatic inline int 17117683Spstxdtoi(c) 17217683Spst register int c; 17317683Spst{ 17417683Spst if (isdigit(c)) 17517683Spst return c - '0'; 17617683Spst else if (islower(c)) 17717683Spst return c - 'a' + 10; 17817683Spst else 17917683Spst return c - 'A' + 10; 18017683Spst} 18117683Spst 18217683Spst/* 18317683Spst * Convert string to integer. Just like atoi(), but checks for 18417683Spst * preceding 0x or 0 and uses hex or octal instead of decimal. 18517683Spst */ 18617683Spststatic int 18717683Spststoi(s) 18817683Spst char *s; 18917683Spst{ 19017683Spst int base = 10; 19117683Spst int n = 0; 19217683Spst 19317683Spst if (*s == '0') { 19417683Spst if (s[1] == 'x' || s[1] == 'X') { 19517683Spst s += 2; 19617683Spst base = 16; 19717683Spst } 19817683Spst else { 19917683Spst base = 8; 20017683Spst s += 1; 20117683Spst } 20217683Spst } 20317683Spst while (*s) 20417683Spst n = n * base + xdtoi(*s++); 20517683Spst 20617683Spst return n; 20717683Spst} 20817683Spst 209