From 591515da1e99a8e42ab2770be1f05b54d25f9511 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Sat, 28 Jan 2023 02:32:13 +0900 Subject: [PATCH] tools: Make sure UTF-8 encoded command line arguments on Windows On Windows, arguments passed in main() are system codepage encoded and might not be valid UTF-8 string. Part-of: --- subprojects/gst-plugins-bad/tools/gst-transcoder.c | 23 ++++++++++++++++++--- .../gst-plugins-base/tools/gst-device-monitor.c | 23 ++++++++++++++++++--- .../gst-plugins-base/tools/gst-discoverer.c | 23 ++++++++++++++++++--- subprojects/gst-plugins-base/tools/gst-play.c | 23 ++++++++++++++++++--- subprojects/gstreamer/tools/gst-inspect.c | 24 +++++++++++++++++++--- subprojects/gstreamer/tools/gst-launch.c | 23 ++++++++++++++++++--- subprojects/gstreamer/tools/gst-stats.c | 16 ++++++++++++++- subprojects/gstreamer/tools/gst-typefind.c | 23 ++++++++++++++++++--- 8 files changed, 156 insertions(+), 22 deletions(-) diff --git a/subprojects/gst-plugins-bad/tools/gst-transcoder.c b/subprojects/gst-plugins-bad/tools/gst-transcoder.c index a1ba29c..dccf3eb 100644 --- a/subprojects/gst-plugins-bad/tools/gst-transcoder.c +++ b/subprojects/gst-plugins-bad/tools/gst-transcoder.c @@ -362,7 +362,12 @@ real_main (int argc, char *argv[]) g_option_context_add_main_entries (ctx, options, NULL); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); g_clear_error (&err); g_option_context_free (ctx); @@ -468,9 +473,21 @@ no_extension: int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gst-plugins-base/tools/gst-device-monitor.c b/subprojects/gst-plugins-base/tools/gst-device-monitor.c index d826ee2..aa65f59 100644 --- a/subprojects/gst-plugins-base/tools/gst-device-monitor.c +++ b/subprojects/gst-plugins-base/tools/gst-device-monitor.c @@ -324,7 +324,12 @@ real_main (int argc, char **argv) "[DEVICE_CLASSES[:FILTER_CAPS]] …"); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); g_option_context_free (ctx); g_clear_error (&err); @@ -409,9 +414,21 @@ real_main (int argc, char **argv) int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gst-plugins-base/tools/gst-discoverer.c b/subprojects/gst-plugins-base/tools/gst-discoverer.c index fefee4f..a50f5aa 100644 --- a/subprojects/gst-plugins-base/tools/gst-discoverer.c +++ b/subprojects/gst-plugins-base/tools/gst-discoverer.c @@ -671,7 +671,12 @@ real_main (int argc, char **argv) g_option_context_add_main_entries (ctx, options, NULL); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_print ("Error initializing: %s\n", err->message); g_option_context_free (ctx); g_clear_error (&err); @@ -738,9 +743,21 @@ real_main (int argc, char **argv) int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gst-plugins-base/tools/gst-play.c b/subprojects/gst-plugins-base/tools/gst-play.c index 7508414..ee8847d 100644 --- a/subprojects/gst-plugins-base/tools/gst-play.c +++ b/subprojects/gst-plugins-base/tools/gst-play.c @@ -1698,7 +1698,12 @@ real_main (int argc, char **argv) ctx = g_option_context_new ("FILE1|URI1 [FILE2|URI2] [FILE3|URI3] ..."); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { gst_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); g_option_context_free (ctx); g_clear_error (&err); @@ -1845,9 +1850,21 @@ real_main (int argc, char **argv) int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gstreamer/tools/gst-inspect.c b/subprojects/gstreamer/tools/gst-inspect.c index 40001a1..b8550ea 100644 --- a/subprojects/gstreamer/tools/gst-inspect.c +++ b/subprojects/gstreamer/tools/gst-inspect.c @@ -2171,7 +2171,12 @@ real_main (int argc, char *argv[]) ctx = g_option_context_new ("[ELEMENT-NAME | PLUGIN-NAME]"); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#if defined(G_OS_WIN32) && !defined(GST_CHECK_MAIN) + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_printerr ("Error initializing: %s\n", err->message); g_clear_error (&err); g_option_context_free (ctx); @@ -2338,9 +2343,22 @@ done: int main (int argc, char *argv[]) { + int ret; + + /* gstinspect.c calls this function */ +#if defined(G_OS_WIN32) && !defined(GST_CHECK_MAIN) + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#if defined(G_OS_WIN32) && !defined(GST_CHECK_MAIN) + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gstreamer/tools/gst-launch.c b/subprojects/gstreamer/tools/gst-launch.c index 7d93993..9ad8a4f 100644 --- a/subprojects/gstreamer/tools/gst-launch.c +++ b/subprojects/gstreamer/tools/gst-launch.c @@ -1159,7 +1159,12 @@ real_main (int argc, char *argv[]) ctx = g_option_context_new ("PIPELINE-DESCRIPTION"); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { if (err) gst_printerr ("Error initializing: %s\n", GST_STR_NULL (err->message)); else @@ -1368,9 +1373,21 @@ real_main (int argc, char *argv[]) int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } diff --git a/subprojects/gstreamer/tools/gst-stats.c b/subprojects/gstreamer/tools/gst-stats.c index 597c1ba..57a5a26 100644 --- a/subprojects/gstreamer/tools/gst-stats.c +++ b/subprojects/gstreamer/tools/gst-stats.c @@ -1283,10 +1283,19 @@ main (gint argc, gchar * argv[]) g_set_prgname ("gst-stats-" GST_API_VERSION); +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + ctx = g_option_context_new ("FILE"); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); exit (1); } @@ -1312,5 +1321,10 @@ main (gint argc, gchar * argv[]) done (); g_strfreev (filenames); + +#ifdef G_OS_WIN23 + g_strfreev (argv); +#endif + return 0; } diff --git a/subprojects/gstreamer/tools/gst-typefind.c b/subprojects/gstreamer/tools/gst-typefind.c index b6b1b8a..7f0b4db 100644 --- a/subprojects/gstreamer/tools/gst-typefind.c +++ b/subprojects/gstreamer/tools/gst-typefind.c @@ -164,7 +164,12 @@ real_main (int argc, char *argv[]) ctx = g_option_context_new ("FILES"); g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); g_option_context_add_group (ctx, gst_init_get_option_group ()); - if (!g_option_context_parse (ctx, &argc, &argv, &err)) { +#ifdef G_OS_WIN32 + if (!g_option_context_parse_strv (ctx, &argv, &err)) +#else + if (!g_option_context_parse (ctx, &argc, &argv, &err)) +#endif + { g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); g_clear_error (&err); g_option_context_free (ctx); @@ -193,9 +198,21 @@ real_main (int argc, char *argv[]) int main (int argc, char *argv[]) { + int ret; + +#ifdef G_OS_WIN32 + argv = g_win32_get_command_line (); +#endif + #if defined(__APPLE__) && TARGET_OS_MAC && !TARGET_OS_IPHONE - return gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); + ret = gst_macos_main ((GstMainFunc) real_main, argc, argv, NULL); #else - return real_main (argc, argv); + ret = real_main (argc, argv); +#endif + +#ifdef G_OS_WIN32 + g_strfreev (argv); #endif + + return ret; } -- 2.7.4