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 strtoll mystrtoll
37 #define strtoull mystrtoull
43 #define strdup _strdup
47 #include "dbus-print-message.h"
49 static const char *appname;
54 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);
59 append_arg (DBusMessageIter *iter, int type, const char *value)
69 dbus_bool_t v_BOOLEAN;
71 /* FIXME - we are ignoring OOM returns on all these functions */
75 byte = strtoul (value, NULL, 0);
76 dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
79 case DBUS_TYPE_DOUBLE:
80 d = strtod (value, NULL);
81 dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
85 int16 = strtol (value, NULL, 0);
86 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT16, &int16);
89 case DBUS_TYPE_UINT16:
90 uint16 = strtoul (value, NULL, 0);
91 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT16, &uint16);
95 int32 = strtol (value, NULL, 0);
96 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
99 case DBUS_TYPE_UINT32:
100 uint32 = strtoul (value, NULL, 0);
101 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
104 case DBUS_TYPE_INT64:
105 int64 = strtoll (value, NULL, 0);
106 dbus_message_iter_append_basic (iter, DBUS_TYPE_INT64, &int64);
109 case DBUS_TYPE_UINT64:
110 uint64 = strtoull (value, NULL, 0);
111 dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT64, &uint64);
114 case DBUS_TYPE_STRING:
115 dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
118 case DBUS_TYPE_OBJECT_PATH:
119 dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
122 case DBUS_TYPE_BOOLEAN:
123 if (strcmp (value, "true") == 0)
126 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
128 else if (strcmp (value, "false") == 0)
131 dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
135 fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
141 fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
147 append_array (DBusMessageIter *iter, int type, const char *value)
150 char *dupval = strdup (value);
152 val = strtok (dupval, ",");
155 append_arg (iter, type, val);
156 val = strtok (NULL, ",");
162 append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
165 char *dupval = strdup (value);
167 val = strtok (dupval, ",");
170 DBusMessageIter subiter;
172 dbus_message_iter_open_container (iter,
173 DBUS_TYPE_DICT_ENTRY,
177 append_arg (&subiter, keytype, val);
178 val = strtok (NULL, ",");
181 fprintf (stderr, "%s: Malformed dictionary\n", appname);
184 append_arg (&subiter, valtype, val);
186 dbus_message_iter_close_container (iter, &subiter);
187 val = strtok (NULL, ",");
193 type_from_name (const char *arg)
196 if (!strcmp (arg, "string"))
197 type = DBUS_TYPE_STRING;
198 else if (!strcmp (arg, "int16"))
199 type = DBUS_TYPE_INT16;
200 else if (!strcmp (arg, "uint16"))
201 type = DBUS_TYPE_UINT16;
202 else if (!strcmp (arg, "int32"))
203 type = DBUS_TYPE_INT32;
204 else if (!strcmp (arg, "uint32"))
205 type = DBUS_TYPE_UINT32;
206 else if (!strcmp (arg, "int64"))
207 type = DBUS_TYPE_INT64;
208 else if (!strcmp (arg, "uint64"))
209 type = DBUS_TYPE_UINT64;
210 else if (!strcmp (arg, "double"))
211 type = DBUS_TYPE_DOUBLE;
212 else if (!strcmp (arg, "byte"))
213 type = DBUS_TYPE_BYTE;
214 else if (!strcmp (arg, "boolean"))
215 type = DBUS_TYPE_BOOLEAN;
216 else if (!strcmp (arg, "objpath"))
217 type = DBUS_TYPE_OBJECT_PATH;
220 fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
227 main (int argc, char *argv[])
229 DBusConnection *connection;
231 DBusMessage *message;
233 int print_reply_literal;
235 DBusMessageIter iter;
237 DBusBusType type = DBUS_BUS_SESSION;
238 const char *dest = NULL;
239 const char *name = NULL;
240 const char *path = NULL;
241 int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
242 const char *type_str = NULL;
243 const char *address = NULL;
244 int session_or_system = FALSE;
252 print_reply_literal = FALSE;
255 for (i = 1; i < argc && name == NULL; i++)
259 if (strcmp (arg, "--system") == 0)
261 type = DBUS_BUS_SYSTEM;
262 session_or_system = TRUE;
264 else if (strcmp (arg, "--session") == 0)
266 type = DBUS_BUS_SESSION;
267 session_or_system = TRUE;
269 else if (strstr (arg, "--address") == arg)
271 address = strchr (arg, '=');
275 fprintf (stderr, "\"--address=\" requires an ADDRESS\n");
280 address = address + 1;
283 else if (strncmp (arg, "--print-reply", 13) == 0)
286 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
287 if (*(arg + 13) != '\0')
288 print_reply_literal = TRUE;
290 else if (strstr (arg, "--reply-timeout=") == arg)
292 reply_timeout = strtol (strchr (arg, '=') + 1,
295 else if (strstr (arg, "--dest=") == arg)
296 dest = strchr (arg, '=') + 1;
297 else if (strstr (arg, "--type=") == arg)
298 type_str = strchr (arg, '=') + 1;
299 else if (!strcmp(arg, "--help"))
301 else if (arg[0] == '-')
303 else if (path == NULL)
305 else /* name == NULL guaranteed by the 'while' loop */
312 if (session_or_system &&
315 fprintf (stderr, "\"--address\" may not be used with \"--system\" or \"--session\"\n");
319 if (type_str != NULL)
321 message_type = dbus_message_type_from_string (type_str);
322 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
323 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
325 fprintf (stderr, "Message type \"%s\" is not supported\n",
331 dbus_error_init (&error);
335 connection = dbus_connection_open (address, &error);
339 connection = dbus_bus_get (type, &error);
342 if (connection == NULL)
344 fprintf (stderr, "Failed to open connection to \"%s\" message bus: %s\n",
345 (address != NULL) ? address :
346 ((type == DBUS_BUS_SYSTEM) ? "system" : "session"),
348 dbus_error_free (&error);
352 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
356 last_dot = strrchr (name, '.');
357 if (last_dot == NULL)
359 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
365 message = dbus_message_new_method_call (NULL,
369 dbus_message_set_auto_start (message, TRUE);
371 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
375 last_dot = strrchr (name, '.');
376 if (last_dot == NULL)
378 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
384 message = dbus_message_new_signal (path, name, last_dot + 1);
388 fprintf (stderr, "Internal error, unknown message type\n");
394 fprintf (stderr, "Couldn't allocate D-Bus message\n");
398 if (dest && !dbus_message_set_destination (message, dest))
400 fprintf (stderr, "Not enough memory\n");
404 dbus_message_iter_init_append (message, &iter);
413 DBusMessageIter *target_iter;
414 DBusMessageIter container_iter;
416 type = DBUS_TYPE_INVALID;
418 c = strchr (arg, ':');
422 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
428 container_type = DBUS_TYPE_INVALID;
430 if (strcmp (arg, "variant") == 0)
431 container_type = DBUS_TYPE_VARIANT;
432 else if (strcmp (arg, "array") == 0)
433 container_type = DBUS_TYPE_ARRAY;
434 else if (strcmp (arg, "dict") == 0)
435 container_type = DBUS_TYPE_DICT_ENTRY;
437 if (container_type != DBUS_TYPE_INVALID)
440 c = strchr (arg, ':');
443 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
450 type = DBUS_TYPE_STRING;
452 type = type_from_name (arg);
454 if (container_type == DBUS_TYPE_DICT_ENTRY)
461 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
465 secondary_type = type_from_name (arg);
466 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
468 sig[2] = secondary_type;
469 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
471 dbus_message_iter_open_container (&iter,
475 target_iter = &container_iter;
477 else if (container_type != DBUS_TYPE_INVALID)
482 dbus_message_iter_open_container (&iter,
486 target_iter = &container_iter;
491 if (container_type == DBUS_TYPE_ARRAY)
493 append_array (target_iter, type, c);
495 else if (container_type == DBUS_TYPE_DICT_ENTRY)
497 append_dict (target_iter, type, secondary_type, c);
500 append_arg (target_iter, type, c);
502 if (container_type != DBUS_TYPE_INVALID)
504 dbus_message_iter_close_container (&iter,
513 dbus_error_init (&error);
514 reply = dbus_connection_send_with_reply_and_block (connection,
515 message, reply_timeout,
517 if (dbus_error_is_set (&error))
519 fprintf (stderr, "Error %s: %s\n",
527 print_message (reply, print_reply_literal);
528 dbus_message_unref (reply);
533 dbus_connection_send (connection, message, NULL);
534 dbus_connection_flush (connection);
537 dbus_message_unref (message);
539 dbus_connection_unref (connection);