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