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