Fix detection of optional libvirt support in virt-inspector.
[libguestfs.git] / fish / destpaths.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #define _GNU_SOURCE             // for strndup, asprintf
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #ifdef HAVE_LIBREADLINE
28 #include <readline/readline.h>
29 #endif
30
31 #include <guestfs.h>
32
33 #include "fish.h"
34
35 /* Readline completion for paths on the guest filesystem, also for
36  * devices and LVM names.
37  */
38
39 int complete_dest_paths = 0; /* SEE NOTE */
40
41 /* NOTE: This is currently disabled by default (with no way to
42  * enable it).  That's because it's not particularly natural.
43  *
44  * Also there is a quite serious performance problem.  When listing
45  * even moderately long directories, this takes many seconds.  The
46  * reason is because it calls guestfs_is_dir on each directory
47  * entry, thus lots of round trips to the server.  We could have
48  * a "readdir and stat each entry" call to ease this.
49  */
50
51 char *
52 complete_dest_paths_generator (const char *text, int state)
53 {
54 #ifdef HAVE_LIBREADLINE
55
56   static int len, index;
57   static char **words = NULL;
58   static int nr_words = 0;
59   char *word;
60   guestfs_error_handler_cb old_error_cb;
61   void *old_error_cb_data;
62
63   /* Temporarily replace the error handler so that messages don't
64    * get printed to stderr while we are issuing commands.
65    */
66 #define SAVE_ERROR_CB                                                   \
67   old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data);     \
68   guestfs_set_error_handler (g, NULL, NULL);
69
70   /* Restore error handler. */
71 #define RESTORE_ERROR_CB                                                \
72   guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
73
74   if (!state) {
75     char **strs;
76     int i, n;
77
78     len = strlen (text);
79     index = 0;
80
81     if (words) {
82       /* NB. 'words' array is NOT NULL-terminated. */
83       for (i = 0; i < nr_words; ++i)
84         free (words[i]);
85       free (words);
86     }
87
88     words = NULL;
89     nr_words = 0;
90
91     SAVE_ERROR_CB
92
93 #define APPEND_STRS_AND_FREE                                            \
94     if (strs) {                                                         \
95       n = count_strings (strs);                                         \
96       words = realloc (words, sizeof (char *) * (nr_words + n));        \
97       for (i = 0; i < n; ++i)                                           \
98         words[nr_words++] = strs[i];                                    \
99       free (strs);                                                      \
100     }
101
102     /* Is it a device? */
103     if (len < 5 || strncmp (text, "/dev/", 5) == 0) {
104       /* Get a list of everything that can possibly begin with /dev/ */
105       strs = guestfs_list_devices (g);
106       APPEND_STRS_AND_FREE
107
108       strs = guestfs_list_partitions (g);
109       APPEND_STRS_AND_FREE
110
111       strs = guestfs_lvs (g);
112       APPEND_STRS_AND_FREE
113     }
114
115     if (len < 1 || text[0] == '/') {
116       /* If we've got a partial path already, we need to list everything
117        * in that directory, otherwise list everything in /
118        */
119       char *p, *dir;
120
121       p = strrchr (text, '/');
122       dir = p && p > text ? strndup (text, p - text) : strdup ("/");
123
124       strs = guestfs_ls (g, dir);
125
126       /* Prepend directory to names. */
127       if (strs) {
128         for (i = 0; strs[i]; ++i) {
129           p = NULL;
130           if (strcmp (dir, "/") == 0)
131             asprintf (&p, "/%s", strs[i]);
132           else
133             asprintf (&p, "%s/%s", dir, strs[i]);
134           free (strs[i]);
135           strs[i] = p;
136         }
137       }
138
139       free (dir);
140       APPEND_STRS_AND_FREE
141     }
142
143     /* else ...  In theory we could complete other things here such as VG
144      * names.  At the moment we don't do that.
145      */
146
147     RESTORE_ERROR_CB
148   }
149
150   /* This inhibits ordinary (local filename) completion. */
151   rl_attempted_completion_over = 1;
152
153   /* Complete the string. */
154   while (index < nr_words) {
155     word = words[index];
156     index++;
157     if (strncasecmp (word, text, len) == 0) {
158       /* Is it a directory? */
159       if (strncmp (word, "/dev/", 5) != 0) {
160         SAVE_ERROR_CB
161         if (guestfs_is_dir (g, word) > 0)
162           rl_completion_append_character = '/';
163         RESTORE_ERROR_CB
164       }
165
166       return strdup (word);
167     }
168   }
169
170 #endif /* HAVE_LIBREADLINE */
171
172   return NULL;
173 }