dbus-send.c revision fc5e3a06cf4876fb10d0205a626e51109f29eb82
1/* -*- mode: C; c-file-style: "gnu" -*- */ 2/* dbus-send.c Utility program to send messages from the command line 3 * 4 * Copyright (C) 2003 Philip Blundell <philb@gnu.org> 5 * 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. 10 * 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. 15 * 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 19 * 20 */ 21 22#include <stdio.h> 23#include <stdlib.h> 24#include <string.h> 25 26#include <dbus/dbus.h> 27 28#include "dbus-print-message.h" 29 30static void 31usage (char *name, int ecode) 32{ 33 fprintf (stderr, "Usage: %s [--help] [--session] [--dest=SERVICE] [--print-reply] <message type> [contents ...]\n", name); 34 exit (ecode); 35} 36 37int 38main (int argc, char *argv[]) 39{ 40 DBusConnection *connection; 41 DBusError error; 42 DBusMessage *message; 43 int print_reply; 44 DBusMessageIter iter; 45 int i; 46 DBusBusType type = DBUS_BUS_SESSION; 47 char *dest = DBUS_SERVICE_BROADCAST; 48 char *name = NULL; 49 50 if (argc < 2) 51 usage (argv[0], 1); 52 53 print_reply = FALSE; 54 55 for (i = 1; i < argc && name == NULL; i++) 56 { 57 char *arg = argv[i]; 58 59 if (strcmp (arg, "--system") == 0) 60 type = DBUS_BUS_SYSTEM; 61 else if (strcmp (arg, "--print-reply") == 0) 62 print_reply = TRUE; 63 else if (strstr (arg, "--dest=") == arg) 64 dest = strchr (arg, '=') + 1; 65 else if (!strcmp(arg, "--help")) 66 usage (argv[0], 0); 67 else if (arg[0] == '-') 68 usage (argv[0], 1); 69 else 70 name = arg; 71 } 72 73 if (name == NULL) 74 usage (argv[0], 1); 75 76 dbus_error_init (&error); 77 connection = dbus_bus_get (type, &error); 78 if (connection == NULL) 79 { 80 fprintf (stderr, "Failed to open connection to %s message bus: %s\n", 81 (type == DBUS_BUS_SYSTEM) ? "system" : "session", 82 error.message); 83 dbus_error_free (&error); 84 exit (1); 85 } 86 87 message = dbus_message_new (name, dest); 88 if (message == NULL) 89 { 90 fprintf (stderr, "Couldn't allocate D-BUS message\n"); 91 exit (1); 92 } 93 94 dbus_message_append_iter_init (message, &iter); 95 96 while (i < argc) 97 { 98 char *arg; 99 char *c; 100 int type; 101 dbus_uint32_t uint32; 102 dbus_int32_t int32; 103 double d; 104 unsigned char byte; 105 106 type = DBUS_TYPE_INVALID; 107 arg = argv[i++]; 108 c = strchr (arg, ':'); 109 110 if (c == NULL) 111 { 112 fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg); 113 exit (1); 114 } 115 116 *(c++) = 0; 117 118 if (arg[0] == 0 || !strcmp (arg, "string")) 119 type = DBUS_TYPE_STRING; 120 else if (!strcmp (arg, "int32")) 121 type = DBUS_TYPE_INT32; 122 else if (!strcmp (arg, "uint32")) 123 type = DBUS_TYPE_UINT32; 124 else if (!strcmp (arg, "double")) 125 type = DBUS_TYPE_DOUBLE; 126 else if (!strcmp (arg, "byte")) 127 type = DBUS_TYPE_BYTE; 128 else if (!strcmp (arg, "boolean")) 129 type = DBUS_TYPE_BOOLEAN; 130 else 131 { 132 fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg); 133 exit (1); 134 } 135 136 switch (type) 137 { 138 case DBUS_TYPE_BYTE: 139 byte = strtoul (c, NULL, 0); 140 dbus_message_iter_append_byte (&iter, byte); 141 break; 142 143 case DBUS_TYPE_DOUBLE: 144 d = strtod (c, NULL); 145 dbus_message_iter_append_double (&iter, d); 146 break; 147 148 case DBUS_TYPE_INT32: 149 int32 = strtol (c, NULL, 0); 150 dbus_message_iter_append_int32 (&iter, int32); 151 break; 152 153 case DBUS_TYPE_UINT32: 154 uint32 = strtoul (c, NULL, 0); 155 dbus_message_iter_append_uint32 (&iter, uint32); 156 break; 157 158 case DBUS_TYPE_STRING: 159 dbus_message_iter_append_string (&iter, c); 160 break; 161 162 default: 163 fprintf (stderr, "%s: Unsupported data type\n", argv[0]); 164 exit (1); 165 } 166 } 167 168 if (print_reply) 169 { 170 DBusMessage *reply; 171 172 dbus_error_init (&error); 173 reply = dbus_connection_send_with_reply_and_block (connection, 174 message, -1, 175 &error); 176 if (dbus_error_is_set (&error)) 177 { 178 fprintf (stderr, "Error: %s\n", 179 error.message); 180 exit (1); 181 } 182 183 if (reply) 184 { 185 print_message (reply); 186 dbus_message_unref (reply); 187 } 188 } 189 else 190 { 191 dbus_connection_send (connection, message, NULL); 192 dbus_connection_flush (connection); 193 } 194 195 dbus_message_unref (message); 196 197 dbus_connection_disconnect (connection); 198 199 exit (0); 200} 201