Make "epan_init()" take, as additional arguments, pointers to routines
[jlayton/wireshark.git] / dftest.c
1 /* dftest.c.c
2  *
3  * $Id: dftest.c,v 1.10 2004/03/23 21:19:55 guy Exp $
4  *
5  * Ethereal - Network traffic analyzer
6  * By Gerald Combs <gerald@ethereal.com>
7  * Copyright 1998 Gerald Combs
8  *
9  * Shows display filter byte-code, for debugging dfilter routines.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <locale.h>
33 #include <string.h>
34 #include <errno.h>
35
36 #ifdef NEED_STRERROR_H
37 #include "strerror.h"
38 #endif
39
40 #include <glib.h>
41 #include <epan/epan.h>
42
43 #include <epan/timestamp.h>
44 #include <epan/plugins.h>
45 #include <epan/filesystem.h>
46 #include "prefs.h"
47 #include "util.h"
48 #include "epan/dfilter/dfilter.h"
49 #include "register.h"
50
51 packet_info     pi;
52
53 static void open_failure_message(const char *filename, int err,
54     gboolean for_writing);
55 static void read_failure_message(const char *filename, int err);
56
57 int
58 main(int argc, char **argv)
59 {
60         char            *text;
61         char            *gpf_path, *pf_path;
62         int             gpf_open_errno, gpf_read_errno;
63         int             pf_open_errno, pf_read_errno;
64         e_prefs         *prefs;
65         dfilter_t       *df;
66
67         set_timestamp_setting(TS_RELATIVE);
68
69         /* register all dissectors; we must do this before checking for the
70         "-g" flag, as the "-g" flag dumps a list of fields registered
71         by the dissectors, and we must do it before we read the preferences,
72         in case any dissectors register preferences. */
73         epan_init(PLUGIN_DIR,register_all_protocols,
74                   register_all_protocol_handoffs,
75                   open_failure_message, read_failure_message);
76
77         /* now register the preferences for any non-dissector modules.
78         we must do that before we read the preferences as well. */
79         prefs_register_modules();
80
81         /* set the c-language locale to the native environment. */
82         setlocale(LC_ALL, "");
83
84         prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
85             &pf_open_errno, &pf_read_errno, &pf_path);
86         if (gpf_path != NULL) {
87                 if (gpf_open_errno != 0) {
88                         fprintf(stderr,
89                             "can't open global preferences file \"%s\": %s.\n",
90                             pf_path, strerror(gpf_open_errno));
91                 }
92                 if (gpf_read_errno != 0) {
93                         fprintf(stderr,
94                             "I/O error reading global preferences file \"%s\": %s.\n",
95                             pf_path, strerror(gpf_read_errno));
96                 }
97         }
98         if (pf_path != NULL) {
99                 if (pf_open_errno != 0) {
100                         fprintf(stderr,
101                             "can't open your preferences file \"%s\": %s.\n",
102                             pf_path, strerror(pf_open_errno));
103                 }
104                 if (pf_read_errno != 0) {
105                         fprintf(stderr,
106                             "I/O error reading your preferences file \"%s\": %s.\n",
107                             pf_path, strerror(pf_read_errno));
108                 }
109         }
110
111         /* notify all registered modules that have had any of their preferences
112         changed either from one of the preferences file or from the command
113         line that its preferences have changed. */
114         prefs_apply_all();
115
116         /* Check for filter on command line */
117         if (argc <= 1) {
118                 fprintf(stderr, "Usage: dftest filter\n");
119                 exit(1);
120         }
121
122         /* Get filter text */
123         text = get_args_as_string(argc, argv, 1);
124
125         printf("Filter: \"%s\"\n", text);
126
127         /* Compile it */
128         if (!dfilter_compile(text, &df)) {
129                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
130                 epan_cleanup();
131                 exit(2);
132         }
133         printf("dfilter ptr = 0x%08x\n", (unsigned int) df);
134
135         printf("\n\n");
136
137         dfilter_dump(df);
138
139         epan_cleanup();
140         exit(0);
141 }
142
143 /*
144  * Open/create errors are reported with an console message in "dftest".
145  */
146 static void
147 open_failure_message(const char *filename, int err, gboolean for_writing)
148 {
149         char *errmsg;
150
151         errmsg = g_strdup_printf(file_open_error_message(err, for_writing),
152             filename);
153         fprintf(stderr, "dftest: %s\n", errmsg);
154         g_free(errmsg);
155 }
156
157 /*
158  * Read errors are reported with an console message in "dftest".
159  */
160 static void
161 read_failure_message(const char *filename, int err)
162 {
163         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.",
164             filename, strerror(err));
165 }