summaryrefslogtreecommitdiffstats
path: root/lib
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
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')
-rw-r--r--lib/libedit/tty.c25
-rw-r--r--lib/libedit/tty.h9
2 files changed, 25 insertions, 9 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 */
+ }
}
diff --git a/lib/libedit/tty.h b/lib/libedit/tty.h
index 49a7541942a..1e004e06404 100644
--- a/lib/libedit/tty.h
+++ b/lib/libedit/tty.h
@@ -1,5 +1,5 @@
-/* $OpenBSD: tty.h,v 1.10 2016/03/20 22:57:59 schwarze Exp $ */
-/* $NetBSD: tty.h,v 1.12 2009/12/30 22:37:40 christos Exp $ */
+/* $OpenBSD: tty.h,v 1.11 2016/04/09 18:42:49 schwarze Exp $ */
+/* $NetBSD: tty.h,v 1.19 2016/02/27 18:13:21 christos Exp $ */
/*-
* Copyright (c) 1992, 1993
@@ -469,12 +469,13 @@ protected void tty_bind_char(EditLine *, int);
typedef struct {
ttyperm_t t_t;
ttychar_t t_c;
- struct termios t_ex, t_ed, t_ts;
+ struct termios t_or, t_ex, t_ed, t_ts;
int t_tabs;
int t_eight;
speed_t t_speed;
- int t_mode;
+ unsigned char t_mode;
unsigned char t_vdisable;
+ unsigned char t_initialized;
} el_tty_t;