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"
30 #define _(x) dgettext (GETTEXT_PACKAGE, x)
36 #ifdef DBUS_BUILD_TESTS
37 static void run_all_tests (const char *test_data_dir);
43 depth *= 2; /* 2-space indent */
52 static void pretty_print (BaseInfo *base,
56 pretty_print_list (GSList *list,
64 pretty_print (tmp->data, depth);
70 pretty_print (BaseInfo *base,
76 t = base_info_get_type (base);
77 name = base_info_get_name (base);
85 NodeInfo *n = (NodeInfo*) base;
88 printf (_("<anonymous node> {\n"));
90 printf (_("node \"%s\" {\n"), name);
92 pretty_print_list (node_info_get_interfaces (n), depth + 1);
93 pretty_print_list (node_info_get_nodes (n), depth + 1);
99 case INFO_TYPE_INTERFACE:
101 InterfaceInfo *i = (InterfaceInfo*) base;
103 g_assert (name != NULL);
105 printf (_("interface \"%s\" {\n"), name);
107 pretty_print_list (interface_info_get_methods (i), depth + 1);
108 pretty_print_list (interface_info_get_signals (i), depth + 1);
114 case INFO_TYPE_METHOD:
116 MethodInfo *m = (MethodInfo*) base;
118 g_assert (name != NULL);
120 printf (_("method \"%s\" (\n"), name);
122 pretty_print_list (method_info_get_args (m), depth + 1);
128 case INFO_TYPE_SIGNAL:
130 SignalInfo *s = (SignalInfo*) base;
132 g_assert (name != NULL);
134 printf (_("signal \"%s\" (\n"), name);
136 pretty_print_list (signal_info_get_args (s), depth + 1);
144 ArgInfo *a = (ArgInfo*) base;
145 int at = arg_info_get_type (a);
146 ArgDirection d = arg_info_get_direction (a);
149 d == ARG_IN ? "in" : "out",
150 _dbus_gutils_type_to_string (at));
152 printf (" %s\n", name);
163 fprintf (stderr, "dbus-glib-tool [--version] [--help]\n");
170 printf ("D-BUS GLib Tool %s\n"
171 "Copyright (C) 2003, 2004 Red Hat, Inc.\n"
172 "This is free software; see the source for copying conditions.\n"
173 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n",
179 main (int argc, char **argv)
181 const char *prev_arg;
184 gboolean end_of_args;
186 gboolean just_pretty_print;
188 setlocale (LC_ALL, "");
189 bindtextdomain (GETTEXT_PACKAGE, DBUS_LOCALEDIR);
190 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
191 textdomain (GETTEXT_PACKAGE);
193 just_pretty_print = FALSE;
200 const char *arg = argv[i];
204 if (strcmp (arg, "--help") == 0 ||
205 strcmp (arg, "-h") == 0 ||
206 strcmp (arg, "-?") == 0)
208 else if (strcmp (arg, "--version") == 0)
210 #ifdef DBUS_BUILD_TESTS
211 else if (strcmp (arg, "--self-test") == 0)
212 run_all_tests (NULL);
213 #endif /* DBUS_BUILD_TESTS */
214 else if (strcmp (arg, "--pretty-print") == 0)
215 just_pretty_print = TRUE;
216 else if (arg[0] == '-' &&
220 else if (arg[0] == '-')
226 files = g_slist_prepend (files, (char*) arg);
230 files = g_slist_prepend (files, (char*) arg);
237 files = g_slist_reverse (files);
244 const char *filename;
246 filename = tmp->data;
249 node = description_load_from_file (filename,
253 g_assert (error != NULL);
254 fprintf (stderr, _("Unable to load \"%s\": %s\n"),
255 filename, error->message);
256 g_error_free (error);
259 else if (just_pretty_print)
261 pretty_print ((BaseInfo*) node, 0);
265 /* FIXME process the file to generate metadata variable
266 * definition rather than just printing it.
267 * i.e. we want to create DBusGObjectInfo.
268 * This probably requires extending the introspection XML format to
269 * allow a "native function name":
270 * <method name="Frobate" native="my_object_frobate">
272 pretty_print ((BaseInfo*) node, 0);
276 node_info_unref (node);
285 #ifdef DBUS_BUILD_TESTS
287 test_die (const char *failure)
289 fprintf (stderr, "Unit test failed: %s\n", failure);
295 * Unit test for GLib utility tool
296 * @returns #TRUE on success.
299 _dbus_gtool_test (const char *test_data_dir)
306 run_all_tests (const char *test_data_dir)
308 if (test_data_dir == NULL)
309 test_data_dir = g_getenv ("DBUS_TEST_DATA");
311 if (test_data_dir != NULL)
312 printf ("Test data in %s\n", test_data_dir);
314 printf ("No test data!\n");
316 printf ("%s: running gtool tests\n", "dbus-glib-tool");
317 if (!_dbus_gtool_test (test_data_dir))
320 printf ("%s: completed successfully\n", "dbus-glib-tool");
323 #endif /* DBUS_BUILD_TESTS */