From a24687ce8eba24d3065bd38c139abfd7720fbb7b Mon Sep 17 00:00:00 2001 From: Gerald Combs Date: Tue, 24 May 2011 00:07:56 +0000 Subject: Move the Windows argument list conversion code to a common routine. svn path=/trunk/; revision=37372 --- Makefile.nmake | 12 ++++++------ capinfos.c | 16 ++-------------- dumpcap.c | 11 +---------- editcap.c | 22 +++++----------------- gtk/main.c | 11 ++--------- mergecap.c | 16 ++-------------- randpkt.c | 17 ++--------------- rawshark.c | 12 ++---------- text2pcap.c | 15 ++------------- tshark.c | 12 ++---------- wsutil/Makefile.nmake | 2 +- wsutil/libwsutil.def | 1 + wsutil/unicode-utils.c | 16 ++++++++++++++++ wsutil/unicode-utils.h | 12 ++++++++++++ 14 files changed, 56 insertions(+), 119 deletions(-) diff --git a/Makefile.nmake b/Makefile.nmake index 06bc4704af..cbc3522e4b 100644 --- a/Makefile.nmake +++ b/Makefile.nmake @@ -93,7 +93,7 @@ wireshark_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ !ENDIF tshark_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ - wsock32.lib user32.lib shell32.lib \ + wsock32.lib user32.lib \ $(GLIB_LIBS) \ wsutil\libwsutil.lib \ $(GNUTLS_LIBS) \ @@ -113,7 +113,7 @@ tshark_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ !ENDIF rawshark_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ - wsock32.lib user32.lib shell32.lib \ + wsock32.lib user32.lib \ $(GLIB_LIBS) \ wsutil\libwsutil.lib \ $(GNUTLS_LIBS) \ @@ -144,17 +144,17 @@ editcap_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ $(GLIB_LIBS) mergecap_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ - wsock32.lib user32.lib shell32.lib \ + wsock32.lib user32.lib \ wsutil\libwsutil.lib \ $(GLIB_LIBS) text2pcap_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ - wsock32.lib user32.lib shell32.lib \ + wsock32.lib user32.lib \ wsutil\libwsutil.lib \ $(GLIB_LIBS) dumpcap_LIBS= \ - wsock32.lib user32.lib shell32.lib \ + wsock32.lib user32.lib \ wsutil\libwsutil.lib \ $(GLIB_LIBS) \ $(GTHREAD_LIBS) @@ -178,7 +178,7 @@ dftest_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ !ENDIF randpkt_LIBS= wiretap\wiretap-$(WTAP_VERSION).lib \ - user32.lib shell32.lib \ + user32.lib \ wsutil\libwsutil.lib \ $(GLIB_LIBS) diff --git a/capinfos.c b/capinfos.c index a5c6bd40e0..88af0b5237 100644 --- a/capinfos.c +++ b/capinfos.c @@ -95,7 +95,7 @@ #endif #ifdef _WIN32 -#include +#include #endif /* _WIN32 */ #include "svnversion.h" @@ -847,11 +847,6 @@ main(int argc, char *argv[]) int opt; int overall_error_status; -#ifdef _WIN32 - LPWSTR *wc_argv; - int wc_argc, i; -#endif /* _WIN32 */ - int status = 0; #ifdef HAVE_PLUGINS char *init_progfile_dir_error; @@ -864,13 +859,7 @@ main(int argc, char *argv[]) #endif #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* @@ -1114,4 +1103,3 @@ main(int argc, char *argv[]) } return overall_error_status; } - diff --git a/dumpcap.c b/dumpcap.c index 17946b6d12..c83a39bc99 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -85,7 +85,6 @@ #include "pcapio.h" #ifdef _WIN32 -#include #include "capture-wpcap.h" #include #endif @@ -3645,8 +3644,6 @@ main(int argc, char *argv[]) #ifdef _WIN32 WSADATA wsaData; - LPWSTR *wc_argv; - int wc_argc; #else struct sigaction action, oldaction; #endif @@ -3670,13 +3667,7 @@ main(int argc, char *argv[]) #endif #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ #ifdef _WIN32 diff --git a/editcap.c b/editcap.c index 3c70c7f14d..f8af5e1356 100644 --- a/editcap.c +++ b/editcap.c @@ -49,8 +49,7 @@ #endif #ifdef _WIN32 -#include -#include +#include #include /* getpid */ #ifdef HAVE_WINSOCK2_H #include @@ -745,13 +744,13 @@ struct string_elem { static gint string_compare(gconstpointer a, gconstpointer b) { - return strcmp(((struct string_elem *)a)->sstr, + return strcmp(((struct string_elem *)a)->sstr, ((struct string_elem *)b)->sstr); -} +} static void string_elem_print(gpointer data, gpointer not_used _U_) -{ +{ fprintf(stderr, " %s - %s\n", ((struct string_elem *)data)->sstr, ((struct string_elem *)data)->lstr); @@ -818,11 +817,6 @@ main(int argc, char *argv[]) gchar *err_info; int opt; -#ifdef _WIN32 - LPWSTR *wc_argv; - int wc_argc; -#endif /* _WIN32 */ - char *p; unsigned int snaplen = 0; /* No limit */ int choplen = 0; /* No chop */ @@ -849,13 +843,7 @@ main(int argc, char *argv[]) #endif #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* diff --git a/gtk/main.c b/gtk/main.c index d1ea6a0e85..36c915ce0d 100644 --- a/gtk/main.c +++ b/gtk/main.c @@ -118,6 +118,7 @@ #include "../capture-wpcap.h" #include "../capture_wpcap_packet.h" #include /* Needed for Unicode */ +#include #include #include #endif /* _WIN32 */ @@ -2006,8 +2007,6 @@ main(int argc, char *argv[]) #ifdef _WIN32 WSADATA wsaData; - LPWSTR *wc_argv; - int wc_argc, i; #endif /* _WIN32 */ char *rf_path; @@ -2065,13 +2064,7 @@ main(int argc, char *argv[]) static const char optstring[] = OPTSTRING; #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* diff --git a/mergecap.c b/mergecap.c index 460f938ba9..9c658cb433 100644 --- a/mergecap.c +++ b/mergecap.c @@ -42,8 +42,7 @@ #endif #ifdef _WIN32 -#include -#include +#include #endif /* _WIN32 */ static int @@ -149,11 +148,6 @@ main(int argc, char *argv[]) { int opt; -#ifdef _WIN32 - LPWSTR *wc_argv; - int wc_argc; -#endif /* _WIN32 */ - gboolean do_append = FALSE; gboolean verbose = FALSE; int in_file_count = 0; @@ -174,13 +168,7 @@ main(int argc, char *argv[]) int count; #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* Process the options first */ diff --git a/randpkt.c b/randpkt.c index a9d157ac52..fbd77535c1 100644 --- a/randpkt.c +++ b/randpkt.c @@ -51,8 +51,7 @@ #include "wiretap/wtap.h" #ifdef _WIN32 -#include -#include +#include #endif /* _WIN32 */ #define array_length(x) (sizeof x / sizeof x[0]) @@ -508,12 +507,6 @@ main(int argc, char **argv) int opt; -#ifdef _WIN32 - LPWSTR *wc_argv; - int wc_argc; -#endif /* _WIN32 */ - - int produce_count = 1000; /* number of pkts to produce */ int produce_type = PKT_ETHERNET; char *produce_filename = NULL; @@ -521,13 +514,7 @@ main(int argc, char **argv) pkt_example *example; #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ while ((opt = getopt(argc, argv, "b:c:ht:")) != -1) { diff --git a/rawshark.c b/rawshark.c index 58c31ff172..3d90e32679 100644 --- a/rawshark.c +++ b/rawshark.c @@ -118,7 +118,7 @@ #include "log.h" #ifdef _WIN32 -#include +#include #endif /* _WIN32 */ /* @@ -435,8 +435,6 @@ main(int argc, char *argv[]) #ifdef _WIN32 WSADATA wsaData; - LPWSTR *wc_argv; - int wc_argc; #endif /* _WIN32 */ char *gpf_path, *pf_path; @@ -460,13 +458,7 @@ main(int argc, char *argv[]) static const char optstring[] = OPTSTRING_INIT; #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* diff --git a/text2pcap.c b/text2pcap.c index 558ca35cca..18df1c917b 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -139,8 +139,7 @@ #include "svnversion.h" #ifdef _WIN32 -#include -#include +#include #endif /* _WIN32 */ /*--- Options --------------------------------------------------------------------*/ @@ -1131,19 +1130,9 @@ parse_options (int argc, char *argv[]) { int c; char *p; -#ifdef _WIN32 - LPWSTR *wc_argv; - int wc_argc, i; -#endif /* _WIN32 */ #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* Scan CLI parameters */ diff --git a/tshark.c b/tshark.c index 5db00c8da9..022384f128 100644 --- a/tshark.c +++ b/tshark.c @@ -96,7 +96,7 @@ #ifdef _WIN32 #include "capture-wpcap.h" #include "capture_errs.h" -#include +#include #endif /* _WIN32 */ #include "capture_sync.h" #endif /* HAVE_LIBPCAP */ @@ -798,8 +798,6 @@ main(int argc, char *argv[]) #ifdef _WIN32 WSADATA wsaData; - LPWSTR *wc_argv; - int wc_argc, i; #endif /* _WIN32 */ char *gpf_path, *pf_path; @@ -854,13 +852,7 @@ main(int argc, char *argv[]) static const char optstring[] = OPTSTRING; #ifdef _WIN32 - /* Convert our arg list to UTF-8. */ - wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); - if (wc_argv && wc_argc == argc) { - for (i = 0; i < argc; i++) { - argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); - } - } /* XXX else bail because something is horribly, horribly wrong? */ + arg_list_utf_16to8(argc, argv); #endif /* _WIN32 */ /* diff --git a/wsutil/Makefile.nmake b/wsutil/Makefile.nmake index 8d4c376b9c..38f5718b5f 100644 --- a/wsutil/Makefile.nmake +++ b/wsutil/Makefile.nmake @@ -35,7 +35,7 @@ libwsutil.lib: libwsutil.dll libwsutil.exp: libwsutil.dll libwsutil.dll : $(OBJECTS) libwsutil.def ..\image\libwsutil.res - $(link) $(dlllflags) $(conlibsdll) \ + $(link) $(dlllflags) $(conlibsdll) shell32.lib \ $(LOCAL_LDFLAGS) $(DLL_LDFLAGS) \ /DEF:libwsutil.def /OUT:libwsutil.dll \ /IMPLIB:libwsutil.lib \ diff --git a/wsutil/libwsutil.def b/wsutil/libwsutil.def index 5abb4d2b76..7aadd7e848 100644 --- a/wsutil/libwsutil.def +++ b/wsutil/libwsutil.def @@ -66,6 +66,7 @@ type_util_guint64_to_gdouble utf_16to8 utf_8to16 utf_8to16_snprintf +arg_list_utf_16to8 ; wsgetopt.c getopt diff --git a/wsutil/unicode-utils.c b/wsutil/unicode-utils.c index 80541a5784..567ef180ca 100644 --- a/wsutil/unicode-utils.c +++ b/wsutil/unicode-utils.c @@ -28,6 +28,8 @@ #include "unicode-utils.h" +#include + /** @file * Unicode utilities (internal interface) * @@ -141,3 +143,17 @@ utf_16to8(const wchar_t *utf16str) return utf8buf[idx]; } +/* Convert our argument list from UTF-16 to UTF-8. */ +void +arg_list_utf_16to8(int argc, char *argv[]) { + LPWSTR *wc_argv; + int wc_argc, i; + + /* Convert our arg list to UTF-8. */ + wc_argv = CommandLineToArgvW(GetCommandLineW(), &wc_argc); + if (wc_argv && wc_argc == argc) { + for (i = 0; i < argc; i++) { + argv[i] = g_utf16_to_utf8(wc_argv[i], -1, NULL, NULL, NULL); + } + } /* XXX else bail because something is horribly, horribly wrong? */ +} diff --git a/wsutil/unicode-utils.h b/wsutil/unicode-utils.h index f3c423ffd8..a03a172732 100644 --- a/wsutil/unicode-utils.h +++ b/wsutil/unicode-utils.h @@ -66,6 +66,18 @@ void utf_8to16_snprintf(TCHAR *utf16buf, gint utf16buf_len, const gchar* fmt, .. */ gchar * utf_16to8(const wchar_t *utf16str); +/** Convert the program argument list from UTF-16 to UTF-8 and + * store it in the supplied array. This is intended to be used + * to normalize command line arguments at program startup. + * + * @param argc The number of arguments. You should simply pass the + * first argument from main(). + * @param argv The argument values (vector). You should simply pass + * the second argument from main(). + */ +void arg_list_utf_16to8(int argc, char *argv[]); + + #endif /* _WIN32 */ #endif /* __UNICODEUTIL_H__ */ -- cgit v1.2.3