git: 128f63cedc14 - main - (s)tty: add support for IUTF8 input flag

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Sat, 07 Oct 2023 18:00:50 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=128f63cedc14ae21b35f74e11e2fe1a5659c58e8

commit 128f63cedc14ae21b35f74e11e2fe1a5659c58e8
Author:     Bojan Novković <bojan.novkovic@fer.hr>
AuthorDate: 2023-10-07 17:59:57 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2023-10-07 17:59:57 +0000

    (s)tty: add support for IUTF8 input flag
    
    This patch adds the necessary kernel and stty code to support setting
    the IUTF8 flag for ttys. It is the first of two patches that fix
    backspace behaviour for UTF-8 encoded characters when in canonical mode.
    
    Reported by:    christos
    Reviewed by:    christos, imp
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D42066
---
 bin/stty/modes.c         | 2 ++
 bin/stty/print.c         | 1 +
 bin/stty/stty.1          | 3 +++
 share/man/man4/termios.4 | 2 ++
 sys/kern/tty.c           | 4 ++--
 sys/sys/_termios.h       | 1 +
 6 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/bin/stty/modes.c b/bin/stty/modes.c
index ce39083d7d63..3818677ddc4e 100644
--- a/bin/stty/modes.c
+++ b/bin/stty/modes.c
@@ -126,6 +126,8 @@ static const struct modes imodes[] = {
 	{ "-decctlq",	IXANY, 0 },
 	{ "imaxbel",	IMAXBEL, 0 },
 	{ "-imaxbel",	0, IMAXBEL },
+	{ "iutf8",	IUTF8, 0 },
+	{ "-iutf8",	0, IUTF8 },
 	{ NULL,		0, 0 },
 };
 
diff --git a/bin/stty/print.c b/bin/stty/print.c
index 07a3c2058474..33472bbccea1 100644
--- a/bin/stty/print.c
+++ b/bin/stty/print.c
@@ -127,6 +127,7 @@ print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt)
 	put("-inpck", INPCK, 0);
 	put("-ignpar", IGNPAR, 0);
 	put("-parmrk", PARMRK, 0);
+	put("-iutf8", IUTF8, 1);
 
 	/* output flags */
 	tmp = tp->c_oflag;
diff --git a/bin/stty/stty.1 b/bin/stty/stty.1
index 9e9405928c67..c572a657a997 100644
--- a/bin/stty/stty.1
+++ b/bin/stty/stty.1
@@ -224,6 +224,9 @@ if
 .Cm imaxbel
 is unset and the input queue is full, the next input character causes
 the entire input and output queues to be discarded.
+.It Cm iutf8 Pq Fl iutf8
+Assume that input characters are UTF-8 encoded. Setting this flag
+causes backspace to properly delete multibyte characters in canonical mode.
 .El
 .Ss Output Modes:
 This corresponds to the c_oflag of the termios structure.
diff --git a/share/man/man4/termios.4 b/share/man/man4/termios.4
index d3a5c8fa582d..80015025345e 100644
--- a/share/man/man4/termios.4
+++ b/share/man/man4/termios.4
@@ -884,6 +884,8 @@ following masks:
 /* any char will restart after stop */
 .It Dv IMAXBEL
 /* ring bell on input queue full */
+.It Dv IUTF8
+/* assume input is UTF-8 encoded */
 .El
 .Pp
 In the context of asynchronous serial data transmission, a break
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index 134c1dfba98b..620233947410 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -85,8 +85,8 @@ static const char	*dev_console_filename;
 /*
  * Flags that are supported and stored by this implementation.
  */
-#define TTYSUP_IFLAG	(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
-			INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
+#define TTYSUP_IFLAG	(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|\
+			IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL|IUTF8)
 #define TTYSUP_OFLAG	(OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
 #define TTYSUP_LFLAG	(ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
 			ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
diff --git a/sys/sys/_termios.h b/sys/sys/_termios.h
index 327ffcb2f98b..823752732ee2 100644
--- a/sys/sys/_termios.h
+++ b/sys/sys/_termios.h
@@ -97,6 +97,7 @@
 #endif
 #if __BSD_VISIBLE
 #define	IMAXBEL		0x00002000	/* ring bell on input queue full */
+#define IUTF8		0x00004000	/* assume input is utf-8 encoded */
 #endif
 
 /*