1
/* $NetBSD: el.c,v 1.41 2005/08/19 04:21:47 christos Exp $ */
4
* Copyright (c) 1992, 1993
5
* The Regents of the University of California. All rights reserved.
7
* This code is derived from software contributed to Berkeley by
8
* Christos Zoulas of Cornell University.
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. Neither the name of the University nor the names of its contributors
19
* may be used to endorse or promote products derived from this software
20
* without specific prior written permission.
22
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36
#if !defined(lint) && !defined(SCCSID)
38
static char sccsid[] = "@(#)el.c 8.2 (Berkeley) 1/3/94";
40
__RCSID("$NetBSD: el.c,v 1.41 2005/08/19 04:21:47 christos Exp $");
42
#endif /* not lint && not SCCSID */
45
* el.c: EditLine interface functions
47
#include <sys/types.h>
48
#include <sys/param.h>
55
* Initialize editline and set default parameters.
58
el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr)
61
EditLine *el = (EditLine *) el_malloc(sizeof(EditLine));
66
memset(el, 0, sizeof(EditLine));
68
el->el_infd = fileno(fin);
69
el->el_outfile = fout;
70
el->el_errfile = ferr;
71
if ((el->el_prog = el_strdup(prog)) == NULL) {
77
* Initialize all the modules. Order is important!!!
81
if (term_init(el) == -1) {
88
if (tty_init(el) == -1)
89
el->el_flags |= NO_TTY;
91
(void) search_init(el);
93
(void) prompt_init(el);
123
el_free((ptr_t) el->el_prog);
129
* Reset the tty and the parser
132
el_reset(EditLine *el)
136
ch_reset(el, 0); /* XXX: Do we want that? */
141
* set the editline parameters
144
el_set(EditLine *el, int op, ...)
156
rv = prompt_set(el, va_arg(va, el_pfunc_t), op);
160
rv = term_set(el, va_arg(va, char *));
164
rv = map_set_editor(el, va_arg(va, char *));
169
el->el_flags |= HANDLE_SIGNALS;
171
el->el_flags &= ~HANDLE_SIGNALS;
180
const char *argv[20];
183
for (i = 1; i < 20; i++)
184
if ((argv[i] = va_arg(va, char *)) == NULL)
190
rv = map_bind(el, i, argv);
195
rv = term_telltc(el, i, argv);
200
rv = term_settc(el, i, argv);
205
rv = term_echotc(el, i, argv);
210
rv = tty_stty(el, i, argv);
215
EL_ABORT((el->el_errfile, "Bad op %d\n", op));
223
char *name = va_arg(va, char *);
224
char *help = va_arg(va, char *);
225
el_func_t func = va_arg(va, el_func_t);
227
rv = map_addfunc(el, name, help, func);
233
hist_fun_t func = va_arg(va, hist_fun_t);
234
ptr_t ptr = va_arg(va, char *);
236
rv = hist_set(el, func, ptr);
242
el->el_flags &= ~EDIT_DISABLED;
244
el->el_flags |= EDIT_DISABLED;
250
el_rfunc_t rc = va_arg(va, el_rfunc_t);
251
rv = el_read_setfn(el, rc);
256
el->el_data = va_arg(va, void *);
260
rv = va_arg(va, int);
261
if (rv && !(el->el_flags & UNBUFFERED)) {
262
el->el_flags |= UNBUFFERED;
264
} else if (!rv && (el->el_flags & UNBUFFERED)) {
265
el->el_flags &= ~UNBUFFERED;
272
rv = va_arg(va, int);
274
(void) tty_rawmode(el);
276
(void) tty_cookedmode(el);
291
* retrieve the editline parameters
294
el_get(EditLine *el, int op, void *ret)
298
if (el == NULL || ret == NULL)
303
rv = prompt_get(el, (el_pfunc_t *) ret, op);
307
rv = map_get_editor(el, (const char **)ret);
311
*((int *) ret) = (el->el_flags & HANDLE_SIGNALS);
316
*((int *) ret) = (!(el->el_flags & EDIT_DISABLED));
321
term_get(el, (const char **)ret);
332
const char *argv[20];
335
for (i = 1; i < sizeof(argv) / sizeof(argv[0]); i++)
336
if ((argv[i] = va_arg(va, char *)) == NULL)
342
rv = map_bind(el, i, argv);
347
rv = term_telltc(el, i, argv);
352
rv = term_settc(el, i, argv);
357
rv = term_echotc(el, i, argv);
362
rv = tty_stty(el, i, argv);
367
EL_ABORT((el->errfile, "Bad op %d\n", op));
375
char *name = va_arg(va, char *);
376
char *help = va_arg(va, char *);
377
el_func_t func = va_arg(va, el_func_t);
379
rv = map_addfunc(el, name, help, func);
385
hist_fun_t func = va_arg(va, hist_fun_t);
386
ptr_t ptr = va_arg(va, char *);
387
rv = hist_set(el, func, ptr);
393
*((el_rfunc_t *)ret) = el_read_getfn(el);
398
*((void **)ret) = el->el_data;
403
*((int *) ret) = (!(el->el_flags & UNBUFFERED));
416
* Return editing info
418
public const LineInfo *
419
el_line(EditLine *el)
422
return (const LineInfo *) (void *) &el->el_line;
430
el_source(EditLine *el, const char *fname)
438
static const char elpath[] = "/.editrc";
440
char path[MAXPATHLEN];
445
#ifdef HAVE_ISSETUGID
449
if ((ptr = getenv("HOME")) == NULL)
451
if (strlcpy(path, ptr, sizeof(path)) >= sizeof(path))
453
if (strlcat(path, elpath, sizeof(path)) >= sizeof(path))
458
fp = fopen(fname, "r");
462
while ((ptr = fgetln(fp, &len)) != NULL) {
463
if (len > 0 && ptr[len - 1] == '\n')
466
if (parse_line(el, ptr) == -1) {
478
* Called from program when terminal is resized
481
el_resize(EditLine *el)
486
(void) sigemptyset(&nset);
487
(void) sigaddset(&nset, SIGWINCH);
488
(void) sigprocmask(SIG_BLOCK, &nset, &oset);
490
/* get the correct window size */
491
if (term_get_size(el, &lins, &cols))
492
term_change_size(el, lins, cols);
494
(void) sigprocmask(SIG_SETMASK, &oset, NULL);
499
* Called from the program to beep
502
el_beep(EditLine *el)
510
* Set the state of EDIT_DISABLED from the `edit' command.
514
el_editmode(EditLine *el, int argc, const char **argv)
518
if (argv == NULL || argc != 2 || argv[1] == NULL)
522
if (strcmp(how, "on") == 0) {
523
el->el_flags &= ~EDIT_DISABLED;
525
} else if (strcmp(how, "off") == 0) {
527
el->el_flags |= EDIT_DISABLED;
530
(void) fprintf(el->el_errfile, "edit: Bad value `%s'.\n", how);