2003-10-09 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, int ecode)
32 {
33   fprintf (stderr, "Usage: %s [--help] [--system | --session] [--dest=SERVICE] [--type=TYPE] [--print-reply] <destination object path> <message name> [contents ...]\n", name);
34   exit (ecode);
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_SESSION;
47   const char *dest = NULL;
48   const char *name = NULL;
49   const char *path = NULL;
50   int message_type = DBUS_MESSAGE_TYPE_SIGNAL;
51   const char *type_str = NULL;
52   
53   if (argc < 3)
54     usage (argv[0], 1);
55
56   print_reply = FALSE;
57   
58   for (i = 1; i < argc && name == NULL; i++)
59     {
60       char *arg = argv[i];
61
62       if (strcmp (arg, "--system") == 0)
63         type = DBUS_BUS_SYSTEM;
64       else if (strcmp (arg, "--session") == 0)
65         type = DBUS_BUS_SESSION;
66       else if (strcmp (arg, "--print-reply") == 0)
67         print_reply = TRUE;
68       else if (strstr (arg, "--dest=") == arg)
69         dest = strchr (arg, '=') + 1;
70       else if (strstr (arg, "--type=") == arg)
71         type_str = strchr (arg, '=') + 1;
72       else if (!strcmp(arg, "--help"))
73         usage (argv[0], 0);
74       else if (arg[0] == '-')
75         usage (argv[0], 1);
76       else if (path == NULL)
77         path = arg;
78       else if (name == NULL)
79         name = arg;
80       else
81         usage (argv[0], 1);
82     }
83
84   if (name == NULL)
85     usage (argv[0], 1);
86
87   if (type_str != NULL)
88     {
89       message_type = dbus_message_type_from_string (type_str);
90       if (!(message_type == DBUS_MESSAGE_TYPE_METHOD_CALL ||
91             message_type == DBUS_MESSAGE_TYPE_SIGNAL))
92         {
93           fprintf (stderr, "Message type \"%s\" is not supported\n",
94                    type_str);
95           exit (1);
96         }
97     }
98   
99   dbus_error_init (&error);
100   connection = dbus_bus_get (type, &error);
101   if (connection == NULL)
102     {
103       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
104                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
105                error.message);
106       dbus_error_free (&error);
107       exit (1);
108     }
109
110   if (message_type == DBUS_MESSAGE_TYPE_METHOD_CALL)
111     {
112       char *last_dot;
113
114       last_dot = strrchr (name, '.');
115       if (last_dot == NULL)
116         {
117           fprintf (stderr, "Must use org.mydomain.Interface.Method notation, no dot in \"%s\"\n",
118                    name);
119           exit (1);
120         }
121       *last_dot = '\0';
122       
123       message = dbus_message_new_method_call (NULL,
124                                               path,
125                                               name,
126                                               last_dot + 1);
127     }
128   else if (message_type == DBUS_MESSAGE_TYPE_SIGNAL)
129     {
130       char *last_dot;
131
132       last_dot = strrchr (name, '.');
133       if (last_dot == NULL)
134         {
135           fprintf (stderr, "Must use org.mydomain.Interface.Signal notation, no dot in \"%s\"\n",
136                    name);
137           exit (1);
138         }
139       *last_dot = '\0';
140       
141       message = dbus_message_new_signal (path, name, last_dot + 1);
142     }
143   else
144     {
145       fprintf (stderr, "Internal error, unknown message type\n");
146       exit (1);
147     }
148
149   if (message == NULL)
150     {
151       fprintf (stderr, "Couldn't allocate D-BUS message\n");
152       exit (1);
153     }
154
155   if (dest && !dbus_message_set_destination (message, dest))
156     {
157       fprintf (stderr, "Not enough memory\n");
158       exit (1);
159     }
160   
161   dbus_message_append_iter_init (message, &iter);
162
163   while (i < argc)
164     {
165       char *arg;
166       char *c;
167       int type;
168       dbus_uint32_t uint32;
169       dbus_int32_t int32;
170       double d;
171       unsigned char byte;
172
173       type = DBUS_TYPE_INVALID;
174       arg = argv[i++];
175       c = strchr (arg, ':');
176
177       if (c == NULL)
178         {
179           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
180           exit (1);
181         }
182
183       *(c++) = 0;
184
185       if (arg[0] == 0 || !strcmp (arg, "string"))
186         type = DBUS_TYPE_STRING;
187       else if (!strcmp (arg, "int32"))
188         type = DBUS_TYPE_INT32;
189       else if (!strcmp (arg, "uint32"))
190         type = DBUS_TYPE_UINT32;
191       else if (!strcmp (arg, "double"))
192         type = DBUS_TYPE_DOUBLE;
193       else if (!strcmp (arg, "byte"))
194         type = DBUS_TYPE_BYTE;
195       else if (!strcmp (arg, "boolean"))
196         type = DBUS_TYPE_BOOLEAN;
197       else
198         {
199           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
200           exit (1);
201         }
202
203       /* FIXME - we are ignoring OOM returns on all these functions */
204       switch (type)
205         {
206         case DBUS_TYPE_BYTE:
207           byte = strtoul (c, NULL, 0);
208           dbus_message_iter_append_byte (&iter, byte);
209           break;
210
211         case DBUS_TYPE_DOUBLE:
212           d = strtod (c, NULL);
213           dbus_message_iter_append_double (&iter, d);
214           break;
215
216         case DBUS_TYPE_INT32:
217           int32 = strtol (c, NULL, 0);
218           dbus_message_iter_append_int32 (&iter, int32);
219           break;
220
221         case DBUS_TYPE_UINT32:
222           uint32 = strtoul (c, NULL, 0);
223           dbus_message_iter_append_uint32 (&iter, uint32);
224           break;
225
226         case DBUS_TYPE_STRING:
227           dbus_message_iter_append_string (&iter, c);
228           break;
229
230         default:
231           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
232           exit (1);
233         }
234     }
235
236   if (print_reply)
237     {
238       DBusMessage *reply;
239
240       dbus_error_init (&error);
241       reply = dbus_connection_send_with_reply_and_block (connection,
242                                                          message, -1,
243                                                          &error);
244       if (dbus_error_is_set (&error))
245         {
246           fprintf (stderr, "Error: %s\n",
247                    error.message);
248           exit (1);
249         }
250
251       if (reply)
252         {
253           print_message (reply);
254           dbus_message_unref (reply);
255         }
256     }
257   else
258     {
259       dbus_connection_send (connection, message, NULL);
260       dbus_connection_flush (connection);
261     }
262
263   dbus_message_unref (message);
264
265   dbus_connection_disconnect (connection);
266
267   exit (0);
268 }