complete.c revision 1.25 1 /* $NetBSD: complete.c,v 1.25 1999/06/24 14:54:28 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1997, 1998, 1999 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 FOUNDATION OR CONTRIBUTORS
30 * BE 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 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: complete.c,v 1.25 1999/06/24 14:54:28 christos Exp $");
42 #endif /* not lint */
43
44 /*
45 * FTP user program - command and file completion routines
46 */
47
48 #include <sys/stat.h>
49 #include <signal.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <dirent.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 #include "ftp_var.h"
59
60 #ifndef NO_EDITCOMPLETE
61
62 static int comparstr __P((const void *, const void *));
63 static unsigned char complete_ambiguous __P((char *, int, StringList *));
64 static unsigned char complete_command __P((char *, int));
65 static unsigned char complete_local __P((char *, int));
66 static unsigned char complete_remote __P((char *, int));
67
68 static int
69 comparstr(a, b)
70 const void *a, *b;
71 {
72 return (strcmp(*(char **)a, *(char **)b));
73 }
74
75 /*
76 * Determine if complete is ambiguous. If unique, insert.
77 * If no choices, error. If unambiguous prefix, insert that.
78 * Otherwise, list choices. words is assumed to be filtered
79 * to only contain possible choices.
80 * Args:
81 * word word which started the match
82 * list list by default
83 * words stringlist containing possible matches
84 * Returns a result as per el_set(EL_ADDFN, ...)
85 */
86 static unsigned char
87 complete_ambiguous(word, list, words)
88 char *word;
89 int list;
90 StringList *words;
91 {
92 char insertstr[MAXPATHLEN];
93 char *lastmatch;
94 int i, j;
95 size_t matchlen, wordlen;
96
97 wordlen = strlen(word);
98 if (words->sl_cur == 0)
99 return (CC_ERROR); /* no choices available */
100
101 if (words->sl_cur == 1) { /* only once choice available */
102 char *p = words->sl_str[0] + wordlen;
103 ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
104 if (el_insertstr(el, insertstr) == -1)
105 return (CC_ERROR);
106 else
107 return (CC_REFRESH);
108 }
109
110 if (!list) {
111 matchlen = 0;
112 lastmatch = words->sl_str[0];
113 matchlen = strlen(lastmatch);
114 for (i = 1 ; i < words->sl_cur ; i++) {
115 for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
116 if (lastmatch[j] != words->sl_str[i][j])
117 break;
118 if (j < matchlen)
119 matchlen = j;
120 }
121 if (matchlen > wordlen) {
122 ftpvis(insertstr, sizeof(insertstr),
123 lastmatch + wordlen, matchlen - wordlen);
124 if (el_insertstr(el, insertstr) == -1)
125 return (CC_ERROR);
126 else
127 return (CC_REFRESH_BEEP);
128 }
129 }
130
131 putc('\n', ttyout);
132 qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
133 list_vertical(words);
134 return (CC_REDISPLAY);
135 }
136
137 /*
138 * Complete a command
139 */
140 static unsigned char
141 complete_command(word, list)
142 char *word;
143 int list;
144 {
145 struct cmd *c;
146 StringList *words;
147 size_t wordlen;
148 unsigned char rv;
149
150 words = sl_init();
151 wordlen = strlen(word);
152
153 for (c = cmdtab; c->c_name != NULL; c++) {
154 if (wordlen > strlen(c->c_name))
155 continue;
156 if (strncmp(word, c->c_name, wordlen) == 0)
157 sl_add(words, c->c_name);
158 }
159
160 rv = complete_ambiguous(word, list, words);
161 if (rv == CC_REFRESH) {
162 if (el_insertstr(el, " ") == -1)
163 rv = CC_ERROR;
164 }
165 sl_free(words, 0);
166 return (rv);
167 }
168
169 /*
170 * Complete a local file
171 */
172 static unsigned char
173 complete_local(word, list)
174 char *word;
175 int list;
176 {
177 StringList *words;
178 char dir[MAXPATHLEN];
179 char *file;
180 DIR *dd;
181 struct dirent *dp;
182 unsigned char rv;
183 size_t len;
184
185 if ((file = strrchr(word, '/')) == NULL) {
186 dir[0] = '.';
187 dir[1] = '\0';
188 file = word;
189 } else {
190 if (file == word) {
191 dir[0] = '/';
192 dir[1] = '\0';
193 } else {
194 (void)strncpy(dir, word, file - word);
195 dir[file - word] = '\0';
196 }
197 file++;
198 }
199 if (dir[0] == '~') {
200 char *p;
201
202 p = dir;
203 if (!globulize(&p))
204 return (CC_ERROR);
205 if (p != dir) {
206 strncpy(dir, p, sizeof(dir));
207 dir[sizeof(dir)-1] = '\0';
208 free(p);
209 }
210 }
211
212 if ((dd = opendir(dir)) == NULL)
213 return (CC_ERROR);
214
215 words = sl_init();
216
217 len = strlen(file);
218
219 for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
220 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
221 continue;
222
223 #if defined(__SVR4) || defined(__linux__)
224 if (len > strlen(dp->d_name))
225 continue;
226 #else
227 if (len > dp->d_namlen)
228 continue;
229 #endif
230 if (strncmp(file, dp->d_name, len) == 0) {
231 char *tcp;
232
233 tcp = xstrdup(dp->d_name);
234 sl_add(words, tcp);
235 }
236 }
237 closedir(dd);
238
239 rv = complete_ambiguous(file, list, words);
240 if (rv == CC_REFRESH) {
241 struct stat sb;
242 char path[MAXPATHLEN];
243
244 snprintf(path, sizeof(path), "%s/%s", dir, words->sl_str[0]);
245 if (stat(path, &sb) >= 0) {
246 char suffix[2] = " ";
247
248 if (S_ISDIR(sb.st_mode))
249 suffix[0] = '/';
250 if (el_insertstr(el, suffix) == -1)
251 rv = CC_ERROR;
252 }
253 }
254 sl_free(words, 1);
255 return (rv);
256 }
257
258 /*
259 * Complete a remote file
260 */
261 static unsigned char
262 complete_remote(word, list)
263 char *word;
264 int list;
265 {
266 static StringList *dirlist;
267 static char lastdir[MAXPATHLEN];
268 StringList *words;
269 char dir[MAXPATHLEN];
270 char *file, *cp;
271 int i;
272 unsigned char rv;
273
274 char *dummyargv[] = { "complete", NULL, NULL };
275 dummyargv[1] = dir;
276
277 if ((file = strrchr(word, '/')) == NULL) {
278 dir[0] = '.';
279 dir[1] = '\0';
280 file = word;
281 } else {
282 cp = file;
283 while (*cp == '/' && cp > word)
284 cp--;
285 (void)strncpy(dir, word, cp - word + 1);
286 dir[cp - word + 1] = '\0';
287 file++;
288 }
289
290 if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
291 char *emesg;
292
293 if (dirlist != NULL)
294 sl_free(dirlist, 1);
295 dirlist = sl_init();
296
297 mflag = 1;
298 emesg = NULL;
299 while ((cp = remglob(dummyargv, 0, &emesg)) != NULL) {
300 char *tcp;
301
302 if (!mflag)
303 continue;
304 if (*cp == '\0') {
305 mflag = 0;
306 continue;
307 }
308 tcp = strrchr(cp, '/');
309 if (tcp)
310 tcp++;
311 else
312 tcp = cp;
313 tcp = xstrdup(tcp);
314 sl_add(dirlist, tcp);
315 }
316 if (emesg != NULL) {
317 fprintf(ttyout, "\n%s\n", emesg);
318 return (CC_REDISPLAY);
319 }
320 (void)strcpy(lastdir, dir);
321 dirchange = 0;
322 }
323
324 words = sl_init();
325 for (i = 0; i < dirlist->sl_cur; i++) {
326 cp = dirlist->sl_str[i];
327 if (strlen(file) > strlen(cp))
328 continue;
329 if (strncmp(file, cp, strlen(file)) == 0)
330 sl_add(words, cp);
331 }
332 rv = complete_ambiguous(file, list, words);
333 sl_free(words, 0);
334 return (rv);
335 }
336
337 /*
338 * Generic complete routine
339 */
340 unsigned char
341 complete(el, ch)
342 EditLine *el;
343 int ch;
344 {
345 static char word[FTPBUFLEN];
346 static int lastc_argc, lastc_argo;
347
348 struct cmd *c;
349 const LineInfo *lf;
350 int celems, dolist;
351 size_t len;
352
353 lf = el_line(el);
354 len = lf->lastchar - lf->buffer;
355 if (len >= sizeof(line))
356 return (CC_ERROR);
357 (void)strncpy(line, lf->buffer, len);
358 line[len] = '\0';
359 cursor_pos = line + (lf->cursor - lf->buffer);
360 lastc_argc = cursor_argc; /* remember last cursor pos */
361 lastc_argo = cursor_argo;
362 makeargv(); /* build argc/argv of current line */
363
364 if (cursor_argo >= sizeof(word))
365 return (CC_ERROR);
366
367 dolist = 0;
368 /* if cursor and word is same, list alternatives */
369 if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
370 && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
371 dolist = 1;
372 else
373 (void)strncpy(word, margv[cursor_argc], cursor_argo);
374 word[cursor_argo] = '\0';
375
376 if (cursor_argc == 0)
377 return (complete_command(word, dolist));
378
379 c = getcmd(margv[0]);
380 if (c == (struct cmd *)-1 || c == 0)
381 return (CC_ERROR);
382 celems = strlen(c->c_complete);
383
384 /* check for 'continuation' completes (which are uppercase) */
385 if ((cursor_argc > celems) && (celems > 0)
386 && isupper((unsigned char) c->c_complete[celems-1]))
387 cursor_argc = celems;
388
389 if (cursor_argc > celems)
390 return (CC_ERROR);
391
392 switch (c->c_complete[cursor_argc - 1]) {
393 case 'l': /* local complete */
394 case 'L':
395 return (complete_local(word, dolist));
396 case 'r': /* remote complete */
397 case 'R':
398 if (connected != -1) {
399 fputs("\nMust be logged in to complete.\n",
400 ttyout);
401 return (CC_REDISPLAY);
402 }
403 return (complete_remote(word, dolist));
404 case 'c': /* command complete */
405 case 'C':
406 return (complete_command(word, dolist));
407 case 'n': /* no complete */
408 default:
409 return (CC_ERROR);
410 }
411
412 return (CC_ERROR);
413 }
414
415 #endif /* !NO_EDITCOMPLETE */
416