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 const char rcsid[] = 25 "@(#) $Header: scanner.l,v 1.55 96/09/27 22:34:45 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 } 144 {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext); 145 return EID; } 146 {B}:+({B}:+)+ { bpf_error("bogus ethernet address %s", yytext); } 147 [A-Za-z][-_.A-Za-z0-9]* { 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!<>()&|=]+ { bpf_error("illegal token: %s\n", yytext); } 150 . { bpf_error("illegal char '%c'", *yytext); } 151 %% 152 void 153 lex_init(buf) 154 char *buf; 155 { 156 in_buffer = buf; 157 } 158 159 /* 160 * Also define a yywrap. Note that if we're using flex, it will 161 * define a macro to map this identifier to pcap_wrap. 162 */ 163 int 164 yywrap() 165 { 166 return 1; 167 } 168 169 /* Hex digit to integer. */ 170 static inline int 171 xdtoi(c) 172 register int c; 173 { 174 if (isdigit(c)) 175 return c - '0'; 176 else if (islower(c)) 177 return c - 'a' + 10; 178 else 179 return c - 'A' + 10; 180 } 181 182 /* 183 * Convert string to integer. Just like atoi(), but checks for 184 * preceding 0x or 0 and uses hex or octal instead of decimal. 185 */ 186 static int 187 stoi(s) 188 char *s; 189 { 190 int base = 10; 191 int n = 0; 192 193 if (*s == '0') { 194 if (s[1] == 'x' || s[1] == 'X') { 195 s += 2; 196 base = 16; 197 } 198 else { 199 base = 8; 200 s += 1; 201 } 202 } 203 while (*s) 204 n = n * base + xdtoi(*s++); 205 206 return n; 207 } 208 209