1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
33 /* ---------------------------------------------------------------------------------------------------- */
35 G_GNUC_UNUSED static void completion_debug (const gchar *format, ...);
37 /* Uncomment to get debug traces in /tmp/gdbus-completion-debug.txt (nice
38 * to not have it interfere with stdout/stderr)
41 G_GNUC_UNUSED static void
42 completion_debug (const gchar *format, ...)
46 static FILE *f = NULL;
48 va_start (var_args, format);
49 s = g_strdup_vprintf (format, var_args);
52 f = fopen ("/tmp/gdbus-completion-debug.txt", "a+");
54 fprintf (f, "%s\n", s);
59 completion_debug (const gchar *format, ...)
64 /* ---------------------------------------------------------------------------------------------------- */
68 remove_arg (gint num, gint *argc, gchar **argv[])
72 g_assert (num <= (*argc));
74 for (n = num; (*argv)[n] != NULL; n++)
75 (*argv)[n] = (*argv)[n+1];
77 (*argc) = (*argc) - 1;
81 usage (gint *argc, gchar **argv[], gboolean use_stdout)
87 o = g_option_context_new (_("COMMAND"));
88 g_option_context_set_help_enabled (o, FALSE);
89 /* Ignore parsing result */
90 g_option_context_parse (o, argc, argv, NULL);
91 program_name = g_path_get_basename ((*argv)[0]);
92 s = g_strdup_printf (_("Commands:\n"
93 " help Shows this information\n"
94 " introspect Introspect a remote object\n"
95 " monitor Monitor a remote object\n"
96 " call Invoke a method on a remote object\n"
98 "Use \"%s COMMAND --help\" to get help on each command.\n"),
100 g_free (program_name);
101 g_option_context_set_description (o, s);
103 s = g_option_context_get_help (o, FALSE, NULL);
107 g_printerr ("%s", s);
109 g_option_context_free (o);
113 modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command)
119 * 1. get a g_set_prgname() ?; or
120 * 2. save old argv[0] and restore later
123 g_assert (g_strcmp0 ((*argv)[1], command) == 0);
124 remove_arg (1, argc, argv);
126 program_name = g_path_get_basename ((*argv)[0]);
127 s = g_strdup_printf ("%s %s", (*argv)[0], command);
129 g_free (program_name);
132 /* ---------------------------------------------------------------------------------------------------- */
135 print_methods (GDBusConnection *c,
141 const gchar *xml_data;
147 result = g_dbus_connection_call_sync (c,
150 "org.freedesktop.DBus.Introspectable",
153 G_DBUS_CALL_FLAGS_NONE,
159 g_printerr (_("Error: %s\n"), error->message);
160 g_error_free (error);
163 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)")))
165 g_printerr (_("Error: Result is type `%s', expected `(s)'\n"),
166 g_variant_get_type_string (result));
167 g_variant_unref (result);
170 g_variant_get (result, "(s)", &xml_data);
173 node = g_dbus_node_info_new_for_xml (xml_data, &error);
174 g_variant_unref (result);
177 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
178 g_error_free (error);
182 for (n = 0; node->interfaces != NULL && node->interfaces[n] != NULL; n++)
184 const GDBusInterfaceInfo *iface = node->interfaces[n];
185 for (m = 0; iface->methods != NULL && iface->methods[m] != NULL; m++)
187 const GDBusMethodInfo *method = iface->methods[m];
188 g_print ("%s.%s \n", iface->name, method->name);
191 g_dbus_node_info_unref (node);
198 print_paths (GDBusConnection *c,
204 const gchar *xml_data;
209 result = g_dbus_connection_call_sync (c,
212 "org.freedesktop.DBus.Introspectable",
215 G_DBUS_CALL_FLAGS_NONE,
221 g_printerr (_("Error: %s\n"), error->message);
222 g_error_free (error);
225 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)")))
227 g_printerr (_("Error: Result is type `%s', expected `(s)'\n"),
228 g_variant_get_type_string (result));
229 g_variant_unref (result);
232 g_variant_get (result, "(s)", &xml_data);
235 node = g_dbus_node_info_new_for_xml (xml_data, &error);
236 g_variant_unref (result);
239 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
240 g_error_free (error);
244 //g_printerr ("xml=`%s'", xml_data);
246 //g_printerr ("bar `%s'\n", path);
248 if (node->interfaces != NULL)
249 g_print ("%s \n", path);
251 for (n = 0; node->nodes != NULL && node->nodes[n] != NULL; n++)
255 //g_printerr ("foo `%s'\n", node->nodes[n].path);
257 if (g_strcmp0 (path, "/") == 0)
258 s = g_strdup_printf ("/%s", node->nodes[n]->path);
260 s = g_strdup_printf ("%s/%s", path, node->nodes[n]->path);
262 print_paths (c, name, s);
266 g_dbus_node_info_unref (node);
273 print_names (GDBusConnection *c,
274 gboolean include_unique_names)
280 GHashTable *name_set;
284 name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
287 result = g_dbus_connection_call_sync (c,
288 "org.freedesktop.DBus",
289 "/org/freedesktop/DBus",
290 "org.freedesktop.DBus",
293 G_DBUS_CALL_FLAGS_NONE,
299 g_printerr (_("Error: %s\n"), error->message);
300 g_error_free (error);
303 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(as)")))
305 g_printerr (_("Error: Result is type `%s', expected `(as)'\n"), g_variant_get_type_string (result));
306 g_variant_unref (result);
309 g_variant_get (result, "(as)", &iter);
310 while (g_variant_iter_loop (iter, "s", &str))
311 g_hash_table_insert (name_set, g_strdup (str), NULL);
312 g_variant_iter_free (iter);
313 g_variant_unref (result);
316 result = g_dbus_connection_call_sync (c,
317 "org.freedesktop.DBus",
318 "/org/freedesktop/DBus",
319 "org.freedesktop.DBus",
320 "ListActivatableNames",
322 G_DBUS_CALL_FLAGS_NONE,
328 g_printerr (_("Error: %s\n"), error->message);
329 g_error_free (error);
332 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(as)")))
334 g_printerr (_("Error: Result is type `%s', expected `(as)'\n"), g_variant_get_type_string (result));
335 g_variant_unref (result);
338 g_variant_get (result, "(as)", &iter);
339 while (g_variant_iter_loop (iter, "s", &str))
340 g_hash_table_insert (name_set, g_strdup (str), NULL);
341 g_variant_iter_free (iter);
342 g_variant_unref (result);
344 keys = g_hash_table_get_keys (name_set);
345 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
346 for (l = keys; l != NULL; l = l->next)
348 const gchar *name = l->data;
349 if (!include_unique_names && g_str_has_prefix (name, ":"))
352 g_print ("%s \n", name);
357 g_hash_table_unref (name_set);
360 /* ---------------------------------------------------------------------------------------------------- */
362 static gboolean opt_connection_system = FALSE;
363 static gboolean opt_connection_session = FALSE;
364 static gchar *opt_connection_address = NULL;
366 static const GOptionEntry connection_entries[] =
368 { "system", 'y', 0, G_OPTION_ARG_NONE, &opt_connection_system, N_("Connect to the system bus"), NULL},
369 { "session", 'e', 0, G_OPTION_ARG_NONE, &opt_connection_session, N_("Connect to the session bus"), NULL},
370 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_connection_address, N_("Connect to given D-Bus address"), NULL},
374 static GOptionGroup *
375 connection_get_group (void)
377 static GOptionGroup *g;
379 g = g_option_group_new ("connection",
380 N_("Connection Endpoint Options:"),
381 N_("Options specifying the connection endpoint"),
384 g_option_group_add_entries (g, connection_entries);
388 static GDBusConnection *
389 connection_get_dbus_connection (GError **error)
395 /* First, ensure we have exactly one connect */
396 if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
401 _("No connection endpoint specified"));
404 else if ((opt_connection_system && (opt_connection_session || opt_connection_address != NULL)) ||
405 (opt_connection_session && (opt_connection_system || opt_connection_address != NULL)) ||
406 (opt_connection_address != NULL && (opt_connection_system || opt_connection_session)))
411 _("Multiple connection endpoints specified"));
415 if (opt_connection_system)
417 c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
419 else if (opt_connection_session)
421 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
423 else if (opt_connection_address != NULL)
425 c = g_dbus_connection_new_for_address_sync (opt_connection_address,
426 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
427 NULL, /* GCancellable */
435 /* ---------------------------------------------------------------------------------------------------- */
438 call_helper_get_method_in_signature (GDBusConnection *c,
441 const gchar *interface_name,
442 const gchar *method_name,
447 GDBusNodeInfo *node_info;
448 const gchar *xml_data;
449 const GDBusInterfaceInfo *interface_info;
450 const GDBusMethodInfo *method_info;
457 result = g_dbus_connection_call_sync (c,
460 "org.freedesktop.DBus.Introspectable",
463 G_DBUS_CALL_FLAGS_NONE,
470 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)")))
472 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
473 _("Error: Result is type `%s', expected `(s)'\n"),
474 g_variant_get_type_string (result));
478 g_variant_get (result, "(s)", &xml_data);
479 node_info = g_dbus_node_info_new_for_xml (xml_data, error);
480 if (node_info == NULL)
483 interface_info = g_dbus_node_info_lookup_interface (node_info, interface_name);
484 if (interface_info == NULL)
486 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
487 _("Warning: According to introspection data, interface `%s' does not exist\n"),
492 method_info = g_dbus_interface_info_lookup_method (interface_info, method_name);
493 if (method_info == NULL)
495 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
496 _("Warning: According to introspection data, method `%s' does not exist on interface `%s'\n"),
502 ret = g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_type_free);
503 for (n = 0; method_info->in_args != NULL && method_info->in_args[n] != NULL; n++)
505 g_ptr_array_add (ret, g_variant_type_new (method_info->in_args[n]->signature));
509 if (node_info != NULL)
510 g_dbus_node_info_unref (node_info);
512 g_variant_unref (result);
517 /* ---------------------------------------------------------------------------------------------------- */
520 _g_variant_parse_me_harder (GVariantType *type,
521 const gchar *given_str,
529 str = g_string_new ("\"");
530 for (n = 0; given_str[n] != '\0'; n++)
532 if (G_UNLIKELY (given_str[n] == '\"'))
533 g_string_append (str, "\\\"");
535 g_string_append_c (str, given_str[n]);
537 g_string_append_c (str, '"');
538 s = g_string_free (str, FALSE);
540 value = g_variant_parse (type,
550 /* ---------------------------------------------------------------------------------------------------- */
552 static gchar *opt_call_dest = NULL;
553 static gchar *opt_call_object_path = NULL;
554 static gchar *opt_call_method = NULL;
556 static const GOptionEntry call_entries[] =
558 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_call_dest, N_("Destination name to invoke method on"), NULL},
559 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_call_object_path, N_("Object path to invoke method on"), NULL},
560 { "method", 'm', 0, G_OPTION_ARG_STRING, &opt_call_method, N_("Method and interface name"), NULL},
565 handle_call (gint *argc,
567 gboolean request_completion,
568 const gchar *completion_cur,
569 const gchar *completion_prev)
576 GVariant *parameters;
577 gchar *interface_name;
580 GPtrArray *in_signature_types;
581 gboolean complete_names;
582 gboolean complete_paths;
583 gboolean complete_methods;
584 GVariantBuilder builder;
590 interface_name = NULL;
593 in_signature_types = NULL;
595 modify_argv0_for_command (argc, argv, "call");
597 o = g_option_context_new (NULL);
598 g_option_context_set_help_enabled (o, FALSE);
599 g_option_context_set_summary (o, _("Invoke a method on a remote object."));
600 g_option_context_add_main_entries (o, call_entries, NULL /* GETTEXT_PACKAGE*/);
601 g_option_context_add_group (o, connection_get_group ());
603 complete_names = FALSE;
604 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
606 complete_names = TRUE;
607 remove_arg ((*argc) - 1, argc, argv);
610 complete_paths = FALSE;
611 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
613 complete_paths = TRUE;
614 remove_arg ((*argc) - 1, argc, argv);
617 complete_methods = FALSE;
618 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--method") == 0)
620 complete_methods = TRUE;
621 remove_arg ((*argc) - 1, argc, argv);
624 if (!g_option_context_parse (o, argc, argv, NULL))
626 if (!request_completion)
628 s = g_option_context_get_help (o, FALSE, NULL);
629 g_printerr ("%s", s);
636 c = connection_get_dbus_connection (&error);
639 if (request_completion)
641 if (g_strcmp0 (completion_prev, "--address") == 0)
649 g_print ("--system \n--session \n--address \n");
654 g_printerr (_("Error connecting: %s\n"), error->message);
655 g_error_free (error);
660 /* validate and complete destination (bus name) */
661 if (g_dbus_connection_get_unique_name (c) != NULL)
663 /* this only makes sense on message bus connections */
666 print_names (c, FALSE);
669 if (opt_call_dest == NULL)
671 if (request_completion)
672 g_print ("--dest \n");
674 g_printerr (_("Error: Destination is not specified\n"));
677 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
679 print_names (c, g_str_has_prefix (opt_call_dest, ":"));
684 /* validate and complete object path */
687 print_paths (c, opt_call_dest, "/");
690 if (opt_call_object_path == NULL)
692 if (request_completion)
693 g_print ("--object-path \n");
695 g_printerr (_("Error: Object path is not specified\n"));
698 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
701 s = g_strdup (opt_call_object_path);
702 p = strrchr (s, '/');
709 print_paths (c, opt_call_dest, s);
713 if (!request_completion && !g_variant_is_object_path (opt_call_object_path))
715 g_printerr (_("Error: %s is not a valid object path\n"), opt_call_object_path);
719 /* validate and complete method (interface + method name) */
720 if (complete_methods)
722 print_methods (c, opt_call_dest, opt_call_object_path);
725 if (opt_call_method == NULL)
727 if (request_completion)
728 g_print ("--method \n");
730 g_printerr (_("Error: Method name is not specified\n"));
733 if (request_completion && g_strcmp0 ("--method", completion_prev) == 0)
735 print_methods (c, opt_call_dest, opt_call_object_path);
738 s = strrchr (opt_call_method, '.');
739 if (!request_completion && s == NULL)
741 g_printerr (_("Error: Method name `%s' is invalid\n"), opt_call_method);
744 method_name = g_strdup (s + 1);
745 interface_name = g_strndup (opt_call_method, s - opt_call_method);
747 /* All done with completion now */
748 if (request_completion)
751 /* Introspect, for easy conversion - it's not fatal if we can't do this */
752 in_signature_types = call_helper_get_method_in_signature (c,
754 opt_call_object_path,
758 if (in_signature_types == NULL)
760 //g_printerr ("Error getting introspection data: %s\n", error->message);
761 g_error_free (error);
765 /* Read parameters */
766 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
767 for (n = 1; n < (guint) *argc; n++)
773 if (in_signature_types != NULL)
775 if (n - 1 >= in_signature_types->len)
777 /* Only warn for the first param */
778 if (n - 1 == in_signature_types->len)
780 g_printerr ("Warning: Introspection data indicates %d parameters but more was passed\n",
781 in_signature_types->len);
786 type = in_signature_types->pdata[n - 1];
791 value = g_variant_parse (type,
798 g_error_free (error);
800 value = _g_variant_parse_me_harder (type, (*argv)[n], &error);
805 s = g_variant_type_dup_string (type);
806 g_printerr (_("Error parsing parameter %d of type `%s': %s\n"),
814 g_printerr (_("Error parsing parameter %d: %s\n"),
818 g_error_free (error);
819 g_variant_builder_clear (&builder);
823 g_variant_builder_add_value (&builder, value);
825 parameters = g_variant_builder_end (&builder);
827 if (parameters != NULL)
828 parameters = g_variant_ref_sink (parameters);
829 result = g_dbus_connection_call_sync (c,
831 opt_call_object_path,
835 G_DBUS_CALL_FLAGS_NONE,
841 g_printerr (_("Error: %s\n"), error->message);
842 g_error_free (error);
843 if (in_signature_types != NULL)
846 s = g_string_new (NULL);
847 for (n = 0; n < in_signature_types->len; n++)
849 GVariantType *type = in_signature_types->pdata[n];
850 g_string_append_len (s,
851 g_variant_type_peek_string (type),
852 g_variant_type_get_string_length (type));
854 g_printerr ("(According to introspection data, you need to pass `%s')\n", s->str);
855 g_string_free (s, TRUE);
860 s = g_variant_print (result, TRUE);
867 if (in_signature_types != NULL)
868 g_ptr_array_unref (in_signature_types);
870 g_variant_unref (result);
873 if (parameters != NULL)
874 g_variant_unref (parameters);
875 g_free (interface_name);
876 g_free (method_name);
877 g_option_context_free (o);
881 /* ---------------------------------------------------------------------------------------------------- */
883 /* TODO: dump annotations */
886 dump_annotation (const GDBusAnnotationInfo *o,
888 gboolean ignore_indent)
891 g_print ("%*s@%s(\"%s\")\n",
892 ignore_indent ? 0 : indent, "",
895 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
896 dump_annotation (o->annotations[n], indent + 2, FALSE);
900 dump_arg (const GDBusArgInfo *o,
902 const gchar *direction,
903 gboolean ignore_indent,
904 gboolean include_newline)
908 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
910 dump_annotation (o->annotations[n], indent, ignore_indent);
911 ignore_indent = FALSE;
914 g_print ("%*s%s%s %s%s",
915 ignore_indent ? 0 : indent, "",
919 include_newline ? ",\n" : "");
923 count_args (GDBusArgInfo **args)
929 while (args[n] != NULL)
936 dump_method (const GDBusMethodInfo *o,
942 guint total_num_args;
944 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
945 dump_annotation (o->annotations[n], indent, FALSE);
947 g_print ("%*s%s(", indent, "", o->name);
948 name_len = strlen (o->name);
949 total_num_args = count_args (o->in_args) + count_args (o->out_args);
950 for (n = 0, m = 0; o->in_args != NULL && o->in_args[n] != NULL; n++, m++)
952 gboolean ignore_indent = (m == 0);
953 gboolean include_newline = (m != total_num_args - 1);
955 dump_arg (o->in_args[n],
956 indent + name_len + 1,
961 for (n = 0; o->out_args != NULL && o->out_args[n] != NULL; n++, m++)
963 gboolean ignore_indent = (m == 0);
964 gboolean include_newline = (m != total_num_args - 1);
965 dump_arg (o->out_args[n],
966 indent + name_len + 1,
975 dump_signal (const GDBusSignalInfo *o,
980 guint total_num_args;
982 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
983 dump_annotation (o->annotations[n], indent, FALSE);
985 g_print ("%*s%s(", indent, "", o->name);
986 name_len = strlen (o->name);
987 total_num_args = count_args (o->args);
988 for (n = 0; o->args != NULL && o->args[n] != NULL; n++)
990 gboolean ignore_indent = (n == 0);
991 gboolean include_newline = (n != total_num_args - 1);
992 dump_arg (o->args[n],
993 indent + name_len + 1,
1002 dump_property (const GDBusPropertyInfo *o,
1006 const gchar *access;
1009 if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
1010 access = "readonly";
1011 else if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
1012 access = "writeonly";
1013 else if (o->flags == (G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
1014 access = "readwrite";
1016 g_assert_not_reached ();
1018 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1019 dump_annotation (o->annotations[n], indent, FALSE);
1023 gchar *s = g_variant_print (value, FALSE);
1024 g_print ("%*s%s %s %s = %s;\n", indent, "", access, o->signature, o->name, s);
1029 g_print ("%*s%s %s %s;\n", indent, "", access, o->signature, o->name);
1034 dump_interface (GDBusConnection *c,
1036 const GDBusInterfaceInfo *o,
1038 const gchar *object_path)
1041 GHashTable *properties;
1043 properties = g_hash_table_new_full (g_str_hash,
1046 (GDestroyNotify) g_variant_unref);
1048 /* Try to get properties */
1049 if (c != NULL && name != NULL && object_path != NULL)
1052 result = g_dbus_connection_call_sync (c,
1055 "org.freedesktop.DBus.Properties",
1057 g_variant_new ("(s)", o->name),
1058 G_DBUS_CALL_FLAGS_NONE,
1064 if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1068 g_variant_get (result,
1071 while ((item = g_variant_iter_next_value (iter)))
1075 g_variant_get (item,
1080 g_hash_table_insert (properties, g_strdup (key), g_variant_ref (value));
1083 g_variant_unref (result);
1088 for (n = 0; o->properties != NULL && o->properties[n] != NULL; n++)
1090 result = g_dbus_connection_call_sync (c,
1093 "org.freedesktop.DBus.Properties",
1095 g_variant_new ("(ss)", o->name, o->properties[n]->name),
1096 G_DBUS_CALL_FLAGS_NONE,
1102 GVariant *property_value;
1103 g_variant_get (result,
1106 g_hash_table_insert (properties,
1107 g_strdup (o->properties[n]->name),
1108 g_variant_ref (property_value));
1109 g_variant_unref (result);
1115 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1116 dump_annotation (o->annotations[n], indent, FALSE);
1118 g_print ("%*sinterface %s {\n", indent, "", o->name);
1119 if (o->methods != NULL)
1121 g_print ("%*s methods:\n", indent, "");
1122 for (n = 0; o->methods[n] != NULL; n++)
1123 dump_method (o->methods[n], indent + 4);
1125 if (o->signals != NULL)
1127 g_print ("%*s signals:\n", indent, "");
1128 for (n = 0; o->signals[n] != NULL; n++)
1129 dump_signal (o->signals[n], indent + 4);
1131 if (o->properties != NULL)
1133 g_print ("%*s properties:\n", indent, "");
1134 for (n = 0; o->properties[n] != NULL; n++)
1136 dump_property (o->properties[n],
1138 g_hash_table_lookup (properties, (o->properties[n])->name));
1144 g_hash_table_unref (properties);
1148 dump_node (GDBusConnection *c,
1150 const GDBusNodeInfo *o,
1152 const gchar *object_path)
1155 const gchar *object_path_to_print;
1157 object_path_to_print = object_path;
1158 if (o->path != NULL)
1159 object_path_to_print = o->path;
1161 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1162 dump_annotation (o->annotations[n], indent, FALSE);
1164 g_print ("%*snode %s", indent, "", object_path_to_print != NULL ? object_path_to_print : "(not set)");
1165 if (o->interfaces != NULL || o->nodes != NULL)
1168 for (n = 0; o->interfaces != NULL && o->interfaces[n] != NULL; n++)
1169 dump_interface (c, name, o->interfaces[n], indent + 2, object_path);
1170 for (n = 0; o->nodes != NULL && o->nodes[n] != NULL; n++)
1171 dump_node (NULL, NULL, o->nodes[n], indent + 2, NULL);
1181 static gchar *opt_introspect_dest = NULL;
1182 static gchar *opt_introspect_object_path = NULL;
1184 static const GOptionEntry introspect_entries[] =
1186 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_introspect_dest, N_("Destination name to introspect"), NULL},
1187 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_introspect_object_path, N_("Object path to introspect"), NULL},
1192 handle_introspect (gint *argc,
1194 gboolean request_completion,
1195 const gchar *completion_cur,
1196 const gchar *completion_prev)
1204 const gchar *xml_data;
1205 GDBusNodeInfo *node;
1206 gboolean complete_names;
1207 gboolean complete_paths;
1214 modify_argv0_for_command (argc, argv, "introspect");
1216 o = g_option_context_new (NULL);
1217 if (request_completion)
1218 g_option_context_set_ignore_unknown_options (o, TRUE);
1219 g_option_context_set_help_enabled (o, FALSE);
1220 g_option_context_set_summary (o, _("Introspect a remote object."));
1221 g_option_context_add_main_entries (o, introspect_entries, NULL /* GETTEXT_PACKAGE*/);
1222 g_option_context_add_group (o, connection_get_group ());
1224 complete_names = FALSE;
1225 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1227 complete_names = TRUE;
1228 remove_arg ((*argc) - 1, argc, argv);
1231 complete_paths = FALSE;
1232 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1234 complete_paths = TRUE;
1235 remove_arg ((*argc) - 1, argc, argv);
1238 if (!g_option_context_parse (o, argc, argv, NULL))
1240 if (!request_completion)
1242 s = g_option_context_get_help (o, FALSE, NULL);
1243 g_printerr ("%s", s);
1250 c = connection_get_dbus_connection (&error);
1253 if (request_completion)
1255 if (g_strcmp0 (completion_prev, "--address") == 0)
1263 g_print ("--system \n--session \n--address \n");
1268 g_printerr (_("Error connecting: %s\n"), error->message);
1269 g_error_free (error);
1274 if (g_dbus_connection_get_unique_name (c) != NULL)
1278 print_names (c, FALSE);
1281 /* this only makes sense on message bus connections */
1282 if (opt_introspect_dest == NULL)
1284 if (request_completion)
1285 g_print ("--dest \n");
1287 g_printerr (_("Error: Destination is not specified\n"));
1290 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1292 print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1298 print_paths (c, opt_introspect_dest, "/");
1301 if (opt_introspect_object_path == NULL)
1303 if (request_completion)
1304 g_print ("--object-path \n");
1306 g_printerr (_("Error: Object path is not specified\n"));
1309 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1312 s = g_strdup (opt_introspect_object_path);
1313 p = strrchr (s, '/');
1320 print_paths (c, opt_introspect_dest, s);
1324 if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1326 g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1330 /* All done with completion now */
1331 if (request_completion)
1334 result = g_dbus_connection_call_sync (c,
1335 opt_introspect_dest,
1336 opt_introspect_object_path,
1337 "org.freedesktop.DBus.Introspectable",
1340 G_DBUS_CALL_FLAGS_NONE,
1346 g_printerr (_("Error: %s\n"), error->message);
1347 g_error_free (error);
1350 if (!g_variant_is_of_type (result, G_VARIANT_TYPE ("(s)")))
1352 g_printerr (_("Error: Result is type `%s', expected `(s)'\n"),
1353 g_variant_get_type_string (result));
1356 g_variant_get (result, "(s)", &xml_data);
1359 node = g_dbus_node_info_new_for_xml (xml_data, &error);
1362 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1363 g_error_free (error);
1367 dump_node (c, opt_introspect_dest, node, 0, opt_introspect_object_path);
1373 g_dbus_node_info_unref (node);
1375 g_variant_unref (result);
1378 g_option_context_free (o);
1382 /* ---------------------------------------------------------------------------------------------------- */
1384 static gchar *opt_monitor_dest = NULL;
1385 static gchar *opt_monitor_object_path = NULL;
1387 static guint monitor_filter_id = 0;
1390 monitor_signal_cb (GDBusConnection *connection,
1391 const gchar *sender_name,
1392 const gchar *object_path,
1393 const gchar *interface_name,
1394 const gchar *signal_name,
1395 GVariant *parameters,
1399 s = g_variant_print (parameters, TRUE);
1400 g_print ("%s: %s.%s %s\n",
1409 monitor_on_name_appeared (GDBusConnection *connection,
1411 const gchar *name_owner,
1414 g_print ("The name %s is owned by %s\n", name, name_owner);
1415 g_assert (monitor_filter_id == 0);
1416 monitor_filter_id = g_dbus_connection_signal_subscribe (connection,
1418 NULL, /* any interface */
1419 NULL, /* any member */
1420 opt_monitor_object_path,
1423 NULL, /* user_data */
1424 NULL); /* user_data destroy notify */
1428 monitor_on_name_vanished (GDBusConnection *connection,
1432 g_print ("The name %s does not have an owner\n", name);
1434 if (monitor_filter_id != 0)
1436 g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1437 monitor_filter_id = 0;
1441 static const GOptionEntry monitor_entries[] =
1443 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_monitor_dest, N_("Destination name to monitor"), NULL},
1444 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_monitor_object_path, N_("Object path to monitor"), NULL},
1449 handle_monitor (gint *argc,
1451 gboolean request_completion,
1452 const gchar *completion_cur,
1453 const gchar *completion_prev)
1461 GDBusNodeInfo *node;
1462 gboolean complete_names;
1463 gboolean complete_paths;
1471 modify_argv0_for_command (argc, argv, "monitor");
1473 o = g_option_context_new (NULL);
1474 if (request_completion)
1475 g_option_context_set_ignore_unknown_options (o, TRUE);
1476 g_option_context_set_help_enabled (o, FALSE);
1477 g_option_context_set_summary (o, _("Monitor a remote object."));
1478 g_option_context_add_main_entries (o, monitor_entries, NULL /* GETTEXT_PACKAGE*/);
1479 g_option_context_add_group (o, connection_get_group ());
1481 complete_names = FALSE;
1482 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1484 complete_names = TRUE;
1485 remove_arg ((*argc) - 1, argc, argv);
1488 complete_paths = FALSE;
1489 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1491 complete_paths = TRUE;
1492 remove_arg ((*argc) - 1, argc, argv);
1495 if (!g_option_context_parse (o, argc, argv, NULL))
1497 if (!request_completion)
1499 s = g_option_context_get_help (o, FALSE, NULL);
1500 g_printerr ("%s", s);
1507 c = connection_get_dbus_connection (&error);
1510 if (request_completion)
1512 if (g_strcmp0 (completion_prev, "--address") == 0)
1520 g_print ("--system \n--session \n--address \n");
1525 g_printerr (_("Error connecting: %s\n"), error->message);
1526 g_error_free (error);
1531 if (g_dbus_connection_get_unique_name (c) != NULL)
1535 print_names (c, FALSE);
1538 /* this only makes sense on message bus connections */
1539 if (opt_monitor_dest == NULL)
1541 if (request_completion)
1542 g_print ("--dest \n");
1544 g_printerr (_("Error: Destination is not specified\n"));
1547 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1549 print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1555 print_paths (c, opt_monitor_dest, "/");
1558 if (opt_monitor_object_path == NULL)
1560 if (request_completion)
1562 g_print ("--object-path \n");
1565 /* it's fine to not have an object path */
1567 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1570 s = g_strdup (opt_monitor_object_path);
1571 p = strrchr (s, '/');
1578 print_paths (c, opt_monitor_dest, s);
1582 if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1584 g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1588 /* All done with completion now */
1589 if (request_completion)
1592 if (opt_monitor_object_path != NULL)
1593 g_print ("Monitoring signals on object %s owned by %s\n", opt_monitor_object_path, opt_monitor_dest);
1595 g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1597 loop = g_main_loop_new (NULL, FALSE);
1598 g_bus_watch_name_on_connection (c,
1600 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1601 monitor_on_name_appeared,
1602 monitor_on_name_vanished,
1606 g_main_loop_run (loop);
1607 g_main_loop_unref (loop);
1613 g_dbus_node_info_unref (node);
1615 g_variant_unref (result);
1618 g_option_context_free (o);
1622 /* ---------------------------------------------------------------------------------------------------- */
1625 pick_word_at (const gchar *s,
1627 gint *out_word_begins_at)
1634 if (out_word_begins_at != NULL)
1635 *out_word_begins_at = -1;
1639 if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1641 if (out_word_begins_at != NULL)
1642 *out_word_begins_at = cursor;
1643 return g_strdup ("");
1646 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1651 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1654 if (out_word_begins_at != NULL)
1655 *out_word_begins_at = begin;
1657 return g_strndup (s + begin, end - begin);
1661 main (gint argc, gchar *argv[])
1664 const gchar *command;
1665 gboolean request_completion;
1666 gchar *completion_cur;
1667 gchar *completion_prev;
1670 completion_cur = NULL;
1671 completion_prev = NULL;
1677 usage (&argc, &argv, FALSE);
1681 request_completion = FALSE;
1683 //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1687 if (g_strcmp0 (command, "help") == 0)
1689 if (request_completion)
1695 usage (&argc, &argv, TRUE);
1700 else if (g_strcmp0 (command, "call") == 0)
1702 if (handle_call (&argc,
1710 else if (g_strcmp0 (command, "introspect") == 0)
1712 if (handle_introspect (&argc,
1720 else if (g_strcmp0 (command, "monitor") == 0)
1722 if (handle_monitor (&argc,
1730 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
1732 const gchar *completion_line;
1733 gchar **completion_argv;
1734 gint completion_argc;
1735 gint completion_point;
1739 request_completion = TRUE;
1741 completion_line = argv[2];
1742 completion_point = strtol (argv[3], &endp, 10);
1743 if (endp == argv[3] || *endp != '\0')
1747 completion_debug ("completion_point=%d", completion_point);
1748 completion_debug ("----");
1749 completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789");
1750 completion_debug ("`%s'", completion_line);
1751 completion_debug (" %*s^",
1752 completion_point, "");
1753 completion_debug ("----");
1756 if (!g_shell_parse_argv (completion_line,
1761 /* it's very possible the command line can't be parsed (for
1762 * example, missing quotes etc) - in that case, we just
1763 * don't autocomplete at all
1768 /* compute cur and prev */
1769 completion_prev = NULL;
1770 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
1774 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
1776 if (!g_ascii_isspace (completion_line[prev_end]))
1778 completion_prev = pick_word_at (completion_line, prev_end, NULL);
1784 completion_debug (" cur=`%s'", completion_cur);
1785 completion_debug ("prev=`%s'", completion_prev);
1788 argc = completion_argc;
1789 argv = completion_argv;
1797 if (request_completion)
1799 g_print ("help \ncall \nintrospect \nmonitor \n");
1805 g_printerr ("Unknown command `%s'\n", command);
1806 usage (&argc, &argv, FALSE);
1812 g_free (completion_cur);
1813 g_free (completion_prev);