complete.c revision 1.7 1 /* $NetBSD: complete.c,v 1.7 1997/04/14 09:09:16 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef SMALL
40 #ifndef lint
41 static char rcsid[] = "$NetBSD: complete.c,v 1.7 1997/04/14 09:09:16 lukem Exp $";
42 #endif /* not lint */
43
44 /*
45 * FTP user program - command and file completion routines
46 */
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <dirent.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54
55 #include "ftp_var.h"
56
57 static int
58 comparstr(a, b)
59 const void *a, *b;
60 {
61 return (strcmp(*(char **)a, *(char **)b));
62 }
63
64 /*
65 * Determine if complete is ambiguous. If unique, insert.
66 * If no choices, error. If unambiguous prefix, insert that.
67 * Otherwise, list choices. words is assumed to be filtered
68 * to only contain possible choices.
69 * Args:
70 * word word which started the match
71 * list list by default
72 * words stringlist containing possible matches
73 */
74 static unsigned char
75 complete_ambiguous(word, list, words)
76 char *word;
77 int list;
78 StringList *words;
79 {
80 char insertstr[MAXPATHLEN];
81 char *lastmatch;
82 int i, j, matchlen, wordlen;
83
84 wordlen = strlen(word);
85 if (words->sl_cur == 0)
86 return (CC_ERROR); /* no choices available */
87
88 if (words->sl_cur == 1) { /* only once choice available */
89 (void)strcpy(insertstr, words->sl_str[0]);
90 if (el_insertstr(el, insertstr + wordlen) == -1)
91 return (CC_ERROR);
92 else
93 return (CC_REFRESH);
94 }
95
96 if (!list) {
97 matchlen = 0;
98 lastmatch = words->sl_str[0];
99 matchlen = strlen(lastmatch);
100 for (i = 1 ; i < words->sl_cur ; i++) {
101 for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
102 if (lastmatch[j] != words->sl_str[i][j])
103 break;
104 if (j < matchlen)
105 matchlen = j;
106 }
107 if (matchlen > wordlen) {
108 (void)strncpy(insertstr, lastmatch, matchlen);
109 insertstr[matchlen] = '\0';
110 if (el_insertstr(el, insertstr + wordlen) == -1)
111 return (CC_ERROR);
112 else
113 /*
114 * XXX: really want CC_REFRESH_BEEP
115 */
116 return (CC_REFRESH);
117 }
118 }
119
120 putchar('\n');
121 qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
122 list_vertical(words);
123 return (CC_REDISPLAY);
124 }
125
126 /*
127 * Complete a command
128 */
129 static unsigned char
130 complete_command(word, list)
131 char *word;
132 int list;
133 {
134 struct cmd *c;
135 StringList *words;
136 int wordlen;
137 unsigned char rv;
138
139 words = sl_init();
140 wordlen = strlen(word);
141
142 for (c = cmdtab; c->c_name != NULL; c++) {
143 if (wordlen > strlen(c->c_name))
144 continue;
145 if (strncmp(word, c->c_name, wordlen) == 0)
146 sl_add(words, c->c_name);
147 }
148
149 rv = complete_ambiguous(word, list, words);
150 sl_free(words, 0);
151 return (rv);
152 }
153
154 /*
155 * Complete a local file
156 */
157 static unsigned char
158 complete_local(word, list)
159 char *word;
160 int list;
161 {
162 StringList *words;
163 char dir[MAXPATHLEN];
164 char *file;
165 DIR *dd;
166 struct dirent *dp;
167 unsigned char rv;
168
169 if ((file = strrchr(word, '/')) == NULL) {
170 dir[0] = '.';
171 dir[1] = '\0';
172 file = word;
173 } else {
174 if (file == word) {
175 dir[0] = '/';
176 dir[1] = '\0';
177 } else {
178 (void)strncpy(dir, word, file - word);
179 dir[file - word] = '\0';
180 }
181 file++;
182 }
183
184 if ((dd = opendir(dir)) == NULL)
185 return (CC_ERROR);
186
187 words = sl_init();
188
189 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
190 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
191 continue;
192 if (strlen(file) > dp->d_namlen)
193 continue;
194 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
195 char *tcp;
196
197 tcp = strdup(dp->d_name);
198 if (tcp == NULL)
199 errx(1, "Can't allocate memory for local dir");
200 sl_add(words, tcp);
201 }
202 }
203 closedir(dd);
204
205 rv = complete_ambiguous(file, list, words);
206 sl_free(words, 1);
207 return (rv);
208 }
209
210 /*
211 * Complete a remote file
212 */
213 static unsigned char
214 complete_remote(word, list)
215 char *word;
216 int list;
217 {
218 static StringList *dirlist;
219 static char lastdir[MAXPATHLEN];
220 StringList *words;
221 char dir[MAXPATHLEN];
222 char *file, *cp;
223 int i;
224 unsigned char rv;
225
226 char *dummyargv[] = { "complete", dir, NULL };
227
228 if ((file = strrchr(word, '/')) == NULL) {
229 dir[0] = '.';
230 dir[1] = '\0';
231 file = word;
232 } else {
233 cp = file;
234 while (*cp == '/' && cp > word)
235 cp--;
236 if (cp == word) {
237 dir[0] = '/';
238 dir[1] = '\0';
239 } else {
240 (void)strncpy(dir, word, cp - word + 1);
241 dir[cp - word + 1] = '\0';
242 }
243 file++;
244 }
245
246 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
247 char *emesg;
248 int dirlen, ftpdslashbug;
249
250 dirlen = strlen(dir);
251 ftpdslashbug = 0;
252 if (strcmp(dir, "/") == 0)
253 ftpdslashbug = 1;
254 else if (strcmp(dir, ".") == 0)
255 dirlen = 0;
256 else
257 dirlen++;
258 if (dirlist != NULL)
259 sl_free(dirlist, 1);
260 dirlist = sl_init();
261
262 mflag = 1;
263 emesg = NULL;
264 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
265 char *tcp;
266
267 if (!mflag)
268 continue;
269 if (*cp == '\0') {
270 mflag = 0;
271 continue;
272 }
273 /*
274 * Work around ftpd(1) bug, which puts a // instead
275 * of / in front of each filename returned by "NLST /".
276 * Without this, remote completes of / don't work.
277 * However, only do this if the bug is present.
278 */
279 if (ftpdslashbug && (cp[dirlen] != '/'))
280 ftpdslashbug = 0;
281 tcp = strdup(cp + dirlen + ftpdslashbug);
282 if (tcp == NULL)
283 errx(1, "Can't allocate memory for remote dir");
284 sl_add(dirlist, tcp);
285 }
286 if (emesg != NULL) {
287 printf("\n%s\n", emesg);
288 return (CC_REDISPLAY);
289 }
290 (void)strcpy(lastdir, dir);
291 dirchange = 0;
292 }
293
294 words = sl_init();
295 for (i = 0; i < dirlist->sl_cur; i++) {
296 cp = dirlist->sl_str[i];
297 if (strlen(file) > strlen(cp))
298 continue;
299 if (strncmp(file, cp, strlen(file)) == 0)
300 sl_add(words, cp);
301 }
302 rv = complete_ambiguous(file, list, words);
303 sl_free(words, 0);
304 return (rv);
305 }
306
307 /*
308 * Generic complete routine
309 */
310 unsigned char
311 complete(el, ch)
312 EditLine *el;
313 int ch;
314 {
315 static char word[FTPBUFLEN];
316 static int lastc_argc, lastc_argo;
317
318 struct cmd *c;
319 const LineInfo *lf;
320 int len, celems, dolist;
321
322 lf = el_line(el);
323 len = lf->lastchar - lf->buffer;
324 if (len >= sizeof(line))
325 return (CC_ERROR);
326 (void)strncpy(line, lf->buffer, len);
327 line[len] = '\0';
328 cursor_pos = line + (lf->cursor - lf->buffer);
329 lastc_argc = cursor_argc; /* remember last cursor pos */
330 lastc_argo = cursor_argo;
331 makeargv(); /* build argc/argv of current line */
332
333 if (cursor_argo >= sizeof(word))
334 return (CC_ERROR);
335
336 dolist = 0;
337 /* if cursor and word is same, list alternatives */
338 if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
339 && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
340 dolist = 1;
341 else
342 (void)strncpy(word, margv[cursor_argc], cursor_argo);
343 word[cursor_argo] = '\0';
344
345 if (cursor_argc == 0)
346 return (complete_command(word, dolist));
347
348 c = getcmd(margv[0]);
349 if (c == (struct cmd *)-1 || c == 0)
350 return (CC_ERROR);
351 celems = strlen(c->c_complete);
352
353 /* check for 'continuation' completes (which are uppercase) */
354 if ((cursor_argc > celems) && (celems > 0)
355 && isupper(c->c_complete[celems-1]))
356 cursor_argc = celems;
357
358 if (cursor_argc > celems)
359 return (CC_ERROR);
360
361 switch (c->c_complete[cursor_argc - 1]) {
362 case 'l': /* local complete */
363 case 'L':
364 return (complete_local(word, dolist));
365 case 'r': /* remote complete */
366 case 'R':
367 if (connected != -1) {
368 puts("\nMust be logged in to complete.");
369 return (CC_REDISPLAY);
370 }
371 return (complete_remote(word, dolist));
372 case 'c': /* command complete */
373 case 'C':
374 return (complete_command(word, dolist));
375 case 'n': /* no complete */
376 default:
377 return (CC_ERROR);
378 }
379
380 return (CC_ERROR);
381 }
382 #endif
383