2 /* $NetBSD: complete.c,v 1.11 1997/09/13 09:05:53 lukem Exp $ */
5 * Copyright (c) 1997 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/cdefs.h>
45 __RCSID_SOURCE("$NetBSD: complete.c,v 1.11 1997/09/13 09:05:53 lukem Exp $");
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[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 (void)strcpy(insertstr, words->sl_str[0]);
96 if (el_insertstr(el, insertstr + wordlen) == -1)
104 lastmatch = words->sl_str[0];
105 matchlen = strlen(lastmatch);
106 for (i = 1 ; i < words->sl_cur ; i++) {
107 for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
108 if (lastmatch[j] != words->sl_str[i][j])
113 if (matchlen > wordlen) {
114 (void)strncpy(insertstr, lastmatch, matchlen);
115 insertstr[matchlen] = '\0';
116 if (el_insertstr(el, insertstr + wordlen) == -1)
120 * XXX: really want CC_REFRESH_BEEP
127 qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
128 list_vertical(words);
129 return (CC_REDISPLAY);
136 complete_command(word, list)
146 wordlen = strlen(word);
148 for (c = cmdtab; c->c_name != NULL; c++) {
149 if (wordlen > strlen(c->c_name))
151 if (strncmp(word, c->c_name, wordlen) == 0)
152 sl_add(words, c->c_name);
155 rv = complete_ambiguous(word, list, words);
161 * Complete a local file
164 complete_local(word, list)
169 char dir[MAXPATHLEN];
175 if ((file = strrchr(word, '/')) == NULL) {
184 (void)strncpy(dir, word, file - word);
185 dir[file - word] = '\0';
190 if ((dd = opendir(dir)) == NULL)
195 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
196 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
198 if (strlen(file) > dp->d_namlen)
200 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
203 tcp = strdup(dp->d_name);
205 errx(1, "Can't allocate memory for local dir");
211 rv = complete_ambiguous(file, list, words);
217 * Complete a remote file
220 complete_remote(word, list)
224 static StringList *dirlist;
225 static char lastdir[MAXPATHLEN];
227 char dir[MAXPATHLEN];
232 char *dummyargv[] = { "complete", dir, NULL };
234 if ((file = strrchr(word, '/')) == NULL) {
240 while (*cp == '/' && cp > word)
242 (void)strncpy(dir, word, cp - word + 1);
243 dir[cp - word + 1] = '\0';
247 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
256 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
265 tcp = strrchr(cp, '/');
272 errx(1, "Can't allocate memory for remote dir");
273 sl_add(dirlist, tcp);
276 printf("\n%s\n", emesg);
277 return (CC_REDISPLAY);
279 (void)strcpy(lastdir, dir);
284 for (i = 0; i < dirlist->sl_cur; i++) {
285 cp = dirlist->sl_str[i];
286 if (strlen(file) > strlen(cp))
288 if (strncmp(file, cp, strlen(file)) == 0)
291 rv = complete_ambiguous(file, list, words);
297 * Generic complete routine
304 static char word[FTPBUFLEN];
305 static int lastc_argc, lastc_argo;
313 len = lf->lastchar - lf->buffer;
314 if (len >= sizeof(line))
316 (void)strncpy(line, lf->buffer, len);
318 cursor_pos = line + (lf->cursor - lf->buffer);
319 lastc_argc = cursor_argc; /* remember last cursor pos */
320 lastc_argo = cursor_argo;
321 makeargv(); /* build argc/argv of current line */
323 if (cursor_argo >= sizeof(word))
327 /* if cursor and word is same, list alternatives */
328 if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
329 && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
332 (void)strncpy(word, margv[cursor_argc], cursor_argo);
333 word[cursor_argo] = '\0';
335 if (cursor_argc == 0)
336 return (complete_command(word, dolist));
338 c = getcmd(margv[0]);
339 if (c == (struct cmd *)-1 || c == 0)
341 celems = strlen(c->c_complete);
343 /* check for 'continuation' completes (which are uppercase) */
344 if ((cursor_argc > celems) && (celems > 0)
345 && isupper((unsigned char)c->c_complete[celems-1]))
346 cursor_argc = celems;
348 if (cursor_argc > celems)
351 switch (c->c_complete[cursor_argc - 1]) {
352 case 'l': /* local complete */
354 return (complete_local(word, dolist));
355 case 'r': /* remote complete */
357 if (connected != -1) {
358 puts("\nMust be logged in to complete.");
359 return (CC_REDISPLAY);
361 return (complete_remote(word, dolist));
362 case 'c': /* command complete */
364 return (complete_command(word, dolist));
365 case 'n': /* no complete */