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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
33 #include "glib/glib-private.h"
36 /* ---------------------------------------------------------------------------------------------------- */
38 G_GNUC_UNUSED static void completion_debug (const gchar *format, ...);
40 /* Uncomment to get debug traces in /tmp/gdbus-completion-debug.txt (nice
41 * to not have it interfere with stdout/stderr)
44 G_GNUC_UNUSED static void
45 completion_debug (const gchar *format, ...)
49 static FILE *f = NULL;
51 va_start (var_args, format);
52 s = g_strdup_vprintf (format, var_args);
55 f = fopen ("/tmp/gdbus-completion-debug.txt", "a+");
57 fprintf (f, "%s\n", s);
62 completion_debug (const gchar *format, ...)
67 /* ---------------------------------------------------------------------------------------------------- */
71 remove_arg (gint num, gint *argc, gchar **argv[])
75 g_assert (num <= (*argc));
77 for (n = num; (*argv)[n] != NULL; n++)
78 (*argv)[n] = (*argv)[n+1];
80 (*argc) = (*argc) - 1;
84 usage (gint *argc, gchar **argv[], gboolean use_stdout)
90 o = g_option_context_new (_("COMMAND"));
91 g_option_context_set_help_enabled (o, FALSE);
92 /* Ignore parsing result */
93 g_option_context_parse (o, argc, argv, NULL);
94 program_name = g_path_get_basename ((*argv)[0]);
95 s = g_strdup_printf (_("Commands:\n"
96 " help Shows this information\n"
97 " introspect Introspect a remote object\n"
98 " monitor Monitor a remote object\n"
99 " call Invoke a method on a remote object\n"
100 " emit Emit a signal\n"
102 "Use \"%s COMMAND --help\" to get help on each command.\n"),
104 g_free (program_name);
105 g_option_context_set_description (o, s);
107 s = g_option_context_get_help (o, FALSE, NULL);
111 g_printerr ("%s", s);
113 g_option_context_free (o);
117 modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command)
123 * 1. get a g_set_prgname() ?; or
124 * 2. save old argv[0] and restore later
127 g_assert (g_strcmp0 ((*argv)[1], command) == 0);
128 remove_arg (1, argc, argv);
130 program_name = g_path_get_basename ((*argv)[0]);
131 s = g_strdup_printf ("%s %s", (*argv)[0], command);
133 g_free (program_name);
136 /* ---------------------------------------------------------------------------------------------------- */
139 print_methods (GDBusConnection *c,
145 const gchar *xml_data;
151 result = g_dbus_connection_call_sync (c,
154 "org.freedesktop.DBus.Introspectable",
157 G_VARIANT_TYPE ("(s)"),
158 G_DBUS_CALL_FLAGS_NONE,
164 g_printerr (_("Error: %s\n"), error->message);
165 g_error_free (error);
168 g_variant_get (result, "(&s)", &xml_data);
171 node = g_dbus_node_info_new_for_xml (xml_data, &error);
172 g_variant_unref (result);
175 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
176 g_error_free (error);
180 for (n = 0; node->interfaces != NULL && node->interfaces[n] != NULL; n++)
182 const GDBusInterfaceInfo *iface = node->interfaces[n];
183 for (m = 0; iface->methods != NULL && iface->methods[m] != NULL; m++)
185 const GDBusMethodInfo *method = iface->methods[m];
186 g_print ("%s.%s \n", iface->name, method->name);
189 g_dbus_node_info_unref (node);
196 print_paths (GDBusConnection *c,
202 const gchar *xml_data;
207 result = g_dbus_connection_call_sync (c,
210 "org.freedesktop.DBus.Introspectable",
213 G_VARIANT_TYPE ("(s)"),
214 G_DBUS_CALL_FLAGS_NONE,
220 g_printerr (_("Error: %s\n"), error->message);
221 g_error_free (error);
224 g_variant_get (result, "(&s)", &xml_data);
226 //g_printerr ("xml='%s'", xml_data);
229 node = g_dbus_node_info_new_for_xml (xml_data, &error);
230 g_variant_unref (result);
233 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
234 g_error_free (error);
238 //g_printerr ("bar '%s'\n", path);
240 if (node->interfaces != NULL)
241 g_print ("%s \n", path);
243 for (n = 0; node->nodes != NULL && node->nodes[n] != NULL; n++)
247 //g_printerr ("foo '%s'\n", node->nodes[n].path);
249 if (g_strcmp0 (path, "/") == 0)
250 s = g_strdup_printf ("/%s", node->nodes[n]->path);
252 s = g_strdup_printf ("%s/%s", path, node->nodes[n]->path);
254 print_paths (c, name, s);
258 g_dbus_node_info_unref (node);
265 print_names (GDBusConnection *c,
266 gboolean include_unique_names)
272 GHashTable *name_set;
276 name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
279 result = g_dbus_connection_call_sync (c,
280 "org.freedesktop.DBus",
281 "/org/freedesktop/DBus",
282 "org.freedesktop.DBus",
285 G_VARIANT_TYPE ("(as)"),
286 G_DBUS_CALL_FLAGS_NONE,
292 g_printerr (_("Error: %s\n"), error->message);
293 g_error_free (error);
296 g_variant_get (result, "(as)", &iter);
297 while (g_variant_iter_loop (iter, "s", &str))
298 g_hash_table_insert (name_set, g_strdup (str), NULL);
299 g_variant_iter_free (iter);
300 g_variant_unref (result);
303 result = g_dbus_connection_call_sync (c,
304 "org.freedesktop.DBus",
305 "/org/freedesktop/DBus",
306 "org.freedesktop.DBus",
307 "ListActivatableNames",
309 G_VARIANT_TYPE ("(as)"),
310 G_DBUS_CALL_FLAGS_NONE,
316 g_printerr (_("Error: %s\n"), error->message);
317 g_error_free (error);
320 g_variant_get (result, "(as)", &iter);
321 while (g_variant_iter_loop (iter, "s", &str))
322 g_hash_table_insert (name_set, g_strdup (str), NULL);
323 g_variant_iter_free (iter);
324 g_variant_unref (result);
326 keys = g_hash_table_get_keys (name_set);
327 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
328 for (l = keys; l != NULL; l = l->next)
330 const gchar *name = l->data;
331 if (!include_unique_names && g_str_has_prefix (name, ":"))
334 g_print ("%s \n", name);
339 g_hash_table_unref (name_set);
342 /* ---------------------------------------------------------------------------------------------------- */
344 static gboolean opt_connection_system = FALSE;
345 static gboolean opt_connection_session = FALSE;
346 static gchar *opt_connection_address = NULL;
348 static const GOptionEntry connection_entries[] =
350 { "system", 'y', 0, G_OPTION_ARG_NONE, &opt_connection_system, N_("Connect to the system bus"), NULL},
351 { "session", 'e', 0, G_OPTION_ARG_NONE, &opt_connection_session, N_("Connect to the session bus"), NULL},
352 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_connection_address, N_("Connect to given D-Bus address"), NULL},
356 static GOptionGroup *
357 connection_get_group (void)
359 static GOptionGroup *g;
361 g = g_option_group_new ("connection",
362 N_("Connection Endpoint Options:"),
363 N_("Options specifying the connection endpoint"),
366 g_option_group_set_translation_domain (g, GETTEXT_PACKAGE);
367 g_option_group_add_entries (g, connection_entries);
372 static GDBusConnection *
373 connection_get_dbus_connection (GError **error)
379 /* First, ensure we have exactly one connect */
380 if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
385 _("No connection endpoint specified"));
388 else if ((opt_connection_system && (opt_connection_session || opt_connection_address != NULL)) ||
389 (opt_connection_session && (opt_connection_system || opt_connection_address != NULL)) ||
390 (opt_connection_address != NULL && (opt_connection_system || opt_connection_session)))
395 _("Multiple connection endpoints specified"));
399 if (opt_connection_system)
401 c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
403 else if (opt_connection_session)
405 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
407 else if (opt_connection_address != NULL)
409 c = g_dbus_connection_new_for_address_sync (opt_connection_address,
410 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
411 NULL, /* GDBusAuthObserver */
412 NULL, /* GCancellable */
420 /* ---------------------------------------------------------------------------------------------------- */
423 call_helper_get_method_in_signature (GDBusConnection *c,
426 const gchar *interface_name,
427 const gchar *method_name,
432 GDBusNodeInfo *node_info;
433 const gchar *xml_data;
434 GDBusInterfaceInfo *interface_info;
435 GDBusMethodInfo *method_info;
442 result = g_dbus_connection_call_sync (c,
445 "org.freedesktop.DBus.Introspectable",
448 G_VARIANT_TYPE ("(s)"),
449 G_DBUS_CALL_FLAGS_NONE,
456 g_variant_get (result, "(&s)", &xml_data);
457 node_info = g_dbus_node_info_new_for_xml (xml_data, error);
458 if (node_info == NULL)
461 interface_info = g_dbus_node_info_lookup_interface (node_info, interface_name);
462 if (interface_info == NULL)
464 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
465 _("Warning: According to introspection data, interface '%s' does not exist\n"),
470 method_info = g_dbus_interface_info_lookup_method (interface_info, method_name);
471 if (method_info == NULL)
473 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
474 _("Warning: According to introspection data, method '%s' does not exist on interface '%s'\n"),
480 ret = g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_type_free);
481 for (n = 0; method_info->in_args != NULL && method_info->in_args[n] != NULL; n++)
483 g_ptr_array_add (ret, g_variant_type_new (method_info->in_args[n]->signature));
487 if (node_info != NULL)
488 g_dbus_node_info_unref (node_info);
490 g_variant_unref (result);
495 /* ---------------------------------------------------------------------------------------------------- */
498 _g_variant_parse_me_harder (GVariantType *type,
499 const gchar *given_str,
507 str = g_string_new ("\"");
508 for (n = 0; given_str[n] != '\0'; n++)
510 if (G_UNLIKELY (given_str[n] == '\"'))
511 g_string_append (str, "\\\"");
513 g_string_append_c (str, given_str[n]);
515 g_string_append_c (str, '"');
516 s = g_string_free (str, FALSE);
518 value = g_variant_parse (type,
528 /* ---------------------------------------------------------------------------------------------------- */
530 static gchar *opt_emit_dest = NULL;
531 static gchar *opt_emit_object_path = NULL;
532 static gchar *opt_emit_signal = NULL;
534 static const GOptionEntry emit_entries[] =
536 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_emit_dest, N_("Optional destination for signal (unique name)"), NULL},
537 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_emit_object_path, N_("Object path to emit signal on"), NULL},
538 { "signal", 's', 0, G_OPTION_ARG_STRING, &opt_emit_signal, N_("Signal and interface name"), NULL},
543 handle_emit (gint *argc,
545 gboolean request_completion,
546 const gchar *completion_cur,
547 const gchar *completion_prev)
554 GVariant *parameters;
555 gchar *interface_name;
557 GVariantBuilder builder;
563 interface_name = NULL;
566 modify_argv0_for_command (argc, argv, "emit");
568 o = g_option_context_new (NULL);
569 g_option_context_set_help_enabled (o, FALSE);
570 g_option_context_set_summary (o, _("Emit a signal."));
571 g_option_context_add_main_entries (o, emit_entries, GETTEXT_PACKAGE);
572 g_option_context_add_group (o, connection_get_group ());
574 if (!g_option_context_parse (o, argc, argv, NULL))
576 if (!request_completion)
578 s = g_option_context_get_help (o, FALSE, NULL);
579 g_printerr ("%s", s);
586 c = connection_get_dbus_connection (&error);
589 if (request_completion)
591 if (g_strcmp0 (completion_prev, "--address") == 0)
599 g_print ("--system \n--session \n--address \n");
604 g_printerr (_("Error connecting: %s\n"), error->message);
605 g_error_free (error);
610 /* All done with completion now */
611 if (request_completion)
614 if (opt_emit_object_path == NULL)
616 g_printerr (_("Error: object path not specified.\n"));
619 if (!g_variant_is_object_path (opt_emit_object_path))
621 g_printerr (_("Error: %s is not a valid object path\n"), opt_emit_object_path);
625 if (opt_emit_signal == NULL)
627 g_printerr (_("Error: signal not specified.\n"));
631 s = strrchr (opt_emit_signal, '.');
634 g_printerr (_("Error: signal must be the fully-qualified name.\n"));
637 signal_name = g_strdup (s + 1);
638 interface_name = g_strndup (opt_emit_signal, s - opt_emit_signal);
640 if (!g_dbus_is_interface_name (interface_name))
642 g_printerr (_("Error: %s is not a valid interface name\n"), interface_name);
646 if (!g_dbus_is_member_name (signal_name))
648 g_printerr (_("Error: %s is not a valid member name\n"), signal_name);
652 if (opt_emit_dest != NULL && !g_dbus_is_unique_name (opt_emit_dest))
654 g_printerr (_("Error: %s is not a valid unique bus name.\n"), opt_emit_dest);
658 /* Read parameters */
659 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
660 for (n = 1; n < (guint) *argc; n++)
665 value = g_variant_parse (NULL,
674 context = g_variant_parse_error_print_context (error, (*argv)[n]);
675 g_error_free (error);
677 value = _g_variant_parse_me_harder (NULL, (*argv)[n], &error);
680 /* Use the original non-"parse-me-harder" error */
681 g_printerr (_("Error parsing parameter %d: %s\n"),
684 g_error_free (error);
686 g_variant_builder_clear (&builder);
691 g_variant_builder_add_value (&builder, value);
693 parameters = g_variant_builder_end (&builder);
695 if (parameters != NULL)
696 parameters = g_variant_ref_sink (parameters);
697 if (!g_dbus_connection_emit_signal (c,
699 opt_emit_object_path,
705 g_printerr (_("Error: %s\n"), error->message);
706 g_error_free (error);
710 if (!g_dbus_connection_flush_sync (c, NULL, &error))
712 g_printerr (_("Error flushing connection: %s\n"), error->message);
713 g_error_free (error);
722 if (parameters != NULL)
723 g_variant_unref (parameters);
724 g_free (interface_name);
725 g_free (signal_name);
726 g_option_context_free (o);
730 /* ---------------------------------------------------------------------------------------------------- */
732 static gchar *opt_call_dest = NULL;
733 static gchar *opt_call_object_path = NULL;
734 static gchar *opt_call_method = NULL;
735 static gint opt_call_timeout = -1;
737 static const GOptionEntry call_entries[] =
739 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_call_dest, N_("Destination name to invoke method on"), NULL},
740 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_call_object_path, N_("Object path to invoke method on"), NULL},
741 { "method", 'm', 0, G_OPTION_ARG_STRING, &opt_call_method, N_("Method and interface name"), NULL},
742 { "timeout", 't', 0, G_OPTION_ARG_INT, &opt_call_timeout, N_("Timeout in seconds"), NULL},
747 handle_call (gint *argc,
749 gboolean request_completion,
750 const gchar *completion_cur,
751 const gchar *completion_prev)
758 GVariant *parameters;
759 gchar *interface_name;
762 GPtrArray *in_signature_types;
763 gboolean complete_names;
764 gboolean complete_paths;
765 gboolean complete_methods;
766 GVariantBuilder builder;
772 interface_name = NULL;
775 in_signature_types = NULL;
777 modify_argv0_for_command (argc, argv, "call");
779 o = g_option_context_new (NULL);
780 g_option_context_set_help_enabled (o, FALSE);
781 g_option_context_set_summary (o, _("Invoke a method on a remote object."));
782 g_option_context_add_main_entries (o, call_entries, GETTEXT_PACKAGE);
783 g_option_context_add_group (o, connection_get_group ());
785 complete_names = FALSE;
786 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
788 complete_names = TRUE;
789 remove_arg ((*argc) - 1, argc, argv);
792 complete_paths = FALSE;
793 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
795 complete_paths = TRUE;
796 remove_arg ((*argc) - 1, argc, argv);
799 complete_methods = FALSE;
800 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--method") == 0)
802 complete_methods = TRUE;
803 remove_arg ((*argc) - 1, argc, argv);
806 if (!g_option_context_parse (o, argc, argv, NULL))
808 if (!request_completion)
810 s = g_option_context_get_help (o, FALSE, NULL);
811 g_printerr ("%s", s);
818 c = connection_get_dbus_connection (&error);
821 if (request_completion)
823 if (g_strcmp0 (completion_prev, "--address") == 0)
831 g_print ("--system \n--session \n--address \n");
836 g_printerr (_("Error connecting: %s\n"), error->message);
837 g_error_free (error);
842 /* validate and complete destination (bus name) */
843 if (g_dbus_connection_get_unique_name (c) != NULL)
845 /* this only makes sense on message bus connections */
848 print_names (c, FALSE);
851 if (opt_call_dest == NULL)
853 if (request_completion)
854 g_print ("--dest \n");
856 g_printerr (_("Error: Destination is not specified\n"));
859 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
861 print_names (c, g_str_has_prefix (opt_call_dest, ":"));
866 /* validate and complete object path */
869 print_paths (c, opt_call_dest, "/");
872 if (opt_call_object_path == NULL)
874 if (request_completion)
875 g_print ("--object-path \n");
877 g_printerr (_("Error: Object path is not specified\n"));
880 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
883 s = g_strdup (opt_call_object_path);
884 p = strrchr (s, '/');
891 print_paths (c, opt_call_dest, s);
895 if (!request_completion && !g_variant_is_object_path (opt_call_object_path))
897 g_printerr (_("Error: %s is not a valid object path\n"), opt_call_object_path);
901 /* validate and complete method (interface + method name) */
902 if (complete_methods)
904 print_methods (c, opt_call_dest, opt_call_object_path);
907 if (opt_call_method == NULL)
909 if (request_completion)
910 g_print ("--method \n");
912 g_printerr (_("Error: Method name is not specified\n"));
915 if (request_completion && g_strcmp0 ("--method", completion_prev) == 0)
917 print_methods (c, opt_call_dest, opt_call_object_path);
920 s = strrchr (opt_call_method, '.');
921 if (!request_completion && s == NULL)
923 g_printerr (_("Error: Method name '%s' is invalid\n"), opt_call_method);
926 method_name = g_strdup (s + 1);
927 interface_name = g_strndup (opt_call_method, s - opt_call_method);
929 /* All done with completion now */
930 if (request_completion)
933 /* Introspect, for easy conversion - it's not fatal if we can't do this */
934 in_signature_types = call_helper_get_method_in_signature (c,
936 opt_call_object_path,
940 if (in_signature_types == NULL)
942 //g_printerr ("Error getting introspection data: %s\n", error->message);
943 g_error_free (error);
947 /* Read parameters */
948 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
949 for (n = 1; n < (guint) *argc; n++)
955 if (in_signature_types != NULL)
957 if (n - 1 >= in_signature_types->len)
959 /* Only warn for the first param */
960 if (n - 1 == in_signature_types->len)
962 g_printerr ("Warning: Introspection data indicates %d parameters but more was passed\n",
963 in_signature_types->len);
968 type = in_signature_types->pdata[n - 1];
973 value = g_variant_parse (type,
982 context = g_variant_parse_error_print_context (error, (*argv)[n]);
983 g_error_free (error);
985 value = _g_variant_parse_me_harder (type, (*argv)[n], &error);
990 s = g_variant_type_dup_string (type);
991 g_printerr (_("Error parsing parameter %d of type '%s': %s\n"),
999 g_printerr (_("Error parsing parameter %d: %s\n"),
1003 g_error_free (error);
1004 g_variant_builder_clear (&builder);
1010 g_variant_builder_add_value (&builder, value);
1012 parameters = g_variant_builder_end (&builder);
1014 if (parameters != NULL)
1015 parameters = g_variant_ref_sink (parameters);
1016 result = g_dbus_connection_call_sync (c,
1018 opt_call_object_path,
1023 G_DBUS_CALL_FLAGS_NONE,
1024 opt_call_timeout > 0 ? opt_call_timeout * 1000 : opt_call_timeout,
1031 g_printerr (_("Error: %s\n"), error->message);
1032 g_error_free (error);
1034 if (in_signature_types != NULL)
1037 s = g_string_new (NULL);
1038 for (n = 0; n < in_signature_types->len; n++)
1040 GVariantType *type = in_signature_types->pdata[n];
1041 g_string_append_len (s,
1042 g_variant_type_peek_string (type),
1043 g_variant_type_get_string_length (type));
1045 g_printerr ("(According to introspection data, you need to pass '%s')\n", s->str);
1046 g_string_free (s, TRUE);
1051 s = g_variant_print (result, TRUE);
1052 g_print ("%s\n", s);
1058 if (in_signature_types != NULL)
1059 g_ptr_array_unref (in_signature_types);
1061 g_variant_unref (result);
1064 if (parameters != NULL)
1065 g_variant_unref (parameters);
1066 g_free (interface_name);
1067 g_free (method_name);
1068 g_option_context_free (o);
1072 /* ---------------------------------------------------------------------------------------------------- */
1074 static gchar *opt_introspect_dest = NULL;
1075 static gchar *opt_introspect_object_path = NULL;
1076 static gboolean opt_introspect_xml = FALSE;
1077 static gboolean opt_introspect_recurse = FALSE;
1078 static gboolean opt_introspect_only_properties = FALSE;
1081 dump_annotation (const GDBusAnnotationInfo *o,
1083 gboolean ignore_indent)
1086 g_print ("%*s@%s(\"%s\")\n",
1087 ignore_indent ? 0 : indent, "",
1090 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1091 dump_annotation (o->annotations[n], indent + 2, FALSE);
1095 dump_arg (const GDBusArgInfo *o,
1097 const gchar *direction,
1098 gboolean ignore_indent,
1099 gboolean include_newline)
1103 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1105 dump_annotation (o->annotations[n], indent, ignore_indent);
1106 ignore_indent = FALSE;
1109 g_print ("%*s%s%s %s%s",
1110 ignore_indent ? 0 : indent, "",
1114 include_newline ? ",\n" : "");
1118 count_args (GDBusArgInfo **args)
1124 while (args[n] != NULL)
1131 dump_method (const GDBusMethodInfo *o,
1137 guint total_num_args;
1139 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1140 dump_annotation (o->annotations[n], indent, FALSE);
1142 g_print ("%*s%s(", indent, "", o->name);
1143 name_len = strlen (o->name);
1144 total_num_args = count_args (o->in_args) + count_args (o->out_args);
1145 for (n = 0, m = 0; o->in_args != NULL && o->in_args[n] != NULL; n++, m++)
1147 gboolean ignore_indent = (m == 0);
1148 gboolean include_newline = (m != total_num_args - 1);
1150 dump_arg (o->in_args[n],
1151 indent + name_len + 1,
1156 for (n = 0; o->out_args != NULL && o->out_args[n] != NULL; n++, m++)
1158 gboolean ignore_indent = (m == 0);
1159 gboolean include_newline = (m != total_num_args - 1);
1160 dump_arg (o->out_args[n],
1161 indent + name_len + 1,
1170 dump_signal (const GDBusSignalInfo *o,
1175 guint total_num_args;
1177 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1178 dump_annotation (o->annotations[n], indent, FALSE);
1180 g_print ("%*s%s(", indent, "", o->name);
1181 name_len = strlen (o->name);
1182 total_num_args = count_args (o->args);
1183 for (n = 0; o->args != NULL && o->args[n] != NULL; n++)
1185 gboolean ignore_indent = (n == 0);
1186 gboolean include_newline = (n != total_num_args - 1);
1187 dump_arg (o->args[n],
1188 indent + name_len + 1,
1197 dump_property (const GDBusPropertyInfo *o,
1201 const gchar *access;
1204 if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
1205 access = "readonly";
1206 else if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
1207 access = "writeonly";
1208 else if (o->flags == (G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
1209 access = "readwrite";
1211 g_assert_not_reached ();
1213 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1214 dump_annotation (o->annotations[n], indent, FALSE);
1218 gchar *s = g_variant_print (value, FALSE);
1219 g_print ("%*s%s %s %s = %s;\n", indent, "", access, o->signature, o->name, s);
1224 g_print ("%*s%s %s %s;\n", indent, "", access, o->signature, o->name);
1229 dump_interface (GDBusConnection *c,
1231 const GDBusInterfaceInfo *o,
1233 const gchar *object_path)
1236 GHashTable *properties;
1238 properties = g_hash_table_new_full (g_str_hash,
1241 (GDestroyNotify) g_variant_unref);
1243 /* Try to get properties */
1244 if (c != NULL && name != NULL && object_path != NULL && o->properties != NULL)
1247 result = g_dbus_connection_call_sync (c,
1250 "org.freedesktop.DBus.Properties",
1252 g_variant_new ("(s)", o->name),
1254 G_DBUS_CALL_FLAGS_NONE,
1260 if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1264 g_variant_get (result,
1267 while ((item = g_variant_iter_next_value (iter)))
1271 g_variant_get (item,
1276 g_hash_table_insert (properties, key, g_variant_ref (value));
1279 g_variant_unref (result);
1284 for (n = 0; o->properties != NULL && o->properties[n] != NULL; n++)
1286 result = g_dbus_connection_call_sync (c,
1289 "org.freedesktop.DBus.Properties",
1291 g_variant_new ("(ss)", o->name, o->properties[n]->name),
1292 G_VARIANT_TYPE ("(v)"),
1293 G_DBUS_CALL_FLAGS_NONE,
1299 GVariant *property_value;
1300 g_variant_get (result,
1303 g_hash_table_insert (properties,
1304 g_strdup (o->properties[n]->name),
1305 g_variant_ref (property_value));
1306 g_variant_unref (result);
1312 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1313 dump_annotation (o->annotations[n], indent, FALSE);
1315 g_print ("%*sinterface %s {\n", indent, "", o->name);
1316 if (o->methods != NULL && !opt_introspect_only_properties)
1318 g_print ("%*s methods:\n", indent, "");
1319 for (n = 0; o->methods[n] != NULL; n++)
1320 dump_method (o->methods[n], indent + 4);
1322 if (o->signals != NULL && !opt_introspect_only_properties)
1324 g_print ("%*s signals:\n", indent, "");
1325 for (n = 0; o->signals[n] != NULL; n++)
1326 dump_signal (o->signals[n], indent + 4);
1328 if (o->properties != NULL)
1330 g_print ("%*s properties:\n", indent, "");
1331 for (n = 0; o->properties[n] != NULL; n++)
1333 dump_property (o->properties[n],
1335 g_hash_table_lookup (properties, (o->properties[n])->name));
1341 g_hash_table_unref (properties);
1345 introspect_do (GDBusConnection *c,
1346 const gchar *object_path,
1350 dump_node (GDBusConnection *c,
1352 const GDBusNodeInfo *o,
1354 const gchar *object_path,
1358 const gchar *object_path_to_print;
1360 object_path_to_print = object_path;
1361 if (o->path != NULL)
1362 object_path_to_print = o->path;
1364 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1365 dump_annotation (o->annotations[n], indent, FALSE);
1367 g_print ("%*snode %s", indent, "", object_path_to_print != NULL ? object_path_to_print : "(not set)");
1368 if (o->interfaces != NULL || o->nodes != NULL)
1371 for (n = 0; o->interfaces != NULL && o->interfaces[n] != NULL; n++)
1373 if (opt_introspect_only_properties)
1375 if (o->interfaces[n]->properties != NULL && o->interfaces[n]->properties[0] != NULL)
1376 dump_interface (c, name, o->interfaces[n], indent + 2, object_path);
1380 dump_interface (c, name, o->interfaces[n], indent + 2, object_path);
1383 for (n = 0; o->nodes != NULL && o->nodes[n] != NULL; n++)
1388 if (g_variant_is_object_path (o->nodes[n]->path))
1390 child_path = g_strdup (o->nodes[n]->path);
1391 /* avoid infinite loops */
1392 if (g_str_has_prefix (child_path, object_path))
1394 introspect_do (c, child_path, indent + 2);
1398 g_print ("Skipping path %s that is not enclosed by parent %s\n",
1399 child_path, object_path);
1404 if (g_strcmp0 (object_path, "/") == 0)
1405 child_path = g_strdup_printf ("/%s", o->nodes[n]->path);
1407 child_path = g_strdup_printf ("%s/%s", object_path, o->nodes[n]->path);
1408 introspect_do (c, child_path, indent + 2);
1410 g_free (child_path);
1414 dump_node (NULL, NULL, o->nodes[n], indent + 2, NULL, recurse);
1426 static const GOptionEntry introspect_entries[] =
1428 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_introspect_dest, N_("Destination name to introspect"), NULL},
1429 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_introspect_object_path, N_("Object path to introspect"), NULL},
1430 { "xml", 'x', 0, G_OPTION_ARG_NONE, &opt_introspect_xml, N_("Print XML"), NULL},
1431 { "recurse", 'r', 0, G_OPTION_ARG_NONE, &opt_introspect_recurse, N_("Introspect children"), NULL},
1432 { "only-properties", 'p', 0, G_OPTION_ARG_NONE, &opt_introspect_only_properties, N_("Only print properties"), NULL},
1437 introspect_do (GDBusConnection *c,
1438 const gchar *object_path,
1443 GDBusNodeInfo *node;
1445 const gchar *xml_data;
1452 result = g_dbus_connection_call_sync (c,
1453 opt_introspect_dest,
1455 "org.freedesktop.DBus.Introspectable",
1458 G_VARIANT_TYPE ("(s)"),
1459 G_DBUS_CALL_FLAGS_NONE,
1465 g_printerr (_("Error: %s\n"), error->message);
1466 g_error_free (error);
1469 g_variant_get (result, "(&s)", &xml_data);
1471 if (opt_introspect_xml)
1473 g_print ("%s", xml_data);
1478 node = g_dbus_node_info_new_for_xml (xml_data, &error);
1481 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1482 g_error_free (error);
1486 dump_node (c, opt_introspect_dest, node, indent, object_path, opt_introspect_recurse);
1493 g_dbus_node_info_unref (node);
1495 g_variant_unref (result);
1500 handle_introspect (gint *argc,
1502 gboolean request_completion,
1503 const gchar *completion_cur,
1504 const gchar *completion_prev)
1511 gboolean complete_names;
1512 gboolean complete_paths;
1517 modify_argv0_for_command (argc, argv, "introspect");
1519 o = g_option_context_new (NULL);
1520 if (request_completion)
1521 g_option_context_set_ignore_unknown_options (o, TRUE);
1522 g_option_context_set_help_enabled (o, FALSE);
1523 g_option_context_set_summary (o, _("Introspect a remote object."));
1524 g_option_context_add_main_entries (o, introspect_entries, GETTEXT_PACKAGE);
1525 g_option_context_add_group (o, connection_get_group ());
1527 complete_names = FALSE;
1528 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1530 complete_names = TRUE;
1531 remove_arg ((*argc) - 1, argc, argv);
1534 complete_paths = FALSE;
1535 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1537 complete_paths = TRUE;
1538 remove_arg ((*argc) - 1, argc, argv);
1541 if (!g_option_context_parse (o, argc, argv, NULL))
1543 if (!request_completion)
1545 s = g_option_context_get_help (o, FALSE, NULL);
1546 g_printerr ("%s", s);
1553 c = connection_get_dbus_connection (&error);
1556 if (request_completion)
1558 if (g_strcmp0 (completion_prev, "--address") == 0)
1566 g_print ("--system \n--session \n--address \n");
1571 g_printerr (_("Error connecting: %s\n"), error->message);
1572 g_error_free (error);
1577 if (g_dbus_connection_get_unique_name (c) != NULL)
1581 print_names (c, FALSE);
1584 /* this only makes sense on message bus connections */
1585 if (opt_introspect_dest == NULL)
1587 if (request_completion)
1588 g_print ("--dest \n");
1590 g_printerr (_("Error: Destination is not specified\n"));
1593 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1595 print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1601 print_paths (c, opt_introspect_dest, "/");
1604 if (opt_introspect_object_path == NULL)
1606 if (request_completion)
1607 g_print ("--object-path \n");
1609 g_printerr (_("Error: Object path is not specified\n"));
1612 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1615 s = g_strdup (opt_introspect_object_path);
1616 p = strrchr (s, '/');
1623 print_paths (c, opt_introspect_dest, s);
1627 if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1629 g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1633 if (request_completion && opt_introspect_object_path != NULL && !opt_introspect_recurse)
1635 g_print ("--recurse \n");
1638 if (request_completion && opt_introspect_object_path != NULL && !opt_introspect_only_properties)
1640 g_print ("--only-properties \n");
1643 /* All done with completion now */
1644 if (request_completion)
1647 if (!introspect_do (c, opt_introspect_object_path, 0))
1655 g_option_context_free (o);
1659 /* ---------------------------------------------------------------------------------------------------- */
1661 static gchar *opt_monitor_dest = NULL;
1662 static gchar *opt_monitor_object_path = NULL;
1664 static guint monitor_filter_id = 0;
1667 monitor_signal_cb (GDBusConnection *connection,
1668 const gchar *sender_name,
1669 const gchar *object_path,
1670 const gchar *interface_name,
1671 const gchar *signal_name,
1672 GVariant *parameters,
1676 s = g_variant_print (parameters, TRUE);
1677 g_print ("%s: %s.%s %s\n",
1686 monitor_on_name_appeared (GDBusConnection *connection,
1688 const gchar *name_owner,
1691 g_print ("The name %s is owned by %s\n", name, name_owner);
1692 g_assert (monitor_filter_id == 0);
1693 monitor_filter_id = g_dbus_connection_signal_subscribe (connection,
1695 NULL, /* any interface */
1696 NULL, /* any member */
1697 opt_monitor_object_path,
1699 G_DBUS_SIGNAL_FLAGS_NONE,
1701 NULL, /* user_data */
1702 NULL); /* user_data destroy notify */
1706 monitor_on_name_vanished (GDBusConnection *connection,
1710 g_print ("The name %s does not have an owner\n", name);
1712 if (monitor_filter_id != 0)
1714 g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1715 monitor_filter_id = 0;
1719 static const GOptionEntry monitor_entries[] =
1721 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_monitor_dest, N_("Destination name to monitor"), NULL},
1722 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_monitor_object_path, N_("Object path to monitor"), NULL},
1727 handle_monitor (gint *argc,
1729 gboolean request_completion,
1730 const gchar *completion_cur,
1731 const gchar *completion_prev)
1739 GDBusNodeInfo *node;
1740 gboolean complete_names;
1741 gboolean complete_paths;
1749 modify_argv0_for_command (argc, argv, "monitor");
1751 o = g_option_context_new (NULL);
1752 if (request_completion)
1753 g_option_context_set_ignore_unknown_options (o, TRUE);
1754 g_option_context_set_help_enabled (o, FALSE);
1755 g_option_context_set_summary (o, _("Monitor a remote object."));
1756 g_option_context_add_main_entries (o, monitor_entries, GETTEXT_PACKAGE);
1757 g_option_context_add_group (o, connection_get_group ());
1759 complete_names = FALSE;
1760 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1762 complete_names = TRUE;
1763 remove_arg ((*argc) - 1, argc, argv);
1766 complete_paths = FALSE;
1767 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1769 complete_paths = TRUE;
1770 remove_arg ((*argc) - 1, argc, argv);
1773 if (!g_option_context_parse (o, argc, argv, NULL))
1775 if (!request_completion)
1777 s = g_option_context_get_help (o, FALSE, NULL);
1778 g_printerr ("%s", s);
1785 c = connection_get_dbus_connection (&error);
1788 if (request_completion)
1790 if (g_strcmp0 (completion_prev, "--address") == 0)
1798 g_print ("--system \n--session \n--address \n");
1803 g_printerr (_("Error connecting: %s\n"), error->message);
1804 g_error_free (error);
1809 if (g_dbus_connection_get_unique_name (c) != NULL)
1813 print_names (c, FALSE);
1816 /* this only makes sense on message bus connections */
1817 if (opt_monitor_dest == NULL)
1819 if (request_completion)
1820 g_print ("--dest \n");
1822 g_printerr (_("Error: Destination is not specified\n"));
1825 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1827 print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1833 print_paths (c, opt_monitor_dest, "/");
1836 if (opt_monitor_object_path == NULL)
1838 if (request_completion)
1840 g_print ("--object-path \n");
1843 /* it's fine to not have an object path */
1845 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1848 s = g_strdup (opt_monitor_object_path);
1849 p = strrchr (s, '/');
1856 print_paths (c, opt_monitor_dest, s);
1860 if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1862 g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1866 /* All done with completion now */
1867 if (request_completion)
1870 if (opt_monitor_object_path != NULL)
1871 g_print ("Monitoring signals on object %s owned by %s\n", opt_monitor_object_path, opt_monitor_dest);
1873 g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1875 loop = g_main_loop_new (NULL, FALSE);
1876 g_bus_watch_name_on_connection (c,
1878 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1879 monitor_on_name_appeared,
1880 monitor_on_name_vanished,
1884 g_main_loop_run (loop);
1885 g_main_loop_unref (loop);
1891 g_dbus_node_info_unref (node);
1893 g_variant_unref (result);
1896 g_option_context_free (o);
1900 /* ---------------------------------------------------------------------------------------------------- */
1903 pick_word_at (const gchar *s,
1905 gint *out_word_begins_at)
1912 if (out_word_begins_at != NULL)
1913 *out_word_begins_at = -1;
1917 if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1919 if (out_word_begins_at != NULL)
1920 *out_word_begins_at = cursor;
1921 return g_strdup ("");
1924 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1929 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1932 if (out_word_begins_at != NULL)
1933 *out_word_begins_at = begin;
1935 return g_strndup (s + begin, end - begin);
1939 main (gint argc, gchar *argv[])
1942 const gchar *command;
1943 gboolean request_completion;
1944 gchar *completion_cur;
1945 gchar *completion_prev;
1950 setlocale (LC_ALL, "");
1951 textdomain (GETTEXT_PACKAGE);
1954 tmp = _glib_get_locale_dir ();
1955 bindtextdomain (GETTEXT_PACKAGE, tmp);
1958 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1961 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1962 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1966 completion_cur = NULL;
1967 completion_prev = NULL;
1971 usage (&argc, &argv, FALSE);
1975 request_completion = FALSE;
1977 //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1981 if (g_strcmp0 (command, "help") == 0)
1983 if (request_completion)
1989 usage (&argc, &argv, TRUE);
1994 else if (g_strcmp0 (command, "emit") == 0)
1996 if (handle_emit (&argc,
2004 else if (g_strcmp0 (command, "call") == 0)
2006 if (handle_call (&argc,
2014 else if (g_strcmp0 (command, "introspect") == 0)
2016 if (handle_introspect (&argc,
2024 else if (g_strcmp0 (command, "monitor") == 0)
2026 if (handle_monitor (&argc,
2034 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
2036 const gchar *completion_line;
2037 gchar **completion_argv;
2038 gint completion_argc;
2039 gint completion_point;
2043 request_completion = TRUE;
2045 completion_line = argv[2];
2046 completion_point = strtol (argv[3], &endp, 10);
2047 if (endp == argv[3] || *endp != '\0')
2051 completion_debug ("completion_point=%d", completion_point);
2052 completion_debug ("----");
2053 completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789");
2054 completion_debug ("'%s'", completion_line);
2055 completion_debug (" %*s^",
2056 completion_point, "");
2057 completion_debug ("----");
2060 if (!g_shell_parse_argv (completion_line,
2065 /* it's very possible the command line can't be parsed (for
2066 * example, missing quotes etc) - in that case, we just
2067 * don't autocomplete at all
2072 /* compute cur and prev */
2073 completion_prev = NULL;
2074 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
2078 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
2080 if (!g_ascii_isspace (completion_line[prev_end]))
2082 completion_prev = pick_word_at (completion_line, prev_end, NULL);
2088 completion_debug (" cur='%s'", completion_cur);
2089 completion_debug ("prev='%s'", completion_prev);
2092 argc = completion_argc;
2093 argv = completion_argv;
2101 if (request_completion)
2103 g_print ("help \nemit \ncall \nintrospect \nmonitor \n");
2109 g_printerr ("Unknown command '%s'\n", command);
2110 usage (&argc, &argv, FALSE);
2116 g_free (completion_cur);
2117 g_free (completion_prev);