summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/libedit/tty.c229
-rw-r--r--lib/libedit/tty.h3
2 files changed, 99 insertions, 133 deletions
diff --git a/lib/libedit/tty.c b/lib/libedit/tty.c
index 54c7dae5b85..4873454dcd4 100644
--- a/lib/libedit/tty.c
+++ b/lib/libedit/tty.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty.c,v 1.13 2011/07/07 05:40:42 okan Exp $ */
+/* $OpenBSD: tty.c,v 1.14 2014/05/20 22:28:07 yasuoka Exp $ */
/* $NetBSD: tty.c,v 1.34 2011/01/27 23:11:40 christos Exp $ */
/*-
@@ -453,6 +453,7 @@ private void tty__getchar(struct termios *, unsigned char *);
private void tty__setchar(struct termios *, unsigned char *);
private speed_t tty__getspeed(struct termios *);
private int tty_setup(EditLine *);
+private void tty_setup_flags(EditLine *, struct termios *, int);
#define t_qu t_ts
@@ -511,17 +512,7 @@ tty_setup(EditLine *el)
el->el_tty.t_tabs = tty__gettabs(&el->el_tty.t_ex);
el->el_tty.t_eight = tty__geteightbit(&el->el_tty.t_ex);
- el->el_tty.t_ex.c_iflag &= ~el->el_tty.t_t[EX_IO][MD_INP].t_clrmask;
- el->el_tty.t_ex.c_iflag |= el->el_tty.t_t[EX_IO][MD_INP].t_setmask;
-
- el->el_tty.t_ex.c_oflag &= ~el->el_tty.t_t[EX_IO][MD_OUT].t_clrmask;
- el->el_tty.t_ex.c_oflag |= el->el_tty.t_t[EX_IO][MD_OUT].t_setmask;
-
- el->el_tty.t_ex.c_cflag &= ~el->el_tty.t_t[EX_IO][MD_CTL].t_clrmask;
- el->el_tty.t_ex.c_cflag |= el->el_tty.t_t[EX_IO][MD_CTL].t_setmask;
-
- el->el_tty.t_ex.c_lflag &= ~el->el_tty.t_t[EX_IO][MD_LIN].t_clrmask;
- el->el_tty.t_ex.c_lflag |= el->el_tty.t_t[EX_IO][MD_LIN].t_setmask;
+ tty_setup_flags(el, &el->el_tty.t_ex, EX_IO);
/*
* Reset the tty chars to reasonable defaults
@@ -561,17 +552,7 @@ tty_setup(EditLine *el)
tty__setchar(&el->el_tty.t_ex, el->el_tty.t_c[EX_IO]);
#endif
- el->el_tty.t_ed.c_iflag &= ~el->el_tty.t_t[ED_IO][MD_INP].t_clrmask;
- el->el_tty.t_ed.c_iflag |= el->el_tty.t_t[ED_IO][MD_INP].t_setmask;
-
- el->el_tty.t_ed.c_oflag &= ~el->el_tty.t_t[ED_IO][MD_OUT].t_clrmask;
- el->el_tty.t_ed.c_oflag |= el->el_tty.t_t[ED_IO][MD_OUT].t_setmask;
-
- el->el_tty.t_ed.c_cflag &= ~el->el_tty.t_t[ED_IO][MD_CTL].t_clrmask;
- el->el_tty.t_ed.c_cflag |= el->el_tty.t_t[ED_IO][MD_CTL].t_setmask;
-
- el->el_tty.t_ed.c_lflag &= ~el->el_tty.t_t[ED_IO][MD_LIN].t_clrmask;
- el->el_tty.t_ed.c_lflag |= el->el_tty.t_t[ED_IO][MD_LIN].t_setmask;
+ tty_setup_flags(el, &el->el_tty.t_ed, ED_IO);
tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
tty_bind_char(el, 1);
@@ -933,6 +914,58 @@ tty_bind_char(EditLine *el, int force)
}
+private tcflag_t *
+tty__get_flag(struct termios *t, int kind) {
+ switch (kind) {
+ case MD_INP:
+ return &t->c_iflag;
+ case MD_OUT:
+ return &t->c_oflag;
+ case MD_CTL:
+ return &t->c_cflag;
+ case MD_LIN:
+ return &t->c_lflag;
+ default:
+ abort();
+ /*NOTREACHED*/
+ }
+}
+
+
+private tcflag_t
+tty_update_flag(EditLine *el, tcflag_t f, int mode, int kind)
+{
+ f &= ~el->el_tty.t_t[mode][kind].t_clrmask;
+ f |= el->el_tty.t_t[mode][kind].t_setmask;
+ return f;
+}
+
+
+private void
+tty_update_flags(EditLine *el, int kind)
+{
+ tcflag_t *tt, *ed, *ex;
+ tt = tty__get_flag(&el->el_tty.t_ts, kind);
+ ed = tty__get_flag(&el->el_tty.t_ed, kind);
+ ex = tty__get_flag(&el->el_tty.t_ex, kind);
+
+ if (*tt != *ex && (kind != MD_CTL || *tt != *ed)) {
+ *ed = tty_update_flag(el, *tt, ED_IO, kind);
+ *ex = tty_update_flag(el, *tt, EX_IO, kind);
+ }
+}
+
+
+private void
+tty_update_char(EditLine *el, int mode, int c) {
+ if (!((el->el_tty.t_t[mode][MD_CHAR].t_setmask & C_SH(c)))
+ && (el->el_tty.t_c[TS_IO][c] != el->el_tty.t_c[EX_IO][c]))
+ el->el_tty.t_c[mode][c] = el->el_tty.t_c[TS_IO][c];
+ if (el->el_tty.t_t[mode][MD_CHAR].t_clrmask & C_SH(c))
+ el->el_tty.t_c[mode][c] = el->el_tty.t_vdisable;
+}
+
+
/* tty_rawmode():
* Set terminal into 1 character at a time mode.
*/
@@ -968,112 +1001,42 @@ tty_rawmode(EditLine *el)
(void) cfsetospeed(&el->el_tty.t_ed, el->el_tty.t_speed);
}
if (tty__cooked_mode(&el->el_tty.t_ts)) {
- if (el->el_tty.t_ts.c_cflag != el->el_tty.t_ex.c_cflag) {
- el->el_tty.t_ex.c_cflag =
- el->el_tty.t_ts.c_cflag;
- el->el_tty.t_ex.c_cflag &=
- ~el->el_tty.t_t[EX_IO][MD_CTL].t_clrmask;
- el->el_tty.t_ex.c_cflag |=
- el->el_tty.t_t[EX_IO][MD_CTL].t_setmask;
-
- el->el_tty.t_ed.c_cflag =
- el->el_tty.t_ts.c_cflag;
- el->el_tty.t_ed.c_cflag &=
- ~el->el_tty.t_t[ED_IO][MD_CTL].t_clrmask;
- el->el_tty.t_ed.c_cflag |=
- el->el_tty.t_t[ED_IO][MD_CTL].t_setmask;
- }
- if ((el->el_tty.t_ts.c_lflag != el->el_tty.t_ex.c_lflag) &&
- (el->el_tty.t_ts.c_lflag != el->el_tty.t_ed.c_lflag)) {
- el->el_tty.t_ex.c_lflag =
- el->el_tty.t_ts.c_lflag;
- el->el_tty.t_ex.c_lflag &=
- ~el->el_tty.t_t[EX_IO][MD_LIN].t_clrmask;
- el->el_tty.t_ex.c_lflag |=
- el->el_tty.t_t[EX_IO][MD_LIN].t_setmask;
-
- el->el_tty.t_ed.c_lflag =
- el->el_tty.t_ts.c_lflag;
- el->el_tty.t_ed.c_lflag &=
- ~el->el_tty.t_t[ED_IO][MD_LIN].t_clrmask;
- el->el_tty.t_ed.c_lflag |=
- el->el_tty.t_t[ED_IO][MD_LIN].t_setmask;
- }
- if ((el->el_tty.t_ts.c_iflag != el->el_tty.t_ex.c_iflag) &&
- (el->el_tty.t_ts.c_iflag != el->el_tty.t_ed.c_iflag)) {
- el->el_tty.t_ex.c_iflag =
- el->el_tty.t_ts.c_iflag;
- el->el_tty.t_ex.c_iflag &=
- ~el->el_tty.t_t[EX_IO][MD_INP].t_clrmask;
- el->el_tty.t_ex.c_iflag |=
- el->el_tty.t_t[EX_IO][MD_INP].t_setmask;
-
- el->el_tty.t_ed.c_iflag =
- el->el_tty.t_ts.c_iflag;
- el->el_tty.t_ed.c_iflag &=
- ~el->el_tty.t_t[ED_IO][MD_INP].t_clrmask;
- el->el_tty.t_ed.c_iflag |=
- el->el_tty.t_t[ED_IO][MD_INP].t_setmask;
- }
- if ((el->el_tty.t_ts.c_oflag != el->el_tty.t_ex.c_oflag) &&
- (el->el_tty.t_ts.c_oflag != el->el_tty.t_ed.c_oflag)) {
- el->el_tty.t_ex.c_oflag =
- el->el_tty.t_ts.c_oflag;
- el->el_tty.t_ex.c_oflag &=
- ~el->el_tty.t_t[EX_IO][MD_OUT].t_clrmask;
- el->el_tty.t_ex.c_oflag |=
- el->el_tty.t_t[EX_IO][MD_OUT].t_setmask;
-
- el->el_tty.t_ed.c_oflag =
- el->el_tty.t_ts.c_oflag;
- el->el_tty.t_ed.c_oflag &=
- ~el->el_tty.t_t[ED_IO][MD_OUT].t_clrmask;
- el->el_tty.t_ed.c_oflag |=
- el->el_tty.t_t[ED_IO][MD_OUT].t_setmask;
- }
+ int i;
+
+ for (i = MD_INP; i <= MD_LIN; i++)
+ tty_update_flags(el, i);
+
if (tty__gettabs(&el->el_tty.t_ex) == 0)
el->el_tty.t_tabs = 0;
else
el->el_tty.t_tabs = EL_CAN_TAB ? 1 : 0;
- {
- int i;
+ tty__getchar(&el->el_tty.t_ts, el->el_tty.t_c[TS_IO]);
+ /*
+ * Check if the user made any changes.
+ * If he did, then propagate the changes to the
+ * edit and execute data structures.
+ */
+ for (i = 0; i < C_NCC; i++)
+ if (el->el_tty.t_c[TS_IO][i] !=
+ el->el_tty.t_c[EX_IO][i])
+ break;
- tty__getchar(&el->el_tty.t_ts, el->el_tty.t_c[TS_IO]);
+ if (i != C_NCC) {
/*
- * Check if the user made any changes.
- * If he did, then propagate the changes to the
- * edit and execute data structures.
- */
+ * Propagate changes only to the unprotected
+ * chars that have been modified just now.
+ */
for (i = 0; i < C_NCC; i++)
- if (el->el_tty.t_c[TS_IO][i] !=
- el->el_tty.t_c[EX_IO][i])
- break;
-
- if (i != C_NCC) {
- /*
- * Propagate changes only to the unprotected
- * chars that have been modified just now.
- */
- for (i = 0; i < C_NCC; i++) {
- if (!((el->el_tty.t_t[ED_IO][MD_CHAR].t_setmask & C_SH(i)))
- && (el->el_tty.t_c[TS_IO][i] != el->el_tty.t_c[EX_IO][i]))
- el->el_tty.t_c[ED_IO][i] = el->el_tty.t_c[TS_IO][i];
- if (el->el_tty.t_t[ED_IO][MD_CHAR].t_clrmask & C_SH(i))
- el->el_tty.t_c[ED_IO][i] = el->el_tty.t_vdisable;
- }
- tty_bind_char(el, 0);
- tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
-
- for (i = 0; i < C_NCC; i++) {
- if (!((el->el_tty.t_t[EX_IO][MD_CHAR].t_setmask & C_SH(i)))
- && (el->el_tty.t_c[TS_IO][i] != el->el_tty.t_c[EX_IO][i]))
- el->el_tty.t_c[EX_IO][i] = el->el_tty.t_c[TS_IO][i];
- if (el->el_tty.t_t[EX_IO][MD_CHAR].t_clrmask & C_SH(i))
- el->el_tty.t_c[EX_IO][i] = el->el_tty.t_vdisable;
- }
- tty__setchar(&el->el_tty.t_ex, el->el_tty.t_c[EX_IO]);
- }
+ tty_update_char(el, ED_IO, i);
+
+ tty_bind_char(el, 0);
+ tty__setchar(&el->el_tty.t_ed, el->el_tty.t_c[ED_IO]);
+
+ for (i = 0; i < C_NCC; i++)
+ tty_update_char(el, EX_IO, i);
+
+ tty__setchar(&el->el_tty.t_ex, el->el_tty.t_c[EX_IO]);
}
}
if (tty_setty(el, TCSADRAIN, &el->el_tty.t_ed) == -1) {
@@ -1125,17 +1088,7 @@ tty_quotemode(EditLine *el)
el->el_tty.t_qu = el->el_tty.t_ed;
- el->el_tty.t_qu.c_iflag &= ~el->el_tty.t_t[QU_IO][MD_INP].t_clrmask;
- el->el_tty.t_qu.c_iflag |= el->el_tty.t_t[QU_IO][MD_INP].t_setmask;
-
- el->el_tty.t_qu.c_oflag &= ~el->el_tty.t_t[QU_IO][MD_OUT].t_clrmask;
- el->el_tty.t_qu.c_oflag |= el->el_tty.t_t[QU_IO][MD_OUT].t_setmask;
-
- el->el_tty.t_qu.c_cflag &= ~el->el_tty.t_t[QU_IO][MD_CTL].t_clrmask;
- el->el_tty.t_qu.c_cflag |= el->el_tty.t_t[QU_IO][MD_CTL].t_setmask;
-
- el->el_tty.t_qu.c_lflag &= ~el->el_tty.t_t[QU_IO][MD_LIN].t_clrmask;
- el->el_tty.t_qu.c_lflag |= el->el_tty.t_t[QU_IO][MD_LIN].t_setmask;
+ tty_setup_flags(el, &el->el_tty.t_qu, QU_IO);
if (tty_setty(el, TCSADRAIN, &el->el_tty.t_qu) == -1) {
#ifdef DEBUG_TTY
@@ -1312,6 +1265,7 @@ tty_stty(EditLine *el, int argc __attribute__((__unused__)), const Char **argv)
}
}
+ tty_setup_flags(el, tios, z);
if (el->el_tty.t_mode == z) {
if (tty_setty(el, TCSADRAIN, tios) == -1) {
#ifdef DEBUG_TTY
@@ -1349,3 +1303,14 @@ tty_printchar(EditLine *el, unsigned char *s)
(void) fprintf(el->el_errfile, "\n");
}
#endif /* notyet */
+
+
+private void
+tty_setup_flags(EditLine *el, struct termios *tios, int mode)
+{
+ int kind;
+ for (kind = MD_INP; kind <= MD_LIN; kind++) {
+ tcflag_t *f = tty__get_flag(tios, kind);
+ *f = tty_update_flag(el, *f, mode, kind);
+ }
+}
diff --git a/lib/libedit/tty.h b/lib/libedit/tty.h
index a80f4eea7c0..cb531a646c2 100644
--- a/lib/libedit/tty.h
+++ b/lib/libedit/tty.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tty.h,v 1.8 2010/06/30 00:05:35 nicm Exp $ */
+/* $OpenBSD: tty.h,v 1.9 2014/05/20 22:28:07 yasuoka Exp $ */
/* $NetBSD: tty.h,v 1.12 2009/12/30 22:37:40 christos Exp $ */
/*-
@@ -443,6 +443,7 @@
#define QU_IO 2 /* used only for quoted chars */
#define NN_IO 3 /* The number of entries */
+/* Don't re-order */
#define MD_INP 0
#define MD_OUT 1
#define MD_CTL 2