2005-06-29 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] [--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 reply_timeout;
187   DBusMessageIter iter;
188   int i;
189   DBusBusType type = DBUS_BUS_SESSION;
190   const char *dest = NULL;
191   const char *name = NULL;
192   const char *path = NULL;
193   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
194   const char *type_str = NULL;
195
196   appname = argv[0];
197   
198   if (argc < 3)
199     usage (1);
200
201   print_reply = FALSE;
202   reply_timeout = -1;
203   
204   for (i = 1; i < argc && name == NULL; i++)
205     {
206       char *arg = argv[i];
207
208       if (strcmp (arg, "--system") == 0)
209         type = DBUS_BUS_SYSTEM;
210       else if (strcmp (arg, "--session") == 0)
211         type = DBUS_BUS_SESSION;
212       else if (strcmp (arg, "--print-reply") == 0)
213         {
214           print_reply = TRUE;
215           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
216         }
217       else if (strstr (arg, "--reply-timeout=") == arg)
218         {
219           reply_timeout = strtol (strchr (arg, '=') + 1,
220                                   NULL, 10);
221         }
222       else if (strstr (arg, "--dest=") == arg)
223         dest = strchr (arg, '=') + 1;
224       else if (strstr (arg, "--type=") == arg)
225         type_str = strchr (arg, '=') + 1;
226       else if (!strcmp(arg, "--help"))
227         usage (0);
228       else if (arg[0] == '-')
229         usage (1);
230       else if (path == NULL)
231         path = arg;
232       else if (name == NULL)
233         name = arg;
234       else
235         usage (1);
236     }
237
238   if (name == NULL)
239     usage (1);
240
241   if (type_str != NULL)
242     {
243       message_type = dbus_message_type_from_string (type_str);
244       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
245             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
246         {
247           fprintf (stderr, "Message type \"%s\" is not supported\n",
248                    type_str);
249           exit (1);
250         }
251     }
252   
253   dbus_error_init (&error);
254   connection = dbus_bus_get (type, &error);
255   if (connection == NULL)
256     {
257       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
258                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
259                error.message);
260       dbus_error_free (&error);
261       exit (1);
262     }
263
264   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
265     {
266       char *last_dot;
267
268       last_dot = strrchr (name, '.');
269       if (last_dot == NULL)
270         {
271           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
272                    name);
273           exit (1);
274         }
275       *last_dot = '\0';
276       
277       message = dbus_message_new_method_call (NULL,
278                                               path,
279                                               name,
280                                               last_dot + 1);
281       dbus_message_set_auto_start (message, TRUE);
282     }
283   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
284     {
285       char *last_dot;
286
287       last_dot = strrchr (name, '.');
288       if (last_dot == NULL)
289         {
290           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
291                    name);
292           exit (1);
293         }
294       *last_dot = '\0';
295       
296       message = dbus_message_new_signal (path, name, last_dot + 1);
297     }
298   else
299     {
300       fprintf (stderr, "Internal error, unknown message type\n");
301       exit (1);
302     }
303
304   if (message == NULL)
305     {
306       fprintf (stderr, "Couldn't allocate D-BUS message\n");
307       exit (1);
308     }
309
310   if (dest && !dbus_message_set_destination (message, dest))
311     {
312       fprintf (stderr, "Not enough memory\n");
313       exit (1);
314     }
315   
316   dbus_message_iter_init_append (message, &iter);
317
318   while (i < argc)
319     {
320       char *arg;
321       char *c;
322       int type;
323       int secondary_type;
324       int container_type;
325       DBusMessageIter *target_iter;
326       DBusMessageIter container_iter;
327
328       type = DBUS_TYPE_INVALID;
329       arg = argv[i++];
330       c = strchr (arg, ':');
331
332       if (c == NULL)
333         {
334           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
335           exit (1);
336         }
337
338       *(c++) = 0;
339
340       container_type = DBUS_TYPE_INVALID;
341
342       if (strcmp (arg, "variant") == 0)
343         container_type = DBUS_TYPE_VARIANT;
344       else if (strcmp (arg, "array") == 0)
345         container_type = DBUS_TYPE_ARRAY;
346       else if (strcmp (arg, "dict") == 0)
347         container_type = DBUS_TYPE_DICT_ENTRY;
348
349       if (container_type != DBUS_TYPE_INVALID)
350         {
351           arg = c;
352           c = strchr (arg, ':');
353           if (c == NULL)
354             {
355               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
356               exit (1);
357             }
358           *(c++) = 0;
359         }
360
361       if (arg[0] == 0)
362         type = DBUS_TYPE_STRING;
363       else
364         type = type_from_name (arg);
365
366       if (container_type == DBUS_TYPE_DICT_ENTRY)
367         {
368           arg = c;
369           c = strchr (c, ':');
370           if (c == NULL)
371             {
372               fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
373               exit (1);
374             }
375           *(c++) = 0;
376           secondary_type = type_from_name (arg);
377           char sig[5];
378           sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
379           sig[1] = type;
380           sig[2] = secondary_type;
381           sig[3] = DBUS_DICT_ENTRY_END_CHAR;
382           sig[4] = '\0';
383           dbus_message_iter_open_container (&iter,
384                                             DBUS_TYPE_ARRAY,
385                                             sig,
386                                             &container_iter);
387           target_iter = &container_iter;
388         }
389       else if (container_type != DBUS_TYPE_INVALID)
390         {
391           char sig[2];
392           sig[0] = type;
393           sig[1] = '\0';
394           dbus_message_iter_open_container (&iter,
395                                             container_type,
396                                             sig,
397                                             &container_iter);
398           target_iter = &container_iter;
399         }
400       else
401         target_iter = &iter;
402
403       if (container_type == DBUS_TYPE_ARRAY)
404         {
405           append_array (target_iter, type, c);
406         }
407       else if (container_type == DBUS_TYPE_DICT_ENTRY)
408         {
409           append_dict (target_iter, type, secondary_type, c);
410         }
411       else
412         append_arg (target_iter, type, c);
413
414       if (container_type != DBUS_TYPE_INVALID)
415         {
416           dbus_message_iter_close_container (&iter,
417                                              &container_iter);
418         }
419     }
420
421   if (print_reply)
422     {
423       DBusMessage *reply;
424
425       dbus_error_init (&error);
426       reply = dbus_connection_send_with_reply_and_block (connection,
427                                                          message, reply_timeout,
428                                                          &error);
429       if (dbus_error_is_set (&error))
430         {
431           fprintf (stderr, "Error %s: %s\n",
432                    error.name,
433                    error.message);
434           exit (1);
435         }
436
437       if (reply)
438         {
439           print_message (reply);
440           dbus_message_unref (reply);
441         }
442     }
443   else
444     {
445       dbus_connection_send (connection, message, NULL);
446       dbus_connection_flush (connection);
447     }
448
449   dbus_message_unref (message);
450
451   dbus_connection_close (connection);
452
453   exit (0);
454 }