dbus/tools/dbus-send.c
Colin Walters 2ca6e95764 2005-07-06 Colin Walters <walters@verbum.org>
* dbus/dbus-glib.h (DBusGPendingCall, DBusGPendingCallNotify)
	(DBUS_TYPE_G_PENDING_CALL, dbus_g_pending_call_get_g_type)
	(dbus_g_pending_call_ref, dbus_g_pending_call_unref): Delete.
	(dbus_g_pending_call_set_notify, dbus_g_pending_call_cancel):
	Delete in favor of dbus_g_proxy_begin_call and
	dbus_g_proxy_cancel_call.
	(DBusGProxyCall, DBusGProxyCallNotify): New.
	(dbus_g_proxy_begin_call): Change prototype to take callback, user
	data, and destroy function.  This replaces
	dbus_g_pending_call_set_notify.
	(dbus_g_proxy_cancel_call): Prototype.
	(DBusGAsyncData): Delete, shouldn't be needed anymore.

	* glib/dbus-gproxy.c (struct _DBusGProxy): Add call_id_counter and
	pending_calls map.
	(struct _DBusGProxyManager): Add bus_proxy member, which is an
	internal proxy for calls to the bus. Remove
	pending_nameowner_calls, now the internal proxy keeps track.
	(dbus_g_proxy_manager_unref): Unref bus proxy, remove reference to
	pending_nameowner_calls.
	(got_name_owner_cb): Update prototype, and use
	dbus_g_proxy_end_call.
	(got_name_owner_cb): Remove reference to pending_nameowner_calls.
	(dbus_g_proxy_manager_register): Delete directly libdbus code in
	favor of using internal proxy.
	(dbus_g_proxy_manager_unregister): Update to use
	dbus_g_proxy_cancel_call for any pending GetNameOwner call.
	(dbus_g_proxy_init): Initialize pending calls map.
	(dbus_g_proxy_constructor): New.
	(dbus_g_proxy_class_init): Add get/set property functions,
	constructor, and add NAME, PATH, and INTERFACE properties.
	(cancel_pending_call): New function.
	(dbus_g_proxy_dispose): Iterate over any outstanding calls and
	cancel them.
	(dbus_g_proxy_set_property, dbus_g_proxy_get_property): New.
	(GPendingNotifyClosure): New structure.
	(d_pending_call_notify, d_pending_call_free): Moved here from
	dbus-glib.c.
	(DBUS_G_VALUE_ARRAY_COLLECT_ALL): Moved around to satisfy function
	ordering.
	(manager_begin_bus_call): New internal function for talking to
	internal bus proxy.
	(dbus_g_proxy_new): Construct object using GObjet properties.
	(dbus_g_proxy_begin_call_internal): Update to take user data, etc.
	Create closure of same, and insert call into map of pending calls.
	(dbus_g_proxy_end_call_internal): Take call id instead of pending
	call.  Look up pending call in current set.  Remove it when we've
	completed.
	(dbus_g_pending_call_end, dbus_g_proxy_end_call_internal): Delete.
	(dbus_g_proxy_begin_call): Change API to take callback, user data,
	and destroy function directly.
	(dbus_g_proxy_end_call): Update to take DBusGProxyCall.
	(dbus_g_proxy_call): Invoke with NULL callback.
	(dbus_g_proxy_cancel_call): New function, replaces
	dbus_g_pending_call_cancel.

	* glib/dbus-gparser.c (validate_signature): Fix call to
	dbus_set_g_error.

	* glib/dbus-gobject.c (dbus_g_object_type_dbus_metadata_quark):
	New quark for attaching metadata to GType.
	(info_hash): Delete.
	(lookup_object_info): Look up using quark.
	(dbus_g_object_type_install_info): Check that a type is classed,
	not that it's an object.  Also just install type data using quark
	instead of using global hash.

	* glib/dbus-glib.c (dbus_g_pending_call_ref)
	(dbus_g_pending_call_unref, dbus_pending_call_get_g_type)
	(GPendingNotifyClosure): Delete.
	(d_pending_call_notify, d_pending_call_free): Move to dbus-gproxy.c.
	(dbus_g_pending_call_set_notify, dbus_g_pending_call_cancel): Delete.

	* glib/dbus-binding-tool-glib.c (generate_client_glue): Disable async
	client method generation until we can fix it...

	* tools/dbus-viewer.c (load_child_nodes): Use dbus_g_proxy_call.
	(load_from_service_thread_func): Ditto.

	* tools/dbus-names-model.c (struct NamesModel): Hold
	DBusGProxyCall.
	(have_names_notify): Update prototype, use
	dbus_g_proxy_cancel_call.
	(names_model_reload): Update for new dbus_g_proxy_begin_call API.

	* tools/dbus-monitor.c (filter_func): Update for print_message
	API change.

	* test/glib/test-dbus-glib.c: Add more tests for async
	invocations.  Update many begin_call/end_call pairs to just use
	dbus_g_proxy_call.

	* tools/dbus-send.c (main): Add --print-reply=literal mode.  This
	allows us to dump print-introspect.c.

	* tools/dbus-print-message.h (print_message): Add literal argument
	to print_message which is intended to allow printing arguments without
	metadata like "string=".

	* tools/dbus-print-message.c (print_iter): Add literal argument.
	(print_message): Allow printing string messages literally.
