From 2bc276c7ac419f0bb283e15bf8340fee786a2fb6 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sun, 2 Oct 2016 23:40:18 +0200
Subject: [PATCH 4/6] libc/termios: Sync with FreeBSD kind of.

Adds tcsetsid() and cfmakesane() in preparation for further changes.
---
 lib/libc/Versions.def   |  4 +++
 lib/libc/gen/Symbol.map |  5 ++++
 lib/libc/gen/termios.c  | 35 +++++++++++++++++++++---
 sys/sys/_termios.h      | 11 ++++----
 sys/sys/termios.h       | 26 +++++++-----------
 sys/sys/ttydefaults.h   | 71 ++++++++++++++++++++++++++++++-------------------
 6 files changed, 100 insertions(+), 52 deletions(-)

diff --git a/lib/libc/Versions.def b/lib/libc/Versions.def
index b341f05..ec92c3c 100644
--- a/lib/libc/Versions.def
+++ b/lib/libc/Versions.def
@@ -25,6 +25,10 @@ DF404.0 {
 DF406.0 {
 } DF404.0;
 
+# version for DragonFly 4.7
+DF408.0 {
+} DF406.0;
+
 # This is our private namespace.  Any global interfaces that are
 # strictly for use only by other DragonFly applications and libraries
 # are listed here.  We use a separate namespace so we can write
diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map
index 8e94912..edf8bd4 100644
--- a/lib/libc/gen/Symbol.map
+++ b/lib/libc/gen/Symbol.map
@@ -452,6 +452,11 @@ DF404.0 {
     wordfree;
 };
 
+DF408.0 {
+    cfmakesane;
+    tcsetsid;
+};
+
 DFprivate_1.0 {
 	/* pthread stubs */
     _pthread_atfork;
diff --git a/lib/libc/gen/termios.c b/lib/libc/gen/termios.c
index 19e84cc..578fb06 100644
--- a/lib/libc/gen/termios.c
+++ b/lib/libc/gen/termios.c
@@ -26,10 +26,8 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $FreeBSD: src/lib/libc/gen/termios.c,v 1.9.2.1 2000/03/18 23:13:25 jasone Exp $
- * $DragonFly: src/lib/libc/gen/termios.c,v 1.5 2005/11/19 22:32:53 swildner Exp $
- *
  * @(#)termios.c	8.2 (Berkeley) 2/21/94
+ * $FreeBSD: head/lib/libc/gen/termios.c 276630 2015-01-03 18:38:46Z kib $
  */
 
 #include "namespace.h"
@@ -39,6 +37,8 @@
 #include <sys/time.h>
 
 #include <errno.h>
+#include <string.h>
+#define	TTYDEFCHARS
 #include <termios.h>
 #include <unistd.h>
 #include "un-namespace.h"
@@ -104,6 +104,18 @@ tcgetsid(int fd)
 	return ((pid_t)s);
 }
 
+int
+tcsetsid(int fd, pid_t pid)
+{
+
+	if (pid != getsid(0)) {
+		errno = EINVAL;
+		return (-1);
+	}
+
+	return (_ioctl(fd, TIOCSCTTY, NULL));
+}
+
 speed_t
 cfgetospeed(const struct termios *t)
 {
@@ -160,6 +172,23 @@ cfmakeraw(struct termios *t)
 	t->c_cc[VTIME] = 0;
 }
 
+/*
+ * Obtain a termios structure which is similar to the one provided by
+ * the kernel.
+ */
+void
+cfmakesane(struct termios *t)
+{
+
+	t->c_cflag = TTYDEF_CFLAG;
+	t->c_iflag = TTYDEF_IFLAG;
+	t->c_lflag = TTYDEF_LFLAG;
+	t->c_oflag = TTYDEF_OFLAG;
+	t->c_ispeed = TTYDEF_SPEED;
+	t->c_ospeed = TTYDEF_SPEED;
+	memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
+}
+
 int
 tcsendbreak(int fd, int len __unused)
 {
diff --git a/sys/sys/_termios.h b/sys/sys/_termios.h
index 582e965..ed0f0cb 100644
--- a/sys/sys/_termios.h
+++ b/sys/sys/_termios.h
@@ -102,12 +102,9 @@
 #define	OPOST		0x00000001	/* enable following output processing */
 #if __XSI_VISIBLE
 #define	ONLCR		0x00000002	/* map NL to CR-NL (ala CRMOD) */
-#endif
-#if __BSD_VISIBLE
-#define	OXTABS		0x00000004	/* expand tabs to spaces */
-#define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
-#endif  /*__BSD_VISIBLE */
-#if __XSI_VISIBLE
+#define	TABDLY		0x00000004	/* tab delay mask */
+#define	    TAB0	    0x00000000	    /* no tab delay and expansion */
+#define	    TAB3	    0x00000004	    /* expand tabs to spaces */
 #define	OCRNL		0x00000010	/* map CR to NL on output */
 #define	ONOCR		0x00000020	/* no CR output at column 0 */
 #define	ONLRET		0x00000040	/* NL performs CR function */
@@ -204,6 +201,8 @@
 #define	B76800	76800
 #define	B115200	115200
 #define	B230400	230400
+#define	B460800	460800
+#define	B921600	921600
 #define	EXTA	19200
 #define	EXTB	38400
 #endif  /* __BSD_VISIBLE */
diff --git a/sys/sys/termios.h b/sys/sys/termios.h
index 3b75a96..20501e2 100644
--- a/sys/sys/termios.h
+++ b/sys/sys/termios.h
@@ -1,4 +1,4 @@
-/*
+/*-
  * Copyright (c) 1988, 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -27,7 +27,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)termios.h	8.3 (Berkeley) 3/28/94
- * $FreeBSD: src/sys/sys/termios.h,v 1.13.2.1 2001/03/06 06:31:44 jhb Exp $
+ * $FreeBSD: head/include/termios.h 265878 2014-05-11 13:48:21Z jilles $
  */
 
 #ifndef _SYS_TERMIOS_H_
@@ -37,12 +37,15 @@
 #include <sys/_termios.h>
 /* Needed by tcgetsid(3). */
 #include <sys/stdint.h>
+
 #ifndef _PID_T_DECLARED
-typedef	__pid_t pid_t;
+typedef	__pid_t		pid_t;
 #define	_PID_T_DECLARED
 #endif
 
 #if __BSD_VISIBLE
+#define	OXTABS		TAB3
+#define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
 #define	CCEQ(val, c)	((c) == (val) ? (val) != _POSIX_VDISABLE : 0)
 #endif
 
@@ -81,26 +84,17 @@ pid_t	tcgetsid(int);
 #endif	/* __POSIX_VISIBLE >= 200809 || __XSI_VISIBLE */
 
 #if __BSD_VISIBLE
+int	tcsetsid(int, pid_t);
+
 void	cfmakeraw(struct termios *);
+void	cfmakesane(struct termios *);
 int	cfsetspeed(struct termios *, speed_t);
 #endif /* __BSD_VISIBLE */
 __END_DECLS
 
-#if __BSD_VISIBLE
-
-/*
- * Include tty ioctl's that aren't just for backwards compatibility
- * with the old tty driver.  These ioctl definitions were previously
- * in <sys/ioctl.h>.
- */
-#include <sys/ttycom.h>
-#endif
-
-/*
- * END OF PROTECTED INCLUDE.
- */
 #endif /* !_SYS_TERMIOS_H_ */
 
 #if __BSD_VISIBLE
+#include <sys/ttycom.h>
 #include <sys/ttydefaults.h>
 #endif
diff --git a/sys/sys/ttydefaults.h b/sys/sys/ttydefaults.h
index b72bc0d..2f8d514 100644
--- a/sys/sys/ttydefaults.h
+++ b/sys/sys/ttydefaults.h
@@ -32,7 +32,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)ttydefaults.h	8.4 (Berkeley) 1/21/94
- * $FreeBSD: src/sys/sys/ttydefaults.h,v 1.13.2.1 2001/03/06 03:37:08 jkh Exp $
+ * $FreeBSD: head/sys/sys/ttydefaults.h 249311 2013-04-09 16:16:34Z ed $
  */
 
 /*
@@ -45,38 +45,48 @@
  * Defaults on "first" open.
  */
 #define	TTYDEF_IFLAG	(BRKINT	| ICRNL	| IMAXBEL | IXON | IXANY)
-#define TTYDEF_OFLAG	(OPOST | ONLCR)
-#define TTYDEF_LFLAG	(ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
+#define	TTYDEF_OFLAG	(OPOST | ONLCR)
+#define	TTYDEF_LFLAG_NOECHO (ICANON | ISIG | IEXTEN)
+#define	TTYDEF_LFLAG_ECHO (TTYDEF_LFLAG_NOECHO \
+	| ECHO | ECHOE | ECHOKE | ECHOCTL)
+#define	TTYDEF_LFLAG TTYDEF_LFLAG_ECHO
 #define	TTYDEF_CFLAG	(CREAD | CS8 | HUPCL)
-#define TTYDEF_SPEED	(B9600)
+#define	TTYDEF_SPEED	(B9600)
 
 /*
  * Control Character Defaults
  */
-#define CTRL(x)	(x&037)
-#define CCHECKPT	CTRL('e')
-#define	CEOF		CTRL('d')
+/*
+ * XXX: A lot of code uses lowercase characters, but control-character
+ * conversion is actually only valid when applied to uppercase
+ * characters. We just treat lowercase characters as if they were
+ * inserted as uppercase.
+ */
+#define	CTRL(x) ((x) >= 'a' && (x) <= 'z' ? \
+	((x) - 'a' + 1) : (((x) - 'A' + 1) & 0x7f))
+#define	CCHECKPT	CTRL('E')
+#define	CEOF		CTRL('D')
 #define	CEOL		0xff		/* XXX avoid _POSIX_VDISABLE */
