Have scan_plugins() take an argument specify what to do on load failures.
[metze/wireshark/wip.git] / captype.c
1 /* captype.c
2  * Reports capture file type
3  *
4  * Based on capinfos.c
5  * Copyright 2004 Ian Schorr
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <config.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <locale.h>
33 #include <errno.h>
34
35 #ifdef HAVE_GETOPT_H
36 #include <getopt.h>
37 #endif
38
39 #include <glib.h>
40
41 #include <wiretap/wtap.h>
42
43 #include <wsutil/crash_info.h>
44 #include <wsutil/file_util.h>
45 #include <wsutil/filesystem.h>
46 #include <wsutil/privileges.h>
47 #include <ws_version_info.h>
48
49 #ifdef HAVE_PLUGINS
50 #include <wsutil/plugins.h>
51 #endif
52
53 #include <wsutil/report_err.h>
54 #include <wsutil/str_util.h>
55
56 #ifdef _WIN32
57 #include <wsutil/unicode-utils.h>
58 #endif /* _WIN32 */
59
60 #ifndef HAVE_GETOPT_LONG
61 #include "wsutil/wsgetopt.h"
62 #endif
63
64 static void
65 print_usage(FILE *output)
66 {
67   fprintf(output, "\n");
68   fprintf(output, "Usage: captype <infile> ...\n");
69 }
70
71 #ifdef HAVE_PLUGINS
72 /*
73  * General errors are reported with an console message in captype.
74  */
75 static void
76 failure_message(const char *msg_format, va_list ap)
77 {
78   fprintf(stderr, "captype: ");
79   vfprintf(stderr, msg_format, ap);
80   fprintf(stderr, "\n");
81 }
82 #endif
83
84 int
85 main(int argc, char *argv[])
86 {
87   GString *comp_info_str;
88   GString *runtime_info_str;
89   wtap  *wth;
90   int    err;
91   gchar *err_info;
92   int    i;
93   int    opt;
94   int    overall_error_status;
95   static const struct option long_options[] = {
96       {"help", no_argument, NULL, 'h'},
97       {"version", no_argument, NULL, 'v'},
98       {0, 0, 0, 0 }
99   };
100
101 #ifdef HAVE_PLUGINS
102   char  *init_progfile_dir_error;
103 #endif
104
105   /* Set the C-language locale to the native environment. */
106   setlocale(LC_ALL, "");
107
108   /* Get the compile-time version information string */
109   comp_info_str = get_compiled_version_info(NULL, NULL);
110
111   /* Get the run-time version information string */
112   runtime_info_str = get_runtime_version_info(NULL);
113
114   /* Add it to the information to be reported on a crash. */
115   ws_add_crash_info("Captype (Wireshark) %s\n"
116          "\n"
117          "%s"
118          "\n"
119          "%s",
120       get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
121
122 #ifdef _WIN32
123   arg_list_utf_16to8(argc, argv);
124   create_app_running_mutex();
125 #endif /* _WIN32 */
126
127   /*
128    * Get credential information for later use.
129    */
130   init_process_policies();
131   init_open_routines();
132
133 #ifdef HAVE_PLUGINS
134   if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
135     g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
136     g_free(init_progfile_dir_error);
137   } else {
138     /* Register all the plugin types we have. */
139     wtap_register_plugin_types(); /* Types known to libwiretap */
140
141     init_report_err(failure_message,NULL,NULL,NULL);
142
143     /* Scan for plugins.  This does *not* call their registration routines;
144        that's done later.
145
146        Don't report failures to load plugins because most (non-wiretap)
147        plugins *should* fail to load (because we're not linked against
148        libwireshark and dissector plugins need libwireshark). */
149     scan_plugins(DONT_REPORT_LOAD_FAILURE);
150
151     /* Register all libwiretap plugin modules. */
152     register_all_wiretap_modules();
153   }
154 #endif
155
156   /* Process the options */
157   while ((opt = getopt_long(argc, argv, "hv", long_options, NULL)) !=-1) {
158
159     switch (opt) {
160
161       case 'h':
162         printf("Captype (Wireshark) %s\n"
163                "Print the file types of capture files.\n"
164                "See https://www.wireshark.org for more information.\n",
165                get_ws_vcs_version_info());
166         print_usage(stdout);
167         exit(0);
168         break;
169
170       case 'v':
171         show_version("Captype (Wireshark)", comp_info_str, runtime_info_str);
172         g_string_free(comp_info_str, TRUE);
173         g_string_free(runtime_info_str, TRUE);
174         exit(0);
175         break;
176
177       case '?':              /* Bad flag - print usage message */
178         print_usage(stderr);
179         exit(1);
180         break;
181     }
182   }
183
184   if (argc < 2) {
185     print_usage(stderr);
186     return 1;
187   }
188
189   overall_error_status = 0;
190
191   for (i = 1; i < argc; i++) {
192     wth = wtap_open_offline(argv[i], WTAP_TYPE_AUTO, &err, &err_info, FALSE);
193
194     if(wth) {
195       printf("%s: %s\n", argv[i], wtap_file_type_subtype_short_string(wtap_file_type_subtype(wth)));
196       wtap_close(wth);
197     } else {
198       if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT)
199         printf("%s: unknown\n", argv[i]);
200       else {
201         fprintf(stderr, "captype: Can't open %s: %s\n", argv[i],
202                 wtap_strerror(err));
203         if (err_info != NULL) {
204           fprintf(stderr, "(%s)\n", err_info);
205           g_free(err_info);
206         }
207         overall_error_status = 1; /* remember that an error has occurred */
208       }
209     }
210
211   }
212
213   return overall_error_status;
214 }
215
216 /*
217  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
218  *
219  * Local variables:
220  * c-basic-offset: 2
221  * tab-width: 8
222  * indent-tabs-mode: nil
223  * End:
224  *
225  * vi: set shiftwidth=2 tabstop=8 expandtab:
226  * :indentSize=2:tabSize=8:noTabs=true:
227  */