dftest.c 5.79 KB
Newer Older
1 2
/* dftest.c
 * Shows display filter byte-code, for debugging dfilter routines.
3
 *
Ronnie Sahlberg's avatar
Ronnie Sahlberg committed
4 5
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
6 7 8 9 10 11
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
12
 *
13 14 15 16
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
17
 *
18 19
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 22
 */

23
#include <config.h>
24 25 26 27 28 29 30 31 32

#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#include <string.h>
#include <errno.h>

#include <glib.h>

33
#include <epan/epan.h>
34
#include <epan/timestamp.h>
35 36 37
#include <epan/prefs.h>
#include <epan/dfilter/dfilter.h>

38
#ifdef HAVE_PLUGINS
39
#include <wsutil/plugins.h>
40
#endif
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 55 56
int
main(int argc, char **argv)
{
57
	char		*init_progfile_dir_error;
58 59
	char		*text;
	char		*gpf_path, *pf_path;
60 61
	int		gpf_open_errno, gpf_read_errno;
	int		pf_open_errno, pf_read_errno;
62
	dfilter_t	*df;
63
	gchar		*err_msg;
64

65
	/*
66
	 * Get credential information for later use.
67
	 */
68
	init_process_policies();
69

70
	/*
71
	 * Attempt to get the pathname of the executable file.
72
	 */
73
	init_progfile_dir_error = init_progfile_dir(argv[0], main);
74 75
	if (init_progfile_dir_error != NULL) {
		fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
76
			init_progfile_dir_error);
77
	}
78

79 80 81
	init_report_err(failure_message, open_failure_message,
			read_failure_message, write_failure_message);

82
	timestamp_set_type(TS_RELATIVE);
83
	timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
84

85 86 87 88 89 90
#ifdef HAVE_PLUGINS
	/* Register all the plugin types we have. */
	epan_register_plugin_types(); /* Types known to libwireshark */

	/* Scan for plugins.  This does *not* call their registration routines;
	   that's done later. */
91
	scan_plugins(REPORT_LOAD_FAILURE);
92 93
#endif

94 95 96 97
	/* Register all dissectors; we must do this before checking for the
	   "-g" flag, as the "-g" flag dumps a list of fields registered
	   by the dissectors, and we must do it before we read the preferences,
	   in case any dissectors register preferences. */
98 99 100
	if (!epan_init(register_all_protocols, register_all_protocol_handoffs,
	    NULL, NULL))
		return 2;
101 102 103 104

	/* set the c-language locale to the native environment. */
	setlocale(LC_ALL, "");

105
	read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
106
		&pf_open_errno, &pf_read_errno, &pf_path);
107
	if (gpf_path != NULL) {
108 109
		if (gpf_open_errno != 0) {
			fprintf(stderr,
110
				"can't open global preferences file \"%s\": %s.\n",
111
				pf_path, g_strerror(gpf_open_errno));
112 113 114
		}
		if (gpf_read_errno != 0) {
			fprintf(stderr,
115
				"I/O error reading global preferences file \"%s\": %s.\n",
116
				pf_path, g_strerror(gpf_read_errno));
117
		}
118 119
	}
	if (pf_path != NULL) {
120 121
		if (pf_open_errno != 0) {
			fprintf(stderr,
122
				"can't open your preferences file \"%s\": %s.\n",
123
				pf_path, g_strerror(pf_open_errno));
124 125 126
		}
		if (pf_read_errno != 0) {
			fprintf(stderr,
127
				"I/O error reading your preferences file \"%s\": %s.\n",
128
				pf_path, g_strerror(pf_read_errno));
129
		}
130 131 132 133 134 135 136 137 138
	}

	/* notify all registered modules that have had any of their preferences
	changed either from one of the preferences file or from the command
	line that its preferences have changed. */
	prefs_apply_all();

	/* Check for filter on command line */
	if (argc <= 1) {
139
		fprintf(stderr, "Usage: dftest <filter>\n");
140 141 142 143 144 145 146
		exit(1);
	}

	/* Get filter text */
	text = get_args_as_string(argc, argv, 1);

	printf("Filter: \"%s\"\n", text);
147

148
	/* Compile it */
149 150 151
	if (!dfilter_compile(text, &df, &err_msg)) {
		fprintf(stderr, "dftest: %s\n", err_msg);
		g_free(err_msg);
152 153 154 155
		epan_cleanup();
		exit(2);
	}

156
	printf("\n");
157

158 159 160 161
	if (df == NULL)
		printf("Filter is empty\n");
	else
		dfilter_dump(df);
162

163
	dfilter_free(df);
164 165 166
	epan_cleanup();
	exit(0);
}
167

168 169 170 171 172 173 174 175 176 177 178
/*
 * General errors are reported with an console message in "dftest".
 */
static void
failure_message(const char *msg_format, va_list ap)
{
	fprintf(stderr, "dftest: ");
	vfprintf(stderr, msg_format, ap);
	fprintf(stderr, "\n");
}

179 180 181
/*
 * Open/create errors are reported with an console message in "dftest".
 */
182 183
static void
open_failure_message(const char *filename, int err, gboolean for_writing)
184
{
185 186 187
	fprintf(stderr, "dftest: ");
	fprintf(stderr, file_open_error_message(err, for_writing), filename);
	fprintf(stderr, "\n");
188 189 190 191 192
}

/*
 * Read errors are reported with an console message in "dftest".
 */
193 194
static void
read_failure_message(const char *filename, int err)
195
{
196
	fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
197
		filename, g_strerror(err));
198
}
199 200 201 202 203 204 205 206

/*
 * Write errors are reported with an console message in "dftest".
 */
static void
write_failure_message(const char *filename, int err)
{
	fprintf(stderr, "dftest: An error occurred while writing to the file \"%s\": %s.\n",
207
		filename, g_strerror(err));
208
}
209 210 211 212 213 214 215 216 217 218 219 220 221

/*
 * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
 * :indentSize=8:tabSize=8:noTabs=false:
 */