gnome-terminal r3131 - trunk/src



Author: chpe
Date: Tue Oct  7 14:30:50 2008
New Revision: 3131
URL: http://svn.gnome.org/viewvc/gnome-terminal?rev=3131&view=rev

Log:
Move option parsing completely to terminal-options.c.

Modified:
   trunk/src/terminal-options.c
   trunk/src/terminal-options.h
   trunk/src/terminal.c

Modified: trunk/src/terminal-options.c
==============================================================================
--- trunk/src/terminal-options.c	(original)
+++ trunk/src/terminal-options.c	Tue Oct  7 14:30:50 2008
@@ -23,13 +23,18 @@
 
 #include <errno.h>
 #include <string.h>
-#include <stdlib.h>
 
 #include <glib.h>
 
 #include "terminal-options.h"
 #include "terminal-intl.h"
 
+static GOptionContext *get_goption_context (TerminalOptions *options,
+                                            gboolean is_for_remote);
+static void check_for_display_name (TerminalOptions *options,
+                                    int *argc,
+                                    char **argv);
+
 static InitialTab*
 initial_tab_new (const char *profile,
                  gboolean    is_id)
@@ -645,15 +650,23 @@
 }
 
 TerminalOptions *
-option_options_new (const char *working_directory,
-                            const char *display_name,
-                            const char *startup_id,
-                            const char **env,
-                            int *argc,
-                            char **argv)
+terminal_options_parse (const char *working_directory,
+                        const char *display_name,
+                        const char *startup_id,
+                        const char **env,
+                        gboolean is_for_remote,
+                        int *argcp,
+                        char ***argvp,
+                        GError **error,
+                        ...)
 {
   TerminalOptions *options;
+  GOptionContext *context;
+  GOptionGroup *extra_group;
+  va_list va_args;
+  gboolean retval;
   int i;
+  char **argv = *argvp;
 
   options = g_slice_new0 (TerminalOptions);
 
@@ -665,7 +678,7 @@
   options->use_factory = TRUE;
 
   options->env = g_strdupv ((char **) env);
-  options->startup_id = g_strdup (startup_id);
+  options->startup_id = g_strdup (startup_id && startup_id[0] ? startup_id : NULL);
   options->display_name = g_strdup (display_name);
   options->initial_windows = NULL;
   options->default_role = NULL;
@@ -679,7 +692,7 @@
   /* The old -x/--execute option is broken, so we need to pre-scan for it. */
   /* We now also support passing the command after the -- switch. */
   options->exec_argv = NULL;
-  for (i = 1 ; i < *argc; ++i)
+  for (i = 1 ; i < *argcp; ++i)
     {
       gboolean is_execute;
       gboolean is_dashdash;
@@ -696,24 +709,49 @@
       /* Skip the switch */
       last = i;
       ++i;
-      if (i == *argc)
+      if (i == *argcp)
         break; /* we'll complain about this later for -x/--execute; it's fine for -- */
 
       /* Collect the args, and remove them from argv */
-      options->exec_argv = g_new0 (char*, *argc - i + 1);
-      for (j = 0; i < *argc; ++i, ++j)
+      options->exec_argv = g_new0 (char*, *argcp - i + 1);
+      for (j = 0; i < *argcp; ++i, ++j)
         options->exec_argv[j] = g_strdup (argv[i]);
       options->exec_argv[j] = NULL;
 
-      *argc = last;
+      *argcp = last;
       break;
     }
 
-  return options;
+  context = get_goption_context (options, is_for_remote);
+
+  va_start (va_args, error);
+  extra_group = va_arg (va_args, GOptionGroup*);
+  while (extra_group != NULL)
+    {
+      g_option_context_add_group (context, extra_group);
+      extra_group = va_arg (va_args, GOptionGroup*);
+    }
+  va_end (va_args);
+
+  if (is_for_remote)
+    {
+      /* FIXMEchpe: I don't think we need this for the forwarded args! */
+      /* Find and parse --display */
+      check_for_display_name (options, argcp, *argvp);
+    }
+
+  retval = g_option_context_parse (context, argcp, argvp, error);
+  g_option_context_free (context);
+
+  if (retval)
+    return options;
+
+  terminal_options_free (options);
+  return NULL;
 }
 
 void
-option_options_free (TerminalOptions *options)
+terminal_options_free (TerminalOptions *options)
 {
   g_list_foreach (options->initial_windows, (GFunc) initial_window_free, NULL);
   g_list_free (options->initial_windows);
@@ -733,9 +771,10 @@
   g_slice_free (TerminalOptions, options);
 }
 
-void
-option_options_check_for_display_name (TerminalOptions *options,
-                                               int *argc, char **argv)
+static void
+check_for_display_name (TerminalOptions *options,
+                        int *argc,
+                        char **argv)
 {
   int i;
 
@@ -824,8 +863,9 @@
     }
 }
 
