2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / bus / main.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* main.c  main() for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "bus.h"
24 #include "loop.h"
25 #include <dbus/dbus-internals.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 static void
31 usage (void)
32 {
33   fprintf (stderr, "dbus-daemon-1 [--session] [--system] [--config-file=FILE] [--version]\n");
34   exit (1);
35 }
36
37 static void
38 version (void)
39 {
40   printf ("D-BUS Message Bus Daemon %s\n"
41           "Copyright (C) 2002, 2003 Red Hat, Inc., CodeFactory AB, and others\n"
42           "This is free software; see the source for copying conditions.\n"
43           "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
44           VERSION);
45   exit (0);
46 }
47
48 static void
49 check_two_config_files (const DBusString *config_file,
50                         const char       *extra_arg)
51 {
52   if (_dbus_string_get_length (config_file) > 0)
53     {
54       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
55                extra_arg, _dbus_string_get_const_data (config_file));
56       exit (1);
57     }
58 }
59
60 int
61 main (int argc, char **argv)
62 {
63   BusContext *context;
64   DBusError error;
65   DBusString config_file;
66   const char *prev_arg;
67   int i;
68
69   if (!_dbus_string_init (&config_file))
70     return 1;
71   
72   prev_arg = NULL;
73   i = 1;
74   while (i < argc)
75     {
76       const char *arg = argv[i];
77       
78       if (strcmp (arg, "--help") == 0 ||
79           strcmp (arg, "-h") == 0 ||
80           strcmp (arg, "-?") == 0)
81         usage ();
82       else if (strcmp (arg, "--version") == 0)
83         version ();
84       else if (strcmp (arg, "--system") == 0)
85         {
86           check_two_config_files (&config_file, "system");
87
88           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
89             exit (1);
90         }
91       else if (strcmp (arg, "--session") == 0)
92         {
93           check_two_config_files (&config_file, "session");
94
95           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
96             exit (1);
97         }
98       else if (strstr (arg, "--config-file=") == arg)
99         {
100           const char *file;
101
102           check_two_config_files (&config_file, "config-file");
103           
104           file = strchr (arg, '=');
105           ++file;
106
107           if (!_dbus_string_append (&config_file, file))
108             exit (1);
109         }
110       else if (prev_arg &&
111                strcmp (prev_arg, "--config-file") == 0)
112         {
113           check_two_config_files (&config_file, "config-file");
114           
115           if (!_dbus_string_append (&config_file, arg))
116             exit (1);
117         }
118       else if (strcmp (arg, "--config-file") == 0)
119         ; /* wait for next arg */
120       else
121         usage ();
122       
123       prev_arg = arg;
124       
125       ++i;
126     }
127
128   if (_dbus_string_get_length (&config_file) == 0)
129     {
130       fprintf (stderr, "No configuration file specified.\n");
131       usage ();
132     }
133   
134   dbus_error_init (&error);
135   context = bus_context_new (&config_file, &error);
136   _dbus_string_free (&config_file);
137   if (context == NULL)
138     {
139       _dbus_warn ("Failed to start message bus: %s\n",
140                   error.message);
141       dbus_error_free (&error);
142       return 1;
143     }
144   
145   _dbus_verbose ("We are on D-Bus...\n");
146   bus_loop_run ();
147   
148   bus_context_shutdown (context);
149   bus_context_unref (context);
150   
151   return 0;
152 }