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=(literal)] [--reply-timeout=MSEC] <destination object path> <message name> [contents ...]\n", appname);
40 append_arg (DBusMessageIter *iter, int type, const char *value)
50 dbus_bool_t v_BOOLEAN;
52 /* FIXME - we are ignoring OOM returns on all these functions */
56 byte = strtoul (value, NULL, 0);
57 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
60 case DBUS_TYPE_DOUBLE:
61 d = strtod (value, NULL);
62 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
66 int16 = strtol (value, NULL, 0);
67 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
70 case DBUS_TYPE_UINT16:
71 uint16 = strtoul (value, NULL, 0);
72 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
76 int32 = strtol (value, NULL, 0);
77 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
80 case DBUS_TYPE_UINT32:
81 uint32 = strtoul (value, NULL, 0);
82 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
86 int64 = strtoll (value, NULL, 0);
87 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
90 case DBUS_TYPE_UINT64:
91 uint64 = strtoull (value, NULL, 0);
92 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
95 case DBUS_TYPE_STRING:
96 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
99 case DBUS_TYPE_OBJECT_PATH:
100 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
103 case DBUS_TYPE_BOOLEAN:
104 if (strcmp (value, "true") == 0)
107 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
109 else if (strcmp (value, "false") == 0)
112 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
116 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
122 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
128 append_array (DBusMessageIter *iter, int type, const char *value)
131 char *dupval = strdup (value);
133 val = strtok (dupval, ",");
136 append_arg (iter, type, val);
137 val = strtok (NULL, ",");
143 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
146 char *dupval = strdup (value);
148 val = strtok (dupval, ",");
151 DBusMessageIter subiter;
157 dbus_message_iter_open_container (iter,
158 DBUS_TYPE_DICT_ENTRY,
162 append_arg (&subiter, keytype, val);
163 val = strtok (NULL, ",");
166 fprintf (stderr, "%s: Malformed dictionary\n", appname);
169 append_arg (&subiter, valtype, val);
171 dbus_message_iter_close_container (iter, &subiter);
172 val = strtok (NULL, ",");
178 type_from_name (const char *arg)
181 if (!strcmp (arg, "string"))
182 type = DBUS_TYPE_STRING;
183 else if (!strcmp (arg, "int16"))
184 type = DBUS_TYPE_INT16;
185 else if (!strcmp (arg, "uint16"))
186 type = DBUS_TYPE_UINT16;
187 else if (!strcmp (arg, "int32"))
188 type = DBUS_TYPE_INT32;
189 else if (!strcmp (arg, "uint32"))
190 type = DBUS_TYPE_UINT32;
191 else if (!strcmp (arg, "int64"))
192 type = DBUS_TYPE_INT64;
193 else if (!strcmp (arg, "uint64"))
194 type = DBUS_TYPE_UINT64;
195 else if (!strcmp (arg, "double"))
196 type = DBUS_TYPE_DOUBLE;
197 else if (!strcmp (arg, "byte"))
198 type = DBUS_TYPE_BYTE;
199 else if (!strcmp (arg, "boolean"))
200 type = DBUS_TYPE_BOOLEAN;
201 else if (!strcmp (arg, "objpath"))
202 type = DBUS_TYPE_OBJECT_PATH;
205 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
212 main (int argc, char *argv[])
214 DBusConnection *connection;
216 DBusMessage *message;
218 int print_reply_literal;
220 DBusMessageIter iter;
222 DBusBusType type = DBUS_BUS_SESSION;
223 const char *dest = NULL;
224 const char *name = NULL;
225 const char *path = NULL;
226 int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
227 const char *type_str = NULL;
235 print_reply_literal = FALSE;
238 for (i = 1; i < argc && name == NULL; i++)
242 if (strcmp (arg, "--system") == 0)
243 type = DBUS_BUS_SYSTEM;
244 else if (strcmp (arg, "--session") == 0)
245 type = DBUS_BUS_SESSION;
246 else if (strncmp (arg, "--print-reply", 13) == 0)
249 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
250 if (*(arg + 13) != '\0')
251 print_reply_literal = TRUE;
253 else if (strstr (arg, "--reply-timeout=") == arg)
255 reply_timeout = strtol (strchr (arg, '=') + 1,
258 else if (strstr (arg, "--dest=") == arg)
259 dest = strchr (arg, '=') + 1;
260 else if (strstr (arg, "--type=") == arg)
261 type_str = strchr (arg, '=') + 1;
262 else if (!strcmp(arg, "--help"))
264 else if (arg[0] == '-')
266 else if (path == NULL)
268 else if (name == NULL)
277 if (type_str != NULL)
279 message_type = dbus_message_type_from_string (type_str);
280 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
281 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
283 fprintf (stderr, "Message type \"%s\" is not supported\n",
289 dbus_error_init (&error);
290 connection = dbus_bus_get (type, &error);
291 if (connection == NULL)
293 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
294 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
296 dbus_error_free (&error);
300 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
304 last_dot = strrchr (name, '.');
305 if (last_dot == NULL)
307 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
313 message = dbus_message_new_method_call (NULL,
317 dbus_message_set_auto_start (message, TRUE);
319 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
323 last_dot = strrchr (name, '.');
324 if (last_dot == NULL)
326 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
332 message = dbus_message_new_signal (path, name, last_dot + 1);
336 fprintf (stderr, "Internal error, unknown message type\n");
342 fprintf (stderr, "Couldn't allocate D-Bus message\n");
346 if (dest && !dbus_message_set_destination (message, dest))
348 fprintf (stderr, "Not enough memory\n");
352 dbus_message_iter_init_append (message, &iter);
361 DBusMessageIter *target_iter;
362 DBusMessageIter container_iter;
364 type = DBUS_TYPE_INVALID;
366 c = strchr (arg, ':');
370 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
376 container_type = DBUS_TYPE_INVALID;
378 if (strcmp (arg, "variant") == 0)
379 container_type = DBUS_TYPE_VARIANT;
380 else if (strcmp (arg, "array") == 0)
381 container_type = DBUS_TYPE_ARRAY;
382 else if (strcmp (arg, "dict") == 0)
383 container_type = DBUS_TYPE_DICT_ENTRY;
385 if (container_type != DBUS_TYPE_INVALID)
388 c = strchr (arg, ':');
391 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
398 type = DBUS_TYPE_STRING;
400 type = type_from_name (arg);
402 if (container_type == DBUS_TYPE_DICT_ENTRY)
409 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
413 secondary_type = type_from_name (arg);
414 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
416 sig[2] = secondary_type;
417 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
419 dbus_message_iter_open_container (&iter,
423 target_iter = &container_iter;
425 else if (container_type != DBUS_TYPE_INVALID)
430 dbus_message_iter_open_container (&iter,
434 target_iter = &container_iter;
439 if (container_type == DBUS_TYPE_ARRAY)
441 append_array (target_iter, type, c);
443 else if (container_type == DBUS_TYPE_DICT_ENTRY)
445 append_dict (target_iter, type, secondary_type, c);
448 append_arg (target_iter, type, c);
450 if (container_type != DBUS_TYPE_INVALID)
452 dbus_message_iter_close_container (&iter,
461 dbus_error_init (&error);
462 reply = dbus_connection_send_with_reply_and_block (connection,
463 message, reply_timeout,
465 if (dbus_error_is_set (&error))
467 fprintf (stderr, "Error %s: %s\n",
475 print_message (reply, print_reply_literal);
476 dbus_message_unref (reply);
481 dbus_connection_send (connection, message, NULL);
482 dbus_connection_flush (connection);
485 dbus_message_unref (message);
487 dbus_connection_unref (connection);