1 /* $NetBSD: el.c,v 1.83 2016/02/24 17:13:22 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
40 __RCSID("$NetBSD: el.c,v 1.83 2016/02/24 17:13:22 christos Exp $");
42 #endif /* not lint && not SCCSID */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * el.c: EditLine interface functions
49 #include <sys/types.h>
50 #include <sys/param.h>
64 * Initialize editline and set default parameters.
67 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
69 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
74 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
75 int fdin, int fdout, int fderr)
77 EditLine *el = el_malloc(sizeof(*el));
82 memset(el, 0, sizeof(EditLine));
85 el->el_outfile = fout;
86 el->el_errfile = ferr;
92 el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
93 if (el->el_prog == NULL) {
99 * Initialize all the modules. Order is important!!!
103 if (terminal_init(el) == -1) {
104 el_free(el->el_prog);
108 (void) keymacro_init(el);
110 if (tty_init(el) == -1)
111 el->el_flags |= NO_TTY;
113 (void) search_init(el);
114 (void) hist_init(el);
115 (void) prompt_init(el);
117 (void) read_init(el);
138 if (!(el->el_flags & NO_TTY))
146 el_free(el->el_prog);
148 el_free(el->el_scratch.cbuff);
149 el_free(el->el_scratch.wbuff);
150 el_free(el->el_lgcyconv.cbuff);
151 el_free(el->el_lgcyconv.wbuff);
158 * Reset the tty and the parser
161 el_reset(EditLine *el)
165 ch_reset(el, 0); /* XXX: Do we want that? */
170 * set the editline parameters
173 FUN(el,set)(EditLine *el, int op, ...)
185 el_pfunc_t p = va_arg(ap, el_pfunc_t);
187 rv = prompt_set(el, p, 0, op, 1);
192 el_zfunc_t p = va_arg(ap, el_zfunc_t);
193 void *arg = va_arg(ap, void *);
194 rv = ch_resizefun(el, p, arg);
198 case EL_ALIAS_TEXT: {
199 el_afunc_t p = va_arg(ap, el_afunc_t);
200 void *arg = va_arg(ap, void *);
201 rv = ch_aliasfun(el, p, arg);
206 case EL_RPROMPT_ESC: {
207 el_pfunc_t p = va_arg(ap, el_pfunc_t);
208 int c = va_arg(ap, int);
210 rv = prompt_set(el, p, (Char)c, op, 1);
215 rv = terminal_set(el, va_arg(ap, char *));
219 rv = map_set_editor(el, va_arg(ap, Char *));
224 el->el_flags |= HANDLE_SIGNALS;
226 el->el_flags &= ~HANDLE_SIGNALS;
235 const Char *argv[20];
238 for (i = 1; i < (int)__arraycount(argv); i++)
239 if ((argv[i] = va_arg(ap, Char *)) == NULL)
244 argv[0] = STR("bind");
245 rv = map_bind(el, i, argv);
249 argv[0] = STR("telltc");
250 rv = terminal_telltc(el, i, argv);
254 argv[0] = STR("settc");
255 rv = terminal_settc(el, i, argv);
259 argv[0] = STR("echotc");
260 rv = terminal_echotc(el, i, argv);
264 argv[0] = STR("setty");
265 rv = tty_stty(el, i, argv);
270 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
278 Char *name = va_arg(ap, Char *);
279 Char *help = va_arg(ap, Char *);
280 el_func_t func = va_arg(ap, el_func_t);
282 rv = map_addfunc(el, name, help, func);
288 hist_fun_t func = va_arg(ap, hist_fun_t);
289 void *ptr = va_arg(ap, void *);
291 rv = hist_set(el, func, ptr);
293 el->el_flags &= ~NARROW_HISTORY;
299 el->el_flags &= ~EDIT_DISABLED;
301 el->el_flags |= EDIT_DISABLED;
307 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
308 rv = el_read_setfn(el, rc);
313 el->el_data = va_arg(ap, void *);
317 rv = va_arg(ap, int);
318 if (rv && !(el->el_flags & UNBUFFERED)) {
319 el->el_flags |= UNBUFFERED;
321 } else if (!rv && (el->el_flags & UNBUFFERED)) {
322 el->el_flags &= ~UNBUFFERED;
329 rv = va_arg(ap, int);
331 (void) tty_rawmode(el);
333 (void) tty_cookedmode(el);
342 what = va_arg(ap, int);
343 fp = va_arg(ap, FILE *);
349 el->el_infd = fileno(fp);
353 el->el_outfd = fileno(fp);
357 el->el_errfd = fileno(fp);
367 re_clear_display(el);
383 * retrieve the editline parameters
386 FUN(el,get)(EditLine *el, int op, ...)
399 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
400 rv = prompt_get(el, p, 0, op);
404 case EL_RPROMPT_ESC: {
405 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
406 Char *c = va_arg(ap, Char *);
408 rv = prompt_get(el, p, c, op);
413 rv = map_get_editor(el, va_arg(ap, const Char **));
417 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
422 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
427 terminal_get(el, va_arg(ap, const char **));
433 static char name[] = "gettc";
437 for (i = 1; i < (int)__arraycount(argv); i++)
438 if ((argv[i] = va_arg(ap, char *)) == NULL)
442 rv = terminal_gettc(el, i, argv);
447 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
452 *va_arg(ap, void **) = el->el_data;
457 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
466 what = va_arg(ap, int);
467 fpp = va_arg(ap, FILE **);
471 *fpp = el->el_infile;
474 *fpp = el->el_outfile;
477 *fpp = el->el_errfile;
496 * Return editing info
498 public const TYPE(LineInfo) *
499 FUN(el,line)(EditLine *el)
502 return (const TYPE(LineInfo) *)(void *)&el->el_line;
510 el_source(EditLine *el, const char *fname)
522 #ifdef HAVE_ISSETUGID
523 static const char elpath[] = "/.editrc";
524 size_t plen = sizeof(elpath);
528 if ((ptr = getenv("HOME")) == NULL)
531 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
533 (void)snprintf(path, plen, "%s%s", ptr, elpath);
537 * If issetugid() is missing, always return an error, in order
538 * to keep from inadvertently opening up the user to a security
545 fp = fopen(fname, "r");
553 while ((slen = getline(&ptr, &len, fp)) != -1) {
555 continue; /* Empty line. */
556 if (slen > 0 && ptr[--slen] == '\n')
559 dptr = ct_decode_string(ptr, &el->el_scratch);
562 /* loop until first non-space char or EOL */
563 while (*dptr != '\0' && Isspace(*dptr))
566 continue; /* ignore, this is a comment line */
567 if ((error = parse_line(el, dptr)) == -1)
579 * Called from program when terminal is resized
582 el_resize(EditLine *el)
587 (void) sigemptyset(&nset);
588 (void) sigaddset(&nset, SIGWINCH);
589 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
591 /* get the correct window size */
592 if (terminal_get_size(el, &lins, &cols))
593 terminal_change_size(el, lins, cols);
595 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
600 * Called from the program to beep
603 el_beep(EditLine *el)
611 * Set the state of EDIT_DISABLED from the `edit' command.
615 el_editmode(EditLine *el, int argc, const Char **argv)
619 if (argv == NULL || argc != 2 || argv[1] == NULL)
623 if (Strcmp(how, STR("on")) == 0) {
624 el->el_flags &= ~EDIT_DISABLED;
626 } else if (Strcmp(how, STR("off")) == 0) {
628 el->el_flags |= EDIT_DISABLED;
631 (void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",