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
37 #if !defined(lint) && !defined(SCCSID)
39 static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
41 static const char rcsid[] =
42 "$FreeBSD: src/lib/libedit/el.c,v 1.6.2.1 2000/05/22 06:07:00 imp Exp $";
43 #endif /* not lint && not SCCSID */
46 * el.c: EditLine interface functions
50 #include <sys/types.h>
51 #include <sys/param.h>
63 * Initialize editline and set default parameters.
66 el_init(prog, fin, fout)
70 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
78 memset(el, 0, sizeof(EditLine));
80 el->el_infd = fileno(fin);
81 el->el_outfile = fout;
82 el->el_prog = strdup(prog);
85 if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) {
86 el->el_errfile = fopen(tty, "w");
87 if (el->el_errfile == NULL) {
88 (void) fprintf(stderr, "Cannot open %s (%s).\n",
89 tty, strerror(errno));
95 el->el_errfile = stderr;
98 * Initialize all the modules. Order is important!!!
100 (void) term_init(el);
105 (void) search_init(el);
106 (void) hist_init(el);
107 (void) prompt_init(el);
138 el_free((ptr_t) el->el_prog);
144 * Reset the tty and the parser
151 ch_reset(el); /* XXX: Do we want that? */
156 * set the editline parameters
160 el_set(EditLine *el, int op, ...)
175 el = va_arg(va, EditLine *);
176 op = va_arg(va, int);
181 rv = prompt_set(el, va_arg(va, el_pfunc_t));
185 rv = term_set(el, va_arg(va, char *));
189 rv = map_set_editor(el, va_arg(va, char *));
194 el->el_flags |= HANDLE_SIGNALS;
196 el->el_flags &= ~HANDLE_SIGNALS;
208 for (i = 1; i < 20; i++)
209 if ((argv[i] = va_arg(va, char *)) == NULL)
215 rv = map_bind(el, i, argv);
220 rv = term_telltc(el, i, argv);
225 rv = term_settc(el, i, argv);
230 rv = term_echotc(el, i, argv);
235 rv = tty_stty(el, i, argv);
248 char *name = va_arg(va, char *);
249 char *help = va_arg(va, char *);
250 el_func_t func = va_arg(va, el_func_t);
251 rv = map_addfunc(el, name, help, func);
257 hist_fun_t func = va_arg(va, hist_fun_t);
258 ptr_t ptr = va_arg(va, char *);
259 rv = hist_set(el, func, ptr);
273 * Return editing info
275 public const LineInfo *
279 return (const LineInfo *) &el->el_line;
282 static const char elpath[] = "/.editrc";
294 char *ptr, path[MAXPATHLEN];
297 if (issetugid() != 0 || (ptr = getenv("HOME")) == NULL)
299 (void) snprintf(path, sizeof(path), "%s%s", ptr, elpath);
303 if ((fp = fopen(fname, "r")) == NULL)
306 while ((ptr = fgetln(fp, &len)) != NULL) {
307 if (ptr[len - 1] == '\n')
311 if (parse_line(el, ptr) == -1) {
323 * Called from program when terminal is resized
331 (void) sigemptyset(&nset);
332 (void) sigaddset(&nset, SIGWINCH);
333 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
335 /* get the correct window size */
336 if (term_get_size(el, &lins, &cols))
337 term_change_size(el, lins, cols);
339 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
343 el_data_set (el, data)