From 4317db9b0a5ab5f7d10135788d6ef6c9a8c73039 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Wed, 28 Sep 2016 00:00:06 +0200
Subject: [PATCH 2/2] WIP split out <sys/termios.h> and assorted fallout.

---
 bin/stty/print.c                |   3 -
 lib/libc/Versions.def           |   4 +
 lib/libc/gen/Symbol.map         |   5 +
 lib/libc/gen/termios.c          |  35 ++++++-
 libexec/getty/subr.c            | 154 ---------------------------
 sys/bus/u4b/serial/usb_serial.h |   1 +
 sys/config/LINT64               |   2 +-
 sys/dev/misc/dcons/dcons_os.c   |   1 +
 sys/dev/misc/nmdm/nmdm.c        |   1 +
 sys/dev/misc/syscons/syscons.c  |   1 +
 sys/dev/misc/syscons/sysmouse.c |   1 +
 sys/dev/serial/rp/rp.c          |   1 +
 sys/dev/serial/si/si_drv.c      |   1 +
 sys/dev/serial/sio/sio.c        |   1 +
 sys/kern/tty.c                  |   1 +
 sys/kern/tty_pty.c              |   2 +
 sys/sys/_termios.h              | 223 ++++++++++++++++++++++++++++++++++++++++
 sys/sys/ioctl_compat.h          |   2 -
 sys/sys/termios.h               | 221 +++------------------------------------
 sys/sys/tty.h                   |   4 +-
 sys/sys/ttycom.h                |   2 +-
 sys/sys/ttydefaults.h           |  72 ++++++++-----
 usr.sbin/ppp/physical.c         |   1 +
 23 files changed, 337 insertions(+), 402 deletions(-)
 create mode 100644 sys/sys/_termios.h

