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