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