Update to WinSparkle 0.5.3.
[metze/wireshark/wip.git] / dftest.c
1 /* dftest.c
2  * Shows display filter byte-code, for debugging dfilter routines.
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <locale.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include <glib.h>
32
33 #include <epan/epan.h>
34 #include <epan/timestamp.h>
35 #include <epan/prefs.h>
36 #include <epan/dfilter/dfilter.h>
37
38 #ifdef HAVE_PLUGINS
39 #include <wsutil/plugins.h>
40 #endif
41 #include <wsutil/filesystem.h>
42 #include <wsutil/privileges.h>
43 #include <wsutil/report_err.h>
44
45 #include "ui/util.h"
46 #include "register.h"
47
48 static void failure_message(const char *msg_format, va_list ap);
49 static void open_failure_message(const char *filename, int err,
50         gboolean for_writing);
51 static void read_failure_message(const char *filename, int err);
52 static void write_failure_message(const char *filename, int err);
53
54 int
55 main(int argc, char **argv)
56 {
57         char            *init_progfile_dir_error;
58         char            *text;
59         char            *gpf_path, *pf_path;
60         int             gpf_open_errno, gpf_read_errno;
61         int             pf_open_errno, pf_read_errno;
62         dfilter_t       *df;
63         gchar           *err_msg;
64
65         /*
66          * Get credential information for later use.
67          */
68         init_process_policies();
69
70         /*
71          * Attempt to get the pathname of the directory containing the
72          * executable file.
73          */
74         init_progfile_dir_error = init_progfile_dir(argv[0], main);
75         if (init_progfile_dir_error != NULL) {
76                 fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
77                         init_progfile_dir_error);
78                 g_free(init_progfile_dir_error);
79         }
80
81         init_report_err(failure_message, open_failure_message,
82                         read_failure_message, write_failure_message);
83
84         timestamp_set_type(TS_RELATIVE);
85         timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
86
87 #ifdef HAVE_PLUGINS
88         /* Register all the plugin types we have. */
89         epan_register_plugin_types(); /* Types known to libwireshark */
90
91         /* Scan for plugins.  This does *not* call their registration routines;
92            that's done later. */
93         scan_plugins(REPORT_LOAD_FAILURE);
94 #endif
95
96         /* Register all dissectors; we must do this before checking for the
97            "-g" flag, as the "-g" flag dumps a list of fields registered
98            by the dissectors, and we must do it before we read the preferences,
99            in case any dissectors register preferences. */
100         if (!epan_init(register_all_protocols, register_all_protocol_handoffs,
101             NULL, NULL))
102                 return 2;
103
104         /* set the c-language locale to the native environment. */
105         setlocale(LC_ALL, "");
106
107         read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
108                 &pf_open_errno, &pf_read_errno, &pf_path);
109         if (gpf_path != NULL) {
110                 if (gpf_open_errno != 0) {
111                         fprintf(stderr,
112                                 "can't open global preferences file \"%s\": %s.\n",
113                                 pf_path, g_strerror(gpf_open_errno));
114                 }
115                 if (gpf_read_errno != 0) {
116                         fprintf(stderr,
117                                 "I/O error reading global preferences file \"%s\": %s.\n",
118                                 pf_path, g_strerror(gpf_read_errno));
119                 }
120         }
121         if (pf_path != NULL) {
122                 if (pf_open_errno != 0) {
123                         fprintf(stderr,
124                                 "can't open your preferences file \"%s\": %s.\n",
125                                 pf_path, g_strerror(pf_open_errno));
126                 }
127                 if (pf_read_errno != 0) {
128                         fprintf(stderr,
129                                 "I/O error reading your preferences file \"%s\": %s.\n",
130                                 pf_path, g_strerror(pf_read_errno));
131                 }
132         }
133
134         /* notify all registered modules that have had any of their preferences
135         changed either from one of the preferences file or from the command
136         line that its preferences have changed. */
137         prefs_apply_all();
138
139         /* Check for filter on command line */
140         if (argc <= 1) {
141                 fprintf(stderr, "Usage: dftest <filter>\n");
142                 exit(1);
143         }
144
145         /* Get filter text */
146         text = get_args_as_string(argc, argv, 1);
147
148         printf("Filter: \"%s\"\n", text);
149
150         /* Compile it */
151         if (!dfilter_compile(text, &df, &err_msg)) {
152                 fprintf(stderr, "dftest: %s\n", err_msg);
153                 g_free(err_msg);
154                 epan_cleanup();
155                 exit(2);
156         }
157
158         printf("\n");
159
160         if (df == NULL)
161                 printf("Filter is empty\n");
162         else
163                 dfilter_dump(df);
164
165         dfilter_free(df);
166         epan_cleanup();
167         exit(0);
168 }
169
170 /*
171  * General errors are reported with an console message in "dftest".
172  */
173 static void
174 failure_message(const char *msg_format, va_list ap)
175 {
176         fprintf(stderr, "dftest: ");
177         vfprintf(stderr, msg_format, ap);
178         fprintf(stderr, "\n");
179 }
180
181 /*
182  * Open/create errors are reported with an console message in "dftest".
183  */
184 static void
185 open_failure_message(const char *filename, int err, gboolean for_writing)
186 {
187         fprintf(stderr, "dftest: ");
188         fprintf(stderr, file_open_error_message(err, for_writing), filename);
189         fprintf(stderr, "\n");
190 }
191
192 /*
193  * Read errors are reported with an console message in "dftest".
194  */
195 static void
196 read_failure_message(const char *filename, int err)
197 {
198         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
199                 filename, g_strerror(err));
200 }
201
202 /*
203  * Write errors are reported with an console message in "dftest".
204  */
205 static void
206 write_failure_message(const char *filename, int err)
207 {
208         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
209                 filename, g_strerror(err));
210 }
211
212 /*
213  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
214  *
215  * Local variables:
216  * c-basic-offset: 8
217  * tab-width: 8
218  * indent-tabs-mode: t
219  * End:
220  *
221  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
222  * :indentSize=8:tabSize=8:noTabs=false:
223  */