K&R style function removal. Update functions to ANSI style.
[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.3 2003/10/04 20:36:44 hmp 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 (strlen(file) > dp->d_namlen)
196                         continue;
197                 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
198                         char *tcp;
199
200                         tcp = strdup(dp->d_name);
201                         if (tcp == NULL)
202                                 errx(1, "Can't allocate memory for local dir");
203                         sl_add(words, tcp);
204                 }
205         }
206         closedir(dd);
207
208         rv = complete_ambiguous(file, list, words);
209         sl_free(words, 1);
210         return (rv);
211 }
212
213 /*
214  * Complete a remote file
215  */
216 static unsigned char
217 complete_remote(char *word, int list)
218 {
219         static StringList *dirlist;
220         static char      lastdir[MAXPATHLEN];
221         StringList      *words;
222         char             dir[MAXPATHLEN];
223         char            *file, *cp;
224         int              i;
225         unsigned char    rv;
226
227         char *dummyargv[] = { "complete", dir, NULL };
228
229         if ((file = strrchr(word, '/')) == NULL) {
230                 dir[0] = '.';
231                 dir[1] = '\0';
232                 file = word;
233         } else {
234                 cp = file;
235                 while (*cp == '/' && cp > word)
236                         cp--;
237                 (void)strncpy(dir, word, cp - word + 1);
238                 dir[cp - word + 1] = '\0';
239                 file++;
240         }
241
242         if (dirchange || strcmp(dir, lastdir) != 0) {   /* dir not cached */
243                 char *emesg;
244
245                 if (dirlist != NULL)
246                         sl_free(dirlist, 1);
247                 dirlist = sl_init();
248
249                 mflag = 1;
250                 emesg = NULL;
251                 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
252                         char *tcp;
253
254                         if (!mflag)
255                                 continue;
256                         if (*cp == '\0') {
257                                 mflag = 0;
258                                 continue;
259                         }
260                         tcp = strrchr(cp, '/');
261                         if (tcp)
262                                 tcp++;
263                         else
264                                 tcp = cp;
265                         tcp = strdup(tcp);
266                         if (tcp == NULL)
267                                 errx(1, "Can't allocate memory for remote dir");
268                         sl_add(dirlist, tcp);
269                 }
270                 if (emesg != NULL) {
271                         printf("\n%s\n", emesg);
272                         return (CC_REDISPLAY);
273                 }
274                 (void)strcpy(lastdir, dir);
275                 dirchange = 0;
276         }
277
278         words = sl_init();
279         for (i = 0; i < dirlist->sl_cur; i++) {
280                 cp = dirlist->sl_str[i];
281                 if (strlen(file) > strlen(cp))
282                         continue;
283                 if (strncmp(file, cp, strlen(file)) == 0)
284                         sl_add(words, cp);
285         }
286         rv = complete_ambiguous(file, list, words);
287         sl_free(words, 0);
288         return (rv);
289 }
290
291 /*
292  * Generic complete routine
293  */
294 unsigned char
295 complete(EditLine *el, int ch)
296 {
297         static char word[FTPBUFLEN];
298         static int lastc_argc, lastc_argo;
299
300         struct cmd *c;
301         const LineInfo *lf;
302         int celems, dolist;
303         size_t len;
304
305         lf = el_line(el);
306         len = lf->lastchar - lf->buffer;
307         if (len >= sizeof(line))
308                 return (CC_ERROR);
309         (void)strncpy(line, lf->buffer, len);
310         line[len] = '\0';
311         cursor_pos = line + (lf->cursor - lf->buffer);
312         lastc_argc = cursor_argc;       /* remember last cursor pos */
313         lastc_argo = cursor_argo;
314         makeargv();                     /* build argc/argv of current line */
315
316         if (cursor_argo >= sizeof(word))
317                 return (CC_ERROR);
318
319         dolist = 0;
320                         /* if cursor and word is same, list alternatives */
321         if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
322             && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
323                 dolist = 1;
324         else
325             (void)strncpy(word, margv[cursor_argc], cursor_argo);
326         word[cursor_argo] = '\0';
327
328         if (cursor_argc == 0)
329                 return (complete_command(word, dolist));
330
331         c = getcmd(margv[0]);
332         if (c == (struct cmd *)-1 || c == 0)
333                 return (CC_ERROR);
334         celems = strlen(c->c_complete);
335
336                 /* check for 'continuation' completes (which are uppercase) */
337         if ((cursor_argc > celems) && (celems > 0)
338             && isupper((unsigned char)c->c_complete[celems-1]))
339                 cursor_argc = celems;
340
341         if (cursor_argc > celems)
342                 return (CC_ERROR);
343
344         switch (c->c_complete[cursor_argc - 1]) {
345                 case 'l':                       /* local complete */
346                 case 'L':
347                         return (complete_local(word, dolist));
348                 case 'r':                       /* remote complete */
349                 case 'R':
350                         if (connected != -1) {
351                                 puts("\nMust be logged in to complete.");
352                                 return (CC_REDISPLAY);
353                         }
354                         return (complete_remote(word, dolist));
355                 case 'c':                       /* command complete */
356                 case 'C':
357                         return (complete_command(word, dolist));
358                 case 'n':                       /* no complete */
359                 default:
360                         return (CC_ERROR);
361         }
362
363         return (CC_ERROR);
364 }
365
366 #endif /* !SMALL */