daemon: debug segv correct use of dereferencing NULL.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #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.
40
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))
50 #endif
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 #ifdef HAVE_LIBREADLINE
64 static void
65 free_words (struct word *words, size_t nr_words)
66 {
67   size_t i;
68
69   /* NB. 'words' array is NOT NULL-terminated. */
70   for (i = 0; i < nr_words; ++i)
71     free (words[i].name);
72   free (words);
73 }
74
75 static int
76 compare_words (const void *vp1, const void *vp2)
77 {
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);
81 }
82 #endif
83
84 char *
85 complete_dest_paths_generator (const char *text, int state)
86 {
87 #ifdef HAVE_LIBREADLINE
88
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;
94
95   /* Temporarily replace the error handler so that messages don't
96    * get printed to stderr while we are issuing commands.
97    */
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);
101
102   /* Restore error handler. */
103 #define RESTORE_ERROR_CB                                                \
104   guestfs_set_error_handler (g, old_error_cb, old_error_cb_data);
105
106   if (!state) {
107     char **strs;
108
109     len = strlen (text);
110     index = 0;
111
112     if (words) free_words (words, nr_words);
113
114     words = NULL;
115     nr_words = 0;
116
117     SAVE_ERROR_CB
118
119 /* Silently do nothing if an allocation fails */
120 #define APPEND_STRS_AND_FREE                                            \
121   do {                                                                  \
122     if (strs) {                                                         \
123       size_t i;                                                         \
124       size_t n = count_strings (strs);                                  \
125                                                                         \
126       if ( n > 0 && ! xalloc_oversized (nr_words + n, sizeof (struct word))) { \
127         struct word *w;                                                 \
128         w = realloc (words, sizeof (struct word) * (nr_words + n));     \
129                                                                         \
130         if (w == NULL) {                                                \
131           free_words (words, nr_words);                                 \
132           words = NULL;                                                 \
133           nr_words = 0;                                                 \
134         } else {                                                        \
135           words = w;                                                    \
136           for (i = 0; i < n; ++i) {                                     \
137             words[nr_words].name = strs[i];                             \
138             words[nr_words].is_dir = 0;                                 \
139             nr_words++;                                                 \
140           }                                                             \
141         }                                                               \
142       }                                                                 \
143       free (strs);                                                      \
144     }                                                                   \
145   } while (0)
146
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;
152
153       strs = guestfs_list_partitions (g);
154       APPEND_STRS_AND_FREE;
155
156       strs = guestfs_lvs (g);
157       APPEND_STRS_AND_FREE;
158
159       strs = guestfs_list_dm_devices (g);
160       APPEND_STRS_AND_FREE;
161
162       strs = guestfs_list_md_devices (g);
163       APPEND_STRS_AND_FREE;
164     }
165
166     if (len < 1 || text[0] == '/') {
167       /* If we've got a partial path already, we need to list everything
168        * in that directory, otherwise list everything in /
169        */
170       char *p, *dir;
171       struct guestfs_dirent_list *dirents;
172
173       p = strrchr (text, '/');
174       dir = p && p > text ? strndup (text, p - text) : strdup ("/");
175       if (dir) {
176         dirents = guestfs_readdir (g, dir);
177
178         /* Prepend directory to names before adding them to the list
179          * of words.
180          */
181         if (dirents) {
182           size_t i;
183
184           for (i = 0; i < dirents->len; ++i) {
185             int err;
186
187             if (STRNEQ (dirents->val[i].name, ".") &&
188                 STRNEQ (dirents->val[i].name, "..")) {
189               if (STREQ (dir, "/"))
190                 err = asprintf (&p, "/%s", dirents->val[i].name);
191               else
192                 err = asprintf (&p, "%s/%s", dir, dirents->val[i].name);
193               if (err >= 0) {
194                 if (!xalloc_oversized (nr_words+1, sizeof (struct word))) {
195                   struct word *w;
196
197                   w = realloc (words, sizeof (struct word) * (nr_words+1));
198                   if (w == NULL) {
199                     free_words (words, nr_words);
200                     words = NULL;
201                     nr_words = 0;
202                   }
203                   else {
204                     words = w;
205                     words[nr_words].name = p;
206                     words[nr_words].is_dir = dirents->val[i].ftyp == 'd';
207                     nr_words++;
208                   }
209                 }
210               }
211             }
212           }
213
214           guestfs_free_dirent_list (dirents);
215         }
216       }
217     }
218
219     /* else ...  In theory we could complete other things here such as VG
220      * names.  At the moment we don't do that.
221      */
222
223     RESTORE_ERROR_CB
224   }
225
226   /* This inhibits ordinary (local filename) completion. */
227   rl_attempted_completion_over = 1;
228
229   /* Sort the words so the list is stable over multiple calls. */
230   qsort (words, nr_words, sizeof (struct word), compare_words);
231
232   /* Complete the string. */
233   while (index < nr_words) {
234     struct word *word;
235
236     word = &words[index];
237     index++;
238
239     /* Whether we should match case insensitively here or not is
240      * determined by the value of the completion-ignore-case readline
241      * variable.  Default to case insensitive.  (See: RHBZ#582993).
242      */
243     char *cic_var = rl_variable_value ("completion-ignore-case");
244     int cic = 1;
245     if (cic_var && STREQ (cic_var, "off"))
246       cic = 0;
247
248     int matches =
249       cic ? STRCASEEQLEN (word->name, text, len)
250           : STREQLEN (word->name, text, len);
251
252     if (matches) {
253       if (word->is_dir)
254         rl_completion_append_character = '/';
255
256       return strdup (word->name);
257     }
258   }
259
260 #endif /* HAVE_LIBREADLINE */
261
262   return NULL;
263 }