]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/ftp/complete.c
Reverse the FTP_PASSIVE_MODE check, so it checks for "is not NO" rather
[FreeBSD/FreeBSD.git] / usr.bin / ftp / complete.c
1 /*      $Id$    */
2 /*      $NetBSD: complete.c,v 1.11 1997/09/13 09:05:53 lukem Exp $      */
3
4 /*-
5  * Copyright (c) 1997 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Luke Mewburn.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #ifndef SMALL
41
42 #include <sys/cdefs.h>
43 #ifndef lint
44 __RCSID("$Id$");
45 __RCSID_SOURCE("$NetBSD: complete.c,v 1.11 1997/09/13 09:05:53 lukem Exp $");
46 #endif /* not lint */
47
48 /*
49  * FTP user program - command and file completion routines
50  */
51
52 #include <sys/types.h>
53 #include <ctype.h>
54 #include <err.h>
55 #include <dirent.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include "ftp_var.h"
61
62 static int
63 comparstr(a, b)
64         const void *a, *b;
65 {
66         return (strcoll(*(char **)a, *(char **)b));
67 }
68
69 /*
70  * Determine if complete is ambiguous. If unique, insert.
71  * If no choices, error. If unambiguous prefix, insert that.
72  * Otherwise, list choices. words is assumed to be filtered
73  * to only contain possible choices.
74  * Args:
75  *      word    word which started the match
76  *      list    list by default
77  *      words   stringlist containing possible matches
78  */
79 static unsigned char
80 complete_ambiguous(word, list, words)
81         char *word;
82         int list;
83         StringList *words;
84 {
85         char insertstr[MAXPATHLEN];
86         char *lastmatch;
87         int i, j;
88         size_t matchlen, wordlen;
89
90         wordlen = strlen(word);
91         if (words->sl_cur == 0)
92                 return (CC_ERROR);      /* no choices available */
93
94         if (words->sl_cur == 1) {       /* only once choice available */
95                 (void)strcpy(insertstr, words->sl_str[0]);
96                 if (el_insertstr(el, insertstr + wordlen) == -1)
97                         return (CC_ERROR);
98                 else
99                         return (CC_REFRESH);
100         }
101
102         if (!list) {
103                 matchlen = 0;
104                 lastmatch = words->sl_str[0];
105                 matchlen = strlen(lastmatch);
106                 for (i = 1 ; i < words->sl_cur ; i++) {
107                         for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
108                                 if (lastmatch[j] != words->sl_str[i][j])
109                                         break;
110                         if (j < matchlen)
111                                 matchlen = j;
112                 }
113                 if (matchlen > wordlen) {
114                         (void)strncpy(insertstr, lastmatch, matchlen);
115                         insertstr[matchlen] = '\0';
116                         if (el_insertstr(el, insertstr + wordlen) == -1)
117                                 return (CC_ERROR);
118                         else    
119                                         /*
120                                          * XXX: really want CC_REFRESH_BEEP
121                                          */
122                                 return (CC_REFRESH);
123                 }
124         }
125
126         putchar('\n');
127         qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
128         list_vertical(words);
129         return (CC_REDISPLAY);
130 }
131
132 /*
133  * Complete a command
134  */
135 static unsigned char
136 complete_command(word, list)
137         char *word;
138         int list;
139 {
140         struct cmd *c;
141         StringList *words;
142         size_t wordlen;
143         unsigned char rv;
144
145         words = sl_init();
146         wordlen = strlen(word);
147
148         for (c = cmdtab; c->c_name != NULL; c++) {
149                 if (wordlen > strlen(c->c_name))
150                         continue;
151                 if (strncmp(word, c->c_name, wordlen) == 0)
152                         sl_add(words, c->c_name);
153         }
154
155         rv = complete_ambiguous(word, list, words);
156         sl_free(words, 0);
157         return (rv);
158 }
159
160 /*
161  * Complete a local file
162  */
163 static unsigned char
164 complete_local(word, list)
165         char *word;
166         int list;
167 {
168         StringList *words;
169         char dir[MAXPATHLEN];
170         char *file;
171         DIR *dd;
172         struct dirent *dp;
173         unsigned char rv;
174
175         if ((file = strrchr(word, '/')) == NULL) {
176                 dir[0] = '.';
177                 dir[1] = '\0';
178                 file = word;
179         } else {
180                 if (file == word) {
181                         dir[0] = '/';
182                         dir[1] = '\0';
183                 } else {
184                         (void)strncpy(dir, word, file - word);
185                         dir[file - word] = '\0';
186                 }
187                 file++;
188         }
189
190         if ((dd = opendir(dir)) == NULL)
191                 return (CC_ERROR);
192
193         words = sl_init();
194
195         for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
196                 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
197                         continue;
198                 if (strlen(file) > dp->d_namlen)
199                         continue;
200                 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
201                         char *tcp;
202
203                         tcp = strdup(dp->d_name);
204                         if (tcp == NULL)
205                                 errx(1, "Can't allocate memory for local dir");
206                         sl_add(words, tcp);
207                 }
208         }
209         closedir(dd);
210
211         rv = complete_ambiguous(file, list, words);
212         sl_free(words, 1);
213         return (rv);
214 }
215
216 /*
217  * Complete a remote file
218  */
219 static unsigned char
220 complete_remote(word, list)
221         char *word;
222         int list;
223 {
224         static StringList *dirlist;
225         static char      lastdir[MAXPATHLEN];
226         StringList      *words;
227         char             dir[MAXPATHLEN];
228         char            *file, *cp;
229         int              i;
230         unsigned char    rv;
231
232         char *dummyargv[] = { "complete", dir, NULL };
233
234         if ((file = strrchr(word, '/')) == NULL) {
235                 dir[0] = '.';
236                 dir[1] = '\0';
237                 file = word;
238         } else {
239                 cp = file;
240                 while (*cp == '/' && cp > word)
241                         cp--;
242                 (void)strncpy(dir, word, cp - word + 1);
243                 dir[cp - word + 1] = '\0';
244                 file++;
245         }
246
247         if (dirchange || strcmp(dir, lastdir) != 0) {   /* dir not cached */
248                 char *emesg;
249
250                 if (dirlist != NULL)
251                         sl_free(dirlist, 1);
252                 dirlist = sl_init();
253
254                 mflag = 1;
255                 emesg = NULL;
256                 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
257                         char *tcp;
258
259                         if (!mflag)
260                                 continue;
261                         if (*cp == '\0') {
262                                 mflag = 0;
263                                 continue;
264                         }
265                         tcp = strrchr(cp, '/');
266                         if (tcp)
267                                 tcp++;
268                         else
269                                 tcp = cp;
270                         tcp = strdup(tcp);
271                         if (tcp == NULL)
272                                 errx(1, "Can't allocate memory for remote dir");
273                         sl_add(dirlist, tcp);
274                 }
275                 if (emesg != NULL) {
276                         printf("\n%s\n", emesg);
277                         return (CC_REDISPLAY);
278                 }
279                 (void)strcpy(lastdir, dir);
280                 dirchange = 0;
281         }
282
283         words = sl_init();
284         for (i = 0; i < dirlist->sl_cur; i++) {
285                 cp = dirlist->sl_str[i];
286                 if (strlen(file) > strlen(cp))
287                         continue;
288                 if (strncmp(file, cp, strlen(file)) == 0)
289                         sl_add(words, cp);
290         }
291         rv = complete_ambiguous(file, list, words);
292         sl_free(words, 0);
293         return (rv);
294 }
295
296 /*
297  * Generic complete routine
298  */
299 unsigned char
300 complete(el, ch)
301         EditLine *el;
302         int ch;
303 {
304         static char word[FTPBUFLEN];
305         static int lastc_argc, lastc_argo;
306
307         struct cmd *c;
308         const LineInfo *lf;
309         int celems, dolist;
310         size_t len;
311
312         lf = el_line(el);
313         len = lf->lastchar - lf->buffer;
314         if (len >= sizeof(line))
315                 return (CC_ERROR);
316         (void)strncpy(line, lf->buffer, len);
317         line[len] = '\0';
318         cursor_pos = line + (lf->cursor - lf->buffer);
319         lastc_argc = cursor_argc;       /* remember last cursor pos */
320         lastc_argo = cursor_argo;
321         makeargv();                     /* build argc/argv of current line */
322
323         if (cursor_argo >= sizeof(word))
324                 return (CC_ERROR);
325
326         dolist = 0;
327                         /* if cursor and word is same, list alternatives */
328         if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
329             && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
330                 dolist = 1;
331         else
332             (void)strncpy(word, margv[cursor_argc], cursor_argo);
333         word[cursor_argo] = '\0';
334
335         if (cursor_argc == 0)
336                 return (complete_command(word, dolist));
337
338         c = getcmd(margv[0]);
339         if (c == (struct cmd *)-1 || c == 0)
340                 return (CC_ERROR);
341         celems = strlen(c->c_complete);
342
343                 /* check for 'continuation' completes (which are uppercase) */
344         if ((cursor_argc > celems) && (celems > 0)
345             && isupper((unsigned char)c->c_complete[celems-1]))
346                 cursor_argc = celems;
347
348         if (cursor_argc > celems)
349                 return (CC_ERROR);
350
351         switch (c->c_complete[cursor_argc - 1]) {
352                 case 'l':                       /* local complete */
353                 case 'L':
354                         return (complete_local(word, dolist));
355                 case 'r':                       /* remote complete */
356                 case 'R':
357                         if (connected != -1) {
358                                 puts("\nMust be logged in to complete.");
359                                 return (CC_REDISPLAY);
360                         }
361                         return (complete_remote(word, dolist));
362                 case 'c':                       /* command complete */
363                 case 'C':
364                         return (complete_command(word, dolist));
365                 case 'n':                       /* no complete */
366                 default:
367                         return (CC_ERROR);
368         }
369
370         return (CC_ERROR);
371 }
372
373 #endif /* !SMALL */