* tools/dbus-send.c (main):
[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 const char *appname;
31
32 static void
33 usage (int ecode)
34 {
35   fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
36   exit (ecode);
37 }
38
39 static void
40 append_arg (DBusMessageIter *iter, int type, const char *value)
41 {
42   dbus_uint32_t uint32;
43   dbus_int32_t int32;
44   double d;
45   unsigned char byte;
46   dbus_bool_t v_BOOLEAN;
47   
48   /* FIXME - we are ignoring OOM returns on all these functions */
49   switch (type)
50     {
51     case DBUS_TYPE_BYTE:
52       byte = strtoul (value, NULL, 0);
53       dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
54       break;
55
56     case DBUS_TYPE_DOUBLE:
57       d = strtod (value, NULL);
58       dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
59       break;
60
61     case DBUS_TYPE_INT32:
62       int32 = strtol (value, NULL, 0);
63       dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
64       break;
65
66     case DBUS_TYPE_UINT32:
67       uint32 = strtoul (value, NULL, 0);
68       dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
69       break;
70
71     case DBUS_TYPE_STRING:
72       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
73       break;
74
75     case DBUS_TYPE_OBJECT_PATH:
76       dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
77       break;
78
79     case DBUS_TYPE_BOOLEAN:
80       if (strcmp (value, "true") == 0)
81         {
82           v_BOOLEAN = TRUE;
83           dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
84         }
85       else if (strcmp (value, "false") == 0)
86         {
87           v_BOOLEAN = FALSE;
88           dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
89         }
90       else
91         {
92           fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
93           exit (1);
94         }
95       break;
96
97     default:
98       fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
99       exit (1);
100     }
101 }
102
103 static void
104 append_array (DBusMessageIter *iter, int type, const char *value)
105 {
106   const char *c;
107
108   append_arg (iter, type, value);
109   c = value;
110   while ((c = strchr (c + 1, ',')) != NULL)
111     {
112       append_arg (iter, type, c);
113     }
114 }
115
116 int
117 main (int argc, char *argv[])
118 {
119   DBusConnection *connection;
120   DBusError error;
121   DBusMessage *message;
122   int print_reply;
123   int reply_timeout;
124   DBusMessageIter iter;
125   int i;
126   DBusBusType type = DBUS_BUS_SESSION;
127   const char *dest = NULL;
128   const char *name = NULL;
129   const char *path = NULL;
130   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
131   const char *type_str = NULL;
132
133   appname = argv[0];
134   
135   if (argc < 3)
136     usage (1);
137
138   print_reply = FALSE;
139   reply_timeout = -1;
140   
141   for (i = 1; i < argc && name == NULL; i++)
142     {
143       char *arg = argv[i];
144
145       if (strcmp (arg, "--system") == 0)
146         type = DBUS_BUS_SYSTEM;
147       else if (strcmp (arg, "--session") == 0)
148         type = DBUS_BUS_SESSION;
149       else if (strcmp (arg, "--print-reply") == 0)
150         {
151           print_reply = TRUE;
152           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
153         }
154       else if (strstr (arg, "--reply-timeout=") == arg)
155         {
156           reply_timeout = strtol (strchr (arg, '=') + 1,
157                                   NULL, 10);
158         }
159       else if (strstr (arg, "--dest=") == arg)
160         dest = strchr (arg, '=') + 1;
161       else if (strstr (arg, "--type=") == arg)
162         type_str = strchr (arg, '=') + 1;
163       else if (!strcmp(arg, "--help"))
164         usage (0);
165       else if (arg[0] == '-')
166         usage (1);
167       else if (path == NULL)
168         path = arg;
169       else if (name == NULL)
170         name = arg;
171       else
172         usage (1);
173     }
174
175   if (name == NULL)
176     usage (1);
177
178   if (type_str != NULL)
179     {
180       message_type = dbus_message_type_from_string (type_str);
181       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
182             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
183         {
184           fprintf (stderr, "Message type \"%s\" is not supported\n",
185                    type_str);
186           exit (1);
187         }
188     }
189   
190   dbus_error_init (&error);
191   connection = dbus_bus_get (type, &error);
192   if (connection == NULL)
193     {
194       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
195                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
196                error.message);
197       dbus_error_free (&error);
198       exit (1);
199     }
200
201   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
202     {
203       char *last_dot;
204
205       last_dot = strrchr (name, '.');
206       if (last_dot == NULL)
207         {
208           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
209                    name);
210           exit (1);
211         }
212       *last_dot = '\0';
213       
214       message = dbus_message_new_method_call (NULL,
215                                               path,
216                                               name,
217                                               last_dot + 1);
218     }
219   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
220     {
221       char *last_dot;
222
223       last_dot = strrchr (name, '.');
224       if (last_dot == NULL)
225         {
226           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
227                    name);
228           exit (1);
229         }
230       *last_dot = '\0';
231       
232       message = dbus_message_new_signal (path, name, last_dot + 1);
233     }
234   else
235     {
236       fprintf (stderr, "Internal error, unknown message type\n");
237       exit (1);
238     }
239
240   if (message == NULL)
241     {
242       fprintf (stderr, "Couldn't allocate D-BUS message\n");
243       exit (1);
244     }
245
246   if (dest && !dbus_message_set_destination (message, dest))
247     {
248       fprintf (stderr, "Not enough memory\n");
249       exit (1);
250     }
251   
252   dbus_message_iter_init_append (message, &iter);
253
254   while (i < argc)
255     {
256       char *arg;
257       char *c;
258       int type;
259       int container_type;
260       DBusMessageIter *target_iter;
261       DBusMessageIter container_iter;
262
263       type = DBUS_TYPE_INVALID;
264       arg = argv[i++];
265       c = strchr (arg, ':');
266
267       if (c == NULL)
268         {
269           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
270           exit (1);
271         }
272
273       *(c++) = 0;
274
275       container_type = DBUS_TYPE_INVALID;
276
277       if (strcmp (arg, "variant") == 0)
278         container_type = DBUS_TYPE_VARIANT;
279       else if (strcmp (arg, "array") == 0)
280         container_type = DBUS_TYPE_ARRAY;
281
282       if (container_type != DBUS_TYPE_INVALID)
283         {
284           arg = c;
285           c = strchr (arg, ':');
286           if (c == NULL)
287             {
288               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
289               exit (1);
290             }
291           *(c++) = 0;
292         }
293
294       if (arg[0] == 0 || !strcmp (arg, "string"))
295         type = DBUS_TYPE_STRING;
296       else if (!strcmp (arg, "int32"))
297         type = DBUS_TYPE_INT32;
298       else if (!strcmp (arg, "uint32"))
299         type = DBUS_TYPE_UINT32;
300       else if (!strcmp (arg, "double"))
301         type = DBUS_TYPE_DOUBLE;
302       else if (!strcmp (arg, "byte"))
303         type = DBUS_TYPE_BYTE;
304       else if (!strcmp (arg, "boolean"))
305         type = DBUS_TYPE_BOOLEAN;
306       else if (!strcmp (arg, "objpath"))
307         type = DBUS_TYPE_OBJECT_PATH;
308       else
309         {
310           fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
311           exit (1);
312         }
313
314       if (container_type != DBUS_TYPE_INVALID)
315         {
316           char sig[2];
317           sig[0] = type;
318           sig[1] = '\0';
319           dbus_message_iter_open_container (&iter,
320                                             container_type,
321                                             sig,
322                                             &container_iter);
323           target_iter = &container_iter;
324         }
325       else
326         target_iter = &iter;
327
328       if (container_type == DBUS_TYPE_ARRAY)
329         {
330           append_array (target_iter, type, c);
331         }
332       else
333         append_arg (target_iter, type, c);
334
335       if (container_type != DBUS_TYPE_INVALID)
336         {
337           dbus_message_iter_close_container (&iter,
338                                              &container_iter);
339         }
340     }
341
342   if (print_reply)
343     {
344       DBusMessage *reply;
345
346       dbus_error_init (&error);
347       reply = dbus_connection_send_with_reply_and_block (connection,
348                                                          message, reply_timeout,
349                                                          &error);
350       if (dbus_error_is_set (&error))
351         {
352           fprintf (stderr, "Error: %s\n",
353                    error.message);
354           exit (1);
355         }
356
357       if (reply)
358         {
359           print_message (reply);
360           dbus_message_unref (reply);
361         }
362     }
363   else
364     {
365       dbus_connection_send (connection, message, NULL);
366       dbus_connection_flush (connection);
367     }
368
369   dbus_message_unref (message);
370
371   dbus_connection_close (connection);
372
373   exit (0);
374 }