1 /* $NetBSD: el.c,v 1.74 2015/12/08 12:56:55 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.74 2015/12/08 12:56:55 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>
60 * Initialize editline and set default parameters.
63 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
65 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
70 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
71 int fdin, int fdout, int fderr)
73 EditLine *el = el_malloc(sizeof(*el));
78 memset(el, 0, sizeof(EditLine));
81 el->el_outfile = fout;
82 el->el_errfile = ferr;
88 el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
89 if (el->el_prog == NULL) {
95 * Initialize all the modules. Order is important!!!
99 if (setlocale(LC_CTYPE, NULL) != NULL){
100 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
101 el->el_flags |= CHARSET_IS_UTF8;
105 if (terminal_init(el) == -1) {
106 el_free(el->el_prog);
110 (void) keymacro_init(el);
112 if (tty_init(el) == -1)
113 el->el_flags |= NO_TTY;
115 (void) search_init(el);
116 (void) hist_init(el);
117 (void) prompt_init(el);
119 (void) read_init(el);
140 if (!(el->el_flags & NO_TTY))
148 el_free(el->el_prog);
150 el_free(el->el_scratch.cbuff);
151 el_free(el->el_scratch.wbuff);
152 el_free(el->el_lgcyconv.cbuff);
153 el_free(el->el_lgcyconv.wbuff);
160 * Reset the tty and the parser
163 el_reset(EditLine *el)
167 ch_reset(el, 0); /* XXX: Do we want that? */
172 * set the editline parameters
175 FUN(el,set)(EditLine *el, int op, ...)
187 el_pfunc_t p = va_arg(ap, el_pfunc_t);
189 rv = prompt_set(el, p, 0, op, 1);
194 el_zfunc_t p = va_arg(ap, el_zfunc_t);
195 void *arg = va_arg(ap, void *);
196 rv = ch_resizefun(el, p, arg);
200 case EL_ALIAS_TEXT: {
201 el_afunc_t p = va_arg(ap, el_afunc_t);
202 void *arg = va_arg(ap, void *);
203 rv = ch_aliasfun(el, p, arg);
208 case EL_RPROMPT_ESC: {
209 el_pfunc_t p = va_arg(ap, el_pfunc_t);
210 int c = va_arg(ap, int);
212 rv = prompt_set(el, p, c, op, 1);
217 rv = terminal_set(el, va_arg(ap, char *));
221 rv = map_set_editor(el, va_arg(ap, Char *));
226 el->el_flags |= HANDLE_SIGNALS;
228 el->el_flags &= ~HANDLE_SIGNALS;
237 const Char *argv[20];
240 for (i = 1; i < (int)__arraycount(argv); i++)
241 if ((argv[i] = va_arg(ap, Char *)) == NULL)
246 argv[0] = STR("bind");
247 rv = map_bind(el, i, argv);
251 argv[0] = STR("telltc");
252 rv = terminal_telltc(el, i, argv);
256 argv[0] = STR("settc");
257 rv = terminal_settc(el, i, argv);
261 argv[0] = STR("echotc");
262 rv = terminal_echotc(el, i, argv);
266 argv[0] = STR("setty");
267 rv = tty_stty(el, i, argv);
272 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
280 Char *name = va_arg(ap, Char *);
281 Char *help = va_arg(ap, Char *);
282 el_func_t func = va_arg(ap, el_func_t);
284 rv = map_addfunc(el, name, help, func);
290 hist_fun_t func = va_arg(ap, hist_fun_t);
291 void *ptr = va_arg(ap, void *);
293 rv = hist_set(el, func, ptr);
294 if (!(el->el_flags & CHARSET_IS_UTF8))
295 el->el_flags &= ~NARROW_HISTORY;
301 el->el_flags &= ~EDIT_DISABLED;
303 el->el_flags |= EDIT_DISABLED;
309 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
310 rv = el_read_setfn(el, rc);
311 el->el_flags &= ~NARROW_READ;
316 el->el_data = va_arg(ap, void *);
320 rv = va_arg(ap, int);
321 if (rv && !(el->el_flags & UNBUFFERED)) {
322 el->el_flags |= UNBUFFERED;
324 } else if (!rv && (el->el_flags & UNBUFFERED)) {
325 el->el_flags &= ~UNBUFFERED;
332 rv = va_arg(ap, int);
334 (void) tty_rawmode(el);
336 (void) tty_cookedmode(el);
345 what = va_arg(ap, int);
346 fp = va_arg(ap, FILE *);
352 el->el_infd = fileno(fp);
356 el->el_outfd = fileno(fp);
360 el->el_errfd = fileno(fp);
370 re_clear_display(el);
386 * retrieve the editline parameters
389 FUN(el,get)(EditLine *el, int op, ...)
402 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
403 rv = prompt_get(el, p, 0, op);
407 case EL_RPROMPT_ESC: {
408 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
409 Char *c = va_arg(ap, Char *);
411 rv = prompt_get(el, p, c, op);
416 rv = map_get_editor(el, va_arg(ap, const Char **));
420 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
425 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
430 terminal_get(el, va_arg(ap, const char **));
436 static char name[] = "gettc";
440 for (i = 1; i < (int)__arraycount(argv); i++)
441 if ((argv[i] = va_arg(ap, char *)) == NULL)
445 rv = terminal_gettc(el, i, argv);
450 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
455 *va_arg(ap, void **) = el->el_data;
460 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
469 what = va_arg(ap, int);
470 fpp = va_arg(ap, FILE **);
474 *fpp = el->el_infile;
477 *fpp = el->el_outfile;
480 *fpp = el->el_errfile;
499 * Return editing info
501 public const TYPE(LineInfo) *
502 FUN(el,line)(EditLine *el)
505 return (const TYPE(LineInfo) *)(void *)&el->el_line;
513 el_source(EditLine *el, const char *fname)
524 #ifdef HAVE_ISSETUGID
525 static const char elpath[] = "/.editrc";
526 size_t plen = sizeof(elpath);
530 if ((ptr = getenv("HOME")) == NULL)
533 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
535 (void)snprintf(path, plen, "%s%s", ptr, elpath);
539 * If issetugid() is missing, always return an error, in order
540 * to keep from inadvertently opening up the user to a security
547 fp = fopen(fname, "r");
553 while ((ptr = fgetln(fp, &len)) != NULL) {
555 continue; /* Empty line. */
556 dptr = ct_decode_string(ptr, &el->el_scratch);
559 if (len > 0 && dptr[len - 1] == '\n')
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)
578 * Called from program when terminal is resized
581 el_resize(EditLine *el)
586 (void) sigemptyset(&nset);
587 (void) sigaddset(&nset, SIGWINCH);
588 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
590 /* get the correct window size */
591 if (terminal_get_size(el, &lins, &cols))
592 terminal_change_size(el, lins, cols);
594 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
599 * Called from the program to beep
602 el_beep(EditLine *el)
610 * Set the state of EDIT_DISABLED from the `edit' command.
614 el_editmode(EditLine *el, int argc, const Char **argv)
618 if (argv == NULL || argc != 2 || argv[1] == NULL)
622 if (Strcmp(how, STR("on")) == 0) {
623 el->el_flags &= ~EDIT_DISABLED;
625 } else if (Strcmp(how, STR("off")) == 0) {
627 el->el_flags |= EDIT_DISABLED;
630 (void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",