Qt: More byte view and proto tree fixes.
[metze/wireshark/wip.git] / ui / failure_message.c
1 /* failure_message.c
2  * Routines to print various "standard" failure messages used in multiple
3  * places
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include "config.h"
25
26 #include <string.h>
27 #include <errno.h>
28
29 #include <wiretap/wtap.h>
30 #include <wsutil/filesystem.h>
31 #include <wsutil/cmdarg_err.h>
32
33 #include "ui/failure_message.h"
34
35 static char *
36 input_file_description(const char *fname)
37 {
38     char *fstring;
39
40     if (strcmp(fname, "-") == 0) {
41         /* We're reading from the standard input */
42         fstring = g_strdup("standard input");
43     } else {
44         /* We're reading from a file */
45         fstring = g_strdup_printf("file \"%s\"", fname);
46     }
47     return fstring;
48 }
49
50 static char *
51 output_file_description(const char *fname)
52 {
53     char *fstring;
54
55     if (strcmp(fname, "-") == 0) {
56         /* We're writing to to the standard output */
57         fstring = g_strdup("standard output");
58     } else {
59         /* We're writing to a file */
60         fstring = g_strdup_printf("file \"%s\"", fname);
61     }
62     return fstring;
63 }
64
65 /*
66  * Error message for a failed attempt to open a capture file for reading.
67  * "progname" is the name of the program trying to open the file;
68  * "filename" is the name of the file being opened; "err" is assumed
69  * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
70  * to be a string giving further information for some WTAP_ERR_ values.
71  */
72 void
73 cfile_open_failure_message(const char *progname, const char *filename,
74                            int err, gchar *err_info)
75 {
76     char *file_description;
77
78     /* Get a string that describes what we're opening */
79     file_description = input_file_description(filename);
80
81     if (err < 0) {
82         /* Wiretap error. */
83         switch (err) {
84
85         case WTAP_ERR_NOT_REGULAR_FILE:
86             cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
87                        file_description);
88             break;
89
90         case WTAP_ERR_RANDOM_OPEN_PIPE:
91             cmdarg_err("The %s is a pipe or FIFO; %s can't read pipe or FIFO files in two-pass mode.",
92                        file_description, progname);
93             break;
94
95         case WTAP_ERR_FILE_UNKNOWN_FORMAT:
96             cmdarg_err("The %s isn't a capture file in a format %s understands.",
97                        file_description, progname);
98             break;
99
100         case WTAP_ERR_UNSUPPORTED:
101             cmdarg_err("The %s contains record data that %s doesn't support.\n"
102                        "(%s)",
103                        file_description, progname,
104                        err_info != NULL ? err_info : "no information supplied");
105             g_free(err_info);
106             break;
107
108         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
109             cmdarg_err("The %s is a capture for a network type that %s doesn't support.",
110                        file_description, progname);
111             break;
112
113         case WTAP_ERR_BAD_FILE:
114             cmdarg_err("The %s appears to be damaged or corrupt.\n"
115                        "(%s)",
116                        file_description,
117                        err_info != NULL ? err_info : "no information supplied");
118             g_free(err_info);
119             break;
120
121         case WTAP_ERR_CANT_OPEN:
122             cmdarg_err("The %s could not be opened for some unknown reason.",
123                        file_description);
124             break;
125
126         case WTAP_ERR_SHORT_READ:
127             cmdarg_err("The %s appears to have been cut short in the middle of a packet or other data.",
128                        file_description);
129             break;
130
131         case WTAP_ERR_DECOMPRESS:
132             cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt."
133                        "(%s)",
134                        file_description,
135                        err_info != NULL ? err_info : "no information supplied");
136             g_free(err_info);
137             break;
138
139         case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
140             cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support."
141                        "(%s)",
142                        file_description,
143                        err_info != NULL ? err_info : "no information supplied");
144             g_free(err_info);
145             break;
146
147         default:
148             cmdarg_err("The %s could not be opened: %s.",
149                        file_description,
150                        wtap_strerror(err));
151             break;
152         }
153         g_free(file_description);
154     } else
155         cmdarg_err(file_open_error_message(err, FALSE), filename);
156 }
157
158 /*
159  * Error message for a failed attempt to open a capture file for writing.
160  * "progname" is the name of the program trying to open the file;
161  * "filename" is the name of the file being opened; "err" is assumed
162  * to be a UNIX-style errno or a WTAP_ERR_ value; "file_type_subtype" is
163  * a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file being
164  * opened.
165  */
166 void
167 cfile_dump_open_failure_message(const char *progname, const char *filename,
168                                 int err, int file_type_subtype)
169 {
170     char *file_description;
171
172     /* Get a string that describes what we're opening */
173     file_description = output_file_description(filename);
174
175     if (err < 0) {
176         /* Wiretap error. */
177         switch (err) {
178
179         case WTAP_ERR_NOT_REGULAR_FILE:
180             cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.",
181                        file_description);
182             break;
183
184         case WTAP_ERR_CANT_WRITE_TO_PIPE:
185             cmdarg_err("The %s is a pipe, and \"%s\" capture files can't be written to a pipe.",
186                        file_description,
187                        wtap_file_type_subtype_short_string(file_type_subtype));
188             break;
189
190         case WTAP_ERR_UNWRITABLE_FILE_TYPE:
191             cmdarg_err("%s doesn't support writing capture files in that format.",
192                        progname);
193             break;
194
195         case WTAP_ERR_UNWRITABLE_ENCAP:
196             cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
197                        wtap_file_type_subtype_short_string(file_type_subtype));
198             break;
199
200         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
201             cmdarg_err("The capture file being read can't be written as a \"%s\" file.",
202                        wtap_file_type_subtype_short_string(file_type_subtype));
203             break;
204
205         case WTAP_ERR_CANT_OPEN:
206             cmdarg_err("The %s could not be created for some unknown reason.",
207                        file_description);
208             break;
209
210         case WTAP_ERR_SHORT_WRITE:
211             cmdarg_err("A full header couldn't be written to the %s.",
212                        file_description);
213             break;
214
215         case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
216             cmdarg_err("This file type cannot be written as a compressed file.");
217             break;
218
219         default:
220             cmdarg_err("The %s could not be created: %s.",
221                        file_description,
222                        wtap_strerror(err));
223             break;
224         }
225         g_free(file_description);
226     } else
227         cmdarg_err(file_open_error_message(err, TRUE), filename);
228 }
229
230 /*
231  * Error message for a failed attempt to read from a capture file.
232  * "progname" is the name of the program trying to open the file;
233  * "filename" is the name of the file being opened; "err" is assumed
234  * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
235  * to be a string giving further information for some WTAP_ERR_ values.
236  */
237 void
238 cfile_read_failure_message(const char *progname, const char *filename,
239                            int err, gchar *err_info)
240 {
241     char *file_string;
242
243     /* Get a string that describes what we're reading from */
244     file_string = input_file_description(filename);
245
246     switch (err) {
247
248     case WTAP_ERR_UNSUPPORTED:
249         cmdarg_err("The %s contains record data that %s doesn't support.\n"
250                    "(%s)",
251                    file_string, progname,
252                    err_info != NULL ? err_info : "no information supplied");
253         g_free(err_info);
254         break;
255
256     case WTAP_ERR_SHORT_READ:
257         cmdarg_err("The %s appears to have been cut short in the middle of a packet.",
258                    file_string);
259         break;
260
261     case WTAP_ERR_BAD_FILE:
262         cmdarg_err("The %s appears to be damaged or corrupt.\n"
263                    "(%s)",
264                    file_string,
265                    err_info != NULL ? err_info : "no information supplied");
266         g_free(err_info);
267         break;
268
269     case WTAP_ERR_DECOMPRESS:
270         cmdarg_err("The %s cannot be decompressed; it may be damaged or corrupt.\n"
271                    "(%s)",
272                    file_string,
273                    err_info != NULL ? err_info : "no information supplied");
274         g_free(err_info);
275         break;
276
277     case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
278         cmdarg_err("The %s cannot be decompressed; it is compressed in a way that we don't support.\n"
279                    "(%s)",
280                    file_string,
281                    err_info != NULL ? err_info : "no information supplied");
282         g_free(err_info);
283         break;
284
285     default:
286         cmdarg_err("An error occurred while reading the %s: %s.",
287                    file_string, wtap_strerror(err));
288         break;
289     }
290     g_free(file_string);
291 }
292
293 /*
294  * Error message for a failed attempt to write to a capture file.
295  * "progname" is the name of the program trying to open the file;
296  * "in_filename" is the name of the file from which the record
297  * being written came; "out_filename" is the name of the file to
298  * which we're writing; "err" is assumed "err" is assumed to be a
299  * UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed to be
300  * a string giving further information for some WTAP_ERR_ values;
301  * "framenum" is the frame number of the record on which the error
302  * occurred; "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value
303  * for the type and subtype of file being written.
304  */
305 void
306 cfile_write_failure_message(const char *progname, const char *in_filename,
307                             const char *out_filename, int err, gchar *err_info,
308                             guint32 framenum, int file_type_subtype)
309 {
310     char *in_file_string;
311     char *in_frame_string;
312     char *out_file_string;
313
314     /* Get a string that describes what we're reading from */
315     if (in_filename == NULL) {
316         in_frame_string = g_strdup("");
317     } else {
318         in_file_string = input_file_description(in_filename);
319         in_frame_string = g_strdup_printf(" %u of %s", framenum,
320                                           in_file_string);
321         g_free(in_file_string);
322     }
323
324     /* Get a string that describes what we're writing to */
325     out_file_string = output_file_description(out_filename);
326
327     switch (err) {
328
329     case WTAP_ERR_UNWRITABLE_ENCAP:
330         /*
331          * This is a problem with the particular frame we're writing
332          * and the file type and subtype we're writing; note that,
333          * and report the frame number and file type/subtype.
334          */
335         cmdarg_err("Frame%s has a network type that can't be saved in a \"%s\" file.",
336                    in_frame_string,
337                    wtap_file_type_subtype_short_string(file_type_subtype));
338         break;
339
340     case WTAP_ERR_PACKET_TOO_LARGE:
341         /*
342          * This is a problem with the particular frame we're writing
343          * and the file type and subtype we're writing; note that,
344          * and report the frame number and file type/subtype.
345          */
346         cmdarg_err("Frame%s is larger than %s supports in a \"%s\" file.",
347                    in_frame_string, progname,
348                    wtap_file_type_subtype_short_string(file_type_subtype));
349         break;
350
351     case WTAP_ERR_UNWRITABLE_REC_TYPE:
352         /*
353          * This is a problem with the particular record we're writing
354          * and the file type and subtype we're writing; note that,
355          * and report the record number and file type/subtype.
356          */
357         cmdarg_err("Record%s has a record type that can't be saved in a \"%s\" file.",
358                    in_frame_string,
359                    wtap_file_type_subtype_short_string(file_type_subtype));
360         break;
361
362     case WTAP_ERR_UNWRITABLE_REC_DATA:
363         /*
364          * This is a problem with the particular record we're writing
365          * and the file type and subtype we're writing; note that,
366          * and report the record number and file type/subtype.
367          */
368         cmdarg_err("Record%s has data that can't be saved in a \"%s\" file.\n"
369                    "(%s)",
370                    in_frame_string,
371                    wtap_file_type_subtype_short_string(file_type_subtype),
372                    err_info != NULL ? err_info : "no information supplied");
373         g_free(err_info);
374         break;
375
376     case ENOSPC:
377         cmdarg_err("Not all the packets could be written to the %s because there is "
378                    "no space left on the file system.",
379                    out_file_string);
380         break;
381
382 #ifdef EDQUOT
383     case EDQUOT:
384         cmdarg_err("Not all the packets could be written to the %s because you are "
385                    "too close to, or over your disk quota.",
386                    out_file_string);
387   break;
388 #endif
389
390     case WTAP_ERR_SHORT_WRITE:
391         cmdarg_err("A full write couldn't be done to the %s.",
392                    out_file_string);
393         break;
394
395     default:
396         cmdarg_err("An error occurred while writing to the %s: %s.",
397                    out_file_string, wtap_strerror(err));
398         break;
399     }
400     g_free(in_frame_string);
401     g_free(out_file_string);
402 }
403
404 /*
405  * Error message for a failed attempt to close a capture file.
406  * "filename" is the name of the file being closed; "err" is assumed
407  * to be a UNIX-style errno or a WTAP_ERR_ value.
408  *
409  * When closing a capture file:
410  *
411  *    some information in the file that can't be determined until
412  *    all packets have been written might be written to the file
413  *    (such as a table of the file offsets of all packets);
414  *
415  *    data buffered in the low-level file writing code might be
416  *    flushed to the file;
417  *
418  *    for remote file systems, data written to the file but not
419  *    yet sent to the server might be sent to the server or, if
420  *    that data was sent asynchronously, "out of space", "disk
421  *    quota exceeded", or "I/O error" indications might have
422  *    been received but not yet delivered, and the close operation
423  *    could deliver them;
424  *
425  * so we have to check for write errors here.
426  */
427 void
428 cfile_close_failure_message(const char *filename, int err)
429 {
430     char *file_string;
431
432     /* Get a string that describes what we're writing to */
433     file_string = output_file_description(filename);
434
435     switch (err) {
436
437     case ENOSPC:
438         cmdarg_err("Not all the packets could be written to the %s because there is "
439                    "no space left on the file system.",
440                    file_string);
441     break;
442
443 #ifdef EDQUOT
444     case EDQUOT:
445         cmdarg_err("Not all the packets could be written to the %s because you are "
446                    "too close to, or over your disk quota.",
447                    file_string);
448   break;
449 #endif
450
451     case WTAP_ERR_CANT_CLOSE:
452         cmdarg_err("The %s couldn't be closed for some unknown reason.",
453                    file_string);
454         break;
455
456     case WTAP_ERR_SHORT_WRITE:
457         cmdarg_err("A full write couldn't be done to the %s.",
458                    file_string);
459         break;
460
461     default:
462         cmdarg_err("An error occurred while closing the file %s: %s.",
463                    file_string, wtap_strerror(err));
464         break;
465     }
466     g_free(file_string);
467 }