dftest: load plugins so plugin display filters can also be tested.
[jlayton/wireshark.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
64         /*
65          * Get credential information for later use.
66          */
67         init_process_policies();
68
69         /*
70          * Attempt to get the pathname of the executable file.
71          */
72         init_progfile_dir_error = init_progfile_dir(argv[0], main);
73         if (init_progfile_dir_error != NULL) {
74                 fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
75                         init_progfile_dir_error);
76         }
77
78         init_report_err(failure_message, open_failure_message,
79                         read_failure_message, write_failure_message);
80
81         timestamp_set_type(TS_RELATIVE);
82         timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
83
84 #ifdef HAVE_PLUGINS
85         /* Register all the plugin types we have. */
86         epan_register_plugin_types(); /* Types known to libwireshark */
87
88         /* Scan for plugins.  This does *not* call their registration routines;
89            that's done later. */
90         scan_plugins();
91 #endif
92
93         /* Register all dissectors; we must do this before checking for the
94            "-g" flag, as the "-g" flag dumps a list of fields registered
95            by the dissectors, and we must do it before we read the preferences,
96            in case any dissectors register preferences. */
97         epan_init(register_all_protocols, register_all_protocol_handoffs,
98                   NULL, NULL);
99
100         /* set the c-language locale to the native environment. */
101         setlocale(LC_ALL, "");
102
103         read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
104                 &pf_open_errno, &pf_read_errno, &pf_path);
105         if (gpf_path != NULL) {
106                 if (gpf_open_errno != 0) {
107                         fprintf(stderr,
108                                 "can't open global preferences file \"%s\": %s.\n",
109                                 pf_path, g_strerror(gpf_open_errno));
110                 }
111                 if (gpf_read_errno != 0) {
112                         fprintf(stderr,
113                                 "I/O error reading global preferences file \"%s\": %s.\n",
114                                 pf_path, g_strerror(gpf_read_errno));
115                 }
116         }
117         if (pf_path != NULL) {
118                 if (pf_open_errno != 0) {
119                         fprintf(stderr,
120                                 "can't open your preferences file \"%s\": %s.\n",
121                                 pf_path, g_strerror(pf_open_errno));
122                 }
123                 if (pf_read_errno != 0) {
124                         fprintf(stderr,
125                                 "I/O error reading your preferences file \"%s\": %s.\n",
126                                 pf_path, g_strerror(pf_read_errno));
127                 }
128         }
129
130         /* notify all registered modules that have had any of their preferences
131         changed either from one of the preferences file or from the command
132         line that its preferences have changed. */
133         prefs_apply_all();
134
135         /* Check for filter on command line */
136         if (argc <= 1) {
137                 fprintf(stderr, "Usage: dftest <filter>\n");
138                 exit(1);
139         }
140
141         /* Get filter text */
142         text = get_args_as_string(argc, argv, 1);
143
144         printf("Filter: \"%s\"\n", text);
145
146         /* Compile it */
147         if (!dfilter_compile(text, &df)) {
148                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
149                 epan_cleanup();
150                 exit(2);
151         }
152
153         printf("\n");
154
155         if (df == NULL)
156                 printf("Filter is empty\n");
157         else
158                 dfilter_dump(df);
159
160         dfilter_free(df);
161         epan_cleanup();
162         exit(0);
163 }
164
165 /*
166  * General errors are reported with an console message in "dftest".
167  */
168 static void
169 failure_message(const char *msg_format, va_list ap)
170 {
171         fprintf(stderr, "dftest: ");
172         vfprintf(stderr, msg_format, ap);
173         fprintf(stderr, "\n");
174 }
175
176 /*
177  * Open/create errors are reported with an console message in "dftest".
178  */
179 static void
180 open_failure_message(const char *filename, int err, gboolean for_writing)
181 {
182         fprintf(stderr, "dftest: ");
183         fprintf(stderr, file_open_error_message(err, for_writing), filename);
184         fprintf(stderr, "\n");
185 }
186
187 /*
188  * Read errors are reported with an console message in "dftest".
189  */
190 static void
191 read_failure_message(const char *filename, int err)
192 {
193         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
194                 filename, g_strerror(err));
195 }
196
197 /*
198  * Write errors are reported with an console message in "dftest".
199  */
200 static void
201 write_failure_message(const char *filename, int err)
202 {
203         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
204                 filename, g_strerror(err));
205 }
206
207 /*
208  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
209  *
210  * Local variables:
211  * c-basic-offset: 8
212  * tab-width: 8
213  * indent-tabs-mode: t
214  * End:
215  *
216  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
217  * :indentSize=8:tabSize=8:noTabs=false:
218  */