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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)el.c 8.2 (Berkeley) 1/3/94
33 * $NetBSD: el.c,v 1.45 2008/04/05 15:53:28 christos Exp $
34 * $DragonFly: src/lib/libedit/el.c,v 1.7 2008/05/17 22:48:04 pavalos Exp $
40 * el.c: EditLine interface functions
42 #include <sys/types.h>
43 #include <sys/param.h>
50 * Initialize editline and set default parameters.
53 el_init(const char *prog
, FILE *fin
, FILE *fout
, FILE *ferr
)
56 EditLine
*el
= (EditLine
*) el_malloc(sizeof(EditLine
));
61 memset(el
, 0, sizeof(EditLine
));
64 el
->el_outfile
= fout
;
65 el
->el_errfile
= ferr
;
67 el
->el_infd
= fileno(fin
);
69 if ((el
->el_prog
= el_strdup(prog
)) == NULL
) {
75 * Initialize all the modules. Order is important!!!
79 if (term_init(el
) == -1) {
86 if (tty_init(el
) == -1)
87 el
->el_flags
|= NO_TTY
;
89 (void) search_init(el
);
91 (void) prompt_init(el
);
121 el_free((ptr_t
) el
->el_prog
);
127 * Reset the tty and the parser
130 el_reset(EditLine
*el
)
134 ch_reset(el
, 0); /* XXX: Do we want that? */
139 * set the editline parameters
142 el_set(EditLine
*el
, int op
, ...)
154 rv
= prompt_set(el
, va_arg(ap
, el_pfunc_t
), op
);
158 rv
= term_set(el
, va_arg(ap
, char *));
162 rv
= map_set_editor(el
, va_arg(ap
, char *));
167 el
->el_flags
|= HANDLE_SIGNALS
;
169 el
->el_flags
&= ~HANDLE_SIGNALS
;
179 const char *argv
[20];
182 for (i
= 1; i
< 20; i
++)
183 if ((argv
[i
] = va_arg(ap
, 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
);
214 EL_ABORT((el
->el_errfile
, "Bad op %d\n", op
));
222 char *name
= va_arg(ap
, char *);
223 char *help
= va_arg(ap
, char *);
224 el_func_t func
= va_arg(ap
, el_func_t
);
226 rv
= map_addfunc(el
, name
, help
, func
);
232 hist_fun_t func
= va_arg(ap
, hist_fun_t
);
233 ptr_t ptr
= va_arg(ap
, char *);
235 rv
= hist_set(el
, func
, ptr
);
241 el
->el_flags
&= ~EDIT_DISABLED
;
243 el
->el_flags
|= EDIT_DISABLED
;
249 el_rfunc_t rc
= va_arg(ap
, el_rfunc_t
);
250 rv
= el_read_setfn(el
, rc
);
255 el
->el_data
= va_arg(ap
, void *);
259 rv
= va_arg(ap
, int);
260 if (rv
&& !(el
->el_flags
& UNBUFFERED
)) {
261 el
->el_flags
|= UNBUFFERED
;
263 } else if (!rv
&& (el
->el_flags
& UNBUFFERED
)) {
264 el
->el_flags
&= ~UNBUFFERED
;
271 rv
= va_arg(ap
, int);
273 (void) tty_rawmode(el
);
275 (void) tty_cookedmode(el
);
284 what
= va_arg(ap
, int);
285 fp
= va_arg(ap
, FILE *);
291 el
->el_infd
= fileno(fp
);
307 re_clear_display(el
);
323 * retrieve the editline parameters
326 el_get(EditLine
*el
, int op
, ...)
339 rv
= prompt_get(el
, va_arg(ap
, el_pfunc_t
*), op
);
343 rv
= map_get_editor(el
, va_arg(ap
, const char **));
347 *va_arg(ap
, int *) = (el
->el_flags
& HANDLE_SIGNALS
);
352 *va_arg(ap
, int *) = !(el
->el_flags
& EDIT_DISABLED
);
357 term_get(el
, va_arg(ap
, const char **));
363 static char name
[] = "gettc";
367 for (i
= 1; i
< sizeof(argv
) / sizeof(argv
[0]); i
++)
368 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
374 rv
= term_gettc(el
, i
, argv
);
379 EL_ABORT((el
->el_errfile
, "Bad op %d\n", op
));
388 char *name
= va_arg(ap
, char *);
389 char *help
= va_arg(ap
, char *);
390 el_func_t func
= va_arg(ap
, el_func_t
);
392 rv
= map_addfunc(el
, name
, help
, func
);
398 hist_fun_t func
= va_arg(ap
, hist_fun_t
);
399 ptr_t ptr
= va_arg(ap
, char *);
400 rv
= hist_set(el
, func
, ptr
);
406 *va_arg(ap
, el_rfunc_t
*) = el_read_getfn(el
);
411 *va_arg(ap
, void **) = el
->el_data
;
416 *va_arg(ap
, int *) = (!(el
->el_flags
& UNBUFFERED
));
425 what
= va_arg(ap
, int);
426 fpp
= va_arg(ap
, FILE **);
430 *fpp
= el
->el_infile
;
433 *fpp
= el
->el_outfile
;
436 *fpp
= el
->el_errfile
;
455 * Return editing info
457 public const LineInfo
*
458 el_line(EditLine
*el
)
461 return (const LineInfo
*) (void *) &el
->el_line
;
469 el_source(EditLine
*el
, const char *fname
)
477 #ifdef HAVE_ISSETUGID
478 static const char elpath
[] = "/.editrc";
479 char path
[MAXPATHLEN
];
483 if ((ptr
= getenv("HOME")) == NULL
)
485 if (strlcpy(path
, ptr
, sizeof(path
)) >= sizeof(path
))
487 if (strlcat(path
, elpath
, sizeof(path
)) >= sizeof(path
))
492 * If issetugid() is missing, always return an error, in order
493 * to keep from inadvertently opening up the user to a security
500 fp
= fopen(fname
, "r");
504 while ((ptr
= fgetln(fp
, &len
)) != NULL
) {
505 if (len
> 0 && ptr
[len
- 1] == '\n')
508 if (parse_line(el
, ptr
) == -1) {
520 * Called from program when terminal is resized
523 el_resize(EditLine
*el
)
528 (void) sigemptyset(&nset
);
529 (void) sigaddset(&nset
, SIGWINCH
);
530 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
532 /* get the correct window size */
533 if (term_get_size(el
, &lins
, &cols
))
534 term_change_size(el
, lins
, cols
);
536 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
541 * Called from the program to beep
544 el_beep(EditLine
*el
)
552 * Set the state of EDIT_DISABLED from the `edit' command.
556 el_editmode(EditLine
*el
, int argc
, const char **argv
)
560 if (argv
== NULL
|| argc
!= 2 || argv
[1] == NULL
)
564 if (strcmp(how
, "on") == 0) {
565 el
->el_flags
&= ~EDIT_DISABLED
;
567 } else if (strcmp(how
, "off") == 0) {
569 el
->el_flags
|= EDIT_DISABLED
;
572 (void) fprintf(el
->el_errfile
, "edit: Bad value `%s'.\n", how
);