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 $ */
6 * Copyright (c) 1997 The NetBSD Foundation, Inc.
9 * This code is derived from software contributed to The NetBSD Foundation
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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 $
46 #include <sys/cdefs.h>
49 * FTP user program - command and file completion routines
52 #include <sys/types.h>
63 comparstr(const void *a, const void *b)
65 return (strcoll(*(char **)a, *(char **)b));
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.
74 * word word which started the match
75 * list list by default
76 * words stringlist containing possible matches
79 complete_ambiguous(char *word, int list, StringList *words)
81 char insertstr[2 * MAXPATHLEN];
84 size_t matchlen, wordlen;
86 wordlen = strlen(word);
87 if (words->sl_cur == 0)
88 return (CC_ERROR); /* no choices available */
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];
97 if (el_insertstr(el, insertstr + wordlen) == -1)
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])
114 if (matchlen > wordlen) {
115 (void)strncpy(insertstr, lastmatch, matchlen);
116 insertstr[matchlen] = '\0';
117 if (el_insertstr(el, insertstr + wordlen) == -1)
121 * XXX: really want CC_REFRESH_BEEP
128 qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
129 list_vertical(words);
130 return (CC_REDISPLAY);
137 complete_command(char *word, int list)
145 wordlen = strlen(word);
147 for (c = cmdtab; c->c_name != NULL; c++) {
148 if (wordlen > strlen(c->c_name))
150 if (strncmp(word, c->c_name, wordlen) == 0)
151 sl_add(words, c->c_name);
154 rv = complete_ambiguous(word, list, words);
160 * Complete a local file
163 complete_local(char *word, int list)
166 char dir[MAXPATHLEN];
172 if ((file = strrchr(word, '/')) == NULL) {
181 (void)strncpy(dir, word, file - word);
182 dir[file - word] = '\0';
187 if ((dd = opendir(dir)) == NULL)
192 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
193 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
195 if (strlen(file) > dp->d_namlen)
197 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
200 tcp = strdup(dp->d_name);
202 errx(1, "Can't allocate memory for local dir");
208 rv = complete_ambiguous(file, list, words);
214 * Complete a remote file
217 complete_remote(char *word, int list)
219 static StringList *dirlist;
220 static char lastdir[MAXPATHLEN];
222 char dir[MAXPATHLEN];
227 char *dummyargv[] = { "complete", dir, NULL };
229 if ((file = strrchr(word, '/')) == NULL) {
235 while (*cp == '/' && cp > word)
237 (void)strncpy(dir, word, cp - word + 1);
238 dir[cp - word + 1] = '\0';
242 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
251 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
260 tcp = strrchr(cp, '/');
267 errx(1, "Can't allocate memory for remote dir");
268 sl_add(dirlist, tcp);
271 printf("\n%s\n", emesg);
272 return (CC_REDISPLAY);
274 (void)strcpy(lastdir, dir);
279 for (i = 0; i < dirlist->sl_cur; i++) {
280 cp = dirlist->sl_str[i];
281 if (strlen(file) > strlen(cp))
283 if (strncmp(file, cp, strlen(file)) == 0)
286 rv = complete_ambiguous(file, list, words);
292 * Generic complete routine
295 complete(EditLine *el, int ch)
297 static char word[FTPBUFLEN];
298 static int lastc_argc, lastc_argo;
306 len = lf->lastchar - lf->buffer;
307 if (len >= sizeof(line))
309 (void)strncpy(line, lf->buffer, len);
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 */
316 if (cursor_argo >= sizeof(word))
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)
325 (void)strncpy(word, margv[cursor_argc], cursor_argo);
326 word[cursor_argo] = '\0';
328 if (cursor_argc == 0)
329 return (complete_command(word, dolist));
331 c = getcmd(margv[0]);
332 if (c == (struct cmd *)-1 || c == 0)
334 celems = strlen(c->c_complete);
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;
341 if (cursor_argc > celems)
344 switch (c->c_complete[cursor_argc - 1]) {
345 case 'l': /* local complete */
347 return (complete_local(word, dolist));
348 case 'r': /* remote complete */
350 if (connected != -1) {
351 puts("\nMust be logged in to complete.");
352 return (CC_REDISPLAY);
354 return (complete_remote(word, dolist));
355 case 'c': /* command complete */
357 return (complete_command(word, dolist));
358 case 'n': /* no complete */