diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a26f26b79..dee3c45fa5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2304,6 +2304,7 @@ if(BUILD_tshark) ${M_LIBRARIES} ) set(tshark_FILES + cli_main.c capture_opts.c tshark-tap-register.c tshark.c @@ -2329,6 +2330,7 @@ if(BUILD_tfshark) ${APPLE_SYSTEM_CONFIGURATION_LIBRARY} ) set(tfshark_FILES + cli_main.c tfshark.c ${TSHARK_TAP_SRC} ${SHARK_COMMON_SRC} @@ -2352,6 +2354,7 @@ if(BUILD_rawshark AND PCAP_FOUND) ${APPLE_SYSTEM_CONFIGURATION_LIBRARY} ) set(rawshark_FILES + cli_main.c ${SHARK_COMMON_SRC} rawshark.c ) @@ -2374,6 +2377,10 @@ if(BUILD_sharkd) ${APPLE_SYSTEM_CONFIGURATION_LIBRARY} ) set(sharkd_FILES + # + # XXX - currently doesn't work on Windows if it uses + # cli_main.c and has real_main(). + # sharkd.c sharkd_daemon.c sharkd_session.c @@ -2419,6 +2426,7 @@ if(BUILD_randpkt) ${ZLIB_LIBRARIES} ) set(randpkt_FILES + cli_main.c randpkt.c version_info.c ) @@ -2441,6 +2449,7 @@ if(BUILD_text2pcap) ${ZLIB_LIBRARIES} ) set(text2pcap_FILES + cli_main.c text2pcap.c version_info.c ) @@ -2464,6 +2473,7 @@ if(BUILD_mergecap) ${CMAKE_DL_LIBS} ) set(mergecap_FILES + cli_main.c mergecap.c version_info.c ) @@ -2483,6 +2493,7 @@ if(BUILD_reordercap) ${CMAKE_DL_LIBS} ) set(reordercap_FILES + cli_main.c reordercap.c version_info.c ) @@ -2504,6 +2515,7 @@ if(BUILD_capinfos) ${CMAKE_DL_LIBS} ) set(capinfos_FILES + cli_main.c capinfos.c version_info.c ) @@ -2524,6 +2536,7 @@ if(BUILD_captype) ${CMAKE_DL_LIBS} ) set(captype_FILES + cli_main.c captype.c version_info.c ) @@ -2544,6 +2557,7 @@ if(BUILD_editcap) ${CMAKE_DL_LIBS} ) set(editcap_FILES + cli_main.c editcap.c version_info.c ) @@ -2571,6 +2585,7 @@ if(BUILD_dumpcap AND PCAP_FOUND) ${NL_LIBRARIES} ) set(dumpcap_FILES + cli_main.c capture_opts.c dumpcap.c ringbuffer.c @@ -2924,6 +2939,7 @@ install( set(SHARK_PUBLIC_HEADERS cfile.h + cli_main.h file.h globals.h log.h diff --git a/capinfos.c b/capinfos.c index ddcf1f5633..0b06e42207 100644 --- a/capinfos.c +++ b/capinfos.c @@ -63,6 +63,7 @@ #include #include #include +#include #include #include @@ -80,10 +81,6 @@ #include "wsutil/wsgetopt.h" #endif -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - #include "ui/failure_message.h" #define INVALID_OPTION 1 @@ -1403,7 +1400,7 @@ hash_to_str(const unsigned char *hash, size_t length, char *str) { } } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -1730,23 +1727,6 @@ exit: return overall_error_status; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/captype.c b/captype.c index 99d8251e27..50de540f41 100644 --- a/captype.c +++ b/captype.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #ifdef HAVE_PLUGINS @@ -42,10 +43,6 @@ #include #include -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - #ifndef HAVE_GETOPT_LONG #include "wsutil/wsgetopt.h" #endif @@ -81,7 +78,7 @@ failure_message_cont(const char *msg_format, va_list ap) fprintf(stderr, "\n"); } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -205,23 +202,6 @@ real_main(int argc, char *argv[]) return overall_error_status; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/cli_main.c b/cli_main.c new file mode 100644 index 0000000000..ee5f67e93c --- /dev/null +++ b/cli_main.c @@ -0,0 +1,73 @@ +/* + * Compile and link this with all CLI programs where the main routine + * should get UTF-8 arguments on Windows. In those programs, declare + * the main program as real_main() rather than main(). + * + * This is used in software licensed under the GPLv2, and its license MUST + * be compatible with that license. + * + * This is used in software licensed under the Apache 2.0 license, and its + * license MUST be compatible with that license. + * + * For that purpose, we use the MIT (X11) license. + * + * SPDX-License-Identifier: MIT + */ + +#include "cli_main.h" + +#ifdef _WIN32 +#include +#include +#include + +int +wmain(int argc, wchar_t *wc_argv[]) +{ + char **argv; + int i; + + argv = (char **)malloc((argc + 1) * sizeof(char *)); + if (argv == NULL) { + fprintf(stderr, "Out of memory for converted argument list\n"); + return 2; + } + for (i = 0; i < argc; i++) { + /* + * XXX = use WC_ERR_INVALID_CHARS rather than 0, and fail if + * the argument isn't valid UTF-16? + */ + int width; + char *utf8_string; + + width = WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, NULL, 0, + NULL, NULL); + if (width == 0) { + fprintf(stderr, "WideCharToMultiByte failed: %d\n", + width); + return 2; + } + utf8_string = malloc(width); + if (utf8_string == NULL) { + fprintf(stderr, + "Out of memory for converted argument list\n"); + return 2; + } + if (WideCharToMultiByte(CP_UTF8, 0, wc_argv[i], -1, utf8_string, + width, NULL, NULL) == 0) { + fprintf(stderr, "WideCharToMultiByte failed: %d\n", + width); + return 2; + } + argv[i] = utf8_string; + } + argv[i] = NULL; + return real_main(argc, argv); +} +#else /* _WIN32 */ +int +main(int argc, char *argv[]) +{ + return real_main(argc, argv); +} +#endif diff --git a/cli_main.h b/cli_main.h new file mode 100644 index 0000000000..fc947b4ce3 --- /dev/null +++ b/cli_main.h @@ -0,0 +1,19 @@ +/* + * Declaration of the real main routine, for all CLI programs where the + * main routine should get UTF-8 arguments on Windows. In those programs, + * in the file that defines the main routine, include this header and define + * the main routine as real_main() rather than main(), and build those + * programs with cli_main.c and link with the object file. + * + * This is used in software licensed under the GPLv2, and its license MUST + * be compatible with that license. + * + * This is used in software licensed under the Apache 2.0 license, and its + * license MUST be compatible with that license. + * + * For that purpose, we use the MIT (X11) license. + * + * SPDX-License-Identifier: MIT + */ + +extern int real_main(int argc, char *argv[]); diff --git a/dumpcap.c b/dumpcap.c index 31652fe07f..f36ce588bd 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #ifndef HAVE_GETOPT_LONG @@ -65,10 +66,6 @@ #include "writecap/pcapio.h" -#ifdef _WIN32 -#include -#endif - #ifndef _WIN32 #include #endif @@ -4613,7 +4610,7 @@ get_dumpcap_runtime_info(GString *str) } /* And now our feature presentation... [ fade to music ] */ -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -5385,23 +5382,6 @@ real_main(int argc, char *argv[]) return 0; /* never here, make compiler happy */ } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - static void console_log_handler(const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer user_data _U_) diff --git a/editcap.c b/editcap.c index a963822b8c..78b83b6a2f 100644 --- a/editcap.c +++ b/editcap.c @@ -54,7 +54,6 @@ #endif #ifdef _WIN32 -#include #include /* getpid */ #include #endif @@ -74,6 +73,7 @@ #include #include #include +#include #include #include #include @@ -982,7 +982,7 @@ editcap_dump_open(const char *filename, const wtap_dump_params *params, return pdh; } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -2055,23 +2055,6 @@ clean_exit: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* Skip meta-information read from file to return offset of real * protocol data */ static int diff --git a/extcap/CMakeLists.txt b/extcap/CMakeLists.txt index 8475a39598..93581b550b 100644 --- a/extcap/CMakeLists.txt +++ b/extcap/CMakeLists.txt @@ -96,6 +96,7 @@ if(BUILD_androiddump) ) endif() set(androiddump_FILES + ../cli_main.c androiddump.c extcap-base.c ) @@ -117,6 +118,7 @@ if(BUILD_sshdump AND LIBSSH_FOUND) ${LIBSSH_LIBRARIES} ) set(sshdump_FILES + ../cli_main.c sshdump.c extcap-base.c ssh-base.c @@ -142,6 +144,7 @@ if(BUILD_ciscodump AND LIBSSH_FOUND) ${LIBSSH_LIBRARIES} ) set(ciscodump_FILES + ../cli_main.c ciscodump.c extcap-base.c ssh-base.c @@ -190,6 +193,7 @@ if(BUILD_udpdump) writecap ) set(udpdump_FILES + ../cli_main.c udpdump.c extcap-base.c ) @@ -212,6 +216,7 @@ if(BUILD_randpktdump) ${CMAKE_DL_LIBS} ) set(randpktdump_FILES + ../cli_main.c extcap-base.c randpktdump.c ) diff --git a/extcap/androiddump.c b/extcap/androiddump.c index 979a3704c1..5b2d1dfcfa 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -76,6 +76,8 @@ #include "wiretap/pcap-encap.h" #endif +#include + #ifdef ANDROIDDUMP_USE_LIBPCAP #define EXTCAP_ENCAP_BLUETOOTH_H4_WITH_PHDR DLT_BLUETOOTH_H4_WITH_PHDR #define EXTCAP_ENCAP_WIRESHARK_UPPER_PDU DLT_WIRESHARK_UPPER_PDU @@ -2743,23 +2745,6 @@ end: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 49c3ecbbf2..798cb7ac50 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -23,6 +23,8 @@ #include #include +#include + #define CISCODUMP_VERSION_MAJOR "1" #define CISCODUMP_VERSION_MINOR "0" #define CISCODUMP_VERSION_RELEASE "0" @@ -706,23 +708,6 @@ end: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/extcap/extcap-base.h b/extcap/extcap-base.h index 021c2f5015..f5112ab622 100644 --- a/extcap/extcap-base.h +++ b/extcap/extcap-base.h @@ -29,7 +29,6 @@ #ifdef _WIN32 #include - #include // arg_list_utf_16to8 #endif #include diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index f7639dcb57..647d291ff9 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -18,6 +18,8 @@ #include #include +#include + #define RANDPKT_EXTCAP_INTERFACE "randpkt" #define RANDPKTDUMP_VERSION_MAJOR "0" #define RANDPKTDUMP_VERSION_MINOR "1" @@ -318,23 +320,6 @@ end: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/extcap/sshdump.c b/extcap/sshdump.c index 4796b857aa..67ee1ee605 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -23,6 +23,8 @@ #include #include +#include + #define SSHDUMP_VERSION_MAJOR "1" #define SSHDUMP_VERSION_MINOR "0" #define SSHDUMP_VERSION_RELEASE "0" @@ -531,23 +533,6 @@ end: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - https://www.wireshark.org/tools/modelines.html * diff --git a/extcap/udpdump.c b/extcap/udpdump.c index ac74f2421b..444f637200 100644 --- a/extcap/udpdump.c +++ b/extcap/udpdump.c @@ -48,6 +48,8 @@ #include #include +#include + #define PCAP_SNAPLEN 0xffff #define UDPDUMP_DEFAULT_PORT 5555 @@ -475,23 +477,6 @@ end: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/mergecap.c b/mergecap.c index 0425efc2f3..21798ad974 100644 --- a/mergecap.c +++ b/mergecap.c @@ -37,6 +37,8 @@ #include #include #include + +#include #include #ifdef HAVE_PLUGINS @@ -47,10 +49,6 @@ #include -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - #include "ui/failure_message.h" /* @@ -229,7 +227,7 @@ merge_callback(merge_event event, int num, return FALSE; } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -479,23 +477,6 @@ clean_exit: return (status == MERGE_OK) ? 0 : 2; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/randpkt.c b/randpkt.c index a39cbf53de..ce98df6a83 100644 --- a/randpkt.c +++ b/randpkt.c @@ -17,10 +17,10 @@ #include #include #include -#include #include #include #include +#include #ifdef HAVE_PLUGINS #include @@ -99,7 +99,7 @@ usage(gboolean is_error) fprintf(output, "\nIf type is not specified, a random packet will be chosen\n\n"); } -static int +int real_main(int argc, char **argv) { char *init_progfile_dir_error; @@ -246,23 +246,6 @@ clean_exit: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t **wc_argv) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char **argv) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/rawshark.c b/rawshark.c index 317e6bfd86..15b7b4c933 100644 --- a/rawshark.c +++ b/rawshark.c @@ -53,6 +53,7 @@ #include #include #include +#include #include "globals.h" #include @@ -73,7 +74,6 @@ #include #include #include -#include #include "epan/column-utils.h" #include "epan/proto.h" #include @@ -82,7 +82,7 @@ #include #include -#include +#include #include #include "caputils/capture-pcap-util.h" @@ -403,7 +403,7 @@ set_link_type(const char *lt_arg) { return FALSE; } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -828,23 +828,6 @@ clean_exit: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /** * Read data from a raw pipe. The "raw" data consists of a libpcap * packet header followed by the payload. diff --git a/reordercap.c b/reordercap.c index 56d4ae5e33..ce85acbeef 100644 --- a/reordercap.c +++ b/reordercap.c @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include @@ -161,7 +161,7 @@ failure_message_cont(const char *msg_format, va_list ap) /********************************************************************/ /* Main function. */ /********************************************************************/ -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -380,23 +380,6 @@ clean_exit: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/text2pcap.c b/text2pcap.c index 0eaf1588f1..5ac0e9c7a3 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -101,6 +101,7 @@ #include #include #include +#include #include #include @@ -132,10 +133,6 @@ #include "wiretap/wtap.h" -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - /*--- Options --------------------------------------------------------------------*/ /* File format */ @@ -1874,7 +1871,7 @@ parse_options (int argc, char *argv[]) return EXIT_SUCCESS; } -static int +int real_main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; @@ -1946,23 +1943,6 @@ clean_exit: return ret; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/tfshark.c b/tfshark.c index 0a44c4eba9..52f8fb13a4 100644 --- a/tfshark.c +++ b/tfshark.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include "globals.h" @@ -66,10 +67,6 @@ #include #include -#ifdef _WIN32 -#include -#endif /* _WIN32 */ - #include "log.h" #include @@ -303,7 +300,7 @@ get_tfshark_runtime_version_info(GString *str) epan_get_runtime_version_info(str); } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -1005,23 +1002,6 @@ clean_exit: return exit_status; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - static const nstime_t * tfshark_get_frame_ts(struct packet_provider_data *prov, guint32 frame_num) { diff --git a/tools/pre-commit-ignore.conf b/tools/pre-commit-ignore.conf index 7fd791662b..6adee4c0da 100644 --- a/tools/pre-commit-ignore.conf +++ b/tools/pre-commit-ignore.conf @@ -18,6 +18,7 @@ epan/wmem/wmem_strutil.c epan/wslua/init_wslua.c extcap/* image/stock_icons/* +cli_main.c mmdbresolve.c tools/lemon/* wsutil/file_util.h diff --git a/tshark.c b/tshark.c index 9ee2180199..eb0304d058 100644 --- a/tshark.c +++ b/tshark.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -104,7 +105,6 @@ #include "caputils/capture_ifinfo.h" #ifdef _WIN32 #include "caputils/capture-wpcap.h" -#include #endif /* _WIN32 */ #include #include @@ -670,7 +670,7 @@ must_do_dissection(dfilter_t *rfcode, dfilter_t *dfcode, tap_listeners_require_dissection() || dissect_color; } -static int +int real_main(int argc, char *argv[]) { GString *comp_info_str; @@ -2255,23 +2255,6 @@ clean_exit: return exit_status; } -#ifdef _WIN32 -int -wmain(int argc, wchar_t *wc_argv[]) -{ - char **argv; - - argv = arg_list_utf_16to8(argc, wc_argv); - return real_main(argc, argv); -} -#else -int -main(int argc, char *argv[]) -{ - return real_main(argc, argv); -} -#endif - /*#define USE_BROKEN_G_MAIN_LOOP*/ #ifdef USE_BROKEN_G_MAIN_LOOP