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.4 2005/08/04 17:27:09 drhodus 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(const char *prog, FILE *fin, FILE *fout)
60 EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
68 memset(el, 0, sizeof(EditLine));
70 el->el_infd = fileno(fin);
71 el->el_outfile = fout;
72 el->el_prog = strdup(prog);
75 if (issetugid() == 0 && (tty = getenv("DEBUGTTY")) != NULL) {
76 el->el_errfile = fopen(tty, "w");
77 if (el->el_errfile == NULL) {
78 (void) fprintf(stderr, "Cannot open %s (%s).\n",
79 tty, strerror(errno));
85 el->el_errfile = stderr;
88 * Initialize all the modules. Order is important!!!
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)
139 ch_reset(el); /* XXX: Do we want that? */
144 * set the editline parameters
147 el_set(EditLine *el, int op, ...)
155 rv = prompt_set(el, va_arg(va, el_pfunc_t));
159 rv = term_set(el, va_arg(va, char *));
163 rv = map_set_editor(el, va_arg(va, char *));
168 el->el_flags |= HANDLE_SIGNALS;
170 el->el_flags &= ~HANDLE_SIGNALS;
182 for (i = 1; i < 20; i++)
183 if ((argv[i] = va_arg(va, char *)) == NULL)
189 rv = map_bind(el, i, argv);
194 rv = term_telltc(el, i, argv);
199 rv = term_settc(el, i, argv);
204 rv = term_echotc(el, i, argv);
209 rv = tty_stty(el, i, argv);
222 char *name = va_arg(va, char *);
223 char *help = va_arg(va, char *);
224 el_func_t func = va_arg(va, el_func_t);
225 rv = map_addfunc(el, name, help, func);
231 hist_fun_t func = va_arg(va, hist_fun_t);
232 ptr_t ptr = va_arg(va, char *);
233 rv = hist_set(el, func, ptr);
247 * Return editing info
249 public const LineInfo *
250 el_line(EditLine *el)
252 return (const LineInfo *) &el->el_line;
255 static const char elpath[] = "/.editrc";
261 el_source(EditLine *el, const char *fname)
265 char *ptr, path[MAXPATHLEN];
268 if (issetugid() != 0 || (ptr = getenv("HOME")) == NULL)
270 (void) snprintf(path, sizeof(path), "%s%s", ptr, elpath);
274 if ((fp = fopen(fname, "r")) == NULL)
277 while ((ptr = fgetln(fp, &len)) != NULL) {
278 if (ptr[len - 1] == '\n')
282 if (parse_line(el, ptr) == -1) {
294 * Called from program when terminal is resized
297 el_resize(EditLine *el)
301 (void) sigemptyset(&nset);
302 (void) sigaddset(&nset, SIGWINCH);
303 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
305 /* get the correct window size */
306 if (term_get_size(el, &lins, &cols))
307 term_change_size(el, lins, cols);
309 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
313 el_data_set(EditLine *el, void *data)
321 el_data_get(EditLine *el)