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.3 2004/07/27 11:22:34 asmodai Exp $
42 * el.c: EditLine interface functions
46 #include <sys/types.h>
47 #include <sys/param.h>
55 * Initialize editline and set default parameters.
58 el_init(prog, fin, fout)
62 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
70 memset(el, 0, sizeof(EditLine));
72 el->el_infd = fileno(fin);
73 el->el_outfile = fout;
74 el->el_prog = strdup(prog);
77 if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) {
78 el->el_errfile = fopen(tty, "w");
79 if (el->el_errfile == NULL) {
80 (void) fprintf(stderr, "Cannot open %s (%s).\n",
81 tty, strerror(errno));
87 el->el_errfile = stderr;
90 * Initialize all the modules. Order is important!!!
97 (void) search_init(el);
99 (void) prompt_init(el);
130 el_free((ptr_t) el->el_prog);
136 * Reset the tty and the parser
143 ch_reset(el); /* XXX: Do we want that? */
148 * set the editline parameters
151 el_set(EditLine *el, int op, ...)
159 rv = prompt_set(el, va_arg(va, el_pfunc_t));
163 rv = term_set(el, va_arg(va, char *));
167 rv = map_set_editor(el, va_arg(va, char *));
172 el->el_flags |= HANDLE_SIGNALS;
174 el->el_flags &= ~HANDLE_SIGNALS;
186 for (i = 1; i < 20; i++)
187 if ((argv[i] = va_arg(va, char *)) == NULL)
193 rv = map_bind(el, i, argv);
198 rv = term_telltc(el, i, argv);
203 rv = term_settc(el, i, argv);
208 rv = term_echotc(el, i, argv);
213 rv = tty_stty(el, i, argv);
226 char *name = va_arg(va, char *);
227 char *help = va_arg(va, char *);
228 el_func_t func = va_arg(va, el_func_t);
229 rv = map_addfunc(el, name, help, func);
235 hist_fun_t func = va_arg(va, hist_fun_t);
236 ptr_t ptr = va_arg(va, char *);
237 rv = hist_set(el, func, ptr);
251 * Return editing info
253 public const LineInfo *
257 return (const LineInfo *) &el->el_line;
260 static const char elpath[] = "/.editrc";
272 char *ptr, path[MAXPATHLEN];
275 if (issetugid() != 0 || (ptr = getenv("HOME")) == NULL)
277 (void) snprintf(path, sizeof(path), "%s%s", ptr, elpath);
281 if ((fp = fopen(fname, "r")) == NULL)
284 while ((ptr = fgetln(fp, &len)) != NULL) {
285 if (ptr[len - 1] == '\n')
289 if (parse_line(el, ptr) == -1) {
301 * Called from program when terminal is resized
309 (void) sigemptyset(&nset);
310 (void) sigaddset(&nset, SIGWINCH);
311 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
313 /* get the correct window size */
314 if (term_get_size(el, &lins, &cols))
315 term_change_size(el, lins, cols);
317 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
321 el_data_set (el, data)