2004-08-11 Colin Walters <walters@redhat.com>
[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] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [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   int reply_timeout;
45   DBusMessageIter iter;
46   int i;
47   DBusBusType type = DBUS_BUS_SESSION;
48   const char *dest = NULL;
49   const char *name = NULL;
50   const char *path = NULL;
51   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
52   const char *type_str = NULL;
53   
54   if (argc < 3)
55     usage (argv[0], 1);
56
57   print_reply = FALSE;
58   reply_timeout = -1;
59   
60   for (i = 1; i < argc && name == NULL; i++)
61     {
62       char *arg = argv[i];
63
64       if (strcmp (arg, "--system") == 0)
65         type = DBUS_BUS_SYSTEM;
66       else if (strcmp (arg, "--session") == 0)
67         type = DBUS_BUS_SESSION;
68       else if (strcmp (arg, "--print-reply") == 0)
69         {
70           print_reply = TRUE;
71           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
72         }
73       else if (strstr (arg, "--reply-timeout=") == arg)
74         {
75           reply_timeout = strtol (strchr (arg, '=') + 1,
76                                   NULL, 10);
77         }
78       else if (strstr (arg, "--dest=") == arg)
79         dest = strchr (arg, '=') + 1;
80       else if (strstr (arg, "--type=") == arg)
81         type_str = strchr (arg, '=') + 1;
82       else if (!strcmp(arg, "--help"))
83         usage (argv[0], 0);
84       else if (arg[0] == '-')
85         usage (argv[0], 1);
86       else if (path == NULL)
87         path = arg;
88       else if (name == NULL)
89         name = arg;
90       else
91         usage (argv[0], 1);
92     }
93
94   if (name == NULL)
95     usage (argv[0], 1);
96
97   if (type_str != NULL)
98     {
99       message_type = dbus_message_type_from_string (type_str);
100       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
101             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
102         {
103           fprintf (stderr, "Message type \"%s\" is not supported\n",
104                    type_str);
105           exit (1);
106         }
107     }
108   
109   dbus_error_init (&error);
110   connection = dbus_bus_get (type, &error);
111   if (connection == NULL)
112     {
113       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
114                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
115                error.message);
116       dbus_error_free (&error);
117       exit (1);
118     }
119
120   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
121     {
122       char *last_dot;
123
124       last_dot = strrchr (name, '.');
125       if (last_dot == NULL)
126         {
127           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
128                    name);
129           exit (1);
130         }
131       *last_dot = '\0';
132       
133       message = dbus_message_new_method_call (NULL,
134                                               path,
135                                               name,
136                                               last_dot + 1);
137     }
138   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
139     {
140       char *last_dot;
141
142       last_dot = strrchr (name, '.');
143       if (last_dot == NULL)
144         {
145           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
146                    name);
147           exit (1);
148         }
149       *last_dot = '\0';
150       
151       message = dbus_message_new_signal (path, name, last_dot + 1);
152     }
153   else
154     {
155       fprintf (stderr, "Internal error, unknown message type\n");
156       exit (1);
157     }
158
159   if (message == NULL)
160     {
161       fprintf (stderr, "Couldn't allocate D-BUS message\n");
162       exit (1);
163     }
164
165   if (dest && !dbus_message_set_destination (message, dest))
166     {
167       fprintf (stderr, "Not enough memory\n");
168       exit (1);
169     }
170   
171   dbus_message_append_iter_init (message, &iter);
172
173   while (i < argc)
174     {
175       char *arg;
176       char *c;
177       int type;
178       dbus_uint32_t uint32;
179       dbus_int32_t int32;
180       double d;
181       unsigned char byte;
182
183       type = DBUS_TYPE_INVALID;
184       arg = argv[i++];
185       c = strchr (arg, ':');
186
187       if (c == NULL)
188         {
189           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
190           exit (1);
191         }
192
193       *(c++) = 0;
194
195       if (arg[0] == 0 || !strcmp (arg, "string"))
196         type = DBUS_TYPE_STRING;
197       else if (!strcmp (arg, "int32"))
198         type = DBUS_TYPE_INT32;
199       else if (!strcmp (arg, "uint32"))
200         type = DBUS_TYPE_UINT32;
201       else if (!strcmp (arg, "double"))
202         type = DBUS_TYPE_DOUBLE;
203       else if (!strcmp (arg, "byte"))
204         type = DBUS_TYPE_BYTE;
205       else if (!strcmp (arg, "boolean"))
206         type = DBUS_TYPE_BOOLEAN;
207       else
208         {
209           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
210           exit (1);
211         }
212
213       /* FIXME - we are ignoring OOM returns on all these functions */
214       switch (type)
215         {
216         case DBUS_TYPE_BYTE:
217           byte = strtoul (c, NULL, 0);
218           dbus_message_iter_append_byte (&iter, byte);
219           break;
220
221         case DBUS_TYPE_DOUBLE:
222           d = strtod (c, NULL);
223           dbus_message_iter_append_double (&iter, d);
224           break;
225
226         case DBUS_TYPE_INT32:
227           int32 = strtol (c, NULL, 0);
228           dbus_message_iter_append_int32 (&iter, int32);
229           break;
230
231         case DBUS_TYPE_UINT32:
232           uint32 = strtoul (c, NULL, 0);
233           dbus_message_iter_append_uint32 (&iter, uint32);
234           break;
235
236         case DBUS_TYPE_STRING:
237           dbus_message_iter_append_string (&iter, c);
238           break;
239
240         case DBUS_TYPE_BOOLEAN:
241           if (strcmp(c, "true") == 0)
242             dbus_message_iter_append_boolean (&iter, TRUE);
243           else if (strcmp(c, "false") == 0)
244             dbus_message_iter_append_boolean (&iter, FALSE);
245           else
246             {
247               fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", argv[0], c);
248               exit (1);
249             }
250           break;
251
252         default:
253           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
254           exit (1);
255         }
256     }
257
258   if (print_reply)
259     {
260       DBusMessage *reply;
261
262       dbus_error_init (&error);
263       reply = dbus_connection_send_with_reply_and_block (connection,
264                                                          message, reply_timeout,
265                                                          &error);
266       if (dbus_error_is_set (&error))
267         {
268           fprintf (stderr, "Error: %s\n",
269                    error.message);
270           exit (1);
271         }
272
273       if (reply)
274         {
275           print_message (reply);
276           dbus_message_unref (reply);
277         }
278     }
279   else
280     {
281       dbus_connection_send (connection, message, NULL);
282       dbus_connection_flush (connection);
283     }
284
285   dbus_message_unref (message);
286
287   dbus_connection_disconnect (connection);
288
289   exit (0);
290 }