fix dbus-send so it can correctly send dictionaries
[platform/upstream/dbus.git] / tools / dbus-send.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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 <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <dbus/dbus.h>
28
29 #include "dbus-print-message.h"
30
31 static const char *appname;
32
33 static void
34 usage (int ecode)
35 {
36   fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply=(literal)] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
37   exit (ecode);
38 }
39
40 static void
41 append_arg (DBusMessageIter *iter, int type, const char *value)
42 {
43   dbus_uint16_t uint16;
44   dbus_int16_t int16;
45   dbus_uint32_t uint32;
46   dbus_int32_t int32;
47   dbus_uint64_t uint64;
48   dbus_int64_t int64;
49   double d;
50   unsigned char byte;
51   dbus_bool_t v_BOOLEAN;
52   
53   /* FIXME - we are ignoring OOM returns on all these functions */
54   switch (type)
55     {
56     case DBUS_TYPE_BYTE:
57       byte = strtoul (value, NULL, 0);
58       dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
59       break;
60
61     case DBUS_TYPE_DOUBLE:
62       d = strtod (value, NULL);
63       dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
64       break;
65
66     case DBUS_TYPE_INT16:
67       int16 = strtol (value, NULL, 0);
68       dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
69       break;
70
71     case DBUS_TYPE_UINT16:
72       uint16 = strtoul (value, NULL, 0);
73       dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
74       break;
75
76     case DBUS_TYPE_INT32:
77       int32 = strtol (value, NULL, 0);
78       dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
79       break;
80
81     case DBUS_TYPE_UINT32:
82       uint32 = strtoul (value, NULL, 0);
83       dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
84       break;
85
86     case DBUS_TYPE_INT64:
87       int64 = strtoll (value, NULL, 0);
88       dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
89       break;
90
91     case DBUS_TYPE_UINT64:
92       uint64 = strtoull (value, NULL, 0);
93       dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
94       break;
95
96     case DBUS_TYPE_STRING:
97       dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
98       break;
99
100     case DBUS_TYPE_OBJECT_PATH:
101       dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
102       break;
103
104     case DBUS_TYPE_BOOLEAN:
105       if (strcmp (value, "true") == 0)
106         {
107           v_BOOLEAN = TRUE;
108           dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
109         }
110       else if (strcmp (value, "false") == 0)
111         {
112           v_BOOLEAN = FALSE;
113           dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
114         }
115       else
116         {
117           fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
118           exit (1);
119         }
120       break;
121
122     default:
123       fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
124       exit (1);
125     }
126 }
127
128 static void
129 append_array (DBusMessageIter *iter, int type, const char *value)
130 {
131   const char *val;
132   char *dupval = strdup (value);
133
134   val = strtok (dupval, ",");
135   while (val != NULL)
136     {
137       append_arg (iter, type, val);
138       val = strtok (NULL, ",");
139     }
140   free (dupval);
141 }
142
143 static void
144 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
145 {
146   const char *val;
147   char *dupval = strdup (value);
148
149   val = strtok (dupval, ",");
150   while (val != NULL)
151     {
152       DBusMessageIter subiter;
153       char sig[3];
154
155       sig[0] = keytype;
156       sig[1] = valtype;
157       sig[2] = '\0';
158  
159       dbus_message_iter_open_container (iter,
160                                         DBUS_TYPE_DICT_ENTRY,
161                                         sig,
162                                         &subiter);
163
164       append_arg (&subiter, keytype, val);
165       val = strtok (NULL, ",");
166       if (val == NULL)
167         {
168           fprintf (stderr, "%s: Malformed dictionary\n", appname);
169           exit (1);
170         }
171       append_arg (&subiter, valtype, val);
172
173       dbus_message_iter_close_container (iter, &subiter);
174       val = strtok (NULL, ",");
175     } 
176   free (dupval);
177 }
178
179 static int
180 type_from_name (const char *arg)
181 {
182   int type;
183   if (!strcmp (arg, "string"))
184     type = DBUS_TYPE_STRING;
185   else if (!strcmp (arg, "int16"))
186     type = DBUS_TYPE_INT16;
187   else if (!strcmp (arg, "uint16"))
188     type = DBUS_TYPE_UINT16;
189   else if (!strcmp (arg, "int32"))
190     type = DBUS_TYPE_INT32;
191   else if (!strcmp (arg, "uint32"))
192     type = DBUS_TYPE_UINT32;
193   else if (!strcmp (arg, "int64"))
194     type = DBUS_TYPE_INT64;
195   else if (!strcmp (arg, "uint64"))
196     type = DBUS_TYPE_UINT64;
197   else if (!strcmp (arg, "double"))
198     type = DBUS_TYPE_DOUBLE;
199   else if (!strcmp (arg, "byte"))
200     type = DBUS_TYPE_BYTE;
201   else if (!strcmp (arg, "boolean"))
202     type = DBUS_TYPE_BOOLEAN;
203   else if (!strcmp (arg, "objpath"))
204     type = DBUS_TYPE_OBJECT_PATH;
205   else
206     {
207       fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
208       exit (1);
209     }
210   return type;
211 }
212
213 int
214 main (int argc, char *argv[])
215 {
216   DBusConnection *connection;
217   DBusError error;
218   DBusMessage *message;
219   int print_reply;
220   int print_reply_literal;
221   int reply_timeout;
222   DBusMessageIter iter;
223   int i;
224   DBusBusType type = DBUS_BUS_SESSION;
225   const char *dest = NULL;
226   const char *name = NULL;
227   const char *path = NULL;
228   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
229   const char *type_str = NULL;
230
231   appname = argv[0];
232   
233   if (argc < 3)
234     usage (1);
235
236   print_reply = FALSE;
237   print_reply_literal = FALSE;
238   reply_timeout = -1;
239   
240   for (i = 1; i < argc && name == NULL; i++)
241     {
242       char *arg = argv[i];
243
244       if (strcmp (arg, "--system") == 0)
245         type = DBUS_BUS_SYSTEM;
246       else if (strcmp (arg, "--session") == 0)
247         type = DBUS_BUS_SESSION;
248       else if (strncmp (arg, "--print-reply", 13) == 0)
249         {
250           print_reply = TRUE;
251           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
252           if (*(arg + 13) != '\0')
253             print_reply_literal = TRUE;
254         }
255       else if (strstr (arg, "--reply-timeout=") == arg)
256         {
257           reply_timeout = strtol (strchr (arg, '=') + 1,
258                                   NULL, 10);
259         }
260       else if (strstr (arg, "--dest=") == arg)
261         dest = strchr (arg, '=') + 1;
262       else if (strstr (arg, "--type=") == arg)
263         type_str = strchr (arg, '=') + 1;
264       else if (!strcmp(arg, "--help"))
265         usage (0);
266       else if (arg[0] == '-')
267         usage (1);
268       else if (path == NULL)
269         path = arg;
270       else if (name == NULL)
271         name = arg;
272       else
273         usage (1);
274     }
275
276   if (name == NULL)
277     usage (1);
278
279   if (type_str != NULL)
280     {
281       message_type = dbus_message_type_from_string (type_str);
282       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
283             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
284         {
285           fprintf (stderr, "Message type \"%s\" is not supported\n",
286                    type_str);
287           exit (1);
288         }
289     }
290   
291   dbus_error_init (&error);
292   connection = dbus_bus_get (type, &error);
293   if (connection == NULL)
294     {
295       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
296                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
297                error.message);
298       dbus_error_free (&error);
299       exit (1);
300     }
301
302   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
303     {
304       char *last_dot;
305
306       last_dot = strrchr (name, '.');
307       if (last_dot == NULL)
308         {
309           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
310                    name);
311           exit (1);
312         }
313       *last_dot = '\0';
314       
315       message = dbus_message_new_method_call (NULL,
316                                               path,
317                                               name,
318                                               last_dot + 1);
319       dbus_message_set_auto_start (message, TRUE);
320     }
321   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
322     {
323       char *last_dot;
324
325       last_dot = strrchr (name, '.');
326       if (last_dot == NULL)
327         {
328           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
329                    name);
330           exit (1);
331         }
332       *last_dot = '\0';
333       
334       message = dbus_message_new_signal (path, name, last_dot + 1);
335     }
336   else
337     {
338       fprintf (stderr, "Internal error, unknown message type\n");
339       exit (1);
340     }
341
342   if (message == NULL)
343     {
344       fprintf (stderr, "Couldn't allocate D-Bus message\n");
345       exit (1);
346     }
347
348   if (dest && !dbus_message_set_destination (message, dest))
349     {
350       fprintf (stderr, "Not enough memory\n");
351       exit (1);
352     }
353   
354   dbus_message_iter_init_append (message, &iter);
355
356   while (i < argc)
357     {
358       char *arg;
359       char *c;
360       int type;
361       int secondary_type;
362       int container_type;
363       DBusMessageIter *target_iter;
364       DBusMessageIter container_iter;
365
366       type = DBUS_TYPE_INVALID;
367       arg = argv[i++];
368       c = strchr (arg, ':');
369
370       if (c == NULL)
371         {
372           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
373           exit (1);
374         }
375
376       *(c++) = 0;
377
378       container_type = DBUS_TYPE_INVALID;
379
380       if (strcmp (arg, "variant") == 0)
381         container_type = DBUS_TYPE_VARIANT;
382       else if (strcmp (arg, "array") == 0)
383         container_type = DBUS_TYPE_ARRAY;
384       else if (strcmp (arg, "dict") == 0)
385         container_type = DBUS_TYPE_DICT_ENTRY;
386
387       if (container_type != DBUS_TYPE_INVALID)
388         {
389           arg = c;
390           c = strchr (arg, ':');
391           if (c == NULL)
392             {
393               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
394               exit (1);
395             }
396           *(c++) = 0;
397         }
398
399       if (arg[0] == 0)
400         type = DBUS_TYPE_STRING;
401       else
402         type = type_from_name (arg);
403
404       if (container_type == DBUS_TYPE_DICT_ENTRY)
405         {
406           char sig[5];
407           arg = c;
408           c = strchr (c, ':');
409           if (c == NULL)
410             {
411               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
412               exit (1);
413             }
414           *(c++) = 0;
415           secondary_type = type_from_name (arg);
416           sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
417           sig[1] = type;
418           sig[2] = secondary_type;
419           sig[3] = DBUS_DICT_ENTRY_END_CHAR;
420           sig[4] = '\0';
421           dbus_message_iter_open_container (&iter,
422                                             DBUS_TYPE_ARRAY,
423                                             sig,
424                                             &container_iter);
425           target_iter = &container_iter;
426         }
427       else if (container_type != DBUS_TYPE_INVALID)
428         {
429           char sig[2];
430           sig[0] = type;
431           sig[1] = '\0';
432           dbus_message_iter_open_container (&iter,
433                                             container_type,
434                                             sig,
435                                             &container_iter);
436           target_iter = &container_iter;
437         }
438       else
439         target_iter = &iter;
440
441       if (container_type == DBUS_TYPE_ARRAY)
442         {
443           append_array (target_iter, type, c);
444         }
445       else if (container_type == DBUS_TYPE_DICT_ENTRY)
446         {
447           append_dict (target_iter, type, secondary_type, c);
448         }
449       else
450         append_arg (target_iter, type, c);
451
452       if (container_type != DBUS_TYPE_INVALID)
453         {
454           dbus_message_iter_close_container (&iter,
455                                              &container_iter);
456         }
457     }
458
459   if (print_reply)
460     {
461       DBusMessage *reply;
462
463       dbus_error_init (&error);
464       reply = dbus_connection_send_with_reply_and_block (connection,
465                                                          message, reply_timeout,
466                                                          &error);
467       if (dbus_error_is_set (&error))
468         {
469           fprintf (stderr, "Error %s: %s\n",
470                    error.name,
471                    error.message);
472           exit (1);
473         }
474
475       if (reply)
476         {
477           print_message (reply, print_reply_literal);
478           dbus_message_unref (reply);
479         }
480     }
481   else
482     {
483       dbus_connection_send (connection, message, NULL);
484       dbus_connection_flush (connection);
485     }
486
487   dbus_message_unref (message);
488
489   dbus_connection_unref (connection);
490
491   exit (0);
492 }