Index: main.c
===================================================================
RCS file: /cvs/gnome/metacity/src/main.c,v
retrieving revision 1.55
diff -u -p -r1.55 main.c
--- main.c	16 Sep 2004 23:18:22 -0000	1.55
+++ main.c	24 May 2005 14:36:29 -0000
@@ -46,6 +46,9 @@
 #include <locale.h>
 #include <time.h>
 
+/* For getopt_long(). */
+#include <getopt.h>
+
 static MetaExitCode meta_exit_code = META_EXIT_SUCCESS;
 static GMainLoop *meta_main_loop = NULL;
 static gboolean meta_restart_after_quit = FALSE;
@@ -165,17 +168,162 @@ accessibility_invoke (gboolean init)
   return TRUE;
 }
 
+static void
+meta_print_compilation_info()
+{
+#ifdef HAVE_SHAPE
+	meta_verbose ("Compiled with shape extension\n");
+#else
+	meta_verbose ("Compiled without shape extension\n");
+#endif
+#ifdef HAVE_XINERAMA
+	meta_topic (META_DEBUG_XINERAMA, "Compiled with Xinerama extension\n");
+#else
+	meta_topic (META_DEBUG_XINERAMA, "Compiled without Xinerama extension\n");
+#endif
+#ifdef HAVE_XFREE_XINERAMA
+	meta_topic (META_DEBUG_XINERAMA, " (using XFree86 Xinerama)\n");
+#else
+	meta_topic (META_DEBUG_XINERAMA, " (not using XFree86 Xinerama)\n");
+#endif
+#ifdef HAVE_SOLARIS_XINERAMA
+	meta_topic (META_DEBUG_XINERAMA, " (using Solaris Xinerama)\n");
+#else
+	meta_topic (META_DEBUG_XINERAMA, " (not using Solaris Xinerama)\n");
+#endif
+#ifdef HAVE_XSYNC
+	meta_verbose ("Compiled with sync extension\n");
+#else
+	meta_verbose ("Compiled without sync extension\n");
+#endif
+#ifdef HAVE_RANDR
+	meta_verbose ("Compiled with randr extension\n");
+#else
+	meta_verbose ("Compiled without randr extension\n");
+#endif
+#ifdef HAVE_STARTUP_NOTIFICATION
+	meta_verbose ("Compiled with startup notification\n");
+#else
+	meta_verbose ("Compiled without startup notification\n");
+#endif
+#ifdef HAVE_COMPOSITE_EXTENSIONS
+	meta_verbose ("Compiled with composite extensions\n");
+#else
+	meta_verbose ("Compiled without composite extensions\n");
+#endif
+}
+
+static void
+meta_print_self_identity()
+{
+    char buf[256];
+    GDate d;
+	const char *charset;
+
+	/* Version and current date. */
+    g_date_clear(&d, 1);
+    g_date_set_time(&d, time (NULL));
+    g_date_strftime(buf, sizeof (buf), "%x", &d);
+    meta_verbose("Metacity version %s running on %s\n",
+		VERSION, buf);
+  
+    /* Locale and encoding. */
+    g_get_charset (&charset);
+    meta_verbose("Running in locale \"%s\" with encoding \"%s\"\n",
+		setlocale (LC_ALL, NULL), charset);
+
+	/* Compilation settings. */
+	meta_print_compilation_info();
+}
+
+typedef struct
+{
+	char *save_file;
+	char *display_name;
+	char *client_id;
+	gboolean replace_wm;
+	gboolean disable_sm;
+} MetaArguments;
+
+static MetaArguments
+meta_parse_options(int argc, char **argv)
+{
+	MetaArguments meta_args;
+	int option_index = 0;
+	int opt;
+
+	enum options
+	{
+		OPT_SM_DISABLE,
+		OPT_REPLACE,
+		OPT_SM_CLIENT_ID,
+		OPT_SM_SAVE_FILE,
+		OPT_DISPLAY,
+		OPT_VERSION,
+		OPT_HELP = 'h'
+	};
+	static struct option long_options[] =
+	{
+		{"sm-disable", no_argument, 0, OPT_SM_DISABLE},
+		{"replace", no_argument, 0, OPT_REPLACE},
+		{"sm-client-id", required_argument, 0, OPT_SM_CLIENT_ID},
+		{"sm-save-file", required_argument, 0, OPT_SM_SAVE_FILE},
+		{"display", required_argument, 0, OPT_DISPLAY},
+		{"version", no_argument, 0, OPT_VERSION},
+		{"help", no_argument, 0, OPT_HELP}
+	};
+
+	meta_args.display_name = NULL;
+	meta_args.client_id = NULL;
+	meta_args.save_file = NULL;
+	meta_args.disable_sm = FALSE;
+	meta_args.replace_wm = FALSE;
+	
+	while ((opt = getopt_long(argc, argv, "h", long_options,
+				&option_index)) != EOF)
+	{
+		switch(opt)
+		{
+		case OPT_SM_DISABLE:
+			meta_args.disable_sm = TRUE;
+			break;
+		case OPT_REPLACE:
+			meta_args.replace_wm = TRUE;
+			break;
+		case OPT_SM_CLIENT_ID:
+			meta_args.client_id = g_strdup(optarg);
+			break;
+		case OPT_SM_SAVE_FILE:
+			meta_args.save_file = g_strdup(optarg);
+			break;
+		case OPT_DISPLAY:
+			meta_args.display_name =
+				g_strconcat("DISPLAY=", optarg, NULL);
+			break;
+		case OPT_VERSION:
+			version();
+			break;
+		case OPT_HELP:
+			usage();
+			break;
+		case '?':
+		case ':':
+			/* Abort if unknown option or if a parameter is missing
+			   from an option. */
+			exit(1);
+			break;
+		}
+	}
+	return meta_args;
+}
+	
+
 int
 main (int argc, char **argv)
 {
   struct sigaction act;
   sigset_t empty_mask;
-  char *display_name;
-  int i;
-  const char *client_id;
-  gboolean disable_sm;
-  const char *prev_arg;
-  const char *save_file;
+  MetaArguments meta_args;
 
   g_set_prgname (argv[0]);
   
@@ -204,186 +352,40 @@ main (int argc, char **argv)
   if (g_get_home_dir ())
     chdir (g_get_home_dir ());
 
-  {
-    char buf[256];
-    GDate d;
-    g_date_clear (&d, 1);
-    g_date_set_time (&d, time (NULL));
-    g_date_strftime (buf, sizeof (buf), "%x", &d);
-    meta_verbose ("Metacity version %s running on %s\n", VERSION, buf);
-  }
-  
-  {
-    const char *charset;
-    g_get_charset (&charset);
-    meta_verbose ("Running in locale \"%s\" with encoding \"%s\"\n",
-                  setlocale (LC_ALL, NULL), charset);
-  }
+  meta_print_self_identity();
   
   bindtextdomain (GETTEXT_PACKAGE, METACITY_LOCALEDIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
   textdomain (GETTEXT_PACKAGE);
-  
-  /* Parse options lamely */
-
-  display_name = NULL;
-  client_id = NULL;
-  save_file = NULL;
-  disable_sm = FALSE;
-  prev_arg = NULL;
-  i = 1;
-  while (i < argc)
-    {
-      const char *arg = argv[i];
-      
-      if (strcmp (arg, "--help") == 0 ||
-          strcmp (arg, "-h") == 0 ||
-          strcmp (arg, "-?") == 0)
-        usage ();
-      else if (strcmp (arg, "--version") == 0)
-        version ();
-      else if (strcmp (arg, "--sm-disable") == 0)
-        disable_sm = TRUE;
-      else if (strcmp (arg, "--replace") == 0)
-        meta_set_replace_current_wm (TRUE);
-      else if (strstr (arg, "--display=") == arg)
-        {
-          const char *disp;
-
-          if (display_name != NULL)
-            meta_fatal ("Can't specify display twice\n");
-          
-          disp = strchr (arg, '=');
-          ++disp;
-          
-          display_name =
-            g_strconcat ("DISPLAY=", disp, NULL);
-        }
-      else if (prev_arg &&
-               strcmp (prev_arg, "--display") == 0)
-        {
-          if (display_name != NULL)
-            meta_fatal ("Can't specify display twice\n");
-
-          display_name = g_strconcat ("DISPLAY=", arg, NULL);
-        }
-      else if (strcmp (arg, "--display") == 0)
-        ; /* wait for next arg */
-      else if (strstr (arg, "--sm-client-id=") == arg)
-        {
-          const char *id;
-
-          if (client_id)
-            meta_fatal ("Can't specify client ID twice\n");
-          
-          id = strchr (arg, '=');
-          ++id;
 
-          client_id = g_strdup (id);
-        }
-      else if (prev_arg &&
-               strcmp (prev_arg, "--sm-client-id") == 0)
-        {
-          if (client_id)
-            meta_fatal ("Can't specify client ID twice\n");
-
-          client_id = g_strdup (arg);
-        }
-      else if (strcmp (arg, "--sm-client-id") == 0)
-        ; /* wait for next arg */
-      else if (strstr (arg, "--sm-save-file=") == arg)
-        {
-          const char *file;
-
-          if (save_file)
-            meta_fatal ("Can't specify SM save file twice\n");
-          
-          file = strchr (arg, '=');
-          ++file;
+  /* Parse command line arguments.*/
+  meta_args = meta_parse_options(argc, argv);
 
-          save_file = g_strdup (file);
-        }
-      else if (prev_arg &&
-               strcmp (prev_arg, "--sm-save-file") == 0)
-        {
-          if (save_file)
-            meta_fatal ("Can't specify SM save file twice\n");
-
-          save_file = g_strdup (arg);
-        }
-      else if (strcmp (arg, "--sm-save-file") == 0)
-        ; /* wait for next arg */
-      else
-        usage ();
-      
-      prev_arg = arg;
-      
-      ++i;
-    }
+  if (meta_args.replace_wm)
+	  meta_set_replace_current_wm(TRUE);
 
-  if (save_file && client_id)
+  if (meta_args.save_file && meta_args.client_id)
     meta_fatal ("Can't specify both SM save file and SM client id\n");
   
   meta_main_loop = g_main_loop_new (NULL, FALSE);
   
-  if (display_name == NULL &&
+  if (meta_args.display_name == NULL &&
       g_getenv ("METACITY_DISPLAY"))
     {
       meta_verbose ("Using METACITY_DISPLAY %s\n",
-                    g_getenv ("METACITY_DISPLAY"));
-      display_name =
-        g_strconcat ("DISPLAY=", g_getenv ("METACITY_DISPLAY"), NULL);
+		  g_getenv ("METACITY_DISPLAY"));
+      meta_args.display_name = g_strconcat("DISPLAY=",
+		  g_getenv("METACITY_DISPLAY"), NULL);
     }
 
-  if (display_name)
+  if (meta_args.display_name)
     {
-      putenv (display_name);
+      putenv (meta_args.display_name);
       /* DO NOT FREE display_name, putenv() sucks */
     }  
   
   g_type_init ();
 
-#ifdef HAVE_SHAPE
-  meta_verbose ("Compiled with shape extension\n");
-#else
-  meta_verbose ("Compiled without shape extension\n");
-#endif
-#ifdef HAVE_XINERAMA
-  meta_topic (META_DEBUG_XINERAMA, "Compiled with Xinerama extension\n");
-#else
-  meta_topic (META_DEBUG_XINERAMA, "Compiled without Xinerama extension\n");
-#endif
-#ifdef HAVE_XFREE_XINERAMA
-  meta_topic (META_DEBUG_XINERAMA, " (using XFree86 Xinerama)\n");
-#else
-  meta_topic (META_DEBUG_XINERAMA, " (not using XFree86 Xinerama)\n");
-#endif
-#ifdef HAVE_SOLARIS_XINERAMA
-  meta_topic (META_DEBUG_XINERAMA, " (using Solaris Xinerama)\n");
-#else
-  meta_topic (META_DEBUG_XINERAMA, " (not using Solaris Xinerama)\n");
-#endif
-#ifdef HAVE_XSYNC
-  meta_verbose ("Compiled with sync extension\n");
-#else
-  meta_verbose ("Compiled without sync extension\n");
-#endif
-#ifdef HAVE_RANDR
-  meta_verbose ("Compiled with randr extension\n");
-#else
-  meta_verbose ("Compiled without randr extension\n");
-#endif
-#ifdef HAVE_STARTUP_NOTIFICATION
-  meta_verbose ("Compiled with startup notification\n");
-#else
-  meta_verbose ("Compiled without startup notification\n");
-#endif
-#ifdef HAVE_COMPOSITE_EXTENSIONS
-  meta_verbose ("Compiled with composite extensions\n");
-#else
-  meta_verbose ("Compiled without composite extensions\n");
-#endif
-  
   /* Load prefs */
   meta_prefs_init ();
   meta_prefs_add_listener (prefs_changed_callback, NULL);
@@ -463,8 +465,8 @@ main (int argc, char **argv)
    * or we might try to manage a window before we have the session
    * info
    */
-  if (!disable_sm)
-    meta_session_init (client_id, save_file);
+  if (!meta_args.disable_sm)
+	  meta_session_init (meta_args.client_id, meta_args.save_file);
   
   if (!meta_display_open (NULL))
     meta_exit (META_EXIT_ERROR);
