1 /* $NetBSD: complete.c,v 1.43 2008/04/28 20:24:13 martin Exp $ */
4 * Copyright (c) 1997-2000,2005 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: complete.c,v 1.43 2008/04/28 20:24:13 martin Exp $");
38 * FTP user program - command and file completion routines
52 #ifndef NO_EDITCOMPLETE
54 static int comparstr (const void *, const void *);
55 static unsigned char complete_ambiguous (char *, int, StringList
*);
56 static unsigned char complete_command (char *, int);
57 static unsigned char complete_local (char *, int);
58 static unsigned char complete_option (char *, int);
59 static unsigned char complete_remote (char *, int);
62 comparstr(const void *a
, const void *b
)
64 return (strcmp(*(const char * const *)a
, *(const char * const *)b
));
68 * Determine if complete is ambiguous. If unique, insert.
69 * If no choices, error. If unambiguous prefix, insert that.
70 * Otherwise, list choices. words is assumed to be filtered
71 * to only contain possible choices.
73 * word word which started the match
74 * list list by default
75 * words stringlist containing possible matches
76 * Returns a result as per el_set(EL_ADDFN, ...)
79 complete_ambiguous(char *word
, int list
, StringList
*words
)
81 char insertstr
[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 p
= words
->sl_str
[0] + wordlen
;
92 if (*p
== '\0') /* at end of word? */
94 ftpvis(insertstr
, sizeof(insertstr
), p
, strlen(p
));
95 if (el_insertstr(el
, insertstr
) == -1)
103 lastmatch
= words
->sl_str
[0];
104 matchlen
= strlen(lastmatch
);
105 for (i
= 1 ; i
< words
->sl_cur
; i
++) {
106 for (j
= wordlen
; j
< strlen(words
->sl_str
[i
]); j
++)
107 if (lastmatch
[j
] != words
->sl_str
[i
][j
])
112 if (matchlen
> wordlen
) {
113 ftpvis(insertstr
, sizeof(insertstr
),
114 lastmatch
+ wordlen
, matchlen
- wordlen
);
115 if (el_insertstr(el
, insertstr
) == -1)
118 return (CC_REFRESH_BEEP
);
123 qsort(words
->sl_str
, words
->sl_cur
, sizeof(char *), comparstr
);
124 list_vertical(words
);
125 return (CC_REDISPLAY
);
132 complete_command(char *word
, int list
)
139 words
= ftp_sl_init();
140 wordlen
= strlen(word
);
142 for (c
= cmdtab
; c
->c_name
!= NULL
; c
++) {
143 if (wordlen
> strlen(c
->c_name
))
145 if (strncmp(word
, c
->c_name
, wordlen
) == 0)
146 ftp_sl_add(words
, c
->c_name
);
149 rv
= complete_ambiguous(word
, list
, words
);
150 if (rv
== CC_REFRESH
) {
151 if (el_insertstr(el
, " ") == -1)
159 * Complete a local file
162 complete_local(char *word
, int list
)
165 char dir
[MAXPATHLEN
];
172 if ((file
= strrchr(word
, '/')) == NULL
) {
181 (void)strlcpy(dir
, word
, file
- word
+ 1);
187 if ((p
= globulize(dir
)) == NULL
)
189 (void)strlcpy(dir
, p
, sizeof(dir
));
193 if ((dd
= opendir(dir
)) == NULL
)
196 words
= ftp_sl_init();
199 for (dp
= readdir(dd
); dp
!= NULL
; dp
= readdir(dd
)) {
200 if (!strcmp(dp
->d_name
, ".") || !strcmp(dp
->d_name
, ".."))
203 #if defined(DIRENT_MISSING_D_NAMLEN)
204 if (len
> strlen(dp
->d_name
))
207 if (len
> dp
->d_namlen
)
210 if (strncmp(file
, dp
->d_name
, len
) == 0) {
213 tcp
= ftp_strdup(dp
->d_name
);
214 ftp_sl_add(words
, tcp
);
219 rv
= complete_ambiguous(file
, list
, words
);
220 if (rv
== CC_REFRESH
) {
222 char path
[MAXPATHLEN
];
224 (void)strlcpy(path
, dir
, sizeof(path
));
225 (void)strlcat(path
, "/", sizeof(path
));
226 (void)strlcat(path
, words
->sl_str
[0], sizeof(path
));
228 if (stat(path
, &sb
) >= 0) {
229 char suffix
[2] = " ";
231 if (S_ISDIR(sb
.st_mode
))
233 if (el_insertstr(el
, suffix
) == -1)
244 complete_option(char *word
, int list
)
251 words
= ftp_sl_init();
252 wordlen
= strlen(word
);
254 for (o
= optiontab
; o
->name
!= NULL
; o
++) {
255 if (wordlen
> strlen(o
->name
))
257 if (strncmp(word
, o
->name
, wordlen
) == 0)
258 ftp_sl_add(words
, o
->name
);
261 rv
= complete_ambiguous(word
, list
, words
);
262 if (rv
== CC_REFRESH
) {
263 if (el_insertstr(el
, " ") == -1)
271 * Complete a remote file
274 complete_remote(char *word
, int list
)
276 static StringList
*dirlist
;
277 static char lastdir
[MAXPATHLEN
];
279 char dir
[MAXPATHLEN
];
284 char *dummyargv
[] = { "complete", NULL
, NULL
};
287 if ((file
= strrchr(word
, '/')) == NULL
) {
292 while (*cp
== '/' && cp
> word
)
294 (void)strlcpy(dir
, word
, cp
- word
+ 2);
298 if (dirchange
|| dirlist
== NULL
||
299 strcmp(dir
, lastdir
) != 0) { /* dir not cached */
304 dirlist
= ftp_sl_init();
308 while ((cp
= remglob(dummyargv
, 0, &emesg
)) != NULL
) {
317 tcp
= strrchr(cp
, '/');
322 tcp
= ftp_strdup(tcp
);
323 ftp_sl_add(dirlist
, tcp
);
326 fprintf(ttyout
, "\n%s\n", emesg
);
327 return (CC_REDISPLAY
);
329 (void)strlcpy(lastdir
, dir
, sizeof(lastdir
));
333 words
= ftp_sl_init();
334 for (i
= 0; i
< dirlist
->sl_cur
; i
++) {
335 cp
= dirlist
->sl_str
[i
];
336 if (strlen(file
) > strlen(cp
))
338 if (strncmp(file
, cp
, strlen(file
)) == 0)
339 ftp_sl_add(words
, cp
);
341 rv
= complete_ambiguous(file
, list
, words
);
347 * Generic complete routine
350 complete(EditLine
*el
, int ch
)
352 static char word
[FTPBUFLEN
];
353 static int lastc_argc
, lastc_argo
;
357 int celems
, dolist
, cmpltype
;
361 len
= lf
->lastchar
- lf
->buffer
;
362 if (len
>= sizeof(line
))
364 (void)strlcpy(line
, lf
->buffer
, len
+ 1);
365 cursor_pos
= line
+ (lf
->cursor
- lf
->buffer
);
366 lastc_argc
= cursor_argc
; /* remember last cursor pos */
367 lastc_argo
= cursor_argo
;
368 makeargv(); /* build argc/argv of current line */
370 if (cursor_argo
>= sizeof(word
))
374 /* if cursor and word is same, list alternatives */
375 if (lastc_argc
== cursor_argc
&& lastc_argo
== cursor_argo
376 && strncmp(word
, margv
[cursor_argc
] ? margv
[cursor_argc
] : "",
379 else if (cursor_argc
< margc
)
380 (void)strlcpy(word
, margv
[cursor_argc
], cursor_argo
+ 1);
381 word
[cursor_argo
] = '\0';
383 if (cursor_argc
== 0)
384 return (complete_command(word
, dolist
));
386 c
= getcmd(margv
[0]);
387 if (c
== (struct cmd
*)-1 || c
== 0)
389 celems
= strlen(c
->c_complete
);
391 /* check for 'continuation' completes (which are uppercase) */
392 if ((cursor_argc
> celems
) && (celems
> 0)
393 && isupper((unsigned char) c
->c_complete
[celems
-1]))
394 cursor_argc
= celems
;
396 if (cursor_argc
> celems
)
399 cmpltype
= c
->c_complete
[cursor_argc
- 1];
401 case 'c': /* command complete */
403 return (complete_command(word
, dolist
));
404 case 'l': /* local complete */
406 return (complete_local(word
, dolist
));
407 case 'n': /* no complete */
408 case 'N': /* no complete */
410 case 'o': /* local complete */
412 return (complete_option(word
, dolist
));
413 case 'r': /* remote complete */
415 if (connected
!= -1) {
416 fputs("\nMust be logged in to complete.\n",
418 return (CC_REDISPLAY
);
420 return (complete_remote(word
, dolist
));
422 errx(1, "complete: unknown complete type `%c'",
429 #endif /* !NO_EDITCOMPLETE */