1 /* $NetBSD: el.c,v 1.97 2018/11/18 17:09:39 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.97 2018/11/18 17:09:39 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * el.c: EditLine interface functions
47 #include <sys/types.h>
48 #include <sys/param.h>
60 #ifndef HAVE_SECURE_GETENV
61 # ifdef HAVE___SECURE_GETENV
62 # define secure_getenv __secure_getenv
63 # define HAVE_SECURE_GETENV 1
65 # ifdef HAVE_ISSETUGID
69 # define issetugid() 1
74 #ifndef HAVE_SECURE_GETENV
75 char *secure_getenv(char const *name)
84 * Initialize editline and set default parameters.
87 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
89 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
93 libedit_private EditLine *
94 el_init_internal(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
95 int fdin, int fdout, int fderr, int flags)
97 EditLine *el = el_malloc(sizeof(*el));
102 memset(el, 0, sizeof(EditLine));
105 el->el_outfile = fout;
106 el->el_errfile = ferr;
109 el->el_outfd = fdout;
110 el->el_errfd = fderr;
112 el->el_prog = wcsdup(ct_decode_string(prog, &el->el_scratch));
113 if (el->el_prog == NULL) {
119 * Initialize all the modules. Order is important!!!
121 el->el_flags = flags;
123 if (terminal_init(el) == -1) {
124 el_free(el->el_prog);
128 (void) keymacro_init(el);
130 if (tty_init(el) == -1)
131 el->el_flags |= NO_TTY;
133 (void) search_init(el);
134 (void) hist_init(el);
135 (void) prompt_init(el);
137 (void) literal_init(el);
138 if (read_init(el) == -1) {
146 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
147 int fdin, int fdout, int fderr)
149 return el_init_internal(prog, fin, fout, ferr, fdin, fdout, fderr, 0);
167 if (!(el->el_flags & NO_TTY))
168 tty_end(el, TCSAFLUSH);
170 read_end(el->el_read);
177 el_free(el->el_prog);
178 el_free(el->el_visual.cbuff);
179 el_free(el->el_visual.wbuff);
180 el_free(el->el_scratch.cbuff);
181 el_free(el->el_scratch.wbuff);
182 el_free(el->el_lgcyconv.cbuff);
183 el_free(el->el_lgcyconv.wbuff);
189 * Reset the tty and the parser
192 el_reset(EditLine *el)
196 ch_reset(el); /* XXX: Do we want that? */
201 * set the editline parameters
204 el_wset(EditLine *el, int op, ...)
216 el_pfunc_t p = va_arg(ap, el_pfunc_t);
218 rv = prompt_set(el, p, 0, op, 1);
223 el_zfunc_t p = va_arg(ap, el_zfunc_t);
224 void *arg = va_arg(ap, void *);
225 rv = ch_resizefun(el, p, arg);
229 case EL_ALIAS_TEXT: {
230 el_afunc_t p = va_arg(ap, el_afunc_t);
231 void *arg = va_arg(ap, void *);
232 rv = ch_aliasfun(el, p, arg);
237 case EL_RPROMPT_ESC: {
238 el_pfunc_t p = va_arg(ap, el_pfunc_t);
239 int c = va_arg(ap, int);
241 rv = prompt_set(el, p, (wchar_t)c, op, 1);
246 rv = terminal_set(el, va_arg(ap, char *));
250 rv = map_set_editor(el, va_arg(ap, wchar_t *));
255 el->el_flags |= HANDLE_SIGNALS;
257 el->el_flags &= ~HANDLE_SIGNALS;
266 const wchar_t *argv[20];
269 for (i = 1; i < (int)__arraycount(argv); i++)
270 if ((argv[i] = va_arg(ap, wchar_t *)) == NULL)
276 rv = map_bind(el, i, argv);
281 rv = terminal_telltc(el, i, argv);
286 rv = terminal_settc(el, i, argv);
291 rv = terminal_echotc(el, i, argv);
296 rv = tty_stty(el, i, argv);
301 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
309 wchar_t *name = va_arg(ap, wchar_t *);
310 wchar_t *help = va_arg(ap, wchar_t *);
311 el_func_t func = va_arg(ap, el_func_t);
313 rv = map_addfunc(el, name, help, func);
319 hist_fun_t func = va_arg(ap, hist_fun_t);
320 void *ptr = va_arg(ap, void *);
322 rv = hist_set(el, func, ptr);
324 el->el_flags &= ~NARROW_HISTORY;
330 el->el_flags &= ~EDIT_DISABLED;
332 el->el_flags |= EDIT_DISABLED;
338 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
339 rv = el_read_setfn(el->el_read, rc);
344 el->el_data = va_arg(ap, void *);
348 rv = va_arg(ap, int);
349 if (rv && !(el->el_flags & UNBUFFERED)) {
350 el->el_flags |= UNBUFFERED;
352 } else if (!rv && (el->el_flags & UNBUFFERED)) {
353 el->el_flags &= ~UNBUFFERED;
360 rv = va_arg(ap, int);
362 (void) tty_rawmode(el);
364 (void) tty_cookedmode(el);
373 what = va_arg(ap, int);
374 fp = va_arg(ap, FILE *);
380 el->el_infd = fileno(fp);
384 el->el_outfd = fileno(fp);
388 el->el_errfd = fileno(fp);
398 re_clear_display(el);
414 * retrieve the editline parameters
417 el_wget(EditLine *el, int op, ...)
430 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
431 rv = prompt_get(el, p, 0, op);
435 case EL_RPROMPT_ESC: {
436 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
437 wchar_t *c = va_arg(ap, wchar_t *);
439 rv = prompt_get(el, p, c, op);
444 rv = map_get_editor(el, va_arg(ap, const wchar_t **));
448 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
453 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
458 terminal_get(el, va_arg(ap, const char **));
464 static char name[] = "gettc";
468 for (i = 1; i < (int)__arraycount(argv); i++)
469 if ((argv[i] = va_arg(ap, char *)) == NULL)
473 rv = terminal_gettc(el, i, argv);
478 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
483 *va_arg(ap, void **) = el->el_data;
488 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
497 what = va_arg(ap, int);
498 fpp = va_arg(ap, FILE **);
502 *fpp = el->el_infile;
505 *fpp = el->el_outfile;
508 *fpp = el->el_errfile;
527 * Return editing info
530 el_wline(EditLine *el)
533 return (const LineInfoW *)(void *)&el->el_line;
541 el_source(EditLine *el, const char *fname)
553 #ifdef HAVE_ISSETUGID
557 if ((fname = getenv("EDITRC")) == NULL) {
558 static const char elpath[] = "/.editrc";
559 size_t plen = sizeof(elpath);
561 if ((ptr = getenv("HOME")) == NULL)
564 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
566 (void)snprintf(path, plen, "%s%s", ptr,
567 elpath + (*ptr == '\0'));
572 * If issetugid() is missing, always return an error, in order
573 * to keep from inadvertently opening up the user to a security
579 if (fname[0] == '\0')
583 fp = fopen(fname, "r");
591 while ((slen = getline(&ptr, &len, fp)) != -1) {
593 continue; /* Empty line. */
594 if (slen > 0 && ptr[--slen] == '\n')
597 dptr = ct_decode_string(ptr, &el->el_scratch);
600 /* loop until first non-space char or EOL */
601 while (*dptr != '\0' && iswspace(*dptr))
604 continue; /* ignore, this is a comment line */
605 if ((error = parse_line(el, dptr)) == -1)
617 * Called from program when terminal is resized
620 el_resize(EditLine *el)
625 (void) sigemptyset(&nset);
626 (void) sigaddset(&nset, SIGWINCH);
627 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
629 /* get the correct window size */
630 if (terminal_get_size(el, &lins, &cols))
631 terminal_change_size(el, lins, cols);
633 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
638 * Called from the program to beep
641 el_beep(EditLine *el)
649 * Set the state of EDIT_DISABLED from the `edit' command.
653 el_editmode(EditLine *el, int argc, const wchar_t **argv)
657 if (argv == NULL || argc != 2 || argv[1] == NULL)
661 if (wcscmp(how, L"on") == 0) {
662 el->el_flags &= ~EDIT_DISABLED;
664 } else if (wcscmp(how, L"off") == 0) {
666 el->el_flags |= EDIT_DISABLED;
669 (void) fprintf(el->el_errfile, "edit: Bad value `%ls'.\n",