The latest patch used the fields
[obnox/wireshark/wip.git] / dftest.c
1 /* dftest.c.c
2  *
3  * $Id$
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * Shows display filter byte-code, for debugging dfilter routines.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <locale.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #ifdef NEED_STRERROR_H
37 #include "strerror.h"
38 #endif
39
40 #include <glib.h>
41 #include <epan/epan.h>
42
43 #include <epan/timestamp.h>
44 #include <epan/plugins.h>
45 #include <epan/filesystem.h>
46 #include <epan/prefs.h>
47 #include "util.h"
48 #include "epan/dfilter/dfilter.h"
49 #include "register.h"
50
51 packet_info     pi;
52
53 static void failure_message(const char *msg_format, va_list ap);
54 static void open_failure_message(const char *filename, int err,
55     gboolean for_writing);
56 static void read_failure_message(const char *filename, int err);
57
58 int
59 main(int argc, char **argv)
60 {
61         char            *text;
62         char            *gpf_path, *pf_path;
63         int             gpf_open_errno, gpf_read_errno;
64         int             pf_open_errno, pf_read_errno;
65         e_prefs         *prefs;
66         dfilter_t       *df;
67
68         set_timestamp_setting(TS_RELATIVE);
69
70         /* register all dissectors; we must do this before checking for the
71         "-g" flag, as the "-g" flag dumps a list of fields registered
72         by the dissectors, and we must do it before we read the preferences,
73         in case any dissectors register preferences. */
74         epan_init(PLUGIN_DIR,register_all_protocols,
75                   register_all_protocol_handoffs,
76                   failure_message, open_failure_message, read_failure_message);
77
78         /* now register the preferences for any non-dissector modules.
79         we must do that before we read the preferences as well. */
80         prefs_register_modules();
81
82         /* set the c-language locale to the native environment. */
83         setlocale(LC_ALL, "");
84
85         prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
86             &pf_open_errno, &pf_read_errno, &pf_path);
87         if (gpf_path != NULL) {
88                 if (gpf_open_errno != 0) {
89                         fprintf(stderr,
90                             "can't open global preferences file \"%s\": %s.\n",
91                             pf_path, strerror(gpf_open_errno));
92                 }
93                 if (gpf_read_errno != 0) {
94                         fprintf(stderr,
95                             "I/O error reading global preferences file \"%s\": %s.\n",
96                             pf_path, strerror(gpf_read_errno));
97                 }
98         }
99         if (pf_path != NULL) {
100                 if (pf_open_errno != 0) {
101                         fprintf(stderr,
102                             "can't open your preferences file \"%s\": %s.\n",
103                             pf_path, strerror(pf_open_errno));
104                 }
105                 if (pf_read_errno != 0) {
106                         fprintf(stderr,
107                             "I/O error reading your preferences file \"%s\": %s.\n",
108                             pf_path, strerror(pf_read_errno));
109                 }
110         }
111
112         /* notify all registered modules that have had any of their preferences
113         changed either from one of the preferences file or from the command
114         line that its preferences have changed. */
115         prefs_apply_all();
116
117         /* Check for filter on command line */
118         if (argc <= 1) {
119                 fprintf(stderr, "Usage: dftest filter\n");
120                 exit(1);
121         }
122
123         /* Get filter text */
124         text = get_args_as_string(argc, argv, 1);
125
126         printf("Filter: \"%s\"\n", text);
127
128         /* Compile it */
129         if (!dfilter_compile(text, &df)) {
130                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
131                 epan_cleanup();
132                 exit(2);
133         }
134         printf("dfilter ptr = 0x%08x\n", GPOINTER_TO_INT(df));
135
136         printf("\n\n");
137
138         dfilter_dump(df);
139
140         epan_cleanup();
141         exit(0);
142 }
143
144 /*
145  * General errors are reported with an console message in "dftest".
146  */
147 static void
148 failure_message(const char *msg_format, va_list ap)
149 {
150         fprintf(stderr, "dftest: ");
151         vfprintf(stderr, msg_format, ap);
152         fprintf(stderr, "\n");
153 }
154
155 /*
156  * Open/create errors are reported with an console message in "dftest".
157  */
158 static void
159 open_failure_message(const char *filename, int err, gboolean for_writing)
160 {
161         fprintf(stderr, "dftest: ");
162         fprintf(stderr, file_open_error_message(err, for_writing), filename);
163         fprintf(stderr, "\n");
164 }
165
166 /*
167  * Read errors are reported with an console message in "dftest".
168  */
169 static void
170 read_failure_message(const char *filename, int err)
171 {
172         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
173             filename, strerror(err));
174 }