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>
34 /* ---------------------------------------------------------------------------------------------------- */
36 G_GNUC_UNUSED static void completion_debug (const gchar *format, ...);
38 /* Uncomment to get debug traces in /tmp/gdbus-completion-debug.txt (nice
39 * to not have it interfere with stdout/stderr)
42 G_GNUC_UNUSED static void
43 completion_debug (const gchar *format, ...)
47 static FILE *f = NULL;
49 va_start (var_args, format);
50 s = g_strdup_vprintf (format, var_args);
53 f = fopen ("/tmp/gdbus-completion-debug.txt", "a+");
55 fprintf (f, "%s\n", s);
60 completion_debug (const gchar *format, ...)
65 /* ---------------------------------------------------------------------------------------------------- */
69 remove_arg (gint num, gint *argc, gchar **argv[])
73 g_assert (num <= (*argc));
75 for (n = num; (*argv)[n] != NULL; n++)
76 (*argv)[n] = (*argv)[n+1];
78 (*argc) = (*argc) - 1;
82 usage (gint *argc, gchar **argv[], gboolean use_stdout)
88 o = g_option_context_new (_("COMMAND"));
89 g_option_context_set_help_enabled (o, FALSE);
90 /* Ignore parsing result */
91 g_option_context_parse (o, argc, argv, NULL);
92 program_name = g_path_get_basename ((*argv)[0]);
93 s = g_strdup_printf (_("Commands:\n"
94 " help Shows this information\n"
95 " introspect Introspect a remote object\n"
96 " monitor Monitor a remote object\n"
97 " call Invoke a method on a remote object\n"
99 "Use \"%s COMMAND --help\" to get help on each command.\n"),
101 g_free (program_name);
102 g_option_context_set_description (o, s);
104 s = g_option_context_get_help (o, FALSE, NULL);
108 g_printerr ("%s", s);
110 g_option_context_free (o);
114 modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command)
120 * 1. get a g_set_prgname() ?; or
121 * 2. save old argv[0] and restore later
124 g_assert (g_strcmp0 ((*argv)[1], command) == 0);
125 remove_arg (1, argc, argv);
127 program_name = g_path_get_basename ((*argv)[0]);
128 s = g_strdup_printf ("%s %s", (*argv)[0], command);
130 g_free (program_name);
133 /* ---------------------------------------------------------------------------------------------------- */
136 print_methods (GDBusConnection *c,
142 const gchar *xml_data;
148 result = g_dbus_connection_call_sync (c,
151 "org.freedesktop.DBus.Introspectable",
154 G_VARIANT_TYPE ("(s)"),
155 G_DBUS_CALL_FLAGS_NONE,
161 g_printerr (_("Error: %s\n"), error->message);
162 g_error_free (error);
165 g_variant_get (result, "(&s)", &xml_data);
168 node = g_dbus_node_info_new_for_xml (xml_data, &error);
169 g_variant_unref (result);
172 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
173 g_error_free (error);
177 for (n = 0; node->interfaces != NULL && node->interfaces[n] != NULL; n++)
179 const GDBusInterfaceInfo *iface = node->interfaces[n];
180 for (m = 0; iface->methods != NULL && iface->methods[m] != NULL; m++)
182 const GDBusMethodInfo *method = iface->methods[m];
183 g_print ("%s.%s \n", iface->name, method->name);
186 g_dbus_node_info_unref (node);
193 print_paths (GDBusConnection *c,
199 const gchar *xml_data;
204 result = g_dbus_connection_call_sync (c,
207 "org.freedesktop.DBus.Introspectable",
210 G_VARIANT_TYPE ("(s)"),
211 G_DBUS_CALL_FLAGS_NONE,
217 g_printerr (_("Error: %s\n"), error->message);
218 g_error_free (error);
221 g_variant_get (result, "(&s)", &xml_data);
223 //g_printerr ("xml=`%s'", xml_data);
226 node = g_dbus_node_info_new_for_xml (xml_data, &error);
227 g_variant_unref (result);
230 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
231 g_error_free (error);
235 //g_printerr ("bar `%s'\n", path);
237 if (node->interfaces != NULL)
238 g_print ("%s \n", path);
240 for (n = 0; node->nodes != NULL && node->nodes[n] != NULL; n++)
244 //g_printerr ("foo `%s'\n", node->nodes[n].path);
246 if (g_strcmp0 (path, "/") == 0)
247 s = g_strdup_printf ("/%s", node->nodes[n]->path);
249 s = g_strdup_printf ("%s/%s", path, node->nodes[n]->path);
251 print_paths (c, name, s);
255 g_dbus_node_info_unref (node);
262 print_names (GDBusConnection *c,
263 gboolean include_unique_names)
269 GHashTable *name_set;
273 name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
276 result = g_dbus_connection_call_sync (c,
277 "org.freedesktop.DBus",
278 "/org/freedesktop/DBus",
279 "org.freedesktop.DBus",
282 G_VARIANT_TYPE ("(as)"),
283 G_DBUS_CALL_FLAGS_NONE,
289 g_printerr (_("Error: %s\n"), error->message);
290 g_error_free (error);
293 g_variant_get (result, "(as)", &iter);
294 while (g_variant_iter_loop (iter, "s", &str))
295 g_hash_table_insert (name_set, g_strdup (str), NULL);
296 g_variant_iter_free (iter);
297 g_variant_unref (result);
300 result = g_dbus_connection_call_sync (c,
301 "org.freedesktop.DBus",
302 "/org/freedesktop/DBus",
303 "org.freedesktop.DBus",
304 "ListActivatableNames",
306 G_VARIANT_TYPE ("(as)"),
307 G_DBUS_CALL_FLAGS_NONE,
313 g_printerr (_("Error: %s\n"), error->message);
314 g_error_free (error);
317 g_variant_get (result, "(as)", &iter);
318 while (g_variant_iter_loop (iter, "s", &str))
319 g_hash_table_insert (name_set, g_strdup (str), NULL);
320 g_variant_iter_free (iter);
321 g_variant_unref (result);
323 keys = g_hash_table_get_keys (name_set);
324 keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
325 for (l = keys; l != NULL; l = l->next)
327 const gchar *name = l->data;
328 if (!include_unique_names && g_str_has_prefix (name, ":"))
331 g_print ("%s \n", name);
336 g_hash_table_unref (name_set);
339 /* ---------------------------------------------------------------------------------------------------- */
341 static gboolean opt_connection_system = FALSE;
342 static gboolean opt_connection_session = FALSE;
343 static gchar *opt_connection_address = NULL;
345 static const GOptionEntry connection_entries[] =
347 { "system", 'y', 0, G_OPTION_ARG_NONE, &opt_connection_system, N_("Connect to the system bus"), NULL},
348 { "session", 'e', 0, G_OPTION_ARG_NONE, &opt_connection_session, N_("Connect to the session bus"), NULL},
349 { "address", 'a', 0, G_OPTION_ARG_STRING, &opt_connection_address, N_("Connect to given D-Bus address"), NULL},
353 static GOptionGroup *
354 connection_get_group (void)
356 static GOptionGroup *g;
358 g = g_option_group_new ("connection",
359 N_("Connection Endpoint Options:"),
360 N_("Options specifying the connection endpoint"),
363 g_option_group_set_translation_domain (g, GETTEXT_PACKAGE);
364 g_option_group_add_entries (g, connection_entries);
369 static GDBusConnection *
370 connection_get_dbus_connection (GError **error)
376 /* First, ensure we have exactly one connect */
377 if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
382 _("No connection endpoint specified"));
385 else if ((opt_connection_system && (opt_connection_session || opt_connection_address != NULL)) ||
386 (opt_connection_session && (opt_connection_system || opt_connection_address != NULL)) ||
387 (opt_connection_address != NULL && (opt_connection_system || opt_connection_session)))
392 _("Multiple connection endpoints specified"));
396 if (opt_connection_system)
398 c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
400 else if (opt_connection_session)
402 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
404 else if (opt_connection_address != NULL)
406 c = g_dbus_connection_new_for_address_sync (opt_connection_address,
407 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
408 NULL, /* GDBusAuthObserver */
409 NULL, /* GCancellable */
417 /* ---------------------------------------------------------------------------------------------------- */
420 call_helper_get_method_in_signature (GDBusConnection *c,
423 const gchar *interface_name,
424 const gchar *method_name,
429 GDBusNodeInfo *node_info;
430 const gchar *xml_data;
431 GDBusInterfaceInfo *interface_info;
432 GDBusMethodInfo *method_info;
439 result = g_dbus_connection_call_sync (c,
442 "org.freedesktop.DBus.Introspectable",
445 G_VARIANT_TYPE ("(s)"),
446 G_DBUS_CALL_FLAGS_NONE,
453 g_variant_get (result, "(&s)", &xml_data);
454 node_info = g_dbus_node_info_new_for_xml (xml_data, error);
455 if (node_info == NULL)
458 interface_info = g_dbus_node_info_lookup_interface (node_info, interface_name);
459 if (interface_info == NULL)
461 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
462 _("Warning: According to introspection data, interface `%s' does not exist\n"),
467 method_info = g_dbus_interface_info_lookup_method (interface_info, method_name);
468 if (method_info == NULL)
470 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
471 _("Warning: According to introspection data, method `%s' does not exist on interface `%s'\n"),
477 ret = g_ptr_array_new_with_free_func ((GDestroyNotify) g_variant_type_free);
478 for (n = 0; method_info->in_args != NULL && method_info->in_args[n] != NULL; n++)
480 g_ptr_array_add (ret, g_variant_type_new (method_info->in_args[n]->signature));
484 if (node_info != NULL)
485 g_dbus_node_info_unref (node_info);
487 g_variant_unref (result);
492 /* ---------------------------------------------------------------------------------------------------- */
495 _g_variant_parse_me_harder (GVariantType *type,
496 const gchar *given_str,
504 str = g_string_new ("\"");
505 for (n = 0; given_str[n] != '\0'; n++)
507 if (G_UNLIKELY (given_str[n] == '\"'))
508 g_string_append (str, "\\\"");
510 g_string_append_c (str, given_str[n]);
512 g_string_append_c (str, '"');
513 s = g_string_free (str, FALSE);
515 value = g_variant_parse (type,
525 /* ---------------------------------------------------------------------------------------------------- */
527 static gchar *opt_call_dest = NULL;
528 static gchar *opt_call_object_path = NULL;
529 static gchar *opt_call_method = NULL;
531 static const GOptionEntry call_entries[] =
533 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_call_dest, N_("Destination name to invoke method on"), NULL},
534 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_call_object_path, N_("Object path to invoke method on"), NULL},
535 { "method", 'm', 0, G_OPTION_ARG_STRING, &opt_call_method, N_("Method and interface name"), NULL},
540 handle_call (gint *argc,
542 gboolean request_completion,
543 const gchar *completion_cur,
544 const gchar *completion_prev)
551 GVariant *parameters;
552 gchar *interface_name;
555 GPtrArray *in_signature_types;
556 gboolean complete_names;
557 gboolean complete_paths;
558 gboolean complete_methods;
559 GVariantBuilder builder;
565 interface_name = NULL;
568 in_signature_types = NULL;
570 modify_argv0_for_command (argc, argv, "call");
572 o = g_option_context_new (NULL);
573 g_option_context_set_help_enabled (o, FALSE);
574 g_option_context_set_summary (o, _("Invoke a method on a remote object."));
575 g_option_context_add_main_entries (o, call_entries, GETTEXT_PACKAGE);
576 g_option_context_add_group (o, connection_get_group ());
578 complete_names = FALSE;
579 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
581 complete_names = TRUE;
582 remove_arg ((*argc) - 1, argc, argv);
585 complete_paths = FALSE;
586 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
588 complete_paths = TRUE;
589 remove_arg ((*argc) - 1, argc, argv);
592 complete_methods = FALSE;
593 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--method") == 0)
595 complete_methods = TRUE;
596 remove_arg ((*argc) - 1, argc, argv);
599 if (!g_option_context_parse (o, argc, argv, NULL))
601 if (!request_completion)
603 s = g_option_context_get_help (o, FALSE, NULL);
604 g_printerr ("%s", s);
611 c = connection_get_dbus_connection (&error);
614 if (request_completion)
616 if (g_strcmp0 (completion_prev, "--address") == 0)
624 g_print ("--system \n--session \n--address \n");
629 g_printerr (_("Error connecting: %s\n"), error->message);
630 g_error_free (error);
635 /* validate and complete destination (bus name) */
636 if (g_dbus_connection_get_unique_name (c) != NULL)
638 /* this only makes sense on message bus connections */
641 print_names (c, FALSE);
644 if (opt_call_dest == NULL)
646 if (request_completion)
647 g_print ("--dest \n");
649 g_printerr (_("Error: Destination is not specified\n"));
652 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
654 print_names (c, g_str_has_prefix (opt_call_dest, ":"));
659 /* validate and complete object path */
662 print_paths (c, opt_call_dest, "/");
665 if (opt_call_object_path == NULL)
667 if (request_completion)
668 g_print ("--object-path \n");
670 g_printerr (_("Error: Object path is not specified\n"));
673 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
676 s = g_strdup (opt_call_object_path);
677 p = strrchr (s, '/');
684 print_paths (c, opt_call_dest, s);
688 if (!request_completion && !g_variant_is_object_path (opt_call_object_path))
690 g_printerr (_("Error: %s is not a valid object path\n"), opt_call_object_path);
694 /* validate and complete method (interface + method name) */
695 if (complete_methods)
697 print_methods (c, opt_call_dest, opt_call_object_path);
700 if (opt_call_method == NULL)
702 if (request_completion)
703 g_print ("--method \n");
705 g_printerr (_("Error: Method name is not specified\n"));
708 if (request_completion && g_strcmp0 ("--method", completion_prev) == 0)
710 print_methods (c, opt_call_dest, opt_call_object_path);
713 s = strrchr (opt_call_method, '.');
714 if (!request_completion && s == NULL)
716 g_printerr (_("Error: Method name `%s' is invalid\n"), opt_call_method);
719 method_name = g_strdup (s + 1);
720 interface_name = g_strndup (opt_call_method, s - opt_call_method);
722 /* All done with completion now */
723 if (request_completion)
726 /* Introspect, for easy conversion - it's not fatal if we can't do this */
727 in_signature_types = call_helper_get_method_in_signature (c,
729 opt_call_object_path,
733 if (in_signature_types == NULL)
735 //g_printerr ("Error getting introspection data: %s\n", error->message);
736 g_error_free (error);
740 /* Read parameters */
741 g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
742 for (n = 1; n < (guint) *argc; n++)
748 if (in_signature_types != NULL)
750 if (n - 1 >= in_signature_types->len)
752 /* Only warn for the first param */
753 if (n - 1 == in_signature_types->len)
755 g_printerr ("Warning: Introspection data indicates %d parameters but more was passed\n",
756 in_signature_types->len);
761 type = in_signature_types->pdata[n - 1];
766 value = g_variant_parse (type,
773 g_error_free (error);
775 value = _g_variant_parse_me_harder (type, (*argv)[n], &error);
780 s = g_variant_type_dup_string (type);
781 g_printerr (_("Error parsing parameter %d of type `%s': %s\n"),
789 g_printerr (_("Error parsing parameter %d: %s\n"),
793 g_error_free (error);
794 g_variant_builder_clear (&builder);
798 g_variant_builder_add_value (&builder, value);
800 parameters = g_variant_builder_end (&builder);
802 if (parameters != NULL)
803 parameters = g_variant_ref_sink (parameters);
804 result = g_dbus_connection_call_sync (c,
806 opt_call_object_path,
811 G_DBUS_CALL_FLAGS_NONE,
817 g_printerr (_("Error: %s\n"), error->message);
818 g_error_free (error);
819 if (in_signature_types != NULL)
822 s = g_string_new (NULL);
823 for (n = 0; n < in_signature_types->len; n++)
825 GVariantType *type = in_signature_types->pdata[n];
826 g_string_append_len (s,
827 g_variant_type_peek_string (type),
828 g_variant_type_get_string_length (type));
830 g_printerr ("(According to introspection data, you need to pass `%s')\n", s->str);
831 g_string_free (s, TRUE);
836 s = g_variant_print (result, TRUE);
843 if (in_signature_types != NULL)
844 g_ptr_array_unref (in_signature_types);
846 g_variant_unref (result);
849 if (parameters != NULL)
850 g_variant_unref (parameters);
851 g_free (interface_name);
852 g_free (method_name);
853 g_option_context_free (o);
857 /* ---------------------------------------------------------------------------------------------------- */
859 /* TODO: dump annotations */
862 dump_annotation (const GDBusAnnotationInfo *o,
864 gboolean ignore_indent)
867 g_print ("%*s@%s(\"%s\")\n",
868 ignore_indent ? 0 : indent, "",
871 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
872 dump_annotation (o->annotations[n], indent + 2, FALSE);
876 dump_arg (const GDBusArgInfo *o,
878 const gchar *direction,
879 gboolean ignore_indent,
880 gboolean include_newline)
884 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
886 dump_annotation (o->annotations[n], indent, ignore_indent);
887 ignore_indent = FALSE;
890 g_print ("%*s%s%s %s%s",
891 ignore_indent ? 0 : indent, "",
895 include_newline ? ",\n" : "");
899 count_args (GDBusArgInfo **args)
905 while (args[n] != NULL)
912 dump_method (const GDBusMethodInfo *o,
918 guint total_num_args;
920 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
921 dump_annotation (o->annotations[n], indent, FALSE);
923 g_print ("%*s%s(", indent, "", o->name);
924 name_len = strlen (o->name);
925 total_num_args = count_args (o->in_args) + count_args (o->out_args);
926 for (n = 0, m = 0; o->in_args != NULL && o->in_args[n] != NULL; n++, m++)
928 gboolean ignore_indent = (m == 0);
929 gboolean include_newline = (m != total_num_args - 1);
931 dump_arg (o->in_args[n],
932 indent + name_len + 1,
937 for (n = 0; o->out_args != NULL && o->out_args[n] != NULL; n++, m++)
939 gboolean ignore_indent = (m == 0);
940 gboolean include_newline = (m != total_num_args - 1);
941 dump_arg (o->out_args[n],
942 indent + name_len + 1,
951 dump_signal (const GDBusSignalInfo *o,
956 guint total_num_args;
958 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
959 dump_annotation (o->annotations[n], indent, FALSE);
961 g_print ("%*s%s(", indent, "", o->name);
962 name_len = strlen (o->name);
963 total_num_args = count_args (o->args);
964 for (n = 0; o->args != NULL && o->args[n] != NULL; n++)
966 gboolean ignore_indent = (n == 0);
967 gboolean include_newline = (n != total_num_args - 1);
968 dump_arg (o->args[n],
969 indent + name_len + 1,
978 dump_property (const GDBusPropertyInfo *o,
985 if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
987 else if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE)
988 access = "writeonly";
989 else if (o->flags == (G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE))
990 access = "readwrite";
992 g_assert_not_reached ();
994 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
995 dump_annotation (o->annotations[n], indent, FALSE);
999 gchar *s = g_variant_print (value, FALSE);
1000 g_print ("%*s%s %s %s = %s;\n", indent, "", access, o->signature, o->name, s);
1005 g_print ("%*s%s %s %s;\n", indent, "", access, o->signature, o->name);
1010 dump_interface (GDBusConnection *c,
1012 const GDBusInterfaceInfo *o,
1014 const gchar *object_path)
1017 GHashTable *properties;
1019 properties = g_hash_table_new_full (g_str_hash,
1022 (GDestroyNotify) g_variant_unref);
1024 /* Try to get properties */
1025 if (c != NULL && name != NULL && object_path != NULL && o->properties != NULL)
1028 result = g_dbus_connection_call_sync (c,
1031 "org.freedesktop.DBus.Properties",
1033 g_variant_new ("(s)", o->name),
1035 G_DBUS_CALL_FLAGS_NONE,
1041 if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1045 g_variant_get (result,
1048 while ((item = g_variant_iter_next_value (iter)))
1052 g_variant_get (item,
1057 g_hash_table_insert (properties, key, g_variant_ref (value));
1060 g_variant_unref (result);
1065 for (n = 0; o->properties != NULL && o->properties[n] != NULL; n++)
1067 result = g_dbus_connection_call_sync (c,
1070 "org.freedesktop.DBus.Properties",
1072 g_variant_new ("(ss)", o->name, o->properties[n]->name),
1073 G_VARIANT_TYPE ("(v)"),
1074 G_DBUS_CALL_FLAGS_NONE,
1080 GVariant *property_value;
1081 g_variant_get (result,
1084 g_hash_table_insert (properties,
1085 g_strdup (o->properties[n]->name),
1086 g_variant_ref (property_value));
1087 g_variant_unref (result);
1093 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1094 dump_annotation (o->annotations[n], indent, FALSE);
1096 g_print ("%*sinterface %s {\n", indent, "", o->name);
1097 if (o->methods != NULL)
1099 g_print ("%*s methods:\n", indent, "");
1100 for (n = 0; o->methods[n] != NULL; n++)
1101 dump_method (o->methods[n], indent + 4);
1103 if (o->signals != NULL)
1105 g_print ("%*s signals:\n", indent, "");
1106 for (n = 0; o->signals[n] != NULL; n++)
1107 dump_signal (o->signals[n], indent + 4);
1109 if (o->properties != NULL)
1111 g_print ("%*s properties:\n", indent, "");
1112 for (n = 0; o->properties[n] != NULL; n++)
1114 dump_property (o->properties[n],
1116 g_hash_table_lookup (properties, (o->properties[n])->name));
1122 g_hash_table_unref (properties);
1126 dump_node (GDBusConnection *c,
1128 const GDBusNodeInfo *o,
1130 const gchar *object_path)
1133 const gchar *object_path_to_print;
1135 object_path_to_print = object_path;
1136 if (o->path != NULL)
1137 object_path_to_print = o->path;
1139 for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1140 dump_annotation (o->annotations[n], indent, FALSE);
1142 g_print ("%*snode %s", indent, "", object_path_to_print != NULL ? object_path_to_print : "(not set)");
1143 if (o->interfaces != NULL || o->nodes != NULL)
1146 for (n = 0; o->interfaces != NULL && o->interfaces[n] != NULL; n++)
1147 dump_interface (c, name, o->interfaces[n], indent + 2, object_path);
1148 for (n = 0; o->nodes != NULL && o->nodes[n] != NULL; n++)
1149 dump_node (NULL, NULL, o->nodes[n], indent + 2, NULL);
1159 static gchar *opt_introspect_dest = NULL;
1160 static gchar *opt_introspect_object_path = NULL;
1161 static gboolean opt_introspect_xml = FALSE;
1163 static const GOptionEntry introspect_entries[] =
1165 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_introspect_dest, N_("Destination name to introspect"), NULL},
1166 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_introspect_object_path, N_("Object path to introspect"), NULL},
1167 { "xml", 'x', 0, G_OPTION_ARG_NONE, &opt_introspect_xml, N_("Print XML"), NULL},
1172 handle_introspect (gint *argc,
1174 gboolean request_completion,
1175 const gchar *completion_cur,
1176 const gchar *completion_prev)
1184 const gchar *xml_data;
1185 GDBusNodeInfo *node;
1186 gboolean complete_names;
1187 gboolean complete_paths;
1194 modify_argv0_for_command (argc, argv, "introspect");
1196 o = g_option_context_new (NULL);
1197 if (request_completion)
1198 g_option_context_set_ignore_unknown_options (o, TRUE);
1199 g_option_context_set_help_enabled (o, FALSE);
1200 g_option_context_set_summary (o, _("Introspect a remote object."));
1201 g_option_context_add_main_entries (o, introspect_entries, GETTEXT_PACKAGE);
1202 g_option_context_add_group (o, connection_get_group ());
1204 complete_names = FALSE;
1205 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1207 complete_names = TRUE;
1208 remove_arg ((*argc) - 1, argc, argv);
1211 complete_paths = FALSE;
1212 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1214 complete_paths = TRUE;
1215 remove_arg ((*argc) - 1, argc, argv);
1218 if (!g_option_context_parse (o, argc, argv, NULL))
1220 if (!request_completion)
1222 s = g_option_context_get_help (o, FALSE, NULL);
1223 g_printerr ("%s", s);
1230 c = connection_get_dbus_connection (&error);
1233 if (request_completion)
1235 if (g_strcmp0 (completion_prev, "--address") == 0)
1243 g_print ("--system \n--session \n--address \n");
1248 g_printerr (_("Error connecting: %s\n"), error->message);
1249 g_error_free (error);
1254 if (g_dbus_connection_get_unique_name (c) != NULL)
1258 print_names (c, FALSE);
1261 /* this only makes sense on message bus connections */
1262 if (opt_introspect_dest == NULL)
1264 if (request_completion)
1265 g_print ("--dest \n");
1267 g_printerr (_("Error: Destination is not specified\n"));
1270 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1272 print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1278 print_paths (c, opt_introspect_dest, "/");
1281 if (opt_introspect_object_path == NULL)
1283 if (request_completion)
1284 g_print ("--object-path \n");
1286 g_printerr (_("Error: Object path is not specified\n"));
1289 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1292 s = g_strdup (opt_introspect_object_path);
1293 p = strrchr (s, '/');
1300 print_paths (c, opt_introspect_dest, s);
1304 if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1306 g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1310 /* All done with completion now */
1311 if (request_completion)
1314 result = g_dbus_connection_call_sync (c,
1315 opt_introspect_dest,
1316 opt_introspect_object_path,
1317 "org.freedesktop.DBus.Introspectable",
1320 G_VARIANT_TYPE ("(s)"),
1321 G_DBUS_CALL_FLAGS_NONE,
1327 g_printerr (_("Error: %s\n"), error->message);
1328 g_error_free (error);
1331 g_variant_get (result, "(&s)", &xml_data);
1333 if (opt_introspect_xml)
1335 g_print ("%s", xml_data);
1340 node = g_dbus_node_info_new_for_xml (xml_data, &error);
1343 g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1344 g_error_free (error);
1348 dump_node (c, opt_introspect_dest, node, 0, opt_introspect_object_path);
1355 g_dbus_node_info_unref (node);
1357 g_variant_unref (result);
1360 g_option_context_free (o);
1364 /* ---------------------------------------------------------------------------------------------------- */
1366 static gchar *opt_monitor_dest = NULL;
1367 static gchar *opt_monitor_object_path = NULL;
1369 static guint monitor_filter_id = 0;
1372 monitor_signal_cb (GDBusConnection *connection,
1373 const gchar *sender_name,
1374 const gchar *object_path,
1375 const gchar *interface_name,
1376 const gchar *signal_name,
1377 GVariant *parameters,
1381 s = g_variant_print (parameters, TRUE);
1382 g_print ("%s: %s.%s %s\n",
1391 monitor_on_name_appeared (GDBusConnection *connection,
1393 const gchar *name_owner,
1396 g_print ("The name %s is owned by %s\n", name, name_owner);
1397 g_assert (monitor_filter_id == 0);
1398 monitor_filter_id = g_dbus_connection_signal_subscribe (connection,
1400 NULL, /* any interface */
1401 NULL, /* any member */
1402 opt_monitor_object_path,
1404 G_DBUS_SIGNAL_FLAGS_NONE,
1406 NULL, /* user_data */
1407 NULL); /* user_data destroy notify */
1411 monitor_on_name_vanished (GDBusConnection *connection,
1415 g_print ("The name %s does not have an owner\n", name);
1417 if (monitor_filter_id != 0)
1419 g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1420 monitor_filter_id = 0;
1424 static const GOptionEntry monitor_entries[] =
1426 { "dest", 'd', 0, G_OPTION_ARG_STRING, &opt_monitor_dest, N_("Destination name to monitor"), NULL},
1427 { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_monitor_object_path, N_("Object path to monitor"), NULL},
1432 handle_monitor (gint *argc,
1434 gboolean request_completion,
1435 const gchar *completion_cur,
1436 const gchar *completion_prev)
1444 GDBusNodeInfo *node;
1445 gboolean complete_names;
1446 gboolean complete_paths;
1454 modify_argv0_for_command (argc, argv, "monitor");
1456 o = g_option_context_new (NULL);
1457 if (request_completion)
1458 g_option_context_set_ignore_unknown_options (o, TRUE);
1459 g_option_context_set_help_enabled (o, FALSE);
1460 g_option_context_set_summary (o, _("Monitor a remote object."));
1461 g_option_context_add_main_entries (o, monitor_entries, GETTEXT_PACKAGE);
1462 g_option_context_add_group (o, connection_get_group ());
1464 complete_names = FALSE;
1465 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1467 complete_names = TRUE;
1468 remove_arg ((*argc) - 1, argc, argv);
1471 complete_paths = FALSE;
1472 if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1474 complete_paths = TRUE;
1475 remove_arg ((*argc) - 1, argc, argv);
1478 if (!g_option_context_parse (o, argc, argv, NULL))
1480 if (!request_completion)
1482 s = g_option_context_get_help (o, FALSE, NULL);
1483 g_printerr ("%s", s);
1490 c = connection_get_dbus_connection (&error);
1493 if (request_completion)
1495 if (g_strcmp0 (completion_prev, "--address") == 0)
1503 g_print ("--system \n--session \n--address \n");
1508 g_printerr (_("Error connecting: %s\n"), error->message);
1509 g_error_free (error);
1514 if (g_dbus_connection_get_unique_name (c) != NULL)
1518 print_names (c, FALSE);
1521 /* this only makes sense on message bus connections */
1522 if (opt_monitor_dest == NULL)
1524 if (request_completion)
1525 g_print ("--dest \n");
1527 g_printerr (_("Error: Destination is not specified\n"));
1530 if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1532 print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1538 print_paths (c, opt_monitor_dest, "/");
1541 if (opt_monitor_object_path == NULL)
1543 if (request_completion)
1545 g_print ("--object-path \n");
1548 /* it's fine to not have an object path */
1550 if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1553 s = g_strdup (opt_monitor_object_path);
1554 p = strrchr (s, '/');
1561 print_paths (c, opt_monitor_dest, s);
1565 if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1567 g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1571 /* All done with completion now */
1572 if (request_completion)
1575 if (opt_monitor_object_path != NULL)
1576 g_print ("Monitoring signals on object %s owned by %s\n", opt_monitor_object_path, opt_monitor_dest);
1578 g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1580 loop = g_main_loop_new (NULL, FALSE);
1581 g_bus_watch_name_on_connection (c,
1583 G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1584 monitor_on_name_appeared,
1585 monitor_on_name_vanished,
1589 g_main_loop_run (loop);
1590 g_main_loop_unref (loop);
1596 g_dbus_node_info_unref (node);
1598 g_variant_unref (result);
1601 g_option_context_free (o);
1605 /* ---------------------------------------------------------------------------------------------------- */
1608 pick_word_at (const gchar *s,
1610 gint *out_word_begins_at)
1617 if (out_word_begins_at != NULL)
1618 *out_word_begins_at = -1;
1622 if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1624 if (out_word_begins_at != NULL)
1625 *out_word_begins_at = cursor;
1626 return g_strdup ("");
1629 while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1634 while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1637 if (out_word_begins_at != NULL)
1638 *out_word_begins_at = begin;
1640 return g_strndup (s + begin, end - begin);
1644 main (gint argc, gchar *argv[])
1647 const gchar *command;
1648 gboolean request_completion;
1649 gchar *completion_cur;
1650 gchar *completion_prev;
1652 setlocale (LC_ALL, "");
1653 textdomain (GETTEXT_PACKAGE);
1656 extern gchar *_glib_get_locale_dir (void);
1657 gchar *tmp = _glib_get_locale_dir ();
1658 bindtextdomain (GETTEXT_PACKAGE, tmp);
1661 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1664 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1665 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1669 completion_cur = NULL;
1670 completion_prev = NULL;
1676 usage (&argc, &argv, FALSE);
1680 request_completion = FALSE;
1682 //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1686 if (g_strcmp0 (command, "help") == 0)
1688 if (request_completion)
1694 usage (&argc, &argv, TRUE);
1699 else if (g_strcmp0 (command, "call") == 0)
1701 if (handle_call (&argc,
1709 else if (g_strcmp0 (command, "introspect") == 0)
1711 if (handle_introspect (&argc,
1719 else if (g_strcmp0 (command, "monitor") == 0)
1721 if (handle_monitor (&argc,
1729 else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
1731 const gchar *completion_line;
1732 gchar **completion_argv;
1733 gint completion_argc;
1734 gint completion_point;
1738 request_completion = TRUE;
1740 completion_line = argv[2];
1741 completion_point = strtol (argv[3], &endp, 10);
1742 if (endp == argv[3] || *endp != '\0')
1746 completion_debug ("completion_point=%d", completion_point);
1747 completion_debug ("----");
1748 completion_debug (" 0123456789012345678901234567890123456789012345678901234567890123456789");
1749 completion_debug ("`%s'", completion_line);
1750 completion_debug (" %*s^",
1751 completion_point, "");
1752 completion_debug ("----");
1755 if (!g_shell_parse_argv (completion_line,
1760 /* it's very possible the command line can't be parsed (for
1761 * example, missing quotes etc) - in that case, we just
1762 * don't autocomplete at all
1767 /* compute cur and prev */
1768 completion_prev = NULL;
1769 completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
1773 for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
1775 if (!g_ascii_isspace (completion_line[prev_end]))
1777 completion_prev = pick_word_at (completion_line, prev_end, NULL);
1783 completion_debug (" cur=`%s'", completion_cur);
1784 completion_debug ("prev=`%s'", completion_prev);
1787 argc = completion_argc;
1788 argv = completion_argv;
1796 if (request_completion)
1798 g_print ("help \ncall \nintrospect \nmonitor \n");
1804 g_printerr ("Unknown command `%s'\n", command);
1805 usage (&argc, &argv, FALSE);
1811 g_free (completion_cur);
1812 g_free (completion_prev);