1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
26 #ifdef HAVE_LIBREADLINE
27 #include <readline/readline.h>
34 #ifdef HAVE_LIBREADLINE
35 // From gnulib's xalloc.h:
36 /* Return 1 if an array of N objects, each of size S, cannot exist due
37 to size arithmetic overflow. S must be positive and N must be
38 nonnegative. This is a macro, not an inline function, so that it
39 works correctly even when SIZE_MAX < N.
41 By gnulib convention, SIZE_MAX represents overflow in size
42 calculations, so the conservative dividend to use here is
43 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
44 However, malloc (SIZE_MAX) fails on all known hosts where
45 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
46 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
47 branch when S is known to be 1. */
48 # define xalloc_oversized(n, s) \
49 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
52 /* Readline completion for paths on the guest filesystem, also for
53 * devices and LVM names.
56 int complete_dest_paths = 1;
63 #ifdef HAVE_LIBREADLINE
65 free_words (struct word *words, size_t nr_words)
69 /* NB. 'words' array is NOT NULL-terminated. */
70 for (i = 0; i < nr_words; ++i)
76 compare_words (const void *vp1, const void *vp2)
78 const struct word *w1 = (const struct word *) vp1;
79 const struct word *w2 = (const struct word *) vp2;
80 return strcmp (w1->name, w2->name);
85 complete_dest_paths_generator (const char *text, int state)
87 #ifdef HAVE_LIBREADLINE
89 static size_t len, index;
90 static struct word *words = NULL;
91 static size_t nr_words = 0;
92 guestfs_error_handler_cb old_error_cb;
93 void *old_error_cb_data;
95 /* Temporarily replace the error handler so that messages don't
96 * get printed to stderr while we are issuing commands.
98 #define SAVE_ERROR_CB \
99 old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data); \
100 guestfs_set_error_handler (g, NULL, NULL);
102 /* Restore error handler. */
103 #define RESTORE_ERROR_CB \
104 guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
112 if (words) free_words (words, nr_words);
119 /* Silently do nothing if an allocation fails */
120 #define APPEND_STRS_AND_FREE \
124 size_t n = count_strings (strs); \
126 if ( n > 0 && ! xalloc_oversized (nr_words + n, sizeof (struct word))) { \
128 w = realloc (words, sizeof (struct word) * (nr_words + n)); \
131 free_words (words, nr_words); \
136 for (i = 0; i < n; ++i) { \
137 words[nr_words].name = strs[i]; \
138 words[nr_words].is_dir = 0; \
147 /* Is it a device? */
148 if (len < 5 || STREQLEN (text, "/dev/", 5)) {
149 /* Get a list of everything that can possibly begin with /dev/ */
150 strs = guestfs_list_devices (g);
151 APPEND_STRS_AND_FREE;
153 strs = guestfs_list_partitions (g);
154 APPEND_STRS_AND_FREE;
156 strs = guestfs_lvs (g);
157 APPEND_STRS_AND_FREE;
159 strs = guestfs_list_dm_devices (g);
160 APPEND_STRS_AND_FREE;
163 if (len < 1 || text[0] == '/') {
164 /* If we've got a partial path already, we need to list everything
165 * in that directory, otherwise list everything in /
168 struct guestfs_dirent_list *dirents;
170 p = strrchr (text, '/');
171 dir = p && p > text ? strndup (text, p - text) : strdup ("/");
173 dirents = guestfs_readdir (g, dir);
175 /* Prepend directory to names before adding them to the list
181 for (i = 0; i < dirents->len; ++i) {
184 if (STRNEQ (dirents->val[i].name, ".") &&
185 STRNEQ (dirents->val[i].name, "..")) {
186 if (STREQ (dir, "/"))
187 err = asprintf (&p, "/%s", dirents->val[i].name);
189 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
191 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
194 w = realloc (words, sizeof (struct word) * (nr_words+1));
196 free_words (words, nr_words);
202 words[nr_words].name = p;
203 words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
211 guestfs_free_dirent_list (dirents);
216 /* else ... In theory we could complete other things here such as VG
217 * names. At the moment we don't do that.
223 /* This inhibits ordinary (local filename) completion. */
224 rl_attempted_completion_over = 1;
226 /* Sort the words so the list is stable over multiple calls. */
227 qsort (words, nr_words, sizeof (struct word), compare_words);
229 /* Complete the string. */
230 while (index < nr_words) {
233 word = &words[index];
236 /* Whether we should match case insensitively here or not is
237 * determined by the value of the completion-ignore-case readline
238 * variable. Default to case insensitive. (See: RHBZ#582993).
240 char *cic_var = rl_variable_value ("completion-ignore-case");
242 if (cic_var && STREQ (cic_var, "off"))
246 cic ? STRCASEEQLEN (word->name, text, len)
247 : STREQLEN (word->name, text, len);
251 rl_completion_append_character = '/';
253 return strdup (word->name);
257 #endif /* HAVE_LIBREADLINE */