diff --git a/CMakeLists.txt b/CMakeLists.txt index 94475bd71d..b0ef9dea85 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2425,6 +2425,7 @@ endif() if(BUILD_reordercap) set(reordercap_LIBS + ui wiretap ${ZLIB_LIBRARIES} ${CMAKE_DL_LIBS} @@ -2443,6 +2444,7 @@ endif() if(BUILD_capinfos) set(capinfos_LIBS + ui wiretap wsutil ${ZLIB_LIBRARIES} @@ -2463,6 +2465,7 @@ endif() if(BUILD_captype) set(captype_LIBS + ui wiretap wsutil ${ZLIB_LIBRARIES} diff --git a/Makefile.am b/Makefile.am index d0e9378fe3..aa0c626c72 100644 --- a/Makefile.am +++ b/Makefile.am @@ -642,6 +642,7 @@ capinfos_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS) # Libraries with which to link capinfos. capinfos_LDADD = \ + ui/libui.a \ wiretap/libwiretap.la \ wsutil/libwsutil.la \ @GLIB_LIBS@ \ @@ -655,6 +656,7 @@ captype_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS) # Libraries with which to link captype. captype_LDADD = \ + ui/libui.a \ wiretap/libwiretap.la \ wsutil/libwsutil.la \ @GLIB_LIBS@ @@ -681,6 +683,7 @@ reordercap_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS) # Libraries with which to link reordercap. reordercap_LDADD = \ + ui/libui.a \ wiretap/libwiretap.la \ wsutil/libwsutil.la \ @GLIB_LIBS@ diff --git a/capinfos.c b/capinfos.c index 507ff05e30..647b6cb093 100644 --- a/capinfos.c +++ b/capinfos.c @@ -71,6 +71,7 @@ #include +#include #include #include #include @@ -95,6 +96,8 @@ #include #endif /* _WIN32 */ +#include "ui/failure_message.h" + #define INVALID_OPTION 1 #define BAD_FLAG 1 @@ -1219,8 +1222,9 @@ process_cap_file(wtap *wth, const char *filename) if (err != 0) { fprintf(stderr, - "capinfos: An error occurred after reading %u packets from \"%s\": %s.\n", - packet, filename, wtap_strerror(err)); + "capinfos: An error occurred after reading %u packets from \"%s\".\n", + packet, filename); + cfile_read_failure_message("capinfos", filename, err, err_info); if (err == WTAP_ERR_SHORT_READ) { /* Don't give up completely with this one. */ status = 1; @@ -1376,7 +1380,6 @@ print_usage(FILE *output) fprintf(output, "output format.\n"); } -#ifdef HAVE_PLUGINS /* * General errors and warnings are reported with an console message * in capinfos. @@ -1388,7 +1391,16 @@ failure_warning_message(const char *msg_format, va_list ap) vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -#endif + +/* + * Report additional information for an error in command-line arguments. + */ +static void +failure_message_cont(const char *msg_format, va_list ap) +{ + vfprintf(stderr, msg_format, ap); + fprintf(stderr, "\n"); +} static void hash_to_str(const unsigned char *hash, size_t length, char *str) { @@ -1425,6 +1437,8 @@ main(int argc, char *argv[]) /* Set the C-language locale to the native environment. */ setlocale(LC_ALL, ""); + cmdarg_err_init(failure_warning_message, failure_message_cont); + /* Get the decimal point. */ decimal_point = g_strdup(localeconv()->decimal_point); @@ -1711,12 +1725,8 @@ main(int argc, char *argv[]) wth = wtap_open_offline(argv[opt], WTAP_TYPE_AUTO, &err, &err_info, FALSE); if (!wth) { - fprintf(stderr, "capinfos: Can't open %s: %s\n", argv[opt], - wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + cfile_open_failure_message("capinfos", argv[opt], err, err_info, FALSE, + WTAP_TYPE_AUTO); overall_error_status = 2; /* remember that an error has occurred */ if (!continue_after_wtap_open_offline_failure) goto exit; diff --git a/captype.c b/captype.c index a61ba4bc81..71fe572d93 100644 --- a/captype.c +++ b/captype.c @@ -40,6 +40,7 @@ #include +#include #include #include #include @@ -61,6 +62,8 @@ #include "wsutil/wsgetopt.h" #endif +#include "ui/failure_message.h" + static void print_usage(FILE *output) { @@ -68,7 +71,6 @@ print_usage(FILE *output) fprintf(output, "Usage: captype ...\n"); } -#ifdef HAVE_PLUGINS /* * General errors and warnings are reported with an console message * in captype. @@ -80,7 +82,16 @@ failure_warning_message(const char *msg_format, va_list ap) vfprintf(stderr, msg_format, ap); fprintf(stderr, "\n"); } -#endif + +/* + * Report additional information for an error in command-line arguments. + */ +static void +failure_message_cont(const char *msg_format, va_list ap) +{ + vfprintf(stderr, msg_format, ap); + fprintf(stderr, "\n"); +} int main(int argc, char *argv[]) @@ -103,6 +114,8 @@ main(int argc, char *argv[]) /* Set the C-language locale to the native environment. */ setlocale(LC_ALL, ""); + cmdarg_err_init(failure_warning_message, failure_message_cont); + /* Get the compile-time version information string */ comp_info_str = get_compiled_version_info(NULL, NULL); @@ -206,12 +219,8 @@ main(int argc, char *argv[]) if (err == WTAP_ERR_FILE_UNKNOWN_FORMAT) printf("%s: unknown\n", argv[i]); else { - fprintf(stderr, "captype: Can't open %s: %s\n", argv[i], - wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + cfile_open_failure_message("captype", argv[i], err, err_info, FALSE, + WTAP_TYPE_AUTO); overall_error_status = 2; /* remember that an error has occurred */ } } diff --git a/editcap.c b/editcap.c index e1bcd88b22..3e684fe234 100644 --- a/editcap.c +++ b/editcap.c @@ -930,8 +930,8 @@ failure_warning_message(const char *msg_format, va_list ap) static void failure_message_cont(const char *msg_format, va_list ap) { - vfprintf(stderr, msg_format, ap); - fprintf(stderr, "\n"); + vfprintf(stderr, msg_format, ap); + fprintf(stderr, "\n"); } static wtap_dumper * @@ -1423,8 +1423,9 @@ main(int argc, char *argv[]) shb_hdrs, idb_inf, nrb_hdrs, &write_err); if (pdh == NULL) { - fprintf(stderr, "editcap: Can't open or create %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_open_failure_message("editcap", filename, + write_err, NULL, TRUE, + out_frame_type); ret = INVALID_FILE; goto clean_exit; } @@ -1447,8 +1448,7 @@ main(int argc, char *argv[]) && phdr->ts.nsecs >= block_start.nsecs )) { /* time for the next file */ if (!wtap_dump_close(pdh, &write_err)) { - fprintf(stderr, "editcap: Error writing to %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_close_failure_message(filename, write_err); ret = WRITE_ERROR; goto clean_exit; } @@ -1465,8 +1465,9 @@ main(int argc, char *argv[]) shb_hdrs, idb_inf, nrb_hdrs, &write_err); if (pdh == NULL) { - fprintf(stderr, "editcap: Can't open or create %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_open_failure_message("editcap", filename, + write_err, NULL, TRUE, + out_frame_type); ret = INVALID_FILE; goto clean_exit; } @@ -1478,8 +1479,7 @@ main(int argc, char *argv[]) /* time for the next file? */ if (written_count > 0 && (written_count % split_packet_count) == 0) { if (!wtap_dump_close(pdh, &write_err)) { - fprintf(stderr, "editcap: Error writing to %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_close_failure_message(filename, write_err); ret = WRITE_ERROR; goto clean_exit; } @@ -1495,8 +1495,9 @@ main(int argc, char *argv[]) snaplen ? MIN(snaplen, wtap_snapshot_length(wth)) : wtap_snapshot_length(wth), shb_hdrs, idb_inf, nrb_hdrs, &write_err); if (pdh == NULL) { - fprintf(stderr, "editcap: Can't open or create %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_open_failure_message("editcap", filename, + write_err, NULL, TRUE, + out_frame_type); ret = INVALID_FILE; goto clean_exit; } @@ -1793,13 +1794,8 @@ main(int argc, char *argv[]) if (read_err != 0) { /* Print a message noting that the read failed somewhere along the * line. */ - fprintf(stderr, - "editcap: An error occurred while reading \"%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_read_failure_message("editcap", argv[optind], read_err, + read_err_info); } if (!pdh) { @@ -1812,16 +1808,16 @@ main(int argc, char *argv[]) snaplen ? MIN(snaplen, wtap_snapshot_length(wth)): wtap_snapshot_length(wth), shb_hdrs, idb_inf, nrb_hdrs, &write_err); if (pdh == NULL) { - fprintf(stderr, "editcap: Can't open or create %s: %s\n", - filename, wtap_strerror(write_err)); + cfile_open_failure_message("editcap", filename, + write_err, NULL, TRUE, + out_frame_type); ret = INVALID_FILE; goto clean_exit; } } if (!wtap_dump_close(pdh, &write_err)) { - fprintf(stderr, "editcap: Error writing to %s: %s\n", filename, - wtap_strerror(write_err)); + cfile_close_failure_message(filename, write_err); ret = WRITE_ERROR; goto clean_exit; } diff --git a/reordercap.c b/reordercap.c index ca55267009..7b44527ece 100644 --- a/reordercap.c +++ b/reordercap.c @@ -51,6 +51,8 @@ #include +#include "ui/failure_message.h" + #define INVALID_OPTION 1 #define OPEN_ERROR 2 #define OUTPUT_FILE_ERROR 1 @@ -92,7 +94,8 @@ typedef struct FrameRecord_t { static void frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, - struct wtap_pkthdr *phdr, Buffer *buf, const char *infile) + struct wtap_pkthdr *phdr, Buffer *buf, const char *infile, + const char *outfile) { int err; gchar *err_info; @@ -106,12 +109,9 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ fprintf(stderr, - "reordercap: An error occurred while re-reading \"%s\": %s.\n", - infile, wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + "reordercap: An error occurred while re-reading \"%s\".\n", + infile); + cfile_read_failure_message("reordercap", infile, err, err_info); exit(1); } } @@ -123,12 +123,8 @@ frame_write(FrameRecord_t *frame, wtap *wth, wtap_dumper *pdh, /* Dump frame to outfile */ if (!wtap_dump(pdh, phdr, ws_buffer_start_ptr(buf), &err, &err_info)) { - fprintf(stderr, "reordercap: Error (%s) writing frame to outfile\n", - wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + cfile_write_failure_message(infile, outfile, err, err_info, + frame->num, wtap_file_type_subtype(wth)); exit(1); } } @@ -298,12 +294,8 @@ main(int argc, char *argv[]) open_routine reader to use, then the following needs to change. */ wth = wtap_open_offline(infile, WTAP_TYPE_AUTO, &err, &err_info, TRUE); if (wth == NULL) { - fprintf(stderr, "reordercap: Can't open %s: %s\n", infile, - wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + cfile_open_failure_message("reordercap", infile, err, err_info, + FALSE, WTAP_TYPE_AUTO); ret = OPEN_ERROR; goto clean_exit; } @@ -317,7 +309,6 @@ main(int argc, char *argv[]) if (strcmp(outfile, "-") == 0) { pdh = wtap_dump_open_stdout_ng(wtap_file_type_subtype(wth), wtap_file_encap(wth), wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); - outfile = "standard output"; } else { pdh = wtap_dump_open_ng(outfile, wtap_file_type_subtype(wth), wtap_file_encap(wth), wtap_snapshot_length(wth), FALSE, shb_hdrs, idb_inf, nrb_hdrs, &err); @@ -326,8 +317,8 @@ main(int argc, char *argv[]) idb_inf = NULL; if (pdh == NULL) { - fprintf(stderr, "reordercap: Failed to open output file: (%s) - error %s\n", - outfile, wtap_strerror(err)); + cfile_open_failure_message("reordercap", outfile, err, err_info, TRUE, + wtap_file_type_subtype(wth)); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); ret = OUTPUT_FILE_ERROR; @@ -361,13 +352,7 @@ main(int argc, char *argv[]) } if (err != 0) { /* Print a message noting that the read failed somewhere along the line. */ - fprintf(stderr, - "reordercap: An error occurred while reading \"%s\": %s.\n", - infile, wtap_strerror(err)); - if (err_info != NULL) { - fprintf(stderr, "(%s)\n", err_info); - g_free(err_info); - } + cfile_read_failure_message("reordercap", infile, err, err_info); } printf("%u frames, %u out of order\n", frames->len, wrong_order_count); @@ -385,7 +370,7 @@ main(int argc, char *argv[]) /* Avoid writing if already sorted and configured to */ if (write_output_regardless || (wrong_order_count > 0)) { - frame_write(frame, wth, pdh, &dump_phdr, &buf, infile); + frame_write(frame, wth, pdh, &dump_phdr, &buf, infile, outfile); } g_slice_free(FrameRecord_t, frame); } @@ -401,8 +386,7 @@ main(int argc, char *argv[]) /* Close outfile */ if (!wtap_dump_close(pdh, &err)) { - fprintf(stderr, "reordercap: Error closing %s: %s\n", outfile, - wtap_strerror(err)); + cfile_close_failure_message(outfile, err); wtap_block_array_free(shb_hdrs); wtap_block_array_free(nrb_hdrs); ret = OUTPUT_FILE_ERROR; diff --git a/tshark.c b/tshark.c index 73286c3eed..7cfd911194 100644 --- a/tshark.c +++ b/tshark.c @@ -643,6 +643,7 @@ main(int argc, char *argv[]) {0, 0, 0, 0 } }; gboolean arg_error = FALSE; + const char *exp_pdu_filename = NULL; #ifdef _WIN32 int result; @@ -1864,7 +1865,6 @@ main(int argc, char *argv[]) /* PDU export requested. Take the ownership of the '-w' file, apply tap * filters and start tapping. */ if (pdu_export_arg) { - const char *exp_pdu_filename; const char *exp_pdu_tap_name = pdu_export_arg; const char *exp_pdu_filter = dfilter; /* may be NULL to disable filter */ char *exp_pdu_error; @@ -1978,7 +1978,7 @@ main(int argc, char *argv[]) if (pdu_export_arg) { err = exp_pdu_close(&exp_pdu_tap_data); if (err) { - cmdarg_err("%s", wtap_strerror(err)); + cfile_close_failure_message(exp_pdu_filename, err); exit_status = 2; } g_free(pdu_export_arg);