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 GDBusInterfaceInfo *interface_info;
429 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;
1158 static gboolean opt_introspect_xml = FALSE;
1160 static const GOptionEntry introspect_entries[] =
1162 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_introspect_dest, N_("Destination name to introspect"), NULL},
1163 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_introspect_object_path, N_("Object path to introspect"), NULL},
1164 { "xml", 'x', 0, G_OPTION_ARG_NONE, &opt_introspect_xml, N_("Print XML"), NULL},
1169 handle_introspect (gint *argc,
1171 gboolean request_completion,
1172 const gchar *completion_cur,
1173 const gchar *completion_prev)
1181 const gchar *xml_data;
1182 GDBusNodeInfo *node;
1183 gboolean complete_names;
1184 gboolean complete_paths;
1191 modify_argv0_for_command (argc, argv, "introspect");
1193 o = g_option_context_new (NULL);
1194 if (request_completion)
1195 g_option_context_set_ignore_unknown_options (o, TRUE);
1196 g_option_context_set_help_enabled (o, FALSE);
1197 g_option_context_set_summary (o, _("Introspect a remote object."));
1198 g_option_context_add_main_entries (o, introspect_entries, NULL /* GETTEXT_PACKAGE*/);
1199 g_option_context_add_group (o, connection_get_group ());
1201 complete_names = FALSE;
1202 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1204 complete_names = TRUE;
1205 remove_arg ((*argc) - 1, argc, argv);
1208 complete_paths = FALSE;
1209 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1211 complete_paths = TRUE;
1212 remove_arg ((*argc) - 1, argc, argv);
1215 if (!g_option_context_parse (o, argc, argv, NULL))
1217 if (!request_completion)
1219 s = g_option_context_get_help (o, FALSE, NULL);
1220 g_printerr ("%s", s);
1227 c = connection_get_dbus_connection (&error);
1230 if (request_completion)
1232 if (g_strcmp0 (completion_prev, "--address") == 0)
1240 g_print ("--system \n--session \n--address \n");
1245 g_printerr (_("Error connecting: %s\n"), error->message);
1246 g_error_free (error);
1251 if (g_dbus_connection_get_unique_name (c) != NULL)
1255 print_names (c, FALSE);
1258 /* this only makes sense on message bus connections */
1259 if (opt_introspect_dest == NULL)
1261 if (request_completion)
1262 g_print ("--dest \n");
1264 g_printerr (_("Error: Destination is not specified\n"));
1267 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1269 print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1275 print_paths (c, opt_introspect_dest, "/");
1278 if (opt_introspect_object_path == NULL)
1280 if (request_completion)
1281 g_print ("--object-path \n");
1283 g_printerr (_("Error: Object path is not specified\n"));
1286 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1289 s = g_strdup (opt_introspect_object_path);
1290 p = strrchr (s, '/');
1297 print_paths (c, opt_introspect_dest, s);
1301 if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1303 g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1307 /* All done with completion now */
1308 if (request_completion)
1311 result = g_dbus_connection_call_sync (c,
1312 opt_introspect_dest,
1313 opt_introspect_object_path,
1314 "org.freedesktop.DBus.Introspectable",
1317 G_VARIANT_TYPE ("(s)"),
1318 G_DBUS_CALL_FLAGS_NONE,
1324 g_printerr (_("Error: %s\n"), error->message);
1325 g_error_free (error);
1328 g_variant_get (result, "(&s)", &xml_data);
1330 if (opt_introspect_xml)
1332 g_print ("%s", xml_data);
1337 node = g_dbus_node_info_new_for_xml (xml_data, &error);
1340 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1341 g_error_free (error);
1345 dump_node (c, opt_introspect_dest, node, 0, opt_introspect_object_path);
1352 g_dbus_node_info_unref (node);
1354 g_variant_unref (result);
1357 g_option_context_free (o);
1361 /* ---------------------------------------------------------------------------------------------------- */
1363 static gchar *opt_monitor_dest = NULL;
1364 static gchar *opt_monitor_object_path = NULL;
1366 static guint monitor_filter_id = 0;
1369 monitor_signal_cb (GDBusConnection *connection,
1370 const gchar *sender_name,
1371 const gchar *object_path,
1372 const gchar *interface_name,
1373 const gchar *signal_name,
1374 GVariant *parameters,
1378 s = g_variant_print (parameters, TRUE);
1379 g_print ("%s: %s.%s %s\n",
1388 monitor_on_name_appeared (GDBusConnection *connection,
1390 const gchar *name_owner,
1393 g_print ("The name %s is owned by %s\n", name, name_owner);
1394 g_assert (monitor_filter_id == 0);
1395 monitor_filter_id = g_dbus_connection_signal_subscribe (connection,
1397 NULL, /* any interface */
1398 NULL, /* any member */
1399 opt_monitor_object_path,
1401 G_DBUS_SIGNAL_FLAGS_NONE,
1403 NULL, /* user_data */
1404 NULL); /* user_data destroy notify */
1408 monitor_on_name_vanished (GDBusConnection *connection,
1412 g_print ("The name %s does not have an owner\n", name);
1414 if (monitor_filter_id != 0)
1416 g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1417 monitor_filter_id = 0;
1421 static const GOptionEntry monitor_entries[] =
1423 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_monitor_dest, N_("Destination name to monitor"), NULL},
1424 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_monitor_object_path, N_("Object path to monitor"), NULL},
1429 handle_monitor (gint *argc,
1431 gboolean request_completion,
1432 const gchar *completion_cur,
1433 const gchar *completion_prev)
1441 GDBusNodeInfo *node;
1442 gboolean complete_names;
1443 gboolean complete_paths;
1451 modify_argv0_for_command (argc, argv, "monitor");
1453 o = g_option_context_new (NULL);
1454 if (request_completion)
1455 g_option_context_set_ignore_unknown_options (o, TRUE);
1456 g_option_context_set_help_enabled (o, FALSE);
1457 g_option_context_set_summary (o, _("Monitor a remote object."));
1458 g_option_context_add_main_entries (o, monitor_entries, NULL /* GETTEXT_PACKAGE*/);
1459 g_option_context_add_group (o, connection_get_group ());
1461 complete_names = FALSE;
1462 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1464 complete_names = TRUE;
1465 remove_arg ((*argc) - 1, argc, argv);
1468 complete_paths = FALSE;
1469 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1471 complete_paths = TRUE;
1472 remove_arg ((*argc) - 1, argc, argv);
1475 if (!g_option_context_parse (o, argc, argv, NULL))
1477 if (!request_completion)
1479 s = g_option_context_get_help (o, FALSE, NULL);
1480 g_printerr ("%s", s);
1487 c = connection_get_dbus_connection (&error);
1490 if (request_completion)
1492 if (g_strcmp0 (completion_prev, "--address") == 0)
1500 g_print ("--system \n--session \n--address \n");
1505 g_printerr (_("Error connecting: %s\n"), error->message);
1506 g_error_free (error);
1511 if (g_dbus_connection_get_unique_name (c) != NULL)
1515 print_names (c, FALSE);
1518 /* this only makes sense on message bus connections */
1519 if (opt_monitor_dest == NULL)
1521 if (request_completion)
1522 g_print ("--dest \n");
1524 g_printerr (_("Error: Destination is not specified\n"));
1527 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1529 print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1535 print_paths (c, opt_monitor_dest, "/");
1538 if (opt_monitor_object_path == NULL)
1540 if (request_completion)
1542 g_print ("--object-path \n");
1545 /* it's fine to not have an object path */
1547 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1550 s = g_strdup (opt_monitor_object_path);
1551 p = strrchr (s, '/');
1558 print_paths (c, opt_monitor_dest, s);
1562 if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1564 g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1568 /* All done with completion now */
1569 if (request_completion)
1572 if (opt_monitor_object_path != NULL)
1573 g_print ("Monitoring signals on object %s owned by %s\n", opt_monitor_object_path, opt_monitor_dest);
1575 g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1577 loop = g_main_loop_new (NULL, FALSE);
1578 g_bus_watch_name_on_connection (c,
1580 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1581 monitor_on_name_appeared,
1582 monitor_on_name_vanished,
1586 g_main_loop_run (loop);
1587 g_main_loop_unref (loop);
1593 g_dbus_node_info_unref (node);
1595 g_variant_unref (result);
1598 g_option_context_free (o);
1602 /* ---------------------------------------------------------------------------------------------------- */
1605 pick_word_at (const gchar *s,
1607 gint *out_word_begins_at)
1614 if (out_word_begins_at != NULL)
1615 *out_word_begins_at = -1;
1619 if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1621 if (out_word_begins_at != NULL)
1622 *out_word_begins_at = cursor;
1623 return g_strdup ("");
1626 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1631 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1634 if (out_word_begins_at != NULL)
1635 *out_word_begins_at = begin;
1637 return g_strndup (s + begin, end - begin);
1641 main (gint argc, gchar *argv[])
1644 const gchar *command;
1645 gboolean request_completion;
1646 gchar *completion_cur;
1647 gchar *completion_prev;
1650 completion_cur = NULL;
1651 completion_prev = NULL;
1657 usage (&argc, &argv, FALSE);
1661 request_completion = FALSE;
1663 //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1667 if (g_strcmp0 (command, "help") == 0)
1669 if (request_completion)
1675 usage (&argc, &argv, TRUE);
1680 else if (g_strcmp0 (command, "call") == 0)
1682 if (handle_call (&argc,
1690 else if (g_strcmp0 (command, "introspect") == 0)
1692 if (handle_introspect (&argc,
1700 else if (g_strcmp0 (command, "monitor") == 0)
1702 if (handle_monitor (&argc,
1710 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
1712 const gchar *completion_line;
1713 gchar **completion_argv;
1714 gint completion_argc;
1715 gint completion_point;
1719 request_completion = TRUE;
1721 completion_line = argv[2];
1722 completion_point = strtol (argv[3], &endp, 10);
1723 if (endp == argv[3] || *endp != '\0')
1727 completion_debug ("completion_point=%d", completion_point);
1728 completion_debug ("----");
1729 completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789");
1730 completion_debug ("`%s'", completion_line);
1731 completion_debug (" %*s^",
1732 completion_point, "");
1733 completion_debug ("----");
1736 if (!g_shell_parse_argv (completion_line,
1741 /* it's very possible the command line can't be parsed (for
1742 * example, missing quotes etc) - in that case, we just
1743 * don't autocomplete at all
1748 /* compute cur and prev */
1749 completion_prev = NULL;
1750 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
1754 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
1756 if (!g_ascii_isspace (completion_line[prev_end]))
1758 completion_prev = pick_word_at (completion_line, prev_end, NULL);
1764 completion_debug (" cur=`%s'", completion_cur);
1765 completion_debug ("prev=`%s'", completion_prev);
1768 argc = completion_argc;
1769 argv = completion_argv;
1777 if (request_completion)
1779 g_print ("help \ncall \nintrospect \nmonitor \n");
1785 g_printerr ("Unknown command `%s'\n", command);
1786 usage (&argc, &argv, FALSE);
1792 g_free (completion_cur);
1793 g_free (completion_prev);