complete.c revision 1.4 1 /* $NetBSD: complete.c,v 1.4 1997/03/13 22:38:40 christos 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 SMALLFTP
40 #ifndef lint
41 static char rcsid[] = "$NetBSD: complete.c,v 1.4 1997/03/13 22:38:40 christos 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 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 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 strcpy(dir, ".");
171 file = word;
172 } else {
173 if (file == word)
174 strcpy(dir, "/");
175 else {
176 strncpy(dir, word, file - word);
177 dir[file - word] = '\0';
178 }
179 ++file;
180 }
181
182 if ((dd = opendir(dir)) == NULL)
183 return CC_ERROR;
184
185 words = sl_init();
186
187 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
188 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
189 continue;
190 if (strlen(file) > dp->d_namlen)
191 continue;
192 if (strncmp(file, dp->d_name, strlen(file)) == 0) {
193 char *tcp;
194
195 tcp = strdup(dp->d_name);
196 if (tcp == NULL)
197 errx(1, "Can't allocate memory for local dir");
198 sl_add(words, tcp);
199 }
200 }
201 closedir(dd);
202
203 rv = complete_ambiguous(file, list, words);
204 sl_free(words, 1);
205 return rv;
206 }
207
208 /*
209 * Complete a remote file
210 */
211 static unsigned char
212 complete_remote(word, list)
213 char *word;
214 int list;
215 {
216 static StringList *dirlist;
217 static char lastdir[MAXPATHLEN];
218 StringList *words;
219 char dir[MAXPATHLEN];
220 char *file, *cp;
221 int i;
222 unsigned char rv;
223
224 char *dummyargv[] = { "complete", dir, NULL };
225
226 if ((file = strrchr(word, '/')) == NULL) {
227 strcpy(dir, ".");
228 file = word;
229 } else {
230 cp = file;
231 while (*cp == '/' && cp > word)
232 cp--;
233 if (cp == word)
234 strcpy(dir, "/");
235 else {
236 strncpy(dir, word, cp - word + 1);
237 dir[cp - word + 1] = '\0';
238 }
239 file++;
240 }
241
242 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
243 char *emesg;
244 int dirlen, ftpdslashbug;
245
246 dirlen = strlen(dir);
247 ftpdslashbug = 0;
248 if (strcmp(dir, "/") == 0)
249 ftpdslashbug = 1;
250 else if (strcmp(dir, ".") == 0)
251 dirlen = 0;
252 else
253 dirlen++;
254 if (dirlist != NULL)
255 sl_free(dirlist, 1);
256 dirlist = sl_init();
257
258 mflag = 1;
259 emesg = NULL;
260 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
261 char *tcp;
262
263 if (!mflag)
264 continue;
265 if (*cp == '\0') {
266 mflag = 0;
267 continue;
268 }
269 /*
270 * Work around ftpd(1) bug, which puts a // instead
271 * of / in front of each filename returned by "NLST /".
272 * Without this, remote completes of / don't work.
273 * However, only do this if the bug is present.
274 */
275 if (ftpdslashbug && (cp[dirlen] != '/'))
276 ftpdslashbug = 0;
277 tcp = strdup(cp + dirlen + ftpdslashbug);
278 if (tcp == NULL)
279 errx(1, "Can't allocate memory for remote dir");
280 sl_add(dirlist, tcp);
281 }
282 if (emesg != NULL) {
283 printf("\n%s\n", emesg);
284 return CC_REDISPLAY;
285 }
286 strcpy(lastdir, dir);
287 dirchange = 0;
288 }
289
290 words = sl_init();
291 for (i = 0; i < dirlist->sl_cur; i++) {
292 cp = dirlist->sl_str[i];
293 if (strlen(file) > strlen(cp))
294 continue;
295 if (strncmp(file, cp, strlen(file)) == 0)
296 sl_add(words, cp);
297 }
298 rv = complete_ambiguous(file, list, words);
299 sl_free(words, 0);
300 return rv;
301 }
302
303 /*
304 * Generic complete routine
305 */
306 unsigned char
307 complete(el, ch)
308 EditLine *el;
309 int ch;
310 {
311 static char word[FTPBUFLEN];
312 static int lastc_argc, lastc_argo;
313
314 struct cmd *c;
315 const LineInfo *lf;
316 int len, celems, dolist;
317
318 lf = el_line(el);
319 len = lf->lastchar - lf->buffer;
320 if (len >= sizeof(line))
321 return CC_ERROR;
322 strncpy(line, lf->buffer, len);
323 line[len] = '\0';
324 cursor_pos = line + (lf->cursor - lf->buffer);
325 lastc_argc = cursor_argc; /* remember last cursor pos */
326 lastc_argo = cursor_argo;
327 makeargv(); /* build argc/argv of current line */
328
329 if (cursor_argo >= sizeof(word))
330 return CC_ERROR;
331
332 dolist = 0;
333 /* if cursor and word is same, list alternatives */
334 if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
335 && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
336 dolist = 1;
337 else
338 strncpy(word, margv[cursor_argc], cursor_argo);
339 word[cursor_argo] = '\0';
340
341 if (cursor_argc == 0)
342 return complete_command(word, dolist);
343
344 c = getcmd(margv[0]);
345 if (c == (struct cmd *)-1 || c == 0)
346 return CC_ERROR;
347 celems = strlen(c->c_complete);
348
349 /* check for 'continuation' completes (which are uppercase) */
350 if ((cursor_argc > celems) && (celems > 0)
351 && isupper(c->c_complete[celems-1]))
352 cursor_argc = celems;
353
354 if (cursor_argc > celems)
355 return CC_ERROR;
356
357 switch (c->c_complete[cursor_argc - 1]) {
358 case 'l': /* local complete */
359 case 'L':
360 return complete_local(word, dolist);
361 case 'r': /* remote complete */
362 case 'R':
363 if (!connected) {
364 puts("\nMust be connected to complete.");
365 return CC_REDISPLAY;
366 }
367 return complete_remote(word, dolist);
368 case 'c': /* command complete */
369 case 'C':
370 return complete_command(word, dolist);
371 case 'n': /* no complete */
372 default:
373 return CC_ERROR;
374 }
375
376 return CC_ERROR;
377 }
378 #endif
379