diff --git a/Makefile.am b/Makefile.am index 4209983d6b..d0e9378fe3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -667,6 +667,7 @@ editcap_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS) # Libraries with which to link editcap. editcap_LDADD = \ + ui/libui.a \ wiretap/libwiretap.la \ wsutil/libwsutil.la \ @GLIB_LIBS@ \ diff --git a/editcap.c b/editcap.c index 5efa2cbc8f..e1bcd88b22 100644 --- a/editcap.c +++ b/editcap.c @@ -92,6 +92,8 @@ #include #include +#include "ui/failure_message.h" + #include "ringbuffer.h" /* For RINGBUFFER_MAX_NUM_FILES */ #define INVALID_OPTION 1 @@ -1349,12 +1351,8 @@ main(int argc, char *argv[]) wth = wtap_open_offline(argv[optind], WTAP_TYPE_AUTO, &read_err, &read_err_info, FALSE); if (!wth) { - fprintf(stderr, "editcap: Can't open %s: %s\n", argv[optind], - wtap_strerror(read_err)); - if (read_err_info != NULL) { - fprintf(stderr, "(%s)\n", read_err_info); - g_free(read_err_info); - } + cfile_open_failure_message("editap", argv[optind], read_err, + read_err_info, FALSE, WTAP_TYPE_AUTO); ret = INVALID_FILE; goto clean_exit; } @@ -1777,66 +1775,10 @@ main(int argc, char *argv[]) /* Attempt to dump out current frame to the output file */ if (!wtap_dump(pdh, phdr, buf, &write_err, &write_err_info)) { - switch (write_err) { - case WTAP_ERR_UNWRITABLE_ENCAP: - /* - * This is a problem with the particular frame we're - * writing and the file type and subtype we're - * writing; note that, and report the frame number - * and file type/subtype. - */ - fprintf(stderr, - "editcap: Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n", - read_count, argv[optind], - wtap_file_type_subtype_string(out_file_type_subtype)); - break; - - case WTAP_ERR_PACKET_TOO_LARGE: - /* - * This is a problem with the particular frame we're - * writing and the file type and subtype we're - * writing; note that, and report the frame number - * and file type/subtype. - */ - fprintf(stderr, - "editcap: Frame %u of \"%s\" is too large for a \"%s\" file.\n", - read_count, argv[optind], - wtap_file_type_subtype_string(out_file_type_subtype)); - break; - - case WTAP_ERR_UNWRITABLE_REC_TYPE: - /* - * This is a problem with the particular record we're - * writing and the file type and subtype we're - * writing; note that, and report the record number - * and file type/subtype. - */ - fprintf(stderr, - "editcap: Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n", - read_count, argv[optind], - wtap_file_type_subtype_string(out_file_type_subtype)); - break; - - case WTAP_ERR_UNWRITABLE_REC_DATA: - /* - * This is a problem with the particular record we're - * writing and the file type and subtype we're - * writing; note that, and report the record number - * and file type/subtype. - */ - fprintf(stderr, - "editcap: Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n", - read_count, argv[optind], - wtap_file_type_subtype_string(out_file_type_subtype), - write_err_info != NULL ? write_err_info : "no information supplied"); - g_free(write_err_info); - break; - - default: - fprintf(stderr, "editcap: Error writing to %s: %s\n", - filename, wtap_strerror(write_err)); - break; - } + cfile_write_failure_message(argv[optind], filename, + write_err, write_err_info, + read_count, + out_file_type_subtype); ret = DUMP_ERROR; goto clean_exit; } diff --git a/rawshark.c b/rawshark.c index 0476223095..bcb2f8ca5d 100644 --- a/rawshark.c +++ b/rawshark.c @@ -82,6 +82,7 @@ #endif #include "ui/util.h" #include "ui/dissect_opts.h" +#include "ui/failure_message.h" #include "register.h" #include "conditions.h" #include "capture_stop_conditions.h" @@ -983,39 +984,7 @@ load_cap_file(capture_file *cf) g_free(pd); if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ - switch (err) { - - case WTAP_ERR_UNSUPPORTED: - cmdarg_err("The file \"%s\" contains record data that Rawshark doesn't support.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_SHORT_READ: - cmdarg_err("The file \"%s\" appears to have been cut short in the middle of a packet.", - cf->filename); - break; - - case WTAP_ERR_BAD_FILE: - cmdarg_err("The file \"%s\" appears to be damaged or corrupt.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_DECOMPRESS: - cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - cmdarg_err("An error occurred while reading the file \"%s\": %s.", - cf->filename, wtap_strerror(err)); - break; - } + cfile_read_failure_message("Rawshark", cf->filename, err, err_info); return FALSE; } diff --git a/sharkd.c b/sharkd.c index 91c7a186aa..435fd07809 100644 --- a/sharkd.c +++ b/sharkd.c @@ -61,6 +61,7 @@ #include "ui/decode_as_utils.h" #include "ui/filter_files.h" #include "ui/tap_export_pdu.h" +#include "ui/failure_message.h" #include "register.h" #include #include @@ -87,9 +88,6 @@ static frame_data ref_frame; static frame_data *prev_dis; static frame_data *prev_cap; -static const char *cf_open_error_message(int err, gchar *err_info, - gboolean for_writing, int file_type); - static void failure_warning_message(const char *msg_format, va_list ap); static void open_failure_message(const char *filename, int err, gboolean for_writing); @@ -425,39 +423,7 @@ load_cap_file(capture_file *cf, int max_packet_count, gint64 max_byte_count) } if (err != 0) { - switch (err) { - - case WTAP_ERR_UNSUPPORTED: - cmdarg_err("The file \"%s\" contains record data that sharkd doesn't support.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_SHORT_READ: - cmdarg_err("The file \"%s\" appears to have been cut short in the middle of a packet.", - cf->filename); - break; - - case WTAP_ERR_BAD_FILE: - cmdarg_err("The file \"%s\" appears to be damaged or corrupt.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_DECOMPRESS: - cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n" - "(%s)", cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - cmdarg_err("An error occurred while reading the file \"%s\": %s.", - cf->filename, wtap_strerror(err)); - break; - } + cfile_read_failure_message("sharkd", cf->filename, err, err_info); } return err; @@ -468,7 +434,6 @@ cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_temp { wtap *wth; gchar *err_info; - char err_msg[2048+1]; wth = wtap_open_offline(fname, type, err, &err_info, TRUE); if (wth == NULL) @@ -519,131 +484,10 @@ cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_temp return CF_OK; fail: - g_snprintf(err_msg, sizeof err_msg, - cf_open_error_message(*err, err_info, FALSE, cf->cd_t), fname); - cmdarg_err("%s", err_msg); + cfile_open_failure_message("sharkd", fname, *err, err_info, FALSE, cf->cd_t); return CF_ERROR; } -static const char * -cf_open_error_message(int err, gchar *err_info, gboolean for_writing, - int file_type) -{ - const char *errmsg; - static char errmsg_errno[1024+1]; - - if (err < 0) { - /* Wiretap error. */ - switch (err) { - - case WTAP_ERR_NOT_REGULAR_FILE: - errmsg = "The file \"%s\" is a \"special file\" or socket or other non-regular file."; - break; - - case WTAP_ERR_RANDOM_OPEN_PIPE: - /* Seen only when opening a capture file for reading. */ - errmsg = "The file \"%s\" is a pipe or FIFO; sharkd can't read pipe or FIFO files in two-pass mode."; - break; - - case WTAP_ERR_FILE_UNKNOWN_FORMAT: - /* Seen only when opening a capture file for reading. */ - errmsg = "The file \"%s\" isn't a capture file in a format sharkd understands."; - break; - - case WTAP_ERR_UNSUPPORTED: - /* Seen only when opening a capture file for reading. */ - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "The file \"%%s\" contains record data that sharkd doesn't support.\n" - "(%s)", - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - errmsg = errmsg_errno; - break; - - case WTAP_ERR_CANT_WRITE_TO_PIPE: - /* Seen only when opening a capture file for writing. */ - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "The file \"%%s\" is a pipe, and \"%s\" capture files can't be " - "written to a pipe.", wtap_file_type_subtype_short_string(file_type)); - errmsg = errmsg_errno; - break; - - case WTAP_ERR_UNWRITABLE_FILE_TYPE: - /* Seen only when opening a capture file for writing. */ - errmsg = "sharkd doesn't support writing capture files in that format."; - break; - - case WTAP_ERR_UNWRITABLE_ENCAP: - /* Seen only when opening a capture file for writing. */ - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "sharkd can't save this capture as a \"%s\" file.", - wtap_file_type_subtype_short_string(file_type)); - errmsg = errmsg_errno; - break; - - case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: - if (for_writing) { - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "sharkd can't save this capture as a \"%s\" file.", - wtap_file_type_subtype_short_string(file_type)); - errmsg = errmsg_errno; - } else - errmsg = "The file \"%s\" is a capture for a network type that sharkd doesn't support."; - break; - - case WTAP_ERR_BAD_FILE: - /* Seen only when opening a capture file for reading. */ - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "The file \"%%s\" appears to be damaged or corrupt.\n" - "(%s)", - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - errmsg = errmsg_errno; - break; - - case WTAP_ERR_CANT_OPEN: - if (for_writing) - errmsg = "The file \"%s\" could not be created for some unknown reason."; - else - errmsg = "The file \"%s\" could not be opened for some unknown reason."; - break; - - case WTAP_ERR_SHORT_READ: - errmsg = "The file \"%s\" appears to have been cut short" - " in the middle of a packet or other data."; - break; - - case WTAP_ERR_SHORT_WRITE: - errmsg = "A full header couldn't be written to the file \"%s\"."; - break; - - case WTAP_ERR_COMPRESSION_NOT_SUPPORTED: - errmsg = "This file type cannot be written as a compressed file."; - break; - - case WTAP_ERR_DECOMPRESS: - /* Seen only when opening a capture file for reading. */ - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "The compressed file \"%%s\" appears to be damaged or corrupt.\n" - "(%s)", - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - errmsg = errmsg_errno; - break; - - default: - g_snprintf(errmsg_errno, sizeof(errmsg_errno), - "The file \"%%s\" could not be %s: %s.", - for_writing ? "created" : "opened", - wtap_strerror(err)); - errmsg = errmsg_errno; - break; - } - } else - errmsg = file_open_error_message(err, for_writing); - return errmsg; -} - /* * General errors and warnings are reported with an console message * in sharkd. diff --git a/tshark.c b/tshark.c index 429418eb73..73286c3eed 100644 --- a/tshark.c +++ b/tshark.c @@ -89,6 +89,7 @@ #include "ui/cli/tap-exportobject.h" #include "ui/tap_export_pdu.h" #include "ui/dissect_opts.h" +#include "ui/failure_message.h" #if defined(HAVE_LIBSMI) #include "epan/oids.h" #endif @@ -231,15 +232,12 @@ static int load_cap_file(capture_file *, char *, int, gboolean, int, gint64); static gboolean process_packet_single_pass(capture_file *cf, epan_dissect_t *edt, gint64 offset, struct wtap_pkthdr *whdr, const guchar *pd, guint tap_flags); -static void show_capture_file_io_error(const char *, int, gboolean); static void show_print_file_io_error(int err); static gboolean write_preamble(capture_file *cf); static gboolean print_packet(capture_file *cf, epan_dissect_t *edt); static gboolean write_finale(void); static void failure_warning_message(const char *msg_format, va_list ap); -static void cfile_open_failure_message(const char *filename, int err, - gchar *err_info, gboolean for_writing, int file_type); static void open_failure_message(const char *filename, int err, gboolean for_writing); static void read_failure_message(const char *filename, int err); @@ -502,22 +500,6 @@ tshark_log_handler (const gchar *log_domain, GLogLevelFlags log_level, } -static char * -output_file_description(const char *fname) -{ - char *save_file_string; - - /* Get a string that describes what we're writing to */ - if (strcmp(fname, "-") == 0) { - /* We're writing to to the standard output */ - save_file_string = g_strdup("standard output"); - } else { - /* We're writing to a file with the name in save_file */ - save_file_string = g_strdup_printf("file \"%s\"", fname); - } - return save_file_string; -} - static void print_current_user(void) { gchar *cur_user, *cur_group; @@ -1928,8 +1910,8 @@ main(int argc, char *argv[]) comment = g_strdup_printf("Dump of PDUs from %s", cf_name); err = exp_pdu_open(&exp_pdu_tap_data, exp_fd, comment); if (err != 0) { - cfile_open_failure_message(exp_pdu_filename, err, NULL, TRUE, - WTAP_FILE_TYPE_SUBTYPE_PCAPNG); + cfile_open_failure_message("TShark", exp_pdu_filename, err, NULL, + TRUE, WTAP_FILE_TYPE_SUBTYPE_PCAPNG); g_free(comment); exit_status = INVALID_EXPORT; goto clean_exit; @@ -3039,7 +3021,8 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, if (pdh == NULL) { /* We couldn't set up to write to the capture file. */ - cfile_open_failure_message(save_file, err, NULL, TRUE, out_file_type); + cfile_open_failure_message("TShark", save_file, err, NULL, TRUE, + out_file_type); goto out; } } else { @@ -3173,74 +3156,12 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, if (!wtap_dump(pdh, &phdr, ws_buffer_start_ptr(&buf), &err, &err_info)) { /* Error writing to a capture file */ tshark_debug("tshark: error writing to a capture file (%d)", err); - switch (err) { - case WTAP_ERR_UNWRITABLE_ENCAP: - /* - * This is a problem with the particular frame we're writing - * and the file type and subtype we're writing; note that, - * and report the frame number and file type/subtype. - * - * XXX - framenum is not necessarily the frame number in - * the input file if there was a read filter. - */ - fprintf(stderr, - "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_PACKET_TOO_LARGE: - /* - * This is a problem with the particular frame we're writing - * and the file type and subtype we're writing; note that, - * and report the frame number and file type/subtype. - * - * XXX - framenum is not necessarily the frame number in - * the input file if there was a read filter. - */ - fprintf(stderr, - "Frame %u of \"%s\" is too large for a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_UNWRITABLE_REC_TYPE: - /* - * This is a problem with the particular record we're writing - * and the file type and subtype we're writing; note that, - * and report the record number and file type/subtype. - * - * XXX - framenum is not necessarily the record number in - * the input file if there was a read filter. - */ - fprintf(stderr, - "Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_UNWRITABLE_REC_DATA: - /* - * This is a problem with the particular record we're writing - * and the file type and subtype we're writing; note that, - * and report the record number and file type/subtype. - * - * XXX - framenum is not necessarily the record number in - * the input file if there was a read filter. - */ - fprintf(stderr, - "Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type), - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - show_capture_file_io_error(save_file, err, FALSE); - break; - } + /* Report the error. + XXX - framenum is not necessarily the frame number in + the input file if there was a read filter. */ + cfile_write_failure_message(cf->filename, save_file, err, + err_info, framenum, out_file_type); wtap_dump_close(pdh, &err); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); @@ -3318,62 +3239,8 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, if (!wtap_dump(pdh, wtap_phdr(cf->wth), wtap_buf_ptr(cf->wth), &err, &err_info)) { /* Error writing to a capture file */ tshark_debug("tshark: error writing to a capture file (%d)", err); - switch (err) { - - case WTAP_ERR_UNWRITABLE_ENCAP: - /* - * This is a problem with the particular frame we're writing - * and the file type and subtype we're writing; note that, - * and report the frame number and file type/subtype. - */ - fprintf(stderr, - "Frame %u of \"%s\" has a network type that can't be saved in a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_PACKET_TOO_LARGE: - /* - * This is a problem with the particular frame we're writing - * and the file type and subtype we're writing; note that, - * and report the frame number and file type/subtype. - */ - fprintf(stderr, - "Frame %u of \"%s\" is too large for a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_UNWRITABLE_REC_TYPE: - /* - * This is a problem with the particular record we're writing - * and the file type and subtype we're writing; note that, - * and report the record number and file type/subtype. - */ - fprintf(stderr, - "Record %u of \"%s\" has a record type that can't be saved in a \"%s\" file.\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type)); - break; - - case WTAP_ERR_UNWRITABLE_REC_DATA: - /* - * This is a problem with the particular record we're writing - * and the file type and subtype we're writing; note that, - * and report the record number and file type/subtype. - */ - fprintf(stderr, - "Record %u of \"%s\" has data that can't be saved in a \"%s\" file.\n(%s)\n", - framenum, cf->filename, - wtap_file_type_subtype_short_string(out_file_type), - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - show_capture_file_io_error(save_file, err, FALSE); - break; - } + cfile_write_failure_message(cf->filename, save_file, err, err_info, + framenum, out_file_type); wtap_dump_close(pdh, &err); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); @@ -3428,43 +3295,11 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, } } #endif - switch (err) { - - case WTAP_ERR_UNSUPPORTED: - cmdarg_err("The file \"%s\" contains record data that TShark doesn't support.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_SHORT_READ: - cmdarg_err("The file \"%s\" appears to have been cut short in the middle of a packet.", - cf->filename); - break; - - case WTAP_ERR_BAD_FILE: - cmdarg_err("The file \"%s\" appears to be damaged or corrupt.\n(%s)", - cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_DECOMPRESS: - cmdarg_err("The compressed file \"%s\" appears to be damaged or corrupt.\n" - "(%s)", cf->filename, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - cmdarg_err("An error occurred while reading the file \"%s\": %s.", - cf->filename, wtap_strerror(err)); - break; - } + cfile_read_failure_message("TShark", cf->filename, err, err_info); if (save_file != NULL) { /* Now close the capture file. */ if (!wtap_dump_close(pdh, &err)) - show_capture_file_io_error(save_file, err, TRUE); + cfile_close_failure_message(save_file, err); } } else { if (save_file != NULL) { @@ -3476,7 +3311,7 @@ load_cap_file(capture_file *cf, char *save_file, int out_file_type, } /* Now close the capture file. */ if (!wtap_dump_close(pdh, &err)) - show_capture_file_io_error(save_file, err, TRUE); + cfile_close_failure_message(save_file, err); } else { if (print_packet_info) { if (!write_finale()) { @@ -4103,56 +3938,10 @@ cf_open(capture_file *cf, const char *fname, unsigned int type, gboolean is_temp return CF_OK; fail: - cfile_open_failure_message(fname, *err, err_info, FALSE, cf->cd_t); + cfile_open_failure_message("TShark", fname, *err, err_info, FALSE, cf->cd_t); return CF_ERROR; } -static void -show_capture_file_io_error(const char *fname, int err, gboolean is_close) -{ - char *save_file_string; - - save_file_string = output_file_description(fname); - - switch (err) { - - case ENOSPC: - cmdarg_err("Not all the packets could be written to the %s because there is " - "no space left on the file system.", - save_file_string); - break; - -#ifdef EDQUOT - case EDQUOT: - cmdarg_err("Not all the packets could be written to the %s because you are " - "too close to, or over your disk quota.", - save_file_string); - break; -#endif - - case WTAP_ERR_CANT_CLOSE: - cmdarg_err("The %s couldn't be closed for some unknown reason.", - save_file_string); - break; - - case WTAP_ERR_SHORT_WRITE: - cmdarg_err("Not all the packets could be written to the %s.", - save_file_string); - break; - - default: - if (is_close) { - cmdarg_err("The %s could not be closed: %s.", save_file_string, - wtap_strerror(err)); - } else { - cmdarg_err("An error occurred while writing to the %s: %s.", - save_file_string, wtap_strerror(err)); - } - break; - } - g_free(save_file_string); -} - static void show_print_file_io_error(int err) { @@ -4192,132 +3981,6 @@ failure_warning_message(const char *msg_format, va_list ap) /* * Open/create errors are reported with an console message in TShark. */ -static void -cfile_open_failure_message(const char *filename, int err, gchar *err_info, - gboolean for_writing, int file_type) -{ - char *file_description; - - /* Get a string that describes what we're opening */ - if (strcmp(filename, "-") == 0) { - /* We're opening the standard input/output */ - file_description = g_strdup(for_writing ? "standard output" : "standard input"); - } else { - /* We're opening a file with the name in filename */ - file_description = g_strdup_printf("file \"%s\"", filename); - } - if (err < 0) { - /* Wiretap error. */ - switch (err) { - - case WTAP_ERR_NOT_REGULAR_FILE: - cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.", - file_description); - break; - - case WTAP_ERR_RANDOM_OPEN_PIPE: - /* Seen only when opening a capture file for reading. */ - cmdarg_err("The %s is a pipe or FIFO; TShark can't read pipe or FIFO files in two-pass mode.", - file_description); - break; - - case WTAP_ERR_FILE_UNKNOWN_FORMAT: - /* Seen only when opening a capture file for reading. */ - cmdarg_err("The %s isn't a capture file in a format TShark understands.", - file_description); - break; - - case WTAP_ERR_UNSUPPORTED: - /* Seen only when opening a capture file for reading. */ - cmdarg_err("The %s contains record data that TShark doesn't support.\n" - "(%s)", - file_description, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_CANT_WRITE_TO_PIPE: - /* Seen only when opening a capture file for writing. */ - cmdarg_err("The %s is a pipe, and \"%s\" capture files can't be written to a pipe.", - file_description, - wtap_file_type_subtype_short_string(file_type)); - break; - - case WTAP_ERR_UNWRITABLE_FILE_TYPE: - /* Seen only when opening a capture file for writing. */ - cmdarg_err("TShark doesn't support writing capture files in that format."); - break; - - case WTAP_ERR_UNWRITABLE_ENCAP: - /* Seen only when opening a capture file for writing. */ - cmdarg_err("The capture file being read can't be written as a \"%s\" file.", - wtap_file_type_subtype_short_string(file_type)); - break; - - case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: - if (for_writing) { - cmdarg_err("The capture file being read can't be written as a \"%s\" file.", - wtap_file_type_subtype_short_string(file_type)); - } else { - cmdarg_err("The %s is a capture for a network type that TShark doesn't support.", - file_description); - } - break; - - case WTAP_ERR_BAD_FILE: - /* Seen only when opening a capture file for reading. */ - cmdarg_err("The %s appears to be damaged or corrupt.\n" - "(%s)", - file_description, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - case WTAP_ERR_CANT_OPEN: - if (for_writing) { - cmdarg_err("The %s could not be created for some unknown reason.", - file_description); - } else { - cmdarg_err("The %s could not be opened for some unknown reason.", - file_description); - } - break; - - case WTAP_ERR_SHORT_READ: - cmdarg_err("The %s appears to have been cut short in the middle of a packet or other data.", - file_description); - break; - - case WTAP_ERR_SHORT_WRITE: - cmdarg_err("A full header couldn't be written to the %s.", - file_description); - break; - - case WTAP_ERR_COMPRESSION_NOT_SUPPORTED: - cmdarg_err("This file type cannot be written as a compressed file."); - break; - - case WTAP_ERR_DECOMPRESS: - /* Seen only when opening a capture file for reading. */ - cmdarg_err("The %s is compressed and appears to be damaged or corrupt.\n" - "(%s)", - file_description, - err_info != NULL ? err_info : "no information supplied"); - g_free(err_info); - break; - - default: - cmdarg_err("The %s could not be %s: %s.", - file_description, - for_writing ? "created" : "opened", - wtap_strerror(err)); - break; - } - g_free(file_description); - } else - cmdarg_err(file_open_error_message(err, for_writing), filename); -} - static void open_failure_message(const char *filename, int err, gboolean for_writing) { @@ -4333,7 +3996,7 @@ static void read_failure_message(const char *filename, int err) { cmdarg_err("An error occurred while reading from the file \"%s\": %s.", - filename, g_strerror(err)); + filename, g_strerror(err)); } /* diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index b15a4dcf21..d1f778ee77 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -31,6 +31,7 @@ set(COMMON_UI_SRC export_object_ui.c export_pdu_ui_utils.c help_url.c + failure_message.c filter_files.c firewall_rules.c iface_lists.c diff --git a/ui/Makefile.am b/ui/Makefile.am index 0a698a60df..c7e984162e 100644 --- a/ui/Makefile.am +++ b/ui/Makefile.am @@ -57,6 +57,7 @@ WIRESHARK_UI_SRC = \ dissect_opts.c \ export_object_ui.c \ export_pdu_ui_utils.c \ + failure_message.c \ filter_files.c \ firewall_rules.c \ iface_lists.c \ @@ -101,6 +102,7 @@ WIRESHARK_UI_INCLUDES = \ export_object_ui.h \ export_pdu_ui_utils.h \ last_open_dir.h \ + failure_message.h \ file_dialog.h \ filter_files.h \ help_url.h \ diff --git a/ui/failure_message.c b/ui/failure_message.c new file mode 100644 index 0000000000..24b3a42ca2 --- /dev/null +++ b/ui/failure_message.c @@ -0,0 +1,413 @@ +/* failure_message.h + * Routines to print various "standard" failure messages used in multiple + * places + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include +#include + +#include +#include +#include + +#include "ui/failure_message.h" + +static char * +input_file_description(const char *fname) +{ + char *fstring; + + if (strcmp(fname, "-") == 0) { + /* We're reading from the standard input */ + fstring = g_strdup("standard input"); + } else { + /* We're reading from a file */ + fstring = g_strdup_printf("file \"%s\"", fname); + } + return fstring; +} + +static char * +output_file_description(const char *fname) +{ + char *fstring; + + if (strcmp(fname, "-") == 0) { + /* We're writing to to the standard output */ + fstring = g_strdup("standard output"); + } else { + /* We're writing to a file */ + fstring = g_strdup_printf("file \"%s\"", fname); + } + return fstring; +} + +/* + * Error message for a failed attempt to open or create a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values; "for_writing" is TRUE if the file is being + * opened for writing and FALSE if it's being opened for reading; + * "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type + * and subtype of file being opened for writing (it's ignored for + * opening-for-reading errors). + */ +void +cfile_open_failure_message(const char *progname, const char *filename, + int err, gchar *err_info, gboolean for_writing, + int file_type) +{ + char *file_description; + + /* Get a string that describes what we're opening */ + if (for_writing) + file_description = output_file_description(filename); + else + file_description = input_file_description(filename); + + if (err < 0) { + /* Wiretap error. */ + switch (err) { + + case WTAP_ERR_NOT_REGULAR_FILE: + cmdarg_err("The %s is a \"special file\" or socket or other non-regular file.", + file_description); + break; + + case WTAP_ERR_RANDOM_OPEN_PIPE: + /* Seen only when opening a capture file for reading. */ + cmdarg_err("The %s is a pipe or FIFO; %s can't read pipe or FIFO files in two-pass mode.", + file_description, progname); + break; + + case WTAP_ERR_FILE_UNKNOWN_FORMAT: + /* Seen only when opening a capture file for reading. */ + cmdarg_err("The %s isn't a capture file in a format %s understands.", + file_description, progname); + break; + + case WTAP_ERR_UNSUPPORTED: + /* Seen only when opening a capture file for reading. */ + cmdarg_err("The %s contains record data that %s doesn't support.\n" + "(%s)", + file_description, progname, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + case WTAP_ERR_CANT_WRITE_TO_PIPE: + /* Seen only when opening a capture file for writing. */ + cmdarg_err("The %s is a pipe, and \"%s\" capture files can't be written to a pipe.", + file_description, + wtap_file_type_subtype_short_string(file_type)); + break; + + case WTAP_ERR_UNWRITABLE_FILE_TYPE: + /* Seen only when opening a capture file for writing. */ + cmdarg_err("%s doesn't support writing capture files in that format.", + progname); + break; + + case WTAP_ERR_UNWRITABLE_ENCAP: + /* Seen only when opening a capture file for writing. */ + cmdarg_err("The capture file being read can't be written as a \"%s\" file.", + wtap_file_type_subtype_short_string(file_type)); + break; + + case WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED: + if (for_writing) { + cmdarg_err("The capture file being read can't be written as a \"%s\" file.", + wtap_file_type_subtype_short_string(file_type)); + } else { + cmdarg_err("The %s is a capture for a network type that %s doesn't support.", + file_description, progname); + } + break; + + case WTAP_ERR_BAD_FILE: + /* Seen only when opening a capture file for reading. */ + cmdarg_err("The %s appears to be damaged or corrupt.\n" + "(%s)", + file_description, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + case WTAP_ERR_CANT_OPEN: + if (for_writing) { + cmdarg_err("The %s could not be created for some unknown reason.", + file_description); + } else { + cmdarg_err("The %s could not be opened for some unknown reason.", + file_description); + } + break; + + case WTAP_ERR_SHORT_READ: + cmdarg_err("The %s appears to have been cut short in the middle of a packet or other data.", + file_description); + break; + + case WTAP_ERR_SHORT_WRITE: + cmdarg_err("A full header couldn't be written to the %s.", + file_description); + break; + + case WTAP_ERR_COMPRESSION_NOT_SUPPORTED: + cmdarg_err("This file type cannot be written as a compressed file."); + break; + + case WTAP_ERR_DECOMPRESS: + /* Seen only when opening a capture file for reading. */ + cmdarg_err("The %s is compressed and appears to be damaged or corrupt.\n" + "(%s)", + file_description, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + default: + cmdarg_err("The %s could not be %s: %s.", + file_description, + for_writing ? "created" : "opened", + wtap_strerror(err)); + break; + } + g_free(file_description); + } else + cmdarg_err(file_open_error_message(err, for_writing), filename); +} + +/* + * Error message for a failed attempt to read from a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values. + */ +void +cfile_read_failure_message(const char *progname, const char *filename, + int err, gchar *err_info) +{ + char *file_string; + + /* Get a string that describes what we're reading from */ + file_string = input_file_description(filename); + + switch (err) { + + case WTAP_ERR_UNSUPPORTED: + cmdarg_err("The %s contains record data that %s doesn't support.\n(%s)", + file_string, progname, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + case WTAP_ERR_SHORT_READ: + cmdarg_err("The %s appears to have been cut short in the middle of a packet.", + file_string); + break; + + case WTAP_ERR_BAD_FILE: + cmdarg_err("The %s appears to be damaged or corrupt.\n(%s)", + file_string, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + case WTAP_ERR_DECOMPRESS: + cmdarg_err("The %s is compressed and appears to be damaged or corrupt.\n" + "(%s)", file_string, + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + default: + cmdarg_err("An error occurred while reading the %s: %s.", + file_string, wtap_strerror(err)); + break; + } + g_free(file_string); +} + +/* + * Error message for a failed attempt to write to a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values; "framenum" is the frame number of the record + * on which the error occurred; "file_type_subtype" is a + * WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file being + * written. + */ +void +cfile_write_failure_message(const char *in_filename, const char *out_filename, + int err, gchar *err_info, guint32 framenum, + int file_type_subtype) +{ + char *in_file_string; + char *out_file_string; + + /* Get a string that describes what we're reading from */ + in_file_string = input_file_description(in_filename); + + /* Get a string that describes what we're writing to */ + out_file_string = output_file_description(out_filename); + + switch (err) { + + case WTAP_ERR_UNWRITABLE_ENCAP: + /* + * This is a problem with the particular frame we're writing + * and the file type and subtype we're writing; note that, + * and report the frame number and file type/subtype. + */ + cmdarg_err("Frame %u of %s has a network type that can't be saved in a \"%s\" file.\n", + framenum, in_file_string, + wtap_file_type_subtype_short_string(file_type_subtype)); + break; + + case WTAP_ERR_PACKET_TOO_LARGE: + /* + * This is a problem with the particular frame we're writing + * and the file type and subtype we're writing; note that, + * and report the frame number and file type/subtype. + */ + cmdarg_err("Frame %u of %s is too large for a \"%s\" file.\n", + framenum, in_file_string, + wtap_file_type_subtype_short_string(file_type_subtype)); + break; + + case WTAP_ERR_UNWRITABLE_REC_TYPE: + /* + * This is a problem with the particular record we're writing + * and the file type and subtype we're writing; note that, + * and report the record number and file type/subtype. + */ + cmdarg_err("Record %u of %s has a record type that can't be saved in a \"%s\" file.\n", + framenum, in_file_string, + wtap_file_type_subtype_short_string(file_type_subtype)); + break; + + case WTAP_ERR_UNWRITABLE_REC_DATA: + /* + * This is a problem with the particular record we're writing + * and the file type and subtype we're writing; note that, + * and report the record number and file type/subtype. + */ + cmdarg_err("Record %u of %s has data that can't be saved in a \"%s\" file.\n(%s)\n", + framenum, in_file_string, + wtap_file_type_subtype_short_string(file_type_subtype), + err_info != NULL ? err_info : "no information supplied"); + g_free(err_info); + break; + + case ENOSPC: + cmdarg_err("Not all the packets could be written to the %s because there is " + "no space left on the file system.", + out_file_string); + break; + +#ifdef EDQUOT + case EDQUOT: + cmdarg_err("Not all the packets could be written to the %s because you are " + "too close to, or over your disk quota.", + out_file_string); + break; +#endif + + case WTAP_ERR_SHORT_WRITE: + cmdarg_err("A full write couldn't be done to the %s.", + out_file_string); + break; + + default: + cmdarg_err("An error occurred while writing to the %s: %s.", + out_file_string, wtap_strerror(err)); + break; + } + g_free(in_file_string); + g_free(out_file_string); +} + +/* + * Error message for a failed attempt to close a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value. + * + * When closing a capture file: + * + * some information in the file that can't be determined until + * all packets have been written might be written to the file + * (such as a table of the file offsets of all packets); + * + * data buffered in the low-level file writing code might be + * flushed to the file; + * + * for remote file systems, data written to the file but not + * yet sent to the server might be sent to the server or, if + * that data was sent asynchronously, "out of space", "disk + * quota exceeded", or "I/O error" indications might have + * been received but not yet delivered, and the close operation + * could deliver them; + * + * so we have to check for write errors here. + */ +void +cfile_close_failure_message(const char *filename, int err) +{ + char *file_string; + + /* Get a string that describes what we're writing to */ + file_string = output_file_description(filename); + + switch (err) { + + case ENOSPC: + cmdarg_err("Not all the packets could be written to the %s because there is " + "no space left on the file system.", + file_string); + break; + +#ifdef EDQUOT + case EDQUOT: + cmdarg_err("Not all the packets could be written to the %s because you are " + "too close to, or over your disk quota.", + file_string); + break; +#endif + + case WTAP_ERR_CANT_CLOSE: + cmdarg_err("The %s couldn't be closed for some unknown reason.", + file_string); + break; + + case WTAP_ERR_SHORT_WRITE: + cmdarg_err("A full write couldn't be done to the %s.", + file_string); + break; + + default: + cmdarg_err("The %s could not be closed: %s.", file_string, + wtap_strerror(err)); + break; + } + g_free(file_string); +} diff --git a/ui/failure_message.h b/ui/failure_message.h new file mode 100644 index 0000000000..95814ff3ff --- /dev/null +++ b/ui/failure_message.h @@ -0,0 +1,112 @@ +/* failure_message.h + * Routines to print various "standard" failure messages used in multiple + * places + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __FAILURE_MESSAGE_H__ +#define __FAILURE_MESSAGE_H__ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * Error message for a failed attempt to open or create a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values; "for_writing" is TRUE if the file is being + * opened for writing and FALSE if it's being opened for reading; + * "file_type_subtype" is a WTAP_FILE_TYPE_SUBTYPE_ value for the type + * and subtype of file being opened for writing (it's ignored for + * opening-for-reading errors). + */ +extern void cfile_open_failure_message(const char *progname, + const char *filename, int err, + gchar *err_info, gboolean for_writing, + int file_type); + +/* + * Error message for a failed attempt to read from a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values. + */ +extern void cfile_read_failure_message(const char *progname, + const char *filename, int err, + gchar *err_info); + +/* + * Error message for a failed attempt to write to a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value; + * "err_info" is assumed to be a string giving further information for + * some WTAP_ERR_ values; "framenum" is the frame number of the record + * on which the error occurred; "file_type_subtype" is a + * WTAP_FILE_TYPE_SUBTYPE_ value for the type and subtype of file being + * written. + */ +extern void cfile_write_failure_message(const char *in_filename, + const char *out_filename, + int err, gchar *err_info, + guint32 framenum, + int file_type_subtype); + +/* + * Error message for a failed attempt to close a capture file. + * "err" is assumed to be a UNIX-style errno or a WTAP_ERR_ value. + * + * When closing a capture file: + * + * some information in the file that can't be determined until + * all packets have been written might be written to the file + * (such as a table of the file offsets of all packets); + * + * data buffered in the low-level file writing code might be + * flushed to the file; + * + * for remote file systems, data written to the file but not + * yet sent to the server might be sent to the server or, if + * that data was sent asynchronously, "out of space", "disk + * quota exceeded", or "I/O error" indications might have + * been received but not yet delivered, and the close operation + * could deliver them; + * + * so we have to check for write errors here. + */ +extern void cfile_close_failure_message(const char *filename, int err); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __FAILURE_MESSAGE_H__ */ + +/* + * Editor modelines + * + * Local Variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */