2003-06-19 Philip Blundell <philb@gnu.org>
[platform/upstream/dbus.git] / tools / dbus-send.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-send.c  Utility program to send messages from the command line
3  *
4  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <dbus/dbus.h>
27
28 #include "dbus-print-message.h"
29
30 static void
31 usage (char *name, int ecode)
32 {
33   fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=SERVICE] [--print-reply] <message type> [contents ...]\n", name);
34   exit (ecode);
35 }
36
37 int
38 main (int argc, char *argv[])
39 {
40   DBusConnection *connection;
41   DBusError error;
42   DBusMessage *message;
43   int print_reply;
44   DBusMessageIter iter;
45   int i;
46   DBusBusType type = DBUS_BUS_SESSION;
47   char *dest = DBUS_SERVICE_BROADCAST;
48   char *name = NULL;
49
50   if (argc < 2)
51     usage (argv[0], 1);
52
53   print_reply = FALSE;
54   
55   for (i = 1; i < argc && name == NULL; i++)
56     {
57       char *arg = argv[i];
58
59       if (strcmp (arg, "--system") == 0)
60         type = DBUS_BUS_SYSTEM;
61       else if (strcmp (arg, "--session") == 0)
62         type = DBUS_BUS_SESSION;
63       else if (strcmp (arg, "--print-reply") == 0)
64         print_reply = TRUE;
65       else if (strstr (arg, "--dest=") == arg)
66         dest = strchr (arg, '=') + 1;
67       else if (!strcmp(arg, "--help"))
68         usage (argv[0], 0);
69       else if (arg[0] == '-')
70         usage (argv[0], 1);
71       else
72         name = arg;
73     }
74
75   if (name == NULL)
76     usage (argv[0], 1);
77
78   dbus_error_init (&error);
79   connection = dbus_bus_get (type, &error);
80   if (connection == NULL)
81     {
82       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
83                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
84                error.message);
85       dbus_error_free (&error);
86       exit (1);
87     }
88
89   message = dbus_message_new (name, dest);
90   if (message == NULL)
91     {
92       fprintf (stderr, "Couldn't allocate D-BUS message\n");
93       exit (1);
94     }
95
96   dbus_message_append_iter_init (message, &iter);
97
98   while (i < argc)
99     {
100       char *arg;
101       char *c;
102       int type;
103       dbus_uint32_t uint32;
104       dbus_int32_t int32;
105       double d;
106       unsigned char byte;
107
108       type = DBUS_TYPE_INVALID;
109       arg = argv[i++];
110       c = strchr (arg, ':');
111
112       if (c == NULL)
113         {
114           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
115           exit (1);
116         }
117
118       *(c++) = 0;
119
120       if (arg[0] == 0 || !strcmp (arg, "string"))
121         type = DBUS_TYPE_STRING;
122       else if (!strcmp (arg, "int32"))
123         type = DBUS_TYPE_INT32;
124       else if (!strcmp (arg, "uint32"))
125         type = DBUS_TYPE_UINT32;
126       else if (!strcmp (arg, "double"))
127         type = DBUS_TYPE_DOUBLE;
128       else if (!strcmp (arg, "byte"))
129         type = DBUS_TYPE_BYTE;
130       else if (!strcmp (arg, "boolean"))
131         type = DBUS_TYPE_BOOLEAN;
132       else
133         {
134           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
135           exit (1);
136         }
137
138       switch (type)
139         {
140         case DBUS_TYPE_BYTE:
141           byte = strtoul (c, NULL, 0);
142           dbus_message_iter_append_byte (&iter, byte);
143           break;
144
145         case DBUS_TYPE_DOUBLE:
146           d = strtod (c, NULL);
147           dbus_message_iter_append_double (&iter, d);
148           break;
149
150         case DBUS_TYPE_INT32:
151           int32 = strtol (c, NULL, 0);
152           dbus_message_iter_append_int32 (&iter, int32);
153           break;
154
155         case DBUS_TYPE_UINT32:
156           uint32 = strtoul (c, NULL, 0);
157           dbus_message_iter_append_uint32 (&iter, uint32);
158           break;
159
160         case DBUS_TYPE_STRING:
161           dbus_message_iter_append_string (&iter, c);
162           break;
163
164         default:
165           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
166           exit (1);
167         }
168     }
169
170   if (print_reply)
171     {
172       DBusMessage *reply;
173
174       dbus_error_init (&error);
175       reply = dbus_connection_send_with_reply_and_block (connection,
176                                                          message, -1,
177                                                          &error);
178       if (dbus_error_is_set (&error))
179         {
180           fprintf (stderr, "Error: %s\n",
181                    error.message);
182           exit (1);
183         }
184
185       if (reply)
186         {
187           print_message (reply);
188           dbus_message_unref (reply);
189         }
190     }
191   else
192     {
193       dbus_connection_send (connection, message, NULL);
194       dbus_connection_flush (connection);
195     }
196
197   dbus_message_unref (message);
198
199   dbus_connection_disconnect (connection);
200
201   exit (0);
202 }