"ssn_range" needs to be a copy of "global_ssn_range", so that it's not
[obnox/wireshark/wip.git] / dftest.c
1 /* dftest.c.c
2  *
3  * $Id$
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 /* With MSVC and a libethereal.dll this file needs to import some variables 
27    in a special way. Therefore _NEED_VAR_IMPORT_ is defined. */  
28 #define _NEED_VAR_IMPORT_
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <locale.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #ifdef NEED_STRERROR_H
41 #include "strerror.h"
42 #endif
43
44 #include <glib.h>
45 #include <epan/epan.h>
46
47 #include <epan/timestamp.h>
48 #include <epan/plugins.h>
49 #include <epan/filesystem.h>
50 #include <epan/prefs.h>
51 #include "util.h"
52 #include "epan/dfilter/dfilter.h"
53 #include "register.h"
54
55 packet_info     pi;
56
57 static void failure_message(const char *msg_format, va_list ap);
58 static void open_failure_message(const char *filename, int err,
59     gboolean for_writing);
60 static void read_failure_message(const char *filename, int err);
61
62 int
63 main(int argc, char **argv)
64 {
65         char            *text;
66         char            *gpf_path, *pf_path;
67         int             gpf_open_errno, gpf_read_errno;
68         int             pf_open_errno, pf_read_errno;
69         e_prefs         *prefs;
70         dfilter_t       *df;
71
72         set_timestamp_setting(TS_RELATIVE);
73
74         /* register all dissectors; we must do this before checking for the
75         "-g" flag, as the "-g" flag dumps a list of fields registered
76         by the dissectors, and we must do it before we read the preferences,
77         in case any dissectors register preferences. */
78         epan_init(PLUGIN_DIR,register_all_protocols,
79                   register_all_protocol_handoffs,
80                   failure_message, open_failure_message, read_failure_message);
81
82         /* now register the preferences for any non-dissector modules.
83         we must do that before we read the preferences as well. */
84         prefs_register_modules();
85
86         /* set the c-language locale to the native environment. */
87         setlocale(LC_ALL, "");
88
89         prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
90             &pf_open_errno, &pf_read_errno, &pf_path);
91         if (gpf_path != NULL) {
92                 if (gpf_open_errno != 0) {
93                         fprintf(stderr,
94                             "can't open global preferences file \"%s\": %s.\n",
95                             pf_path, strerror(gpf_open_errno));
96                 }
97                 if (gpf_read_errno != 0) {
98                         fprintf(stderr,
99                             "I/O error reading global preferences file \"%s\": %s.\n",
100                             pf_path, strerror(gpf_read_errno));
101                 }
102         }
103         if (pf_path != NULL) {
104                 if (pf_open_errno != 0) {
105                         fprintf(stderr,
106                             "can't open your preferences file \"%s\": %s.\n",
107                             pf_path, strerror(pf_open_errno));
108                 }
109                 if (pf_read_errno != 0) {
110                         fprintf(stderr,
111                             "I/O error reading your preferences file \"%s\": %s.\n",
112                             pf_path, strerror(pf_read_errno));
113                 }
114         }
115
116         /* notify all registered modules that have had any of their preferences
117         changed either from one of the preferences file or from the command
118         line that its preferences have changed. */
119         prefs_apply_all();
120
121         /* Check for filter on command line */
122         if (argc <= 1) {
123                 fprintf(stderr, "Usage: dftest filter\n");
124                 exit(1);
125         }
126
127         /* Get filter text */
128         text = get_args_as_string(argc, argv, 1);
129
130         printf("Filter: \"%s\"\n", text);
131
132         /* Compile it */
133         if (!dfilter_compile(text, &df)) {
134                 fprintf(stderr, "dftest: %s\n", dfilter_error_msg);
135                 epan_cleanup();
136                 exit(2);
137         }
138         printf("dfilter ptr = 0x%08x\n", (unsigned int) df);
139
140         printf("\n\n");
141
142         dfilter_dump(df);
143
144         epan_cleanup();
145         exit(0);
146 }
147
148 /*
149  * General errors are reported with an console message in "dftest".
150  */
151 static void
152 failure_message(const char *msg_format, va_list ap)
153 {
154         fprintf(stderr, "dftest: ");
155         vfprintf(stderr, msg_format, ap);
156         fprintf(stderr, "\n");
157 }
158
159 /*
160  * Open/create errors are reported with an console message in "dftest".
161  */
162 static void
163 open_failure_message(const char *filename, int err, gboolean for_writing)
164 {
165         fprintf(stderr, "dftest: ");
166         fprintf(stderr, file_open_error_message(err, for_writing), filename);
167         fprintf(stderr, "\n");
168 }
169
170 /*
171  * Read errors are reported with an console message in "dftest".
172  */
173 static void
174 read_failure_message(const char *filename, int err)
175 {
176         fprintf(stderr, "dftest: An error occurred while reading from the file \"%s\": %s.\n",
177             filename, strerror(err));
178 }