packet-smb2: dissect SMBDirect Buffer Descriptors
[metze/wireshark/wip.git] / dftest.c
1 /* dftest.c
2  * Shows display filter byte-code, for debugging dfilter routines.
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include "config.h"
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <locale.h>
30 #include <string.h>
31 #include <errno.h>
32
33 #include <glib.h>
34
35 #include <epan/epan.h>
36 #include <epan/timestamp.h>
37 #include <epan/prefs.h>
38 #include <epan/dfilter/dfilter.h>
39
40 #include <wsutil/plugins.h>
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         /* Register all dissectors; we must do this before checking for the
85            "-g" flag, as the "-g" flag dumps a list of fields registered
86            by the dissectors, and we must do it before we read the preferences,
87            in case any dissectors register preferences. */
88         epan_init(register_all_protocols, register_all_protocol_handoffs,
89                   NULL, NULL);
90
91         /* set the c-language locale to the native environment. */
92         setlocale(LC_ALL, "");
93
94         read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
95                 &pf_open_errno, &pf_read_errno, &pf_path);
96         if (gpf_path != NULL) {
97                 if (gpf_open_errno != 0) {
98                         fprintf(stderr,
99                                 "can't open global preferences file \"%s\": %s.\n",
100                                 pf_path, g_strerror(gpf_open_errno));
101                 }
102                 if (gpf_read_errno != 0) {
103                         fprintf(stderr,
104                                 "I/O error reading global preferences file \"%s\": %s.\n",
105                                 pf_path, g_strerror(gpf_read_errno));
106                 }
107         }
108         if (pf_path != NULL) {
109                 if (pf_open_errno != 0) {
110                         fprintf(stderr,
111                                 "can't open your preferences file \"%s\": %s.\n",
112                                 pf_path, g_strerror(pf_open_errno));
113                 }
114                 if (pf_read_errno != 0) {
115                         fprintf(stderr,
116                                 "I/O error reading your preferences file \"%s\": %s.\n",
117                                 pf_path, g_strerror(pf_read_errno));
118                 }
119         }
120
121         /* notify all registered modules that have had any of their preferences
122         changed either from one of the preferences file or from the command
123         line that its preferences have changed. */
124         prefs_apply_all();
125
126         /* Check for filter on command line */
127         if (argc <= 1) {
128                 fprintf(stderr, "Usage: dftest <filter>\n");
129                 exit(1);
130         }
131
132         /* Get filter text */
133         text = get_args_as_string(argc, argv, 1);
134
135         printf("Filter: \"%s\"\n", text);
136
137         /* Compile it */
138         if (!dfilter_compile(text, &df)) {
139                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
140                 epan_cleanup();
141                 exit(2);
142         }
143
144         printf("\n");
145
146         if (df == NULL)
147                 printf("Filter is empty\n");
148         else
149                 dfilter_dump(df);
150
151         dfilter_free(df);
152         epan_cleanup();
153         exit(0);
154 }
155
156 /*
157  * General errors are reported with an console message in "dftest".
158  */
159 static void
160 failure_message(const char *msg_format, va_list ap)
161 {
162         fprintf(stderr, "dftest: ");
163         vfprintf(stderr, msg_format, ap);
164         fprintf(stderr, "\n");
165 }
166
167 /*
168  * Open/create errors are reported with an console message in "dftest".
169  */
170 static void
171 open_failure_message(const char *filename, int err, gboolean for_writing)
172 {
173         fprintf(stderr, "dftest: ");
174         fprintf(stderr, file_open_error_message(err, for_writing), filename);
175         fprintf(stderr, "\n");
176 }
177
178 /*
179  * Read errors are reported with an console message in "dftest".
180  */
181 static void
182 read_failure_message(const char *filename, int err)
183 {
184         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
185                 filename, g_strerror(err));
186 }
187
188 /*
189  * Write errors are reported with an console message in "dftest".
190  */
191 static void
192 write_failure_message(const char *filename, int err)
193 {
194         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
195                 filename, g_strerror(err));
196 }