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 // From gnulib's xalloc.h:
35 /* Return 1 if an array of N objects, each of size S, cannot exist due
36 to size arithmetic overflow. S must be positive and N must be
37 nonnegative. This is a macro, not an inline function, so that it
38 works correctly even when SIZE_MAX < N.
40 By gnulib convention, SIZE_MAX represents overflow in size
41 calculations, so the conservative dividend to use here is
42 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
43 However, malloc (SIZE_MAX) fails on all known hosts where
44 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
45 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
46 branch when S is known to be 1. */
47 # define xalloc_oversized(n, s) \
48 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
50 /* Readline completion for paths on the guest filesystem, also for
51 * devices and LVM names.
54 int complete_dest_paths = 1;
62 free_words (struct word *words, size_t nr_words)
66 /* NB. 'words' array is NOT NULL-terminated. */
67 for (i = 0; i < nr_words; ++i)
73 complete_dest_paths_generator (const char *text, int state)
75 #ifdef HAVE_LIBREADLINE
77 static size_t len, index;
78 static struct word *words = NULL;
79 static size_t nr_words = 0;
80 guestfs_error_handler_cb old_error_cb;
81 void *old_error_cb_data;
83 /* Temporarily replace the error handler so that messages don't
84 * get printed to stderr while we are issuing commands.
86 #define SAVE_ERROR_CB \
87 old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data); \
88 guestfs_set_error_handler (g, NULL, NULL);
90 /* Restore error handler. */
91 #define RESTORE_ERROR_CB \
92 guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
100 if (words) free_words (words, nr_words);
107 /* Silently do nothing if an allocation fails */
108 #define APPEND_STRS_AND_FREE \
112 size_t n = count_strings (strs); \
114 if ( n > 0 && ! xalloc_oversized (nr_words + n, sizeof (struct word))) { \
116 w = realloc (words, sizeof (struct word) * (nr_words + n)); \
119 free_words (words, nr_words); \
124 for (i = 0; i < n; ++i) { \
125 words[nr_words].name = strs[i]; \
126 words[nr_words].is_dir = 0; \
135 /* Is it a device? */
136 if (len < 5 || strncmp (text, "/dev/", 5) == 0) {
137 /* Get a list of everything that can possibly begin with /dev/ */
138 strs = guestfs_list_devices (g);
139 APPEND_STRS_AND_FREE;
141 strs = guestfs_list_partitions (g);
142 APPEND_STRS_AND_FREE;
144 strs = guestfs_lvs (g);
145 APPEND_STRS_AND_FREE;
148 if (len < 1 || text[0] == '/') {
149 /* If we've got a partial path already, we need to list everything
150 * in that directory, otherwise list everything in /
153 struct guestfs_dirent_list *dirents;
155 p = strrchr (text, '/');
156 dir = p && p > text ? strndup (text, p - text) : strdup ("/");
158 dirents = guestfs_readdir (g, dir);
160 /* Prepend directory to names before adding them to the list
166 for (i = 0; i < dirents->len; ++i) {
169 if (strcmp (dirents->val[i].name, ".") != 0 &&
170 strcmp (dirents->val[i].name, "..") != 0) {
171 if (strcmp (dir, "/") == 0)
172 err = asprintf (&p, "/%s", dirents->val[i].name);
174 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
176 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
179 w = realloc (words, sizeof (struct word) * (nr_words+1));
181 free_words (words, nr_words);
187 words[nr_words].name = p;
188 words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
196 guestfs_free_dirent_list (dirents);
201 /* else ... In theory we could complete other things here such as VG
202 * names. At the moment we don't do that.
208 /* This inhibits ordinary (local filename) completion. */
209 rl_attempted_completion_over = 1;
211 /* Complete the string. */
212 while (index < nr_words) {
215 word = &words[index];
218 if (strncasecmp (word->name, text, len) == 0) {
220 rl_completion_append_character = '/';
222 return strdup (word->name);
226 #endif /* HAVE_LIBREADLINE */