1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-send.c Utility program to send messages from the command line
4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
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.
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.
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
26 #include <dbus/dbus.h>
28 #include "dbus-print-message.h"
30 static const char *appname;
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);
40 append_arg (DBusMessageIter *iter, int type, const char *value)
46 dbus_bool_t v_BOOLEAN;
48 /* FIXME - we are ignoring OOM returns on all these functions */
52 byte = strtoul (value, NULL, 0);
53 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
56 case DBUS_TYPE_DOUBLE:
57 d = strtod (value, NULL);
58 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
62 int32 = strtol (value, NULL, 0);
63 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
66 case DBUS_TYPE_UINT32:
67 uint32 = strtoul (value, NULL, 0);
68 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
71 case DBUS_TYPE_STRING:
72 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
75 case DBUS_TYPE_OBJECT_PATH:
76 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
79 case DBUS_TYPE_BOOLEAN:
80 if (strcmp (value, "true") == 0)
83 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
85 else if (strcmp (value, "false") == 0)
88 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
92 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
98 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
104 append_array (DBusMessageIter *iter, int type, const char *value)
107 char *dupval = strdup (value);
109 val = strtok (dupval, ",");
112 append_arg (iter, type, val);
113 val = strtok (NULL, ",");
119 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
122 char *dupval = strdup (value);
124 val = strtok (dupval, ",");
127 DBusMessageIter subiter;
133 dbus_message_iter_open_container (iter,
134 DBUS_TYPE_DICT_ENTRY,
138 append_arg (&subiter, keytype, val);
139 val = strtok (NULL, ",");
142 fprintf (stderr, "%s: Malformed dictionary\n", appname);
145 append_arg (&subiter, valtype, val);
147 dbus_message_iter_close_container (iter, &subiter);
148 val = strtok (NULL, ",");
154 type_from_name (const char *arg)
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;
173 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
180 main (int argc, char *argv[])
182 DBusConnection *connection;
184 DBusMessage *message;
187 DBusMessageIter iter;
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;
204 for (i = 1; i < argc && name == NULL; i++)
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)
215 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
217 else if (strstr (arg, "--reply-timeout=") == arg)
219 reply_timeout = strtol (strchr (arg, '=') + 1,
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"))
228 else if (arg[0] == '-')
230 else if (path == NULL)
232 else if (name == NULL)
241 if (type_str != NULL)
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))
247 fprintf (stderr, "Message type \"%s\" is not supported\n",
253 dbus_error_init (&error);
254 connection = dbus_bus_get (type, &error);
255 if (connection == NULL)
257 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
258 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
260 dbus_error_free (&error);
264 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
268 last_dot = strrchr (name, '.');
269 if (last_dot == NULL)
271 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
277 message = dbus_message_new_method_call (NULL,
281 dbus_message_set_auto_start (message, TRUE);
283 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
287 last_dot = strrchr (name, '.');
288 if (last_dot == NULL)
290 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
296 message = dbus_message_new_signal (path, name, last_dot + 1);
300 fprintf (stderr, "Internal error, unknown message type\n");
306 fprintf (stderr, "Couldn't allocate D-BUS message\n");
310 if (dest && !dbus_message_set_destination (message, dest))
312 fprintf (stderr, "Not enough memory\n");
316 dbus_message_iter_init_append (message, &iter);
325 DBusMessageIter *target_iter;
326 DBusMessageIter container_iter;
328 type = DBUS_TYPE_INVALID;
330 c = strchr (arg, ':');
334 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
340 container_type = DBUS_TYPE_INVALID;
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;
349 if (container_type != DBUS_TYPE_INVALID)
352 c = strchr (arg, ':');
355 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
362 type = DBUS_TYPE_STRING;
364 type = type_from_name (arg);
366 if (container_type == DBUS_TYPE_DICT_ENTRY)
372 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
376 secondary_type = type_from_name (arg);
378 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
380 sig[2] = secondary_type;
381 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
383 dbus_message_iter_open_container (&iter,
387 target_iter = &container_iter;
389 else if (container_type != DBUS_TYPE_INVALID)
394 dbus_message_iter_open_container (&iter,
398 target_iter = &container_iter;
403 if (container_type == DBUS_TYPE_ARRAY)
405 append_array (target_iter, type, c);
407 else if (container_type == DBUS_TYPE_DICT_ENTRY)
409 append_dict (target_iter, type, secondary_type, c);
412 append_arg (target_iter, type, c);
414 if (container_type != DBUS_TYPE_INVALID)
416 dbus_message_iter_close_container (&iter,
425 dbus_error_init (&error);
426 reply = dbus_connection_send_with_reply_and_block (connection,
427 message, reply_timeout,
429 if (dbus_error_is_set (&error))
431 fprintf (stderr, "Error: %s\n",
438 print_message (reply);
439 dbus_message_unref (reply);
444 dbus_connection_send (connection, message, NULL);
445 dbus_connection_flush (connection);
448 dbus_message_unref (message);
450 dbus_connection_close (connection);