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