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