1 /* $NetBSD: el.c,v 1.73 2014/06/18 18:12:28 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.73 2014/06/18 18:12:28 christos Exp $");
42 #endif /* not lint && not SCCSID */
45 * el.c: EditLine interface functions
47 #include <sys/types.h>
48 #include <sys/param.h>
57 #ifndef HAVE_SECURE_GETENV
58 # ifdef HAVE___SECURE_GETENV
59 # define secure_getenv __secure_getenv
60 # define HAVE_SECURE_GETENV 1
62 # ifdef HAVE_ISSETUGID
66 # define issetugid() 1
71 #ifndef HAVE_SECURE_GETENV
72 char *secure_getenv(char const *name)
81 * Initialize editline and set default parameters.
84 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
86 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
91 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
92 int fdin, int fdout, int fderr)
94 EditLine *el = el_malloc(sizeof(*el));
99 memset(el, 0, sizeof(EditLine));
102 el->el_outfile = fout;
103 el->el_errfile = ferr;
106 el->el_outfd = fdout;
107 el->el_errfd = fderr;
109 el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
110 if (el->el_prog == NULL) {
116 * Initialize all the modules. Order is important!!!
120 if (setlocale(LC_CTYPE, NULL) != NULL){
121 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
122 el->el_flags |= CHARSET_IS_UTF8;
126 if (terminal_init(el) == -1) {
127 el_free(el->el_prog);
131 (void) keymacro_init(el);
133 if (tty_init(el) == -1)
134 el->el_flags |= NO_TTY;
136 (void) search_init(el);
137 (void) hist_init(el);
138 (void) prompt_init(el);
140 (void) read_init(el);
168 el_free(el->el_prog);
170 el_free(el->el_scratch.cbuff);
171 el_free(el->el_scratch.wbuff);
172 el_free(el->el_lgcyconv.cbuff);
173 el_free(el->el_lgcyconv.wbuff);
180 * Reset the tty and the parser
183 el_reset(EditLine *el)
187 ch_reset(el, 0); /* XXX: Do we want that? */
192 * set the editline parameters
195 FUN(el,set)(EditLine *el, int op, ...)
207 el_pfunc_t p = va_arg(ap, el_pfunc_t);
209 rv = prompt_set(el, p, 0, op, 1);
214 el_zfunc_t p = va_arg(ap, el_zfunc_t);
215 void *arg = va_arg(ap, void *);
216 rv = ch_resizefun(el, p, arg);
220 case EL_ALIAS_TEXT: {
221 el_afunc_t p = va_arg(ap, el_afunc_t);
222 void *arg = va_arg(ap, void *);
223 rv = ch_aliasfun(el, p, arg);
228 case EL_RPROMPT_ESC: {
229 el_pfunc_t p = va_arg(ap, el_pfunc_t);
230 int c = va_arg(ap, int);
232 rv = prompt_set(el, p, c, op, 1);
237 rv = terminal_set(el, va_arg(ap, char *));
241 rv = map_set_editor(el, va_arg(ap, Char *));
246 el->el_flags |= HANDLE_SIGNALS;
248 el->el_flags &= ~HANDLE_SIGNALS;
257 const Char *argv[20];
260 for (i = 1; i < (int)__arraycount(argv); i++)
261 if ((argv[i] = va_arg(ap, Char *)) == NULL)
266 argv[0] = STR("bind");
267 rv = map_bind(el, i, argv);
271 argv[0] = STR("telltc");
272 rv = terminal_telltc(el, i, argv);
276 argv[0] = STR("settc");
277 rv = terminal_settc(el, i, argv);
281 argv[0] = STR("echotc");
282 rv = terminal_echotc(el, i, argv);
286 argv[0] = STR("setty");
287 rv = tty_stty(el, i, argv);
292 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
300 Char *name = va_arg(ap, Char *);
301 Char *help = va_arg(ap, Char *);
302 el_func_t func = va_arg(ap, el_func_t);
304 rv = map_addfunc(el, name, help, func);
310 hist_fun_t func = va_arg(ap, hist_fun_t);
311 void *ptr = va_arg(ap, void *);
313 rv = hist_set(el, func, ptr);
314 if (!(el->el_flags & CHARSET_IS_UTF8))
315 el->el_flags &= ~NARROW_HISTORY;
321 el->el_flags &= ~EDIT_DISABLED;
323 el->el_flags |= EDIT_DISABLED;
329 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
330 rv = el_read_setfn(el, rc);
331 el->el_flags &= ~NARROW_READ;
336 el->el_data = va_arg(ap, void *);
340 rv = va_arg(ap, int);
341 if (rv && !(el->el_flags & UNBUFFERED)) {
342 el->el_flags |= UNBUFFERED;
344 } else if (!rv && (el->el_flags & UNBUFFERED)) {
345 el->el_flags &= ~UNBUFFERED;
352 rv = va_arg(ap, int);
354 (void) tty_rawmode(el);
356 (void) tty_cookedmode(el);
365 what = va_arg(ap, int);
366 fp = va_arg(ap, FILE *);
372 el->el_infd = fileno(fp);
376 el->el_outfd = fileno(fp);
380 el->el_errfd = fileno(fp);
390 re_clear_display(el);
406 * retrieve the editline parameters
409 FUN(el,get)(EditLine *el, int op, ...)
422 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
423 rv = prompt_get(el, p, 0, op);
427 case EL_RPROMPT_ESC: {
428 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
429 Char *c = va_arg(ap, Char *);
431 rv = prompt_get(el, p, c, op);
436 rv = map_get_editor(el, va_arg(ap, const Char **));
440 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
445 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
450 terminal_get(el, va_arg(ap, const char **));
456 static char name[] = "gettc";
460 for (i = 1; i < (int)__arraycount(argv); i++)
461 if ((argv[i] = va_arg(ap, char *)) == NULL)
465 rv = terminal_gettc(el, i, argv);
470 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
475 *va_arg(ap, void **) = el->el_data;
480 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
489 what = va_arg(ap, int);
490 fpp = va_arg(ap, FILE **);
494 *fpp = el->el_infile;
497 *fpp = el->el_outfile;
500 *fpp = el->el_errfile;
519 * Return editing info
521 public const TYPE(LineInfo) *
522 FUN(el,line)(EditLine *el)
525 return (const TYPE(LineInfo) *)(void *)&el->el_line;
533 el_source(EditLine *el, const char *fname)
544 static const char elpath[] = "/.editrc";
545 size_t plen = sizeof(elpath);
547 if ((ptr = secure_getenv("HOME")) == NULL)
550 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
552 (void)snprintf(path, plen, "%s%s", ptr, elpath);
556 fp = fopen(fname, "r");
562 while ((ptr = fgetln(fp, &len)) != NULL) {
564 continue; /* Empty line. */
565 dptr = ct_decode_string(ptr, &el->el_scratch);
568 if (len > 0 && dptr[len - 1] == '\n')
571 /* loop until first non-space char or EOL */
572 while (*dptr != '\0' && Isspace(*dptr))
575 continue; /* ignore, this is a comment line */
576 if ((error = parse_line(el, dptr)) == -1)
587 * Called from program when terminal is resized
590 el_resize(EditLine *el)
595 (void) sigemptyset(&nset);
596 (void) sigaddset(&nset, SIGWINCH);
597 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
599 /* get the correct window size */
600 if (terminal_get_size(el, &lins, &cols))
601 terminal_change_size(el, lins, cols);
603 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
608 * Called from the program to beep
611 el_beep(EditLine *el)
619 * Set the state of EDIT_DISABLED from the `edit' command.
623 el_editmode(EditLine *el, int argc, const Char **argv)
627 if (argv == NULL || argc != 2 || argv[1] == NULL)
631 if (Strcmp(how, STR("on")) == 0) {
632 el->el_flags &= ~EDIT_DISABLED;
634 } else if (Strcmp(how, STR("off")) == 0) {
636 el->el_flags |= EDIT_DISABLED;
639 (void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",