5ff9f576d2dd95ec5371b672ba1077f037ffe9fe
[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 void
31 usage (char *name, int ecode)
32 {
33   fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=SERVICE] [--type=TYPE] [--print-reply] <destination object path> <message name> [contents ...]\n", name);
34   exit (ecode);
35 }
36
37 int
38 main (int argc, char *argv[])
39 {
40   DBusConnection *connection;
41   DBusError error;
42   DBusMessage *message;
43   int print_reply;
44   DBusMessageIter iter;
45   int i;
46   DBusBusType type = DBUS_BUS_SESSION;
47   const char *dest = NULL;
48   const char *name = NULL;
49   const char *path = NULL;
50   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
51   const char *type_str = NULL;
52   
53   if (argc < 3)
54     usage (argv[0], 1);
55
56   print_reply = FALSE;
57   
58   for (i = 1; i < argc && name == NULL; i++)
59     {
60       char *arg = argv[i];
61
62       if (strcmp (arg, "--system") == 0)
63         type = DBUS_BUS_SYSTEM;
64       else if (strcmp (arg, "--session") == 0)
65         type = DBUS_BUS_SESSION;
66       else if (strcmp (arg, "--print-reply") == 0)
67         {
68           print_reply = TRUE;
69           message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
70         }
71       else if (strstr (arg, "--dest=") == arg)
72         dest = strchr (arg, '=') + 1;
73       else if (strstr (arg, "--type=") == arg)
74         type_str = strchr (arg, '=') + 1;
75       else if (!strcmp(arg, "--help"))
76         usage (argv[0], 0);
77       else if (arg[0] == '-')
78         usage (argv[0], 1);
79       else if (path == NULL)
80         path = arg;
81       else if (name == NULL)
82         name = arg;
83       else
84         usage (argv[0], 1);
85     }
86
87   if (name == NULL)
88     usage (argv[0], 1);
89
90   if (type_str != NULL)
91     {
92       message_type = dbus_message_type_from_string (type_str);
93       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
94             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
95         {
96           fprintf (stderr, "Message type \"%s\" is not supported\n",
97                    type_str);
98           exit (1);
99         }
100     }
101   
102   dbus_error_init (&error);
103   connection = dbus_bus_get (type, &error);
104   if (connection == NULL)
105     {
106       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
107                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
108                error.message);
109       dbus_error_free (&error);
110       exit (1);
111     }
112
113   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
114     {
115       char *last_dot;
116
117       last_dot = strrchr (name, '.');
118       if (last_dot == NULL)
119         {
120           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
121                    name);
122           exit (1);
123         }
124       *last_dot = '\0';
125       
126       message = dbus_message_new_method_call (NULL,
127                                               path,
128                                               name,
129                                               last_dot + 1);
130     }
131   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
132     {
133       char *last_dot;
134
135       last_dot = strrchr (name, '.');
136       if (last_dot == NULL)
137         {
138           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
139                    name);
140           exit (1);
141         }
142       *last_dot = '\0';
143       
144       message = dbus_message_new_signal (path, name, last_dot + 1);
145     }
146   else
147     {
148       fprintf (stderr, "Internal error, unknown message type\n");
149       exit (1);
150     }
151
152   if (message == NULL)
153     {
154       fprintf (stderr, "Couldn't allocate D-BUS message\n");
155       exit (1);
156     }
157
158   if (dest && !dbus_message_set_destination (message, dest))
159     {
160       fprintf (stderr, "Not enough memory\n");
161       exit (1);
162     }
163   
164   dbus_message_append_iter_init (message, &iter);
165
166   while (i < argc)
167     {
168       char *arg;
169       char *c;
170       int type;
171       dbus_uint32_t uint32;
172       dbus_int32_t int32;
173       double d;
174       unsigned char byte;
175
176       type = DBUS_TYPE_INVALID;
177       arg = argv[i++];
178       c = strchr (arg, ':');
179
180       if (c == NULL)
181         {
182           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
183           exit (1);
184         }
185
186       *(c++) = 0;
187
188       if (arg[0] == 0 || !strcmp (arg, "string"))
189         type = DBUS_TYPE_STRING;
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, "double"))
195         type = DBUS_TYPE_DOUBLE;
196       else if (!strcmp (arg, "byte"))
197         type = DBUS_TYPE_BYTE;
198       else if (!strcmp (arg, "boolean"))
199         type = DBUS_TYPE_BOOLEAN;
200       else
201         {
202           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
203           exit (1);
204         }
205
206       /* FIXME - we are ignoring OOM returns on all these functions */
207       switch (type)
208         {
209         case DBUS_TYPE_BYTE:
210           byte = strtoul (c, NULL, 0);
211           dbus_message_iter_append_byte (&iter, byte);
212           break;
213
214         case DBUS_TYPE_DOUBLE:
215           d = strtod (c, NULL);
216           dbus_message_iter_append_double (&iter, d);
217           break;
218
219         case DBUS_TYPE_INT32:
220           int32 = strtol (c, NULL, 0);
221           dbus_message_iter_append_int32 (&iter, int32);
222           break;
223
224         case DBUS_TYPE_UINT32:
225           uint32 = strtoul (c, NULL, 0);
226           dbus_message_iter_append_uint32 (&iter, uint32);
227           break;
228
229         case DBUS_TYPE_STRING:
230           dbus_message_iter_append_string (&iter, c);
231           break;
232
233         case DBUS_TYPE_BOOLEAN:
234           if (strcmp(c, "true") == 0)
235             dbus_message_iter_append_boolean (&iter, TRUE);
236           else if (strcmp(c, "false") == 0)
237             dbus_message_iter_append_boolean (&iter, FALSE);
238           else
239             {
240               fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", argv[0], c);
241               exit (1);
242             }
243           break;
244
245         default:
246           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
247           exit (1);
248         }
249     }
250
251   if (print_reply)
252     {
253       DBusMessage *reply;
254
255       dbus_error_init (&error);
256       reply = dbus_connection_send_with_reply_and_block (connection,
257                                                          message, -1,
258                                                          &error);
259       if (dbus_error_is_set (&error))
260         {
261           fprintf (stderr, "Error: %s\n",
262                    error.message);
263           exit (1);
264         }
265
266       if (reply)
267         {
268           print_message (reply);
269           dbus_message_unref (reply);
270         }
271     }
272   else
273     {
274       dbus_connection_send (connection, message, NULL);
275       dbus_connection_flush (connection);
276     }
277
278   dbus_message_unref (message);
279
280   dbus_connection_disconnect (connection);
281
282   exit (0);
283 }