Fix compilation when Augeas is not present.
[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 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25
26 #ifdef HAVE_LIBREADLINE
27 #include <readline/readline.h>
28 #endif
29
30 #include <guestfs.h>
31
32 #include "fish.h"
33
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.
39
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))
49
50 /* Readline completion for paths on the guest filesystem, also for
51  * devices and LVM names.
52  */
53
54 int complete_dest_paths = 1;
55
56 struct word {
57   char *name;
58   int is_dir;
59 };
60
61 static void
62 free_words (struct word *words, size_t nr_words)
63 {
64   size_t i;
65
66   /* NB. 'words' array is NOT NULL-terminated. */
67   for (i = 0; i < nr_words; ++i)
68     free (words[i].name);
69   free (words);
70 }
71
72 char *
73 complete_dest_paths_generator (const char *text, int state)
74 {
75 #ifdef HAVE_LIBREADLINE
76
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;
82
83   /* Temporarily replace the error handler so that messages don't
84    * get printed to stderr while we are issuing commands.
85    */
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);
89
90   /* Restore error handler. */
91 #define RESTORE_ERROR_CB                                                \
92   guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
93
94   if (!state) {
95     char **strs;
96
97     len = strlen (text);
98     index = 0;
99
100     if (words) free_words (words, nr_words);
101
102     words = NULL;
103     nr_words = 0;
104
105     SAVE_ERROR_CB
106
107 /* Silently do nothing if an allocation fails */
108 #define APPEND_STRS_AND_FREE                                            \
109   do {                                                                  \
110     if (strs) {                                                         \
111       size_t i;                                                         \
112       size_t n = count_strings (strs);                                  \
113                                                                         \
114       if ( n > 0 && ! xalloc_oversized (nr_words + n, sizeof (struct word))) { \
115         struct word *w;                                                 \
116         w = realloc (words, sizeof (struct word) * (nr_words + n));     \
117                                                                         \
118         if (w == NULL) {                                                \
119           free_words (words, nr_words);                                 \
120           words = NULL;                                                 \
121           nr_words = 0;                                                 \
122         } else {                                                        \
123           words = w;                                                    \
124           for (i = 0; i < n; ++i) {                                     \
125             words[nr_words].name = strs[i];                             \
126             words[nr_words].is_dir = 0;                                 \
127             nr_words++;                                                 \
128           }                                                             \
129         }                                                               \
130       }                                                                 \
131       free (strs);                                                      \
132     }                                                                   \
133   } while (0)
134
135     /* Is it a device? */
136     if (len < 5 || STREQLEN (text, "/dev/", 5)) {
137       /* Get a list of everything that can possibly begin with /dev/ */
138       strs = guestfs_list_devices (g);
139       APPEND_STRS_AND_FREE;
140
141       strs = guestfs_list_partitions (g);
142       APPEND_STRS_AND_FREE;
143
144       strs = guestfs_lvs (g);
145       APPEND_STRS_AND_FREE;
146     }
147
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 /
151        */
152       char *p, *dir;
153       struct guestfs_dirent_list *dirents;
154
155       p = strrchr (text, '/');
156       dir = p && p > text ? strndup (text, p - text) : strdup ("/");
157       if (dir) {
158         dirents = guestfs_readdir (g, dir);
159
160         /* Prepend directory to names before adding them to the list
161          * of words.
162          */
163         if (dirents) {
164           size_t i;
165
166           for (i = 0; i < dirents->len; ++i) {
167             int err;
168
169             if (STRNEQ (dirents->val[i].name, ".") &&
170                 STRNEQ (dirents->val[i].name, "..")) {
171               if (STREQ (dir, "/"))
172                 err = asprintf (&p, "/%s", dirents->val[i].name);
173               else
174                 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
175               if (err >= 0) {
176                 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
177                   struct word *w;
178
179                   w = realloc (words, sizeof (struct word) * (nr_words+1));
180                   if (w == NULL) {
181                     free_words (words, nr_words);
182                     words = NULL;
183                     nr_words = 0;
184                   }
185                   else {
186                     words = w;
187                     words[nr_words].name = p;
188                     words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
189                     nr_words++;
190                   }
191                 }
192               }
193             }
194           }
195
196           guestfs_free_dirent_list (dirents);
197         }
198       }
199     }
200
201     /* else ...  In theory we could complete other things here such as VG
202      * names.  At the moment we don't do that.
203      */
204
205     RESTORE_ERROR_CB
206   }
207
208   /* This inhibits ordinary (local filename) completion. */
209   rl_attempted_completion_over = 1;
210
211   /* Complete the string. */
212   while (index < nr_words) {
213     struct word *word;
214
215     word = &words[index];
216     index++;
217
218     if (STRCASEEQLEN (word->name, text, len)) {
219       if (word->is_dir)
220         rl_completion_append_character = '/';
221
222       return strdup (word->name);
223     }
224   }
225
226 #endif /* HAVE_LIBREADLINE */
227
228   return NULL;
229 }