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_VARIANT_TYPE ("(s)"),
154 G_DBUS_CALL_FLAGS_NONE,
160 g_printerr (_("Error: %s\n"), error->message);
161 g_error_free (error);
164 g_variant_get (result, "(&s)", &xml_data);
167 node = g_dbus_node_info_new_for_xml (xml_data, &error);
168 g_variant_unref (result);
171 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
172 g_error_free (error);
176 for (n = 0; node->interfaces != NULL && node->interfaces[n] != NULL; n++)
178 const GDBusInterfaceInfo *iface = node->interfaces[n];
179 for (m = 0; iface->methods != NULL && iface->methods[m] != NULL; m++)
181 const GDBusMethodInfo *method = iface->methods[m];
182 g_print ("%s.%s \n", iface->name, method->name);
185 g_dbus_node_info_unref (node);
192 print_paths (GDBusConnection *c,
198 const gchar *xml_data;
203 result = g_dbus_connection_call_sync (c,
206 "org.freedesktop.DBus.Introspectable",
209 G_VARIANT_TYPE ("(s)"),
210 G_DBUS_CALL_FLAGS_NONE,
216 g_printerr (_("Error: %s\n"), error->message);
217 g_error_free (error);
220 g_variant_get (result, "(&s)", &xml_data);
222 //g_printerr ("xml=`%s'", xml_data);
225 node = g_dbus_node_info_new_for_xml (xml_data, &error);
226 g_variant_unref (result);
229 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
230 g_error_free (error);
234 //g_printerr ("bar `%s'\n", path);
236 if (node->interfaces != NULL)
237 g_print ("%s \n", path);
239 for (n = 0; node->nodes != NULL && node->nodes[n] != NULL; n++)
243 //g_printerr ("foo `%s'\n", node->nodes[n].path);
245 if (g_strcmp0 (path, "/") == 0)
246 s = g_strdup_printf ("/%s", node->nodes[n]->path);
248 s = g_strdup_printf ("%s/%s", path, node->nodes[n]->path);
250 print_paths (c, name, s);
254 g_dbus_node_info_unref (node);
261 print_names (GDBusConnection *c,
262 gboolean include_unique_names)
268 GHashTable *name_set;
272 name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
275 result = g_dbus_connection_call_sync (c,
276 "org.freedesktop.DBus",
277 "/org/freedesktop/DBus",
278 "org.freedesktop.DBus",
281 G_VARIANT_TYPE ("(as)"),
282 G_DBUS_CALL_FLAGS_NONE,
288 g_printerr (_("Error: %s\n"), error->message);
289 g_error_free (error);
292 g_variant_get (result, "(as)", &iter);
293 while (g_variant_iter_loop (iter, "s", &str))
294 g_hash_table_insert (name_set, g_strdup (str), NULL);
295 g_variant_iter_free (iter);
296 g_variant_unref (result);
299 result = g_dbus_connection_call_sync (c,
300 "org.freedesktop.DBus",
301 "/org/freedesktop/DBus",
302 "org.freedesktop.DBus",
303 "ListActivatableNames",
305 G_VARIANT_TYPE ("(as)"),
306 G_DBUS_CALL_FLAGS_NONE,
312 g_printerr (_("Error: %s\n"), error->message);
313 g_error_free (error);
316 g_variant_get (result, "(as)", &iter);
317 while (g_variant_iter_loop (iter, "s", &str))
318 g_hash_table_insert (name_set, g_strdup (str), NULL);
319 g_variant_iter_free (iter);
320 g_variant_unref (result);
322 keys = g_hash_table_get_keys (name_set);
323 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
324 for (l = keys; l != NULL; l = l->next)
326 const gchar *name = l->data;
327 if (!include_unique_names && g_str_has_prefix (name, ":"))
330 g_print ("%s \n", name);
335 g_hash_table_unref (name_set);
338 /* ---------------------------------------------------------------------------------------------------- */
340 static gboolean opt_connection_system = FALSE;
341 static gboolean opt_connection_session = FALSE;
342 static gchar *opt_connection_address = NULL;
344 static const GOptionEntry connection_entries[] =
346 { "system", 'y', 0, G_OPTION_ARG_NONE, &opt_connection_system, N_("Connect to the system bus"), NULL},
347 { "session", 'e', 0, G_OPTION_ARG_NONE, &opt_connection_session, N_("Connect to the session bus"), NULL},
348 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_connection_address, N_("Connect to given D-Bus address"), NULL},
352 static GOptionGroup *
353 connection_get_group (void)
355 static GOptionGroup *g;
357 g = g_option_group_new ("connection",
358 N_("Connection Endpoint Options:"),
359 N_("Options specifying the connection endpoint"),
362 g_option_group_add_entries (g, connection_entries);
366 static GDBusConnection *
367 connection_get_dbus_connection (GError **error)
373 /* First, ensure we have exactly one connect */
374 if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
379 _("No connection endpoint specified"));
382 else if ((opt_connection_system && (opt_connection_session || opt_connection_address != NULL)) ||
383 (opt_connection_session && (opt_connection_system || opt_connection_address != NULL)) ||
384 (opt_connection_address != NULL && (opt_connection_system || opt_connection_session)))
389 _("Multiple connection endpoints specified"));
393 if (opt_connection_system)
395 c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
397 else if (opt_connection_session)
399 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
401 else if (opt_connection_address != NULL)
403 c = g_dbus_connection_new_for_address_sync (opt_connection_address,
404 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
405 NULL, /* GDBusAuthObserver */
406 NULL, /* GCancellable */
414 /* ---------------------------------------------------------------------------------------------------- */
417 call_helper_get_method_in_signature (GDBusConnection *c,
420 const gchar *interface_name,
421 const gchar *method_name,
426 GDBusNodeInfo *node_info;
427 const gchar *xml_data;
428 const GDBusInterfaceInfo *interface_info;
429 const GDBusMethodInfo *method_info;
436 result = g_dbus_connection_call_sync (c,
439 "org.freedesktop.DBus.Introspectable",
442 G_VARIANT_TYPE ("(s)"),
443 G_DBUS_CALL_FLAGS_NONE,
450 g_variant_get (result, "(&s)", &xml_data);
451 node_info = g_dbus_node_info_new_for_xml (xml_data, error);
452 if (node_info == NULL)
455 interface_info = g_dbus_node_info_lookup_interface (node_info, interface_name);
456 if (interface_info == NULL)
458 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
459 _("Warning: According to introspection data, interface `%s' does not exist\n"),
464 method_info = g_dbus_interface_info_lookup_method (interface_info, method_name);
465 if (method_info == NULL)
467 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
468 _("Warning: According to introspection data, method `%s' does not exist on interface `%s'\n"),
474 ret = g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_type_free);
475 for (n = 0; method_info->in_args != NULL && method_info->in_args[n] != NULL; n++)
477 g_ptr_array_add (ret, g_variant_type_new (method_info->in_args[n]->signature));
481 if (node_info != NULL)
482 g_dbus_node_info_unref (node_info);
484 g_variant_unref (result);
489 /* ---------------------------------------------------------------------------------------------------- */
492 _g_variant_parse_me_harder (GVariantType *type,
493 const gchar *given_str,
501 str = g_string_new ("\"");
502 for (n = 0; given_str[n] != '\0'; n++)
504 if (G_UNLIKELY (given_str[n] == '\"'))
505 g_string_append (str, "\\\"");
507 g_string_append_c (str, given_str[n]);
509 g_string_append_c (str, '"');
510 s = g_string_free (str, FALSE);
512 value = g_variant_parse (type,
522 /* ---------------------------------------------------------------------------------------------------- */
524 static gchar *opt_call_dest = NULL;
525 static gchar *opt_call_object_path = NULL;
526 static gchar *opt_call_method = NULL;
528 static const GOptionEntry call_entries[] =
530 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_call_dest, N_("Destination name to invoke method on"), NULL},
531 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_call_object_path, N_("Object path to invoke method on"), NULL},
532 { "method", 'm', 0, G_OPTION_ARG_STRING, &opt_call_method, N_("Method and interface name"), NULL},
537 handle_call (gint *argc,
539 gboolean request_completion,
540 const gchar *completion_cur,
541 const gchar *completion_prev)
548 GVariant *parameters;
549 gchar *interface_name;
552 GPtrArray *in_signature_types;
553 gboolean complete_names;
554 gboolean complete_paths;
555 gboolean complete_methods;
556 GVariantBuilder builder;
562 interface_name = NULL;
565 in_signature_types = NULL;
567 modify_argv0_for_command (argc, argv, "call");
569 o = g_option_context_new (NULL);
570 g_option_context_set_help_enabled (o, FALSE);
571 g_option_context_set_summary (o, _("Invoke a method on a remote object."));
572 g_option_context_add_main_entries (o, call_entries, NULL /* GETTEXT_PACKAGE*/);
573 g_option_context_add_group (o, connection_get_group ());
575 complete_names = FALSE;
576 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
578 complete_names = TRUE;
579 remove_arg ((*argc) - 1, argc, argv);
582 complete_paths = FALSE;
583 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
585 complete_paths = TRUE;
586 remove_arg ((*argc) - 1, argc, argv);
589 complete_methods = FALSE;
590 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--method") == 0)
592 complete_methods = TRUE;
593 remove_arg ((*argc) - 1, argc, argv);
596 if (!g_option_context_parse (o, argc, argv, NULL))
598 if (!request_completion)
600 s = g_option_context_get_help (o, FALSE, NULL);
601 g_printerr ("%s", s);
608 c = connection_get_dbus_connection (&error);
611 if (request_completion)
613 if (g_strcmp0 (completion_prev, "--address") == 0)
621 g_print ("--system \n--session \n--address \n");
626 g_printerr (_("Error connecting: %s\n"), error->message);
627 g_error_free (error);
632 /* validate and complete destination (bus name) */
633 if (g_dbus_connection_get_unique_name (c) != NULL)
635 /* this only makes sense on message bus connections */
638 print_names (c, FALSE);
641 if (opt_call_dest == NULL)
643 if (request_completion)
644 g_print ("--dest \n");
646 g_printerr (_("Error: Destination is not specified\n"));
649 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
651 print_names (c, g_str_has_prefix (opt_call_dest, ":"));
656 /* validate and complete object path */
659 print_paths (c, opt_call_dest, "/");
662 if (opt_call_object_path == NULL)
664 if (request_completion)
665 g_print ("--object-path \n");
667 g_printerr (_("Error: Object path is not specified\n"));
670 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
673 s = g_strdup (opt_call_object_path);
674 p = strrchr (s, '/');
681 print_paths (c, opt_call_dest, s);
685 if (!request_completion && !g_variant_is_object_path (opt_call_object_path))
687 g_printerr (_("Error: %s is not a valid object path\n"), opt_call_object_path);
691 /* validate and complete method (interface + method name) */
692 if (complete_methods)
694 print_methods (c, opt_call_dest, opt_call_object_path);
697 if (opt_call_method == NULL)
699 if (request_completion)
700 g_print ("--method \n");
702 g_printerr (_("Error: Method name is not specified\n"));
705 if (request_completion && g_strcmp0 ("--method", completion_prev) == 0)
707 print_methods (c, opt_call_dest, opt_call_object_path);
710 s = strrchr (opt_call_method, '.');
711 if (!request_completion && s == NULL)
713 g_printerr (_("Error: Method name `%s' is invalid\n"), opt_call_method);
716 method_name = g_strdup (s + 1);
717 interface_name = g_strndup (opt_call_method, s - opt_call_method);
719 /* All done with completion now */
720 if (request_completion)
723 /* Introspect, for easy conversion - it's not fatal if we can't do this */
724 in_signature_types = call_helper_get_method_in_signature (c,
726 opt_call_object_path,
730 if (in_signature_types == NULL)
732 //g_printerr ("Error getting introspection data: %s\n", error->message);
733 g_error_free (error);
737 /* Read parameters */
738 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
739 for (n = 1; n < (guint) *argc; n++)
745 if (in_signature_types != NULL)
747 if (n - 1 >= in_signature_types->len)
749 /* Only warn for the first param */
750 if (n - 1 == in_signature_types->len)
752 g_printerr ("Warning: Introspection data indicates %d parameters but more was passed\n",
753 in_signature_types->len);
758 type = in_signature_types->pdata[n - 1];
763 value = g_variant_parse (type,
770 g_error_free (error);
772 value = _g_variant_parse_me_harder (type, (*argv)[n], &error);
777 s = g_variant_type_dup_string (type);
778 g_printerr (_("Error parsing parameter %d of type `%s': %s\n"),
786 g_printerr (_("Error parsing parameter %d: %s\n"),
790 g_error_free (error);
791 g_variant_builder_clear (&builder);
795 g_variant_builder_add_value (&builder, value);
797 parameters = g_variant_builder_end (&builder);
799 if (parameters != NULL)
800 parameters = g_variant_ref_sink (parameters);
801 result = g_dbus_connection_call_sync (c,
803 opt_call_object_path,
808 G_DBUS_CALL_FLAGS_NONE,
814 g_printerr (_("Error: %s\n"), error->message);
815 g_error_free (error);
816 if (in_signature_types != NULL)
819 s = g_string_new (NULL);
820 for (n = 0; n < in_signature_types->len; n++)
822 GVariantType *type = in_signature_types->pdata[n];
823 g_string_append_len (s,
824 g_variant_type_peek_string (type),
825 g_variant_type_get_string_length (type));
827 g_printerr ("(According to introspection data, you need to pass `%s')\n", s->str);
828 g_string_free (s, TRUE);
833 s = g_variant_print (result, TRUE);
840 if (in_signature_types != NULL)
841 g_ptr_array_unref (in_signature_types);
843 g_variant_unref (result);
846 if (parameters != NULL)
847 g_variant_unref (parameters);
848 g_free (interface_name);
849 g_free (method_name);
850 g_option_context_free (o);
854 /* ---------------------------------------------------------------------------------------------------- */
856 /* TODO: dump annotations */
859 dump_annotation (const GDBusAnnotationInfo *o,
861 gboolean ignore_indent)
864 g_print ("%*s@%s(\"%s\")\n",
865 ignore_indent ? 0 : indent, "",
868 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
869 dump_annotation (o->annotations[n], indent + 2, FALSE);
873 dump_arg (const GDBusArgInfo *o,
875 const gchar *direction,
876 gboolean ignore_indent,
877 gboolean include_newline)
881 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
883 dump_annotation (o->annotations[n], indent, ignore_indent);
884 ignore_indent = FALSE;
887 g_print ("%*s%s%s %s%s",
888 ignore_indent ? 0 : indent, "",
892 include_newline ? ",\n" : "");
896 count_args (GDBusArgInfo **args)
902 while (args[n] != NULL)
909 dump_method (const GDBusMethodInfo *o,
915 guint total_num_args;
917 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
918 dump_annotation (o->annotations[n], indent, FALSE);
920 g_print ("%*s%s(", indent, "", o->name);
921 name_len = strlen (o->name);
922 total_num_args = count_args (o->in_args) + count_args (o->out_args);
923 for (n = 0, m = 0; o->in_args != NULL && o->in_args[n] != NULL; n++, m++)
925 gboolean ignore_indent = (m == 0);
926 gboolean include_newline = (m != total_num_args - 1);
928 dump_arg (o->in_args[n],
929 indent + name_len + 1,
934 for (n = 0; o->out_args != NULL && o->out_args[n] != NULL; n++, m++)
936 gboolean ignore_indent = (m == 0);
937 gboolean include_newline = (m != total_num_args - 1);
938 dump_arg (o->out_args[n],
939 indent + name_len + 1,
948 dump_signal (const GDBusSignalInfo *o,
953 guint total_num_args;
955 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
956 dump_annotation (o->annotations[n], indent, FALSE);
958 g_print ("%*s%s(", indent, "", o->name);
959 name_len = strlen (o->name);
960 total_num_args = count_args (o->args);
961 for (n = 0; o->args != NULL && o->args[n] != NULL; n++)
963 gboolean ignore_indent = (n == 0);
964 gboolean include_newline = (n != total_num_args - 1);
965 dump_arg (o->args[n],
966 indent + name_len + 1,
975 dump_property (const GDBusPropertyInfo *o,
982 if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
984 else if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
985 access = "writeonly";
986 else if (o->flags == (G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
987 access = "readwrite";
989 g_assert_not_reached ();
991 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
992 dump_annotation (o->annotations[n], indent, FALSE);
996 gchar *s = g_variant_print (value, FALSE);
997 g_print ("%*s%s %s %s = %s;\n", indent, "", access, o->signature, o->name, s);
1002 g_print ("%*s%s %s %s;\n", indent, "", access, o->signature, o->name);
1007 dump_interface (GDBusConnection *c,
1009 const GDBusInterfaceInfo *o,
1011 const gchar *object_path)
1014 GHashTable *properties;
1016 properties = g_hash_table_new_full (g_str_hash,
1019 (GDestroyNotify) g_variant_unref);
1021 /* Try to get properties */
1022 if (c != NULL && name != NULL && object_path != NULL && o->properties != NULL)
1025 result = g_dbus_connection_call_sync (c,
1028 "org.freedesktop.DBus.Properties",
1030 g_variant_new ("(s)", o->name),
1032 G_DBUS_CALL_FLAGS_NONE,
1038 if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1042 g_variant_get (result,
1045 while ((item = g_variant_iter_next_value (iter)))
1049 g_variant_get (item,
1054 g_hash_table_insert (properties, key, g_variant_ref (value));
1057 g_variant_unref (result);
1062 for (n = 0; o->properties != NULL && o->properties[n] != NULL; n++)
1064 result = g_dbus_connection_call_sync (c,
1067 "org.freedesktop.DBus.Properties",
1069 g_variant_new ("(ss)", o->name, o->properties[n]->name),
1070 G_VARIANT_TYPE ("(v)"),
1071 G_DBUS_CALL_FLAGS_NONE,
1077 GVariant *property_value;
1078 g_variant_get (result,
1081 g_hash_table_insert (properties,
1082 g_strdup (o->properties[n]->name),
1083 g_variant_ref (property_value));
1084 g_variant_unref (result);
1090 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1091 dump_annotation (o->annotations[n], indent, FALSE);
1093 g_print ("%*sinterface %s {\n", indent, "", o->name);
1094 if (o->methods != NULL)
1096 g_print ("%*s methods:\n", indent, "");
1097 for (n = 0; o->methods[n] != NULL; n++)
1098 dump_method (o->methods[n], indent + 4);
1100 if (o->signals != NULL)
1102 g_print ("%*s signals:\n", indent, "");
1103 for (n = 0; o->signals[n] != NULL; n++)
1104 dump_signal (o->signals[n], indent + 4);
1106 if (o->properties != NULL)
1108 g_print ("%*s properties:\n", indent, "");
1109 for (n = 0; o->properties[n] != NULL; n++)
1111 dump_property (o->properties[n],
1113 g_hash_table_lookup (properties, (o->properties[n])->name));
1119 g_hash_table_unref (properties);
1123 dump_node (GDBusConnection *c,
1125 const GDBusNodeInfo *o,
1127 const gchar *object_path)
1130 const gchar *object_path_to_print;
1132 object_path_to_print = object_path;
1133 if (o->path != NULL)
1134 object_path_to_print = o->path;
1136 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1137 dump_annotation (o->annotations[n], indent, FALSE);
1139 g_print ("%*snode %s", indent, "", object_path_to_print != NULL ? object_path_to_print : "(not set)");
1140 if (o->interfaces != NULL || o->nodes != NULL)
1143 for (n = 0; o->interfaces != NULL && o->interfaces[n] != NULL; n++)
1144 dump_interface (c, name, o->interfaces[n], indent + 2, object_path);
1145 for (n = 0; o->nodes != NULL && o->nodes[n] != NULL; n++)
1146 dump_node (NULL, NULL, o->nodes[n], indent + 2, NULL);
1156 static gchar *opt_introspect_dest = NULL;
1157 static gchar *opt_introspect_object_path = NULL;
1159 static const GOptionEntry introspect_entries[] =
1161 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_introspect_dest, N_("Destination name to introspect"), NULL},
1162 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_introspect_object_path, N_("Object path to introspect"), NULL},
1167 handle_introspect (gint *argc,
1169 gboolean request_completion,
1170 const gchar *completion_cur,
1171 const gchar *completion_prev)
1179 const gchar *xml_data;
1180 GDBusNodeInfo *node;
1181 gboolean complete_names;
1182 gboolean complete_paths;
1189 modify_argv0_for_command (argc, argv, "introspect");
1191 o = g_option_context_new (NULL);
1192 if (request_completion)
1193 g_option_context_set_ignore_unknown_options (o, TRUE);
1194 g_option_context_set_help_enabled (o, FALSE);
1195 g_option_context_set_summary (o, _("Introspect a remote object."));
1196 g_option_context_add_main_entries (o, introspect_entries, NULL /* GETTEXT_PACKAGE*/);
1197 g_option_context_add_group (o, connection_get_group ());
1199 complete_names = FALSE;
1200 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1202 complete_names = TRUE;
1203 remove_arg ((*argc) - 1, argc, argv);
1206 complete_paths = FALSE;
1207 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1209 complete_paths = TRUE;
1210 remove_arg ((*argc) - 1, argc, argv);
1213 if (!g_option_context_parse (o, argc, argv, NULL))
1215 if (!request_completion)
1217 s = g_option_context_get_help (o, FALSE, NULL);
1218 g_printerr ("%s", s);
1225 c = connection_get_dbus_connection (&error);
1228 if (request_completion)
1230 if (g_strcmp0 (completion_prev, "--address") == 0)
1238 g_print ("--system \n--session \n--address \n");
1243 g_printerr (_("Error connecting: %s\n"), error->message);
1244 g_error_free (error);
1249 if (g_dbus_connection_get_unique_name (c) != NULL)
1253 print_names (c, FALSE);
1256 /* this only makes sense on message bus connections */
1257 if (opt_introspect_dest == NULL)
1259 if (request_completion)
1260 g_print ("--dest \n");
1262 g_printerr (_("Error: Destination is not specified\n"));
1265 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1267 print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1273 print_paths (c, opt_introspect_dest, "/");
1276 if (opt_introspect_object_path == NULL)
1278 if (request_completion)
1279 g_print ("--object-path \n");
1281 g_printerr (_("Error: Object path is not specified\n"));
1284 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1287 s = g_strdup (opt_introspect_object_path);
1288 p = strrchr (s, '/');
1295 print_paths (c, opt_introspect_dest, s);
1299 if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1301 g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1305 /* All done with completion now */
1306 if (request_completion)
1309 result = g_dbus_connection_call_sync (c,
1310 opt_introspect_dest,
1311 opt_introspect_object_path,
1312 "org.freedesktop.DBus.Introspectable",
1315 G_VARIANT_TYPE ("(s)"),
1316 G_DBUS_CALL_FLAGS_NONE,
1322 g_printerr (_("Error: %s\n"), error->message);
1323 g_error_free (error);
1326 g_variant_get (result, "(&s)", &xml_data);
1329 node = g_dbus_node_info_new_for_xml (xml_data, &error);
1332 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1333 g_error_free (error);
1337 dump_node (c, opt_introspect_dest, node, 0, opt_introspect_object_path);
1343 g_dbus_node_info_unref (node);
1345 g_variant_unref (result);
1348 g_option_context_free (o);
1352 /* ---------------------------------------------------------------------------------------------------- */
1354 static gchar *opt_monitor_dest = NULL;
1355 static gchar *opt_monitor_object_path = NULL;
1357 static guint monitor_filter_id = 0;
1360 monitor_signal_cb (GDBusConnection *connection,
1361 const gchar *sender_name,
1362 const gchar *object_path,
1363 const gchar *interface_name,
1364 const gchar *signal_name,
1365 GVariant *parameters,
1369 s = g_variant_print (parameters, TRUE);
1370 g_print ("%s: %s.%s %s\n",
1379 monitor_on_name_appeared (GDBusConnection *connection,
1381 const gchar *name_owner,
1384 g_print ("The name %s is owned by %s\n", name, name_owner);
1385 g_assert (monitor_filter_id == 0);
1386 monitor_filter_id = g_dbus_connection_signal_subscribe (connection,
1388 NULL, /* any interface */
1389 NULL, /* any member */
1390 opt_monitor_object_path,
1393 NULL, /* user_data */
1394 NULL); /* user_data destroy notify */
1398 monitor_on_name_vanished (GDBusConnection *connection,
1402 g_print ("The name %s does not have an owner\n", name);
1404 if (monitor_filter_id != 0)
1406 g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1407 monitor_filter_id = 0;
1411 static const GOptionEntry monitor_entries[] =
1413 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_monitor_dest, N_("Destination name to monitor"), NULL},
1414 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_monitor_object_path, N_("Object path to monitor"), NULL},
1419 handle_monitor (gint *argc,
1421 gboolean request_completion,
1422 const gchar *completion_cur,
1423 const gchar *completion_prev)
1431 GDBusNodeInfo *node;
1432 gboolean complete_names;
1433 gboolean complete_paths;
1441 modify_argv0_for_command (argc, argv, "monitor");
1443 o = g_option_context_new (NULL);
1444 if (request_completion)
1445 g_option_context_set_ignore_unknown_options (o, TRUE);
1446 g_option_context_set_help_enabled (o, FALSE);
1447 g_option_context_set_summary (o, _("Monitor a remote object."));
1448 g_option_context_add_main_entries (o, monitor_entries, NULL /* GETTEXT_PACKAGE*/);
1449 g_option_context_add_group (o, connection_get_group ());
1451 complete_names = FALSE;
1452 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1454 complete_names = TRUE;
1455 remove_arg ((*argc) - 1, argc, argv);
1458 complete_paths = FALSE;
1459 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1461 complete_paths = TRUE;
1462 remove_arg ((*argc) - 1, argc, argv);
1465 if (!g_option_context_parse (o, argc, argv, NULL))
1467 if (!request_completion)
1469 s = g_option_context_get_help (o, FALSE, NULL);
1470 g_printerr ("%s", s);
1477 c = connection_get_dbus_connection (&error);
1480 if (request_completion)
1482 if (g_strcmp0 (completion_prev, "--address") == 0)
1490 g_print ("--system \n--session \n--address \n");
1495 g_printerr (_("Error connecting: %s\n"), error->message);
1496 g_error_free (error);
1501 if (g_dbus_connection_get_unique_name (c) != NULL)
1505 print_names (c, FALSE);
1508 /* this only makes sense on message bus connections */
1509 if (opt_monitor_dest == NULL)
1511 if (request_completion)
1512 g_print ("--dest \n");
1514 g_printerr (_("Error: Destination is not specified\n"));
1517 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1519 print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1525 print_paths (c, opt_monitor_dest, "/");
1528 if (opt_monitor_object_path == NULL)
1530 if (request_completion)
1532 g_print ("--object-path \n");
1535 /* it's fine to not have an object path */
1537 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1540 s = g_strdup (opt_monitor_object_path);
1541 p = strrchr (s, '/');
1548 print_paths (c, opt_monitor_dest, s);
1552 if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1554 g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1558 /* All done with completion now */
1559 if (request_completion)
1562 if (opt_monitor_object_path != NULL)
1563 g_print ("Monitoring signals on object %s owned by %s\n", opt_monitor_object_path, opt_monitor_dest);
1565 g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1567 loop = g_main_loop_new (NULL, FALSE);
1568 g_bus_watch_name_on_connection (c,
1570 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1571 monitor_on_name_appeared,
1572 monitor_on_name_vanished,
1576 g_main_loop_run (loop);
1577 g_main_loop_unref (loop);
1583 g_dbus_node_info_unref (node);
1585 g_variant_unref (result);
1588 g_option_context_free (o);
1592 /* ---------------------------------------------------------------------------------------------------- */
1595 pick_word_at (const gchar *s,
1597 gint *out_word_begins_at)
1604 if (out_word_begins_at != NULL)
1605 *out_word_begins_at = -1;
1609 if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1611 if (out_word_begins_at != NULL)
1612 *out_word_begins_at = cursor;
1613 return g_strdup ("");
1616 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1621 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1624 if (out_word_begins_at != NULL)
1625 *out_word_begins_at = begin;
1627 return g_strndup (s + begin, end - begin);
1631 main (gint argc, gchar *argv[])
1634 const gchar *command;
1635 gboolean request_completion;
1636 gchar *completion_cur;
1637 gchar *completion_prev;
1640 completion_cur = NULL;
1641 completion_prev = NULL;
1647 usage (&argc, &argv, FALSE);
1651 request_completion = FALSE;
1653 //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1657 if (g_strcmp0 (command, "help") == 0)
1659 if (request_completion)
1665 usage (&argc, &argv, TRUE);
1670 else if (g_strcmp0 (command, "call") == 0)
1672 if (handle_call (&argc,
1680 else if (g_strcmp0 (command, "introspect") == 0)
1682 if (handle_introspect (&argc,
1690 else if (g_strcmp0 (command, "monitor") == 0)
1692 if (handle_monitor (&argc,
1700 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
1702 const gchar *completion_line;
1703 gchar **completion_argv;
1704 gint completion_argc;
1705 gint completion_point;
1709 request_completion = TRUE;
1711 completion_line = argv[2];
1712 completion_point = strtol (argv[3], &endp, 10);
1713 if (endp == argv[3] || *endp != '\0')
1717 completion_debug ("completion_point=%d", completion_point);
1718 completion_debug ("----");
1719 completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789");
1720 completion_debug ("`%s'", completion_line);
1721 completion_debug (" %*s^",
1722 completion_point, "");
1723 completion_debug ("----");
1726 if (!g_shell_parse_argv (completion_line,
1731 /* it's very possible the command line can't be parsed (for
1732 * example, missing quotes etc) - in that case, we just
1733 * don't autocomplete at all
1738 /* compute cur and prev */
1739 completion_prev = NULL;
1740 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
1744 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
1746 if (!g_ascii_isspace (completion_line[prev_end]))
1748 completion_prev = pick_word_at (completion_line, prev_end, NULL);
1754 completion_debug (" cur=`%s'", completion_cur);
1755 completion_debug ("prev=`%s'", completion_prev);
1758 argc = completion_argc;
1759 argv = completion_argv;
1767 if (request_completion)
1769 g_print ("help \ncall \nintrospect \nmonitor \n");
1775 g_printerr ("Unknown command `%s'\n", command);
1776 usage (&argc, &argv, FALSE);
1782 g_free (completion_cur);
1783 g_free (completion_prev);