Fix for bug 3079:
[obnox/wireshark/wip.git] / dftest.c
1 /* dftest.c.c
2  *
3  * $Id$
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
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 <wsutil/privileges.h>
47 #include <epan/prefs.h>
48 #include "util.h"
49 #include "epan/dfilter/dfilter.h"
50 #include "register.h"
51
52 packet_info     pi;
53
54 static void failure_message(const char *msg_format, va_list ap);
55 static void open_failure_message(const char *filename, int err,
56     gboolean for_writing);
57 static void read_failure_message(const char *filename, int err);
58
59 int
60 main(int argc, char **argv)
61 {
62         char            *init_progfile_dir_error;
63         char            *text;
64         char            *gpf_path, *pf_path;
65         int             gpf_open_errno, gpf_read_errno;
66         int             pf_open_errno, pf_read_errno;
67         e_prefs         *prefs;
68         dfilter_t       *df;
69
70         /*
71          * Get credential information for later use.
72          */
73         get_credential_info();
74
75         /*
76          * Attempt to get the pathname of the executable file.
77          */
78         init_progfile_dir_error = init_progfile_dir(argv[0]);
79         if (init_progfile_dir_error != NULL) {
80                 fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
81                     init_progfile_dir_error);
82         }
83
84         timestamp_set_type(TS_RELATIVE);
85
86         /* Register all dissectors; we must do this before checking for the
87            "-g" flag, as the "-g" flag dumps a list of fields registered
88            by the dissectors, and we must do it before we read the preferences,
89            in case any dissectors register preferences. */
90         epan_init(register_all_protocols,
91                   register_all_protocol_handoffs, NULL, NULL,
92                   failure_message, open_failure_message, read_failure_message);
93
94         /* now register the preferences for any non-dissector modules.
95         we must do that before we read the preferences as well. */
96         prefs_register_modules();
97
98         /* set the c-language locale to the native environment. */
99         setlocale(LC_ALL, "");
100
101         prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
102             &pf_open_errno, &pf_read_errno, &pf_path);
103         if (gpf_path != NULL) {
104                 if (gpf_open_errno != 0) {
105                         fprintf(stderr,
106                             "can't open global preferences file \"%s\": %s.\n",
107                             pf_path, strerror(gpf_open_errno));
108                 }
109                 if (gpf_read_errno != 0) {
110                         fprintf(stderr,
111                             "I/O error reading global preferences file \"%s\": %s.\n",
112                             pf_path, strerror(gpf_read_errno));
113                 }
114         }
115         if (pf_path != NULL) {
116                 if (pf_open_errno != 0) {
117                         fprintf(stderr,
118                             "can't open your preferences file \"%s\": %s.\n",
119                             pf_path, strerror(pf_open_errno));
120                 }
121                 if (pf_read_errno != 0) {
122                         fprintf(stderr,
123                             "I/O error reading your preferences file \"%s\": %s.\n",
124                             pf_path, strerror(pf_read_errno));
125                 }
126         }
127
128         /* notify all registered modules that have had any of their preferences
129         changed either from one of the preferences file or from the command
130         line that its preferences have changed. */
131         prefs_apply_all();
132
133         /* Check for filter on command line */
134         if (argc <= 1) {
135                 fprintf(stderr, "Usage: dftest <filter>\n");
136                 exit(1);
137         }
138
139         /* Get filter text */
140         text = get_args_as_string(argc, argv, 1);
141
142         printf("Filter: \"%s\"\n", text);
143
144         /* Compile it */
145         if (!dfilter_compile(text, &df)) {
146                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
147                 epan_cleanup();
148                 exit(2);
149         }
150         printf("dfilter ptr = 0x%08x\n", GPOINTER_TO_INT(df));
151
152         printf("\n\n");
153
154         if (df == NULL)
155                 printf("Filter is empty\n");
156         else
157                 dfilter_dump(df);
158
159         epan_cleanup();
160         exit(0);
161 }
162
163 /*
164  * General errors are reported with an console message in "dftest".
165  */
166 static void
167 failure_message(const char *msg_format, va_list ap)
168 {
169         fprintf(stderr, "dftest: ");
170         vfprintf(stderr, msg_format, ap);
171         fprintf(stderr, "\n");
172 }
173
174 /*
175  * Open/create errors are reported with an console message in "dftest".
176  */
177 static void
178 open_failure_message(const char *filename, int err, gboolean for_writing)
179 {
180         fprintf(stderr, "dftest: ");
181         fprintf(stderr, file_open_error_message(err, for_writing), filename);
182         fprintf(stderr, "\n");
183 }
184
185 /*
186  * Read errors are reported with an console message in "dftest".
187  */
188 static void
189 read_failure_message(const char *filename, int err)
190 {
191         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
192             filename, strerror(err));
193 }