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