a105f8b4f9ca752446e48c4872d4a356a2431c11
[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 static void
29 usage (char *name)
30 {
31   fprintf (stderr, "Usage: %s [--session] [--dest=SERVICE] <message type> [contents ...]\n", name);
32   exit (1);
33 }
34
35 int
36 main (int argc, char *argv[])
37 {
38   DBusConnection *connection;
39   DBusError error;
40   DBusMessage *message;
41   DBusMessageIter iter;
42   int i;
43   DBusBusType type = DBUS_BUS_SYSTEM;
44   char *dest = DBUS_SERVICE_BROADCAST;
45   char *name = NULL;
46
47   if (argc < 2)
48     usage (argv[0]);
49
50   for (i = 1; i < argc && name == NULL; i++)
51     {
52       char *arg = argv[i];
53
54       if (!strcmp (arg, "--session"))
55         type = DBUS_BUS_SESSION;
56       else if (strstr (arg, "--dest=") == arg)
57         dest = strchr (arg, '=') + 1;
58       else if (arg[0] == '-')
59         usage (argv[0]);
60       else
61         name = arg;
62     }
63
64   if (name == NULL)
65     usage (argv[0]);
66
67   dbus_error_init (&error);
68   connection = dbus_bus_get (type, &error);
69   if (connection == NULL)
70     {
71       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
72                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
73                error.message);
74       dbus_error_free (&error);
75       exit (1);
76     }
77
78   message = dbus_message_new (name, dest);
79   if (message == NULL)
80     {
81       fprintf (stderr, "Couldn't allocate D-BUS message\n");
82       exit (1);
83     }
84
85   dbus_message_append_iter_init (message, &iter);
86
87   while (i < argc)
88     {
89       char *arg;
90       char *c;
91       int type;
92       dbus_uint32_t uint32;
93       dbus_int32_t int32;
94       double d;
95       unsigned char byte;
96
97       type = DBUS_TYPE_INVALID;
98       arg = argv[i++];
99       c = strchr (arg, ':');
100
101       if (c == NULL)
102         {
103           fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg);
104           exit (1);
105         }
106
107       *(c++) = 0;
108
109       if (arg[0] == 0 || !strcmp (arg, "string"))
110         type = DBUS_TYPE_STRING;
111       else if (!strcmp (arg, "int32"))
112         type = DBUS_TYPE_INT32;
113       else if (!strcmp (arg, "uint32"))
114         type = DBUS_TYPE_UINT32;
115       else if (!strcmp (arg, "double"))
116         type = DBUS_TYPE_DOUBLE;
117       else if (!strcmp (arg, "byte"))
118         type = DBUS_TYPE_BYTE;
119       else if (!strcmp (arg, "boolean"))
120         type = DBUS_TYPE_BOOLEAN;
121       else
122         {
123           fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg);
124           exit (1);
125         }
126
127       switch (type)
128         {
129         case DBUS_TYPE_BYTE:
130           byte = strtoul (c, NULL, 0);
131           dbus_message_iter_append_byte (&iter, byte);
132           break;
133
134         case DBUS_TYPE_DOUBLE:
135           d = strtod (c, NULL);
136           dbus_message_iter_append_double (&iter, d);
137           break;
138
139         case DBUS_TYPE_INT32:
140           int32 = strtol (c, NULL, 0);
141           dbus_message_iter_append_int32 (&iter, int32);
142           break;
143
144         case DBUS_TYPE_UINT32:
145           uint32 = strtoul (c, NULL, 0);
146           dbus_message_iter_append_uint32 (&iter, uint32);
147           break;
148
149         case DBUS_TYPE_STRING:
150           dbus_message_iter_append_string (&iter, c);
151           break;
152
153         default:
154           fprintf (stderr, "%s: Unsupported data type\n", argv[0]);
155           exit (1);
156         }
157     }
158
159   dbus_connection_send (connection, message, NULL);
160
161   dbus_connection_flush (connection);
162
163   dbus_message_unref (message);
164
165   dbus_connection_disconnect (connection);
166
167   exit (0);
168 }