* tools/dbus-print-message.c (print_message): added printing of the reply serial...
[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 <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       sig[0] = keytype;
155       sig[1] = valtype;
156       sig[2] = '\0';
157       
158       dbus_message_iter_open_container (iter,
159                                         DBUS_TYPE_DICT_ENTRY,
160                                         sig,
161                                         &subiter);
162
163       append_arg (&subiter, keytype, val);
164       val = strtok (NULL, ",");
165       if (val == NULL)
166         {
167           fprintf (stderr, "%s: Malformed dictionary\n", appname);
168           exit (1);
169         }
170       append_arg (&subiter, valtype, val);
171
172       dbus_message_iter_close_container (iter, &subiter);
173       val = strtok (NULL, ",");
174     } 
175   free (dupval);
176 }
177
178 static int
179 type_from_name (const char *arg)
180 {
181   int type;
182   if (!strcmp (arg, "string"))
183     type = DBUS_TYPE_STRING;
184   else if (!strcmp (arg, "int16"))
185     type = DBUS_TYPE_INT16;
186   else if (!strcmp (arg, "uint16"))
187     type = DBUS_TYPE_UINT16;
188   else if (!strcmp (arg, "int32"))
189     type = DBUS_TYPE_INT32;
190   else if (!strcmp (arg, "uint32"))
191     type = DBUS_TYPE_UINT32;
192   else if (!strcmp (arg, "int64"))
193     type = DBUS_TYPE_INT64;
194   else if (!strcmp (arg, "uint64"))
195     type = DBUS_TYPE_UINT64;
196   else if (!strcmp (arg, "double"))
197     type = DBUS_TYPE_DOUBLE;
198   else if (!strcmp (arg, "byte"))
199     type = DBUS_TYPE_BYTE;
200   else if (!strcmp (arg, "boolean"))
201     type = DBUS_TYPE_BOOLEAN;
202   else if (!strcmp (arg, "objpath"))
203     type = DBUS_TYPE_OBJECT_PATH;
204   else
205     {
206       fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
207       exit (1);
208     }
209   return type;
210 }
211
212 int
213 main (int argc, char *argv[])
214 {
215   DBusConnection *connection;
216   DBusError error;
217   DBusMessage *message;
218   int print_reply;
219   int print_reply_literal;
220   int reply_timeout;
221   DBusMessageIter iter;
222   int i;
223   DBusBusType type = DBUS_BUS_SESSION;
224   const char *dest = NULL;
225   const char *name = NULL;
226   const char *path = NULL;
227   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
228   const char *type_str = NULL;
229
230   appname = argv[0];
231   
232   if (argc < 3)
233     usage (1);
234
235   print_reply = FALSE;
236   print_reply_literal = FALSE;
237   reply_timeout = -1;
238   
239   for (i = 1; i < argc && name == NULL; i++)
240     {
241       char *arg = argv[i];
242
243       if (strcmp (arg, "--system") == 0)
244         type = DBUS_BUS_SYSTEM;
245       else if (strcmp (arg, "--session") == 0)
246         type = DBUS_BUS_SESSION;
247       else if (strncmp (arg, "--print-reply", 13) == 0)
248         {
249           print_reply = TRUE;
250           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
251           if (*(arg + 13) != '\0')
252             print_reply_literal = TRUE;
253         }
254       else if (strstr (arg, "--reply-timeout=") == arg)
255         {
256           reply_timeout = strtol (strchr (arg, '=') + 1,
257                                   NULL, 10);
258         }
259       else if (strstr (arg, "--dest=") == arg)
260         dest = strchr (arg, '=') + 1;
261       else if (strstr (arg, "--type=") == arg)
262         type_str = strchr (arg, '=') + 1;
263       else if (!strcmp(arg, "--help"))
264         usage (0);
265       else if (arg[0] == '-')
266         usage (1);
267       else if (path == NULL)
268         path = arg;
269       else if (name == NULL)
270         name = arg;
271       else
272         usage (1);
273     }
274
275   if (name == NULL)
276     usage (1);
277
278   if (type_str != NULL)
279     {
280       message_type = dbus_message_type_from_string (type_str);
281       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
282             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
283         {
284           fprintf (stderr, "Message type \"%s\" is not supported\n",
285                    type_str);
286           exit (1);
287         }
288     }
289   
290   dbus_error_init (&error);
291   connection = dbus_bus_get (type, &error);
292   if (connection == NULL)
293     {
294       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
295                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
296                error.message);
297       dbus_error_free (&error);
298       exit (1);
299     }
300
301   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
302     {
303       char *last_dot;
304
305       last_dot = strrchr (name, '.');
306       if (last_dot == NULL)
307         {
308           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
309                    name);
310           exit (1);
311         }
312       *last_dot = '\0';
313       
314       message = dbus_message_new_method_call (NULL,
315                                               path,
316                                               name,
317                                               last_dot + 1);
318       dbus_message_set_auto_start (message, TRUE);
319     }
320   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
321     {
322       char *last_dot;
323
324       last_dot = strrchr (name, '.');
325       if (last_dot == NULL)
326         {
327           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
328                    name);
329           exit (1);
330         }
331       *last_dot = '\0';
332       
333       message = dbus_message_new_signal (path, name, last_dot + 1);
334     }
335   else
336     {
337       fprintf (stderr, "Internal error, unknown message type\n");
338       exit (1);
339     }
340
341   if (message == NULL)
342     {
343       fprintf (stderr, "Couldn't allocate D-Bus message\n");
344       exit (1);
345     }
346
347   if (dest && !dbus_message_set_destination (message, dest))
348     {
349       fprintf (stderr, "Not enough memory\n");
350       exit (1);
351     }
352   
353   dbus_message_iter_init_append (message, &iter);
354
355   while (i < argc)
356     {
357       char *arg;
358       char *c;
359       int type;
360       int secondary_type;
361       int container_type;
362       DBusMessageIter *target_iter;
363       DBusMessageIter container_iter;
364
365       type = DBUS_TYPE_INVALID;
366       arg = argv[i++];
367       c = strchr (arg, ':');
368
369       if (c == NULL)
370         {
371           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
372           exit (1);
373         }
374
375       *(c++) = 0;
376
377       container_type = DBUS_TYPE_INVALID;
378
379       if (strcmp (arg, "variant") == 0)
380         container_type = DBUS_TYPE_VARIANT;
381       else if (strcmp (arg, "array") == 0)
382         container_type = DBUS_TYPE_ARRAY;
383       else if (strcmp (arg, "dict") == 0)
384         container_type = DBUS_TYPE_DICT_ENTRY;
385
386       if (container_type != DBUS_TYPE_INVALID)
387         {
388           arg = c;
389           c = strchr (arg, ':');
390           if (c == NULL)
391             {
392               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
393               exit (1);
394             }
395           *(c++) = 0;
396         }
397
398       if (arg[0] == 0)
399         type = DBUS_TYPE_STRING;
400       else
401         type = type_from_name (arg);
402
403       if (container_type == DBUS_TYPE_DICT_ENTRY)
404         {
405           char sig[5];
406           arg = c;
407           c = strchr (c, ':');
408           if (c == NULL)
409             {
410               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
411               exit (1);
412             }
413           *(c++) = 0;
414           secondary_type = type_from_name (arg);
415           sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
416           sig[1] = type;
417           sig[2] = secondary_type;
418           sig[3] = DBUS_DICT_ENTRY_END_CHAR;
419           sig[4] = '\0';
420           dbus_message_iter_open_container (&iter,
421                                             DBUS_TYPE_ARRAY,
422                                             sig,
423                                             &container_iter);
424           target_iter = &container_iter;
425         }
426       else if (container_type != DBUS_TYPE_INVALID)
427         {
428           char sig[2];
429           sig[0] = type;
430           sig[1] = '\0';
431           dbus_message_iter_open_container (&iter,
432                                             container_type,
433                                             sig,
434                                             &container_iter);
435           target_iter = &container_iter;
436         }
437       else
438         target_iter = &iter;
439
440       if (container_type == DBUS_TYPE_ARRAY)
441         {
442           append_array (target_iter, type, c);
443         }
444       else if (container_type == DBUS_TYPE_DICT_ENTRY)
445         {
446           append_dict (target_iter, type, secondary_type, c);
447         }
448       else
449         append_arg (target_iter, type, c);
450
451       if (container_type != DBUS_TYPE_INVALID)
452         {
453           dbus_message_iter_close_container (&iter,
454                                              &container_iter);
455         }
456     }
457
458   if (print_reply)
459     {
460       DBusMessage *reply;
461
462       dbus_error_init (&error);
463       reply = dbus_connection_send_with_reply_and_block (connection,
464                                                          message, reply_timeout,
465                                                          &error);
466       if (dbus_error_is_set (&error))
467         {
468           fprintf (stderr, "Error %s: %s\n",
469                    error.name,
470                    error.message);
471           exit (1);
472         }
473
474       if (reply)
475         {
476           print_message (reply, print_reply_literal);
477           dbus_message_unref (reply);
478         }
479     }
480   else
481     {
482       dbus_connection_send (connection, message, NULL);
483       dbus_connection_flush (connection);
484     }
485
486   dbus_message_unref (message);
487
488   dbus_connection_unref (connection);
489
490   exit (0);
491 }