--- dbus-send.c.old 2008-04-08 16:09:17.000000000 -0500 +++ dbus-send.c 2008-04-09 08:56:14.000000000 -0500 @@ -33,7 +33,12 @@ static void usage (int ecode) { - fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=NAME] [--type=TYPE] [--print-reply=(literal)] [--reply-timeout=MSEC] [contents ...]\n", appname); + fprintf (stderr, + "Usage: %s [--help] [--system | --session | --private=PEERBUS] \ + [--dest=NAME] [--type=TYPE] [--print-reply[=LITERALLY]] \ + [--reply-timeout=MSEC] \ + [contents ...]\n", + appname); exit (ecode); } @@ -49,10 +54,10 @@ 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); @@ -103,26 +108,29 @@ case DBUS_TYPE_BOOLEAN: if (strcmp (value, "true") == 0) - { - v_BOOLEAN = TRUE; - dbus_message_iter_append_basic (iter, DBUS_TYPE_BOOLEAN, &v_BOOLEAN); - } + { + 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); - } + { + 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); - } + { + 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); + fprintf (stderr, + "%s: Unsupported data type %c\n", appname, (char) type); exit (1); - } + } } static void @@ -132,42 +140,45 @@ char *dupval = strdup (value); val = strtok (dupval, ","); + while (val != NULL) - { - append_arg (iter, type, val); - val = strtok (NULL, ","); - } + { + append_arg (iter, type, val); + val = strtok (NULL, ","); + } + free (dupval); } static void -append_dict (DBusMessageIter *iter, int keytype, int valtype, const char *value) +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; + + dbus_message_iter_open_container (iter, DBUS_TYPE_DICT_ENTRY, NULL, + &subiter); + append_arg (&subiter, keytype, val); + val = strtok (NULL, ","); + + if (val == NULL) { - DBusMessageIter subiter; - - dbus_message_iter_open_container (iter, - DBUS_TYPE_DICT_ENTRY, - NULL, - &subiter); + fprintf (stderr, "%s: Malformed dictionary\n", appname); + exit (1); + } - 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); + append_arg (&subiter, valtype, val); + dbus_message_iter_close_container (iter, &subiter); + val = strtok (NULL, ","); + } - dbus_message_iter_close_container (iter, &subiter); - val = strtok (NULL, ","); - } free (dupval); } @@ -175,33 +186,57 @@ type_from_name (const char *arg) { int type; + if (!strcmp (arg, "string")) + { type = DBUS_TYPE_STRING; + } else if (!strcmp (arg, "int16")) + { type = DBUS_TYPE_INT16; + } else if (!strcmp (arg, "uint16")) + { type = DBUS_TYPE_UINT16; + } else if (!strcmp (arg, "int32")) + { type = DBUS_TYPE_INT32; + } else if (!strcmp (arg, "uint32")) + { type = DBUS_TYPE_UINT32; + } else if (!strcmp (arg, "int64")) + { type = DBUS_TYPE_INT64; + } else if (!strcmp (arg, "uint64")) + { type = DBUS_TYPE_UINT64; + } 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); - } + { + fprintf (stderr, "%s: Unknown type \"%s\"\n", appname, arg); + exit (1); + } + return type; } @@ -222,262 +257,331 @@ const char *path = NULL; int message_type = DBUS_MESSAGE_TYPE_SIGNAL; const char *type_str = NULL; + const char *peerbus = NULL; appname = argv[0]; - + if (argc < 3) + { + fprintf (stderr, "Too few arguments\n"); 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) { - char *arg = argv[i]; + type = DBUS_BUS_SYSTEM; + } + else if (strcmp (arg, "--session") == 0) + { + type = DBUS_BUS_SESSION; + } + else if (strstr (arg, "--private=") == arg) + { + peerbus = strchr (arg, '=') + 1; + } + else if (strncmp (arg, "--print-reply", 13) == 0) + { + print_reply = TRUE; + message_type = DBUS_MESSAGE_TYPE_METHOD_CALL; - 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 (*(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] == '-') + { + fprintf (stderr, "Unknown option \"%s\"\n", arg); + usage (1); + } + else if (path == NULL) + { + path = arg; + } + else if (name == NULL) + { + name = arg; + } + else + { + fprintf (stderr, "Unknown argument \"%s\"\n", arg); + usage (1); + } + } + + if (path == NULL) + { + fprintf (stderr, "Missing destination object path\n"); + usage (1); + } if (name == NULL) + { + fprintf (stderr, "Missing message name\n"); 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)) { - 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); - } + 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) + + if (print_reply && (message_type != DBUS_MESSAGE_TYPE_METHOD_CALL)) { - fprintf (stderr, "Failed to open connection to %s message bus: %s\n", - (type == DBUS_BUS_SYSTEM) ? "system" : "session", - error.message); - dbus_error_free (&error); + fprintf (stderr, "\"--print-reply\" implies \"--type=method_call\"\n"); exit (1); } + } + + dbus_error_init (&error); + + if (peerbus != NULL) + { + connection = dbus_connection_open (peerbus, &error); + } + else { + connection = dbus_bus_get (type, &error); + } + + if (connection == NULL) + { + fprintf (stderr, "Failed to open connection to %s message bus: %s\n", + ((peerbus != NULL) ? "private" : + ((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; + { + 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, '.'); - 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 + if (last_dot == NULL) { - fprintf (stderr, "Internal error, unknown message type\n"); + fprintf (stderr, + "Must use org.mydomain.Interface.Method notation, \ + no dot in \"%s\"\n", name); exit (1); } - if (message == NULL) + *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, "Couldn't allocate D-Bus message\n"); + 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, "Not enough memory\n"); + fprintf (stderr, + "%s: Data item \"%s\" is badly formed\n", argv[0], arg); exit (1); } - - dbus_message_iter_init_append (message, &iter); - while (i < argc) + *(c++) = 0; + container_type = DBUS_TYPE_INVALID; + + if (strcmp (arg, "variant") == 0) { - char *arg; - char *c; - int type; - int secondary_type; - int container_type; - DBusMessageIter *target_iter; - DBusMessageIter container_iter; + 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; + } - type = DBUS_TYPE_INVALID; - arg = argv[i++]; + 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); - } + { + 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 (arg[0] == 0) - type = DBUS_TYPE_STRING; - else - type = type_from_name (arg); + if (container_type == DBUS_TYPE_DICT_ENTRY) + { + char sig[5]; + arg = c; + c = strchr (c, ':'); - if (container_type == DBUS_TYPE_DICT_ENTRY) - { - char sig[5]; - 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); - 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 (c == NULL) + { + fprintf (stderr, + "%s: Data item \"%s\" is badly formed\n", argv[0], arg); + exit (1); + } - 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); + *(c++) = 0; + secondary_type = type_from_name (arg); + 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_INVALID) - { - dbus_message_iter_close_container (&iter, - &container_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 (print_reply) + if (container_type != DBUS_TYPE_INVALID) { - DBusMessage *reply; + dbus_message_iter_close_container (&iter, &container_iter); + } + } - 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 (print_reply) + { + DBusMessage *reply; - if (reply) - { - print_message (reply, print_reply_literal); - dbus_message_unref (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); } - else + + if (reply) { - dbus_connection_send (connection, message, NULL); - dbus_connection_flush (connection); + 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); @@ -485,3 +589,4 @@ exit (0); } + --- dbus-send.1.old 2008-04-08 16:09:17.000000000 -0500 +++ dbus-send.1 2008-04-09 06:20:30.000000000 -0500 @@ -8,8 +8,8 @@ .SH SYNOPSIS .PP .B dbus-send -[\-\-system | \-\-session] [\-\-dest=NAME] [\-\-print-reply] -[\-\-type=TYPE] [contents ...] +[\-\-system\ |\ \-\-session\ |\ \-\-private=PEERBUS] [\-\-dest=NAME] [\-\-print\-reply[=LITERALLY]] +[\-\-type=TYPE] [\-\-reply\-timeout=MSEC] [contents ...] .SH DESCRIPTION @@ -22,8 +22,9 @@ (installed on many systems as the "messagebus" service) and the per-user-login-session message bus (started each time a user logs in). The \-\-system and \-\-session options direct \fIdbus-send\fP to send -messages to the system or session buses respectively. If neither is -specified, \fIdbus-send\fP sends to the session bus. +messages to the system or session buses respectively. It is also possible +to use a private peer\-to\-peer message bus, using the \-\-private option. +If none of these are specified, \fIdbus-send\fP sends to the session bus. .PP Nearly all uses of \fIdbus-send\fP must provide the \-\-dest argument @@ -75,17 +76,27 @@ .I "--dest=NAME" Specify the name of the connection to receive the message. .TP -.I "--print-reply" +.I "--print-reply[=LITERALLY]" Block for a reply to the message sent, and print any reply received. +When specified, --type=method_call is set, and if the optional +LITERALLY value is specified, the resulting method reply will not +be formatted with type information and new lines. LITERALLY may be +any non-blank value. .TP -.I "--system" -Send to the system message bus. +.I "--private=PEERBUS" +Send to the private peer\-to\-peer message bus, specified by PEERBUS. .TP .I "--session" Send to the session message bus. (This is the default.) .TP +.I "--system" +Send to the system message bus. +.TP +.I "--reply-timeout=MSEC" +Specifies the number of milliseconds before the request will timeout (default is to wait forever). +.TP .I "--type=TYPE" -Specify "method_call" or "signal" (defaults to "signal"). +Specify "method_call" or "signal" (defaults to "signal" unless --print-reply is specified). .SH AUTHOR dbus-send was written by Philip Blundell.