2003-05-16 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / tools / dbus-send.c
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
30 static void
31 usage (char *name)
32 {
33   fprintf (stderr, "Usage: %s [--session] [--dest=SERVICE] [--print-reply] <message type> [contents ...]\n", name);
34   exit (1);
35 }
36
37 int
38 main (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_SYSTEM;
47   char *dest = DBUS_SERVICE_BROADCAST;
48   char *name = NULL;
49
50   if (argc < 2)
51     usage (argv[0]);
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, "--session") == 0)
60         type = DBUS_BUS_SESSION;
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 (arg[0] == '-')
66         usage (argv[0]);
67       else
68         name = arg;
69     }
70
71   if (name == NULL)
72     usage (argv[0]);
73
74   dbus_error_init (&error);
75   connection = dbus_bus_get (type, &error);
76   if (connection == NULL)
77     {
78       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
79                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
80                error.message);
81       dbus_error_free (&error);
82       exit (1);
83     }
84
85   message = dbus_message_new (name, dest);
86   if (message == NULL)
87     {
88       fprintf (stderr, "Couldn't allocate D-BUS message\n");
89       exit (1);
90     }
91
92   dbus_message_append_iter_init (message, &iter);
93
94   while (i < argc)
95     {
96       char *arg;
97       char *c;
98       int type;
99       dbus_uint32_t uint32;
100       dbus_int32_t int32;
101       double d;
102       unsigned char byte;
103
104       type = DBUS_TYPE_INVALID;
105       arg = argv[i++];
106       c = strchr (arg, ':');
107
108       if (c == NULL)
109         {
110           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
111           exit (1);
112         }
113
114       *(c++) = 0;
115
116       if (arg[0] == 0 || !strcmp (arg, "string"))
117         type = DBUS_TYPE_STRING;
118       else if (!strcmp (arg, "int32"))
119         type = DBUS_TYPE_INT32;
120       else if (!strcmp (arg, "uint32"))
121         type = DBUS_TYPE_UINT32;
122       else if (!strcmp (arg, "double"))
123         type = DBUS_TYPE_DOUBLE;
124       else if (!strcmp (arg, "byte"))
125         type = DBUS_TYPE_BYTE;
126       else if (!strcmp (arg, "boolean"))
127         type = DBUS_TYPE_BOOLEAN;
128       else
129         {
130           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
131           exit (1);
132         }
133
134       switch (type)
135         {
136         case DBUS_TYPE_BYTE:
137           byte = strtoul (c, NULL, 0);
138           dbus_message_iter_append_byte (&iter, byte);
139           break;
140
141         case DBUS_TYPE_DOUBLE:
142           d = strtod (c, NULL);
143           dbus_message_iter_append_double (&iter, d);
144           break;
145
146         case DBUS_TYPE_INT32:
147           int32 = strtol (c, NULL, 0);
148           dbus_message_iter_append_int32 (&iter, int32);
149           break;
150
151         case DBUS_TYPE_UINT32:
152           uint32 = strtoul (c, NULL, 0);
153           dbus_message_iter_append_uint32 (&iter, uint32);
154           break;
155
156         case DBUS_TYPE_STRING:
157           dbus_message_iter_append_string (&iter, c);
158           break;
159
160         default:
161           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
162           exit (1);
163         }
164     }
165
166   if (print_reply)
167     {
168       DBusMessage *reply;
169
170       dbus_error_init (&error);
171       reply = dbus_connection_send_with_reply_and_block (connection,
172                                                          message, -1,
173                                                          &error);
174       if (dbus_error_is_set (&error))
175         {
176           fprintf (stderr, "Error: %s\n",
177                    error.message);
178           exit (1);
179         }
180
181       if (reply)
182         {
183           print_message (reply);
184           dbus_message_unref (reply);
185         }
186     }
187   else
188     {
189       dbus_connection_send (connection, message, NULL);
190       dbus_connection_flush (connection);
191     }
192
193   dbus_message_unref (message);
194
195   dbus_connection_disconnect (connection);
196
197   exit (0);
198 }