Replace all strerror() with g_strerror().
[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 #include <glib.h>
36 #include <epan/epan.h>
37
38 #include <epan/timestamp.h>
39 #include <epan/plugins.h>
40 #include <epan/filesystem.h>
41 #include <wsutil/privileges.h>
42 #include <epan/prefs.h>
43 #include "util.h"
44 #include "epan/dfilter/dfilter.h"
45 #include "register.h"
46
47 static void failure_message(const char *msg_format, va_list ap);
48 static void open_failure_message(const char *filename, int err,
49         gboolean for_writing);
50 static void read_failure_message(const char *filename, int err);
51 static void write_failure_message(const char *filename, int err);
52
53 int
54 main(int argc, char **argv)
55 {
56         char            *init_progfile_dir_error;
57         char            *text;
58         char            *gpf_path, *pf_path;
59         int             gpf_open_errno, gpf_read_errno;
60         int             pf_open_errno, pf_read_errno;
61         dfilter_t       *df;
62
63         /*
64          * Get credential information for later use.
65          */
66         init_process_policies();
67
68         /*
69          * Attempt to get the pathname of the executable file.
70          */
71         init_progfile_dir_error = init_progfile_dir(argv[0], main);
72         if (init_progfile_dir_error != NULL) {
73                 fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
74                         init_progfile_dir_error);
75         }
76
77         timestamp_set_type(TS_RELATIVE);
78         timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
79
80         /* Register all dissectors; we must do this before checking for the
81            "-g" flag, as the "-g" flag dumps a list of fields registered
82            by the dissectors, and we must do it before we read the preferences,
83            in case any dissectors register preferences. */
84         epan_init(register_all_protocols,
85                   register_all_protocol_handoffs, NULL, NULL,
86                   failure_message, open_failure_message, read_failure_message,
87                   write_failure_message);
88
89         /* now register the preferences for any non-dissector modules.
90         we must do that before we read the preferences as well. */
91         prefs_register_modules();
92
93         /* set the c-language locale to the native environment. */
94         setlocale(LC_ALL, "");
95
96         read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
97                 &pf_open_errno, &pf_read_errno, &pf_path);
98         if (gpf_path != NULL) {
99                 if (gpf_open_errno != 0) {
100                         fprintf(stderr,
101                                 "can't open global preferences file \"%s\": %s.\n",
102                                 pf_path, g_strerror(gpf_open_errno));
103                 }
104                 if (gpf_read_errno != 0) {
105                         fprintf(stderr,
106                                 "I/O error reading global preferences file \"%s\": %s.\n",
107                                 pf_path, g_strerror(gpf_read_errno));
108                 }
109         }
110         if (pf_path != NULL) {
111                 if (pf_open_errno != 0) {
112                         fprintf(stderr,
113                                 "can't open your preferences file \"%s\": %s.\n",
114                                 pf_path, g_strerror(pf_open_errno));
115                 }
116                 if (pf_read_errno != 0) {
117                         fprintf(stderr,
118                                 "I/O error reading your preferences file \"%s\": %s.\n",
119                                 pf_path, g_strerror(pf_read_errno));
120                 }
121         }
122
123         /* notify all registered modules that have had any of their preferences
124         changed either from one of the preferences file or from the command
125         line that its preferences have changed. */
126         prefs_apply_all();
127
128         /* Check for filter on command line */
129         if (argc <= 1) {
130                 fprintf(stderr, "Usage: dftest <filter>\n");
131                 exit(1);
132         }
133
134         /* Get filter text */
135         text = get_args_as_string(argc, argv, 1);
136
137         printf("Filter: \"%s\"\n", text);
138
139         /* Compile it */
140         if (!dfilter_compile(text, &df)) {
141                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
142                 epan_cleanup();
143                 exit(2);
144         }
145         printf("dfilter ptr = 0x%08x\n", GPOINTER_TO_INT(df));
146
147         printf("\n\n");
148
149         if (df == NULL)
150                 printf("Filter is empty\n");
151         else
152                 dfilter_dump(df);
153
154         dfilter_free(df);
155         epan_cleanup();
156         exit(0);
157 }
158
159 /*
160  * General errors are reported with an console message in "dftest".
161  */
162 static void
163 failure_message(const char *msg_format, va_list ap)
164 {
165         fprintf(stderr, "dftest: ");
166         vfprintf(stderr, msg_format, ap);
167         fprintf(stderr, "\n");
168 }
169
170 /*
171  * Open/create errors are reported with an console message in "dftest".
172  */
173 static void
174 open_failure_message(const char *filename, int err, gboolean for_writing)
175 {
176         fprintf(stderr, "dftest: ");
177         fprintf(stderr, file_open_error_message(err, for_writing), filename);
178         fprintf(stderr, "\n");
179 }
180
181 /*
182  * Read errors are reported with an console message in "dftest".
183  */
184 static void
185 read_failure_message(const char *filename, int err)
186 {
187         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
188                 filename, g_strerror(err));
189 }
190
191 /*
192  * Write errors are reported with an console message in "dftest".
193  */
194 static void
195 write_failure_message(const char *filename, int err)
196 {
197         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
198                 filename, g_strerror(err));
199 }