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