1 /* $NetBSD: el.c,v 1.101 2022/10/30 19:11:31 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.101 2022/10/30 19:11:31 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * el.c: EditLine interface functions
47 #include <sys/types.h>
48 #include <sys/param.h>
61 * Initialize editline and set default parameters.
64 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
66 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
70 libedit_private EditLine *
71 el_init_internal(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
72 int fdin, int fdout, int fderr, int flags)
74 EditLine *el = el_calloc(1, sizeof(*el));
80 el->el_outfile = fout;
81 el->el_errfile = ferr;
87 el->el_prog = wcsdup(ct_decode_string(prog, &el->el_scratch));
88 if (el->el_prog == NULL) {
94 * Initialize all the modules. Order is important!!!
98 if (terminal_init(el) == -1) {
103 (void) keymacro_init(el);
105 if (tty_init(el) == -1)
106 el->el_flags |= NO_TTY;
108 (void) search_init(el);
109 (void) hist_init(el);
110 (void) prompt_init(el);
112 (void) literal_init(el);
113 if (read_init(el) == -1) {
121 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
122 int fdin, int fdout, int fderr)
124 return el_init_internal(prog, fin, fout, ferr, fdin, fdout, fderr, 0);
142 if (!(el->el_flags & NO_TTY))
143 tty_end(el, TCSAFLUSH);
152 el_free(el->el_prog);
153 el_free(el->el_visual.cbuff);
154 el_free(el->el_visual.wbuff);
155 el_free(el->el_scratch.cbuff);
156 el_free(el->el_scratch.wbuff);
157 el_free(el->el_lgcyconv.cbuff);
158 el_free(el->el_lgcyconv.wbuff);
164 * Reset the tty and the parser
167 el_reset(EditLine *el)
171 ch_reset(el); /* XXX: Do we want that? */
176 * set the editline parameters
179 el_wset(EditLine *el, int op, ...)
191 el_pfunc_t p = va_arg(ap, el_pfunc_t);
193 rv = prompt_set(el, p, 0, op, 1);
198 el_zfunc_t p = va_arg(ap, el_zfunc_t);
199 void *arg = va_arg(ap, void *);
200 rv = ch_resizefun(el, p, arg);
204 case EL_ALIAS_TEXT: {
205 el_afunc_t p = va_arg(ap, el_afunc_t);
206 void *arg = va_arg(ap, void *);
207 rv = ch_aliasfun(el, p, arg);
212 case EL_RPROMPT_ESC: {
213 el_pfunc_t p = va_arg(ap, el_pfunc_t);
214 int c = va_arg(ap, int);
216 rv = prompt_set(el, p, (wchar_t)c, op, 1);
221 rv = terminal_set(el, va_arg(ap, char *));
225 rv = map_set_editor(el, va_arg(ap, wchar_t *));
230 el->el_flags |= HANDLE_SIGNALS;
232 el->el_flags &= ~HANDLE_SIGNALS;
241 const wchar_t *argv[20];
244 for (i = 1; i < (int)__arraycount(argv); i++)
245 if ((argv[i] = va_arg(ap, wchar_t *)) == NULL)
251 rv = map_bind(el, i, argv);
256 rv = terminal_telltc(el, i, argv);
261 rv = terminal_settc(el, i, argv);
266 rv = terminal_echotc(el, i, argv);
271 rv = tty_stty(el, i, argv);
276 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
284 wchar_t *name = va_arg(ap, wchar_t *);
285 wchar_t *help = va_arg(ap, wchar_t *);
286 el_func_t func = va_arg(ap, el_func_t);
288 rv = map_addfunc(el, name, help, func);
294 hist_fun_t func = va_arg(ap, hist_fun_t);
295 void *ptr = va_arg(ap, void *);
297 rv = hist_set(el, func, ptr);
299 el->el_flags &= ~NARROW_HISTORY;
305 el->el_flags |= FIXIO;
307 el->el_flags &= ~FIXIO;
313 el->el_flags &= ~EDIT_DISABLED;
315 el->el_flags |= EDIT_DISABLED;
321 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
322 rv = el_read_setfn(el->el_read, rc);
327 el->el_data = va_arg(ap, void *);
331 rv = va_arg(ap, int);
332 if (rv && !(el->el_flags & UNBUFFERED)) {
333 el->el_flags |= UNBUFFERED;
335 } else if (!rv && (el->el_flags & UNBUFFERED)) {
336 el->el_flags &= ~UNBUFFERED;
343 rv = va_arg(ap, int);
345 (void) tty_rawmode(el);
347 (void) tty_cookedmode(el);
356 what = va_arg(ap, int);
357 fp = va_arg(ap, FILE *);
363 el->el_infd = fileno(fp);
367 el->el_outfd = fileno(fp);
371 el->el_errfd = fileno(fp);
381 re_clear_display(el);
397 * retrieve the editline parameters
400 el_wget(EditLine *el, int op, ...)
413 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
414 rv = prompt_get(el, p, 0, op);
418 case EL_RPROMPT_ESC: {
419 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
420 wchar_t *c = va_arg(ap, wchar_t *);
422 rv = prompt_get(el, p, c, op);
427 rv = map_get_editor(el, va_arg(ap, const wchar_t **));
431 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
436 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
441 *va_arg(ap, int *) = (el->el_flags & FIXIO);
446 terminal_get(el, va_arg(ap, const char **));
452 static char name[] = "gettc";
455 argv[1] = va_arg(ap, char *);
456 argv[2] = va_arg(ap, void *);
457 rv = terminal_gettc(el, 3, argv);
462 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
467 *va_arg(ap, void **) = el->el_data;
472 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
481 what = va_arg(ap, int);
482 fpp = va_arg(ap, FILE **);
486 *fpp = el->el_infile;
489 *fpp = el->el_outfile;
492 *fpp = el->el_errfile;
511 * Return editing info
514 el_wline(EditLine *el)
517 return (const LineInfoW *)(void *)&el->el_line;
525 el_source(EditLine *el, const char *fname)
537 #ifdef HAVE_ISSETUGID
541 if ((fname = getenv("EDITRC")) == NULL) {
542 static const char elpath[] = "/.editrc";
543 size_t plen = sizeof(elpath);
545 if ((ptr = getenv("HOME")) == NULL)
548 if ((path = el_calloc(plen, sizeof(*path))) == NULL)
550 (void)snprintf(path, plen, "%s%s", ptr,
551 elpath + (*ptr == '\0'));
556 * If issetugid() is missing, always return an error, in order
557 * to keep from inadvertently opening up the user to a security
563 if (fname[0] == '\0')
567 fp = fopen(fname, "r");
575 while ((slen = getline(&ptr, &len, fp)) != -1) {
577 continue; /* Empty line. */
578 if (slen > 0 && ptr[--slen] == '\n')
581 dptr = ct_decode_string(ptr, &el->el_scratch);
584 /* loop until first non-space char or EOL */
585 while (*dptr != '\0' && iswspace(*dptr))
588 continue; /* ignore, this is a comment line */
589 if ((error = parse_line(el, dptr)) == -1)
601 * Called from program when terminal is resized
604 el_resize(EditLine *el)
609 (void) sigemptyset(&nset);
610 (void) sigaddset(&nset, SIGWINCH);
611 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
613 /* get the correct window size */
614 if (terminal_get_size(el, &lins, &cols))
615 terminal_change_size(el, lins, cols);
617 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
622 * Called from the program to beep
625 el_beep(EditLine *el)
633 * Set the state of EDIT_DISABLED from the `edit' command.
637 el_editmode(EditLine *el, int argc, const wchar_t **argv)
641 if (argv == NULL || argc != 2 || argv[1] == NULL)
645 if (wcscmp(how, L"on") == 0) {
646 el->el_flags &= ~EDIT_DISABLED;
648 } else if (wcscmp(how, L"off") == 0) {
650 el->el_flags |= EDIT_DISABLED;
653 (void) fprintf(el->el_errfile, "edit: Bad value `%ls'.\n",