1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-glib-tool.c Tool used by apps using glib bindings
4 * Copyright (C) 2003, 2004 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-gidl.h"
26 #include "dbus-gparser.h"
27 #include "dbus-gutils.h"
28 #include "dbus-binding-tool-glib.h"
31 #define _(x) dgettext (GETTEXT_PACKAGE, x)
37 #ifdef DBUS_BUILD_TESTS
38 static void run_all_tests (const char *test_data_dir);
42 DBUS_BINDING_OUTPUT_NONE,
43 DBUS_BINDING_OUTPUT_PRETTY,
44 DBUS_BINDING_OUTPUT_GLIB_SERVER,
45 DBUS_BINDING_OUTPUT_GLIB_CLIENT,
46 } DBusBindingOutputMode;
51 depth *= 2; /* 2-space indent */
60 static void pretty_print (BaseInfo *base,
64 pretty_print_list (GSList *list,
72 pretty_print (tmp->data, depth);
78 pretty_print (BaseInfo *base,
84 t = base_info_get_type (base);
85 name = base_info_get_name (base);
93 NodeInfo *n = (NodeInfo*) base;
96 printf (_("<anonymous node> {\n"));
98 printf (_("node \"%s\" {\n"), name);
100 pretty_print_list (node_info_get_interfaces (n), depth + 1);
101 pretty_print_list (node_info_get_nodes (n), depth + 1);
107 case INFO_TYPE_INTERFACE:
109 InterfaceInfo *i = (InterfaceInfo*) base;
110 GSList *binding_types, *elt;
112 g_assert (name != NULL);
114 printf (_("interface \"%s\" {\n"), name);
116 binding_types = interface_info_get_binding_names (i);
117 for (elt = binding_types; elt; elt = elt->next)
119 const char *binding_type = elt->data;
120 const char *binding_name = interface_info_get_binding_name (i, binding_type);
122 printf (_(" (binding \"%s\": \"%s\") "),
123 binding_type, binding_name);
125 g_slist_free (binding_types);
127 pretty_print_list (interface_info_get_methods (i), depth + 1);
128 pretty_print_list (interface_info_get_signals (i), depth + 1);
129 pretty_print_list (interface_info_get_properties (i), depth + 1);
135 case INFO_TYPE_METHOD:
137 MethodInfo *m = (MethodInfo*) base;
138 GSList *binding_types, *elt;
140 g_assert (name != NULL);
142 binding_types = method_info_get_binding_names (m);
143 printf (_("method \"%s\""), name);
144 for (elt = binding_types; elt; elt = elt->next)
146 const char *binding_type = elt->data;
147 const char *binding_name = method_info_get_binding_name (m, binding_type);
149 printf (_(" (binding \"%s\": \"%s\") "),
150 binding_type, binding_name);
152 g_slist_free (binding_types);
154 pretty_print_list (method_info_get_args (m), depth + 1);
160 case INFO_TYPE_SIGNAL:
162 SignalInfo *s = (SignalInfo*) base;
164 g_assert (name != NULL);
166 printf (_("signal \"%s\" (\n"), name);
168 pretty_print_list (signal_info_get_args (s), depth + 1);
174 case INFO_TYPE_PROPERTY:
176 PropertyInfo *a = (PropertyInfo*) base;
177 int pt = property_info_get_type (a);
178 PropertyAccessFlags acc = property_info_get_access (a);
181 acc & PROPERTY_READ ? "read" : "",
182 acc & PROPERTY_WRITE ? "write" : "",
183 _dbus_gutils_type_to_string (pt));
185 printf (" %s\n", name);
192 ArgInfo *a = (ArgInfo*) base;
193 int at = arg_info_get_type (a);
194 ArgDirection d = arg_info_get_direction (a);
197 d == ARG_IN ? "in" : "out",
198 _dbus_gutils_type_to_string (at));
200 printf (" %s\n", name);
209 dbus_binding_tool_error_quark (void)
211 static GQuark quark = 0;
213 quark = g_quark_from_static_string ("dbus_binding_tool_error");
221 fprintf (stderr, "dbus-binding-tool [--version] [--help] [--pretty-print]\n");
228 printf ("D-BUS Binding Tool %s\n"
229 "Copyright (C) 2003-2005 Red Hat, Inc.\n"
230 "This is free software; see the source for copying conditions.\n"
231 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
237 main (int argc, char **argv)
239 const char *prev_arg;
242 DBusBindingOutputMode outputmode;
243 gboolean end_of_args;
248 setlocale (LC_ALL, "");
249 bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
250 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
251 textdomain (GETTEXT_PACKAGE);
255 outputmode = DBUS_BINDING_OUTPUT_NONE;
262 const char *arg = argv[i];
266 if (strcmp (arg, "--help") == 0 ||
267 strcmp (arg, "-h") == 0 ||
268 strcmp (arg, "-?") == 0)
270 else if (strcmp (arg, "--version") == 0)
272 #ifdef DBUS_BUILD_TESTS
273 else if (strcmp (arg, "--self-test") == 0)
274 run_all_tests (NULL);
275 #endif /* DBUS_BUILD_TESTS */
276 else if (strncmp (arg, "--mode=", 7) == 0)
278 const char *mode = arg + 7;
279 if (!strcmp (mode, "pretty"))
280 outputmode = DBUS_BINDING_OUTPUT_PRETTY;
281 else if (!strcmp (mode, "glib-server"))
282 outputmode = DBUS_BINDING_OUTPUT_GLIB_SERVER;
283 else if (!strcmp (mode, "glib-client"))
284 outputmode = DBUS_BINDING_OUTPUT_GLIB_CLIENT;
288 else if (arg[0] == '-' &&
292 else if (arg[0] == '-')
298 files = g_slist_prepend (files, (char*) arg);
302 files = g_slist_prepend (files, (char*) arg);
310 channel = g_io_channel_unix_new (fileno (stdout));
311 if (!g_io_channel_set_encoding (channel, NULL, &error))
313 fprintf (stderr, _("Couldn't set channel encoding to NULL: %s\n"),
318 files = g_slist_reverse (files);
325 const char *filename;
327 filename = tmp->data;
330 node = description_load_from_file (filename,
334 g_assert (error != NULL);
335 fprintf (stderr, _("Unable to load \"%s\": %s\n"),
336 filename, error->message);
337 g_error_free (error);
344 case DBUS_BINDING_OUTPUT_PRETTY:
345 pretty_print ((BaseInfo*) node, 0);
347 case DBUS_BINDING_OUTPUT_GLIB_SERVER:
348 if (!dbus_binding_tool_output_glib_server ((BaseInfo *) node, channel, &error))
350 g_error (_("Compilation failed: %s\n"), error->message);
354 case DBUS_BINDING_OUTPUT_GLIB_CLIENT:
355 if (!dbus_binding_tool_output_glib_client ((BaseInfo *) node, channel, &error))
357 g_error (_("Compilation failed: %s\n"), error->message);
361 case DBUS_BINDING_OUTPUT_NONE:
367 node_info_unref (node);
372 if (!g_io_channel_flush (channel, &error))
374 g_error (_("Failed to flush IO channel: %s"), error->message);
382 #ifdef DBUS_BUILD_TESTS
384 test_die (const char *failure)
386 fprintf (stderr, "Unit test failed: %s\n", failure);
392 * Unit test for GLib utility tool
393 * @returns #TRUE on success.
396 _dbus_gtool_test (const char *test_data_dir)
403 run_all_tests (const char *test_data_dir)
405 if (test_data_dir == NULL)
406 test_data_dir = g_getenv ("DBUS_TEST_DATA");
408 if (test_data_dir != NULL)
409 printf ("Test data in %s\n", test_data_dir);
411 printf ("No test data!\n");
413 printf ("%s: running binding tests\n", "dbus-binding-tool");
414 if (!_dbus_gtool_test (test_data_dir))
417 printf ("%s: completed successfully\n", "dbus-binding-tool");
420 #endif /* DBUS_BUILD_TESTS */