tools: Make sure UTF-8 encoded command line arguments on Windows
authorSeungha Yang <seungha@centricular.com>
Fri, 27 Jan 2023 17:32:13 +0000 (02:32 +0900)
committerTim-Philipp Müller <tim@centricular.com>
Sun, 26 Feb 2023 12:30:52 +0000 (12:30 +0000)
On Windows, arguments passed in main() are system codepage
encoded and might not be valid UTF-8 string.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4072>

subprojects/gst-plugins-bad/tools/gst-transcoder.c
subprojects/gst-plugins-base/tools/gst-device-monitor.c
subprojects/gst-plugins-base/tools/gst-discoverer.c
subprojects/gst-plugins-base/tools/gst-play.c
subprojects/gstreamer/tools/gst-inspect.c
subprojects/gstreamer/tools/gst-launch.c
subprojects/gstreamer/tools/gst-stats.c
subprojects/gstreamer/tools/gst-typefind.c

index a1ba29c..dccf3eb 100644 (file)
@@ -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;
 }
index d826ee2..aa65f59 100644 (file)
@@ -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;
 }
index fefee4f..a50f5aa 100644 (file)
@@ -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;
 }
index 7508414..ee8847d 100644 (file)
@@ -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;
 }
index 40001a1..b8550ea 100644 (file)
@@ -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;
 }
index 7d93993..9ad8a4f 100644 (file)
@@ -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;
 }
index 597c1ba..57a5a26 100644 (file)
@@ -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;
 }
index b6b1b8a..7f0b4db 100644 (file)
@@ -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;
 }