complete.c revision 1.10 1 1.9 christos /* $NetBSD: complete.c,v 1.10 2006/10/31 20:07:32 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.9 christos * Copyright (c) 1997-2000,2005,2006 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Luke Mewburn.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos
39 1.1 christos /* Most of this is derived or copied from src/usr.bin/ftp/complete.c (1.41). */
40 1.1 christos
41 1.1 christos
42 1.9 christos #ifdef USE_EDITLINE
43 1.9 christos #undef NO_EDITCOMPLETE
44 1.1 christos
45 1.1 christos #include <sys/cdefs.h>
46 1.1 christos #ifndef lint
47 1.9 christos __RCSID("$NetBSD: complete.c,v 1.10 2006/10/31 20:07:32 christos Exp $");
48 1.1 christos #endif /* not lint */
49 1.1 christos
50 1.1 christos /*
51 1.1 christos * FTP user program - command and file completion routines
52 1.1 christos */
53 1.1 christos
54 1.1 christos #include <sys/stat.h>
55 1.1 christos
56 1.1 christos #include <ctype.h>
57 1.1 christos #include <err.h>
58 1.1 christos #include <dirent.h>
59 1.1 christos #include <glob.h>
60 1.1 christos #include <stdio.h>
61 1.1 christos #include <stdlib.h>
62 1.1 christos #include <string.h>
63 1.1 christos #include <sys/param.h>
64 1.1 christos #include <stringlist.h>
65 1.9 christos #include <util.h>
66 1.1 christos
67 1.1 christos #include "rcv.h" /* includes "glob.h" */
68 1.1 christos #include "extern.h"
69 1.1 christos #include "complete.h"
70 1.9 christos #ifdef MIME_SUPPORT
71 1.9 christos #include "mime.h"
72 1.9 christos #endif
73 1.1 christos
74 1.1 christos /*
75 1.1 christos * Global variables
76 1.1 christos */
77 1.1 christos static int doglob = 1; /* glob local file names */
78 1.1 christos
79 1.1 christos #define ttyout stdout
80 1.1 christos #define ttywidth screenwidth /* in "glob.h" */
81 1.1 christos #define ttyheight screenheight /* in "glob.h" */
82 1.1 christos
83 1.1 christos
84 1.1 christos /* This should find the first command that matches the name given or
85 1.1 christos * NULL if none. Use the routine from mail in lex.c.
86 1.1 christos */
87 1.1 christos #define getcmd(w) lex(w)
88 1.1 christos
89 1.1 christos /************************************************************************/
90 1.9 christos /* from src/usr.bin/ftp/utils.h (1.135) - begin */
91 1.1 christos
92 1.1 christos /*
93 1.1 christos * List words in stringlist, vertically arranged
94 1.1 christos */
95 1.1 christos static void
96 1.1 christos list_vertical(StringList *sl)
97 1.1 christos {
98 1.1 christos int i, j;
99 1.1 christos int columns, lines;
100 1.1 christos char *p;
101 1.1 christos size_t w, width;
102 1.1 christos
103 1.1 christos width = 0;
104 1.1 christos
105 1.9 christos for (i = 0; i < sl->sl_cur; i++) {
106 1.1 christos w = strlen(sl->sl_str[i]);
107 1.1 christos if (w > width)
108 1.1 christos width = w;
109 1.1 christos }
110 1.1 christos width = (width + 8) &~ 7;
111 1.1 christos
112 1.1 christos columns = ttywidth / width;
113 1.1 christos if (columns == 0)
114 1.1 christos columns = 1;
115 1.1 christos lines = (sl->sl_cur + columns - 1) / columns;
116 1.1 christos for (i = 0; i < lines; i++) {
117 1.1 christos for (j = 0; j < columns; j++) {
118 1.1 christos p = sl->sl_str[j * lines + i];
119 1.1 christos if (p)
120 1.9 christos (void)fputs(p, ttyout);
121 1.1 christos if (j * lines + i + lines >= sl->sl_cur) {
122 1.9 christos (void)putc('\n', ttyout);
123 1.1 christos break;
124 1.1 christos }
125 1.1 christos if (p) {
126 1.1 christos w = strlen(p);
127 1.1 christos while (w < width) {
128 1.1 christos w = (w + 8) &~ 7;
129 1.1 christos (void)putc('\t', ttyout);
130 1.1 christos }
131 1.1 christos }
132 1.1 christos }
133 1.1 christos }
134 1.1 christos }
135 1.1 christos
136 1.1 christos /*
137 1.1 christos * Copy characters from src into dst, \ quoting characters that require it
138 1.1 christos */
139 1.1 christos static void
140 1.1 christos ftpvis(char *dst, size_t dstlen, const char *src, size_t srclen)
141 1.1 christos {
142 1.1 christos int di, si;
143 1.1 christos
144 1.1 christos for (di = si = 0;
145 1.1 christos src[si] != '\0' && di < dstlen && si < srclen;
146 1.1 christos di++, si++) {
147 1.1 christos switch (src[si]) {
148 1.1 christos case '\\':
149 1.1 christos case ' ':
150 1.1 christos case '\t':
151 1.1 christos case '\r':
152 1.1 christos case '\n':
153 1.1 christos case '"':
154 1.1 christos dst[di++] = '\\';
155 1.1 christos if (di >= dstlen)
156 1.1 christos break;
157 1.1 christos /* FALLTHROUGH */
158 1.1 christos default:
159 1.1 christos dst[di] = src[si];
160 1.1 christos }
161 1.1 christos }
162 1.1 christos dst[di] = '\0';
163 1.1 christos }
164 1.1 christos
165 1.1 christos /*
166 1.1 christos * sl_init() with inbuilt error checking
167 1.1 christos */
168 1.1 christos static StringList *
169 1.9 christos mail_sl_init(void)
170 1.1 christos {
171 1.1 christos StringList *p;
172 1.1 christos
173 1.1 christos p = sl_init();
174 1.1 christos if (p == NULL)
175 1.1 christos err(1, "Unable to allocate memory for stringlist");
176 1.1 christos return (p);
177 1.1 christos }
178 1.1 christos
179 1.1 christos
180 1.1 christos /*
181 1.1 christos * sl_add() with inbuilt error checking
182 1.1 christos */
183 1.1 christos static void
184 1.9 christos mail_sl_add(StringList *sl, char *i)
185 1.1 christos {
186 1.1 christos
187 1.1 christos if (sl_add(sl, i) == -1)
188 1.1 christos err(1, "Unable to add `%s' to stringlist", i);
189 1.1 christos }
190 1.1 christos
191 1.1 christos
192 1.1 christos /*
193 1.1 christos * Glob a local file name specification with the expectation of a single
194 1.1 christos * return value. Can't control multiple values being expanded from the
195 1.1 christos * expression, we return only the first.
196 1.1 christos * Returns NULL on error, or a pointer to a buffer containing the filename
197 1.1 christos * that's the caller's responsiblity to free(3) when finished with.
198 1.1 christos */
199 1.1 christos static char *
200 1.1 christos globulize(const char *pattern)
201 1.1 christos {
202 1.1 christos glob_t gl;
203 1.1 christos int flags;
204 1.1 christos char *p;
205 1.1 christos
206 1.1 christos if (!doglob)
207 1.9 christos return estrdup(pattern);
208 1.1 christos
209 1.1 christos flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
210 1.9 christos (void)memset(&gl, 0, sizeof(gl));
211 1.1 christos if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
212 1.1 christos warnx("%s: not found", pattern);
213 1.1 christos globfree(&gl);
214 1.1 christos return (NULL);
215 1.1 christos }
216 1.9 christos p = estrdup(gl.gl_pathv[0]);
217 1.1 christos globfree(&gl);
218 1.1 christos return (p);
219 1.1 christos }
220 1.1 christos
221 1.9 christos /* from src/usr.bin/ftp/utils.h (1.135) - end */
222 1.1 christos /************************************************************************/
223 1.1 christos
224 1.1 christos static int
225 1.1 christos comparstr(const void *a, const void *b)
226 1.1 christos {
227 1.1 christos return (strcmp(*(const char * const *)a, *(const char * const *)b));
228 1.1 christos }
229 1.1 christos
230 1.1 christos /*
231 1.1 christos * Determine if complete is ambiguous. If unique, insert.
232 1.1 christos * If no choices, error. If unambiguous prefix, insert that.
233 1.1 christos * Otherwise, list choices. words is assumed to be filtered
234 1.1 christos * to only contain possible choices.
235 1.1 christos * Args:
236 1.1 christos * word word which started the match
237 1.9 christos * dolist list by default
238 1.1 christos * words stringlist containing possible matches
239 1.1 christos * Returns a result as per el_set(EL_ADDFN, ...)
240 1.1 christos */
241 1.1 christos static unsigned char
242 1.9 christos complete_ambiguous(EditLine *el, char *word, int dolist, StringList *words)
243 1.1 christos {
244 1.1 christos char insertstr[MAXPATHLEN];
245 1.1 christos char *lastmatch, *p;
246 1.1 christos int i, j;
247 1.1 christos size_t matchlen, wordlen;
248 1.1 christos
249 1.1 christos wordlen = strlen(word);
250 1.1 christos if (words->sl_cur == 0)
251 1.1 christos return (CC_ERROR); /* no choices available */
252 1.1 christos
253 1.1 christos if (words->sl_cur == 1) { /* only once choice available */
254 1.1 christos p = words->sl_str[0] + wordlen;
255 1.1 christos if (*p == '\0') /* at end of word? */
256 1.1 christos return (CC_REFRESH);
257 1.1 christos ftpvis(insertstr, sizeof(insertstr), p, strlen(p));
258 1.1 christos if (el_insertstr(el, insertstr) == -1)
259 1.1 christos return (CC_ERROR);
260 1.1 christos else
261 1.1 christos return (CC_REFRESH);
262 1.1 christos }
263 1.1 christos
264 1.9 christos if (!dolist) {
265 1.1 christos matchlen = 0;
266 1.1 christos lastmatch = words->sl_str[0];
267 1.1 christos matchlen = strlen(lastmatch);
268 1.9 christos for (i = 1; i < words->sl_cur; i++) {
269 1.9 christos for (j = wordlen; j < strlen(words->sl_str[i]); j++)
270 1.1 christos if (lastmatch[j] != words->sl_str[i][j])
271 1.1 christos break;
272 1.1 christos if (j < matchlen)
273 1.1 christos matchlen = j;
274 1.1 christos }
275 1.1 christos if (matchlen > wordlen) {
276 1.1 christos ftpvis(insertstr, sizeof(insertstr),
277 1.1 christos lastmatch + wordlen, matchlen - wordlen);
278 1.1 christos if (el_insertstr(el, insertstr) == -1)
279 1.1 christos return (CC_ERROR);
280 1.1 christos else
281 1.1 christos return (CC_REFRESH_BEEP);
282 1.1 christos }
283 1.1 christos }
284 1.1 christos
285 1.9 christos (void)putc('\n', ttyout);
286 1.1 christos qsort(words->sl_str, words->sl_cur, sizeof(char *), comparstr);
287 1.1 christos list_vertical(words);
288 1.1 christos return (CC_REDISPLAY);
289 1.1 christos }
290 1.1 christos
291 1.1 christos /*
292 1.1 christos * Complete a mail command.
293 1.1 christos */
294 1.1 christos static unsigned char
295 1.9 christos complete_command(EditLine *el, char *word, int dolist)
296 1.1 christos {
297 1.1 christos const struct cmd *c;
298 1.1 christos StringList *words;
299 1.1 christos size_t wordlen;
300 1.1 christos unsigned char rv;
301 1.1 christos
302 1.9 christos words = mail_sl_init();
303 1.1 christos wordlen = strlen(word);
304 1.1 christos
305 1.1 christos for (c = cmdtab; c->c_name != NULL; c++) {
306 1.1 christos if (wordlen > strlen(c->c_name))
307 1.1 christos continue;
308 1.1 christos if (strncmp(word, c->c_name, wordlen) == 0)
309 1.9 christos mail_sl_add(words, __UNCONST(c->c_name));
310 1.1 christos }
311 1.1 christos
312 1.9 christos rv = complete_ambiguous(el, word, dolist, words);
313 1.1 christos if (rv == CC_REFRESH) {
314 1.1 christos if (el_insertstr(el, " ") == -1)
315 1.1 christos rv = CC_ERROR;
316 1.1 christos }
317 1.1 christos sl_free(words, 0);
318 1.1 christos return (rv);
319 1.1 christos }
320 1.1 christos
321 1.1 christos /*
322 1.1 christos * Complete a local filename.
323 1.1 christos */
324 1.1 christos static unsigned char
325 1.9 christos complete_filename(EditLine *el, char *word, int dolist)
326 1.1 christos {
327 1.1 christos StringList *words;
328 1.1 christos char dir[MAXPATHLEN];
329 1.1 christos char *fname;
330 1.1 christos DIR *dd;
331 1.1 christos struct dirent *dp;
332 1.1 christos unsigned char rv;
333 1.1 christos size_t len;
334 1.1 christos
335 1.1 christos if ((fname = strrchr(word, '/')) == NULL) {
336 1.1 christos dir[0] = '.';
337 1.1 christos dir[1] = '\0';
338 1.1 christos fname = word;
339 1.1 christos } else {
340 1.1 christos if (fname == word) {
341 1.1 christos dir[0] = '/';
342 1.1 christos dir[1] = '\0';
343 1.9 christos } else {
344 1.9 christos len = fname - word + 1;
345 1.9 christos (void)estrlcpy(dir, word, sizeof(dir));
346 1.9 christos dir[len] = '\0';
347 1.9 christos }
348 1.1 christos fname++;
349 1.1 christos }
350 1.1 christos if (dir[0] == '~') {
351 1.1 christos char *p;
352 1.1 christos
353 1.1 christos if ((p = globulize(dir)) == NULL)
354 1.1 christos return (CC_ERROR);
355 1.9 christos (void)estrlcpy(dir, p, sizeof(dir));
356 1.1 christos free(p);
357 1.1 christos }
358 1.1 christos
359 1.1 christos if ((dd = opendir(dir)) == NULL)
360 1.1 christos return (CC_ERROR);
361 1.1 christos
362 1.9 christos words = mail_sl_init();
363 1.1 christos len = strlen(fname);
364 1.1 christos
365 1.1 christos for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
366 1.1 christos if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
367 1.1 christos continue;
368 1.1 christos
369 1.1 christos #if defined(DIRENT_MISSING_D_NAMLEN)
370 1.1 christos if (len > strlen(dp->d_name))
371 1.1 christos continue;
372 1.1 christos #else
373 1.1 christos if (len > dp->d_namlen)
374 1.1 christos continue;
375 1.1 christos #endif
376 1.1 christos if (strncmp(fname, dp->d_name, len) == 0) {
377 1.1 christos char *tcp;
378 1.1 christos
379 1.9 christos tcp = estrdup(dp->d_name);
380 1.9 christos mail_sl_add(words, tcp);
381 1.1 christos }
382 1.1 christos }
383 1.9 christos (void)closedir(dd);
384 1.1 christos
385 1.9 christos rv = complete_ambiguous(el, fname, dolist, words);
386 1.1 christos if (rv == CC_REFRESH) {
387 1.1 christos struct stat sb;
388 1.1 christos char path[MAXPATHLEN];
389 1.1 christos
390 1.9 christos (void)estrlcpy(path, dir, sizeof(path));
391 1.9 christos (void)estrlcat(path, "/", sizeof(path));
392 1.9 christos (void)estrlcat(path, words->sl_str[0], sizeof(path));
393 1.1 christos
394 1.1 christos if (stat(path, &sb) >= 0) {
395 1.1 christos char suffix[2] = " ";
396 1.1 christos
397 1.1 christos if (S_ISDIR(sb.st_mode))
398 1.1 christos suffix[0] = '/';
399 1.1 christos if (el_insertstr(el, suffix) == -1)
400 1.1 christos rv = CC_ERROR;
401 1.1 christos }
402 1.1 christos }
403 1.1 christos sl_free(words, 1);
404 1.1 christos return (rv);
405 1.1 christos }
406 1.1 christos
407 1.1 christos static int
408 1.1 christos find_execs(char *word, char *path, StringList *list)
409 1.1 christos {
410 1.1 christos char *sep;
411 1.1 christos char *dir=path;
412 1.1 christos DIR *dd;
413 1.1 christos struct dirent *dp;
414 1.9 christos size_t len = strlen(word);
415 1.1 christos uid_t uid = getuid();
416 1.1 christos gid_t gid = getgid();
417 1.1 christos
418 1.9 christos for (sep = dir; sep; dir = sep + 1) {
419 1.1 christos if ((sep=strchr(dir, ':')) != NULL) {
420 1.1 christos *sep=0;
421 1.1 christos }
422 1.1 christos
423 1.1 christos if ((dd = opendir(dir)) == NULL) {
424 1.1 christos perror("dir");
425 1.1 christos return -1;
426 1.1 christos }
427 1.1 christos
428 1.1 christos for (dp = readdir(dd); dp != NULL; dp = readdir(dd)) {
429 1.1 christos
430 1.1 christos if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
431 1.1 christos continue;
432 1.1 christos
433 1.1 christos #if defined(DIRENT_MISSING_D_NAMLEN)
434 1.1 christos if (len > strlen(dp->d_name))
435 1.1 christos continue;
436 1.1 christos #else
437 1.1 christos if (len > dp->d_namlen)
438 1.1 christos continue;
439 1.1 christos #endif
440 1.1 christos
441 1.1 christos if (strncmp(word, dp->d_name, len) == 0) {
442 1.1 christos struct stat sb;
443 1.1 christos char pathname[ MAXPATHLEN ];
444 1.1 christos unsigned mask;
445 1.1 christos
446 1.9 christos (void)snprintf(pathname, sizeof(pathname),
447 1.9 christos "%s/%s", dir, dp->d_name);
448 1.1 christos if (stat(pathname, &sb) != 0) {
449 1.1 christos perror(pathname);
450 1.1 christos continue;
451 1.1 christos }
452 1.1 christos
453 1.1 christos mask = 0001;
454 1.1 christos if (sb.st_uid == uid) mask |= 0100;
455 1.1 christos if (sb.st_gid == gid) mask |= 0010;
456 1.1 christos
457 1.1 christos if ((sb.st_mode & mask) != 0) {
458 1.1 christos char *tcp;
459 1.9 christos tcp = estrdup(dp->d_name);
460 1.9 christos mail_sl_add(list, tcp);
461 1.1 christos }
462 1.1 christos }
463 1.1 christos
464 1.1 christos }
465 1.1 christos
466 1.9 christos (void)closedir(dd);
467 1.1 christos }
468 1.1 christos
469 1.1 christos return 0;
470 1.1 christos }
471 1.1 christos
472 1.1 christos
473 1.1 christos /*
474 1.1 christos * Complete a local executable
475 1.1 christos */
476 1.1 christos static unsigned char
477 1.9 christos complete_executable(EditLine *el, char *word, int dolist)
478 1.1 christos {
479 1.1 christos StringList *words;
480 1.1 christos char dir[ MAXPATHLEN ];
481 1.1 christos char *fname;
482 1.1 christos unsigned char rv;
483 1.9 christos size_t len;
484 1.1 christos int error;
485 1.1 christos
486 1.1 christos if ((fname = strrchr(word, '/')) == NULL) {
487 1.1 christos dir[0] = '\0'; /* walk the path */
488 1.1 christos fname = word;
489 1.1 christos } else {
490 1.1 christos if (fname == word) {
491 1.1 christos dir[0] = '/';
492 1.1 christos dir[1] = '\0';
493 1.1 christos } else {
494 1.9 christos len = fname - word;
495 1.9 christos (void)strncpy(dir, word, len);
496 1.1 christos dir[fname - word] = '\0';
497 1.1 christos }
498 1.1 christos fname++;
499 1.1 christos }
500 1.1 christos
501 1.1 christos words = sl_init();
502 1.1 christos
503 1.1 christos if (*dir == '\0') { /* walk path */
504 1.1 christos char *env;
505 1.1 christos char *path;
506 1.1 christos env = getenv("PATH");
507 1.1 christos len = strlen(env);
508 1.1 christos path = salloc(len + 1);
509 1.9 christos (void)strcpy(path, env);
510 1.1 christos error = find_execs(word, path, words);
511 1.1 christos }
512 1.1 christos else { /* check specified dir only */
513 1.1 christos error = find_execs(word, dir, words);
514 1.1 christos }
515 1.1 christos if (error != 0)
516 1.1 christos return CC_ERROR;
517 1.1 christos
518 1.9 christos rv = complete_ambiguous(el, fname, dolist, words);
519 1.1 christos
520 1.1 christos if (rv == CC_REFRESH)
521 1.1 christos if (el_insertstr(el, " ") == -1)
522 1.1 christos rv = CC_ERROR;
523 1.1 christos
524 1.1 christos sl_free(words, 1);
525 1.1 christos
526 1.1 christos return (rv);
527 1.1 christos }
528 1.1 christos
529 1.1 christos
530 1.1 christos static unsigned
531 1.9 christos char complete_set(EditLine *el, char *word, int dolist)
532 1.1 christos {
533 1.1 christos struct var *vp;
534 1.1 christos char **ap;
535 1.1 christos char **p;
536 1.1 christos int h;
537 1.1 christos int s;
538 1.9 christos size_t len = strlen(word);
539 1.1 christos StringList *words;
540 1.1 christos unsigned char rv;
541 1.1 christos
542 1.1 christos words = sl_init();
543 1.1 christos
544 1.1 christos /* allocate space for variables table */
545 1.10 christos s = 1;
546 1.10 christos for (h = 0; h < HSHSIZE; h++)
547 1.1 christos for (vp = variables[h]; vp != NULL; vp = vp->v_link)
548 1.1 christos s++;
549 1.10 christos ap = (char **)salloc(s * sizeof *ap);
550 1.1 christos
551 1.10 christos /* save the pointers */
552 1.1 christos for (h = 0, p = ap; h < HSHSIZE; h++)
553 1.1 christos for (vp = variables[h]; vp != NULL; vp = vp->v_link)
554 1.1 christos *p++ = vp->v_name;
555 1.1 christos *p = NULL;
556 1.1 christos sort(ap);
557 1.10 christos for (p = ap; *p != NULL; p++)
558 1.10 christos if (len == 0 || strncmp(*p, word, len) == 0)
559 1.10 christos mail_sl_add(words, estrdup(*p));
560 1.1 christos
561 1.9 christos rv = complete_ambiguous(el, word, dolist, words);
562 1.1 christos
563 1.1 christos sl_free(words, 1);
564 1.1 christos
565 1.1 christos return(rv);
566 1.1 christos }
567 1.1 christos
568 1.1 christos
569 1.1 christos static unsigned char
570 1.9 christos complete_alias(EditLine *el, char *word, int dolist)
571 1.1 christos {
572 1.1 christos struct grouphead *gh;
573 1.1 christos char **ap;
574 1.1 christos char **p;
575 1.1 christos int h;
576 1.1 christos int s;
577 1.9 christos size_t len = strlen(word);
578 1.1 christos StringList *words;
579 1.1 christos unsigned char rv;
580 1.1 christos
581 1.1 christos words = sl_init();
582 1.1 christos
583 1.1 christos /* allocate space for alias table */
584 1.10 christos s = 1;
585 1.10 christos for (h = 0; h < HSHSIZE; h++)
586 1.1 christos for (gh = groups[h]; gh != NULL; gh = gh->g_link)
587 1.1 christos s++;
588 1.10 christos ap = (char **)salloc(s * sizeof *ap);
589 1.10 christos
590 1.1 christos /* save pointers */
591 1.10 christos p = ap;
592 1.10 christos for (h = 0; h < HSHSIZE; h++)
593 1.1 christos for (gh = groups[h]; gh != NULL; gh = gh->g_link)
594 1.1 christos *p++ = gh->g_name;
595 1.1 christos *p = NULL;
596 1.10 christos sort(ap);
597 1.10 christos for (p = ap; *p != NULL; p++)
598 1.10 christos if (len == 0 || strncmp(*p, word, len) == 0)
599 1.10 christos mail_sl_add(words, estrdup(*p));
600 1.10 christos
601 1.10 christos rv = complete_ambiguous(el, word, dolist, words);
602 1.10 christos if (rv == CC_REFRESH)
603 1.10 christos if (el_insertstr(el, " ") == -1)
604 1.10 christos rv = CC_ERROR;
605 1.10 christos
606 1.10 christos sl_free(words, 1);
607 1.10 christos
608 1.10 christos return(rv);
609 1.10 christos }
610 1.10 christos
611 1.10 christos
612 1.10 christos #ifdef SMOPTS_CMD
613 1.10 christos static unsigned
614 1.10 christos char complete_smopts(EditLine *el, char *word, int dolist)
615 1.10 christos {
616 1.10 christos struct grouphead *gh;
617 1.10 christos struct smopts_s *sp;
618 1.10 christos char **ap;
619 1.10 christos char **p;
620 1.10 christos int h;
621 1.10 christos int s1;
622 1.10 christos int s2;
623 1.10 christos size_t len;
624 1.10 christos StringList *words;
625 1.10 christos unsigned char rv;
626 1.1 christos
627 1.10 christos len = strlen(word);
628 1.10 christos words = sl_init();
629 1.1 christos
630 1.10 christos /* count the entries in the smoptstbl and groups (alias) tables */
631 1.10 christos s1 = 1;
632 1.10 christos s2 = 1;
633 1.10 christos for (h = 0; h < HSHSIZE; h++) {
634 1.10 christos for (sp = smoptstbl[h]; sp != NULL; sp = sp->s_link)
635 1.10 christos s1++;
636 1.10 christos for (gh = groups[h]; gh != NULL; gh = gh->g_link)
637 1.10 christos s2++;
638 1.1 christos }
639 1.1 christos
640 1.10 christos /* allocate sufficient space for the pointers */
641 1.10 christos ap = (char **)salloc(MAX(s1, s2) * sizeof *ap);
642 1.10 christos
643 1.10 christos /*
644 1.10 christos * First do the smoptstbl pointers. (case _insensitive_)
645 1.10 christos */
646 1.10 christos p = ap;
647 1.10 christos for (h = 0; h < HSHSIZE; h++)
648 1.10 christos for (sp = smoptstbl[h]; sp != NULL; sp = sp->s_link)
649 1.10 christos *p++ = sp->s_name;
650 1.10 christos *p = NULL;
651 1.10 christos sort(ap);
652 1.10 christos for (p = ap; *p != NULL; p++)
653 1.10 christos if (len == 0 || strncasecmp(*p, word, len) == 0)
654 1.10 christos mail_sl_add(words, estrdup(*p));
655 1.10 christos
656 1.10 christos /*
657 1.10 christos * Now do the groups (alias) pointers. (case sensitive)
658 1.10 christos */
659 1.10 christos p = ap;
660 1.10 christos for (h = 0; h < HSHSIZE; h++)
661 1.10 christos for (gh = groups[h]; gh != NULL; gh = gh->g_link)
662 1.10 christos *p++ = gh->g_name;
663 1.10 christos *p = NULL;
664 1.10 christos sort(ap);
665 1.10 christos for (p = ap; *p != NULL; p++)
666 1.10 christos if (len == 0 || strncmp(*p, word, len) == 0)
667 1.10 christos mail_sl_add(words, estrdup(*p));
668 1.10 christos
669 1.9 christos rv = complete_ambiguous(el, word, dolist, words);
670 1.1 christos
671 1.1 christos sl_free(words, 1);
672 1.1 christos
673 1.1 christos return(rv);
674 1.1 christos }
675 1.10 christos #endif /* SMOPTS_CMD */
676 1.1 christos
677 1.1 christos
678 1.1 christos static char *stringbase; /* current scan point in line buffer */
679 1.1 christos static char *argbase; /* current storage point in arg buffer */
680 1.1 christos static StringList *marg_sl; /* stringlist containing margv */
681 1.1 christos static int margc; /* count of arguments on input line */
682 1.1 christos #define margv (marg_sl->sl_str) /* args parsed from input line */
683 1.1 christos
684 1.1 christos static char *cursor_pos;
685 1.1 christos static int cursor_argc;
686 1.1 christos static int cursor_argo;
687 1.1 christos
688 1.1 christos static void
689 1.1 christos init_complete(void)
690 1.1 christos {
691 1.1 christos marg_sl = sl_init();
692 1.1 christos return;
693 1.1 christos }
694 1.1 christos
695 1.1 christos
696 1.1 christos
697 1.1 christos /************************************************************************/
698 1.9 christos /* from /usr/src/usr.bin/ftp/main.c(1.101) - begin */
699 1.1 christos
700 1.1 christos static int slrflag;
701 1.9 christos #ifdef NEED_ALTARG
702 1.1 christos static char *altarg; /* argv[1] with no shell-like preprocessing */
703 1.9 christos #endif
704 1.1 christos
705 1.1 christos #ifdef NO_EDITCOMPLETE
706 1.1 christos #define INC_CHKCURSOR(x) (x)++
707 1.1 christos #else /* !NO_EDITCOMPLETE */
708 1.1 christos #define INC_CHKCURSOR(x) \
709 1.1 christos do { \
710 1.9 christos (x)++; \
711 1.1 christos if (x == cursor_pos) { \
712 1.1 christos cursor_argc = margc; \
713 1.1 christos cursor_argo = ap - argbase; \
714 1.1 christos cursor_pos = NULL; \
715 1.1 christos } \
716 1.9 christos } while(/* CONSTCOND */ 0)
717 1.1 christos #endif /* !NO_EDITCOMPLETE */
718 1.1 christos
719 1.1 christos /*
720 1.1 christos * Parse string into argbuf;
721 1.1 christos * implemented with FSM to
722 1.1 christos * handle quoting and strings
723 1.1 christos */
724 1.1 christos static char *
725 1.1 christos slurpstring(void)
726 1.1 christos {
727 1.1 christos int got_one = 0;
728 1.1 christos char *sb = stringbase;
729 1.1 christos char *ap = argbase;
730 1.1 christos char *tmp = argbase; /* will return this if token found */
731 1.1 christos
732 1.1 christos if (*sb == '!' || *sb == '$') { /* recognize ! as a token for shell */
733 1.1 christos switch (slrflag) { /* and $ as token for macro invoke */
734 1.1 christos case 0:
735 1.1 christos slrflag++;
736 1.1 christos INC_CHKCURSOR(stringbase);
737 1.1 christos return __UNCONST((*sb == '!') ? "!" : "$");
738 1.1 christos /* NOTREACHED */
739 1.1 christos case 1:
740 1.1 christos slrflag++;
741 1.9 christos #ifdef NEED_ALTARG
742 1.1 christos altarg = stringbase;
743 1.9 christos #endif
744 1.1 christos break;
745 1.1 christos default:
746 1.1 christos break;
747 1.1 christos }
748 1.1 christos }
749 1.1 christos
750 1.1 christos S0:
751 1.1 christos switch (*sb) {
752 1.1 christos
753 1.1 christos case '\0':
754 1.1 christos goto OUT;
755 1.1 christos
756 1.1 christos case ' ':
757 1.1 christos case '\t':
758 1.1 christos INC_CHKCURSOR(sb);
759 1.1 christos goto S0;
760 1.1 christos
761 1.1 christos default:
762 1.1 christos switch (slrflag) {
763 1.1 christos case 0:
764 1.1 christos slrflag++;
765 1.1 christos break;
766 1.1 christos case 1:
767 1.1 christos slrflag++;
768 1.9 christos #ifdef NEED_ALTARG
769 1.1 christos altarg = sb;
770 1.9 christos #endif
771 1.1 christos break;
772 1.1 christos default:
773 1.1 christos break;
774 1.1 christos }
775 1.1 christos goto S1;
776 1.1 christos }
777 1.1 christos
778 1.1 christos S1:
779 1.1 christos switch (*sb) {
780 1.1 christos
781 1.1 christos case ' ':
782 1.1 christos case '\t':
783 1.1 christos case '\0':
784 1.1 christos goto OUT; /* end of token */
785 1.1 christos
786 1.1 christos case '\\':
787 1.1 christos INC_CHKCURSOR(sb);
788 1.1 christos goto S2; /* slurp next character */
789 1.1 christos
790 1.1 christos case '"':
791 1.1 christos INC_CHKCURSOR(sb);
792 1.1 christos goto S3; /* slurp quoted string */
793 1.1 christos
794 1.1 christos default:
795 1.1 christos /* the first arg (command) is special - see execute() in lex.c */
796 1.8 christos if (margc == 0 && got_one &&
797 1.8 christos strchr(" \t0123456789$^.:/-+*'\"", *sb))
798 1.1 christos goto OUT;
799 1.1 christos
800 1.1 christos *ap = *sb; /* add character to token */
801 1.1 christos ap++;
802 1.1 christos INC_CHKCURSOR(sb);
803 1.1 christos got_one = 1;
804 1.1 christos goto S1;
805 1.1 christos }
806 1.1 christos
807 1.1 christos S2:
808 1.1 christos switch (*sb) {
809 1.1 christos
810 1.1 christos case '\0':
811 1.1 christos goto OUT;
812 1.1 christos
813 1.1 christos default:
814 1.1 christos *ap = *sb;
815 1.1 christos ap++;
816 1.1 christos INC_CHKCURSOR(sb);
817 1.1 christos got_one = 1;
818 1.1 christos goto S1;
819 1.1 christos }
820 1.1 christos
821 1.1 christos S3:
822 1.1 christos switch (*sb) {
823 1.1 christos
824 1.1 christos case '\0':
825 1.1 christos goto OUT;
826 1.1 christos
827 1.1 christos case '"':
828 1.1 christos INC_CHKCURSOR(sb);
829 1.1 christos goto S1;
830 1.1 christos
831 1.1 christos default:
832 1.1 christos *ap = *sb;
833 1.1 christos ap++;
834 1.1 christos INC_CHKCURSOR(sb);
835 1.1 christos got_one = 1;
836 1.1 christos goto S3;
837 1.1 christos }
838 1.1 christos
839 1.1 christos OUT:
840 1.1 christos if (got_one)
841 1.1 christos *ap++ = '\0';
842 1.1 christos argbase = ap; /* update storage pointer */
843 1.1 christos stringbase = sb; /* update scan pointer */
844 1.1 christos if (got_one) {
845 1.1 christos return (tmp);
846 1.1 christos }
847 1.1 christos switch (slrflag) {
848 1.1 christos case 0:
849 1.1 christos slrflag++;
850 1.1 christos break;
851 1.1 christos case 1:
852 1.1 christos slrflag++;
853 1.9 christos #ifdef NEED_ALTARG
854 1.1 christos altarg = NULL;
855 1.9 christos #endif
856 1.1 christos break;
857 1.1 christos default:
858 1.1 christos break;
859 1.1 christos }
860 1.1 christos return (NULL);
861 1.1 christos }
862 1.1 christos
863 1.1 christos
864 1.1 christos /*
865 1.1 christos * Slice a string up into argc/argv.
866 1.1 christos */
867 1.1 christos static void
868 1.1 christos makeargv(char *line)
869 1.1 christos {
870 1.1 christos static char argbuf[ LINESIZE ]; /* argument storage buffer */
871 1.1 christos char *argp;
872 1.1 christos
873 1.1 christos stringbase = line; /* scan from first of buffer */
874 1.1 christos argbase = argbuf; /* store from first of buffer */
875 1.1 christos slrflag = 0;
876 1.1 christos marg_sl->sl_cur = 0; /* reset to start of marg_sl */
877 1.9 christos for (margc = 0; /* EMPTY */; margc++) {
878 1.1 christos argp = slurpstring();
879 1.9 christos mail_sl_add(marg_sl, argp);
880 1.1 christos if (argp == NULL)
881 1.1 christos break;
882 1.1 christos }
883 1.1 christos #ifndef NO_EDITCOMPLETE
884 1.1 christos if (cursor_pos == line) {
885 1.1 christos cursor_argc = 0;
886 1.1 christos cursor_argo = 0;
887 1.1 christos } else if (cursor_pos != NULL) {
888 1.1 christos cursor_argc = margc;
889 1.1 christos cursor_argo = strlen(margv[margc-1]);
890 1.1 christos }
891 1.1 christos #endif /* !NO_EDITCOMPLETE */
892 1.1 christos }
893 1.1 christos
894 1.9 christos /* from /usr/src/usr.bin/ftp/main.c(1.101) - end */
895 1.1 christos /************************************************************************/
896 1.1 christos
897 1.9 christos /* Some people like to bind file completion to CTRL-D. In emacs mode,
898 1.9 christos * CTRL-D is also used to delete the current character, we have to
899 1.9 christos * special case this situation.
900 1.9 christos */
901 1.9 christos #define EMACS_CTRL_D_BINDING_HACK
902 1.9 christos
903 1.9 christos #ifdef EMACS_CTRL_D_BINDING_HACK
904 1.9 christos static int
905 1.9 christos is_emacs_mode(EditLine *el)
906 1.9 christos {
907 1.9 christos char *mode;
908 1.9 christos if (el_get(el, EL_EDITOR, &mode) == -1)
909 1.9 christos return 0;
910 1.9 christos return equal(mode, "emacs");
911 1.9 christos }
912 1.9 christos
913 1.9 christos static int
914 1.9 christos emacs_ctrl_d(EditLine *el, const LineInfo *lf, int ch, size_t len)
915 1.9 christos {
916 1.9 christos static char delunder[3] = { CTRL('f'), CTRL('h'), '\0' };
917 1.9 christos if (ch == CTRL('d') && is_emacs_mode(el)) { /* CTRL-D is special */
918 1.9 christos if (len == 0)
919 1.9 christos return (CC_EOF);
920 1.9 christos if (lf->lastchar != lf->cursor) { /* delete without using ^D */
921 1.9 christos el_push(el, delunder); /* ^F^H */
922 1.9 christos return (CC_NORM);
923 1.9 christos }
924 1.9 christos }
925 1.9 christos return -1;
926 1.9 christos }
927 1.9 christos #endif /* EMACS_CTRL_D_BINDING_HACK */
928 1.1 christos
929 1.1 christos /*
930 1.1 christos * Generic complete routine
931 1.1 christos */
932 1.1 christos static unsigned char
933 1.9 christos mail_complete(EditLine *el, int ch)
934 1.1 christos {
935 1.1 christos static char line[LINESIZE]; /* input line buffer */
936 1.1 christos static char word[LINESIZE];
937 1.1 christos static int lastc_argc, lastc_argo;
938 1.1 christos
939 1.1 christos const struct cmd *c;
940 1.1 christos const LineInfo *lf;
941 1.1 christos int celems, dolist, cmpltype;
942 1.1 christos size_t len;
943 1.1 christos
944 1.1 christos lf = el_line(el);
945 1.1 christos len = lf->lastchar - lf->buffer;
946 1.9 christos
947 1.9 christos #ifdef EMACS_CTRL_D_BINDING_HACK
948 1.9 christos {
949 1.9 christos int cc_ret;
950 1.9 christos if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
951 1.9 christos return cc_ret;
952 1.1 christos }
953 1.9 christos #endif /* EMACS_CTRL_D_BINDING_HACK */
954 1.9 christos
955 1.9 christos if (len >= sizeof(line) - 1)
956 1.1 christos return (CC_ERROR);
957 1.9 christos
958 1.9 christos (void)strlcpy(line, lf->buffer, len + 1); /* do not use estrlcpy here! */
959 1.1 christos cursor_pos = line + (lf->cursor - lf->buffer);
960 1.1 christos lastc_argc = cursor_argc; /* remember last cursor pos */
961 1.1 christos lastc_argo = cursor_argo;
962 1.1 christos makeargv(line); /* build argc/argv of current line */
963 1.1 christos
964 1.9 christos if (cursor_argo >= sizeof(word) - 1)
965 1.1 christos return (CC_ERROR);
966 1.1 christos
967 1.1 christos dolist = 0;
968 1.1 christos /* if cursor and word are the same, list alternatives */
969 1.1 christos if (lastc_argc == cursor_argc && lastc_argo == cursor_argo
970 1.1 christos && strncmp(word, margv[cursor_argc] ? margv[cursor_argc] : "",
971 1.9 christos (size_t)cursor_argo) == 0)
972 1.1 christos dolist = 1;
973 1.1 christos else if (cursor_argc < margc)
974 1.9 christos (void)strlcpy(word, margv[cursor_argc], (size_t)cursor_argo + 1); /* do not use estrlcpy() here */
975 1.1 christos word[cursor_argo] = '\0';
976 1.1 christos
977 1.1 christos if (cursor_argc == 0)
978 1.1 christos return (complete_command(el, word, dolist));
979 1.1 christos
980 1.1 christos c = getcmd(margv[0]);
981 1.1 christos if (c == NULL)
982 1.1 christos return (CC_ERROR);
983 1.1 christos celems = strlen(c->c_complete);
984 1.1 christos
985 1.1 christos /* check for 'continuation' completes (which are uppercase) */
986 1.1 christos if ((cursor_argc > celems) && (celems > 0)
987 1.1 christos && isupper((unsigned char) c->c_complete[celems-1]))
988 1.1 christos cursor_argc = celems;
989 1.1 christos
990 1.1 christos if (cursor_argc > celems)
991 1.1 christos return (CC_ERROR);
992 1.1 christos
993 1.1 christos cmpltype = c->c_complete[cursor_argc - 1];
994 1.1 christos switch (cmpltype) {
995 1.1 christos case 'a': /* alias complete */
996 1.1 christos case 'A':
997 1.1 christos return (complete_alias(el, word, dolist));
998 1.1 christos
999 1.1 christos case 'c': /* command complete */
1000 1.1 christos case 'C':
1001 1.1 christos return (complete_command(el, word, dolist));
1002 1.1 christos
1003 1.1 christos case 'f': /* filename complete */
1004 1.1 christos case 'F':
1005 1.1 christos return (complete_filename(el, word, dolist));
1006 1.10 christos #ifdef SMOPTS_CMD
1007 1.10 christos case 'm':
1008 1.10 christos case 'M':
1009 1.10 christos return (complete_smopts(el, word, dolist));
1010 1.10 christos #endif
1011 1.1 christos case 'n': /* no complete */
1012 1.1 christos case 'N': /* no complete */
1013 1.1 christos return (CC_ERROR);
1014 1.1 christos
1015 1.1 christos case 's':
1016 1.1 christos case 'S':
1017 1.1 christos return (complete_set(el, word, dolist));
1018 1.1 christos
1019 1.1 christos case 'x': /* executable complete */
1020 1.1 christos case 'X':
1021 1.1 christos return (complete_executable(el, word, dolist));
1022 1.1 christos
1023 1.1 christos default:
1024 1.1 christos errx(1, "unknown complete type `%c'", cmpltype);
1025 1.1 christos return (CC_ERROR);
1026 1.1 christos }
1027 1.1 christos /* NOTREACHED */
1028 1.1 christos }
1029 1.1 christos
1030 1.1 christos
1031 1.9 christos /*
1032 1.9 christos * Generic file completion routine
1033 1.9 christos */
1034 1.9 christos static unsigned char
1035 1.9 christos file_complete(EditLine *el, int ch)
1036 1.9 christos {
1037 1.9 christos static char word[LINESIZE];
1038 1.9 christos
1039 1.9 christos const LineInfo *lf;
1040 1.9 christos size_t len, word_len;
1041 1.9 christos
1042 1.9 christos lf = el_line(el);
1043 1.9 christos len = lf->lastchar - lf->buffer;
1044 1.9 christos
1045 1.9 christos #ifdef EMACS_CTRL_D_BINDING_HACK
1046 1.9 christos {
1047 1.9 christos int cc_ret;
1048 1.9 christos if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
1049 1.9 christos return cc_ret;
1050 1.9 christos }
1051 1.9 christos #endif /* EMACS_CTRL_D_BINDING_HACK */
1052 1.9 christos
1053 1.9 christos if (len >= sizeof(word) - 1)
1054 1.9 christos return (CC_ERROR);
1055 1.9 christos
1056 1.9 christos word_len = lf->cursor - lf->buffer;
1057 1.9 christos (void)strlcpy(word, lf->buffer, word_len + 1); /* do not use estrlcpy here! */
1058 1.9 christos return complete_filename(el, word, len != word_len);
1059 1.9 christos }
1060 1.1 christos
1061 1.1 christos
1062 1.9 christos #ifdef MIME_SUPPORT
1063 1.9 christos /*
1064 1.9 christos * Complete mime_transfer_encoding type
1065 1.9 christos */
1066 1.9 christos static unsigned char
1067 1.9 christos mime_enc_complete(EditLine *el, int ch)
1068 1.1 christos {
1069 1.9 christos static char word[LINESIZE];
1070 1.9 christos StringList *words;
1071 1.9 christos unsigned char rv;
1072 1.9 christos const LineInfo *lf;
1073 1.9 christos size_t len, word_len;
1074 1.9 christos
1075 1.9 christos lf = el_line(el);
1076 1.9 christos len = lf->lastchar - lf->buffer;
1077 1.1 christos
1078 1.9 christos #ifdef EMACS_CTRL_D_BINDING_HACK
1079 1.9 christos {
1080 1.9 christos int cc_ret;
1081 1.9 christos if ((cc_ret = emacs_ctrl_d(el, lf, ch, len)) != -1)
1082 1.9 christos return cc_ret;
1083 1.9 christos }
1084 1.9 christos #endif /* EMACS_CTRL_D_BINDING_HACK */
1085 1.1 christos
1086 1.9 christos if (len >= sizeof(word) - 1)
1087 1.9 christos return (CC_ERROR);
1088 1.1 christos
1089 1.9 christos words = mail_sl_init();
1090 1.9 christos word_len = lf->cursor - lf->buffer;
1091 1.9 christos {
1092 1.9 christos const char *ename;
1093 1.9 christos const void *cookie;
1094 1.9 christos cookie = NULL;
1095 1.9 christos for (ename = mime_next_encoding_name(&cookie);
1096 1.9 christos ename;
1097 1.9 christos ename = mime_next_encoding_name(&cookie))
1098 1.9 christos if (word_len == 0 ||
1099 1.9 christos strncmp(lf->buffer, ename, word_len) == 0) {
1100 1.9 christos char *cp;
1101 1.9 christos cp = estrdup(ename);
1102 1.9 christos mail_sl_add(words, cp);
1103 1.9 christos }
1104 1.9 christos }
1105 1.9 christos (void)strlcpy(word, lf->buffer, word_len + 1); /* do not use estrlcpy here */
1106 1.1 christos
1107 1.9 christos rv = complete_ambiguous(el, word, len != word_len, words);
1108 1.1 christos
1109 1.9 christos sl_free(words, 1);
1110 1.9 christos return (rv);
1111 1.1 christos }
1112 1.9 christos #endif /* MIME_SUPPORT */
1113 1.9 christos
1114 1.9 christos /*************************************************************************
1115 1.9 christos * Our public interface to el_gets():
1116 1.9 christos *
1117 1.9 christos * init_editline()
1118 1.9 christos * Initializes of all editline and completion data strutures.
1119 1.9 christos *
1120 1.9 christos * my_gets()
1121 1.9 christos * Returns the next line of input as a NULL termnated string without
1122 1.9 christos * the trailing newline.
1123 1.9 christos */
1124 1.1 christos
1125 1.9 christos static const char *el_prompt;
1126 1.9 christos
1127 1.9 christos /*ARGSUSED*/
1128 1.9 christos static const char *
1129 1.9 christos show_prompt(EditLine *e __unused)
1130 1.9 christos {
1131 1.9 christos return el_prompt;
1132 1.9 christos }
1133 1.1 christos
1134 1.1 christos char *
1135 1.9 christos my_gets(el_mode_t *em, const char *prompt, char *string)
1136 1.1 christos {
1137 1.9 christos int cnt;
1138 1.9 christos size_t len;
1139 1.9 christos const char *buf;
1140 1.9 christos HistEvent ev;
1141 1.3 christos static char line[LINE_MAX];
1142 1.1 christos
1143 1.9 christos el_prompt = prompt;
1144 1.1 christos
1145 1.1 christos if (string)
1146 1.9 christos el_push(em->el, string);
1147 1.9 christos
1148 1.9 christos buf = el_gets(em->el, &cnt);
1149 1.1 christos
1150 1.3 christos if (buf == NULL || cnt <= 0) {
1151 1.3 christos if (cnt == 0)
1152 1.9 christos (void)putc('\n', stdout);
1153 1.9 christos return NULL;
1154 1.3 christos }
1155 1.1 christos
1156 1.9 christos cnt--; /* trash the trailing LF */
1157 1.9 christos len = MIN(sizeof(line) - 1, cnt);
1158 1.9 christos (void)memcpy(line, buf, len);
1159 1.1 christos line[cnt] = '\0';
1160 1.1 christos
1161 1.9 christos /* enter non-empty lines into history */
1162 1.9 christos if (em->hist) {
1163 1.9 christos const char *p;
1164 1.9 christos p = skip_white(line);
1165 1.9 christos if (*p && history(em->hist, &ev, H_ENTER, line) == 0)
1166 1.9 christos (void)printf("Failed history entry: %s", line);
1167 1.9 christos }
1168 1.1 christos return line;
1169 1.1 christos }
1170 1.1 christos
1171 1.9 christos static el_mode_t
1172 1.9 christos init_el_mode(
1173 1.9 christos const char *el_editor,
1174 1.9 christos unsigned char (*completer)(EditLine *, int),
1175 1.9 christos struct name *keys,
1176 1.9 christos int history_size)
1177 1.9 christos {
1178 1.9 christos el_mode_t em;
1179 1.9 christos (void)memset(&em, 0, sizeof(em));
1180 1.9 christos
1181 1.9 christos em.el = el_init(getprogname(), stdin, stdout, stderr);
1182 1.1 christos
1183 1.9 christos (void)el_set(em.el, EL_PROMPT, show_prompt);
1184 1.9 christos (void)el_set(em.el, EL_SIGNAL, SIGHUP);
1185 1.9 christos
1186 1.9 christos if (el_editor)
1187 1.9 christos (void)el_set(em.el, EL_EDITOR, el_editor);
1188 1.9 christos
1189 1.9 christos if (completer) {
1190 1.9 christos struct name *np;
1191 1.9 christos (void)el_set(em.el, EL_ADDFN, "mail-complete",
1192 1.9 christos "Context sensitive argument completion", completer);
1193 1.9 christos for (np = keys; np; np = np->n_flink)
1194 1.9 christos (void)el_set(em.el, EL_BIND, np->n_name,
1195 1.9 christos "mail-complete", NULL);
1196 1.9 christos }
1197 1.9 christos
1198 1.9 christos if (history_size) {
1199 1.9 christos HistEvent ev;
1200 1.9 christos em.hist = history_init();
1201 1.9 christos if (history(em.hist, &ev, H_SETSIZE, history_size))
1202 1.9 christos (void)printf("history: %s\n", ev.str);
1203 1.9 christos (void)el_set(em.el, EL_HIST, history, em.hist);
1204 1.9 christos }
1205 1.9 christos
1206 1.9 christos (void)el_source(em.el, NULL); /* read ~/.editrc */
1207 1.9 christos
1208 1.9 christos return em;
1209 1.1 christos }
1210 1.1 christos
1211 1.9 christos
1212 1.9 christos struct el_modes_s elm = {
1213 1.9 christos .command = { .el = NULL, .hist = NULL, },
1214 1.9 christos .string = { .el = NULL, .hist = NULL, },
1215 1.9 christos .filec = { .el = NULL, .hist = NULL, },
1216 1.9 christos #ifdef MIME_SUPPORT
1217 1.9 christos .mime_enc = { .el = NULL, .hist = NULL, },
1218 1.9 christos #endif
1219 1.9 christos };
1220 1.9 christos
1221 1.1 christos void
1222 1.9 christos init_editline(void)
1223 1.1 christos {
1224 1.9 christos const char *mode;
1225 1.9 christos int hist_size;
1226 1.9 christos struct name *keys;
1227 1.9 christos char *cp;
1228 1.9 christos
1229 1.9 christos mode = value(ENAME_EL_EDITOR);
1230 1.3 christos
1231 1.9 christos cp = value(ENAME_EL_HISTORY_SIZE);
1232 1.9 christos hist_size = cp ? atoi(cp) : 0;
1233 1.1 christos
1234 1.9 christos cp = value(ENAME_EL_COMPLETION_KEYS);
1235 1.9 christos keys = cp && *cp ? lexpand(cp, 0) : NULL;
1236 1.1 christos
1237 1.9 christos elm.command = init_el_mode(mode, mail_complete, keys, hist_size);
1238 1.9 christos elm.filec = init_el_mode(mode, file_complete, keys, 0);
1239 1.9 christos elm.string = init_el_mode(mode, NULL, NULL, 0);
1240 1.9 christos #ifdef MIME_SUPPORT
1241 1.9 christos elm.mime_enc = init_el_mode(mode, mime_enc_complete, keys, 0);
1242 1.9 christos #endif
1243 1.1 christos init_complete();
1244 1.1 christos
1245 1.1 christos return;
1246 1.1 christos }
1247 1.1 christos
1248 1.9 christos #endif /* USE_EDITLINE */
1249