1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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
27 #include <dbus/dbus.h>
29 #include "dbus-print-message.h"
31 static const char *appname;
36 fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply=(literal)] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
41 append_arg (DBusMessageIter *iter, int type, const char *value)
51 dbus_bool_t v_BOOLEAN;
53 /* FIXME - we are ignoring OOM returns on all these functions */
57 byte = strtoul (value, NULL, 0);
58 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
61 case DBUS_TYPE_DOUBLE:
62 d = strtod (value, NULL);
63 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
67 int16 = strtol (value, NULL, 0);
68 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
71 case DBUS_TYPE_UINT16:
72 uint16 = strtoul (value, NULL, 0);
73 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
77 int32 = strtol (value, NULL, 0);
78 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
81 case DBUS_TYPE_UINT32:
82 uint32 = strtoul (value, NULL, 0);
83 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
87 int64 = strtoll (value, NULL, 0);
88 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
91 case DBUS_TYPE_UINT64:
92 uint64 = strtoull (value, NULL, 0);
93 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
96 case DBUS_TYPE_STRING:
97 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
100 case DBUS_TYPE_OBJECT_PATH:
101 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
104 case DBUS_TYPE_BOOLEAN:
105 if (strcmp (value, "true") == 0)
108 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
110 else if (strcmp (value, "false") == 0)
113 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
117 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
123 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
129 append_array (DBusMessageIter *iter, int type, const char *value)
132 char *dupval = strdup (value);
134 val = strtok (dupval, ",");
137 append_arg (iter, type, val);
138 val = strtok (NULL, ",");
144 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
147 char *dupval = strdup (value);
149 val = strtok (dupval, ",");
152 DBusMessageIter subiter;
154 dbus_message_iter_open_container (iter,
155 DBUS_TYPE_DICT_ENTRY,
159 append_arg (&subiter, keytype, val);
160 val = strtok (NULL, ",");
163 fprintf (stderr, "%s: Malformed dictionary\n", appname);
166 append_arg (&subiter, valtype, val);
168 dbus_message_iter_close_container (iter, &subiter);
169 val = strtok (NULL, ",");
175 type_from_name (const char *arg)
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;
202 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
209 main (int argc, char *argv[])
211 DBusConnection *connection;
213 DBusMessage *message;
215 int print_reply_literal;
217 DBusMessageIter iter;
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;
232 print_reply_literal = FALSE;
235 for (i = 1; i < argc && name == NULL; i++)
239 if (strcmp (arg, "--system") == 0)
240 type = DBUS_BUS_SYSTEM;
241 else if (strcmp (arg, "--session") == 0)
242 type = DBUS_BUS_SESSION;
243 else if (strncmp (arg, "--print-reply", 13) == 0)
246 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
247 if (*(arg + 13) != '\0')
248 print_reply_literal = TRUE;
250 else if (strstr (arg, "--reply-timeout=") == arg)
252 reply_timeout = strtol (strchr (arg, '=') + 1,
255 else if (strstr (arg, "--dest=") == arg)
256 dest = strchr (arg, '=') + 1;
257 else if (strstr (arg, "--type=") == arg)
258 type_str = strchr (arg, '=') + 1;
259 else if (!strcmp(arg, "--help"))
261 else if (arg[0] == '-')
263 else if (path == NULL)
265 else if (name == NULL)
274 if (type_str != NULL)
276 message_type = dbus_message_type_from_string (type_str);
277 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
278 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
280 fprintf (stderr, "Message type \"%s\" is not supported\n",
286 dbus_error_init (&error);
287 connection = dbus_bus_get (type, &error);
288 if (connection == NULL)
290 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
291 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
293 dbus_error_free (&error);
297 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
301 last_dot = strrchr (name, '.');
302 if (last_dot == NULL)
304 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
310 message = dbus_message_new_method_call (NULL,
314 dbus_message_set_auto_start (message, TRUE);
316 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
320 last_dot = strrchr (name, '.');
321 if (last_dot == NULL)
323 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
329 message = dbus_message_new_signal (path, name, last_dot + 1);
333 fprintf (stderr, "Internal error, unknown message type\n");
339 fprintf (stderr, "Couldn't allocate D-Bus message\n");
343 if (dest && !dbus_message_set_destination (message, dest))
345 fprintf (stderr, "Not enough memory\n");
349 dbus_message_iter_init_append (message, &iter);
358 DBusMessageIter *target_iter;
359 DBusMessageIter container_iter;
361 type = DBUS_TYPE_INVALID;
363 c = strchr (arg, ':');
367 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
373 container_type = DBUS_TYPE_INVALID;
375 if (strcmp (arg, "variant") == 0)
376 container_type = DBUS_TYPE_VARIANT;
377 else if (strcmp (arg, "array") == 0)
378 container_type = DBUS_TYPE_ARRAY;
379 else if (strcmp (arg, "dict") == 0)
380 container_type = DBUS_TYPE_DICT_ENTRY;
382 if (container_type != DBUS_TYPE_INVALID)
385 c = strchr (arg, ':');
388 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
395 type = DBUS_TYPE_STRING;
397 type = type_from_name (arg);
399 if (container_type == DBUS_TYPE_DICT_ENTRY)
406 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
410 secondary_type = type_from_name (arg);
411 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
413 sig[2] = secondary_type;
414 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
416 dbus_message_iter_open_container (&iter,
420 target_iter = &container_iter;
422 else if (container_type != DBUS_TYPE_INVALID)
427 dbus_message_iter_open_container (&iter,
431 target_iter = &container_iter;
436 if (container_type == DBUS_TYPE_ARRAY)
438 append_array (target_iter, type, c);
440 else if (container_type == DBUS_TYPE_DICT_ENTRY)
442 append_dict (target_iter, type, secondary_type, c);
445 append_arg (target_iter, type, c);
447 if (container_type != DBUS_TYPE_INVALID)
449 dbus_message_iter_close_container (&iter,
458 dbus_error_init (&error);
459 reply = dbus_connection_send_with_reply_and_block (connection,
460 message, reply_timeout,
462 if (dbus_error_is_set (&error))
464 fprintf (stderr, "Error %s: %s\n",
472 print_message (reply, print_reply_literal);
473 dbus_message_unref (reply);
478 dbus_connection_send (connection, message, NULL);
479 dbus_connection_flush (connection);
482 dbus_message_unref (message);
484 dbus_connection_unref (connection);