4 * Scanner for display filters
6 * $Id: dfilter-scanner.l,v 1.27 2000/01/10 17:32:50 gram Exp $
8 * Ethereal - Network traffic analyzer
9 * By Gerald Combs <gerald@zing.org>
10 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include <io.h> /* for isatty() on win32 */
36 #ifdef HAVE_SYS_TYPES_H
37 # include <sys/types.h>
60 #include "dfilter-int.h"
62 #include "dfilter-grammar.h"
64 /* Flex has a few routines which help us get the scanner to read
65 * from a string rather than from a file. POSIX lex only provides
66 * for reading from a file; any method of reading from a string
67 * is inherently non-portable. Besides reading from a string,
68 * we have to worry about resetting the scanner after a bad
69 * parse; this too is non-portable. Combine the reset with
70 * a string input, and you have major non-portability. I'll provide
71 * the routines for flex here. If you really want to modify the
72 * scanner and use a non-flex lex implementation, you may
73 * add more ifdef's below.
77 /* Flex has built-in support for using a string as an input source
78 * instead of using a file. Nice!
80 YY_BUFFER_STATE string_input_buffer;
84 static char *in_buffer;
86 #define getc(fp) (*in_buffer == 0 ? EOF : *in_buffer++)
100 [\t\n ]+ /* ignore whitespace */
103 and|\&\& { dfilter_lval.operand = TOK_AND; return TOK_AND; }
104 or|\|\| { dfilter_lval.operand = TOK_OR; return TOK_OR; }
105 not|\! { dfilter_lval.operand = TOK_NOT; return TOK_NOT; }
106 xor|\^\^ { dfilter_lval.operand = TOK_XOR; return TOK_XOR; }
107 eq|\=\= { dfilter_lval.operand = TOK_EQ; return TOK_EQ; }
108 ne|\!\= { dfilter_lval.operand = TOK_NE; return TOK_NE; }
109 gt|\> { dfilter_lval.operand = TOK_GT; return TOK_GT; }
110 ge|\>\= { dfilter_lval.operand = TOK_GE; return TOK_GE; }
111 lt|\< { dfilter_lval.operand = TOK_LT; return TOK_LT; }
112 le|\<\= { dfilter_lval.operand = TOK_LE; return TOK_LE; }
114 \[{whitespace}*-?[0-9]+{whitespace}*:{whitespace}*[0-9]+{whitespace}*\] { /* range [ x : y ] */
116 char *byterange_string = g_strdup(yytext);
117 char *s = byterange_string + 1; /* I don't want the first '[' */
120 /* Get the offset from the string */
121 if ((p = strtok(s, ":"))) {
122 dfilter_lval.byte_range.offset = strtol(p, NULL, 10);
125 g_free(byterange_string);
129 /* Get the Length from the string */
130 if ((p = strtok(NULL, "]"))) {
131 dfilter_lval.byte_range.length = strtoul(p, NULL, 10);
134 g_free(byterange_string);
137 g_free(byterange_string);
138 return T_VAL_BYTE_RANGE;
141 \[{whitespace}*-?[0-9]+{whitespace}*\] { /* range [ x ] */
143 char *byterange_string = g_strdup(yytext);
144 char *s = byterange_string + 1; /* I don't want the first '[' */
147 /* Get the offset from the string */
148 if ((p = strtok(s, "]"))) {
149 dfilter_lval.byte_range.offset = strtol(p, NULL, 10);
152 g_free(byterange_string);
156 dfilter_lval.byte_range.length = 0;
157 g_free(byterange_string);
158 return T_VAL_BYTE_RANGE;
161 {hex}({hexsep}{hex})+ { /* byte string, any length */
162 dfilter_lval.string = g_strdup(yytext);
163 return T_VAL_BYTE_STRING;
167 0[xX][A-Fa-f0-9]+ { /* hex values */
168 dfilter_lval.string = g_strdup(yytext);
169 return T_VAL_UNQUOTED_STRING;
172 [A-Za-z0-9\:][A-Za-z0-9\.\_\-\:]+ {
173 /* looks like a protocol, field name, or hostname */
177 dfilter_lval.variable.id = dfilter_lookup_token(yytext);
178 if (dfilter_lval.variable.id < 0) {
179 dfilter_lval.string = g_strdup(yytext);
180 return T_VAL_UNQUOTED_STRING;
183 ftype = proto_registrar_get_ftype(dfilter_lval.variable.id);
189 retval = T_FT_BOOLEAN;
195 retval = T_FT_UINT16;
198 retval = T_FT_UINT32;
210 retval = T_FT_DOUBLE;
212 case FT_ABSOLUTE_TIME:
213 dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-of-day values.",
217 case FT_RELATIVE_TIME:
218 dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-delta values.",
223 retval = T_FT_STRING;
238 retval = T_FT_IPXNET;
241 printf("ftype for %s is %d\n", yytext, ftype);
242 g_assert_not_reached();
246 dfilter_lval.variable.type = retval;
250 ({plus}|{minus})?[0-9]+ { /* decimal and octal integers */
251 dfilter_lval.string = g_strdup(yytext);
252 return T_VAL_UNQUOTED_STRING;
255 ({plus}|{minus})?([0-9]+|[0-9]+\.[0-9]+|\.[0-9]+)([eE]({plus}|{minus})?[0-9]+)? {
256 /* I'm trying to capture all floating points here, and
257 * am using the strtod manpage as the description of
259 dfilter_lval.string = g_strdup(yytext);
260 return T_VAL_UNQUOTED_STRING;
264 dfilter_lval.string = g_strdup(yytext);
265 return T_VAL_UNQUOTED_STRING;
271 /* Resets scanner and assigns the char* argument
272 * as the text to scan
275 dfilter_scanner_text(char *text)
278 string_input_buffer = yy_scan_string(text);
285 dfilter_scanner_cleanup(void)
288 yy_delete_buffer(string_input_buffer);
290 /* There is no standard way to reset a lex scanner.
291 * This is necessary after a failed parse on a syntactically
292 * incorrect display filter. You have to reset the scanner
293 * so that yy_lex() doesn't start scanning from the middle
294 * of the previous input string.
299 /* Flex has an option '%option noyywrap' so that I don't have to
300 * provide this yywrap function, but in order to maintain portability,
301 * I'll just use this yywrap() function.
306 return 1; /* stop at EOF, instead of looking for next file */
309 /* converts a string representing a byte array
312 * Returns a non-null GByteArray pointer on success, NULL on failure.
315 byte_str_to_guint8_array(const char *s)
319 char *byte_str = g_strdup(s); /* local copy of string */
322 barray = g_byte_array_new();
323 /* XXX - don't use global_df, but pass in pointer to GSList* */
324 global_df->list_of_byte_arrays = g_slist_append(global_df->list_of_byte_arrays, barray);
326 byte_str = g_strdup(s);
328 while ((p = strtok(str, "-:."))) {
329 val = (guint8) strtoul(p, NULL, 16);
330 g_byte_array_append(barray, &val, 1);
332 /* subsequent calls to strtok() require NULL as arg 1 */