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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus.h>
31 #define strdup _strdup
35 #include "dbus-print-message.h"
37 static const char *appname;
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);
47 append_arg (DBusMessageIter *iter, int type, const char *value)
57 dbus_bool_t v_BOOLEAN;
59 /* FIXME - we are ignoring OOM returns on all these functions */
63 byte = strtoul (value, NULL, 0);
64 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
67 case DBUS_TYPE_DOUBLE:
68 d = strtod (value, NULL);
69 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
73 int16 = strtol (value, NULL, 0);
74 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
77 case DBUS_TYPE_UINT16:
78 uint16 = strtoul (value, NULL, 0);
79 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
83 int32 = strtol (value, NULL, 0);
84 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
87 case DBUS_TYPE_UINT32:
88 uint32 = strtoul (value, NULL, 0);
89 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
93 int64 = strtoll (value, NULL, 0);
94 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
97 case DBUS_TYPE_UINT64:
98 uint64 = strtoull (value, NULL, 0);
99 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
102 case DBUS_TYPE_STRING:
103 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
106 case DBUS_TYPE_OBJECT_PATH:
107 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
110 case DBUS_TYPE_BOOLEAN:
111 if (strcmp (value, "true") == 0)
114 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
116 else if (strcmp (value, "false") == 0)
119 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
123 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
129 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
135 append_array (DBusMessageIter *iter, int type, const char *value)
138 char *dupval = strdup (value);
140 val = strtok (dupval, ",");
143 append_arg (iter, type, val);
144 val = strtok (NULL, ",");
150 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
153 char *dupval = strdup (value);
155 val = strtok (dupval, ",");
158 DBusMessageIter subiter;
160 dbus_message_iter_open_container (iter,
161 DBUS_TYPE_DICT_ENTRY,
165 append_arg (&subiter, keytype, val);
166 val = strtok (NULL, ",");
169 fprintf (stderr, "%s: Malformed dictionary\n", appname);
172 append_arg (&subiter, valtype, val);
174 dbus_message_iter_close_container (iter, &subiter);
175 val = strtok (NULL, ",");
181 type_from_name (const char *arg)
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;
208 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
215 main (int argc, char *argv[])
217 DBusConnection *connection;
219 DBusMessage *message;
221 int print_reply_literal;
223 DBusMessageIter iter;
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;
240 print_reply_literal = FALSE;
243 for (i = 1; i < argc && name == NULL; i++)
247 if (strcmp (arg, "--system") == 0)
249 type = DBUS_BUS_SYSTEM;
250 session_or_system = TRUE;
252 else if (strcmp (arg, "--session") == 0)
254 type = DBUS_BUS_SESSION;
255 session_or_system = TRUE;
257 else if (strstr (arg, "--address") == arg)
259 address = strchr (arg, '=');
263 fprintf (stderr, "\"--address=\" requires an ADDRESS\n");
268 address = address + 1;
271 else if (strncmp (arg, "--print-reply", 13) == 0)
274 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
275 if (*(arg + 13) != '\0')
276 print_reply_literal = TRUE;
278 else if (strstr (arg, "--reply-timeout=") == arg)
280 reply_timeout = strtol (strchr (arg, '=') + 1,
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"))
289 else if (arg[0] == '-')
291 else if (path == NULL)
293 else if (name == NULL)
302 if (session_or_system &&
305 fprintf (stderr, "\"--address\" may not be used with \"--system\" or \"--session\"\n");
309 if (type_str != NULL)
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))
315 fprintf (stderr, "Message type \"%s\" is not supported\n",
321 dbus_error_init (&error);
325 connection = dbus_connection_open (address, &error);
329 connection = dbus_bus_get (type, &error);
332 if (connection == NULL)
334 fprintf (stderr, "Failed to open connection to \"%s\" message bus: %s\n",
335 (address != NULL) ? address :
336 ((type == DBUS_BUS_SYSTEM) ? "system" : "session"),
338 dbus_error_free (&error);
342 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
346 last_dot = strrchr (name, '.');
347 if (last_dot == NULL)
349 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
355 message = dbus_message_new_method_call (NULL,
359 dbus_message_set_auto_start (message, TRUE);
361 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
365 last_dot = strrchr (name, '.');
366 if (last_dot == NULL)
368 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
374 message = dbus_message_new_signal (path, name, last_dot + 1);
378 fprintf (stderr, "Internal error, unknown message type\n");
384 fprintf (stderr, "Couldn't allocate D-Bus message\n");
388 if (dest && !dbus_message_set_destination (message, dest))
390 fprintf (stderr, "Not enough memory\n");
394 dbus_message_iter_init_append (message, &iter);
403 DBusMessageIter *target_iter;
404 DBusMessageIter container_iter;
406 type = DBUS_TYPE_INVALID;
408 c = strchr (arg, ':');
412 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
418 container_type = DBUS_TYPE_INVALID;
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;
427 if (container_type != DBUS_TYPE_INVALID)
430 c = strchr (arg, ':');
433 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
440 type = DBUS_TYPE_STRING;
442 type = type_from_name (arg);
444 if (container_type == DBUS_TYPE_DICT_ENTRY)
451 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
455 secondary_type = type_from_name (arg);
456 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
458 sig[2] = secondary_type;
459 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
461 dbus_message_iter_open_container (&iter,
465 target_iter = &container_iter;
467 else if (container_type != DBUS_TYPE_INVALID)
472 dbus_message_iter_open_container (&iter,
476 target_iter = &container_iter;
481 if (container_type == DBUS_TYPE_ARRAY)
483 append_array (target_iter, type, c);
485 else if (container_type == DBUS_TYPE_DICT_ENTRY)
487 append_dict (target_iter, type, secondary_type, c);
490 append_arg (target_iter, type, c);
492 if (container_type != DBUS_TYPE_INVALID)
494 dbus_message_iter_close_container (&iter,
503 dbus_error_init (&error);
504 reply = dbus_connection_send_with_reply_and_block (connection,
505 message, reply_timeout,
507 if (dbus_error_is_set (&error))
509 fprintf (stderr, "Error %s: %s\n",
517 print_message (reply, print_reply_literal);
518 dbus_message_unref (reply);
523 dbus_connection_send (connection, message, NULL);
524 dbus_connection_flush (connection);
527 dbus_message_unref (message);
529 dbus_connection_unref (connection);