Bug 621213 – GDBusProxy and well-known names
[platform/upstream/glib.git] / gio / gdbus-tool.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <gio/gio.h>
30
31 #include <gi18n.h>
32
33 /* ---------------------------------------------------------------------------------------------------- */
34
35 G_GNUC_UNUSED static void completion_debug (const gchar *format, ...);
36
37 /* Uncomment to get debug traces in /tmp/gdbus-completion-debug.txt (nice
38  * to not have it interfere with stdout/stderr)
39  */
40 #if 0
41 G_GNUC_UNUSED static void
42 completion_debug (const gchar *format, ...)
43 {
44   va_list var_args;
45   gchar *s;
46   static FILE *f = NULL;
47
48   va_start (var_args, format);
49   s = g_strdup_vprintf (format, var_args);
50   if (f == NULL)
51     {
52       f = fopen ("/tmp/gdbus-completion-debug.txt", "a+");
53     }
54   fprintf (f, "%s\n", s);
55   g_free (s);
56 }
57 #else
58 static void
59 completion_debug (const gchar *format, ...)
60 {
61 }
62 #endif
63
64 /* ---------------------------------------------------------------------------------------------------- */
65
66
67 static void
68 remove_arg (gint num, gint *argc, gchar **argv[])
69 {
70   gint n;
71
72   g_assert (num <= (*argc));
73
74   for (n = num; (*argv)[n] != NULL; n++)
75     (*argv)[n] = (*argv)[n+1];
76   (*argv)[n] = NULL;
77   (*argc) = (*argc) - 1;
78 }
79
80 static void
81 usage (gint *argc, gchar **argv[], gboolean use_stdout)
82 {
83   GOptionContext *o;
84   gchar *s;
85   gchar *program_name;
86
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"
97                          "\n"
98                          "Use \"%s COMMAND --help\" to get help on each command.\n"),
99                        program_name);
100   g_free (program_name);
101   g_option_context_set_description (o, s);
102   g_free (s);
103   s = g_option_context_get_help (o, FALSE, NULL);
104   if (use_stdout)
105     g_print ("%s", s);
106   else
107     g_printerr ("%s", s);
108   g_free (s);
109   g_option_context_free (o);
110 }
111
112 static void
113 modify_argv0_for_command (gint *argc, gchar **argv[], const gchar *command)
114 {
115   gchar *s;
116   gchar *program_name;
117
118   /* TODO:
119    *  1. get a g_set_prgname() ?; or
120    *  2. save old argv[0] and restore later
121    */
122
123   g_assert (g_strcmp0 ((*argv)[1], command) == 0);
124   remove_arg (1, argc, argv);
125
126   program_name = g_path_get_basename ((*argv)[0]);
127   s = g_strdup_printf ("%s %s", (*argv)[0], command);
128   (*argv)[0] = s;
129   g_free (program_name);
130 }
131
132 /* ---------------------------------------------------------------------------------------------------- */
133
134 static void
135 print_methods (GDBusConnection *c,
136                const gchar *name,
137                const gchar *path)
138 {
139   GVariant *result;
140   GError *error;
141   const gchar *xml_data;
142   GDBusNodeInfo *node;
143   guint n;
144   guint m;
145
146   error = NULL;
147   result = g_dbus_connection_call_sync (c,
148                                         name,
149                                         path,
150                                         "org.freedesktop.DBus.Introspectable",
151                                         "Introspect",
152                                         NULL,
153                                         G_VARIANT_TYPE ("(s)"),
154                                         G_DBUS_CALL_FLAGS_NONE,
155                                         3000, /* 3 secs */
156                                         NULL,
157                                         &error);
158   if (result == NULL)
159     {
160       g_printerr (_("Error: %s\n"), error->message);
161       g_error_free (error);
162       goto out;
163     }
164   g_variant_get (result, "(&s)", &xml_data);
165
166   error = NULL;
167   node = g_dbus_node_info_new_for_xml (xml_data, &error);
168   g_variant_unref (result);
169   if (node == NULL)
170     {
171       g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
172       g_error_free (error);
173       goto out;
174     }
175
176   for (n = 0; node->interfaces != NULL && node->interfaces[n] != NULL; n++)
177     {
178       const GDBusInterfaceInfo *iface = node->interfaces[n];
179       for (m = 0; iface->methods != NULL && iface->methods[m] != NULL; m++)
180         {
181           const GDBusMethodInfo *method = iface->methods[m];
182           g_print ("%s.%s \n", iface->name, method->name);
183         }
184     }
185   g_dbus_node_info_unref (node);
186
187  out:
188   ;
189 }
190
191 static void
192 print_paths (GDBusConnection *c,
193              const gchar *name,
194              const gchar *path)
195 {
196   GVariant *result;
197   GError *error;
198   const gchar *xml_data;
199   GDBusNodeInfo *node;
200   guint n;
201
202   error = NULL;
203   result = g_dbus_connection_call_sync (c,
204                                         name,
205                                         path,
206                                         "org.freedesktop.DBus.Introspectable",
207                                         "Introspect",
208                                         NULL,
209                                         G_VARIANT_TYPE ("(s)"),
210                                         G_DBUS_CALL_FLAGS_NONE,
211                                         3000, /* 3 secs */
212                                         NULL,
213                                         &error);
214   if (result == NULL)
215     {
216       g_printerr (_("Error: %s\n"), error->message);
217       g_error_free (error);
218       goto out;
219     }
220   g_variant_get (result, "(&s)", &xml_data);
221
222   //g_printerr ("xml=`%s'", xml_data);
223
224   error = NULL;
225   node = g_dbus_node_info_new_for_xml (xml_data, &error);
226   g_variant_unref (result);
227   if (node == NULL)
228     {
229       g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
230       g_error_free (error);
231       goto out;
232     }
233
234   //g_printerr ("bar `%s'\n", path);
235
236   if (node->interfaces != NULL)
237     g_print ("%s \n", path);
238
239   for (n = 0; node->nodes != NULL && node->nodes[n] != NULL; n++)
240     {
241       gchar *s;
242
243       //g_printerr ("foo `%s'\n", node->nodes[n].path);
244
245       if (g_strcmp0 (path, "/") == 0)
246         s = g_strdup_printf ("/%s", node->nodes[n]->path);
247       else
248         s = g_strdup_printf ("%s/%s", path, node->nodes[n]->path);
249
250       print_paths (c, name, s);
251
252       g_free (s);
253     }
254   g_dbus_node_info_unref (node);
255
256  out:
257   ;
258 }
259
260 static void
261 print_names (GDBusConnection *c,
262              gboolean         include_unique_names)
263 {
264   GVariant *result;
265   GError *error;
266   GVariantIter *iter;
267   gchar *str;
268   GHashTable *name_set;
269   GList *keys;
270   GList *l;
271
272   name_set = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
273
274   error = NULL;
275   result = g_dbus_connection_call_sync (c,
276                                         "org.freedesktop.DBus",
277                                         "/org/freedesktop/DBus",
278                                         "org.freedesktop.DBus",
279                                         "ListNames",
280                                         NULL,
281                                         G_VARIANT_TYPE ("(as)"),
282                                         G_DBUS_CALL_FLAGS_NONE,
283                                         3000, /* 3 secs */
284                                         NULL,
285                                         &error);
286   if (result == NULL)
287     {
288       g_printerr (_("Error: %s\n"), error->message);
289       g_error_free (error);
290       goto out;
291     }
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);
297
298   error = NULL;
299   result = g_dbus_connection_call_sync (c,
300                                         "org.freedesktop.DBus",
301                                         "/org/freedesktop/DBus",
302                                         "org.freedesktop.DBus",
303                                         "ListActivatableNames",
304                                         NULL,
305                                         G_VARIANT_TYPE ("(as)"),
306                                         G_DBUS_CALL_FLAGS_NONE,
307                                         3000, /* 3 secs */
308                                         NULL,
309                                         &error);
310   if (result == NULL)
311     {
312       g_printerr (_("Error: %s\n"), error->message);
313       g_error_free (error);
314       goto out;
315     }
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);
321
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)
325     {
326       const gchar *name = l->data;
327       if (!include_unique_names && g_str_has_prefix (name, ":"))
328         continue;
329
330       g_print ("%s \n", name);
331     }
332   g_list_free (keys);
333
334  out:
335   g_hash_table_unref (name_set);
336 }
337
338 /* ---------------------------------------------------------------------------------------------------- */
339
340 static gboolean  opt_connection_system  = FALSE;
341 static gboolean  opt_connection_session = FALSE;
342 static gchar    *opt_connection_address = NULL;
343
344 static const GOptionEntry connection_entries[] =
345 {
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},
349   { NULL }
350 };
351
352 static GOptionGroup *
353 connection_get_group (void)
354 {
355   static GOptionGroup *g;
356
357   g = g_option_group_new ("connection",
358                           N_("Connection Endpoint Options:"),
359                           N_("Options specifying the connection endpoint"),
360                           NULL,
361                           NULL);
362   g_option_group_add_entries (g, connection_entries);
363   return g;
364 }
365
366 static GDBusConnection *
367 connection_get_dbus_connection (GError **error)
368 {
369   GDBusConnection *c;
370
371   c = NULL;
372
373   /* First, ensure we have exactly one connect */
374   if (!opt_connection_system && !opt_connection_session && opt_connection_address == NULL)
375     {
376       g_set_error (error,
377                    G_IO_ERROR,
378                    G_IO_ERROR_FAILED,
379                    _("No connection endpoint specified"));
380       goto out;
381     }
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)))
385     {
386       g_set_error (error,
387                    G_IO_ERROR,
388                    G_IO_ERROR_FAILED,
389                    _("Multiple connection endpoints specified"));
390       goto out;
391     }
392
393   if (opt_connection_system)
394     {
395       c = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, error);
396     }
397   else if (opt_connection_session)
398     {
399       c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, error);
400     }
401   else if (opt_connection_address != NULL)
402     {
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 */
407                                                   error);
408     }
409
410  out:
411   return c;
412 }
413
414 /* ---------------------------------------------------------------------------------------------------- */
415
416 static GPtrArray *
417 call_helper_get_method_in_signature (GDBusConnection  *c,
418                                      const gchar      *dest,
419                                      const gchar      *path,
420                                      const gchar      *interface_name,
421                                      const gchar      *method_name,
422                                      GError          **error)
423 {
424   GPtrArray *ret;
425   GVariant *result;
426   GDBusNodeInfo *node_info;
427   const gchar *xml_data;
428   const GDBusInterfaceInfo *interface_info;
429   const GDBusMethodInfo *method_info;
430   guint n;
431
432   ret = NULL;
433   result = NULL;
434   node_info = NULL;
435
436   result = g_dbus_connection_call_sync (c,
437                                         dest,
438                                         path,
439                                         "org.freedesktop.DBus.Introspectable",
440                                         "Introspect",
441                                         NULL,
442                                         G_VARIANT_TYPE ("(s)"),
443                                         G_DBUS_CALL_FLAGS_NONE,
444                                         3000, /* 3 secs */
445                                         NULL,
446                                         error);
447   if (result == NULL)
448     goto out;
449
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)
453       goto out;
454
455   interface_info = g_dbus_node_info_lookup_interface (node_info, interface_name);
456   if (interface_info == NULL)
457     {
458       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
459                    _("Warning: According to introspection data, interface `%s' does not exist\n"),
460                    interface_name);
461       goto out;
462     }
463
464   method_info = g_dbus_interface_info_lookup_method (interface_info, method_name);
465   if (method_info == NULL)
466     {
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"),
469                    method_name,
470                    interface_name);
471       goto out;
472     }
473
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++)
476     {
477       g_ptr_array_add (ret, g_variant_type_new (method_info->in_args[n]->signature));
478     }
479
480  out:
481   if (node_info != NULL)
482     g_dbus_node_info_unref (node_info);
483   if (result != NULL)
484     g_variant_unref (result);
485
486   return ret;
487 }
488
489 /* ---------------------------------------------------------------------------------------------------- */
490
491 static GVariant *
492 _g_variant_parse_me_harder (GVariantType   *type,
493                             const gchar    *given_str,
494                             GError        **error)
495 {
496   GVariant *value;
497   gchar *s;
498   guint n;
499   GString *str;
500
501   str = g_string_new ("\"");
502   for (n = 0; given_str[n] != '\0'; n++)
503     {
504       if (G_UNLIKELY (given_str[n] == '\"'))
505         g_string_append (str, "\\\"");
506       else
507         g_string_append_c (str, given_str[n]);
508     }
509   g_string_append_c (str, '"');
510   s = g_string_free (str, FALSE);
511
512   value = g_variant_parse (type,
513                            s,
514                            NULL,
515                            NULL,
516                            error);
517   g_free (s);
518
519   return value;
520 }
521
522 /* ---------------------------------------------------------------------------------------------------- */
523
524 static gchar *opt_call_dest = NULL;
525 static gchar *opt_call_object_path = NULL;
526 static gchar *opt_call_method = NULL;
527
528 static const GOptionEntry call_entries[] =
529 {
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},
533   { NULL }
534 };
535
536 static gboolean
537 handle_call (gint        *argc,
538              gchar      **argv[],
539              gboolean     request_completion,
540              const gchar *completion_cur,
541              const gchar *completion_prev)
542 {
543   gint ret;
544   GOptionContext *o;
545   gchar *s;
546   GError *error;
547   GDBusConnection *c;
548   GVariant *parameters;
549   gchar *interface_name;
550   gchar *method_name;
551   GVariant *result;
552   GPtrArray *in_signature_types;
553   gboolean complete_names;
554   gboolean complete_paths;
555   gboolean complete_methods;
556   GVariantBuilder builder;
557   guint n;
558
559   ret = FALSE;
560   c = NULL;
561   parameters = NULL;
562   interface_name = NULL;
563   method_name = NULL;
564   result = NULL;
565   in_signature_types = NULL;
566
567   modify_argv0_for_command (argc, argv, "call");
568
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 ());
574
575   complete_names = FALSE;
576   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
577     {
578       complete_names = TRUE;
579       remove_arg ((*argc) - 1, argc, argv);
580     }
581
582   complete_paths = FALSE;
583   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
584     {
585       complete_paths = TRUE;
586       remove_arg ((*argc) - 1, argc, argv);
587     }
588
589   complete_methods = FALSE;
590   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--method") == 0)
591     {
592       complete_methods = TRUE;
593       remove_arg ((*argc) - 1, argc, argv);
594     }
595
596   if (!g_option_context_parse (o, argc, argv, NULL))
597     {
598       if (!request_completion)
599         {
600           s = g_option_context_get_help (o, FALSE, NULL);
601           g_printerr ("%s", s);
602           g_free (s);
603           goto out;
604         }
605     }
606
607   error = NULL;
608   c = connection_get_dbus_connection (&error);
609   if (c == NULL)
610     {
611       if (request_completion)
612         {
613           if (g_strcmp0 (completion_prev, "--address") == 0)
614             {
615               g_print ("unix:\n"
616                        "tcp:\n"
617                        "nonce-tcp:\n");
618             }
619           else
620             {
621               g_print ("--system \n--session \n--address \n");
622             }
623         }
624       else
625         {
626           g_printerr (_("Error connecting: %s\n"), error->message);
627           g_error_free (error);
628         }
629       goto out;
630     }
631
632   /* validate and complete destination (bus name) */
633   if (g_dbus_connection_get_unique_name (c) != NULL)
634     {
635       /* this only makes sense on message bus connections */
636       if (complete_names)
637         {
638           print_names (c, FALSE);
639           goto out;
640         }
641       if (opt_call_dest == NULL)
642         {
643           if (request_completion)
644             g_print ("--dest \n");
645           else
646             g_printerr (_("Error: Destination is not specified\n"));
647           goto out;
648         }
649       if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
650         {
651           print_names (c, g_str_has_prefix (opt_call_dest, ":"));
652           goto out;
653         }
654     }
655
656   /* validate and complete object path */
657   if (complete_paths)
658     {
659       print_paths (c, opt_call_dest, "/");
660       goto out;
661     }
662   if (opt_call_object_path == NULL)
663     {
664       if (request_completion)
665         g_print ("--object-path \n");
666       else
667         g_printerr (_("Error: Object path is not specified\n"));
668       goto out;
669     }
670   if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
671     {
672       gchar *p;
673       s = g_strdup (opt_call_object_path);
674       p = strrchr (s, '/');
675       if (p != NULL)
676         {
677           if (p == s)
678             p++;
679           *p = '\0';
680         }
681       print_paths (c, opt_call_dest, s);
682       g_free (s);
683       goto out;
684     }
685   if (!request_completion && !g_variant_is_object_path (opt_call_object_path))
686     {
687       g_printerr (_("Error: %s is not a valid object path\n"), opt_call_object_path);
688       goto out;
689     }
690
691   /* validate and complete method (interface + method name) */
692   if (complete_methods)
693     {
694       print_methods (c, opt_call_dest, opt_call_object_path);
695       goto out;
696     }
697   if (opt_call_method == NULL)
698     {
699       if (request_completion)
700         g_print ("--method \n");
701       else
702         g_printerr (_("Error: Method name is not specified\n"));
703       goto out;
704     }
705   if (request_completion && g_strcmp0 ("--method", completion_prev) == 0)
706     {
707       print_methods (c, opt_call_dest, opt_call_object_path);
708       goto out;
709     }
710   s = strrchr (opt_call_method, '.');
711   if (!request_completion && s == NULL)
712     {
713       g_printerr (_("Error: Method name `%s' is invalid\n"), opt_call_method);
714       goto out;
715     }
716   method_name = g_strdup (s + 1);
717   interface_name = g_strndup (opt_call_method, s - opt_call_method);
718
719   /* All done with completion now */
720   if (request_completion)
721     goto out;
722
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,
725                                                             opt_call_dest,
726                                                             opt_call_object_path,
727                                                             interface_name,
728                                                             method_name,
729                                                             &error);
730   if (in_signature_types == NULL)
731     {
732       //g_printerr ("Error getting introspection data: %s\n", error->message);
733       g_error_free (error);
734       error = NULL;
735     }
736
737   /* Read parameters */
738   g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
739   for (n = 1; n < (guint) *argc; n++)
740     {
741       GVariant *value;
742       GVariantType *type;
743
744       type = NULL;
745       if (in_signature_types != NULL)
746         {
747           if (n - 1 >= in_signature_types->len)
748             {
749               /* Only warn for the first param */
750               if (n - 1 == in_signature_types->len)
751                 {
752                   g_printerr ("Warning: Introspection data indicates %d parameters but more was passed\n",
753                               in_signature_types->len);
754                 }
755             }
756           else
757             {
758               type = in_signature_types->pdata[n - 1];
759             }
760         }
761
762       error = NULL;
763       value = g_variant_parse (type,
764                                (*argv)[n],
765                                NULL,
766                                NULL,
767                                &error);
768       if (value == NULL)
769         {
770           g_error_free (error);
771           error = NULL;
772           value = _g_variant_parse_me_harder (type, (*argv)[n], &error);
773           if (value == NULL)
774             {
775               if (type != NULL)
776                 {
777                   s = g_variant_type_dup_string (type);
778                   g_printerr (_("Error parsing parameter %d of type `%s': %s\n"),
779                               n,
780                               s,
781                               error->message);
782                   g_free (s);
783                 }
784               else
785                 {
786                   g_printerr (_("Error parsing parameter %d: %s\n"),
787                               n,
788                               error->message);
789                 }
790               g_error_free (error);
791               g_variant_builder_clear (&builder);
792               goto out;
793             }
794         }
795       g_variant_builder_add_value (&builder, value);
796     }
797   parameters = g_variant_builder_end (&builder);
798
799   if (parameters != NULL)
800     parameters = g_variant_ref_sink (parameters);
801   result = g_dbus_connection_call_sync (c,
802                                         opt_call_dest,
803                                         opt_call_object_path,
804                                         interface_name,
805                                         method_name,
806                                         parameters,
807                                         NULL,
808                                         G_DBUS_CALL_FLAGS_NONE,
809                                         -1,
810                                         NULL,
811                                         &error);
812   if (result == NULL)
813     {
814       g_printerr (_("Error: %s\n"), error->message);
815       g_error_free (error);
816       if (in_signature_types != NULL)
817         {
818           GString *s;
819           s = g_string_new (NULL);
820           for (n = 0; n < in_signature_types->len; n++)
821             {
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));
826             }
827           g_printerr ("(According to introspection data, you need to pass `%s')\n", s->str);
828           g_string_free (s, TRUE);
829         }
830       goto out;
831     }
832
833   s = g_variant_print (result, TRUE);
834   g_print ("%s\n", s);
835   g_free (s);
836
837   ret = TRUE;
838
839  out:
840   if (in_signature_types != NULL)
841     g_ptr_array_unref (in_signature_types);
842   if (result != NULL)
843     g_variant_unref (result);
844   if (c != NULL)
845     g_object_unref (c);
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);
851   return ret;
852 }
853
854 /* ---------------------------------------------------------------------------------------------------- */
855
856 /* TODO: dump annotations */
857
858 static void
859 dump_annotation (const GDBusAnnotationInfo *o,
860                  guint indent,
861                  gboolean ignore_indent)
862 {
863   guint n;
864   g_print ("%*s@%s(\"%s\")\n",
865            ignore_indent ? 0 : indent, "",
866            o->key,
867            o->value);
868   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
869     dump_annotation (o->annotations[n], indent + 2, FALSE);
870 }
871
872 static void
873 dump_arg (const GDBusArgInfo *o,
874           guint indent,
875           const gchar *direction,
876           gboolean ignore_indent,
877           gboolean include_newline)
878 {
879   guint n;
880
881   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
882     {
883       dump_annotation (o->annotations[n], indent, ignore_indent);
884       ignore_indent = FALSE;
885     }
886
887   g_print ("%*s%s%s %s%s",
888            ignore_indent ? 0 : indent, "",
889            direction,
890            o->signature,
891            o->name,
892            include_newline ? ",\n" : "");
893 }
894
895 static guint
896 count_args (GDBusArgInfo **args)
897 {
898   guint n;
899   n = 0;
900   if (args == NULL)
901     goto out;
902   while (args[n] != NULL)
903     n++;
904  out:
905   return n;
906 }
907
908 static void
909 dump_method (const GDBusMethodInfo *o,
910              guint                  indent)
911 {
912   guint n;
913   guint m;
914   guint name_len;
915   guint total_num_args;
916
917   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
918     dump_annotation (o->annotations[n], indent, FALSE);
919
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++)
924     {
925       gboolean ignore_indent = (m == 0);
926       gboolean include_newline = (m != total_num_args - 1);
927
928       dump_arg (o->in_args[n],
929                 indent + name_len + 1,
930                 "in  ",
931                 ignore_indent,
932                 include_newline);
933     }
934   for (n = 0; o->out_args != NULL && o->out_args[n] != NULL; n++, m++)
935     {
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,
940                 "out ",
941                 ignore_indent,
942                 include_newline);
943     }
944   g_print (");\n");
945 }
946
947 static void
948 dump_signal (const GDBusSignalInfo *o,
949              guint                  indent)
950 {
951   guint n;
952   guint name_len;
953   guint total_num_args;
954
955   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
956     dump_annotation (o->annotations[n], indent, FALSE);
957
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++)
962     {
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,
967                 "",
968                 ignore_indent,
969                 include_newline);
970     }
971   g_print (");\n");
972 }
973
974 static void
975 dump_property (const GDBusPropertyInfo *o,
976                guint                    indent,
977                GVariant                *value)
978 {
979   const gchar *access;
980   guint n;
981
982   if (o->flags == G_DBUS_PROPERTY_INFO_FLAGS_READABLE)
983     access = "readonly";
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";
988   else
989     g_assert_not_reached ();
990
991   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
992     dump_annotation (o->annotations[n], indent, FALSE);
993
994   if (value != NULL)
995     {
996       gchar *s = g_variant_print (value, FALSE);
997       g_print ("%*s%s %s %s = %s;\n", indent, "", access, o->signature, o->name, s);
998       g_free (s);
999     }
1000   else
1001     {
1002       g_print ("%*s%s %s %s;\n", indent, "", access, o->signature, o->name);
1003     }
1004 }
1005
1006 static void
1007 dump_interface (GDBusConnection          *c,
1008                 const gchar              *name,
1009                 const GDBusInterfaceInfo *o,
1010                 guint                     indent,
1011                 const gchar              *object_path)
1012 {
1013   guint n;
1014   GHashTable *properties;
1015
1016   properties = g_hash_table_new_full (g_str_hash,
1017                                       g_str_equal,
1018                                       g_free,
1019                                       (GDestroyNotify) g_variant_unref);
1020
1021   /* Try to get properties */
1022   if (c != NULL && name != NULL && object_path != NULL && o->properties != NULL)
1023     {
1024       GVariant *result;
1025       result = g_dbus_connection_call_sync (c,
1026                                             name,
1027                                             object_path,
1028                                             "org.freedesktop.DBus.Properties",
1029                                             "GetAll",
1030                                             g_variant_new ("(s)", o->name),
1031                                             NULL,
1032                                             G_DBUS_CALL_FLAGS_NONE,
1033                                             3000,
1034                                             NULL,
1035                                             NULL);
1036       if (result != NULL)
1037         {
1038           if (g_variant_is_of_type (result, G_VARIANT_TYPE ("(a{sv})")))
1039             {
1040               GVariantIter *iter;
1041               GVariant *item;
1042               g_variant_get (result,
1043                              "(a{sv})",
1044                              &iter);
1045               while ((item = g_variant_iter_next_value (iter)))
1046                 {
1047                   gchar *key;
1048                   GVariant *value;
1049                   g_variant_get (item,
1050                                  "{sv}",
1051                                  &key,
1052                                  &value);
1053
1054                   g_hash_table_insert (properties, key, g_variant_ref (value));
1055                 }
1056             }
1057           g_variant_unref (result);
1058         }
1059       else
1060         {
1061           guint n;
1062           for (n = 0; o->properties != NULL && o->properties[n] != NULL; n++)
1063             {
1064               result = g_dbus_connection_call_sync (c,
1065                                                     name,
1066                                                     object_path,
1067                                                     "org.freedesktop.DBus.Properties",
1068                                                     "Get",
1069                                                     g_variant_new ("(ss)", o->name, o->properties[n]->name),
1070                                                     G_VARIANT_TYPE ("(v)"),
1071                                                     G_DBUS_CALL_FLAGS_NONE,
1072                                                     3000,
1073                                                     NULL,
1074                                                     NULL);
1075               if (result != NULL)
1076                 {
1077                   GVariant *property_value;
1078                   g_variant_get (result,
1079                                  "(v)",
1080                                  &property_value);
1081                   g_hash_table_insert (properties,
1082                                        g_strdup (o->properties[n]->name),
1083                                        g_variant_ref (property_value));
1084                   g_variant_unref (result);
1085                 }
1086             }
1087         }
1088     }
1089
1090   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1091     dump_annotation (o->annotations[n], indent, FALSE);
1092
1093   g_print ("%*sinterface %s {\n", indent, "", o->name);
1094   if (o->methods != NULL)
1095     {
1096       g_print ("%*s  methods:\n", indent, "");
1097       for (n = 0; o->methods[n] != NULL; n++)
1098         dump_method (o->methods[n], indent + 4);
1099     }
1100   if (o->signals != NULL)
1101     {
1102       g_print ("%*s  signals:\n", indent, "");
1103       for (n = 0; o->signals[n] != NULL; n++)
1104         dump_signal (o->signals[n], indent + 4);
1105     }
1106   if (o->properties != NULL)
1107     {
1108       g_print ("%*s  properties:\n", indent, "");
1109       for (n = 0; o->properties[n] != NULL; n++)
1110         {
1111           dump_property (o->properties[n],
1112                          indent + 4,
1113                          g_hash_table_lookup (properties, (o->properties[n])->name));
1114         }
1115     }
1116   g_print ("%*s};\n",
1117            indent, "");
1118
1119   g_hash_table_unref (properties);
1120 }
1121
1122 static void
1123 dump_node (GDBusConnection      *c,
1124            const gchar          *name,
1125            const GDBusNodeInfo  *o,
1126            guint                 indent,
1127            const gchar          *object_path)
1128 {
1129   guint n;
1130   const gchar *object_path_to_print;
1131
1132   object_path_to_print = object_path;
1133   if (o->path != NULL)
1134     object_path_to_print = o->path;
1135
1136   for (n = 0; o->annotations != NULL && o->annotations[n] != NULL; n++)
1137     dump_annotation (o->annotations[n], indent, FALSE);
1138
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)
1141     {
1142       g_print (" {\n");
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);
1147       g_print ("%*s};\n",
1148                indent, "");
1149     }
1150   else
1151     {
1152       g_print ("\n");
1153     }
1154 }
1155
1156 static gchar *opt_introspect_dest = NULL;
1157 static gchar *opt_introspect_object_path = NULL;
1158
1159 static const GOptionEntry introspect_entries[] =
1160 {
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},
1163   { NULL }
1164 };
1165
1166 static gboolean
1167 handle_introspect (gint        *argc,
1168                    gchar      **argv[],
1169                    gboolean     request_completion,
1170                    const gchar *completion_cur,
1171                    const gchar *completion_prev)
1172 {
1173   gint ret;
1174   GOptionContext *o;
1175   gchar *s;
1176   GError *error;
1177   GDBusConnection *c;
1178   GVariant *result;
1179   const gchar *xml_data;
1180   GDBusNodeInfo *node;
1181   gboolean complete_names;
1182   gboolean complete_paths;
1183
1184   ret = FALSE;
1185   c = NULL;
1186   node = NULL;
1187   result = NULL;
1188
1189   modify_argv0_for_command (argc, argv, "introspect");
1190
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 ());
1198
1199   complete_names = FALSE;
1200   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1201     {
1202       complete_names = TRUE;
1203       remove_arg ((*argc) - 1, argc, argv);
1204     }
1205
1206   complete_paths = FALSE;
1207   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1208     {
1209       complete_paths = TRUE;
1210       remove_arg ((*argc) - 1, argc, argv);
1211     }
1212
1213   if (!g_option_context_parse (o, argc, argv, NULL))
1214     {
1215       if (!request_completion)
1216         {
1217           s = g_option_context_get_help (o, FALSE, NULL);
1218           g_printerr ("%s", s);
1219           g_free (s);
1220           goto out;
1221         }
1222     }
1223
1224   error = NULL;
1225   c = connection_get_dbus_connection (&error);
1226   if (c == NULL)
1227     {
1228       if (request_completion)
1229         {
1230           if (g_strcmp0 (completion_prev, "--address") == 0)
1231             {
1232               g_print ("unix:\n"
1233                        "tcp:\n"
1234                        "nonce-tcp:\n");
1235             }
1236           else
1237             {
1238               g_print ("--system \n--session \n--address \n");
1239             }
1240         }
1241       else
1242         {
1243           g_printerr (_("Error connecting: %s\n"), error->message);
1244           g_error_free (error);
1245         }
1246       goto out;
1247     }
1248
1249   if (g_dbus_connection_get_unique_name (c) != NULL)
1250     {
1251       if (complete_names)
1252         {
1253           print_names (c, FALSE);
1254           goto out;
1255         }
1256       /* this only makes sense on message bus connections */
1257       if (opt_introspect_dest == NULL)
1258         {
1259           if (request_completion)
1260             g_print ("--dest \n");
1261           else
1262             g_printerr (_("Error: Destination is not specified\n"));
1263           goto out;
1264         }
1265       if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1266         {
1267           print_names (c, g_str_has_prefix (opt_introspect_dest, ":"));
1268           goto out;
1269         }
1270     }
1271   if (complete_paths)
1272     {
1273       print_paths (c, opt_introspect_dest, "/");
1274       goto out;
1275     }
1276   if (opt_introspect_object_path == NULL)
1277     {
1278       if (request_completion)
1279         g_print ("--object-path \n");
1280       else
1281         g_printerr (_("Error: Object path is not specified\n"));
1282       goto out;
1283     }
1284   if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1285     {
1286       gchar *p;
1287       s = g_strdup (opt_introspect_object_path);
1288       p = strrchr (s, '/');
1289       if (p != NULL)
1290         {
1291           if (p == s)
1292             p++;
1293           *p = '\0';
1294         }
1295       print_paths (c, opt_introspect_dest, s);
1296       g_free (s);
1297       goto out;
1298     }
1299   if (!request_completion && !g_variant_is_object_path (opt_introspect_object_path))
1300     {
1301       g_printerr (_("Error: %s is not a valid object path\n"), opt_introspect_object_path);
1302       goto out;
1303     }
1304
1305   /* All done with completion now */
1306   if (request_completion)
1307     goto out;
1308
1309   result = g_dbus_connection_call_sync (c,
1310                                         opt_introspect_dest,
1311                                         opt_introspect_object_path,
1312                                         "org.freedesktop.DBus.Introspectable",
1313                                         "Introspect",
1314                                         NULL,
1315                                         G_VARIANT_TYPE ("(s)"),
1316                                         G_DBUS_CALL_FLAGS_NONE,
1317                                         3000, /* 3 sec */
1318                                         NULL,
1319                                         &error);
1320   if (result == NULL)
1321     {
1322       g_printerr (_("Error: %s\n"), error->message);
1323       g_error_free (error);
1324       goto out;
1325     }
1326   g_variant_get (result, "(&s)", &xml_data);
1327
1328   error = NULL;
1329   node = g_dbus_node_info_new_for_xml (xml_data, &error);
1330   if (node == NULL)
1331     {
1332       g_printerr (_("Error parsing introspection XML: %s\n"), error->message);
1333       g_error_free (error);
1334       goto out;
1335     }
1336
1337   dump_node (c, opt_introspect_dest, node, 0, opt_introspect_object_path);
1338
1339   ret = TRUE;
1340
1341  out:
1342   if (node != NULL)
1343     g_dbus_node_info_unref (node);
1344   if (result != NULL)
1345     g_variant_unref (result);
1346   if (c != NULL)
1347     g_object_unref (c);
1348   g_option_context_free (o);
1349   return ret;
1350 }
1351
1352 /* ---------------------------------------------------------------------------------------------------- */
1353
1354 static gchar *opt_monitor_dest = NULL;
1355 static gchar *opt_monitor_object_path = NULL;
1356
1357 static guint monitor_filter_id = 0;
1358
1359 static void
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,
1366                    gpointer         user_data)
1367 {
1368   gchar *s;
1369   s = g_variant_print (parameters, TRUE);
1370   g_print ("%s: %s.%s %s\n",
1371            object_path,
1372            interface_name,
1373            signal_name,
1374            s);
1375   g_free (s);
1376 }
1377
1378 static void
1379 monitor_on_name_appeared (GDBusConnection *connection,
1380                           const gchar *name,
1381                           const gchar *name_owner,
1382                           gpointer user_data)
1383 {
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,
1387                                                           name_owner,
1388                                                           NULL,  /* any interface */
1389                                                           NULL,  /* any member */
1390                                                           opt_monitor_object_path,
1391                                                           NULL,  /* arg0 */
1392                                                           monitor_signal_cb,
1393                                                           NULL,  /* user_data */
1394                                                           NULL); /* user_data destroy notify */
1395 }
1396
1397 static void
1398 monitor_on_name_vanished (GDBusConnection *connection,
1399                           const gchar *name,
1400                           gpointer user_data)
1401 {
1402   g_print ("The name %s does not have an owner\n", name);
1403
1404   if (monitor_filter_id != 0)
1405     {
1406       g_dbus_connection_signal_unsubscribe (connection, monitor_filter_id);
1407       monitor_filter_id = 0;
1408     }
1409 }
1410
1411 static const GOptionEntry monitor_entries[] =
1412 {
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},
1415   { NULL }
1416 };
1417
1418 static gboolean
1419 handle_monitor (gint        *argc,
1420                 gchar      **argv[],
1421                 gboolean     request_completion,
1422                 const gchar *completion_cur,
1423                 const gchar *completion_prev)
1424 {
1425   gint ret;
1426   GOptionContext *o;
1427   gchar *s;
1428   GError *error;
1429   GDBusConnection *c;
1430   GVariant *result;
1431   GDBusNodeInfo *node;
1432   gboolean complete_names;
1433   gboolean complete_paths;
1434   GMainLoop *loop;
1435
1436   ret = FALSE;
1437   c = NULL;
1438   node = NULL;
1439   result = NULL;
1440
1441   modify_argv0_for_command (argc, argv, "monitor");
1442
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 ());
1450
1451   complete_names = FALSE;
1452   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--dest") == 0)
1453     {
1454       complete_names = TRUE;
1455       remove_arg ((*argc) - 1, argc, argv);
1456     }
1457
1458   complete_paths = FALSE;
1459   if (request_completion && *argc > 1 && g_strcmp0 ((*argv)[(*argc)-1], "--object-path") == 0)
1460     {
1461       complete_paths = TRUE;
1462       remove_arg ((*argc) - 1, argc, argv);
1463     }
1464
1465   if (!g_option_context_parse (o, argc, argv, NULL))
1466     {
1467       if (!request_completion)
1468         {
1469           s = g_option_context_get_help (o, FALSE, NULL);
1470           g_printerr ("%s", s);
1471           g_free (s);
1472           goto out;
1473         }
1474     }
1475
1476   error = NULL;
1477   c = connection_get_dbus_connection (&error);
1478   if (c == NULL)
1479     {
1480       if (request_completion)
1481         {
1482           if (g_strcmp0 (completion_prev, "--address") == 0)
1483             {
1484               g_print ("unix:\n"
1485                        "tcp:\n"
1486                        "nonce-tcp:\n");
1487             }
1488           else
1489             {
1490               g_print ("--system \n--session \n--address \n");
1491             }
1492         }
1493       else
1494         {
1495           g_printerr (_("Error connecting: %s\n"), error->message);
1496           g_error_free (error);
1497         }
1498       goto out;
1499     }
1500
1501   if (g_dbus_connection_get_unique_name (c) != NULL)
1502     {
1503       if (complete_names)
1504         {
1505           print_names (c, FALSE);
1506           goto out;
1507         }
1508       /* this only makes sense on message bus connections */
1509       if (opt_monitor_dest == NULL)
1510         {
1511           if (request_completion)
1512             g_print ("--dest \n");
1513           else
1514             g_printerr (_("Error: Destination is not specified\n"));
1515           goto out;
1516         }
1517       if (request_completion && g_strcmp0 ("--dest", completion_prev) == 0)
1518         {
1519           print_names (c, g_str_has_prefix (opt_monitor_dest, ":"));
1520           goto out;
1521         }
1522     }
1523   if (complete_paths)
1524     {
1525       print_paths (c, opt_monitor_dest, "/");
1526       goto out;
1527     }
1528   if (opt_monitor_object_path == NULL)
1529     {
1530       if (request_completion)
1531         {
1532           g_print ("--object-path \n");
1533           goto out;
1534         }
1535       /* it's fine to not have an object path */
1536     }
1537   if (request_completion && g_strcmp0 ("--object-path", completion_prev) == 0)
1538     {
1539       gchar *p;
1540       s = g_strdup (opt_monitor_object_path);
1541       p = strrchr (s, '/');
1542       if (p != NULL)
1543         {
1544           if (p == s)
1545             p++;
1546           *p = '\0';
1547         }
1548       print_paths (c, opt_monitor_dest, s);
1549       g_free (s);
1550       goto out;
1551     }
1552   if (!request_completion && (opt_monitor_object_path != NULL && !g_variant_is_object_path (opt_monitor_object_path)))
1553     {
1554       g_printerr (_("Error: %s is not a valid object path\n"), opt_monitor_object_path);
1555       goto out;
1556     }
1557
1558   /* All done with completion now */
1559   if (request_completion)
1560     goto out;
1561
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);
1564   else
1565     g_print ("Monitoring signals from all objects owned by %s\n", opt_monitor_dest);
1566
1567   loop = g_main_loop_new (NULL, FALSE);
1568   g_bus_watch_name_on_connection (c,
1569                                   opt_monitor_dest,
1570                                   G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
1571                                   monitor_on_name_appeared,
1572                                   monitor_on_name_vanished,
1573                                   NULL,
1574                                   NULL);
1575
1576   g_main_loop_run (loop);
1577   g_main_loop_unref (loop);
1578
1579   ret = TRUE;
1580
1581  out:
1582   if (node != NULL)
1583     g_dbus_node_info_unref (node);
1584   if (result != NULL)
1585     g_variant_unref (result);
1586   if (c != NULL)
1587     g_object_unref (c);
1588   g_option_context_free (o);
1589   return ret;
1590 }
1591
1592 /* ---------------------------------------------------------------------------------------------------- */
1593
1594 static gchar *
1595 pick_word_at (const gchar  *s,
1596               gint          cursor,
1597               gint         *out_word_begins_at)
1598 {
1599   gint begin;
1600   gint end;
1601
1602   if (s[0] == '\0')
1603     {
1604       if (out_word_begins_at != NULL)
1605         *out_word_begins_at = -1;
1606       return NULL;
1607     }
1608
1609   if (g_ascii_isspace (s[cursor]) && ((cursor > 0 && g_ascii_isspace(s[cursor-1])) || cursor == 0))
1610     {
1611       if (out_word_begins_at != NULL)
1612         *out_word_begins_at = cursor;
1613       return g_strdup ("");
1614     }
1615
1616   while (!g_ascii_isspace (s[cursor - 1]) && cursor > 0)
1617     cursor--;
1618   begin = cursor;
1619
1620   end = begin;
1621   while (!g_ascii_isspace (s[end]) && s[end] != '\0')
1622     end++;
1623
1624   if (out_word_begins_at != NULL)
1625     *out_word_begins_at = begin;
1626
1627   return g_strndup (s + begin, end - begin);
1628 }
1629
1630 gint
1631 main (gint argc, gchar *argv[])
1632 {
1633   gint ret;
1634   const gchar *command;
1635   gboolean request_completion;
1636   gchar *completion_cur;
1637   gchar *completion_prev;
1638
1639   ret = 1;
1640   completion_cur = NULL;
1641   completion_prev = NULL;
1642
1643   g_type_init ();
1644
1645   if (argc < 2)
1646     {
1647       usage (&argc, &argv, FALSE);
1648       goto out;
1649     }
1650
1651   request_completion = FALSE;
1652
1653   //completion_debug ("---- argc=%d --------------------------------------------------------", argc);
1654
1655  again:
1656   command = argv[1];
1657   if (g_strcmp0 (command, "help") == 0)
1658     {
1659       if (request_completion)
1660         {
1661           /* do nothing */
1662         }
1663       else
1664         {
1665           usage (&argc, &argv, TRUE);
1666           ret = 0;
1667         }
1668       goto out;
1669     }
1670   else if (g_strcmp0 (command, "call") == 0)
1671     {
1672       if (handle_call (&argc,
1673                        &argv,
1674                        request_completion,
1675                        completion_cur,
1676                        completion_prev))
1677         ret = 0;
1678       goto out;
1679     }
1680   else if (g_strcmp0 (command, "introspect") == 0)
1681     {
1682       if (handle_introspect (&argc,
1683                              &argv,
1684                              request_completion,
1685                              completion_cur,
1686                              completion_prev))
1687         ret = 0;
1688       goto out;
1689     }
1690   else if (g_strcmp0 (command, "monitor") == 0)
1691     {
1692       if (handle_monitor (&argc,
1693                           &argv,
1694                           request_completion,
1695                           completion_cur,
1696                           completion_prev))
1697         ret = 0;
1698       goto out;
1699     }
1700   else if (g_strcmp0 (command, "complete") == 0 && argc == 4 && !request_completion)
1701     {
1702       const gchar *completion_line;
1703       gchar **completion_argv;
1704       gint completion_argc;
1705       gint completion_point;
1706       gchar *endp;
1707       gint cur_begin;
1708
1709       request_completion = TRUE;
1710
1711       completion_line = argv[2];
1712       completion_point = strtol (argv[3], &endp, 10);
1713       if (endp == argv[3] || *endp != '\0')
1714         goto out;
1715
1716 #if 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 ("----");
1724 #endif
1725
1726       if (!g_shell_parse_argv (completion_line,
1727                                &completion_argc,
1728                                &completion_argv,
1729                                NULL))
1730         {
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
1734            */
1735           goto out;
1736         }
1737
1738       /* compute cur and prev */
1739       completion_prev = NULL;
1740       completion_cur = pick_word_at (completion_line, completion_point, &cur_begin);
1741       if (cur_begin > 0)
1742         {
1743           gint prev_end;
1744           for (prev_end = cur_begin - 1; prev_end >= 0; prev_end--)
1745             {
1746               if (!g_ascii_isspace (completion_line[prev_end]))
1747                 {
1748                   completion_prev = pick_word_at (completion_line, prev_end, NULL);
1749                   break;
1750                 }
1751             }
1752         }
1753 #if 0
1754       completion_debug (" cur=`%s'", completion_cur);
1755       completion_debug ("prev=`%s'", completion_prev);
1756 #endif
1757
1758       argc = completion_argc;
1759       argv = completion_argv;
1760
1761       ret = 0;
1762
1763       goto again;
1764     }
1765   else
1766     {
1767       if (request_completion)
1768         {
1769           g_print ("help \ncall \nintrospect \nmonitor \n");
1770           ret = 0;
1771           goto out;
1772         }
1773       else
1774         {
1775           g_printerr ("Unknown command `%s'\n", command);
1776           usage (&argc, &argv, FALSE);
1777           goto out;
1778         }
1779     }
1780
1781  out:
1782   g_free (completion_cur);
1783   g_free (completion_prev);
1784   return ret;
1785 }