Update MCC/MNC list; remove double appearance of the same value
[obnox/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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <locale.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #ifdef NEED_STRERROR_H
36 #include "strerror.h"
37 #endif
38
39 #include <glib.h>
40 #include <epan/epan.h>
41
42 #include <epan/timestamp.h>
43 #include <epan/plugins.h>
44 #include <epan/filesystem.h>
45 #include <wsutil/privileges.h>
46 #include <epan/prefs.h>
47 #include "util.h"
48 #include "epan/dfilter/dfilter.h"
49 #include "register.h"
50
51 static void failure_message(const char *msg_format, va_list ap);
52 static void open_failure_message(const char *filename, int err,
53         gboolean for_writing);
54 static void read_failure_message(const char *filename, int err);
55 static void write_failure_message(const char *filename, int err);
56
57 int
58 main(int argc, char **argv)
59 {
60         char            *init_progfile_dir_error;
61         char            *text;
62         char            *gpf_path, *pf_path;
63         int             gpf_open_errno, gpf_read_errno;
64         int             pf_open_errno, pf_read_errno;
65         e_prefs         *prefs_p;
66         dfilter_t       *df;
67
68         /*
69          * Get credential information for later use.
70          */
71         get_credential_info();
72
73         /*
74          * Attempt to get the pathname of the executable file.
75          */
76         init_progfile_dir_error = init_progfile_dir(argv[0], main);
77         if (init_progfile_dir_error != NULL) {
78                 fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
79                         init_progfile_dir_error);
80         }
81
82         timestamp_set_type(TS_RELATIVE);
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,
89                   register_all_protocol_handoffs, NULL, NULL,
90                   failure_message, open_failure_message, read_failure_message,
91                   write_failure_message);
92
93         /* now register the preferences for any non-dissector modules.
94         we must do that before we read the preferences as well. */
95         prefs_register_modules();
96
97         /* set the c-language locale to the native environment. */
98         setlocale(LC_ALL, "");
99
100         prefs_p = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
101                 &pf_open_errno, &pf_read_errno, &pf_path);
102         if (gpf_path != NULL) {
103                 if (gpf_open_errno != 0) {
104                         fprintf(stderr,
105                                 "can't open global preferences file \"%s\": %s.\n",
106                                 pf_path, strerror(gpf_open_errno));
107                 }
108                 if (gpf_read_errno != 0) {
109                         fprintf(stderr,
110                                 "I/O error reading global preferences file \"%s\": %s.\n",
111                                 pf_path, strerror(gpf_read_errno));
112                 }
113         }
114         if (pf_path != NULL) {
115                 if (pf_open_errno != 0) {
116                         fprintf(stderr,
117                                 "can't open your preferences file \"%s\": %s.\n",
118                                 pf_path, strerror(pf_open_errno));
119                 }
120                 if (pf_read_errno != 0) {
121                         fprintf(stderr,
122                                 "I/O error reading your preferences file \"%s\": %s.\n",
123                                 pf_path, strerror(pf_read_errno));
124                 }
125         }
126
127         /* notify all registered modules that have had any of their preferences
128         changed either from one of the preferences file or from the command
129         line that its preferences have changed. */
130         prefs_apply_all();
131
132         /* Check for filter on command line */
133         if (argc <= 1) {
134                 fprintf(stderr, "Usage: dftest <filter>\n");
135                 exit(1);
136         }
137
138         /* Get filter text */
139         text = get_args_as_string(argc, argv, 1);
140
141         printf("Filter: \"%s\"\n", text);
142
143         /* Compile it */
144         if (!dfilter_compile(text, &df)) {
145                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
146                 epan_cleanup();
147                 exit(2);
148         }
149         printf("dfilter ptr = 0x%08x\n", GPOINTER_TO_INT(df));
150
151         printf("\n\n");
152
153         if (df == NULL)
154                 printf("Filter is empty\n");
155         else
156                 dfilter_dump(df);
157
158         dfilter_free(df);
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 }
194
195 /*
196  * Write errors are reported with an console message in "dftest".
197  */
198 static void
199 write_failure_message(const char *filename, int err)
200 {
201         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
202                 filename, strerror(err));
203 }