nl80211: display interface name as a string
[metze/wireshark/wip.git] / ui / alert_box.c
1 /* alert_box.c
2  * Routines to put up various "standard" alert boxes 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  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "config.h"
13
14 #include <string.h>
15
16 #include <wiretap/wtap.h>
17 #include <wsutil/filesystem.h>
18
19 #include "ui/alert_box.h"
20
21 #include "ui/simple_dialog.h"
22
23 /*
24  * Alert box for general errors.
25  */
26 void
27 failure_alert_box(const char *msg_format, ...)
28 {
29     va_list ap;
30
31     va_start(ap, msg_format);
32     vsimple_error_message_box(msg_format, ap);
33     va_end(ap);
34 }
35
36 void
37 vfailure_alert_box(const char *msg_format, va_list ap)
38 {
39     vsimple_error_message_box(msg_format, ap);
40 }
41
42 void
43 vwarning_alert_box(const char *msg_format, va_list ap)
44 {
45     vsimple_warning_message_box(msg_format, ap);
46 }
47
48 /*
49  * Alert box for a failed attempt to open a capture file for reading.
50  * "filename" is the name of the file being opened; "err" is assumed
51  * to be a UNIX-style errno or a WTAP_ERR_ value; "err_info" is assumed
52  * to be a string giving further information for some WTAP_ERR_ values..
53  *
54  * XXX - add explanatory secondary text for at least some of the errors;
55  * various HIGs suggest that you should, for example, suggest that the
56  * user remove files if the file system is full.  Perhaps that's because
57  * they're providing guidelines for people less sophisticated than the
58  * typical Wireshark user is, but....
59  */
60 void
61 cfile_open_failure_alert_box(const char *filename, int err, gchar *err_info)
62 {
63     gchar *display_basename;
64
65     if (err < 0) {
66         /* Wiretap error. */
67         display_basename = g_filename_display_basename(filename);
68         switch (err) {
69
70         case WTAP_ERR_NOT_REGULAR_FILE:
71             simple_error_message_box(
72                         "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
73                         display_basename);
74             break;
75
76         case WTAP_ERR_RANDOM_OPEN_PIPE:
77             simple_error_message_box(
78                         "The file \"%s\" is a pipe or FIFO; Wireshark can't read pipe or FIFO files.\n"
79                         "To capture from a pipe or FIFO use wireshark -i -",
80                         display_basename);
81             break;
82
83         case WTAP_ERR_FILE_UNKNOWN_FORMAT:
84             simple_error_message_box(
85                         "The file \"%s\" isn't a capture file in a format Wireshark understands.",
86                         display_basename);
87             break;
88
89         case WTAP_ERR_UNSUPPORTED:
90             simple_error_message_box(
91                         "The file \"%s\" contains record data that Wireshark doesn't support.\n"
92                         "(%s)",
93                         display_basename,
94                         err_info != NULL ? err_info : "no information supplied");
95             g_free(err_info);
96             break;
97
98         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
99             simple_error_message_box(
100                         "The file \"%s\" is a capture for a network type that Wireshark doesn't support.",
101                         display_basename);
102             break;
103
104         case WTAP_ERR_BAD_FILE:
105             simple_error_message_box(
106                         "The file \"%s\" appears to be damaged or corrupt.\n"
107                         "(%s)",
108                         display_basename,
109                         err_info != NULL ? err_info : "no information supplied");
110             g_free(err_info);
111             break;
112
113         case WTAP_ERR_CANT_OPEN:
114             simple_error_message_box(
115                         "The file \"%s\" could not be opened for some unknown reason.",
116                         display_basename);
117             break;
118
119         case WTAP_ERR_SHORT_READ:
120             simple_error_message_box(
121                         "The file \"%s\" appears to have been cut short"
122                         " in the middle of a packet or other data.",
123                         display_basename);
124             break;
125
126         case WTAP_ERR_DECOMPRESS:
127             simple_error_message_box(
128                         "The file \"%s\" cannot be decompressed; it may be damaged or corrupt.\n"
129                         "(%s)", display_basename,
130                         err_info != NULL ? err_info : "no information supplied");
131             g_free(err_info);
132             break;
133
134         case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
135             simple_error_message_box(
136                         "The file \"%s\" cannot be decompressed; it is compressed in a way that we don't support.\n"
137                         "(%s)", display_basename,
138                         err_info != NULL ? err_info : "no information supplied");
139             g_free(err_info);
140             break;
141
142         default:
143             simple_error_message_box(
144                         "The file \"%s\" could not be opened: %s.",
145                         display_basename,
146                         wtap_strerror(err));
147             break;
148         }
149         g_free(display_basename);
150     } else {
151         /* OS error. */
152         open_failure_alert_box(filename, err, FALSE);
153     }
154 }
155
156 /*
157  * Alert box for a failed attempt to open a capture file for writing.
158  * "filename" is the name of the file being opened; "err" is assumed
159  * to be a UNIX-style errno or a WTAP_ERR_ value; "file_type_subtype"
160  * is a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file
161  * being opened.
162  *
163  * XXX - add explanatory secondary text for at least some of the errors;
164  * various HIGs suggest that you should, for example, suggest that the
165  * user remove files if the file system is full.  Perhaps that's because
166  * they're providing guidelines for people less sophisticated than the
167  * typical Wireshark user is, but....
168  */
169 void
170 cfile_dump_open_failure_alert_box(const char *filename, int err,
171                                   int file_type_subtype)
172 {
173     gchar *display_basename;
174
175     if (err < 0) {
176         /* Wiretap error. */
177         display_basename = g_filename_display_basename(filename);
178         switch (err) {
179
180         case WTAP_ERR_NOT_REGULAR_FILE:
181             simple_error_message_box(
182                         "The file \"%s\" is a \"special file\" or socket or other non-regular file.",
183                         display_basename);
184             break;
185
186         case WTAP_ERR_CANT_WRITE_TO_PIPE:
187             simple_error_message_box(
188                         "The file \"%s\" is a pipe, and %s capture files can't be "
189                         "written to a pipe.",
190                         display_basename, wtap_file_type_subtype_string(file_type_subtype));
191             break;
192
193         case WTAP_ERR_UNWRITABLE_FILE_TYPE:
194             simple_error_message_box(
195                         "Wireshark doesn't support writing capture files in that format.");
196             break;
197
198         case WTAP_ERR_UNWRITABLE_ENCAP:
199             simple_error_message_box("Wireshark can't save this capture in that format.");
200             break;
201
202         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
203             simple_error_message_box(
204                         "Wireshark can't save this capture in that format.");
205             break;
206
207         case WTAP_ERR_CANT_OPEN:
208             simple_error_message_box(
209                         "The file \"%s\" could not be created for some unknown reason.",
210                         display_basename);
211             break;
212
213         case WTAP_ERR_SHORT_WRITE:
214             simple_error_message_box(
215                         "A full header couldn't be written to the file \"%s\".",
216                         display_basename);
217             break;
218
219         case WTAP_ERR_COMPRESSION_NOT_SUPPORTED:
220             simple_error_message_box(
221                         "This file type cannot be written as a compressed file.");
222             break;
223
224         default:
225             simple_error_message_box(
226                         "The file \"%s\" could not be created: %s.",
227                         display_basename,
228                         wtap_strerror(err));
229             break;
230         }
231         g_free(display_basename);
232     } else {
233         /* OS error. */
234         open_failure_alert_box(filename, err, TRUE);
235     }
236 }
237
238 /*
239  * Alert box for a failed attempt to read from a capture file.
240  * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value;
241  * "err_info" is assumed to be a string giving further information for
242  * some WTAP_ERR_ values.
243  */
244 void
245 cfile_read_failure_alert_box(const char *filename, int err, gchar *err_info)
246 {
247     gchar *display_name;
248
249     if (filename == NULL)
250         display_name = g_strdup("capture file");
251     else {
252         gchar *display_basename;
253
254         display_basename = g_filename_display_basename(filename);
255         display_name = g_strdup_printf("capture file \"%s\"", display_basename);
256         g_free(display_basename);
257     }
258
259     switch (err) {
260
261     case WTAP_ERR_UNSUPPORTED:
262         simple_error_message_box(
263                     "The %s contains record data that Wireshark doesn't support.\n"
264                     "(%s)",
265                     display_name,
266                     err_info != NULL ? err_info : "no information supplied");
267         g_free(err_info);
268         break;
269
270     case WTAP_ERR_SHORT_READ:
271         simple_error_message_box(
272                     "The %s appears to have been cut short in the middle of a packet.",
273                     display_name);
274         break;
275
276     case WTAP_ERR_BAD_FILE:
277         simple_error_message_box(
278                     "The %s appears to be damaged or corrupt.\n"
279                     "(%s)",
280                     display_name,
281                     err_info != NULL ? err_info : "no information supplied");
282         g_free(err_info);
283         break;
284
285     case WTAP_ERR_DECOMPRESS:
286         simple_error_message_box(
287                     "The %s cannot be decompressed; it may be damaged or corrupt.\n"
288                     "(%s)",
289                     display_name,
290                     err_info != NULL ? err_info : "no information supplied");
291         g_free(err_info);
292         break;
293
294     case WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED:
295         simple_error_message_box(
296                     "The %s cannot be decompressed; it is compressed in a way that we don't support.\n"
297                     "(%s)",
298                     display_name,
299                     err_info != NULL ? err_info : "no information supplied");
300         g_free(err_info);
301         break;
302
303     default:
304         simple_error_message_box(
305                     "An error occurred while reading the %s: %s.",
306                     display_name,
307                     wtap_strerror(err));
308         break;
309     }
310     g_free(display_name);
311 }
312
313 /*
314  * Alert box for a failed attempt to write to a capture file.
315  * "in_filename" is the name of the file from which the record being
316  * written came; "out_filename" is the name of the file to which we're
317  * writing; "err" is assumed "err" is assumed to be a UNIX-style errno
318  * or a WTAP_ERR_ value; "err_info" is assumed to be a string giving
319  * further information for some WTAP_ERR_ values; "framenum" is the frame
320  * number of the record on which the error occurred; "file_type_subtype"
321  * is a WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file
322  * being written.
323  */
324 void
325 cfile_write_failure_alert_box(const char *in_filename, const char *out_filename,
326                               int err, gchar *err_info, guint32 framenum,
327                               int file_type_subtype)
328 {
329     char *in_file_string;
330     char *out_display_basename;
331
332     if (err < 0) {
333         /* Wiretap error. */
334         if (in_filename == NULL)
335             in_file_string = g_strdup("");
336         else
337             in_file_string = g_strdup_printf(" of file \"%s\"", in_filename);
338
339         switch (err) {
340
341         case WTAP_ERR_UNWRITABLE_ENCAP:
342             /*
343              * This is a problem with the particular frame we're writing and
344              * the file type and subtype we're writing; note that, and report
345              * the frame number and file type/subtype.
346              */
347             simple_error_message_box(
348                         "Frame %u%s has a network type that can't be saved in a \"%s\" file.",
349                         framenum, in_file_string,
350                         wtap_file_type_subtype_string(file_type_subtype));
351             break;
352
353         case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED:
354             /*
355              * This is a problem with the particular frame we're writing and
356              * the file type and subtype we're writing; note that, and report
357              * the frame number and file type/subtype.
358              */
359             simple_error_message_box(
360                         "Frame %u%s has a network type that differs from the network type of earlier packets, which isn't supported in a \"%s\" file.",
361                         framenum, in_file_string,
362                         wtap_file_type_subtype_string(file_type_subtype));
363             break;
364
365         case WTAP_ERR_PACKET_TOO_LARGE:
366             /*
367              * This is a problem with the particular frame we're writing and
368              * the file type and subtype we're writing; note that, and report
369              * the frame number and file type/subtype.
370              */
371             simple_error_message_box(
372                         "Frame %u%s is larger than Wireshark supports in a \"%s\" file.",
373                         framenum, in_file_string,
374                         wtap_file_type_subtype_string(file_type_subtype));
375             break;
376
377         case WTAP_ERR_UNWRITABLE_REC_TYPE:
378             /*
379              * This is a problem with the particular record we're writing and
380              * the file type and subtype we're writing; note that, and report
381              * the record number and file type/subtype.
382              */
383             simple_error_message_box(
384                         "Record %u%s has a record type that can't be saved in a \"%s\" file.",
385                         framenum, in_file_string,
386                         wtap_file_type_subtype_string(file_type_subtype));
387             break;
388
389         case WTAP_ERR_UNWRITABLE_REC_DATA:
390             /*
391              * This is a problem with the particular record we're writing and
392              * the file type and subtype we're writing; note that, and report
393              * the record number and file type/subtype.
394              */
395             simple_error_message_box(
396                         "Record %u%s has data that can't be saved in a \"%s\" file.\n"
397                         "(%s)",
398                         framenum, in_file_string,
399                         wtap_file_type_subtype_string(file_type_subtype),
400                         err_info != NULL ? err_info : "no information supplied");
401             g_free(err_info);
402             break;
403
404         case WTAP_ERR_SHORT_WRITE:
405             out_display_basename = g_filename_display_basename(out_filename);
406             simple_error_message_box(
407                         "A full write couldn't be done to the file \"%s\".",
408                         out_display_basename);
409             g_free(out_display_basename);
410             break;
411
412         default:
413             out_display_basename = g_filename_display_basename(out_filename);
414             simple_error_message_box(
415                         "An error occurred while writing to the file \"%s\": %s.",
416                         out_display_basename, wtap_strerror(err));
417             g_free(out_display_basename);
418             break;
419         }
420         g_free(in_file_string);
421     } else {
422         /* OS error. */
423         write_failure_alert_box(out_filename, err);
424     }
425 }
426
427 /*
428  * Alert box for a failed attempt to close a capture file.
429  * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value.
430  *
431  * When closing a capture file:
432  *
433  *    some information in the file that can't be determined until
434  *    all packets have been written might be written to the file
435  *    (such as a table of the file offsets of all packets);
436  *
437  *    data buffered in the low-level file writing code might be
438  *    flushed to the file;
439  *
440  *    for remote file systems, data written to the file but not
441  *    yet sent to the server might be sent to the server or, if
442  *    that data was sent asynchronously, "out of space", "disk
443  *    quota exceeded", or "I/O error" indications might have
444  *    been received but not yet delivered, and the close operation
445  *    could deliver them;
446  *
447  * so we have to check for write errors here.
448  *
449  * XXX - add explanatory secondary text for at least some of the errors;
450  * various HIGs suggest that you should, for example, suggest that the
451  * user remove files if the file system is full.  Perhaps that's because
452  * they're providing guidelines for people less sophisticated than the
453  * typical Wireshark user is, but....
454  */
455 void
456 cfile_close_failure_alert_box(const char *filename, int err)
457 {
458     gchar *display_basename;
459
460     if (err < 0) {
461         /* Wiretap error. */
462         display_basename = g_filename_display_basename(filename);
463         switch (err) {
464
465         case WTAP_ERR_CANT_CLOSE:
466             simple_error_message_box(
467                         "The file \"%s\" couldn't be closed for some unknown reason.",
468                         display_basename);
469             break;
470
471         case WTAP_ERR_SHORT_WRITE:
472             simple_error_message_box(
473                         "A full write couldn't be done to the file \"%s\".",
474                         display_basename);
475             break;
476
477         default:
478             simple_error_message_box(
479                         "An error occurred while closing the file \"%s\": %s.",
480                         display_basename, wtap_strerror(err));
481             break;
482         }
483         g_free(display_basename);
484     } else {
485         /* OS error.
486            We assume that a close error from the OS is really a write error. */
487         write_failure_alert_box(filename, err);
488     }
489 }
490
491 /*
492  * Alert box for a failed attempt to open or create a file.
493  * "err" is assumed to be a UNIX-style errno; "for_writing" is TRUE if
494  * the file is being opened for writing and FALSE if it's being opened
495  * for reading.
496  *
497  * XXX - add explanatory secondary text for at least some of the errors;
498  * various HIGs suggest that you should, for example, suggest that the
499  * user remove files if the file system is full.  Perhaps that's because
500  * they're providing guidelines for people less sophisticated than the
501  * typical Wireshark user is, but....
502  */
503 void
504 open_failure_alert_box(const char *filename, int err, gboolean for_writing)
505 {
506     gchar *display_basename;
507
508     display_basename = g_filename_display_basename(filename);
509     simple_message_box(ESD_TYPE_ERROR, NULL, NULL,
510                         file_open_error_message(err, for_writing),
511                         display_basename);
512     g_free(display_basename);
513 }
514
515 /*
516  * Alert box for a failed attempt to read a file.
517  * "err" is assumed to be a UNIX-style errno.
518  */
519 void
520 read_failure_alert_box(const char *filename, int err)
521 {
522     gchar *display_basename;
523
524     display_basename = g_filename_display_basename(filename);
525     simple_message_box(ESD_TYPE_ERROR, NULL, NULL,
526                        "An error occurred while reading from the file \"%s\": %s.",
527                        display_basename, g_strerror(err));
528     g_free(display_basename);
529 }
530
531 /*
532  * Alert box for a failed attempt to write to a file.
533  * "err" is assumed to be a UNIX-style errno if positive and a
534  * Wiretap error if negative.
535  *
536  * XXX - add explanatory secondary text for at least some of the errors;
537  * various HIGs suggest that you should, for example, suggest that the
538  * user remove files if the file system is full.  Perhaps that's because
539  * they're providing guidelines for people less sophisticated than the
540  * typical Wireshark user is, but....
541  */
542 void
543 write_failure_alert_box(const char *filename, int err)
544 {
545     gchar *display_basename;
546
547     display_basename = g_filename_display_basename(filename);
548     if (err < 0) {
549         switch (err) {
550
551         case WTAP_ERR_SHORT_WRITE:
552             simple_message_box(ESD_TYPE_ERROR, NULL, NULL,
553                               "A full write couldn't be done to the file \"%s\".",
554                               display_basename);
555         break;
556
557         default:
558             simple_message_box(ESD_TYPE_ERROR, NULL, NULL,
559                               "An error occurred while writing to the file \"%s\": %s.",
560                               display_basename, wtap_strerror(err));
561         break;
562         }
563     } else {
564         simple_message_box(ESD_TYPE_ERROR, NULL, NULL,
565                            file_write_error_message(err), display_basename);
566     }
567     g_free(display_basename);
568 }
569
570 /*
571  * Editor modelines
572  *
573  * Local Variables:
574  * c-basic-offset: 4
575  * tab-width: 8
576  * indent-tabs-mode: nil
577  * End:
578  *
579  * ex: set shiftwidth=4 tabstop=8 expandtab:
580  * :indentSize=4:tabSize=8:noTabs=true:
581  */