Merge with the FreeRADIUS version.
[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_message.h>
44
45 #include <wiretap/wtap.h>
46
47 #include "ui/util.h"
48 #include "register.h"
49
50 static void failure_warning_message(const char *msg_format, va_list ap);
51 static void open_failure_message(const char *filename, int err,
52         gboolean for_writing);
53 static void read_failure_message(const char *filename, int err);
54 static void write_failure_message(const char *filename, int err);
55
56 int
57 main(int argc, char **argv)
58 {
59         char            *init_progfile_dir_error;
60         char            *text;
61         dfilter_t       *df;
62         gchar           *err_msg;
63
64         /*
65          * Get credential information for later use.
66          */
67         init_process_policies();
68
69         /*
70          * Attempt to get the pathname of the directory containing the
71          * executable file.
72          */
73         init_progfile_dir_error = init_progfile_dir(argv[0], main);
74         if (init_progfile_dir_error != NULL) {
75                 fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
76                         init_progfile_dir_error);
77                 g_free(init_progfile_dir_error);
78         }
79
80         init_report_message(failure_warning_message, failure_warning_message,
81                             open_failure_message, read_failure_message,
82                             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         wtap_init();
97
98         /* Register all dissectors; we must do this before checking for the
99            "-g" flag, as the "-g" flag dumps a list of fields registered
100            by the dissectors, and we must do it before we read the preferences,
101            in case any dissectors register preferences. */
102         if (!epan_init(register_all_protocols, register_all_protocol_handoffs,
103             NULL, NULL))
104                 return 2;
105
106         /* set the c-language locale to the native environment. */
107         setlocale(LC_ALL, "");
108
109         /* Load libwireshark settings from the current profile. */
110         epan_load_settings();
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, &err_msg)) {
130                 fprintf(stderr, "dftest: %s\n", err_msg);
131                 g_free(err_msg);
132                 epan_cleanup();
133                 exit(2);
134         }
135
136         printf("\n");
137
138         if (df == NULL)
139                 printf("Filter is empty\n");
140         else
141                 dfilter_dump(df);
142
143         dfilter_free(df);
144         epan_cleanup();
145         exit(0);
146 }
147
148 /*
149  * General errors and warnings are reported with an console message
150  * in "dftest".
151  */
152 static void
153 failure_warning_message(const char *msg_format, va_list ap)
154 {
155         fprintf(stderr, "dftest: ");
156         vfprintf(stderr, msg_format, ap);
157         fprintf(stderr, "\n");
158 }
159
160 /*
161  * Open/create errors are reported with an console message in "dftest".
162  */
163 static void
164 open_failure_message(const char *filename, int err, gboolean for_writing)
165 {
166         fprintf(stderr, "dftest: ");
167         fprintf(stderr, file_open_error_message(err, for_writing), filename);
168         fprintf(stderr, "\n");
169 }
170
171 /*
172  * Read errors are reported with an console message in "dftest".
173  */
174 static void
175 read_failure_message(const char *filename, int err)
176 {
177         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
178                 filename, g_strerror(err));
179 }
180
181 /*
182  * Write errors are reported with an console message in "dftest".
183  */
184 static void
185 write_failure_message(const char *filename, int err)
186 {
187         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
188                 filename, g_strerror(err));
189 }
190
191 /*
192  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
193  *
194  * Local variables:
195  * c-basic-offset: 8
196  * tab-width: 8
197  * indent-tabs-mode: t
198  * End:
199  *
200  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
201  * :indentSize=8:tabSize=8:noTabs=false:
202  */