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 $ */
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>
66 return (strcoll(*(char **)a, *(char **)b));
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.
75 * word word which started the match
76 * list list by default
77 * words stringlist containing possible matches
80 complete_ambiguous(word, list, words)
85 char insertstr[2 * MAXPATHLEN];
88 size_t matchlen, wordlen;
90 wordlen = strlen(word);
91 if (words->sl_cur == 0)
92 return (CC_ERROR); /* no choices available */
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];
101 if (el_insertstr(el, insertstr + wordlen) == -1)
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])
118 if (matchlen > wordlen) {
119 (void)strncpy(insertstr, lastmatch, matchlen);
120 insertstr[matchlen] = '\0';
121 if (el_insertstr(el, insertstr + wordlen) == -1)
125 * XXX: really want CC_REFRESH_BEEP
132 qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
133 list_vertical(words);
134 return (CC_REDISPLAY);
141 complete_command(word, list)
151 wordlen = strlen(word);
153 for (c = cmdtab; c->c_name != NULL; c++) {
154 if (wordlen > strlen(c->c_name))
156 if (strncmp(word, c->c_name, wordlen) == 0)
157 sl_add(words, c->c_name);
160 rv = complete_ambiguous(word, list, words);
166 * Complete a local file
169 complete_local(word, list)
174 char dir[MAXPATHLEN];
180 if ((file = strrchr(word, '/')) == NULL) {
189 (void)strncpy(dir, word, file - word);
190 dir[file - word] = '\0';
195 if ((dd = opendir(dir)) == NULL)
200 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
201 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
203 if (strlen(file) > dp->d_namlen)
205 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
208 tcp = strdup(dp->d_name);
210 errx(1, "Can't allocate memory for local dir");
216 rv = complete_ambiguous(file, list, words);
222 * Complete a remote file
225 complete_remote(word, list)
229 static StringList *dirlist;
230 static char lastdir[MAXPATHLEN];
232 char dir[MAXPATHLEN];
237 char *dummyargv[] = { "complete", dir, NULL };
239 if ((file = strrchr(word, '/')) == NULL) {
245 while (*cp == '/' && cp > word)
247 (void)strncpy(dir, word, cp - word + 1);
248 dir[cp - word + 1] = '\0';
252 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
261 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
270 tcp = strrchr(cp, '/');
277 errx(1, "Can't allocate memory for remote dir");
278 sl_add(dirlist, tcp);
281 printf("\n%s\n", emesg);
282 return (CC_REDISPLAY);
284 (void)strcpy(lastdir, dir);
289 for (i = 0; i < dirlist->sl_cur; i++) {
290 cp = dirlist->sl_str[i];
291 if (strlen(file) > strlen(cp))
293 if (strncmp(file, cp, strlen(file)) == 0)
296 rv = complete_ambiguous(file, list, words);
302 * Generic complete routine
309 static char word[FTPBUFLEN];
310 static int lastc_argc, lastc_argo;
318 len = lf->lastchar - lf->buffer;
319 if (len >= sizeof(line))
321 (void)strncpy(line, lf->buffer, len);
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 */
328 if (cursor_argo >= sizeof(word))
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)
337 (void)strncpy(word, margv[cursor_argc], cursor_argo);
338 word[cursor_argo] = '\0';
340 if (cursor_argc == 0)
341 return (complete_command(word, dolist));
343 c = getcmd(margv[0]);
344 if (c == (struct cmd *)-1 || c == 0)
346 celems = strlen(c->c_complete);
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;
353 if (cursor_argc > celems)
356 switch (c->c_complete[cursor_argc - 1]) {
357 case 'l': /* local complete */
359 return (complete_local(word, dolist));
360 case 'r': /* remote complete */
362 if (connected != -1) {
363 puts("\nMust be logged in to complete.");
364 return (CC_REDISPLAY);
366 return (complete_remote(word, dolist));
367 case 'c': /* command complete */
369 return (complete_command(word, dolist));
370 case 'n': /* no complete */