Merge from vendor branch TEXINFO:
[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.4 2005/08/08 16:43:33 joerg 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(const void *a, const void *b)
64 {
65         return (strcoll(*(char **)a, *(char **)b));
66 }
67
68 /*
69  * Determine if complete is ambiguous. If unique, insert.
70  * If no choices, error. If unambiguous prefix, insert that.
71  * Otherwise, list choices. words is assumed to be filtered
72  * to only contain possible choices.
73  * Args:
74  *      word    word which started the match
75  *      list    list by default
76  *      words   stringlist containing possible matches
77  */
78 static unsigned char
79 complete_ambiguous(char *word, int list, StringList *words)
80 {
81         char insertstr[2 * MAXPATHLEN];
82         char *lastmatch;
83         int i, j;
84         size_t matchlen, wordlen;
85
86         wordlen = strlen(word);
87         if (words->sl_cur == 0)
88                 return (CC_ERROR);      /* no choices available */
89
90         if (words->sl_cur == 1) {       /* only once choice available */
91                 for (i = 0, j = 0; words->sl_str[0][i] != '\0'; i++) {
92                         if (isspace((u_char)words->sl_str[0][i]))
93                                 insertstr[j++] = '\\';
94                         insertstr[j++] = words->sl_str[0][i];
95                 }
96                 insertstr[j] = '\0';
97                 if (el_insertstr(el, insertstr + wordlen) == -1)
98                         return (CC_ERROR);
99                 else
100                         return (CC_REFRESH);
101         }
102
103         if (!list) {
104                 matchlen = 0;
105                 lastmatch = words->sl_str[0];
106                 matchlen = strlen(lastmatch);
107                 for (i = 1 ; i < words->sl_cur ; i++) {
108                         for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
109                                 if (lastmatch[j] != words->sl_str[i][j])
110                                         break;
111                         if (j < matchlen)
112                                 matchlen = j;
113                 }
114                 if (matchlen > wordlen) {
115                         (void)strncpy(insertstr, lastmatch, matchlen);
116                         insertstr[matchlen] = '\0';
117                         if (el_insertstr(el, insertstr + wordlen) == -1)
118                                 return (CC_ERROR);
119                         else    
120                                         /*
121                                          * XXX: really want CC_REFRESH_BEEP
122                                          */
123                                 return (CC_REFRESH);
124                 }
125         }
126
127         putchar('\n');
128         qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
129         list_vertical(words);
130         return (CC_REDISPLAY);
131 }
132
133 /*
134  * Complete a command
135  */
136 static unsigned char
137 complete_command(char *word, int list)
138 {
139         struct cmd *c;
140         StringList *words;
141         size_t wordlen;
142         unsigned char rv;
143
144         words = sl_init();
145         wordlen = strlen(word);
146
147         for (c = cmdtab; c->c_name != NULL; c++) {
148                 if (wordlen > strlen(c->c_name))
149                         continue;
150                 if (strncmp(word, c->c_name, wordlen) == 0)
151                         sl_add(words, c->c_name);
152         }
153
154         rv = complete_ambiguous(word, list, words);
155         sl_free(words, 0);
156         return (rv);
157 }
158
159 /*
160  * Complete a local file
161  */
162 static unsigned char
163 complete_local(char *word, int list)
164 {
165         StringList *words;
166         char dir[MAXPATHLEN];
167         char *file;
168         DIR *dd;
169         struct dirent *dp;
170         unsigned char rv;
171
172         if ((file = strrchr(word, '/')) == NULL) {
173                 dir[0] = '.';
174                 dir[1] = '\0';
175                 file = word;
176         } else {
177                 if (file == word) {
178                         dir[0] = '/';
179                         dir[1] = '\0';
180                 } else {
181                         (void)strncpy(dir, word, file - word);
182                         dir[file - word] = '\0';
183                 }
184                 file++;
185         }
186
187         if ((dd = opendir(dir)) == NULL)
188                 return (CC_ERROR);
189
190         words = sl_init();
191
192         for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
193                 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
194                         continue;
195                 if (strcmp(file, dp->d_name) == 0) {
196                         char *tcp;
197
198                         tcp = strdup(dp->d_name);
199                         if (tcp == NULL)
200                                 errx(1, "Can't allocate memory for local dir");
201                         sl_add(words, tcp);
202                 }
203         }
204         closedir(dd);
205
206         rv = complete_ambiguous(file, list, words);
207         sl_free(words, 1);
208         return (rv);
209 }
210
211 /*
212  * Complete a remote file
213  */
214 static unsigned char
215 complete_remote(char *word, int list)
216 {
217         static StringList *dirlist;
218         static char      lastdir[MAXPATHLEN];
219         StringList      *words;
220         char             dir[MAXPATHLEN];
221         char            *file, *cp;
222         int              i;
223         unsigned char    rv;
224
225         char *dummyargv[] = { "complete", dir, NULL };
226
227         if ((file = strrchr(word, '/')) == NULL) {
228                 dir[0] = '.';
229                 dir[1] = '\0';
230                 file = word;
231         } else {
232                 cp = file;
233                 while (*cp == '/' && cp > word)
234                         cp--;
235                 (void)strncpy(dir, word, cp - word + 1);
236                 dir[cp - word + 1] = '\0';
237                 file++;
238         }
239
240         if (dirchange || strcmp(dir, lastdir) != 0) {   /* dir not cached */
241                 char *emesg;
242
243                 if (dirlist != NULL)
244                         sl_free(dirlist, 1);
245                 dirlist = sl_init();
246
247                 mflag = 1;
248                 emesg = NULL;
249                 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
250                         char *tcp;
251
252                         if (!mflag)
253                                 continue;
254                         if (*cp == '\0') {
255                                 mflag = 0;
256                                 continue;
257                         }
258                         tcp = strrchr(cp, '/');
259                         if (tcp)
260                                 tcp++;
261                         else
262                                 tcp = cp;
263                         tcp = strdup(tcp);
264                         if (tcp == NULL)
265                                 errx(1, "Can't allocate memory for remote dir");
266                         sl_add(dirlist, tcp);
267                 }
268                 if (emesg != NULL) {
269                         printf("\n%s\n", emesg);
270                         return (CC_REDISPLAY);
271                 }
272                 (void)strcpy(lastdir, dir);
273                 dirchange = 0;
274         }
275
276         words = sl_init();
277         for (i = 0; i < dirlist->sl_cur; i++) {
278                 cp = dirlist->sl_str[i];
279                 if (strlen(file) > strlen(cp))
280                         continue;
281                 if (strncmp(file, cp, strlen(file)) == 0)
282                         sl_add(words, cp);
283         }
284         rv = complete_ambiguous(file, list, words);
285         sl_free(words, 0);
286         return (rv);
287 }
288
289 /*
290  * Generic complete routine
291  */
292 unsigned char
293 complete(EditLine *el, int ch)
294 {
295         static char word[FTPBUFLEN];
296         static int lastc_argc, lastc_argo;
297
298         struct cmd *c;
299         const LineInfo *lf;
300         int celems, dolist;
301         size_t len;
302
303         lf = el_line(el);
304         len = lf->lastchar - lf->buffer;
305         if (len >= sizeof(line))
306                 return (CC_ERROR);
307         (void)strncpy(line, lf->buffer, len);
308         line[len] = '\0';
309         cursor_pos = line + (lf->cursor - lf->buffer);
310         lastc_argc = cursor_argc;       /* remember last cursor pos */
311         lastc_argo = cursor_argo;
312         makeargv();                     /* build argc/argv of current line */
313
314         if (cursor_argo >= sizeof(word))
315                 return (CC_ERROR);
316
317         dolist = 0;
318                         /* if cursor and word is same, list alternatives */
319         if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
320             && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
321                 dolist = 1;
322         else
323             (void)strncpy(word, margv[cursor_argc], cursor_argo);
324         word[cursor_argo] = '\0';
325
326         if (cursor_argc == 0)
327                 return (complete_command(word, dolist));
328
329         c = getcmd(margv[0]);
330         if (c == (struct cmd *)-1 || c == 0)
331                 return (CC_ERROR);
332         celems = strlen(c->c_complete);
333
334                 /* check for 'continuation' completes (which are uppercase) */
335         if ((cursor_argc > celems) && (celems > 0)
336             && isupper((unsigned char)c->c_complete[celems-1]))
337                 cursor_argc = celems;
338
339         if (cursor_argc > celems)
340                 return (CC_ERROR);
341
342         switch (c->c_complete[cursor_argc - 1]) {
343                 case 'l':                       /* local complete */
344                 case 'L':
345                         return (complete_local(word, dolist));
346                 case 'r':                       /* remote complete */
347                 case 'R':
348                         if (connected != -1) {
349                                 puts("\nMust be logged in to complete.");
350                                 return (CC_REDISPLAY);
351                         }
352                         return (complete_remote(word, dolist));
353                 case 'c':                       /* command complete */
354                 case 'C':
355                         return (complete_command(word, dolist));
356                 case 'n':                       /* no complete */
357                 default:
358                         return (CC_ERROR);
359         }
360
361         return (CC_ERROR);
362 }
363
364 #endif /* !SMALL */