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