2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $NetBSD: el.c,v 1.41 2005/08/19 04:21:47 christos Exp $
35 #if !defined(lint) && !defined(SCCSID)
36 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
37 #endif /* not lint && not SCCSID */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * el.c: EditLine interface functions
46 #include <sys/types.h>
47 #include <sys/param.h>
53 #define HAVE_ISSETUGID
56 * Initialize editline and set default parameters.
59 el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
62 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
67 memset(el, 0, sizeof(EditLine));
69 el->el_infd = fileno(fin);
70 el->el_outfile = fout;
71 el->el_errfile = ferr;
72 if ((el->el_prog = el_strdup(prog)) == NULL) {
78 * Initialize all the modules. Order is important!!!
82 if (term_init(el) == -1) {
89 if (tty_init(el) == -1)
90 el->el_flags |= NO_TTY;
92 (void) search_init(el);
94 (void) prompt_init(el);
124 el_free((ptr_t) el->el_prog);
130 * Reset the tty and the parser
133 el_reset(EditLine *el)
137 ch_reset(el, 0); /* XXX: Do we want that? */
142 * set the editline parameters
145 el_set(EditLine *el, int op, ...)
157 rv = prompt_set(el, va_arg(va, el_pfunc_t), op);
161 rv = term_set(el, va_arg(va, char *));
165 rv = map_set_editor(el, va_arg(va, char *));
170 el->el_flags |= HANDLE_SIGNALS;
172 el->el_flags &= ~HANDLE_SIGNALS;
181 const char *argv[20];
184 for (i = 1; i < 20; i++)
185 if ((argv[i] = va_arg(va, char *)) == NULL)
191 rv = map_bind(el, i, argv);
196 rv = term_telltc(el, i, argv);
201 rv = term_settc(el, i, argv);
206 rv = term_echotc(el, i, argv);
211 rv = tty_stty(el, i, argv);
216 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
224 char *name = va_arg(va, char *);
225 char *help = va_arg(va, char *);
226 el_func_t func = va_arg(va, el_func_t);
228 rv = map_addfunc(el, name, help, func);
234 hist_fun_t func = va_arg(va, hist_fun_t);
235 ptr_t ptr = va_arg(va, char *);
237 rv = hist_set(el, func, ptr);
243 el->el_flags &= ~EDIT_DISABLED;
245 el->el_flags |= EDIT_DISABLED;
251 el_rfunc_t rc = va_arg(va, el_rfunc_t);
252 rv = el_read_setfn(el, rc);
257 el->el_data = va_arg(va, void *);
261 rv = va_arg(va, int);
262 if (rv && !(el->el_flags & UNBUFFERED)) {
263 el->el_flags |= UNBUFFERED;
265 } else if (!rv && (el->el_flags & UNBUFFERED)) {
266 el->el_flags &= ~UNBUFFERED;
273 rv = va_arg(va, int);
275 (void) tty_rawmode(el);
277 (void) tty_cookedmode(el);
292 * retrieve the editline parameters
295 el_get(EditLine *el, int op, void *ret)
299 if (el == NULL || ret == NULL)
304 rv = prompt_get(el, (el_pfunc_t *) ret, op);
308 rv = map_get_editor(el, (const char **)ret);
312 *((int *) ret) = (el->el_flags & HANDLE_SIGNALS);
317 *((int *) ret) = (!(el->el_flags & EDIT_DISABLED));
322 term_get(el, (const char **)ret);
333 const char *argv[20];
336 for (i = 1; i < sizeof(argv) / sizeof(argv[0]); i++)
337 if ((argv[i] = va_arg(va, char *)) == NULL)
343 rv = map_bind(el, i, argv);
348 rv = term_telltc(el, i, argv);
353 rv = term_settc(el, i, argv);
358 rv = term_echotc(el, i, argv);
363 rv = tty_stty(el, i, argv);
368 EL_ABORT((el->errfile, "Bad op %d\n", op));
376 char *name = va_arg(va, char *);
377 char *help = va_arg(va, char *);
378 el_func_t func = va_arg(va, el_func_t);
380 rv = map_addfunc(el, name, help, func);
386 hist_fun_t func = va_arg(va, hist_fun_t);
387 ptr_t ptr = va_arg(va, char *);
388 rv = hist_set(el, func, ptr);
394 *((el_rfunc_t *)ret) = el_read_getfn(el);
399 *((void **)ret) = el->el_data;
404 *((int *) ret) = (!(el->el_flags & UNBUFFERED));
416 * Set user private data.
419 el_data_set (el, data)
429 * Return user private data.
436 return (el->el_data);
441 * Return editing info
443 public const LineInfo *
444 el_line(EditLine *el)
447 return (const LineInfo *) (void *) &el->el_line;
455 el_source(EditLine *el, const char *fname)
463 #ifdef HAVE_ISSETUGID
464 static const char elpath[] = "/.editrc";
465 char path[MAXPATHLEN];
469 if ((ptr = getenv("HOME")) == NULL)
471 if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path))
473 if (strlcat(path, elpath, sizeof(path)) >= sizeof(path))
478 * If issetugid() is missing, always return an error, in order
479 * to keep from inadvertently opening up the user to a security
486 fp = fopen(fname, "r");
490 while ((ptr = fgetln(fp, &len)) != NULL) {
491 if (len > 0 && ptr[len - 1] == '\n')
494 if (parse_line(el, ptr) == -1) {
506 * Called from program when terminal is resized
509 el_resize(EditLine *el)
514 (void) sigemptyset(&nset);
515 (void) sigaddset(&nset, SIGWINCH);
516 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
518 /* get the correct window size */
519 if (term_get_size(el, &lins, &cols))
520 term_change_size(el, lins, cols);
522 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
527 * Called from the program to beep
530 el_beep(EditLine *el)
538 * Set the state of EDIT_DISABLED from the `edit' command.
542 el_editmode(EditLine *el, int argc, const char **argv)
546 if (argv == NULL || argc != 2 || argv[1] == NULL)
550 if (strcmp(how, "on") == 0) {
551 el->el_flags &= ~EDIT_DISABLED;
553 } else if (strcmp(how, "off") == 0) {
555 el->el_flags |= EDIT_DISABLED;
558 (void) fprintf(el->el_errfile, "edit: Bad value `%s'.\n", how);