1 /* $NetBSD: el.c,v 1.92 2016/05/22 19:44:26 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.92 2016/05/22 19:44:26 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),
71 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
72 int fdin, int fdout, int fderr)
74 EditLine *el = el_malloc(sizeof(*el));
79 memset(el, 0, sizeof(EditLine));
82 el->el_outfile = fout;
83 el->el_errfile = ferr;
89 el->el_prog = wcsdup(ct_decode_string(prog, &el->el_scratch));
90 if (el->el_prog == NULL) {
96 * 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;
104 if (terminal_init(el) == -1) {
105 el_free(el->el_prog);
109 (void) keymacro_init(el);
111 if (tty_init(el) == -1)
112 el->el_flags |= NO_TTY;
114 (void) search_init(el);
115 (void) hist_init(el);
116 (void) prompt_init(el);
118 if (read_init(el) == -1) {
141 if (!(el->el_flags & NO_TTY))
144 read_end(el->el_read);
150 el_free(el->el_prog);
151 el_free(el->el_visual.cbuff);
152 el_free(el->el_visual.wbuff);
153 el_free(el->el_scratch.cbuff);
154 el_free(el->el_scratch.wbuff);
155 el_free(el->el_lgcyconv.cbuff);
156 el_free(el->el_lgcyconv.wbuff);
162 * Reset the tty and the parser
165 el_reset(EditLine *el)
169 ch_reset(el); /* XXX: Do we want that? */
174 * set the editline parameters
177 el_wset(EditLine *el, int op, ...)
189 el_pfunc_t p = va_arg(ap, el_pfunc_t);
191 rv = prompt_set(el, p, 0, op, 1);
196 el_zfunc_t p = va_arg(ap, el_zfunc_t);
197 void *arg = va_arg(ap, void *);
198 rv = ch_resizefun(el, p, arg);
202 case EL_ALIAS_TEXT: {
203 el_afunc_t p = va_arg(ap, el_afunc_t);
204 void *arg = va_arg(ap, void *);
205 rv = ch_aliasfun(el, p, arg);
210 case EL_RPROMPT_ESC: {
211 el_pfunc_t p = va_arg(ap, el_pfunc_t);
212 int c = va_arg(ap, int);
214 rv = prompt_set(el, p, (wchar_t)c, op, 1);
219 rv = terminal_set(el, va_arg(ap, char *));
223 rv = map_set_editor(el, va_arg(ap, wchar_t *));
228 el->el_flags |= HANDLE_SIGNALS;
230 el->el_flags &= ~HANDLE_SIGNALS;
239 const wchar_t *argv[20];
242 for (i = 1; i < (int)__arraycount(argv); i++)
243 if ((argv[i] = va_arg(ap, wchar_t *)) == NULL)
249 rv = map_bind(el, i, argv);
254 rv = terminal_telltc(el, i, argv);
259 rv = terminal_settc(el, i, argv);
264 rv = terminal_echotc(el, i, argv);
269 rv = tty_stty(el, i, argv);
274 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
282 wchar_t *name = va_arg(ap, wchar_t *);
283 wchar_t *help = va_arg(ap, wchar_t *);
284 el_func_t func = va_arg(ap, el_func_t);
286 rv = map_addfunc(el, name, help, func);
292 hist_fun_t func = va_arg(ap, hist_fun_t);
293 void *ptr = va_arg(ap, void *);
295 rv = hist_set(el, func, ptr);
296 if (!(el->el_flags & CHARSET_IS_UTF8))
297 el->el_flags &= ~NARROW_HISTORY;
303 el->el_flags &= ~EDIT_DISABLED;
305 el->el_flags |= EDIT_DISABLED;
311 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
312 rv = el_read_setfn(el->el_read, rc);
317 el->el_data = va_arg(ap, void *);
321 rv = va_arg(ap, int);
322 if (rv && !(el->el_flags & UNBUFFERED)) {
323 el->el_flags |= UNBUFFERED;
325 } else if (!rv && (el->el_flags & UNBUFFERED)) {
326 el->el_flags &= ~UNBUFFERED;
333 rv = va_arg(ap, int);
335 (void) tty_rawmode(el);
337 (void) tty_cookedmode(el);
346 what = va_arg(ap, int);
347 fp = va_arg(ap, FILE *);
353 el->el_infd = fileno(fp);
357 el->el_outfd = fileno(fp);
361 el->el_errfd = fileno(fp);
371 re_clear_display(el);
387 * retrieve the editline parameters
390 el_wget(EditLine *el, int op, ...)
403 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
404 rv = prompt_get(el, p, 0, op);
408 case EL_RPROMPT_ESC: {
409 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
410 wchar_t *c = va_arg(ap, wchar_t *);
412 rv = prompt_get(el, p, c, op);
417 rv = map_get_editor(el, va_arg(ap, const wchar_t **));
421 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
426 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
431 terminal_get(el, va_arg(ap, const char **));
437 static char name[] = "gettc";
441 for (i = 1; i < (int)__arraycount(argv); i++)
442 if ((argv[i] = va_arg(ap, char *)) == NULL)
446 rv = terminal_gettc(el, i, argv);
451 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el->el_read);
456 *va_arg(ap, void **) = el->el_data;
461 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
470 what = va_arg(ap, int);
471 fpp = va_arg(ap, FILE **);
475 *fpp = el->el_infile;
478 *fpp = el->el_outfile;
481 *fpp = el->el_errfile;
500 * Return editing info
503 el_wline(EditLine *el)
506 return (const LineInfoW *)(void *)&el->el_line;
514 el_source(EditLine *el, const char *fname)
526 #ifdef HAVE_ISSETUGID
527 static const char elpath[] = "/.editrc";
528 size_t plen = sizeof(elpath);
532 if ((ptr = getenv("HOME")) == NULL)
535 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
537 (void)snprintf(path, plen, "%s%s", ptr, elpath);
541 * If issetugid() is missing, always return an error, in order
542 * to keep from inadvertently opening up the user to a security
549 fp = fopen(fname, "r");
557 while ((slen = getline(&ptr, &len, fp)) != -1) {
559 continue; /* Empty line. */
560 if (slen > 0 && ptr[--slen] == '\n')
563 dptr = ct_decode_string(ptr, &el->el_scratch);
566 /* loop until first non-space char or EOL */
567 while (*dptr != '\0' && iswspace(*dptr))
570 continue; /* ignore, this is a comment line */
571 if ((error = parse_line(el, dptr)) == -1)
583 * Called from program when terminal is resized
586 el_resize(EditLine *el)
591 (void) sigemptyset(&nset);
592 (void) sigaddset(&nset, SIGWINCH);
593 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
595 /* get the correct window size */
596 if (terminal_get_size(el, &lins, &cols))
597 terminal_change_size(el, lins, cols);
599 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
604 * Called from the program to beep
607 el_beep(EditLine *el)
615 * Set the state of EDIT_DISABLED from the `edit' command.
619 el_editmode(EditLine *el, int argc, const wchar_t **argv)
623 if (argv == NULL || argc != 2 || argv[1] == NULL)
627 if (wcscmp(how, L"on") == 0) {
628 el->el_flags &= ~EDIT_DISABLED;
630 } else if (wcscmp(how, L"off") == 0) {
632 el->el_flags |= EDIT_DISABLED;
635 (void) fprintf(el->el_errfile, "edit: Bad value `%ls'.\n",