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.44 2006/12/15 22:13:33 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));
70 el->el_outfile = fout;
71 el->el_errfile = ferr;
73 el->el_infd = fileno(fin);
75 if ((el->el_prog = el_strdup(prog)) == NULL) {
81 * Initialize all the modules. Order is important!!!
85 if (term_init(el) == -1) {
92 if (tty_init(el) == -1)
93 el->el_flags |= NO_TTY;
95 (void) search_init(el);
97 (void) prompt_init(el);
127 el_free((ptr_t) el->el_prog);
133 * Reset the tty and the parser
136 el_reset(EditLine *el)
140 ch_reset(el, 0); /* XXX: Do we want that? */
145 * set the editline parameters
148 el_set(EditLine *el, int op, ...)
160 rv = prompt_set(el, va_arg(ap, el_pfunc_t), op);
164 rv = term_set(el, va_arg(ap, char *));
168 rv = map_set_editor(el, va_arg(ap, char *));
173 el->el_flags |= HANDLE_SIGNALS;
175 el->el_flags &= ~HANDLE_SIGNALS;
185 const char *argv[20];
188 for (i = 1; i < 20; i++)
189 if ((argv[i] = va_arg(ap, char *)) == NULL)
195 rv = map_bind(el, i, argv);
200 rv = term_telltc(el, i, argv);
205 rv = term_settc(el, i, argv);
210 rv = term_echotc(el, i, argv);
215 rv = tty_stty(el, i, argv);
220 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
228 char *name = va_arg(ap, char *);
229 char *help = va_arg(ap, char *);
230 el_func_t func = va_arg(ap, el_func_t);
232 rv = map_addfunc(el, name, help, func);
238 hist_fun_t func = va_arg(ap, hist_fun_t);
239 ptr_t ptr = va_arg(ap, char *);
241 rv = hist_set(el, func, ptr);
247 el->el_flags &= ~EDIT_DISABLED;
249 el->el_flags |= EDIT_DISABLED;
255 el_rfunc_t rc = va_arg(ap, el_rfunc_t);
256 rv = el_read_setfn(el, rc);
261 el->el_data = va_arg(ap, void *);
265 rv = va_arg(ap, int);
266 if (rv && !(el->el_flags & UNBUFFERED)) {
267 el->el_flags |= UNBUFFERED;
269 } else if (!rv && (el->el_flags & UNBUFFERED)) {
270 el->el_flags &= ~UNBUFFERED;
277 rv = va_arg(ap, int);
279 (void) tty_rawmode(el);
281 (void) tty_cookedmode(el);
290 what = va_arg(ap, int);
291 fp = va_arg(ap, FILE *);
297 el->el_infd = fileno(fp);
323 * retrieve the editline parameters
326 el_get(EditLine *el, int op, ...)
339 rv = prompt_get(el, va_arg(ap, el_pfunc_t *), op);
343 rv = map_get_editor(el, va_arg(ap, const char **));
347 *va_arg(ap, int *) = (el->el_flags & HANDLE_SIGNALS);
352 *va_arg(ap, int *) = !(el->el_flags & EDIT_DISABLED);
357 term_get(el, va_arg(ap, const char **));
363 static char name[] = "gettc";
367 for (i = 1; i < sizeof(argv) / sizeof(argv[0]); i++)
368 if ((argv[i] = va_arg(ap, char *)) == NULL)
374 rv = term_gettc(el, i, argv);
379 EL_ABORT((el->el_errfile, "Bad op %d\n", op));
388 char *name = va_arg(ap, char *);
389 char *help = va_arg(ap, char *);
390 el_func_t func = va_arg(ap, el_func_t);
392 rv = map_addfunc(el, name, help, func);
398 hist_fun_t func = va_arg(ap, hist_fun_t);
399 ptr_t ptr = va_arg(ap, char *);
400 rv = hist_set(el, func, ptr);
406 *va_arg(ap, el_rfunc_t *) = el_read_getfn(el);
411 *va_arg(ap, void **) = el->el_data;
416 *va_arg(ap, int *) = (!(el->el_flags & UNBUFFERED));
425 what = va_arg(ap, int);
426 fpp = va_arg(ap, FILE **);
430 *fpp = el->el_infile;
433 *fpp = el->el_outfile;
436 *fpp = el->el_errfile;
454 * Set user private data.
457 el_data_set (el, data)
467 * Return user private data.
474 return (el->el_data);
479 * Return editing info
481 public const LineInfo *
482 el_line(EditLine *el)
485 return (const LineInfo *) (void *) &el->el_line;
493 el_source(EditLine *el, const char *fname)
501 #ifdef HAVE_ISSETUGID
502 static const char elpath[] = "/.editrc";
503 char path[MAXPATHLEN];
507 if ((ptr = getenv("HOME")) == NULL)
509 if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path))
511 if (strlcat(path, elpath, sizeof(path)) >= sizeof(path))
516 * If issetugid() is missing, always return an error, in order
517 * to keep from inadvertently opening up the user to a security
524 fp = fopen(fname, "r");
528 while ((ptr = fgetln(fp, &len)) != NULL) {
529 if (len > 0 && ptr[len - 1] == '\n')
532 if (parse_line(el, ptr) == -1) {
544 * Called from program when terminal is resized
547 el_resize(EditLine *el)
552 (void) sigemptyset(&nset);
553 (void) sigaddset(&nset, SIGWINCH);
554 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
556 /* get the correct window size */
557 if (term_get_size(el, &lins, &cols))
558 term_change_size(el, lins, cols);
560 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
565 * Called from the program to beep
568 el_beep(EditLine *el)
576 * Set the state of EDIT_DISABLED from the `edit' command.
580 el_editmode(EditLine *el, int argc, const char **argv)
584 if (argv == NULL || argc != 2 || argv[1] == NULL)
588 if (strcmp(how, "on") == 0) {
589 el->el_flags &= ~EDIT_DISABLED;
591 } else if (strcmp(how, "off") == 0) {
593 el->el_flags |= EDIT_DISABLED;
596 (void) fprintf(el->el_errfile, "edit: Bad value `%s'.\n", how);