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       const char *s;
55       _dbus_string_get_const_data (config_file, &s);
56       fprintf (stderr, "--%s specified but configuration file %s already requested\n",
57                extra_arg, s);
58       exit (1);
59     }
60 }
61
62 int
63 main (int argc, char **argv)
64 {
65   BusContext *context;
66   DBusError error;
67   DBusString config_file;
68   const char *prev_arg;
69   int i;
70
71   if (!_dbus_string_init (&config_file, _DBUS_INT_MAX))
72     return 1;
73   
74   prev_arg = NULL;
75   i = 1;
76   while (i < argc)
77     {
78       const char *arg = argv[i];
79       
80       if (strcmp (arg, "--help") == 0 ||
81           strcmp (arg, "-h") == 0 ||
82           strcmp (arg, "-?") == 0)
83         usage ();
84       else if (strcmp (arg, "--version") == 0)
85         version ();
86       else if (strcmp (arg, "--system") == 0)
87         {
88           check_two_config_files (&config_file, "system");
89
90           if (!_dbus_string_append (&config_file, DBUS_SYSTEM_CONFIG_FILE))
91             exit (1);
92         }
93       else if (strcmp (arg, "--session") == 0)
94         {
95           check_two_config_files (&config_file, "session");
96
97           if (!_dbus_string_append (&config_file, DBUS_SESSION_CONFIG_FILE))
98             exit (1);
99         }
100       else if (strstr (arg, "--config-file=") == arg)
101         {
102           const char *file;
103
104           check_two_config_files (&config_file, "config-file");
105           
106           file = strchr (arg, '=');
107           ++file;
108
109           if (!_dbus_string_append (&config_file, file))
110             exit (1);
111         }
112       else if (prev_arg &&
113                strcmp (prev_arg, "--config-file") == 0)
114         {
115           check_two_config_files (&config_file, "config-file");
116           
117           if (!_dbus_string_append (&config_file, arg))
118             exit (1);
119         }
120       else if (strcmp (arg, "--config-file") == 0)
121         ; /* wait for next arg */
122       else
123         usage ();
124       
125       prev_arg = arg;
126       
127       ++i;
128     }
129
130   if (_dbus_string_get_length (&config_file) == 0)
131     {
132       fprintf (stderr, "No configuration file specified.\n");
133       usage ();
134     }
135   
136   dbus_error_init (&error);
137   context = bus_context_new (&config_file, &error);
138   _dbus_string_free (&config_file);
139   if (context == NULL)
140     {
141       _dbus_warn ("Failed to start message bus: %s\n",
142                   error.message);
143       dbus_error_free (&error);
144       return 1;
145     }
146   
147   _dbus_verbose ("We are on D-Bus...\n");
148   bus_loop_run ();
149   
150   bus_context_shutdown (context);
151   bus_context_unref (context);
152   
153   return 0;
154 }