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 #define MAXPATHLEN 4096
49 * el.c: EditLine interface functions
51 #include <sys/types.h>
52 #include <sys/param.h>
61 #ifndef HAVE_SECURE_GETENV
62 # ifdef HAVE___SECURE_GETENV
63 # define secure_getenv __secure_getenv
64 # define HAVE_SECURE_GETENV 1
66 # ifdef HAVE_ISSETUGID
70 # define issetugid() 1
75 #ifndef HAVE_SECURE_GETENV
76 char *secure_getenv(char const *name)
85 * Initialize editline and set default parameters.
88 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
90 return el_init_fd(prog, fin, fout, ferr, fileno(fin), fileno(fout),
95 el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
96 int fdin, int fdout, int fderr)
98 EditLine *el = el_malloc(sizeof(*el));
103 memset(el, 0, sizeof(EditLine));
106 el->el_outfile = fout;
107 el->el_errfile = ferr;
110 el->el_outfd = fdout;
111 el->el_errfd = fderr;
113 el->el_prog = Strdup(ct_decode_string(prog, &el->el_scratch));
114 if (el->el_prog == NULL) {
120 * Initialize all the modules. Order is important!!!
124 if (setlocale(LC_CTYPE, NULL) != NULL){
125 if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)
126 el->el_flags |= CHARSET_IS_UTF8;
130 if (terminal_init(el) == -1) {
131 el_free(el->el_prog);
135 (void) keymacro_init(el);
137 if (tty_init(el) == -1)
138 el->el_flags |= NO_TTY;
140 (void) search_init(el);
141 (void) hist_init(el);
142 (void) prompt_init(el);
144 (void) read_init(el);
172 el_free(el->el_prog);
174 el_free(el->el_scratch.cbuff);
175 el_free(el->el_scratch.wbuff);
176 el_free(el->el_lgcyconv.cbuff);
177 el_free(el->el_lgcyconv.wbuff);
184 * Reset the tty and the parser
187 el_reset(EditLine *el)
191 ch_reset(el, 0); /* XXX: Do we want that? */
196 * set the editline parameters
199 FUN(el,set)(EditLine *el, int op, ...)
211 el_pfunc_t p = va_arg(ap, el_pfunc_t);
213 rv = prompt_set(el, p, 0, op, 1);
218 el_zfunc_t p = va_arg(ap, el_zfunc_t);
219 void *arg = va_arg(ap, void *);
220 rv = ch_resizefun(el, p, arg);
224 case EL_ALIAS_TEXT: {
225 el_afunc_t p = va_arg(ap, el_afunc_t);
226 void *arg = va_arg(ap, void *);
227 rv = ch_aliasfun(el, p, arg);
232 case EL_RPROMPT_ESC: {
233 el_pfunc_t p = va_arg(ap, el_pfunc_t);
234 int c = va_arg(ap, int);
236 rv = prompt_set(el, p, c, op, 1);
241 rv = terminal_set(el, va_arg(ap, char *));
245 rv = map_set_editor(el, va_arg(ap, Char *));
250 el->el_flags |= HANDLE_SIGNALS;
252 el->el_flags &= ~HANDLE_SIGNALS;
261 const Char *argv[20];
264 for (i = 1; i < (int)__arraycount(argv); i++)
265 if ((argv[i] = va_arg(ap, Char *)) == NULL)
270 argv[0] = STR("bind");
271 rv = map_bind(el, i, argv);
275 argv[0] = STR("telltc");
276 rv = terminal_telltc(el, i, argv);
280 argv[0] = STR("settc");
281 rv = terminal_settc(el, i, argv);
285 argv[0] = STR("echotc");
286 rv = terminal_echotc(el, i, argv);
290 argv[0] = STR("setty");
291 rv = tty_stty(el, i, argv);
296 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
304 Char *name = va_arg(ap, Char *);
305 Char *help = va_arg(ap, Char *);
306 el_func_t func = va_arg(ap, el_func_t);
308 rv = map_addfunc(el, name, help, func);
314 hist_fun_t func = va_arg(ap, hist_fun_t);
315 void *ptr = va_arg(ap, void *);
317 rv = hist_set(el, func, ptr);
318 if (!(el->el_flags & CHARSET_IS_UTF8))
319 el->el_flags &= ~NARROW_HISTORY;
325 el->el_flags &= ~EDIT_DISABLED;
327 el->el_flags |= EDIT_DISABLED;
333 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
334 rv = el_read_setfn(el, rc);
335 el->el_flags &= ~NARROW_READ;
340 el->el_data = va_arg(ap, void *);
344 rv = va_arg(ap, int);
345 if (rv && !(el->el_flags & UNBUFFERED)) {
346 el->el_flags |= UNBUFFERED;
348 } else if (!rv && (el->el_flags & UNBUFFERED)) {
349 el->el_flags &= ~UNBUFFERED;
356 rv = va_arg(ap, int);
358 (void) tty_rawmode(el);
360 (void) tty_cookedmode(el);
369 what = va_arg(ap, int);
370 fp = va_arg(ap, FILE *);
376 el->el_infd = fileno(fp);
380 el->el_outfd = fileno(fp);
384 el->el_errfd = fileno(fp);
394 re_clear_display(el);
410 * retrieve the editline parameters
413 FUN(el,get)(EditLine *el, int op, ...)
426 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
427 rv = prompt_get(el, p, 0, op);
431 case EL_RPROMPT_ESC: {
432 el_pfunc_t *p = va_arg(ap, el_pfunc_t *);
433 Char *c = va_arg(ap, Char *);
435 rv = prompt_get(el, p, c, op);
440 rv = map_get_editor(el, va_arg(ap, const Char **));
444 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
449 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
454 terminal_get(el, va_arg(ap, const char **));
460 static char name[] = "gettc";
464 for (i = 1; i < (int)__arraycount(argv); i++)
465 if ((argv[i] = va_arg(ap, char *)) == NULL)
469 rv = terminal_gettc(el, i, argv);
474 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
479 *va_arg(ap, void **) = el->el_data;
484 *va_arg(ap, int *) = (el->el_flags & UNBUFFERED) != 0;
493 what = va_arg(ap, int);
494 fpp = va_arg(ap, FILE **);
498 *fpp = el->el_infile;
501 *fpp = el->el_outfile;
504 *fpp = el->el_errfile;
523 * Return editing info
525 public const TYPE(LineInfo) *
526 FUN(el,line)(EditLine *el)
529 return (const TYPE(LineInfo) *)(void *)&el->el_line;
537 el_source(EditLine *el, const char *fname)
548 static const char elpath[] = "/.editrc";
549 size_t plen = sizeof(elpath);
551 if ((ptr = secure_getenv("HOME")) == NULL)
554 if ((path = el_malloc(plen * sizeof(*path))) == NULL)
556 (void)snprintf(path, plen, "%s%s", ptr, elpath);
560 fp = fopen(fname, "r");
566 while ((ptr = fgetln(fp, &len)) != NULL) {
568 continue; /* Empty line. */
569 dptr = ct_decode_string(ptr, &el->el_scratch);
572 if (len > 0 && dptr[len - 1] == '\n')
575 /* loop until first non-space char or EOL */
576 while (*dptr != '\0' && Isspace(*dptr))
579 continue; /* ignore, this is a comment line */
580 if ((error = parse_line(el, dptr)) == -1)
591 * Called from program when terminal is resized
594 el_resize(EditLine *el)
599 (void) sigemptyset(&nset);
600 (void) sigaddset(&nset, SIGWINCH);
601 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
603 /* get the correct window size */
604 if (terminal_get_size(el, &lins, &cols))
605 terminal_change_size(el, lins, cols);
607 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
612 * Called from the program to beep
615 el_beep(EditLine *el)
623 * Set the state of EDIT_DISABLED from the `edit' command.
627 el_editmode(EditLine *el, int argc, const Char **argv)
631 if (argv == NULL || argc != 2 || argv[1] == NULL)
635 if (Strcmp(how, STR("on")) == 0) {
636 el->el_flags &= ~EDIT_DISABLED;
638 } else if (Strcmp(how, STR("off")) == 0) {
640 el->el_flags |= EDIT_DISABLED;
643 (void) fprintf(el->el_errfile, "edit: Bad value `" FSTR "'.\n",