2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / main.c
index 905e4ba..ae62548 100644 (file)
 #include "bus.h"
 #include "loop.h"
 #include <dbus/dbus-internals.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static void
+usage (void)
+{
+  fprintf (stderr, "dbus-daemon-1 [--session] [--system] [--config-file=FILE] [--version]\n");
+  exit (1);
+}
+
+static void
+version (void)
+{
+  printf ("D-BUS Message Bus Daemon %s\n"
+          "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
+          "This is free software; see the source for copying conditions.\n"
+          "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
+          VERSION);
+  exit (0);
+}
+
+static void
+check_two_config_files (const DBusString *config_file,
+                        const char       *extra_arg)
+{
+  if (_dbus_string_get_length (config_file) > 0)
+    {
+      const char *s;
+      _dbus_string_get_const_data (config_file, &s);
+      fprintf (stderr, "--%s specified but configuration file %s already requested\n",
+               extra_arg, s);
+      exit (1);
+    }
+}
 
 int
 main (int argc, char **argv)
@@ -30,22 +65,77 @@ main (int argc, char **argv)
   BusContext *context;
   DBusError error;
   DBusString config_file;
+  const char *prev_arg;
+  int i;
 
-  /* FIXME I think the arguments should be like:
-   * --system    use standard system config file
-   * --session   use standard session config file
-   * --config-file=foo.conf  use some other file
-   */
+  if (!_dbus_string_init (&config_file, _DBUS_INT_MAX))
+    return 1;
   
-  if (argc != 2)
+  prev_arg = NULL;
+  i = 1;
+  while (i < argc)
     {
-      _dbus_warn ("The message bus configuration file must be given as the only argument\n");
-      return 1;
+      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, "--system") == 0)
+        {
+          check_two_config_files (&config_file, "system");
+
+          if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
+            exit (1);
+        }
+      else if (strcmp (arg, "--session") == 0)
+        {
+          check_two_config_files (&config_file, "session");
+
+          if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
+            exit (1);
+        }
+      else if (strstr (arg, "--config-file=") == arg)
+        {
+          const char *file;
+
+          check_two_config_files (&config_file, "config-file");
+          
+          file = strchr (arg, '=');
+          ++file;
+
+          if (!_dbus_string_append (&config_file, file))
+            exit (1);
+        }
+      else if (prev_arg &&
+               strcmp (prev_arg, "--config-file") == 0)
+        {
+          check_two_config_files (&config_file, "config-file");
+          
+          if (!_dbus_string_append (&config_file, arg))
+            exit (1);
+        }
+      else if (strcmp (arg, "--config-file") == 0)
+        ; /* wait for next arg */
+      else
+        usage ();
+      
+      prev_arg = arg;
+      
+      ++i;
+    }
+
+  if (_dbus_string_get_length (&config_file) == 0)
+    {
+      fprintf (stderr, "No configuration file specified.\n");
+      usage ();
     }
   
   dbus_error_init (&error);
-  _dbus_string_init_const (&config_file, argv[1]);
   context = bus_context_new (&config_file, &error);
+  _dbus_string_free (&config_file);
   if (context == NULL)
     {
       _dbus_warn ("Failed to start message bus: %s\n",
@@ -53,12 +143,12 @@ main (int argc, char **argv)
       dbus_error_free (&error);
       return 1;
     }
-
+  
   _dbus_verbose ("We are on D-Bus...\n");
   bus_loop_run ();
   
   bus_context_shutdown (context);
   bus_context_unref (context);
+  
   return 0;
 }