From 423d8e66bacd10b7a4407872d4c367fc993b25d7 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 30 Oct 2019 23:30:41 +0100
Subject: [PATCH] tty: Add TABDLY, TAB0 and TAB3 to satisfy POSIX a bit better.

* TAB3 is what we already have as OXTABS. Make the latter an alias of
  the former in <sys/_termios.h>.

* Add 'tab0' and 'tab3' flags to stty(1) too.

Most other output flags from the POSIX spec deal with actual time delays
'to allow for mechanical or other movement when certain characters are
sent to the terminal'. Blast from the past.

Taken-from (with adjustments): FreeBSD
---
 bin/stty/modes.c         | 11 ++++++-----
 bin/stty/stty.1          | 16 ++++++++++++++--
 share/man/man4/termios.4 | 10 ++++++++--
 sys/kern/tty.c           |  8 ++++----
 sys/sys/_termios.h       |  5 ++++-
 5 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/bin/stty/modes.c b/bin/stty/modes.c
index 07172f7fd1..5ad892ebb8 100644
--- a/bin/stty/modes.c
+++ b/bin/stty/modes.c
@@ -28,7 +28,6 @@
  *
  * @(#)modes.c	8.3 (Berkeley) 4/2/94
  * $FreeBSD: src/bin/stty/modes.c,v 1.8.2.2 2001/07/04 22:40:00 kris Exp $
- * $DragonFly: src/bin/stty/modes.c,v 1.4 2003/09/28 14:39:15 hmp Exp $
  */
 
 #include <sys/types.h>
@@ -187,10 +186,12 @@ struct modes omodes[] = {
 	{ "-onlcr",	0, ONLCR },
 	{ "ocrnl",	OCRNL, 0 },
 	{ "-ocrnl",	0, OCRNL },
-	{ "tabs",	0, OXTABS },		/* "preserve" tabs */
-	{ "-tabs",	OXTABS, 0 },
-	{ "oxtabs",	OXTABS, 0 },
-	{ "-oxtabs",	0, OXTABS },
+	{ "tabs",	TAB0, TABDLY },		/* "preserve" tabs */
+	{ "-tabs",	TAB3, TABDLY },
+	{ "oxtabs",	TAB3, TABDLY },
+	{ "-oxtabs",	TAB0, TABDLY },
+	{ "tab0",	TAB0, TABDLY },
+	{ "tab3",	TAB3, TABDLY },
 	{ "onocr",	ONOCR, 0 },
 	{ "-onocr",	0, ONOCR },
 	{ "onlret",	ONLRET, 0 },
diff --git a/bin/stty/stty.1 b/bin/stty/stty.1
index a78cfad9bf..37b4f3e41a 100644
--- a/bin/stty/stty.1
+++ b/bin/stty/stty.1
@@ -31,7 +31,7 @@
 .\"     @(#)stty.1	8.4 (Berkeley) 4/18/94
 .\" $FreeBSD: src/bin/stty/stty.1,v 1.13.2.6 2001/12/14 14:22:09 ru Exp $
 .\"
-.Dd August 23, 2016
+.Dd October 30, 2019
 .Dt STTY 1
 .Os
 .Sh NAME
@@ -240,8 +240,20 @@ Map (do not map)
 to
 .Dv NL
 on output.
+.It Cm tab0 tab3
+Select tab expansion policy.
+.Cm tab0
+disables tab expansion, while
+.Cm tab3
+enables it.
 .It Cm oxtabs Pq Fl oxtabs
-Expand (do not expand) tabs to spaces on output.
+.Cm oxtabs
+is an alias for
+.Cm tab3 ,
+while
+.Cm Fl oxtabs
+is an alias for
+.Cm tab0 .
 .It Cm onocr Pq Fl onocr
 Do not (do) output CRs at column zero.
 .It Cm onlret Pq Fl onlret
diff --git a/share/man/man4/termios.4 b/share/man/man4/termios.4
index 9166291e0b..f582051cea 100644
--- a/share/man/man4/termios.4
+++ b/share/man/man4/termios.4
@@ -28,7 +28,7 @@
 .\"	@(#)termios.4	8.4 (Berkeley) 4/19/94
 .\" $FreeBSD: src/share/man/man4/termios.4,v 1.12.2.9 2001/12/17 11:30:12 ru Exp $
 .\"
-.Dd December 31, 2017
+.Dd October 30, 2019
 .Dt TERMIOS 4
 .Os
 .Sh NAME
@@ -1064,8 +1064,14 @@ and are composed of the following masks:
 */
 .It Dv OCRNL
 /* map CR to NL */
-.It Dv OXTABS
+.It Dv TABDLY
+/* tab delay mask */
+.It Dv TAB0
+/* no tab delay and expansion */
+.It Dv TAB3
 /* expand tabs to spaces */
+.It Dv OXTABS
+/* alias for TAB3 */
 .It Dv ONOEOT
 /* discard
 .Dv EOT Ns 's
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index 5443f977ae..e571d06447 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -764,10 +764,10 @@ ttyoutput(int c, struct tty *tp)
 		return (-1);
 	}
 	/*
-	 * Do tab expansion if OXTABS is set.  Special case if we external
-	 * processing, we don't do the tab expansion because we'll probably
-	 * get it wrong.  If tab expansion needs to be done, let it happen
-	 * externally.
+	 * Do tab expansion if OXTABS aka TAB3 is set.  Special case if we
+	 * external processing, we don't do the tab expansion because we'll
+	 * probably get it wrong.  If tab expansion needs to be done, let
+	 * it happen externally.
 	 */
 	CLR(c, ~TTY_CHARMASK);
 	if (c == '\t' &&
diff --git a/sys/sys/_termios.h b/sys/sys/_termios.h
index 76a748bc34..4801eb4167 100644
--- a/sys/sys/_termios.h
+++ b/sys/sys/_termios.h
@@ -102,9 +102,12 @@
 #define	OPOST		0x00000001	/* enable following output processing */
 #if __XSI_VISIBLE
 #define	ONLCR		0x00000002	/* map NL to CR-NL (ala CRMOD) */
+#define	TABDLY		0x00000004	/* tab delay mask */
+#define	    TAB0	    0x00000000	    /* no tab delay and expansion */
+#define	    TAB3	    0x00000004	    /* expand tabs to spaces */
 #endif
 #if __BSD_VISIBLE
-#define	OXTABS		0x00000004	/* expand tabs to spaces */
+#define	OXTABS		TAB3
 #define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
 #endif  /*__BSD_VISIBLE */
 #if __XSI_VISIBLE
-- 
2.23.0

