summaryrefslogtreecommitdiffstats
path: root/lib/libedit/tty.c
diff options
context:
space:
mode:
authorschwarze <schwarze@openbsd.org>2016-04-09 18:42:49 +0000
committerschwarze <schwarze@openbsd.org>2016-04-09 18:42:49 +0000
commit491df958dfb17b13c78bdc2dfbd7ce70d6c68c7c (patch)
treeb50f8cfde6d0770b4468b9d248cc606f40074d9b /lib/libedit/tty.c
parentRemove _tz as intermediate variable and use CGI_TZ directly. (diff)
downloadwireguard-openbsd-491df958dfb17b13c78bdc2dfbd7ce70d6c68c7c.tar.xz
wireguard-openbsd-491df958dfb17b13c78bdc2dfbd7ce70d6c68c7c.zip
Reset the terminal to its initial state before exiting a program
using libedit, using code from NetBSD tty.c rev. 1.42, 1.48, 1.49, 1.58, and 1.59. Code mostly by Christos Zoulas, one bug report by John Hein, one additional bugfix by me (rev. 1.59). While here, switch t_mode to unsigned char, which is sufficient, also from NetBSD. OK martijn@. Also checked by Christian Heckendorf <mbie at ulmus dot me>.
Diffstat (limited to 'lib/libedit/tty.c')
-rw-r--r--lib/libedit/tty.c25
1 files changed, 20 insertions, 5 deletions
diff --git a/lib/libedit/tty.c b/lib/libedit/tty.c
index dd3fed61ea2..c68f8ecc530 100644
--- a/lib/libedit/tty.c
+++ b/lib/libedit/tty.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty.c,v 1.21 2016/03/20 23:48:27 schwarze Exp $ */
+/* $OpenBSD: tty.c,v 1.22 2016/04/09 18:42:49 schwarze Exp $ */
/* $NetBSD: tty.c,v 1.34 2011/01/27 23:11:40 christos Exp $ */
/*-
@@ -495,6 +495,9 @@ tty_setup(EditLine *el)
if (el->el_flags & EDIT_DISABLED)
return 0;
+ if (el->el_tty.t_initialized)
+ return -1;
+
if (!isatty(el->el_outfd)) {
#ifdef DEBUG_TTY
(void) fprintf(el->el_errfile,
@@ -502,14 +505,14 @@ tty_setup(EditLine *el)
#endif /* DEBUG_TTY */
return -1;
}
- if (tty_getty(el, &el->el_tty.t_ed) == -1) {
+ if (tty_getty(el, &el->el_tty.t_or) == -1) {
#ifdef DEBUG_TTY
(void) fprintf(el->el_errfile,
"tty_setup: tty_getty: %s\n", strerror(errno));
#endif /* DEBUG_TTY */
return -1;
}
- el->el_tty.t_ts = el->el_tty.t_ex = el->el_tty.t_ed;
+ el->el_tty.t_ts = el->el_tty.t_ex = el->el_tty.t_ed = el->el_tty.t_or;
el->el_tty.t_speed = tty__getspeed(&el->el_tty.t_ex);
el->el_tty.t_tabs = tty__gettabs(&el->el_tty.t_ex);
@@ -555,6 +558,7 @@ tty_setup(EditLine *el)
tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
tty_bind_char(el, 1);
+ el->el_tty.t_initialized = 1;
return 0;
}
@@ -564,6 +568,7 @@ tty_init(EditLine *el)
el->el_tty.t_mode = EX_IO;
el->el_tty.t_vdisable = _POSIX_VDISABLE;
+ el->el_tty.t_initialized = 0;
(void) memcpy(el->el_tty.t_t, ttyperm, sizeof(ttyperm_t));
(void) memcpy(el->el_tty.t_c, ttychar, sizeof(ttychar_t));
return tty_setup(el);
@@ -575,10 +580,20 @@ tty_init(EditLine *el)
*/
protected void
/*ARGSUSED*/
-tty_end(EditLine *el __attribute__((__unused__)))
+tty_end(EditLine *el)
{
+ if (el->el_flags & EDIT_DISABLED)
+ return;
+
+ if (!el->el_tty.t_initialized)
+ return;
- /* XXX: Maybe reset to an initial state? */
+ if (tty_setty(el, TCSAFLUSH, &el->el_tty.t_or) == -1) {
+#ifdef DEBUG_TTY
+ (void) fprintf(el->el_errfile,
+ "%s: tty_setty: %s\n", __func__, strerror(errno));
+#endif /* DEBUG_TTY */
+ }
}