diff --git a/bin/stty/print.c b/bin/stty/print.c
index 698cb07..88d2886 100644
--- a/bin/stty/print.c
+++ b/bin/stty/print.c
@@ -28,7 +28,6 @@
  *
  * @(#)print.c	8.6 (Berkeley) 4/16/94
  * $FreeBSD: src/bin/stty/print.c,v 1.12.2.2 2001/07/04 22:40:00 kris Exp $
- * $DragonFly: src/bin/stty/print.c,v 1.5 2004/11/07 20:54:52 eirikn Exp $
  */
 
 #include <sys/types.h>
@@ -40,8 +39,6 @@
 #include "stty.h"
 #include "extern.h"
 
-#include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
-
 static void  binit (const char *);
 static void  bput (const char *);
 static const char *ccval (struct cchar *, int);
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/libexec/getty/subr.c b/libexec/getty/subr.c
index b3e8fc8..aeb7107 100644
--- a/libexec/getty/subr.c
+++ b/libexec/getty/subr.c
@@ -33,7 +33,6 @@
 /*
  * Melbourne getty.
  */
-#define COMPAT_43
 #ifdef DEBUG
 #include <stdio.h>
 #endif
@@ -50,11 +49,6 @@
 #include "pathnames.h"
 #include "extern.h"
 
-
-#ifdef COMPAT_43
-static void	compatflags (long);
-#endif
-
 /*
  * Get a table entry.
  */
@@ -251,29 +245,6 @@ set_flags(int n)
 {
 	tcflag_t iflag, oflag, cflag, lflag;
 
-#ifdef COMPAT_43
-	switch (n) {
-	case 0:
-		if (F0set) {
-			compatflags(F0);
-			return;
-		}
-		break;
-	case 1:
-		if (F1set) {
-			compatflags(F1);
-			return;
-		}
-		break;
-	default:
-		if (F2set) {
-			compatflags(F2);
-			return;
-		}
-		break;
-	}
-#endif
-
 	switch (n) {
 	case 0:
 		if (C0set && I0set && L0set && O0set) {
@@ -428,131 +399,6 @@ out:
 	tmode.c_lflag = lflag;
 }
 
-#ifdef COMPAT_43
-/*
- * Old TTY => termios, snatched from <sys/kern/tty_compat.c>
- */
-void
-compatflags(long flags)
-{
-	tcflag_t iflag, oflag, cflag, lflag;
-
-	iflag = BRKINT|ICRNL|IMAXBEL|IXON|IXANY;
-	oflag = OPOST|ONLCR|OXTABS;
-	cflag = CREAD;
-	lflag = ICANON|ISIG|IEXTEN;
-
-	if (ISSET(flags, TANDEM))
-		SET(iflag, IXOFF);
-	else
-		CLR(iflag, IXOFF);
-	if (ISSET(flags, ECHO))
-		SET(lflag, ECHO);
-	else
-		CLR(lflag, ECHO);
-	if (ISSET(flags, CRMOD)) {
-		SET(iflag, ICRNL);
-		SET(oflag, ONLCR);
-	} else {
-		CLR(iflag, ICRNL);
-		CLR(oflag, ONLCR);
-	}
-	if (ISSET(flags, XTABS))
-		SET(oflag, OXTABS);
-	else
-		CLR(oflag, OXTABS);
-
-
-	if (ISSET(flags, RAW)) {
-		iflag &= IXOFF;
-		CLR(lflag, ISIG|ICANON|IEXTEN);
-		CLR(cflag, PARENB);
-	} else {
-		SET(iflag, BRKINT|IXON|IMAXBEL);
-		SET(lflag, ISIG|IEXTEN);
-		if (ISSET(flags, CBREAK))
-			CLR(lflag, ICANON);
-		else
-			SET(lflag, ICANON);
-		switch (ISSET(flags, ANYP)) {
-		case 0:
-			CLR(cflag, PARENB);
-			break;
-		case ANYP:
-			SET(cflag, PARENB);
-			CLR(iflag, INPCK);
-			break;
-		case EVENP:
-			SET(cflag, PARENB);
-			SET(iflag, INPCK);
-			CLR(cflag, PARODD);
-			break;
-		case ODDP:
-			SET(cflag, PARENB);
-			SET(iflag, INPCK);
-			SET(cflag, PARODD);
-			break;
-		}
-	}
-
-	/* Nothing we can do with CRTBS. */
-	if (ISSET(flags, PRTERA))
-		SET(lflag, ECHOPRT);
-	else
-		CLR(lflag, ECHOPRT);
-	if (ISSET(flags, CRTERA))
-		SET(lflag, ECHOE);
-	else
-		CLR(lflag, ECHOE);
-	/* Nothing we can do with TILDE. */
-	if (ISSET(flags, MDMBUF))
-		SET(cflag, MDMBUF);
-	else
-		CLR(cflag, MDMBUF);
-	if (ISSET(flags, NOHANG))
-		CLR(cflag, HUPCL);
-	else
-		SET(cflag, HUPCL);
-	if (ISSET(flags, CRTKIL))
-		SET(lflag, ECHOKE);
-	else
-		CLR(lflag, ECHOKE);
-	if (ISSET(flags, CTLECH))
-		SET(lflag, ECHOCTL);
-	else
-		CLR(lflag, ECHOCTL);
-	if (!ISSET(flags, DECCTQ))
-		SET(iflag, IXANY);
-	else
-		CLR(iflag, IXANY);
-	CLR(lflag, TOSTOP|FLUSHO|PENDIN|NOFLSH);
-	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
-
-	if (ISSET(flags, RAW|LITOUT|PASS8)) {
-		CLR(cflag, CSIZE);
-		SET(cflag, CS8);
-		if (!ISSET(flags, RAW|PASS8))
-			SET(iflag, ISTRIP);
-		else
-			CLR(iflag, ISTRIP);
-		if (!ISSET(flags, RAW|LITOUT))
-			SET(oflag, OPOST);
-		else
-			CLR(oflag, OPOST);
-	} else {
-		CLR(cflag, CSIZE);
-		SET(cflag, CS7);
-		SET(iflag, ISTRIP);
-		SET(oflag, OPOST);
-	}
-
-	tmode.c_iflag = iflag;
-	tmode.c_oflag = oflag;
-	tmode.c_cflag = cflag;
-	tmode.c_lflag = lflag;
-}
-#endif
-
 #ifdef XXX_DELAY
 struct delayval {
 	unsigned	delay;		/* delay in ms */
diff --git a/sys/bus/u4b/serial/usb_serial.h b/sys/bus/u4b/serial/usb_serial.h
index b4abfc7..db83a73 100644
--- a/sys/bus/u4b/serial/usb_serial.h
+++ b/sys/bus/u4b/serial/usb_serial.h
@@ -61,6 +61,7 @@
 #define	_USB_SERIAL_H_
 
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/fcntl.h>
 #include <sys/sysctl.h>
 #include <sys/timepps.h>
diff --git a/sys/config/LINT64 b/sys/config/LINT64
index 59a94dd..6d60b1e 100644
--- a/sys/config/LINT64
+++ b/sys/config/LINT64
@@ -124,7 +124,7 @@ options 	CPU_DISABLE_AVX
 # FreeBSD.  You probably do NOT want to remove this as much current code
 # still relies on the 4.3 emulation.
 #
-options 	COMPAT_43
+#options 	COMPAT_43
 
 # Enable NDIS binary driver support
 options 	NDISAPI
diff --git a/sys/dev/misc/dcons/dcons_os.c b/sys/dev/misc/dcons/dcons_os.c
index 2065a0e8..73a6aa8 100644
--- a/sys/dev/misc/dcons/dcons_os.c
+++ b/sys/dev/misc/dcons/dcons_os.c
@@ -49,6 +49,7 @@
 #include <sys/cons.h>
 #include <sys/consio.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/malloc.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
diff --git a/sys/dev/misc/nmdm/nmdm.c b/sys/dev/misc/nmdm/nmdm.c
index 96642a8..74a89a4 100644
--- a/sys/dev/misc/nmdm/nmdm.c
+++ b/sys/dev/misc/nmdm/nmdm.c
@@ -49,6 +49,7 @@
 #include <sys/priv.h>
 #include <sys/thread2.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/kernel.h>
diff --git a/sys/dev/misc/syscons/syscons.c b/sys/dev/misc/syscons/syscons.c
index 216dd27..44f74af 100644
--- a/sys/dev/misc/syscons/syscons.c
+++ b/sys/dev/misc/syscons/syscons.c
@@ -47,6 +47,7 @@
 #include <sys/signalvar.h>
 #include <sys/sysctl.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/kernel.h>
 #include <sys/cons.h>
 #include <sys/random.h>
diff --git a/sys/dev/misc/syscons/sysmouse.c b/sys/dev/misc/syscons/sysmouse.c
index b145144..60da5ca 100644
--- a/sys/dev/misc/syscons/sysmouse.c
+++ b/sys/dev/misc/syscons/sysmouse.c
@@ -40,6 +40,7 @@
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/kernel.h>
 #include <sys/thread2.h>
 
diff --git a/sys/dev/serial/rp/rp.c b/sys/dev/serial/rp/rp.c
index b3a23ef..0d81ce6 100644
--- a/sys/dev/serial/rp/rp.c
+++ b/sys/dev/serial/rp/rp.c
@@ -43,6 +43,7 @@
 #include <sys/fcntl.h>
 #include <sys/malloc.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/dkstat.h>
diff --git a/sys/dev/serial/si/si_drv.c b/sys/dev/serial/si/si_drv.c
index cc21700..e814389 100644
--- a/sys/dev/serial/si/si_drv.c
+++ b/sys/dev/serial/si/si_drv.c
@@ -50,6 +50,7 @@ static const char si_copyright1[] =  "@(#) Copyright (C) Specialix International
 #include <sys/ioctl_compat.h>
 #endif
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/conf.h>
diff --git a/sys/dev/serial/sio/sio.c b/sys/dev/serial/sio/sio.c
index ccf1022..01d684a 100644
--- a/sys/dev/serial/sio/sio.c
+++ b/sys/dev/serial/sio/sio.c
@@ -54,6 +54,7 @@
 #include <sys/reboot.h>
 #include <sys/malloc.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/proc.h>
 #include <sys/priv.h>
 #include <sys/module.h>
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index b4d0966..f434ceb 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -83,6 +83,7 @@
 #include <sys/proc.h>
 #include <sys/priv.h>
 #define	TTYDEFCHARS
+#include <sys/termios.h>
 #include <sys/tty.h>
 #include <sys/clist.h>
 #undef	TTYDEFCHARS
diff --git a/sys/kern/tty_pty.c b/sys/kern/tty_pty.c
index b372b31..69509a4 100644
--- a/sys/kern/tty_pty.c
+++ b/sys/kern/tty_pty.c
@@ -54,7 +54,9 @@
 #endif
 #include <sys/proc.h>
 #include <sys/priv.h>
+#include <sys/termios.h>
 #include <sys/tty.h>
+#include <sys/ttydefaults.h>
 #include <sys/conf.h>
 #include <sys/fcntl.h>
 #include <sys/kernel.h>
diff --git a/sys/sys/_termios.h b/sys/sys/_termios.h
new file mode 100644
index 0000000..c0ab792
--- /dev/null
+++ b/sys/sys/_termios.h
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) 2016 The DragonFly Project.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ * 3. Neither the name of The DragonFly Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific, prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef _SYS__TERMIOS_H_
+#define	_SYS__TERMIOS_H_
+
+/*
+ * Special Control Characters
+ *
+ * Index into c_cc[] character array.
+ *
+ *	Name	     Subscript	Enabled by
+ */
+#define	VEOF		0	/* ICANON */
+#define	VEOL		1	/* ICANON */
+#if __BSD_VISIBLE
+#define	VEOL2		2	/* ICANON together with IEXTEN */
+#endif
+#define	VERASE		3	/* ICANON */
+#if __BSD_VISIBLE
+#define	VWERASE 	4	/* ICANON together with IEXTEN */
+#endif
+#define	VKILL		5	/* ICANON */
+#if __BSD_VISIBLE
+#define	VREPRINT 	6	/* ICANON together with IEXTEN */
+#define	VERASE2 	7	/* ICANON */
+#endif
+#define	VINTR		8	/* ISIG */
+#define	VQUIT		9	/* ISIG */
+#define	VSUSP		10	/* ISIG */
+#if __BSD_VISIBLE
+#define	VDSUSP		11	/* ISIG together with IEXTEN */
+#endif
+#define	VSTART		12	/* IXON, IXOFF */
+#define	VSTOP		13	/* IXON, IXOFF */
+#if __BSD_VISIBLE
+#define	VLNEXT		14	/* IEXTEN */
+#define	VDISCARD	15	/* IEXTEN */
+#endif
+#define	VMIN		16	/* !ICANON */
+#define	VTIME		17	/* !ICANON */
+#if __BSD_VISIBLE
+#define	VSTATUS		18	/* ICANON together with IEXTEN */
+#define	VCHECKPT	19	/* ICANON together with IEXTEN */
+#endif
+#define	NCCS		20
+
+#define	_POSIX_VDISABLE	0xff
+
+/*
+ * Input flags - software input processing
+ */
+#define	IGNBRK		0x00000001	/* ignore BREAK condition */
+#define	BRKINT		0x00000002	/* map BREAK to SIGINTR */
+#define	IGNPAR		0x00000004	/* ignore (discard) parity errors */
+#define	PARMRK		0x00000008	/* mark parity and framing errors */
+#define	INPCK		0x00000010	/* enable checking of parity errors */
+#define	ISTRIP		0x00000020	/* strip 8th bit off chars */
+#define	INLCR		0x00000040	/* map NL into CR */
+#define	IGNCR		0x00000080	/* ignore CR */
+#define	ICRNL		0x00000100	/* map CR to NL (ala CRMOD) */
+#define	IXON		0x00000200	/* enable output flow control */
+#define	IXOFF		0x00000400	/* enable input flow control */
+#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200809
+#define	IXANY		0x00000800	/* any char will restart after stop */
+#endif
+#if __BSD_VISIBLE
+#define	IMAXBEL		0x00002000	/* ring bell on input queue full */
+#endif  /*__BSD_VISIBLE */
+
+/*
+ * Output flags - software output processing
+ */
+#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 */
+#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 */
+#endif
+
+/*
+ * Control flags - hardware control of terminal
+ */
+#if __BSD_VISIBLE
+#define	CIGNORE		0x00000001	/* ignore control flags */
+#endif
+#define	CSIZE		0x00000300	/* character size mask */
+#define	    CS5		    0x00000000	    /* 5 bits (pseudo) */
+#define	    CS6		    0x00000100	    /* 6 bits */
+#define	    CS7		    0x00000200	    /* 7 bits */
+#define	    CS8		    0x00000300	    /* 8 bits */
+#define	CSTOPB		0x00000400	/* send 2 stop bits */
+#define	CREAD		0x00000800	/* enable receiver */
+#define	PARENB		0x00001000	/* parity enable */
+#define	PARODD		0x00002000	/* odd parity, else even */
+#define	HUPCL		0x00004000	/* hang up on last close */
+#define	CLOCAL		0x00008000	/* ignore modem status lines */
+#if __BSD_VISIBLE
+#define	CCTS_OFLOW	0x00010000	/* CTS flow control of output */
+#define	CRTSCTS		(CCTS_OFLOW | CRTS_IFLOW)
+#define	CRTS_IFLOW	0x00020000	/* RTS flow control of input */
+#define	CDTR_IFLOW	0x00040000	/* DTR flow control of input */
+#define	CDSR_OFLOW	0x00080000	/* DSR flow control of output */
+#define	CCAR_OFLOW	0x00100000	/* DCD flow control of output */
+#endif
+
+
+/*
+ * "Local" flags - dumping ground for other state
+ *
+ * Warning: some flags in this structure begin with
+ * the letter "I" and look like they belong in the
+ * input flag.
+ */
+
+#if __BSD_VISIBLE
+#define	ECHOKE		0x00000001	/* visual erase for line kill */
+#endif  /*__BSD_VISIBLE */
+#define	ECHOE		0x00000002	/* visually erase chars */
+#define	ECHOK		0x00000004	/* echo NL after line kill */
+#define	ECHO		0x00000008	/* enable echoing */
+#define	ECHONL		0x00000010	/* echo NL even if ECHO is off */
+#if __BSD_VISIBLE
+#define	ECHOPRT		0x00000020	/* visual erase mode for hardcopy */
+#define	ECHOCTL  	0x00000040	/* echo control chars as ^(Char) */
+#endif  /*__BSD_VISIBLE */
+#define	ISIG		0x00000080	/* enable signals INTR, QUIT, [D]SUSP */
+#define	ICANON		0x00000100	/* canonicalize input lines */
+#if __BSD_VISIBLE
+#define	ALTWERASE	0x00000200	/* use alternate WERASE algorithm */
+#endif  /*__BSD_VISIBLE */
+#define	IEXTEN		0x00000400	/* enable DISCARD and LNEXT */
+#if __BSD_VISIBLE
+#define	EXTPROC         0x00000800      /* external processing */
+#endif	/* __BSD_VISIBLE */
+#define	TOSTOP		0x00400000	/* stop background jobs from output */
+#if __BSD_VISIBLE
+#define	FLUSHO		0x00800000	/* output being flushed (state) */
+#define	NOKERNINFO	0x02000000	/* no kernel output from VSTATUS */
+#define	PENDIN		0x20000000	/* XXX retype pending input (state) */
+#endif  /*__BSD_VISIBLE */
+#define	NOFLSH		0x80000000	/* don't flush after interrupt */
+
+/*
+ * Standard speeds
+ */
+#define	B0	0
+#define	B50	50
+#define	B75	75
+#define	B110	110
+#define	B134	134
+#define	B150	150
+#define	B200	200
+#define	B300	300
+#define	B600	600
+#define	B1200	1200
+#define	B1800	1800
+#define	B2400	2400
+#define	B4800	4800
+#define	B9600	9600
+#define	B19200	19200
+#define	B38400	38400
+#if __BSD_VISIBLE
+#define	B7200	7200
+#define	B14400	14400
+#define	B28800	28800
+#define	B57600	57600
+#define	B76800	76800
+#define	B115200	115200
+#define	B230400	230400
+#define	B460800	460800
+#define	B921600	921600
+#define	EXTA	19200
+#define	EXTB	38400
+#endif  /* __BSD_VISIBLE */
+
+typedef	unsigned int	tcflag_t;
+typedef	unsigned char	cc_t;
+typedef	unsigned int	speed_t;
+
+struct termios {
+	tcflag_t	c_iflag;	/* input flags */
+	tcflag_t	c_oflag;	/* output flags */
+	tcflag_t	c_cflag;	/* control flags */
+	tcflag_t	c_lflag;	/* local flags */
+	cc_t		c_cc[NCCS];	/* control chars */
+	speed_t		c_ispeed;	/* input speed */
+	speed_t		c_ospeed;	/* output speed */
+};
+
+#endif /* !_SYS__TERMIOS_H_ */
diff --git a/sys/sys/ioctl_compat.h b/sys/sys/ioctl_compat.h
index f188bd8..dd70fbf 100644
--- a/sys/sys/ioctl_compat.h
+++ b/sys/sys/ioctl_compat.h
@@ -33,7 +33,6 @@
  *
  *	@(#)ioctl_compat.h	8.4 (Berkeley) 1/21/94
  * $FreeBSD: src/sys/sys/ioctl_compat.h,v 1.5 1999/08/28 00:51:48 peter Exp $
- * $DragonFly: src/sys/sys/ioctl_compat.h,v 1.2 2003/06/17 04:28:58 dillon Exp $
  */
 
 #ifndef _SYS_IOCTL_COMPAT_H_
@@ -161,7 +160,6 @@ struct sgttyb {
 #define OTIOCCONS	_IO('t', 98)	/* for hp300 -- sans int arg */
 #define	OTTYDISC	0
 #define	NETLDISC	1
-#define	NTTYDISC	2
 #define IOCTLTRIM	_IOW('t', 128, off_t[2])
 
 #endif /* !_SYS_IOCTL_COMPAT_H_ */
diff --git a/sys/sys/termios.h b/sys/sys/termios.h
index ce45355..2fd5cfa 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,182 +27,29 @@
  * 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_
 #define	_SYS_TERMIOS_H_
 
+#include <sys/cdefs.h>
+#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
 
-/*
- * Special Control Characters
- *
- * Index into c_cc[] character array.
- *
- *	Name	     Subscript	Enabled by
- */
-#define	VEOF		0	/* ICANON */
-#define	VEOL		1	/* ICANON */
-#if __BSD_VISIBLE
-#define	VEOL2		2	/* ICANON together with IEXTEN */
-#endif
-#define	VERASE		3	/* ICANON */
-#if __BSD_VISIBLE
-#define	VWERASE 	4	/* ICANON together with IEXTEN */
-#endif
-#define	VKILL		5	/* ICANON */
-#if __BSD_VISIBLE
-#define	VREPRINT 	6	/* ICANON together with IEXTEN */
-#define	VERASE2 	7	/* ICANON */
-#endif
-#define	VINTR		8	/* ISIG */
-#define	VQUIT		9	/* ISIG */
-#define	VSUSP		10	/* ISIG */
-#if __BSD_VISIBLE
-#define	VDSUSP		11	/* ISIG together with IEXTEN */
-#endif
-#define	VSTART		12	/* IXON, IXOFF */
-#define	VSTOP		13	/* IXON, IXOFF */
-#if __BSD_VISIBLE
-#define	VLNEXT		14	/* IEXTEN */
-#define	VDISCARD	15	/* IEXTEN */
-#endif
-#define	VMIN		16	/* !ICANON */
-#define	VTIME		17	/* !ICANON */
-#if __BSD_VISIBLE
-#define	VSTATUS		18	/* ICANON together with IEXTEN */
-#define	VCHECKPT	19	/* ICANON together with IEXTEN */
-#endif
-#define	NCCS		20
-
-#define	_POSIX_VDISABLE	0xff
-
-#if __BSD_VISIBLE
-#define	CCEQ(val, c)	((c) == (val) ? (val) != _POSIX_VDISABLE : 0)
-#endif
-
-/*
- * Input flags - software input processing
- */
-#define	IGNBRK		0x00000001	/* ignore BREAK condition */
-#define	BRKINT		0x00000002	/* map BREAK to SIGINTR */
-#define	IGNPAR		0x00000004	/* ignore (discard) parity errors */
-#define	PARMRK		0x00000008	/* mark parity and framing errors */
-#define	INPCK		0x00000010	/* enable checking of parity errors */
-#define	ISTRIP		0x00000020	/* strip 8th bit off chars */
-#define	INLCR		0x00000040	/* map NL into CR */
-#define	IGNCR		0x00000080	/* ignore CR */
-#define	ICRNL		0x00000100	/* map CR to NL (ala CRMOD) */
-#define	IXON		0x00000200	/* enable output flow control */
-#define	IXOFF		0x00000400	/* enable input flow control */
-#if __XSI_VISIBLE || __POSIX_VISIBLE >= 200809
-#define	IXANY		0x00000800	/* any char will restart after stop */
-#endif
-#if __BSD_VISIBLE
-#define	IMAXBEL		0x00002000	/* ring bell on input queue full */
-#endif  /*__BSD_VISIBLE */
-
-/*
- * Output flags - software output processing
- */
-#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	OXTABS		TAB3
 #define	ONOEOT		0x00000008	/* discard EOT's (^D) on output) */
-#endif  /*__BSD_VISIBLE */
-#if __XSI_VISIBLE
-#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 */
-#endif
-
-/*
- * Control flags - hardware control of terminal
- */
-#if __BSD_VISIBLE
-#define	CIGNORE		0x00000001	/* ignore control flags */
-#endif
-#define	CSIZE		0x00000300	/* character size mask */
-#define	    CS5		    0x00000000	    /* 5 bits (pseudo) */
-#define	    CS6		    0x00000100	    /* 6 bits */
-#define	    CS7		    0x00000200	    /* 7 bits */
-#define	    CS8		    0x00000300	    /* 8 bits */
-#define	CSTOPB		0x00000400	/* send 2 stop bits */
-#define	CREAD		0x00000800	/* enable receiver */
-#define	PARENB		0x00001000	/* parity enable */
-#define	PARODD		0x00002000	/* odd parity, else even */
-#define	HUPCL		0x00004000	/* hang up on last close */
-#define	CLOCAL		0x00008000	/* ignore modem status lines */
-#if __BSD_VISIBLE
-#define	CCTS_OFLOW	0x00010000	/* CTS flow control of output */
-#define	CRTSCTS		(CCTS_OFLOW | CRTS_IFLOW)
-#define	CRTS_IFLOW	0x00020000	/* RTS flow control of input */
-#define	CDTR_IFLOW	0x00040000	/* DTR flow control of input */
-#define	CDSR_OFLOW	0x00080000	/* DSR flow control of output */
-#define	CCAR_OFLOW	0x00100000	/* DCD flow control of output */
-#define	MDMBUF		0x00100000	/* old name for CCAR_OFLOW */
+#define	MDMBUF		CCAR_OFLOW
+#define	CCEQ(val, c)	((c) == (val) ? (val) != _POSIX_VDISABLE : 0)
 #endif
 
-
-/*
- * "Local" flags - dumping ground for other state
- *
- * Warning: some flags in this structure begin with
- * the letter "I" and look like they belong in the
- * input flag.
- */
-
-#if __BSD_VISIBLE
-#define	ECHOKE		0x00000001	/* visual erase for line kill */
-#endif  /*__BSD_VISIBLE */
-#define	ECHOE		0x00000002	/* visually erase chars */
-#define	ECHOK		0x00000004	/* echo NL after line kill */
-#define	ECHO		0x00000008	/* enable echoing */
-#define	ECHONL		0x00000010	/* echo NL even if ECHO is off */
-#if __BSD_VISIBLE
-#define	ECHOPRT		0x00000020	/* visual erase mode for hardcopy */
-#define	ECHOCTL  	0x00000040	/* echo control chars as ^(Char) */
-#endif  /*__BSD_VISIBLE */
-#define	ISIG		0x00000080	/* enable signals INTR, QUIT, [D]SUSP */
-#define	ICANON		0x00000100	/* canonicalize input lines */
-#if __BSD_VISIBLE
-#define	ALTWERASE	0x00000200	/* use alternate WERASE algorithm */
-#endif  /*__BSD_VISIBLE */
-#define	IEXTEN		0x00000400	/* enable DISCARD and LNEXT */
-#if __BSD_VISIBLE
-#define	EXTPROC         0x00000800      /* external processing */
-#endif	/* __BSD_VISIBLE */
-#define	TOSTOP		0x00400000	/* stop background jobs from output */
-#if __BSD_VISIBLE
-#define	FLUSHO		0x00800000	/* output being flushed (state) */
-#define	NOKERNINFO	0x02000000	/* no kernel output from VSTATUS */
-#define	PENDIN		0x20000000	/* XXX retype pending input (state) */
-#endif  /*__BSD_VISIBLE */
-#define	NOFLSH		0x80000000	/* don't flush after interrupt */
-
-typedef	unsigned int	tcflag_t;
-typedef	unsigned char	cc_t;
-typedef	unsigned int	speed_t;
-
-struct termios {
-	tcflag_t	c_iflag;	/* input flags */
-	tcflag_t	c_oflag;	/* output flags */
-	tcflag_t	c_cflag;	/* control flags */
-	tcflag_t	c_lflag;	/* local flags */
-	cc_t		c_cc[NCCS];	/* control chars */
-	speed_t		c_ispeed;	/* input speed */
-	speed_t		c_ospeed;	/* output speed */
-};
-
 /*
  * Commands passed to tcsetattr() for setting the termios structure.
  */
@@ -213,37 +60,6 @@ struct termios {
 #define	TCSASOFT	0x10		/* flag - don't alter h.w. state */
 #endif
 
-/*
- * Standard speeds
- */
-#define	B0	0
-#define	B50	50
-#define	B75	75
-#define	B110	110
-#define	B134	134
-#define	B150	150
-#define	B200	200
-#define	B300	300
-#define	B600	600
-#define	B1200	1200
-#define	B1800	1800
-#define	B2400	2400
-#define	B4800	4800
-#define	B9600	9600
-#define	B19200	19200
-#define	B38400	38400
-#if __BSD_VISIBLE
-#define	B7200	7200
-#define	B14400	14400
-#define	B28800	28800
-#define	B57600	57600
-#define	B76800	76800
-#define	B115200	115200
-#define	B230400	230400
-#define	EXTA	19200
-#define	EXTB	38400
-#endif  /* __BSD_VISIBLE */
-
 #define	TCIFLUSH	1
 #define	TCOFLUSH	2
 #define	TCIOFLUSH	3
@@ -252,8 +68,6 @@ struct termios {
 #define	TCIOFF		3
 #define	TCION		4
 
-#include <sys/cdefs.h>
-
 __BEGIN_DECLS
 speed_t	cfgetispeed(const struct termios *);
 speed_t	cfgetospeed(const struct termios *);
@@ -271,26 +85,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/tty.h b/sys/sys/tty.h
index 1f85ebc..8346744 100644
--- a/sys/sys/tty.h
+++ b/sys/sys/tty.h
@@ -33,14 +33,13 @@
  *
  *	@(#)tty.h	8.6 (Berkeley) 1/21/94
  * $FreeBSD: src/sys/sys/tty.h,v 1.53.2.1 2001/02/26 04:23:21 jlemon Exp $
- * $DragonFly: src/sys/sys/tty.h,v 1.12 2006/09/10 01:26:40 dillon Exp $
  */
 
 #ifndef _SYS_TTY_H_
 #define	_SYS_TTY_H_
 
 #ifndef _SYS_TERMIOS_H_
-#include <sys/termios.h>
+#include <sys/_termios.h>
 #endif
 #ifndef _SYS_EVENT_H_
 #include <sys/event.h>
@@ -48,6 +47,7 @@
 #ifdef _KERNEL
 #include <sys/device.h>
 #endif
+#include <sys/ttycom.h>
 
 /*
  * Clists are character lists, which is a variable length linked list
diff --git a/sys/sys/ttycom.h b/sys/sys/ttycom.h
index 5940fb9..06cc012 100644
--- a/sys/sys/ttycom.h
+++ b/sys/sys/ttycom.h
@@ -33,7 +33,6 @@
  *
  *	@(#)ttycom.h	8.1 (Berkeley) 3/28/94
  * $FreeBSD: src/sys/sys/ttycom.h,v 1.16 2000/01/29 16:47:35 peter Exp $
- * $DragonFly: src/sys/sys/ttycom.h,v 1.3 2007/12/30 20:02:57 hasso Exp $
  */
 
 #ifndef	_SYS_TTYCOM_H_
@@ -131,6 +130,7 @@ struct winsize {
 #define	TIOCISPTMASTER	_IO('t', 85)	/* is pty master */
 
 #define	TTYDISC		0		/* termios tty line discipline */
+#define	NTTYDISC	2		/* XXX temporary */
 #define	SLIPDISC	4		/* serial IP discipline */
 #define	PPPDISC		5		/* PPP discipline */
 #define	NETGRAPHDISC	6		/* Netgraph tty node discipline */
diff --git a/sys/sys/ttydefaults.h b/sys/sys/ttydefaults.h
index 7cad640..2f8d514 100644
--- a/sys/sys/ttydefaults.h
+++ b/sys/sys/ttydefaults.h
@@ -32,8 +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 $
- * $DragonFly: src/sys/sys/ttydefaults.h,v 1.3 2003/10/13 21:08:50 dillon Exp $
+ * $FreeBSD: head/sys/sys/ttydefaults.h 249311 2013-04-09 16:16:34Z ed $
  */
 
 /*
@@ -46,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 */
@@ -87,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, _POSIX_VDISABLE
+
+#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 */
diff --git a/usr.sbin/ppp/physical.c b/usr.sbin/ppp/physical.c
index 238349b..0ab3e62 100644
--- a/usr.sbin/ppp/physical.c
+++ b/usr.sbin/ppp/physical.c
@@ -38,6 +38,7 @@
 #include <string.h>
 #include <sys/tty.h>	/* TIOCOUTQ */
 #include <sys/uio.h>
+#include <termios.h>
 #include <time.h>
 #include <unistd.h>
 #include <utmp.h>
-- 
2.9.2

