1 /* $NetBSD: complete.c,v 1.45 2009/04/12 07:07:41 lukem Exp $ */
4 * Copyright (c) 1997-2009 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.45 2009/04/12 07:07:41 lukem 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
, ftp_strdup(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
, ftp_strdup(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
];
283 char cmdbuf
[MAX_C_NAME
];
284 char *dummyargv
[3] = { NULL
, NULL
, NULL
};
286 (void)strlcpy(cmdbuf
, "complete", sizeof(cmdbuf
));
287 dummyargv
[0] = cmdbuf
;
290 if ((file
= strrchr(word
, '/')) == NULL
) {
295 while (*cp
== '/' && cp
> word
)
297 (void)strlcpy(dir
, word
, cp
- word
+ 2);
301 if (dirchange
|| dirlist
== NULL
||
302 strcmp(dir
, lastdir
) != 0) { /* dir not cached */
307 dirlist
= ftp_sl_init();
311 while ((cp
= remglob(dummyargv
, 0, &emesg
)) != NULL
) {
320 tcp
= strrchr(cp
, '/');
325 tcp
= ftp_strdup(tcp
);
326 ftp_sl_add(dirlist
, tcp
);
329 fprintf(ttyout
, "\n%s\n", emesg
);
330 return (CC_REDISPLAY
);
332 (void)strlcpy(lastdir
, dir
, sizeof(lastdir
));
336 words
= ftp_sl_init();
337 for (i
= 0; i
< dirlist
->sl_cur
; i
++) {
338 cp
= dirlist
->sl_str
[i
];
339 if (strlen(file
) > strlen(cp
))
341 if (strncmp(file
, cp
, strlen(file
)) == 0)
342 ftp_sl_add(words
, cp
);
344 rv
= complete_ambiguous(file
, list
, words
);
350 * Generic complete routine
353 complete(EditLine
*cel
, int ch
)
355 static char word
[FTPBUFLEN
];
356 static size_t lastc_argc
, lastc_argo
;
360 int dolist
, cmpltype
;
364 len
= lf
->lastchar
- lf
->buffer
;
365 if (len
>= sizeof(line
))
367 (void)strlcpy(line
, lf
->buffer
, len
+ 1);
368 cursor_pos
= line
+ (lf
->cursor
- lf
->buffer
);
369 lastc_argc
= cursor_argc
; /* remember last cursor pos */
370 lastc_argo
= cursor_argo
;
371 makeargv(); /* build argc/argv of current line */
373 if (cursor_argo
>= sizeof(word
))
377 /* if cursor and word is same, list alternatives */
378 if (lastc_argc
== cursor_argc
&& lastc_argo
== cursor_argo
379 && strncmp(word
, margv
[cursor_argc
] ? margv
[cursor_argc
] : "",
382 else if (cursor_argc
< (size_t)margc
)
383 (void)strlcpy(word
, margv
[cursor_argc
], cursor_argo
+ 1);
384 word
[cursor_argo
] = '\0';
386 if (cursor_argc
== 0)
387 return (complete_command(word
, dolist
));
389 c
= getcmd(margv
[0]);
390 if (c
== (struct cmd
*)-1 || c
== 0)
392 celems
= strlen(c
->c_complete
);
394 /* check for 'continuation' completes (which are uppercase) */
395 if ((cursor_argc
> celems
) && (celems
> 0)
396 && isupper((unsigned char) c
->c_complete
[celems
-1]))
397 cursor_argc
= celems
;
399 if (cursor_argc
> celems
)
402 cmpltype
= c
->c_complete
[cursor_argc
- 1];
404 case 'c': /* command complete */
406 return (complete_command(word
, dolist
));
407 case 'l': /* local complete */
409 return (complete_local(word
, dolist
));
410 case 'n': /* no complete */
411 case 'N': /* no complete */
413 case 'o': /* local complete */
415 return (complete_option(word
, dolist
));
416 case 'r': /* remote complete */
418 if (connected
!= -1) {
419 fputs("\nMust be logged in to complete.\n",
421 return (CC_REDISPLAY
);
423 return (complete_remote(word
, dolist
));
425 errx(1, "complete: unknown complete type `%c'",
432 #endif /* !NO_EDITCOMPLETE */