Have the per-capture-file-type open routines "wtap_open_offline()" calls
[obnox/wireshark/wip.git] / dfilter-grammar.y
1 %{
2
3 /* dfilter-grammar.y
4  * Parser for display filters
5  *
6  * $Id: dfilter-grammar.y,v 1.10 1999/08/14 06:24:26 gram Exp $
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@zing.org>
10  * Copyright 1998 Gerald Combs
11  *
12  * 
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.
17  * 
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.
22  * 
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
34 #endif
35
36 #ifdef HAVE_NETINET_IN_H
37 # include <netinet/in.h>
38 #endif
39
40 #ifndef __GLIB_H__
41 #include <glib.h>
42 #endif
43
44 #include <string.h>
45
46 #ifndef _STDLIB_H
47 #include <stdlib.h>
48 #endif
49
50 #ifndef __PROTO_H__
51 #include "proto.h"
52 #endif
53
54 #ifndef __PACKET_H__
55 #include "packet.h"
56 #endif
57
58 #ifndef __DFILTER_H__
59 #include "dfilter.h"
60 #endif
61
62 #include "dfilter-int.h"
63
64 #ifndef __RESOLV_H__
65 #include "resolv.h"
66 #endif
67
68 static GNode* dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2);
69 static GNode* dfilter_mknode_unary(int operand, GNode *n2);
70 static GNode* dfilter_mknode_numeric_variable(gint id);
71 static GNode* dfilter_mknode_numeric_value(guint32 val);
72 static GNode* dfilter_mknode_ether_value(guint8*);
73 static GNode* dfilter_mknode_ether_variable(gint id);
74 static GNode* dfilter_mknode_ipxnet_value(guint32);
75 static GNode* dfilter_mknode_ipxnet_variable(gint id);
76 static GNode* dfilter_mknode_ipv4_value(char *host);
77 static GNode* dfilter_mknode_ipv4_variable(gint id);
78 static GNode* dfilter_mknode_existence(gint id);
79 static GNode* dfilter_mknode_bytes_value(GByteArray *barray);
80 static GNode* dfilter_mknode_bytes_variable(gint id, gint offset, guint length);
81 static GNode* dfilter_mknode_boolean_value(gint truth_value);
82 static GNode* dfilter_mknode_boolean_variable(gint id);
83
84 static guint32 string_to_value(char *s);
85
86 /* This is the dfilter we're currently processing. It's how
87  * dfilter_compile communicates with us.
88  */
89 dfilter *global_df = NULL;;
90
91 %}
92
93 %union {
94         gint            operand;        /* logical, relation, alternation */
95         gint            variable;
96         GNode*          node;
97         gchar*          id;
98         GByteArray*     bytes;
99         guint8          ether[6];
100         struct {
101                 gint    offset;
102                 guint   length;
103         } byte_range;
104 }
105
106 %type <node>    statement expression relation
107 %type <node>    numeric_value numeric_variable
108 %type <node>    ether_value ether_variable
109 %type <node>    ipxnet_value ipxnet_variable
110 %type <node>    ipv4_value ipv4_variable
111 %type <node>    variable_name
112 %type <node>    bytes_value bytes_variable
113 %type <node>    boolean_value boolean_variable
114
115 %type <operand> numeric_relation
116 %type <operand> equality_relation
117 %type <operand> bytes_relation
118
119 %type <variable>        any_variable_type
120
121 %token <variable>       T_FT_UINT8
122 %token <variable>       T_FT_UINT16
123 %token <variable>       T_FT_UINT32
124 %token <variable>       T_FT_ETHER
125 %token <variable>       T_FT_IPv4
126 %token <variable>       T_FT_NONE
127 %token <variable>       T_FT_BYTES
128 %token <variable>       T_FT_BOOLEAN
129 %token <variable>       T_FT_STRING
130 %token <variable>       T_FT_IPXNET
131
132 %token <id>             T_VAL_UNQUOTED_STRING
133 %token <ether>          T_VAL_ETHER
134 %token <bytes>          T_VAL_BYTES
135 %token <byte_range>     T_VAL_BYTE_RANGE
136
137 %token <operand>        TOK_AND TOK_OR TOK_NOT TOK_XOR
138 %token <operand>        TOK_EQ TOK_NE TOK_GT TOK_GE TOK_LT TOK_LE
139 %token <operand>        TOK_TRUE TOK_FALSE
140
141 %left TOK_AND
142 %left TOK_OR
143 %left TOK_XOR
144 %nonassoc TOK_NOT
145
146 %%
147
148 statement: expression
149                 {
150                         global_df->dftree = $1;
151                 }
152         |       /* NULL */ { global_df->dftree = NULL; }
153         ;
154
155 expression:     '(' expression ')' { $$ = $2; }
156         |       expression TOK_AND expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
157         |       expression TOK_OR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
158         |       expression TOK_XOR expression { $$ = dfilter_mknode_join($1, logical, $2, $3); }
159         |       TOK_NOT expression { $$ = dfilter_mknode_unary(TOK_NOT, $2); }
160         |       relation { $$ = $1; }
161         |       variable_name { $$ = $1; }
162         ;
163
164 relation:       numeric_variable numeric_relation numeric_value
165                 {
166                         $$ = dfilter_mknode_join($1, relation, $2, $3);
167                 }
168         |       numeric_variable numeric_relation numeric_variable
169                 {
170                         $$ = dfilter_mknode_join($1, relation, $2, $3);
171                 }
172
173         |       ether_variable equality_relation ether_value
174                 {
175                         $$ = dfilter_mknode_join($1, relation, $2, $3);
176                 }
177         |       ether_variable equality_relation ether_variable
178                 {
179                         $$ = dfilter_mknode_join($1, relation, $2, $3);
180                 }
181
182         |       ipxnet_variable equality_relation ipxnet_value
183                 {
184                         $$ = dfilter_mknode_join($1, relation, $2, $3);
185                 }
186         |       ipxnet_variable equality_relation ipxnet_variable
187                 {
188                         $$ = dfilter_mknode_join($1, relation, $2, $3);
189                 }
190
191
192         |       ipv4_variable numeric_relation ipv4_value
193                 {
194                         $$ = dfilter_mknode_join($1, relation, $2, $3);
195                 }
196         |       ipv4_variable numeric_relation ipv4_variable
197                 {
198                         $$ = dfilter_mknode_join($1, relation, $2, $3);
199                 }
200
201         |       bytes_variable bytes_relation bytes_value
202                 {
203                         $$ = dfilter_mknode_join($1, relation, $2, $3);
204                 }
205         |       bytes_variable bytes_relation bytes_variable
206                 {
207                         $$ = dfilter_mknode_join($1, relation, $2, $3);
208                 }
209
210         |       boolean_variable equality_relation boolean_value
211                 {
212                         $$ = dfilter_mknode_join($1, relation, $2, $3);
213                 }
214         |       boolean_variable equality_relation boolean_variable
215                 {
216                         $$ = dfilter_mknode_join($1, relation, $2, $3);
217                 }
218
219         ;
220
221
222 numeric_value:  T_VAL_UNQUOTED_STRING
223         {
224                 $$ = dfilter_mknode_numeric_value(string_to_value($1));
225                 g_free($1);
226          }
227         ;
228
229 ether_value:    T_VAL_ETHER
230                 {
231                         $$ = dfilter_mknode_ether_value($1);
232                 }
233         ;
234
235 ipxnet_value:   T_VAL_UNQUOTED_STRING
236                 {
237                         $$ = dfilter_mknode_ipxnet_value(string_to_value($1));
238                 }
239         ;
240
241 ipv4_value:     T_VAL_UNQUOTED_STRING
242         {
243                 $$ = dfilter_mknode_ipv4_value($1);
244                 g_free($1);
245         }
246         ;
247
248 bytes_value:    T_VAL_BYTES
249         {                                                               /* 2 - 5, or > 6 bytes */
250                  $$ = dfilter_mknode_bytes_value($1);
251         }
252
253         |       T_VAL_UNQUOTED_STRING
254         {                                                               /* one or 4 bytes */
255                 GByteArray      *barray;
256
257                 /* the next function appends to list_of_byte_arrays for me */
258                 barray = byte_str_to_guint8_array($1);
259                 $$ = dfilter_mknode_bytes_value(barray);
260                 g_free($1);
261         }
262
263         |       T_VAL_ETHER
264         {                                                               /* 6 bytes */
265                 GByteArray      *barray = g_byte_array_new();
266
267                 global_df->list_of_byte_arrays = g_slist_append(global_df->list_of_byte_arrays, barray);
268                 g_byte_array_append(barray, $1, 6);
269                 $$ = dfilter_mknode_bytes_value(barray);
270         }
271         ;
272
273
274 boolean_value:  TOK_TRUE                { $$ = dfilter_mknode_boolean_value($1); }
275         |       TOK_FALSE               { $$ = dfilter_mknode_boolean_value($1); }
276         ;
277
278
279 numeric_variable:       T_FT_UINT8      { $$ = dfilter_mknode_numeric_variable($1); }
280         |               T_FT_UINT16     { $$ = dfilter_mknode_numeric_variable($1); }
281         |               T_FT_UINT32     { $$ = dfilter_mknode_numeric_variable($1); }
282         ;
283
284 ether_variable:         T_FT_ETHER      { $$ = dfilter_mknode_ether_variable($1); }
285         ;
286
287 ipxnet_variable:        T_FT_IPXNET     { $$ = dfilter_mknode_ipxnet_variable($1); }
288         ;
289
290 ipv4_variable:          T_FT_IPv4       { $$ = dfilter_mknode_ipv4_variable($1); }
291         ;
292
293 variable_name:          any_variable_type       { $$ = dfilter_mknode_existence($1); }
294         ;
295
296 bytes_variable:         any_variable_type T_VAL_BYTE_RANGE
297                 {
298                         $$ = dfilter_mknode_bytes_variable($1, $2.offset, $2.length);
299                 }
300         ;
301
302 boolean_variable:       T_FT_BOOLEAN    { $$ = dfilter_mknode_boolean_variable($1); }
303         ;
304
305 any_variable_type:      T_FT_UINT8 { $$ = $1; }
306         |               T_FT_UINT16 { $$ = $1; }
307         |               T_FT_UINT32 { $$ = $1; }
308         |               T_FT_ETHER { $$ = $1; }
309         |               T_FT_IPv4 { $$ = $1; }
310         |               T_FT_NONE { $$ = $1; }
311         |               T_FT_BYTES { $$ = $1; }
312         |               T_FT_BOOLEAN { $$ = $1; }
313         |               T_FT_STRING { $$ = $1; }
314         ;
315
316 numeric_relation:       TOK_EQ { $$ = TOK_EQ; }
317         |               TOK_NE { $$ = TOK_NE; }
318         |               TOK_GT { $$ = TOK_GT; }
319         |               TOK_GE { $$ = TOK_GE; }
320         |               TOK_LT { $$ = TOK_LT; }
321         |               TOK_LE { $$ = TOK_LE; }
322         ;
323
324 equality_relation:      TOK_EQ { $$ = TOK_EQ; }
325         |               TOK_NE { $$ = TOK_NE; }
326         ;
327
328 bytes_relation:         TOK_EQ { $$ = TOK_EQ; }
329         |               TOK_NE { $$ = TOK_NE; }
330         |               TOK_GT { $$ = TOK_GT; }
331         |               TOK_LT { $$ = TOK_LT; }
332         ;
333
334 %%
335
336 static GNode*
337 dfilter_mknode_join(GNode *n1, enum node_type ntype, int operand, GNode *n2)
338 {
339         dfilter_node    *node_root;
340         GNode           *gnode_root;
341
342         node_root = g_mem_chunk_alloc(global_df->node_memchunk);
343         node_root->ntype = ntype;
344         node_root->elem_size = 0;
345         node_root->fill_array_func = NULL;
346         node_root->check_relation_func = NULL;
347         if (ntype == relation) {
348                 node_root->value.relation = operand;
349         }
350         else if (ntype == logical) {
351                 node_root->value.logical = operand;
352         }
353         else {
354                 g_assert_not_reached();
355         }
356
357         gnode_root = g_node_new(node_root);
358         g_node_append(gnode_root, n1);
359         g_node_append(gnode_root, n2);
360
361         return gnode_root;
362 }
363
364 static GNode*
365 dfilter_mknode_unary(int operand, GNode *n2)
366 {
367         dfilter_node    *node_root;
368         GNode           *gnode_root;
369
370         node_root = g_mem_chunk_alloc(global_df->node_memchunk);
371         node_root->ntype = logical;
372         node_root->value.logical = operand;
373         node_root->elem_size = 0;
374         node_root->fill_array_func = NULL;
375         node_root->check_relation_func = NULL;
376
377         gnode_root = g_node_new(node_root);
378         g_node_append(gnode_root, n2);
379
380         return gnode_root;
381 }
382
383
384 static GNode*
385 dfilter_mknode_numeric_variable(gint id)
386 {
387         dfilter_node    *node;
388         GNode           *gnode;
389
390         node = g_mem_chunk_alloc(global_df->node_memchunk);
391         node->ntype = variable;
392         node->elem_size = sizeof(guint32);
393         node->fill_array_func = fill_array_numeric_variable;
394         node->check_relation_func = check_relation_numeric;
395         node->value.variable = id;
396         gnode = g_node_new(node);
397
398         return gnode;
399 }
400
401 static GNode*
402 dfilter_mknode_ether_variable(gint id)
403 {
404         dfilter_node    *node;
405         GNode           *gnode;
406
407         node = g_mem_chunk_alloc(global_df->node_memchunk);
408         node->ntype = variable;
409         node->elem_size = sizeof(guint8) * 6;
410         node->fill_array_func = fill_array_ether_variable;
411         node->check_relation_func = check_relation_ether;
412         node->value.variable = id;
413         gnode = g_node_new(node);
414
415         return gnode;
416 }
417
418 static GNode*
419 dfilter_mknode_ipxnet_variable(gint id)
420 {
421         dfilter_node    *node;
422         GNode           *gnode;
423
424         node = g_mem_chunk_alloc(global_df->node_memchunk);
425         node->ntype = variable;
426         node->elem_size = sizeof(guint8) * 4;
427         node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
428         node->check_relation_func = check_relation_numeric; /* cheating ! */
429         node->value.variable = id;
430         gnode = g_node_new(node);
431
432         return gnode;
433 }
434
435 static GNode*
436 dfilter_mknode_ipv4_variable(gint id)
437 {
438         dfilter_node    *node;
439         GNode           *gnode;
440
441         node = g_mem_chunk_alloc(global_df->node_memchunk);
442         node->ntype = variable;
443         node->elem_size = sizeof(guint32);
444         node->fill_array_func = fill_array_numeric_variable; /* cheating ! */
445         node->check_relation_func = check_relation_numeric; /* cheating ! */
446         node->value.variable = id;
447         gnode = g_node_new(node);
448
449         return gnode;
450 }
451
452 static GNode*
453 dfilter_mknode_bytes_variable(gint id, gint offset, guint length)
454 {
455         dfilter_node    *node;
456         GNode           *gnode;
457
458         node = g_mem_chunk_alloc(global_df->node_memchunk);
459         node->ntype = variable;
460         node->elem_size = sizeof(GByteArray*);
461         node->fill_array_func = fill_array_bytes_variable;
462         node->check_relation_func = check_relation_bytes;
463         node->value.variable = id;
464         node->offset = offset;
465         node->length = length;
466         gnode = g_node_new(node);
467
468         return gnode;
469 }
470
471 static GNode*
472 dfilter_mknode_boolean_variable(gint id)
473 {
474         dfilter_node    *node;
475         GNode           *gnode;
476
477         node = g_mem_chunk_alloc(global_df->node_memchunk);
478         node->ntype = variable;
479         node->elem_size = sizeof(guint32);
480         node->fill_array_func = fill_array_boolean_variable; /* cheating ! */
481         node->check_relation_func = check_relation_boolean; /* cheating ! */
482         node->value.variable = id;
483         gnode = g_node_new(node);
484
485         return gnode;
486 }
487
488 static GNode*
489 dfilter_mknode_numeric_value(guint32 val)
490 {
491         dfilter_node    *node;
492         GNode           *gnode;
493
494         node = g_mem_chunk_alloc(global_df->node_memchunk);
495         node->ntype = numeric;
496         node->elem_size = sizeof(guint32);
497         node->fill_array_func = fill_array_numeric_value;
498         node->check_relation_func = check_relation_numeric;
499         node->value.numeric = val;
500         gnode = g_node_new(node);
501
502         return gnode;
503 }
504
505 static GNode*
506 dfilter_mknode_ether_value(guint8 *ether_bytes)
507 {
508         dfilter_node    *node;
509         GNode           *gnode;
510
511         node = g_mem_chunk_alloc(global_df->node_memchunk);
512         node->ntype = ether;
513         node->elem_size = sizeof(guint8) * 6;
514         node->fill_array_func = fill_array_ether_value;
515         node->check_relation_func = check_relation_ether;
516
517         memcpy(&node->value.ether, ether_bytes, 6);
518
519         gnode = g_node_new(node);
520         return gnode;
521 }
522
523 static GNode*
524 dfilter_mknode_ipxnet_value(guint32 ipx_net_val)
525 {
526         dfilter_node    *node;
527         GNode           *gnode;
528
529         node = g_mem_chunk_alloc(global_df->node_memchunk);
530         node->ntype = ipxnet;
531         node->elem_size = sizeof(guint8) * 4;
532         node->fill_array_func = fill_array_numeric_value; /* cheating ! */
533         node->check_relation_func = check_relation_numeric; /* cheating ! */
534         node->value.numeric = ipx_net_val;
535         gnode = g_node_new(node);
536
537         return gnode;
538 }
539
540 static GNode*
541 dfilter_mknode_ipv4_value(char *host)
542 {
543         dfilter_node    *node;
544         GNode           *gnode;
545
546         node = g_mem_chunk_alloc(global_df->node_memchunk);
547         node->ntype = numeric;
548         node->elem_size = sizeof(guint32);
549         node->fill_array_func = fill_array_numeric_value; /* cheating ! */
550         node->check_relation_func = check_relation_numeric; /* cheating ! */
551         node->value.numeric = get_host_ipaddr(host);
552         node->value.numeric = htonl(node->value.numeric);
553         gnode = g_node_new(node);
554
555         return gnode;
556 }
557
558 static GNode*
559 dfilter_mknode_bytes_value(GByteArray *barray)
560 {
561         dfilter_node    *node;
562         GNode           *gnode;
563
564         node = g_mem_chunk_alloc(global_df->node_memchunk);
565         node->ntype = bytes;
566         node->elem_size = sizeof(GByteArray*);
567         node->fill_array_func = fill_array_bytes_value;
568         node->check_relation_func = check_relation_bytes;
569         node->value.bytes = barray;
570         node->offset = G_MAXINT;
571         node->length = barray->len;
572         gnode = g_node_new(node);
573
574         return gnode;
575 }
576
577 static GNode*
578 dfilter_mknode_boolean_value(gint truth_value)
579 {
580         dfilter_node    *node;
581         GNode           *gnode;
582
583         node = g_mem_chunk_alloc(global_df->node_memchunk);
584         node->ntype = numeric;
585         node->elem_size = sizeof(guint32);
586         node->fill_array_func = fill_array_boolean_value;
587         node->check_relation_func = check_relation_boolean;
588         node->value.boolean = truth_value == TOK_TRUE ? TRUE : FALSE;
589         gnode = g_node_new(node);
590
591         return gnode;
592 }
593
594 static guint32
595 string_to_value(char *s)
596 {
597         char    *endptr;
598         guint32 val;
599
600         val = strtoul(s, &endptr, 0);
601         /* I should probably check errno here */
602
603         return (guint32)val;
604 }
605         
606 static GNode*
607 dfilter_mknode_existence(gint id)
608 {
609         dfilter_node    *node;
610         GNode           *gnode;
611
612         node = g_mem_chunk_alloc(global_df->node_memchunk);
613         node->ntype = existence;
614         node->elem_size = sizeof(guint32);
615         node->fill_array_func = NULL;
616         node->check_relation_func = NULL;
617         node->value.variable = id;
618         gnode = g_node_new(node);
619
620         return gnode;
621 }