TrustedCA-Win2k => TrustedCA
[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  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <locale.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include <glib.h>
20
21 #include <epan/epan.h>
22 #include <epan/timestamp.h>
23 #include <epan/prefs.h>
24 #include <epan/dfilter/dfilter.h>
25
26 #ifdef HAVE_PLUGINS
27 #include <wsutil/plugins.h>
28 #endif
29 #include <wsutil/filesystem.h>
30 #include <wsutil/privileges.h>
31 #include <wsutil/report_message.h>
32
33 #include <wiretap/wtap.h>
34
35 #include "ui/util.h"
36
37 static void failure_warning_message(const char *msg_format, va_list ap);
38 static void open_failure_message(const char *filename, int err,
39         gboolean for_writing);
40 static void read_failure_message(const char *filename, int err);
41 static void write_failure_message(const char *filename, int err);
42
43 int
44 main(int argc, char **argv)
45 {
46         char            *init_progfile_dir_error;
47         char            *text;
48         dfilter_t       *df;
49         gchar           *err_msg;
50
51         /*
52          * Get credential information for later use.
53          */
54         init_process_policies();
55
56         /*
57          * Attempt to get the pathname of the directory containing the
58          * executable file.
59          */
60         init_progfile_dir_error = init_progfile_dir(argv[0]);
61         if (init_progfile_dir_error != NULL) {
62                 fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
63                         init_progfile_dir_error);
64                 g_free(init_progfile_dir_error);
65         }
66
67         init_report_message(failure_warning_message, failure_warning_message,
68                             open_failure_message, read_failure_message,
69                             write_failure_message);
70
71         timestamp_set_type(TS_RELATIVE);
72         timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
73
74         wtap_init(TRUE);
75
76         /* Register all dissectors; we must do this before checking for the
77            "-g" flag, as the "-g" flag dumps a list of fields registered
78            by the dissectors, and we must do it before we read the preferences,
79            in case any dissectors register preferences. */
80         if (!epan_init(NULL, NULL, FALSE))
81                 return 2;
82
83         /* set the c-language locale to the native environment. */
84         setlocale(LC_ALL, "");
85
86         /* Load libwireshark settings from the current profile. */
87         epan_load_settings();
88
89         /* notify all registered modules that have had any of their preferences
90         changed either from one of the preferences file or from the command
91         line that its preferences have changed. */
92         prefs_apply_all();
93
94         /* Check for filter on command line */
95         if (argc <= 1) {
96                 fprintf(stderr, "Usage: dftest <filter>\n");
97                 exit(1);
98         }
99
100         /* Get filter text */
101         text = get_args_as_string(argc, argv, 1);
102
103         printf("Filter: \"%s\"\n", text);
104
105         /* Compile it */
106         if (!dfilter_compile(text, &df, &err_msg)) {
107                 fprintf(stderr, "dftest: %s\n", err_msg);
108                 g_free(err_msg);
109                 epan_cleanup();
110                 exit(2);
111         }
112
113         printf("\n");
114
115         if (df == NULL)
116                 printf("Filter is empty\n");
117         else
118                 dfilter_dump(df);
119
120         dfilter_free(df);
121         epan_cleanup();
122         exit(0);
123 }
124
125 /*
126  * General errors and warnings are reported with an console message
127  * in "dftest".
128  */
129 static void
130 failure_warning_message(const char *msg_format, va_list ap)
131 {
132         fprintf(stderr, "dftest: ");
133         vfprintf(stderr, msg_format, ap);
134         fprintf(stderr, "\n");
135 }
136
137 /*
138  * Open/create errors are reported with an console message in "dftest".
139  */
140 static void
141 open_failure_message(const char *filename, int err, gboolean for_writing)
142 {
143         fprintf(stderr, "dftest: ");
144         fprintf(stderr, file_open_error_message(err, for_writing), filename);
145         fprintf(stderr, "\n");
146 }
147
148 /*
149  * Read errors are reported with an console message in "dftest".
150  */
151 static void
152 read_failure_message(const char *filename, int err)
153 {
154         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
155                 filename, g_strerror(err));
156 }
157
158 /*
159  * Write errors are reported with an console message in "dftest".
160  */
161 static void
162 write_failure_message(const char *filename, int err)
163 {
164         fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
165                 filename, g_strerror(err));
166 }
167
168 /*
169  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
170  *
171  * Local variables:
172  * c-basic-offset: 8
173  * tab-width: 8
174  * indent-tabs-mode: t
175  * End:
176  *
177  * vi: set shiftwidth=8 tabstop=8 noexpandtab:
178  * :indentSize=8:tabSize=8:noTabs=false:
179  */