complete.c revision 1.1 1 1.1 lukem /* $NetBSD: complete.c,v 1.1 1997/01/19 14:19:06 lukem 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.1 lukem * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
30 1.1 lukem * 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.1 lukem #ifndef lint
40 1.1 lukem static char rcsid[] = "$NetBSD: complete.c,v 1.1 1997/01/19 14:19:06 lukem Exp $";
41 1.1 lukem #endif /* not lint */
42 1.1 lukem
43 1.1 lukem /*
44 1.1 lukem * FTP user program - command and file completion routines
45 1.1 lukem */
46 1.1 lukem
47 1.1 lukem #include <ctype.h>
48 1.1 lukem #include <err.h>
49 1.1 lukem #include <dirent.h>
50 1.1 lukem #include <stdio.h>
51 1.1 lukem #include <stdlib.h>
52 1.1 lukem #include <string.h>
53 1.1 lukem
54 1.1 lukem #include "ftp_var.h"
55 1.1 lukem
56 1.1 lukem static int
57 1.1 lukem comparstr(a, b)
58 1.1 lukem const void *a, *b;
59 1.1 lukem {
60 1.1 lukem return strcmp(*(char **)a, *(char **)b);
61 1.1 lukem }
62 1.1 lukem
63 1.1 lukem /*
64 1.1 lukem * Determine if complete is ambiguous. If unique, insert.
65 1.1 lukem * If no choices, error. If unambiguous prefix, insert that.
66 1.1 lukem * Otherwise, list choices. words is assumed to be filtered
67 1.1 lukem * to only contain possible choices.
68 1.1 lukem * Args:
69 1.1 lukem * word word which started the match
70 1.1 lukem * list list by default
71 1.1 lukem * words stringlist containing possible matches
72 1.1 lukem */
73 1.1 lukem static unsigned char
74 1.1 lukem complete_ambiguous(word, list, words)
75 1.1 lukem char *word;
76 1.1 lukem int list;
77 1.1 lukem StringList *words;
78 1.1 lukem {
79 1.1 lukem char insertstr[MAXPATHLEN + 1];
80 1.1 lukem char *lastmatch;
81 1.1 lukem int i, j, matchlen, wordlen;
82 1.1 lukem
83 1.1 lukem wordlen = strlen(word);
84 1.1 lukem if (words->sl_cur == 0)
85 1.1 lukem return CC_ERROR; /* no choices available */
86 1.1 lukem
87 1.1 lukem if (words->sl_cur == 1) { /* only once choice available */
88 1.1 lukem strcpy(insertstr, words->sl_str[0]);
89 1.1 lukem if (el_insertstr(el, insertstr + wordlen) == -1)
90 1.1 lukem return CC_ERROR;
91 1.1 lukem else
92 1.1 lukem return CC_REFRESH;
93 1.1 lukem }
94 1.1 lukem
95 1.1 lukem if (!list) {
96 1.1 lukem matchlen = 0;
97 1.1 lukem lastmatch = words->sl_str[0];
98 1.1 lukem matchlen = strlen(lastmatch);
99 1.1 lukem for (i = 1 ; i < words->sl_cur ; i++) {
100 1.1 lukem for (j = wordlen ; j < strlen(words->sl_str[i]); j++)
101 1.1 lukem if (lastmatch[j] != words->sl_str[i][j])
102 1.1 lukem break;
103 1.1 lukem if (j < matchlen)
104 1.1 lukem matchlen = j;
105 1.1 lukem }
106 1.1 lukem if (matchlen > wordlen) {
107 1.1 lukem strncpy(insertstr, lastmatch, matchlen);
108 1.1 lukem insertstr[matchlen] = '\0';
109 1.1 lukem if (el_insertstr(el, insertstr + wordlen) == -1)
110 1.1 lukem return CC_ERROR;
111 1.1 lukem else
112 1.1 lukem /*
113 1.1 lukem * XXX: really want CC_REFRESH_BEEP
114 1.1 lukem */
115 1.1 lukem return CC_REFRESH;
116 1.1 lukem }
117 1.1 lukem }
118 1.1 lukem
119 1.1 lukem putchar('\n');
120 1.1 lukem qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
121 1.1 lukem list_vertical(words);
122 1.1 lukem return CC_REDISPLAY;
123 1.1 lukem }
124 1.1 lukem
125 1.1 lukem /*
126 1.1 lukem * Complete a command
127 1.1 lukem */
128 1.1 lukem static unsigned char
129 1.1 lukem complete_command(word, list)
130 1.1 lukem char *word;
131 1.1 lukem int list;
132 1.1 lukem {
133 1.1 lukem struct cmd *c;
134 1.1 lukem StringList *words;
135 1.1 lukem int wordlen;
136 1.1 lukem unsigned char rv;
137 1.1 lukem
138 1.1 lukem words = sl_init();
139 1.1 lukem wordlen = strlen(word);
140 1.1 lukem
141 1.1 lukem for (c = cmdtab; c->c_name != NULL; c++) {
142 1.1 lukem if (wordlen > strlen(c->c_name))
143 1.1 lukem continue;
144 1.1 lukem if (strncmp(word, c->c_name, wordlen) == 0)
145 1.1 lukem sl_add(words, c->c_name);
146 1.1 lukem }
147 1.1 lukem
148 1.1 lukem rv = complete_ambiguous(word, list, words);
149 1.1 lukem sl_free(words, 0);
150 1.1 lukem return rv;
151 1.1 lukem }
152 1.1 lukem
153 1.1 lukem /*
154 1.1 lukem * Complete a local file
155 1.1 lukem */
156 1.1 lukem static unsigned char
157 1.1 lukem complete_local(word, list)
158 1.1 lukem char *word;
159 1.1 lukem int list;
160 1.1 lukem {
161 1.1 lukem StringList *words;
162 1.1 lukem char dir[MAXPATHLEN + 1];
163 1.1 lukem char *file;
164 1.1 lukem DIR *dd;
165 1.1 lukem struct dirent *dp;
166 1.1 lukem unsigned char rv;
167 1.1 lukem
168 1.1 lukem if ((file = strrchr(word, '/')) == NULL) {
169 1.1 lukem strcpy(dir, ".");
170 1.1 lukem file = word;
171 1.1 lukem } else {
172 1.1 lukem if (file == word)
173 1.1 lukem strcpy(dir, "/");
174 1.1 lukem else {
175 1.1 lukem strncpy(dir, word, file - word);
176 1.1 lukem dir[file - word] = '\0';
177 1.1 lukem }
178 1.1 lukem ++file;
179 1.1 lukem }
180 1.1 lukem
181 1.1 lukem if ((dd = opendir(dir)) == NULL)
182 1.1 lukem return CC_ERROR;
183 1.1 lukem
184 1.1 lukem words = sl_init();
185 1.1 lukem
186 1.1 lukem for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
187 1.1 lukem if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
188 1.1 lukem continue;
189 1.1 lukem if (strlen(file) > dp->d_namlen)
190 1.1 lukem continue;
191 1.1 lukem if (strncmp(file, dp->d_name, strlen(file)) == 0) {
192 1.1 lukem char *tcp;
193 1.1 lukem
194 1.1 lukem tcp = strdup(dp->d_name);
195 1.1 lukem if (tcp == NULL)
196 1.1 lukem errx(1, "Can't allocate memory for local dir");
197 1.1 lukem sl_add(words, tcp);
198 1.1 lukem }
199 1.1 lukem }
200 1.1 lukem closedir(dd);
201 1.1 lukem
202 1.1 lukem rv = complete_ambiguous(file, list, words);
203 1.1 lukem sl_free(words, 1);
204 1.1 lukem return rv;
205 1.1 lukem }
206 1.1 lukem
207 1.1 lukem /*
208 1.1 lukem * Complete a remote file
209 1.1 lukem */
210 1.1 lukem static unsigned char
211 1.1 lukem complete_remote(word, list)
212 1.1 lukem char *word;
213 1.1 lukem int list;
214 1.1 lukem {
215 1.1 lukem static StringList *dirlist;
216 1.1 lukem static char lastdir[MAXPATHLEN + 1];
217 1.1 lukem StringList *words;
218 1.1 lukem char dir[MAXPATHLEN + 1];
219 1.1 lukem char *file, *cp;
220 1.1 lukem int i, offset;
221 1.1 lukem unsigned char rv;
222 1.1 lukem
223 1.1 lukem char *dummyargv[] = { "complete", dir, NULL };
224 1.1 lukem
225 1.1 lukem offset = 0;
226 1.1 lukem if ((file = strrchr(word, '/')) == NULL) {
227 1.1 lukem strcpy(dir, ".");
228 1.1 lukem file = word;
229 1.1 lukem } else {
230 1.1 lukem if (file == word)
231 1.1 lukem strcpy(dir, "/");
232 1.1 lukem else {
233 1.1 lukem offset = file - word;
234 1.1 lukem strncpy(dir, word, offset);
235 1.1 lukem dir[offset] = '\0';
236 1.1 lukem offset++;
237 1.1 lukem }
238 1.1 lukem file++;
239 1.1 lukem }
240 1.1 lukem
241 1.1 lukem if (dirchange || strcmp(dir, lastdir) != 0) { /* dir not cached */
242 1.1 lukem if (dirlist != NULL)
243 1.1 lukem sl_free(dirlist, 1);
244 1.1 lukem dirlist = sl_init();
245 1.1 lukem
246 1.1 lukem mflag = 1;
247 1.1 lukem while ((cp = remglob(dummyargv, 0)) != NULL) {
248 1.1 lukem char *tcp;
249 1.1 lukem
250 1.1 lukem if (!mflag)
251 1.1 lukem continue;
252 1.1 lukem if (*cp == '\0') {
253 1.1 lukem mflag = 0;
254 1.1 lukem continue;
255 1.1 lukem }
256 1.1 lukem tcp = strdup(cp);
257 1.1 lukem if (tcp == NULL)
258 1.1 lukem errx(1, "Can't allocate memory for remote dir");
259 1.1 lukem sl_add(dirlist, tcp);
260 1.1 lukem }
261 1.1 lukem strcpy(lastdir, dir);
262 1.1 lukem dirchange = 0;
263 1.1 lukem }
264 1.1 lukem
265 1.1 lukem words = sl_init();
266 1.1 lukem for (i = 0; i < dirlist->sl_cur; i++) {
267 1.1 lukem cp = dirlist->sl_str[i];
268 1.1 lukem if (strlen(word) > strlen(cp))
269 1.1 lukem continue;
270 1.1 lukem if (strncmp(word, cp, strlen(word)) == 0)
271 1.1 lukem sl_add(words, cp + offset);
272 1.1 lukem }
273 1.1 lukem rv = complete_ambiguous(file, list, words);
274 1.1 lukem sl_free(words, 0);
275 1.1 lukem return rv;
276 1.1 lukem }
277 1.1 lukem
278 1.1 lukem /*
279 1.1 lukem * Generic complete routine
280 1.1 lukem */
281 1.1 lukem unsigned char
282 1.1 lukem complete(el, ch)
283 1.1 lukem EditLine *el;
284 1.1 lukem int ch;
285 1.1 lukem {
286 1.1 lukem static char word[FTPBUFLEN];
287 1.1 lukem static int lastc_argc, lastc_argo;
288 1.1 lukem
289 1.1 lukem struct cmd *c;
290 1.1 lukem const LineInfo *lf;
291 1.1 lukem int len, celems, dolist;
292 1.1 lukem
293 1.1 lukem lf = el_line(el);
294 1.1 lukem len = lf->lastchar - lf->buffer;
295 1.1 lukem if (len >= sizeof(line))
296 1.1 lukem return CC_ERROR;
297 1.1 lukem strncpy(line, lf->buffer, len);
298 1.1 lukem line[len] = '\0';
299 1.1 lukem cursor_pos = line + (lf->cursor - lf->buffer);
300 1.1 lukem lastc_argc = cursor_argc; /* remember last cursor pos */
301 1.1 lukem lastc_argo = cursor_argo;
302 1.1 lukem makeargv(); /* build argc/argv of current line */
303 1.1 lukem
304 1.1 lukem if (cursor_argo >= sizeof(word))
305 1.1 lukem return CC_ERROR;
306 1.1 lukem
307 1.1 lukem dolist = 0;
308 1.1 lukem /* if cursor and word is same, list alternatives */
309 1.1 lukem if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
310 1.1 lukem && strncmp(word, margv[cursor_argc], cursor_argo) == 0)
311 1.1 lukem dolist = 1;
312 1.1 lukem else
313 1.1 lukem strncpy(word, margv[cursor_argc], cursor_argo);
314 1.1 lukem word[cursor_argo] = '\0';
315 1.1 lukem
316 1.1 lukem if (cursor_argc == 0)
317 1.1 lukem return complete_command(word, dolist);
318 1.1 lukem
319 1.1 lukem c = getcmd(margv[0]);
320 1.1 lukem if (c == (struct cmd *)-1 || c == 0)
321 1.1 lukem return CC_ERROR;
322 1.1 lukem celems = strlen(c->c_complete);
323 1.1 lukem
324 1.1 lukem /* check for 'continuation' completes (which are uppercase) */
325 1.1 lukem if ((cursor_argc > celems) && (celems > 0)
326 1.1 lukem && isupper(c->c_complete[celems-1]))
327 1.1 lukem cursor_argc = celems;
328 1.1 lukem
329 1.1 lukem if (cursor_argc > celems)
330 1.1 lukem return CC_ERROR;
331 1.1 lukem
332 1.1 lukem switch (c->c_complete[cursor_argc - 1]) {
333 1.1 lukem case 'l': /* local complete */
334 1.1 lukem case 'L':
335 1.1 lukem return complete_local(word, dolist);
336 1.1 lukem case 'r': /* remote complete */
337 1.1 lukem case 'R':
338 1.1 lukem if (!connected) {
339 1.1 lukem printf("\nMust be connected to complete\n");
340 1.1 lukem return CC_REDISPLAY;
341 1.1 lukem }
342 1.1 lukem return complete_remote(word, dolist);
343 1.1 lukem case 'c': /* command complete */
344 1.1 lukem case 'C':
345 1.1 lukem return complete_command(word, dolist);
346 1.1 lukem case 'n': /* no complete */
347 1.1 lukem default:
348 1.1 lukem return CC_ERROR;
349 1.1 lukem }
350 1.1 lukem
351 1.1 lukem return CC_ERROR;
352 1.1 lukem }
353