-#define	CERASE		0177
-#define	CERASE2		CTRL('h')
-#define	CINTR		CTRL('c')
-#define	CSTATUS		CTRL('t')
-#define	CKILL		CTRL('u')
+#define	CERASE		CTRL('?')
+#define	CERASE2		CTRL('H')
+#define	CINTR		CTRL('C')
+#define	CSTATUS		CTRL('T')
+#define	CKILL		CTRL('U')
 #define	CMIN		1
-#define	CQUIT		034		/* FS, ^\ */
-#define	CSUSP		CTRL('z')
+#define	CQUIT		CTRL('\\')
+#define	CSUSP		CTRL('Z')
 #define	CTIME		0
-#define	CDSUSP		CTRL('y')
-#define	CSTART		CTRL('q')
-#define	CSTOP		CTRL('s')
-#define	CLNEXT		CTRL('v')
-#define	CDISCARD 	CTRL('o')
-#define	CWERASE 	CTRL('w')
-#define	CREPRINT 	CTRL('r')
+#define	CDSUSP		CTRL('Y')
+#define	CSTART		CTRL('Q')
+#define	CSTOP		CTRL('S')
+#define	CLNEXT		CTRL('V')
+#define	CDISCARD	CTRL('O')
+#define	CWERASE		CTRL('W')
+#define	CREPRINT	CTRL('R')
 #define	CEOT		CEOF
 /* compat */
 #define	CBRK		CEOL
