[easytag/wip/mingw-fixes] Ugly WIP process spawn improvements



commit 684185db21600312e579bf714bb3f51dd035b6d9
Author: David King <amigadave amigadave com>
Date:   Tue Feb 18 22:34:33 2014 +0000

    Ugly WIP process spawn improvements

 src/browser.c |   61 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 43 insertions(+), 18 deletions(-)
---
diff --git a/src/browser.c b/src/browser.c
index fc4527c..aba63ee 100644
--- a/src/browser.c
+++ b/src/browser.c
@@ -4286,13 +4286,14 @@ Run_Program_With_Selected_Files (GtkWidget *combobox)
 static gboolean
 Run_Program (const gchar *program_name, GList *args_list)
 {
-    gchar **argv_user;
-    gint    argv_user_number;
+    const gchar *program_args = NULL;
+    gchar **program_args_argv = { NULL };
+    guint n_program_args = 0;
+    gsize i;
     gchar  *msg;
     GPid pid;
     GError *error = NULL;
     gchar **argv;
-    gint    argv_index = 0;
     GList *l;
     gchar *program_path;
 
@@ -4333,27 +4334,51 @@ Run_Program (const gchar *program_name, GList *args_list)
         return FALSE;
     }
 
-    g_free(program_path); // Freed as never used
+    /* If user arguments are included, try to skip them. FIXME: This works
+     * poorly when there are spaces in the absolute path to the binary. */
+    {
+        size_t name_length;
+        size_t path_length;
+
+        name_length = strlen (program_name);
+        path_length = strlen (program_path);
 
-    argv_user = g_strsplit(program_name," ",0); // the string may contains arguments, space is the delimiter
-    // Number of arguments into 'argv_user'
-    for (argv_user_number=0;argv_user[argv_user_number];argv_user_number++);
+        if (name_length != path_length)
+        {
+            /* Skip the binary name and a delimiter. */
+            program_args = program_name + path_length + 1;
+        }
+    }
 
-    argv = g_new0(gchar *,argv_user_number + g_list_length(args_list) + 1); // +1 for NULL
+    g_free (program_path);
 
-    // Load 'user' arguments (program name and more...)
-    while (argv_user[argv_index])
+    if (program_args)
     {
-        argv[argv_index] = argv_user[argv_index];
-        argv_index++;
+        /* FIXME: Splitting arguments based on a delimiting space is bogus if
+         * the arguments have been quoted. */
+        program_args_argv = g_strsplit (program_args, " ", 0);
+        n_program_args = g_strv_length (program_args_argv);
     }
-    // Load arguments from 'args_list'
-    for (l = args_list; l != NULL; l = g_list_next (l))
+
+    /* +1 for NULL. */
+    argv = g_new0 (gchar *, n_program_args + g_list_length (args_list) + 1);
+
+    for (i = 0; program_args_argv[i] != NULL; i++)
     {
-        argv[argv_index] = (gchar *)l->data;
-        argv_index++;
+        argv[i] = program_args_argv[i];
     }
-    argv[argv_index] = NULL;
+
+    /* Load arguments from 'args_list'. */
+    for (l = args_list; l != NULL; l = g_list_next (l), i++)
+    {
+#ifdef G_OS_WIN32
+        argv[i] = g_strconcat ("\"", (gchar *)l->data, "\"", NULL);
+#else /* !G_OS_WIN32 */
+        argv[i] = (gchar *)l->data;
+#endif
+    }
+
+    argv[i] = NULL;
 
     /* Execution ... */
     if (g_spawn_async (NULL, argv, NULL,
@@ -4373,7 +4398,7 @@ Run_Program (const gchar *program_name, GList *args_list)
         g_clear_error (&error);
     }
 
-    g_strfreev (argv_user);
+    g_strfreev (program_args_argv);
 
     return TRUE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]