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.39 2004/07/08 00:51:36 christos Exp $
34 * $DragonFly: src/lib/libedit/el.c,v 1.5 2005/11/13 11:58:30 corecode 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
));
63 el
->el_infd
= fileno(fin
);
64 el
->el_outfile
= fout
;
65 el
->el_errfile
= ferr
;
66 if ((el
->el_prog
= el_strdup(prog
)) == NULL
) {
72 * Initialize all the modules. Order is important!!!
76 if (term_init(el
) == -1) {
83 if (tty_init(el
) == -1)
84 el
->el_flags
|= NO_TTY
;
86 (void) search_init(el
);
88 (void) prompt_init(el
);
118 el_free((ptr_t
) el
->el_prog
);
124 * Reset the tty and the parser
127 el_reset(EditLine
*el
)
131 ch_reset(el
); /* XXX: Do we want that? */
136 * set the editline parameters
139 el_set(EditLine
*el
, int op
, ...)
151 rv
= prompt_set(el
, va_arg(va
, el_pfunc_t
), op
);
155 rv
= term_set(el
, va_arg(va
, char *));
159 rv
= map_set_editor(el
, va_arg(va
, char *));
164 el
->el_flags
|= HANDLE_SIGNALS
;
166 el
->el_flags
&= ~HANDLE_SIGNALS
;
175 const char *argv
[20];
178 for (i
= 1; i
< 20; i
++)
179 if ((argv
[i
] = va_arg(va
, char *)) == NULL
)
185 rv
= map_bind(el
, i
, argv
);
190 rv
= term_telltc(el
, i
, argv
);
195 rv
= term_settc(el
, i
, argv
);
200 rv
= term_echotc(el
, i
, argv
);
205 rv
= tty_stty(el
, i
, argv
);
210 EL_ABORT((el
->el_errfile
, "Bad op %d\n", op
));
218 char *name
= va_arg(va
, char *);
219 char *help
= va_arg(va
, char *);
220 el_func_t func
= va_arg(va
, el_func_t
);
222 rv
= map_addfunc(el
, name
, help
, func
);
228 hist_fun_t func
= va_arg(va
, hist_fun_t
);
229 ptr_t ptr
= va_arg(va
, char *);
231 rv
= hist_set(el
, func
, ptr
);
237 el
->el_flags
&= ~EDIT_DISABLED
;
239 el
->el_flags
|= EDIT_DISABLED
;
245 el_rfunc_t rc
= va_arg(va
, el_rfunc_t
);
246 rv
= el_read_setfn(el
, rc
);
251 el
->el_data
= va_arg(va
, void *);
255 rv
= va_arg(va
, int);
256 if (rv
&& !(el
->el_flags
& UNBUFFERED
)) {
257 el
->el_flags
|= UNBUFFERED
;
259 } else if (!rv
&& (el
->el_flags
& UNBUFFERED
)) {
260 el
->el_flags
&= ~UNBUFFERED
;
267 rv
= va_arg(va
, int);
269 (void) tty_rawmode(el
);
271 (void) tty_cookedmode(el
);
286 * retrieve the editline parameters
289 el_get(EditLine
*el
, int op
, void *ret
)
293 if (el
== NULL
|| ret
== NULL
)
298 rv
= prompt_get(el
, (void *) &ret
, op
);
302 rv
= map_get_editor(el
, (void *) &ret
);
306 *((int *) ret
) = (el
->el_flags
& HANDLE_SIGNALS
);
311 *((int *) ret
) = (!(el
->el_flags
& EDIT_DISABLED
));
316 term_get(el
, (const char **)ret
);
327 const char *argv
[20];
330 for (i
= 1; i
< sizeof(argv
) / sizeof(argv
[0]); i
++)
331 if ((argv
[i
] = va_arg(va
, char *)) == NULL
)
337 rv
= map_bind(el
, i
, argv
);
342 rv
= term_telltc(el
, i
, argv
);
347 rv
= term_settc(el
, i
, argv
);
352 rv
= term_echotc(el
, i
, argv
);
357 rv
= tty_stty(el
, i
, argv
);
362 EL_ABORT((el
->errfile
, "Bad op %d\n", op
));
370 char *name
= va_arg(va
, char *);
371 char *help
= va_arg(va
, char *);
372 el_func_t func
= va_arg(va
, el_func_t
);
374 rv
= map_addfunc(el
, name
, help
, func
);
380 hist_fun_t func
= va_arg(va
, hist_fun_t
);
381 ptr_t ptr
= va_arg(va
, char *);
382 rv
= hist_set(el
, func
, ptr
);
388 *((el_rfunc_t
*)ret
) = el_read_getfn(el
);
393 *((void **)ret
) = el
->el_data
;
398 *((int *) ret
) = (!(el
->el_flags
& UNBUFFERED
));
411 * Return editing info
413 public const LineInfo
*
414 el_line(EditLine
*el
)
417 return (const LineInfo
*) (void *) &el
->el_line
;
425 el_source(EditLine
*el
, const char *fname
)
433 #ifdef HAVE_ISSETUGID
434 static const char elpath
[] = "/.editrc";
435 char path
[MAXPATHLEN
];
439 if ((ptr
= getenv("HOME")) == NULL
)
441 if (strlcpy(path
, ptr
, sizeof(path
)) >= sizeof(path
))
443 if (strlcat(path
, elpath
, sizeof(path
)) >= sizeof(path
))
448 * If issetugid() is missing, always return an error, in order
449 * to keep from inadvertently opening up the user to a security
456 fp
= fopen(fname
, "r");
460 while ((ptr
= fgetln(fp
, &len
)) != NULL
) {
461 if (len
> 0 && ptr
[len
- 1] == '\n')
464 if (parse_line(el
, ptr
) == -1) {
476 * Called from program when terminal is resized
479 el_resize(EditLine
*el
)
484 (void) sigemptyset(&nset
);
485 (void) sigaddset(&nset
, SIGWINCH
);
486 (void) sigprocmask(SIG_BLOCK
, &nset
, &oset
);
488 /* get the correct window size */
489 if (term_get_size(el
, &lins
, &cols
))
490 term_change_size(el
, lins
, cols
);
492 (void) sigprocmask(SIG_SETMASK
, &oset
, NULL
);
497 * Called from the program to beep
500 el_beep(EditLine
*el
)
508 * Set the state of EDIT_DISABLED from the `edit' command.
512 el_editmode(EditLine
*el
, int argc
, const char **argv
)
516 if (argv
== NULL
|| argc
!= 2 || argv
[1] == NULL
)
520 if (strcmp(how
, "on") == 0) {
521 el
->el_flags
&= ~EDIT_DISABLED
;
523 } else if (strcmp(how
, "off") == 0) {
525 el
->el_flags
|= EDIT_DISABLED
;
528 (void) fprintf(el
->el_errfile
, "edit: Bad value `%s'.\n", how
);