aboutsummaryrefslogtreecommitdiffstats
path: root/epan/dfilter/scanner.l
blob: 71c5cc0a80693a54ebfb21c07b383ede726ab015 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
%{
/*
 * $Id: scanner.l,v 1.3 2001/02/27 19:23:28 gram Exp $
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@zing.org>
 * Copyright 2001 Gerald Combs
 *
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <errno.h>

#include "glib-util.h"
#include "dfilter-int.h"
#include "syntax-tree.h"
#include "grammar.h"

#define LVAL		df_lval
#define LVAL_TYPE	stnode_t*
#define LVAL_INIT_VAL	NULL
#define MODNAME		df

#include <lemonflex-head.inc>

/*#undef YY_NO_UNPUT*/

static int set_lval(int token, gpointer data);
static int set_lval_int(int token, char *s);
static int simple(int token);
static gboolean str_to_guint32(char *s, guint32* pint);

%}

%x RANGE

BWCHARS		[[:alnum:]\[\]\-_.+!@#%^&*=/:]
INITVAR		[_A-Za-z]
VARCHARS	[[:alnum:]_]


%%

[[:blank:]\n]+	/* ignore whitespace */



"("				return simple(TOKEN_LPAREN);
")"				return simple(TOKEN_RPAREN);

"=="				return simple(TOKEN_TEST_EQ);
"eq"				return simple(TOKEN_TEST_EQ);
"!="				return simple(TOKEN_TEST_NE);
"ne"				return simple(TOKEN_TEST_NE);
">"				return simple(TOKEN_TEST_GT);
"gt"				return simple(TOKEN_TEST_GT);
">="				return simple(TOKEN_TEST_GE);
"ge"				return simple(TOKEN_TEST_GE);
"<"				return simple(TOKEN_TEST_LT);
"lt"				return simple(TOKEN_TEST_LT);
"<="				return simple(TOKEN_TEST_LE);
"le"				return simple(TOKEN_TEST_LE);

"!"				return simple(TOKEN_TEST_NOT);
"not"				return simple(TOKEN_TEST_NOT);
"&&"				return simple(TOKEN_TEST_AND);
"and"				return simple(TOKEN_TEST_AND);
"||"				return simple(TOKEN_TEST_OR);
"or"				return simple(TOKEN_TEST_OR);


"["				{
	BEGIN(RANGE);
	return simple(TOKEN_LBRACKET);
}

<RANGE>[+-]?[[:digit:]]+		{
	return set_lval_int(TOKEN_INTEGER, yytext);
}
<RANGE>[+-]?0x[[:xdigit:]]+		{
	return set_lval_int(TOKEN_INTEGER, yytext);
}
<RANGE>":"			return simple(TOKEN_COLON);
<RANGE>","			return simple(TOKEN_COMMA);

<RANGE>"]"			{
	BEGIN(INITIAL);
	return simple(TOKEN_RBRACKET);
}


\"[^"]*\"				{
	return set_lval(TOKEN_STRING, g_substrdup(yytext, 1, -2));
}



[[:alnum:]_.:]+	{
	/* Is it a field name? */
	header_field_info *hfinfo;

	hfinfo = dfilter_lookup_token(yytext);
	if (hfinfo) {
		/* Yes, it's a field name */
		return set_lval(TOKEN_FIELD, hfinfo);
	}
	else {
		/* No, so treat it as a string */
		return set_lval(TOKEN_STRING, g_strdup(yytext));
	}
}



%%

static int
simple(int token)
{
	switch (token) {
		case TOKEN_LPAREN:
		case TOKEN_RPAREN:
		case TOKEN_LBRACKET:
		case TOKEN_RBRACKET:
		case TOKEN_COLON:
		case TOKEN_COMMA:
		case TOKEN_TEST_EQ:
		case TOKEN_TEST_NE:
		case TOKEN_TEST_GT:
		case TOKEN_TEST_GE:
		case TOKEN_TEST_LT:
		case TOKEN_TEST_LE:
		case TOKEN_TEST_NOT:
		case TOKEN_TEST_AND:
		case TOKEN_TEST_OR:
			break;
		default:
			g_assert_not_reached();
	}
	return token;
}

static int
set_lval(int token, gpointer data)
{
	sttype_id_t	type_id = STTYPE_UNINITIALIZED;

	switch (token) {
		case TOKEN_STRING:
			type_id = STTYPE_STRING;
			break;
		case TOKEN_FIELD:
			type_id = STTYPE_FIELD;
			break;
		default:
			g_assert_not_reached();
	}

	stnode_init(df_lval, type_id, data);
	return token;
}

static int
set_lval_int(int token, char *s)
{
	sttype_id_t	type_id = STTYPE_UNINITIALIZED;
	guint32		val;

	if (!str_to_guint32(s, &val)) {
		return 0;
	}	

	switch (token) {
		case TOKEN_INTEGER:
			type_id = STTYPE_INTEGER;
			break;
		default:
			g_assert_not_reached();
	}

	stnode_init_int(df_lval, type_id, val);
	return token;
}


static gboolean
str_to_guint32(char *s, guint32* pint)
{
	char    *endptr;
	guint32	integer;

	integer = strtoul(s, &endptr, 0);

	if (endptr == s || *endptr != '\0') {
		/* This isn't a valid number. */
		dfilter_fail("\"%s\" is not a valid number.", s);
		return FALSE;
	}
	if (errno == ERANGE) {
		if (integer == ULONG_MAX) {
			dfilter_fail("\"%s\" causes an integer overflow.", s);
		}
		else {
			dfilter_fail("\"%s\" is not an integer.", s);
		}
		return FALSE;
	}

	*pint = integer;
	return TRUE;
}

#include <lemonflex-tail.inc>