-#define CRPRNT		CREPRINT
+#define	CRPRNT		CREPRINT
 #define	CFLUSH		CDISCARD
 
 /* PROTECTED INCLUSION ENDS HERE */
@@ -86,10 +96,17 @@
  * #define TTYDEFCHARS to include an array of default control characters.
  */
 #ifdef TTYDEFCHARS
-static cc_t	ttydefchars[NCCS] = {
-	CEOF,	CEOL,	CEOL,	CERASE, CWERASE, CKILL, CREPRINT,
-	CERASE2, CINTR,	CQUIT,	CSUSP,	CDSUSP,	CSTART,	CSTOP,	CLNEXT,
-	CDISCARD, CMIN,	CTIME,  CSTATUS, CCHECKPT
+
+#include <sys/cdefs.h>
+#include <sys/_termios.h>
+
+static const cc_t ttydefchars[] = {
+	CEOF, CEOL, CEOL, CERASE, CWERASE, CKILL, CREPRINT, CERASE2, CINTR,
+	CQUIT, CSUSP, CDSUSP, CSTART, CSTOP, CLNEXT, CDISCARD, CMIN, CTIME,
+	CSTATUS, CCHECKPT
 };
+_Static_assert(sizeof(ttydefchars) / sizeof(cc_t) == NCCS,
+    "Size of ttydefchars does not match NCCS");
+
 #undef TTYDEFCHARS
-#endif
+#endif /* TTYDEFCHARS */
-- 
2.9.2

