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.44 2006/12/15 22:13:33 christos Exp $
34 * $DragonFly: src/lib/libedit/el.c,v 1.6 2007/05/05 00:27:39 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
);
317 * retrieve the editline parameters
320 el_get(EditLine
*el
, int op
, ...)
333 rv
= prompt_get(el
, va_arg(ap
, el_pfunc_t
*), op
);
337 rv
= map_get_editor(el
, va_arg(ap
, const char **));
341 *va_arg(ap
, int *) = (el
->el_flags
& HANDLE_SIGNALS
);
346 *va_arg(ap
, int *) = !(el
->el_flags
& EDIT_DISABLED
);
351 term_get(el
, va_arg(ap
, const char **));
357 static char name
[] = "gettc";
361 for (i
= 1; i
< sizeof(argv
) / sizeof(argv
[0]); i
++)
362 if ((argv
[i
] = va_arg(ap
, char *)) == NULL
)
368 rv
= term_gettc(el
, i
, argv
);
373 EL_ABORT((el
->el_errfile
, "Bad op %d\n", op
));
382 char *name
= va_arg(ap
, char *);
383 char *help
= va_arg(ap
, char *);
384 el_func_t func
= va_arg(ap
, el_func_t
);
386 rv
= map_addfunc(el
, name
, help
, func
);
392 hist_fun_t func
= va_arg(ap
, hist_fun_t
);
393 ptr_t ptr
= va_arg(ap
, char *);
394 rv
= hist_set(el
, func
, ptr
);
400 *va_arg(ap
, el_rfunc_t
*) = el_read_getfn(el
);
405 *va_arg(ap
, void **) = el
->el_data
;
410 *va_arg(ap
, int *) = (!(el
->el_flags
& UNBUFFERED
));
419 what
= va_arg(ap
, int);
420 fpp
= va_arg(ap
, FILE **);
424 *fpp
= el
->el_infile
;
427 *fpp
= el
->el_outfile
;
430 *fpp
= el
->el_errfile
;
449 * Return editing info
451 public const LineInfo
*
452 el_line(EditLine
*el
)
455 return (const LineInfo
*) (void *) &el
->el_line
;
463 el_source(EditLine
*el
, const char *fname
)
471 #ifdef HAVE_ISSETUGID
472 static const char elpath
[] = "/.editrc";
473 char path
[MAXPATHLEN
];
477 if ((ptr
= getenv("HOME")) == NULL
)
479 if (strlcpy(path
, ptr
, sizeof(path
)) >= sizeof(path
))
481 if (strlcat(path
, elpath
, sizeof(path
)) >= sizeof(path
))
486 * If issetugid() is missing, always return an error, in order
487 * to keep from inadvertently opening up the user to a security
494 fp
= fopen(fname
, "r");
498 while ((ptr
= fgetln(fp
, &len
)) != NULL
) {
499 if (len
> 0 && ptr
[len
- 1] == '\n')
502 if (parse_line(el
, ptr
) == -1) {
514 * Called from program when terminal is resized
517 el_resize(EditLine
*el
)
522 (void) sigemptyset(&nset
);
523 (void) sigaddset(&nset
, SIGWINCH
);
524 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
526 /* get the correct window size */
527 if (term_get_size(el
, &lins
, &cols
))
528 term_change_size(el
, lins
, cols
);
530 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
535 * Called from the program to beep
538 el_beep(EditLine
*el
)
546 * Set the state of EDIT_DISABLED from the `edit' command.
550 el_editmode(EditLine
*el
, int argc
, const char **argv
)
554 if (argv
== NULL
|| argc
!= 2 || argv
[1] == NULL
)
558 if (strcmp(how
, "on") == 0) {
559 el
->el_flags
&= ~EDIT_DISABLED
;
561 } else if (strcmp(how
, "off") == 0) {
563 el
->el_flags
|= EDIT_DISABLED
;
566 (void) fprintf(el
->el_errfile
, "edit: Bad value `%s'.\n", how
);