maint: use COPYING.LIB version 2.1
[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 <stddef.h>
26 #include <string.h>
27
28 #ifdef HAVE_LIBREADLINE
29 #include <readline/readline.h>
30 #endif
31
32 #include <guestfs.h>
33
34 #include "fish.h"
35
36 // From gnulib's xalloc.h:
37 /* Return 1 if an array of N objects, each of size S, cannot exist due
38    to size arithmetic overflow.  S must be positive and N must be
39    nonnegative.  This is a macro, not an inline function, so that it
40    works correctly even when SIZE_MAX < N.
41
42    By gnulib convention, SIZE_MAX represents overflow in size
43    calculations, so the conservative dividend to use here is
44    SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
45    However, malloc (SIZE_MAX) fails on all known hosts where
46    sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
47    exactly-SIZE_MAX allocations on such hosts; this avoids a test and
48    branch when S is known to be 1.  */
49 # define xalloc_oversized(n, s) \
50     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
51
52 /* Readline completion for paths on the guest filesystem, also for
53  * devices and LVM names.
54  */
55
56 int complete_dest_paths = 1;
57
58 struct word {
59   char *name;
60   int is_dir;
61 };
62
63 static void
64 free_words (struct word *words, int nr_words)
65 {
66   size_t i;
67
68   /* NB. 'words' array is NOT NULL-terminated. */
69   for (i = 0; i < nr_words; ++i)
70     free (words[i].name);
71   free (words);
72 }
73
74 char *
75 complete_dest_paths_generator (const char *text, int state)
76 {
77 #ifdef HAVE_LIBREADLINE
78
79   static size_t len, index;
80   static struct word *words = NULL;
81   static size_t nr_words = 0;
82   guestfs_error_handler_cb old_error_cb;
83   void *old_error_cb_data;
84
85   /* Temporarily replace the error handler so that messages don't
86    * get printed to stderr while we are issuing commands.
87    */
88 #define SAVE_ERROR_CB                                                   \
89   old_error_cb = guestfs_get_error_handler (g, &old_error_cb_data);     \
90   guestfs_set_error_handler (g, NULL, NULL);
91
92   /* Restore error handler. */
93 #define RESTORE_ERROR_CB                                                \
94   guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
95
96   if (!state) {
97     char **strs;
98
99     len = strlen (text);
100     index = 0;
101
102     if (words) free_words (words, nr_words);
103
104     words = NULL;
105     nr_words = 0;
106
107     SAVE_ERROR_CB
108
109 /* Silently do nothing if an allocation fails */
110 #define APPEND_STRS_AND_FREE                                            \
111   do {                                                                  \
112     if (strs) {                                                         \
113       size_t i;                                                         \
114       size_t n = count_strings (strs);                                  \
115                                                                         \
116       if ( ! xalloc_oversized (nr_words + n, sizeof (struct word))) {   \
117         struct word *w;                                                 \
118         w = realloc (words, sizeof (struct word) * (nr_words + n));     \
119                                                                         \
120         if (w == NULL) {                                                \
121           free_words (words, nr_words);                                 \
122           words = NULL;                                                 \
123           nr_words = 0;                                                 \
124         } else {                                                        \
125           words = w;                                                    \
126           for (i = 0; i < n; ++i) {                                     \
127             words[nr_words].name = strs[i];                             \
128             words[nr_words].is_dir = 0;                                 \
129             nr_words++;                                                 \
130           }                                                             \
131         }                                                               \
132         free (strs);                                                    \
133       }                                                                 \
134     }                                                                   \
135   } while (0)
136
137     /* Is it a device? */
138     if (len < 5 || strncmp (text, "/dev/", 5) == 0) {
139       /* Get a list of everything that can possibly begin with /dev/ */
140       strs = guestfs_list_devices (g);
141       APPEND_STRS_AND_FREE;
142
143       strs = guestfs_list_partitions (g);
144       APPEND_STRS_AND_FREE;
145
146       strs = guestfs_lvs (g);
147       APPEND_STRS_AND_FREE;
148     }
149
150     if (len < 1 || text[0] == '/') {
151       /* If we've got a partial path already, we need to list everything
152        * in that directory, otherwise list everything in /
153        */
154       char *p, *dir;
155       struct guestfs_dirent_list *dirents;
156
157       p = strrchr (text, '/');
158       dir = p && p > text ? strndup (text, p - text) : strdup ("/");
159       if (dir) {
160         dirents = guestfs_readdir (g, dir);
161
162         /* Prepend directory to names before adding them to the list
163          * of words.
164          */
165         if (dirents) {
166           size_t i;
167
168           for (i = 0; i < dirents->len; ++i) {
169             int err;
170
171             if (strcmp (dirents->val[i].name, ".") != 0 &&
172                 strcmp (dirents->val[i].name, "..") != 0) {
173               if (strcmp (dir, "/") == 0)
174                 err = asprintf (&p, "/%s", dirents->val[i].name);
175               else
176                 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
177               if (err >= 0) {
178                 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
179                   struct word *w;
180
181                   w = realloc (words, sizeof (struct word) * (nr_words+1));
182                   if (w == NULL) {
183                     free_words (words, nr_words);
184                     words = NULL;
185                     nr_words = 0;
186                   }
187                   else {
188                     words = w;
189                     words[nr_words].name = p;
190                     words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
191                     nr_words++;
192                   }
193                 }
194               }
195             }
196           }
197
198           guestfs_free_dirent_list (dirents);
199         }
200       }
201     }
202
203     /* else ...  In theory we could complete other things here such as VG
204      * names.  At the moment we don't do that.
205      */
206
207     RESTORE_ERROR_CB
208   }
209
210   /* This inhibits ordinary (local filename) completion. */
211   rl_attempted_completion_over = 1;
212
213   /* Complete the string. */
214   while (index < nr_words) {
215     struct word *word;
216
217     word = &words[index];
218     index++;
219
220     if (strncasecmp (word->name, text, len) == 0) {
221       if (word->is_dir)
222         rl_completion_append_character = '/';
223
224       return strdup (word->name);
225     }
226   }
227
228 #endif /* HAVE_LIBREADLINE */
229
230   return NULL;
231 }