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)
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;
186 int print_reply_literal;
188 DBusMessageIter iter;
190 DBusBusType type = DBUS_BUS_SESSION;
191 const char *dest = NULL;
192 const char *name = NULL;
193 const char *path = NULL;
194 int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
195 const char *type_str = NULL;
203 print_reply_literal = FALSE;
206 for (i = 1; i < argc && name == NULL; i++)
210 if (strcmp (arg, "--system") == 0)
211 type = DBUS_BUS_SYSTEM;
212 else if (strcmp (arg, "--session") == 0)
213 type = DBUS_BUS_SESSION;
214 else if (strncmp (arg, "--print-reply", 13) == 0)
217 message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
218 if (*(arg + 13) != '\0')
219 print_reply_literal = TRUE;
221 else if (strstr (arg, "--reply-timeout=") == arg)
223 reply_timeout = strtol (strchr (arg, '=') + 1,
226 else if (strstr (arg, "--dest=") == arg)
227 dest = strchr (arg, '=') + 1;
228 else if (strstr (arg, "--type=") == arg)
229 type_str = strchr (arg, '=') + 1;
230 else if (!strcmp(arg, "--help"))
232 else if (arg[0] == '-')
234 else if (path == NULL)
236 else if (name == NULL)
245 if (type_str != NULL)
247 message_type = dbus_message_type_from_string (type_str);
248 if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
249 message_type == DBUS_MESSAGE_TYPE_SIGNAL))
251 fprintf (stderr, "Message type \"%s\" is not supported\n",
257 dbus_error_init (&error);
258 connection = dbus_bus_get (type, &error);
259 if (connection == NULL)
261 fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
262 (type == DBUS_BUS_SYSTEM) ? "system" : "session",
264 dbus_error_free (&error);
268 if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
272 last_dot = strrchr (name, '.');
273 if (last_dot == NULL)
275 fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
281 message = dbus_message_new_method_call (NULL,
285 dbus_message_set_auto_start (message, TRUE);
287 else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
291 last_dot = strrchr (name, '.');
292 if (last_dot == NULL)
294 fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
300 message = dbus_message_new_signal (path, name, last_dot + 1);
304 fprintf (stderr, "Internal error, unknown message type\n");
310 fprintf (stderr, "Couldn't allocate D-BUS message\n");
314 if (dest && !dbus_message_set_destination (message, dest))
316 fprintf (stderr, "Not enough memory\n");
320 dbus_message_iter_init_append (message, &iter);
329 DBusMessageIter *target_iter;
330 DBusMessageIter container_iter;
332 type = DBUS_TYPE_INVALID;
334 c = strchr (arg, ':');
338 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
344 container_type = DBUS_TYPE_INVALID;
346 if (strcmp (arg, "variant") == 0)
347 container_type = DBUS_TYPE_VARIANT;
348 else if (strcmp (arg, "array") == 0)
349 container_type = DBUS_TYPE_ARRAY;
350 else if (strcmp (arg, "dict") == 0)
351 container_type = DBUS_TYPE_DICT_ENTRY;
353 if (container_type != DBUS_TYPE_INVALID)
356 c = strchr (arg, ':');
359 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
366 type = DBUS_TYPE_STRING;
368 type = type_from_name (arg);
370 if (container_type == DBUS_TYPE_DICT_ENTRY)
376 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
380 secondary_type = type_from_name (arg);
382 sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
384 sig[2] = secondary_type;
385 sig[3] = DBUS_DICT_ENTRY_END_CHAR;
387 dbus_message_iter_open_container (&iter,
391 target_iter = &container_iter;
393 else if (container_type != DBUS_TYPE_INVALID)
398 dbus_message_iter_open_container (&iter,
402 target_iter = &container_iter;
407 if (container_type == DBUS_TYPE_ARRAY)
409 append_array (target_iter, type, c);
411 else if (container_type == DBUS_TYPE_DICT_ENTRY)
413 append_dict (target_iter, type, secondary_type, c);
416 append_arg (target_iter, type, c);
418 if (container_type != DBUS_TYPE_INVALID)
420 dbus_message_iter_close_container (&iter,
429 dbus_error_init (&error);
430 reply = dbus_connection_send_with_reply_and_block (connection,
431 message, reply_timeout,
433 if (dbus_error_is_set (&error))
435 fprintf (stderr, "Error %s: %s\n",
443 print_message (reply, print_reply_literal);
444 dbus_message_unref (reply);
449 dbus_connection_send (connection, message, NULL);
450 dbus_connection_flush (connection);
453 dbus_message_unref (message);
455 dbus_connection_close (connection);