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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)el.c 8.2 (Berkeley) 1/3/94
37 * $FreeBSD: src/lib/libedit/el.c,v 1.6.2.1 2000/05/22 06:07:00 imp Exp $
38 * $DragonFly: src/lib/libedit/el.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
42 * el.c: EditLine interface functions
46 #include <sys/types.h>
47 #include <sys/param.h>
59 * Initialize editline and set default parameters.
62 el_init(prog, fin, fout)
66 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
74 memset(el, 0, sizeof(EditLine));
76 el->el_infd = fileno(fin);
77 el->el_outfile = fout;
78 el->el_prog = strdup(prog);
81 if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) {
82 el->el_errfile = fopen(tty, "w");
83 if (el->el_errfile == NULL) {
84 (void) fprintf(stderr, "Cannot open %s (%s).\n",
85 tty, strerror(errno));
91 el->el_errfile = stderr;
94 * Initialize all the modules. Order is important!!!
101 (void) search_init(el);
102 (void) hist_init(el);
103 (void) prompt_init(el);
134 el_free((ptr_t) el->el_prog);
140 * Reset the tty and the parser
147 ch_reset(el); /* XXX: Do we want that? */
152 * set the editline parameters
156 el_set(EditLine *el, int op, ...)
171 el = va_arg(va, EditLine *);
172 op = va_arg(va, int);
177 rv = prompt_set(el, va_arg(va, el_pfunc_t));
181 rv = term_set(el, va_arg(va, char *));
185 rv = map_set_editor(el, va_arg(va, char *));
190 el->el_flags |= HANDLE_SIGNALS;
192 el->el_flags &= ~HANDLE_SIGNALS;
204 for (i = 1; i < 20; i++)
205 if ((argv[i] = va_arg(va, char *)) == NULL)
211 rv = map_bind(el, i, argv);
216 rv = term_telltc(el, i, argv);
221 rv = term_settc(el, i, argv);
226 rv = term_echotc(el, i, argv);
231 rv = tty_stty(el, i, argv);
244 char *name = va_arg(va, char *);
245 char *help = va_arg(va, char *);
246 el_func_t func = va_arg(va, el_func_t);
247 rv = map_addfunc(el, name, help, func);
253 hist_fun_t func = va_arg(va, hist_fun_t);
254 ptr_t ptr = va_arg(va, char *);
255 rv = hist_set(el, func, ptr);
269 * Return editing info
271 public const LineInfo *
275 return (const LineInfo *) &el->el_line;
278 static const char elpath[] = "/.editrc";
290 char *ptr, path[MAXPATHLEN];
293 if (issetugid() != 0 || (ptr = getenv("HOME")) == NULL)
295 (void) snprintf(path, sizeof(path), "%s%s", ptr, elpath);
299 if ((fp = fopen(fname, "r")) == NULL)
302 while ((ptr = fgetln(fp, &len)) != NULL) {
303 if (ptr[len - 1] == '\n')
307 if (parse_line(el, ptr) == -1) {
319 * Called from program when terminal is resized
327 (void) sigemptyset(&nset);
328 (void) sigaddset(&nset, SIGWINCH);
329 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
331 /* get the correct window size */
332 if (term_get_size(el, &lins, &cols))
333 term_change_size(el, lins, cols);
335 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
339 el_data_set (el, data)