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