-GOptionContext *
-terminal_options_get_goption_context (TerminalOptions *options)
+static GOptionContext *
+get_goption_context (TerminalOptions *options,
+                     gboolean is_for_remote)
 {
   const GOptionEntry global_unique_goptions[] = {
     {
@@ -1264,5 +1304,8 @@
   g_option_group_add_entries (group, terminal_goptions);
   g_option_context_add_group (context, group);
   
+  if (is_for_remote)
+    g_option_context_set_ignore_unknown_options (context, TRUE);
+
   return context;
 }

Modified: trunk/src/terminal-options.h
==============================================================================
--- trunk/src/terminal-options.h	(original)
+++ trunk/src/terminal-options.h	Tue Oct  7 14:30:50 2008
@@ -61,8 +61,6 @@
   double    zoom;
 } TerminalOptions;
 
-GOptionContext *get_goption_context (TerminalOptions *options);
-
 typedef struct
 {
   char *profile;
@@ -90,18 +88,16 @@
 
 } InitialWindow;
 
-TerminalOptions *option_options_new (const char *working_directory,
-                                                  const char *display_name,
-                                                  const char *startup_id,
-                                                  const char **env,
-                                                  int *argc,
-                                                  char **argv);
-
-void option_options_free (TerminalOptions *options);
-
-GOptionContext * terminal_options_get_goption_context (TerminalOptions *options);
+TerminalOptions *terminal_options_parse (const char *working_directory,
+                                         const char *display_name,
+                                         const char *startup_id,
+                                         const char **env,
+                                         gboolean is_for_remote,
+                                         int *argcp,
+                                         char ***argvp,
+                                         GError **error,
+                                         ...);
 
-void option_options_check_for_display_name (TerminalOptions *options,
-                                                    int *argc, char **argv);
+void terminal_options_free (TerminalOptions *options);
 
 G_END_DECLS

Modified: trunk/src/terminal.c
==============================================================================
--- trunk/src/terminal.c	(original)
+++ trunk/src/terminal.c	Tue Oct  7 14:30:50 2008
@@ -363,7 +363,6 @@
 int
 main (int argc, char **argv)
 {
-  GOptionContext *context;
   int i;
   char **argv_copy;
   const char *startup_id;
@@ -385,38 +384,43 @@
     argv_copy [i] = argv [i];
   argv_copy [i] = NULL;
 
-  options = option_options_new (NULL, NULL, NULL, NULL, &argc, argv);
   startup_id = g_getenv ("DESKTOP_STARTUP_ID");
-  if (startup_id != NULL && startup_id[0] != '\0')
-    {
-      options->startup_id = g_strdup (startup_id);
-      g_unsetenv ("DESKTOP_STARTUP_ID");
-    }
-
-  gtk_window_set_auto_startup_notification (FALSE); /* we'll do it ourselves due
-                                                     * to complicated factory setup
-                                                     */
 
-  context = terminal_options_get_goption_context (options);
-  g_option_context_add_group (context, gtk_get_option_group (TRUE));
-  g_option_context_add_group (context, egg_sm_client_get_option_group ());
-
-  if (!g_option_context_parse (context, &argc, &argv, &error))
+  options = terminal_options_parse (NULL,
+                                    NULL,
+                                    startup_id,
+                                    NULL,
+                                    FALSE,
+                                    &argc, &argv,
+                                    &error,
+                                    gtk_get_option_group (TRUE),
+                                    egg_sm_client_get_option_group (),
+                                    NULL);
+  if (!options)
     {
       g_printerr (_("Failed to parse arguments: %s\n"), error->message);
       g_error_free (error);
-      g_option_context_free (context);
       exit (1);
     }
 
-  g_option_context_free (context);
   g_set_application_name (_("Terminal"));
   
+  /* Unset the startup ID, so it doesn't end up in the factory's env
+   * and thus in the terminals' envs.
+   */
+  if (startup_id)
+    g_unsetenv ("DESKTOP_STARTUP_ID");
+
+  gtk_window_set_auto_startup_notification (FALSE); /* we'll do it ourselves due
+                                                     * to complicated factory setup
+                                                     */
+
  /* Do this here so that gdk_display is initialized */
   if (options->startup_id == NULL)
     {
       /* Create a fake one containing a timestamp that we can use */
       Time timestamp;
+
       timestamp = slowly_and_stupidly_obtain_timestamp (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()));
 
       options->startup_id = g_strdup_printf ("_TIME%lu", timestamp);
@@ -514,7 +518,7 @@
 
       g_free (argv_copy);
       g_strfreev (env);
-      option_options_free (options);
+      terminal_options_free (options);
 
       exit (ret);
     }
@@ -538,7 +542,7 @@
   g_signal_connect (terminal_app_get (), "quit", G_CALLBACK (gtk_main_quit), NULL);
 
   new_terminal_with_options (terminal_app_get (), options);
-  option_options_free (options);
+  terminal_options_free (options);
 
   gtk_main ();
 
@@ -570,38 +574,30 @@
                                GError **error)
 {
   TerminalOptions *options;
-  GOptionContext *context;
   char **argv;
   int argc;
 
+  /* Copy the arguments since terminal_options_parse potentially modifies the array */
   argc = g_strv_length ((char **) arguments);
   argv = (char **) g_memdup (arguments, (argc + 1) * sizeof (char *));
 
-  options = option_options_new (working_directory,
-                                                display_name,
-                                                startup_id,
-                                                env,
-                                                &argc, argv);
-
-  /* FIXMEchpe: I don't think we need this for the forwarded args! */
-  /* Find and parse --display */
-  option_options_check_for_display_name (options, &argc, argv);
-
-  context = terminal_options_get_goption_context (options);
-  g_option_context_set_ignore_unknown_options (context, TRUE);
-  if (!g_option_context_parse (context, &argc, &argv, error))
-    {
-      g_option_context_free (context);
-      option_options_free (options);
+  options = terminal_options_parse (working_directory,
+                                    display_name,
+                                    startup_id,
+                                    env,
+                                    TRUE,
+                                    &argc, &argv,
+                                    error,
+                                    NULL);
+  g_free (argv);
 
-      return FALSE;
-    }
-  g_option_context_free (context);
+  if (!options)
+    return FALSE;
 
   g_idle_add_full (G_PRIORITY_HIGH_IDLE,
                    (GSourceFunc) handle_new_terminal_event,
                    options,
-                   (GDestroyNotify) option_options_free);
+                   (GDestroyNotify) terminal_options_free);
 
   return TRUE;
 }



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