2005-07-06 21:27:53 +00:00

458 lines
11 KiB
C

/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-send.c Utility program to send messages from the command line
*
* Copyright (C) 2003 Philip Blundell <philb@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus.h>
#include "dbus-print-message.h"
static const char *appname;
static void
usage (int ecode)
{
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);
exit (ecode);
}
static void
append_arg (DBusMessageIter *iter, int type, const char *value)
{
dbus_uint32_t uint32;
dbus_int32_t int32;
double d;
unsigned char byte;
dbus_bool_t v_BOOLEAN;
/* FIXME - we are ignoring OOM returns on all these functions */
switch (type)
{
case DBUS_TYPE_BYTE:
byte = strtoul (value, NULL, 0);
dbus_message_iter_append_basic (iter, DBUS_TYPE_BYTE, &byte);
break;
case DBUS_TYPE_DOUBLE:
d = strtod (value, NULL);
dbus_message_iter_append_basic (iter, DBUS_TYPE_DOUBLE, &d);
break;
case DBUS_TYPE_INT32:
int32 = strtol (value, NULL, 0);
dbus_message_iter_append_basic (iter, DBUS_TYPE_INT32, &int32);
break;
case DBUS_TYPE_UINT32:
uint32 = strtoul (value, NULL, 0);
dbus_message_iter_append_basic (iter, DBUS_TYPE_UINT32, &uint32);
break;
case DBUS_TYPE_STRING:
dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &value);
break;
case DBUS_TYPE_OBJECT_PATH:
dbus_message_iter_append_basic (iter, DBUS_TYPE_OBJECT_PATH, &value);
break;
case DBUS_TYPE_BOOLEAN:
if (strcmp (value, "true") == 0)
{
v_BOOLEAN = TRUE;
dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
}
else if (strcmp (value, "false") == 0)
{
v_BOOLEAN = FALSE;
dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN);
}
else
{
fprintf (stderr, "%s: Expected \"true\" or \"false\" instead of \"%s\"\n", appname, value);
exit (1);
}
break;
default:
fprintf (stderr, "%s: Unsupported data type %c\n", appname, (char) type);
exit (1);
}
}
static void
append_array (DBusMessageIter *iter, int type, const char *value)
{
const char *val;
char *dupval = strdup (value);
val = strtok (dupval, ",");
while (val != NULL)
{
append_arg (iter, type, val);
val = strtok (NULL, ",");
}
free (dupval);
}
static void
append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value)
{
const char *val;
char *dupval = strdup (value);
val = strtok (dupval, ",");
while (val != NULL)
{
DBusMessageIter subiter;
char sig[3];
sig[0] = keytype;
sig[1] = valtype;
sig[2] = '\0';
dbus_message_iter_open_container (iter,
DBUS_TYPE_DICT_ENTRY,
sig,
&subiter);
append_arg (&subiter, keytype, val);
val = strtok (NULL, ",");
if (val == NULL)
{
fprintf (stderr, "%s: Malformed dictionary\n", appname);
exit (1);
}
append_arg (&subiter, valtype, val);
dbus_message_iter_close_container (iter, &subiter);
val = strtok (NULL, ",");
}
free (dupval);
}
static int
type_from_name (const char *arg)
{
int type;
if (!strcmp (arg, "string"))
type = DBUS_TYPE_STRING;
else if (!strcmp (arg, "int32"))
type = DBUS_TYPE_INT32;
else if (!strcmp (arg, "uint32"))
type = DBUS_TYPE_UINT32;
else if (!strcmp (arg, "double"))
type = DBUS_TYPE_DOUBLE;
else if (!strcmp (arg, "byte"))
type = DBUS_TYPE_BYTE;
else if (!strcmp (arg, "boolean"))
type = DBUS_TYPE_BOOLEAN;
else if (!strcmp (arg, "objpath"))
type = DBUS_TYPE_OBJECT_PATH;
else
{
fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg);
exit (1);
}
return type;
}
int
main (int argc, char *argv[])
{
DBusConnection *connection;
DBusError error;
DBusMessage *message;
int print_reply;
int print_reply_literal;
int reply_timeout;
DBusMessageIter iter;
int i;
DBusBusType type = DBUS_BUS_SESSION;
const char *dest = NULL;
const char *name = NULL;
const char *path = NULL;
int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
const char *type_str = NULL;
appname = argv[0];
if (argc < 3)
usage (1);
print_reply = FALSE;
print_reply_literal = FALSE;
reply_timeout = -1;
for (i = 1; i < argc && name == NULL; i++)
{
char *arg = argv[i];
if (strcmp (arg, "--system") == 0)
type = DBUS_BUS_SYSTEM;
else if (strcmp (arg, "--session") == 0)
type = DBUS_BUS_SESSION;
else if (strncmp (arg, "--print-reply", 13) == 0)
{
print_reply = TRUE;
message_type = DBUS_MESSAGE_TYPE_METHOD_CALL;
if (*(arg + 13) != '\0')
print_reply_literal = TRUE;
}
else if (strstr (arg, "--reply-timeout=") == arg)
{
reply_timeout = strtol (strchr (arg, '=') + 1,
NULL, 10);
}
else if (strstr (arg, "--dest=") == arg)
dest = strchr (arg, '=') + 1;
else if (strstr (arg, "--type=") == arg)
type_str = strchr (arg, '=') + 1;
else if (!strcmp(arg, "--help"))
usage (0);
else if (arg[0] == '-')
usage (1);
else if (path == NULL)
path = arg;
else if (name == NULL)
name = arg;
else
usage (1);
}
if (name == NULL)
usage (1);
if (type_str != NULL)
{
message_type = dbus_message_type_from_string (type_str);
if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
message_type == DBUS_MESSAGE_TYPE_SIGNAL))
{
fprintf (stderr, "Message type \"%s\" is not supported\n",
type_str);
exit (1);
}
}
dbus_error_init (&error);
connection = dbus_bus_get (type, &error);
if (connection == NULL)
{
fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
(type == DBUS_BUS_SYSTEM) ? "system" : "session",
error.message);
dbus_error_free (&error);
exit (1);
}
if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
{
char *last_dot;
last_dot = strrchr (name, '.');
if (last_dot == NULL)
{
fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
name);
exit (1);
}
*last_dot = '\0';
message = dbus_message_new_method_call (NULL,
path,
name,
last_dot + 1);
dbus_message_set_auto_start (message, TRUE);
}
else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
{
char *last_dot;
last_dot = strrchr (name, '.');
if (last_dot == NULL)
{
fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
name);
exit (1);
}
*last_dot = '\0';
message = dbus_message_new_signal (path, name, last_dot + 1);
}
else
{
fprintf (stderr, "Internal error, unknown message type\n");
exit (1);
}
if (message == NULL)
{
fprintf (stderr, "Couldn't allocate D-BUS message\n");
exit (1);
}
if (dest && !dbus_message_set_destination (message, dest))
{
fprintf (stderr, "Not enough memory\n");
exit (1);
}
dbus_message_iter_init_append (message, &iter);
while (i < argc)
{
char *arg;
char *c;
int type;
int secondary_type;
int container_type;
DBusMessageIter *target_iter;
DBusMessageIter container_iter;
type = DBUS_TYPE_INVALID;
arg = argv[i++];
c = strchr (arg, ':');
if (c == NULL)
{
fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
exit (1);
}
*(c++) = 0;
container_type = DBUS_TYPE_INVALID;
if (strcmp (arg, "variant") == 0)
container_type = DBUS_TYPE_VARIANT;
else if (strcmp (arg, "array") == 0)
container_type = DBUS_TYPE_ARRAY;
else if (strcmp (arg, "dict") == 0)
container_type = DBUS_TYPE_DICT_ENTRY;
if (container_type != DBUS_TYPE_INVALID)
{
arg = c;
c = strchr (arg, ':');
if (c == NULL)
{
fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
exit (1);
}
*(c++) = 0;
}
if (arg[0] == 0)
type = DBUS_TYPE_STRING;
else
type = type_from_name (arg);
if (container_type == DBUS_TYPE_DICT_ENTRY)
{
arg = c;
c = strchr (c, ':');
if (c == NULL)
{
fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
exit (1);
}
*(c++) = 0;
secondary_type = type_from_name (arg);
char sig[5];
sig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
sig[1] = type;
sig[2] = secondary_type;
sig[3] = DBUS_DICT_ENTRY_END_CHAR;
sig[4] = '\0';
dbus_message_iter_open_container (&iter,
DBUS_TYPE_ARRAY,
sig,
&container_iter);
target_iter = &container_iter;
}
else if (container_type != DBUS_TYPE_INVALID)
{
char sig[2];
sig[0] = type;
sig[1] = '\0';
dbus_message_iter_open_container (&iter,
container_type,
sig,
&container_iter);
target_iter = &container_iter;
}
else
target_iter = &iter;
if (container_type == DBUS_TYPE_ARRAY)
{
append_array (target_iter, type, c);
}
else if (container_type == DBUS_TYPE_DICT_ENTRY)
{
append_dict (target_iter, type, secondary_type, c);
}
else
append_arg (target_iter, type, c);
if (container_type != DBUS_TYPE_INVALID)
{
dbus_message_iter_close_container (&iter,
&container_iter);
}
}
if (print_reply)
{
DBusMessage *reply;
dbus_error_init (&error);
reply = dbus_connection_send_with_reply_and_block (connection,
message, reply_timeout,
&error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "Error %s: %s\n",
error.name,
error.message);
exit (1);
}
if (reply)
{
print_message (reply, print_reply_literal);
dbus_message_unref (reply);
}
}
else
{
dbus_connection_send (connection, message, NULL);
dbus_connection_flush (connection);
}
dbus_message_unref (message);
dbus_connection_close (connection);
exit (0);
}