Cleanups for 4.4BSD-Lite
[unix-history] / usr / src / sys / kern / tty.c
index da18fe3..4b12b5e 100644 (file)
@@ -1,33 +1,36 @@
-/*
- * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+/*-
+ * Copyright (c) 1982, 1986, 1990, 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)tty.c       7.38 (Berkeley) %G%
+ *     @(#)tty.c       8.1 (Berkeley) %G%
  */
 
  */
 
-#include "param.h"
-#include "systm.h"
-#include "user.h"
-#include "ioctl.h"
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/ioctl.h>
+#include <sys/proc.h>
 #define TTYDEFCHARS
 #define TTYDEFCHARS
-#include "tty.h"
+#include <sys/tty.h>
 #undef TTYDEFCHARS
 #define TTYDEFCHARS
 #include "ttydefaults.h"
 #undef TTYDEFCHARS
 #include "termios.h"
 #undef TTYDEFCHARS
 #define TTYDEFCHARS
 #include "ttydefaults.h"
 #undef TTYDEFCHARS
 #include "termios.h"
-#include "proc.h"
-#include "file.h"
-#include "conf.h"
-#include "dkstat.h"
-#include "uio.h"
-#include "kernel.h"
-#include "vnode.h"
+#include <sys/file.h>
+#include <sys/conf.h>
+#include <sys/dkstat.h>
+#include <sys/uio.h>
+#include <sys/kernel.h>
+#include <sys/vnode.h>
+#include <sys/syslog.h>
+
+#include <vm/vm.h>
 #include "syslog.h"
 
 #include "syslog.h"
 
-#include "machine/reg.h"
-#include "syslog.h"
+static int proc_compare __P((struct proc *p1, struct proc *p2));
 
 /* symbolic sleep message strings */
 char ttyin[] = "ttyin";
 
 /* symbolic sleep message strings */
 char ttyin[] = "ttyin";
@@ -43,46 +46,74 @@ char ttybuf[] = "ttybuf";
  * indicates parity, the 7th bit indicates the character
  * is an alphameric or underscore (for ALTWERASE), and the 
  * low 6 bits indicate delay type.  If the low 6 bits are 0
  * indicates parity, the 7th bit indicates the character
  * is an alphameric or underscore (for ALTWERASE), and the 
  * low 6 bits indicate delay type.  If the low 6 bits are 0
- * then the character needs no special processing on output.
+ * then the character needs no special processing on output;
+ * classes other than 0 might be translated or (not currently)
+ * require delays.
  */
  */
+#define        PARITY(c)       (partab[c] & 0x80)
+#define        ISALPHA(c)      (partab[(c)&TTY_CHARMASK] & 0x40)
+#define        CCLASSMASK      0x3f
+#define        CCLASS(c)       (partab[c] & CCLASSMASK)
+
+#define        E       0x00    /* even parity */
+#define        O       0x80    /* odd parity */
+#define        ALPHA   0x40    /* alpha or underscore */
+
+#define        NO      ORDINARY
+#define        NA      ORDINARY|ALPHA
+#define        CC      CONTROL
+#define        BS      BACKSPACE
+#define        NL      NEWLINE
+#define        TB      TAB
+#define        VT      VTAB
+#define        CR      RETURN
 
 char partab[] = {
 
 char partab[] = {
-       0001,0201,0201,0001,0201,0001,0001,0201,        /* nul - bel */
-       0202,0004,0003,0201,0005,0206,0201,0001,        /* bs - si */
-       0201,0001,0001,0201,0001,0201,0201,0001,        /* dle - etb */
-       0001,0201,0201,0001,0201,0001,0001,0201,        /* can - us */
-       0200,0000,0000,0200,0000,0200,0200,0000,        /* sp - ' */
-       0000,0200,0200,0000,0200,0000,0000,0200,        /* ( - / */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* 0 - 7 */
-       0300,0100,0000,0200,0000,0200,0200,0000,        /* 8 - ? */
-       0200,0100,0100,0300,0100,0300,0300,0100,        /* @ - G */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* H - O */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* P - W */
-       0300,0100,0100,0200,0000,0200,0200,0300,        /* X - _ */
-       0000,0300,0300,0100,0300,0100,0100,0300,        /* ` - g */
-       0300,0100,0100,0300,0100,0300,0300,0100,        /* h - o */
-       0300,0100,0100,0300,0100,0300,0300,0100,        /* p - w */
-       0100,0300,0300,0000,0200,0000,0000,0201,        /* x - del */
+       E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
+       O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
+       O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
+       E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
+       O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
+       E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
+       E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
+       O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
+       O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
+       E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
+       E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
+       O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
+       E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
+       O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
+       O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
+       E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
        /*
        /*
-        * meta chars
+        * "meta" chars; should be settable per charset.
+        * For now, treat all as normal characters.
         */
         */
-       0001,0201,0201,0001,0201,0001,0001,0201,        /* nul - bel */
-       0202,0004,0003,0201,0005,0206,0201,0001,        /* bs - si */
-       0201,0001,0001,0201,0001,0201,0201,0001,        /* dle - etb */
-       0001,0201,0201,0001,0201,0001,0001,0201,        /* can - us */
-       0200,0000,0000,0200,0000,0200,0200,0000,        /* sp - ' */
-       0000,0200,0200,0000,0200,0000,0000,0200,        /* ( - / */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* 0 - 7 */
-       0300,0100,0000,0200,0000,0200,0200,0000,        /* 8 - ? */
-       0200,0100,0100,0300,0100,0300,0300,0100,        /* @ - G */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* H - O */
-       0100,0300,0300,0100,0300,0100,0100,0300,        /* P - W */
-       0300,0100,0100,0200,0000,0200,0200,0300,        /* X - _ */
-       0000,0300,0300,0100,0300,0100,0100,0300,        /* ` - g */
-       0300,0100,0100,0300,0100,0300,0300,0100,        /* h - o */
-       0300,0100,0100,0300,0100,0300,0300,0100,        /* p - w */
-       0100,0300,0300,0000,0200,0000,0000,0201,        /* x - del */
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
+       NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
 };
 };
+#undef NO
+#undef NA
+#undef CC
+#undef BS
+#undef NL
+#undef TB
+#undef VT
+#undef CR
 
 extern struct tty *constty;            /* temporary virtual console */
 
 
 extern struct tty *constty;            /* temporary virtual console */
 
@@ -112,7 +143,7 @@ ttychars(tp)
 
 /*
  *
 
 /*
  *
- * Wait for output to drain, then flush input waiting.
+ * Flush tty after output has drained.
  */
 ttywflush(tp)
        struct tty *tp;
  */
 ttywflush(tp)
        struct tty *tp;
@@ -148,33 +179,40 @@ ttywait(tp)
        return (error);
 }
 
        return (error);
 }
 
+#define        flushq(q) { \
+       if ((q)->c_cc) \
+               ndflush(q, (q)->c_cc); \
+}
+
 /*
 /*
- * Flush all TTY queues
+ * Flush TTY read and/or write queues,
+ * notifying anyone waiting.
  */
 ttyflush(tp, rw)
        register struct tty *tp;
  */
 ttyflush(tp, rw)
        register struct tty *tp;
+       int rw;
 {
 {
-       register s;
+       register int s;
 
        s = spltty();
        if (rw & FREAD) {
 
        s = spltty();
        if (rw & FREAD) {
-               while (getc(&tp->t_canq) >= 0)
-                       ;
+               flushq(&tp->t_canq);
+               flushq(&tp->t_rawq);
+               tp->t_rocount = 0;
+               tp->t_rocol = 0;
+               tp->t_state &= ~TS_LOCAL;
                ttwakeup(tp);
        }
        if (rw & FWRITE) {
                ttwakeup(tp);
        }
        if (rw & FWRITE) {
-               wakeup((caddr_t)&tp->t_outq); /* XXX? what about selwakeup? */
                tp->t_state &= ~TS_TTSTOP;
                tp->t_state &= ~TS_TTSTOP;
+#ifdef sun4c                                           /* XXX */
+               (*tp->t_stop)(tp, rw);
+#else
                (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
                (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
-               while (getc(&tp->t_outq) >= 0)
-                       ;
-       }
-       if (rw & FREAD) {
-               while (getc(&tp->t_rawq) >= 0)
-                       ;
-               tp->t_rocount = 0;
-               tp->t_rocol = 0;
-               tp->t_state &= ~TS_LOCAL;
+#endif
+               flushq(&tp->t_outq);
+               wakeup((caddr_t)&tp->t_outq);
+               selwakeup(&tp->t_wsel);
        }
        splx(s);
 }
        }
        splx(s);
 }
@@ -206,12 +244,6 @@ ttyblock(tp)
        }
 }
 
        }
 }
 
-/*
- * Start output on the typewriter. It is used from the top half
- * after some characters have been put on the output queue,
- * from the interrupt routine to transmit the next
- * character.
- */
 ttstart(tp)
        struct tty *tp;
 {
 ttstart(tp)
        struct tty *tp;
 {
@@ -220,26 +252,37 @@ ttstart(tp)
                (*tp->t_oproc)(tp);
 }
 
                (*tp->t_oproc)(tp);
 }
 
-ttrstrt(tp)                            /* XXX */
-       struct tty *tp;
+void
+ttrstrt(tp0)
+       void *tp0;
 {
 {
+       struct tty *tp;
+       int s;
 
 
+       tp = (struct tty *)tp0;
 #ifdef DIAGNOSTIC
        if (tp == 0)
                panic("ttrstrt");
 #endif
 #ifdef DIAGNOSTIC
        if (tp == 0)
                panic("ttrstrt");
 #endif
+       s = spltty();
        tp->t_state &= ~TS_TIMEOUT;
        ttstart(tp);
        tp->t_state &= ~TS_TIMEOUT;
        ttstart(tp);
+       splx(s);
 }
 
 
 /*
 }
 
 
 /*
- * Common code for tty ioctls.
+ * Common code for ioctls on tty devices.
+ * Called after line-discipline-specific ioctl
+ * has been called to do discipline-specific functions
+ * and/or reject any of these ioctl commands.
  */
 /*ARGSUSED*/
 ttioctl(tp, com, data, flag)
        register struct tty *tp;
  */
 /*ARGSUSED*/
 ttioctl(tp, com, data, flag)
        register struct tty *tp;
+       int com;
        caddr_t data;
        caddr_t data;
+       int flag;
 {
        register struct proc *p = curproc;              /* XXX */
        extern int nldisp;
 {
        register struct proc *p = curproc;              /* XXX */
        extern int nldisp;
@@ -259,7 +302,7 @@ ttioctl(tp, com, data, flag)
        case TIOCSETA:
        case TIOCSETAW:
        case TIOCSETAF:
        case TIOCSETA:
        case TIOCSETAW:
        case TIOCSETAF:
-#ifdef COMPAT_43
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
        case TIOCSETP:
        case TIOCSETN:
        case TIOCSETC:
        case TIOCSETP:
        case TIOCSETN:
        case TIOCSETC:
@@ -301,7 +344,7 @@ ttioctl(tp, com, data, flag)
                        return (ENXIO);
                if (t != tp->t_line) {
                        s = spltty();
                        return (ENXIO);
                if (t != tp->t_line) {
                        s = spltty();
-                       (*linesw[tp->t_line].l_close)(tp);
+                       (*linesw[tp->t_line].l_close)(tp, flag);
                        error = (*linesw[t].l_open)(dev, tp);
                        if (error) {
                                (void)(*linesw[tp->t_line].l_open)(dev, tp);
                        error = (*linesw[t].l_open)(dev, tp);
                        if (error) {
                                (void)(*linesw[tp->t_line].l_open)(dev, tp);
@@ -318,16 +361,23 @@ ttioctl(tp, com, data, flag)
 
        /* prevent more opens on channel */
        case TIOCEXCL:
 
        /* prevent more opens on channel */
        case TIOCEXCL:
+               s = spltty();
                tp->t_state |= TS_XCLUDE;
                tp->t_state |= TS_XCLUDE;
+               splx(s);
                break;
 
        case TIOCNXCL:
                break;
 
        case TIOCNXCL:
+               s = spltty();
                tp->t_state &= ~TS_XCLUDE;
                tp->t_state &= ~TS_XCLUDE;
+               splx(s);
                break;
 
                break;
 
+#ifdef TIOCHPCL
        case TIOCHPCL:
        case TIOCHPCL:
+               s = spltty();
                tp->t_cflag |= HUPCL;
                break;
                tp->t_cflag |= HUPCL;
                break;
+#endif
 
        case TIOCFLUSH: {
                register int flags = *(int *)data;
 
        case TIOCFLUSH: {
                register int flags = *(int *)data;
@@ -341,10 +391,12 @@ ttioctl(tp, com, data, flag)
        }
 
        case FIOASYNC:
        }
 
        case FIOASYNC:
+               s = spltty();
                if (*(int *)data)
                        tp->t_state |= TS_ASYNC;
                else
                        tp->t_state &= ~TS_ASYNC;
                if (*(int *)data)
                        tp->t_state |= TS_ASYNC;
                else
                        tp->t_state &= ~TS_ASYNC;
+               splx(s);
                break;
 
        case FIONBIO:
                break;
 
        case FIONBIO:
@@ -352,7 +404,7 @@ ttioctl(tp, com, data, flag)
 
        /* return number of characters immediately available */
        case FIONREAD:
 
        /* return number of characters immediately available */
        case FIONREAD:
-               *(off_t *)data = ttnread(tp);
+               *(int *)data = ttnread(tp);
                break;
 
        case TIOCOUTQ:
                break;
 
        case TIOCOUTQ:
@@ -363,7 +415,11 @@ ttioctl(tp, com, data, flag)
                s = spltty();
                if ((tp->t_state&TS_TTSTOP) == 0) {
                        tp->t_state |= TS_TTSTOP;
                s = spltty();
                if ((tp->t_state&TS_TTSTOP) == 0) {
                        tp->t_state |= TS_TTSTOP;
+#ifdef sun4c                                           /* XXX */
+                       (*tp->t_stop)(tp, 0);
+#else
                        (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
                        (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
+#endif
                }
                splx(s);
                break;
                }
                splx(s);
                break;
@@ -386,7 +442,7 @@ ttioctl(tp, com, data, flag)
                        return (EPERM);
                if (p->p_ucred->cr_uid && !isctty(p, tp))
                        return (EACCES);
                        return (EPERM);
                if (p->p_ucred->cr_uid && !isctty(p, tp))
                        return (EACCES);
-               (*linesw[tp->t_line].l_rint)(*(char *)data, tp);
+               (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
                break;
 
        case TIOCGETA: {
                break;
 
        case TIOCGETA: {
@@ -489,6 +545,11 @@ ttioctl(tp, com, data, flag)
                        constty = NULL;
                break;
 
                        constty = NULL;
                break;
 
+       case TIOCDRAIN:
+               if (error = ttywait(tp))
+                       return (error);
+               break;
+
        /* allow old ioctls for now */
        case TIOCGETP:
        case TIOCSETP:
        /* allow old ioctls for now */
        case TIOCGETP:
        case TIOCSETP:
@@ -504,7 +565,7 @@ ttioctl(tp, com, data, flag)
                return(ottioctl(tp, com, data, flag));
 
        default:
                return(ottioctl(tp, com, data, flag));
 
        default:
-#ifdef COMPAT_43
+#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
                return (ttcompat(tp, com, data, flag));
 #else
                return (-1);
                return (ttcompat(tp, com, data, flag));
 #else
                return (-1);
@@ -566,9 +627,10 @@ ttnread(tp)
        return (nread);
 }
 
        return (nread);
 }
 
-ttselect(dev, rw)
+ttselect(dev, rw, p)
        dev_t dev;
        int rw;
        dev_t dev;
        int rw;
+       struct proc *p;
 {
        register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
        int nread;
 {
        register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
        int nread;
@@ -581,19 +643,13 @@ ttselect(dev, rw)
                if (nread > 0 || 
                   ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
                        goto win;
                if (nread > 0 || 
                   ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
                        goto win;
-               if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
-                       tp->t_state |= TS_RCOLL;
-               else
-                       tp->t_rsel = curproc;
+               selrecord(p, &tp->t_rsel);
                break;
 
        case FWRITE:
                if (tp->t_outq.c_cc <= tp->t_lowat)
                        goto win;
                break;
 
        case FWRITE:
                if (tp->t_outq.c_cc <= tp->t_lowat)
                        goto win;
-               if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
-                       tp->t_state |= TS_WCOLL;
-               else
-                       tp->t_wsel = curproc;
+               selrecord(p, &tp->t_wsel);
                break;
        }
        splx(s);
                break;
        }
        splx(s);
@@ -604,12 +660,13 @@ win:
 }
 
 /*
 }
 
 /*
- * Initial open of tty, or (re)entry to line discipline.
+ * Initial open of tty, or (re)entry to standard tty line discipline.
  */
 ttyopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
  */
 ttyopen(dev, tp)
        dev_t dev;
        register struct tty *tp;
 {
+       int s = spltty();
 
        tp->t_dev = dev;
 
 
        tp->t_dev = dev;
 
@@ -622,21 +679,28 @@ ttyopen(dev, tp)
                 *  old (old) line discipline.
                 */
        }
                 *  old (old) line discipline.
                 */
        }
+       splx(s);
        return (0);
 }
 
 /*
  * "close" a line discipline
  */
        return (0);
 }
 
 /*
  * "close" a line discipline
  */
-ttylclose(tp)
-       register struct tty *tp;
+ttylclose(tp, flag)
+       struct tty *tp;
+       int flag;
 {
 
 {
 
-       ttywflush(tp);
+       if (flag&IO_NDELAY)
+               ttyflush(tp, FREAD|FWRITE);
+       else
+               ttywflush(tp);
 }
 
 /*
 }
 
 /*
- * clean tp on last close
+ * Handle close() on a tty line: flush and set to initial state,
+ * bumping generation number so that pending read/write calls
+ * can detect recycling of the tty.
  */
 ttyclose(tp)
        register struct tty *tp;
  */
 ttyclose(tp)
        register struct tty *tp;
@@ -658,6 +722,7 @@ ttyclose(tp)
  */
 ttymodem(tp, flag)
        register struct tty *tp;
  */
 ttymodem(tp, flag)
        register struct tty *tp;
+       int flag;
 {
 
        if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag & MDMBUF)) {
 {
 
        if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag & MDMBUF)) {
@@ -669,7 +734,11 @@ ttymodem(tp, flag)
                        ttstart(tp);
                } else if ((tp->t_state&TS_TTSTOP) == 0) {
                        tp->t_state |= TS_TTSTOP;
                        ttstart(tp);
                } else if ((tp->t_state&TS_TTSTOP) == 0) {
                        tp->t_state |= TS_TTSTOP;
+#ifdef sun4c                                           /* XXX */
+                       (*tp->t_stop)(tp, 0);
+#else
                        (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
                        (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
+#endif
                }
        } else if (flag == 0) {
                /*
                }
        } else if (flag == 0) {
                /*
@@ -735,13 +804,7 @@ ttypend(tp)
 }
 
 /*
 }
 
 /*
- *
- *
- * Place a character on raw TTY input queue,
- * putting in delimiters and waking up top
- * half as needed.  Also echo if required.
- * The arguments are the character and the
- * appropriate tty structure.
+ * Process input of a single character received on a tty.
  */
 ttyinput(c, tp)
        register c;
  */
 ttyinput(c, tp)
        register c;
@@ -804,7 +867,7 @@ ttyinput(c, tp)
         * In tandem mode, check high water mark.
         */
        if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
         * In tandem mode, check high water mark.
         */
        if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
-               c &= 0177;
+               c &= ~0x80;
 
        /*
         * Extensions to POSIX input modes which aren't controlled
 
        /*
         * Extensions to POSIX input modes which aren't controlled
@@ -907,7 +970,7 @@ ttyinput(c, tp)
                        /*
                         * Place the cursor over the '^' of the ^D.
                         */
                        /*
                         * Place the cursor over the '^' of the ^D.
                         */
-                       i = MIN(2, tp->t_col - i);
+                       i = min(2, tp->t_col - i);
                        while (i > 0) {
                                (void) ttyoutput('\b', tp);
                                i--;
                        while (i > 0) {
                                (void) ttyoutput('\b', tp);
                                i--;
@@ -930,20 +993,16 @@ startoutput:
 }
 
 /*
 }
 
 /*
- * Put character on TTY output queue, adding delays,
- * expanding tabs, and handling the CR/NL bit.
- * This is called both from the top half for output,
- * and from interrupt level for echoing.
- * The arguments are the character and the tty structure.
- * Returns < 0 if putc succeeds, otherwise returns char to resend
+ * Output a single character on a tty, doing output processing
+ * as needed (expanding tabs, newline processing, etc.).
+ * Returns < 0 if putc succeeds, otherwise returns char to resend.
  * Must be recursive.
  */
 ttyoutput(c, tp)
        register c;
        register struct tty *tp;
 {
  * Must be recursive.
  */
 ttyoutput(c, tp)
        register c;
        register struct tty *tp;
 {
-       register short *colp;
-       register ctype;
+       register int col;
        if (!(tp->t_oflag&OPOST)) {
                if (tp->t_lflag&FLUSHO) 
                        return (-1);
        if (!(tp->t_oflag&OPOST)) {
                if (tp->t_lflag&FLUSHO) 
                        return (-1);
@@ -955,8 +1014,7 @@ ttyoutput(c, tp)
        }
        c &= 0377;
        /*
        }
        c &= 0377;
        /*
-        * Turn tabs to spaces as required
-        *
+        * Do tab expansion if OXTABS is set.
         * Special case if we have 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
         * Special case if we have 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
@@ -982,7 +1040,8 @@ ttyoutput(c, tp)
        tp->t_outcc++;
 #ifdef notdef
        /*
        tp->t_outcc++;
 #ifdef notdef
        /*
-        * turn <nl> to <cr><lf> if desired.
+        * Newline translation: if ONLCR is set,
+        * translate newline into "\r\n".
         */
 #endif
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
         */
 #endif
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
@@ -990,43 +1049,42 @@ ttyoutput(c, tp)
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
                return (c);
 
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
                return (c);
 
-       colp = &tp->t_col;
-       ctype = partab[c];
-       switch (ctype&077) {
+       col = tp->t_col;
+       switch (CCLASS(c)) {
 
        case ORDINARY:
 
        case ORDINARY:
-               (*colp)++;
+               col++;
 
        case CONTROL:
                break;
 
        case BACKSPACE:
 
        case CONTROL:
                break;
 
        case BACKSPACE:
-               if (*colp)
-                       (*colp)--;
+               if (col > 0)
+                       col--;
                break;
 
        case NEWLINE:
                break;
 
        case NEWLINE:
-               *colp = 0;
+               col = 0;
                break;
 
        case TAB:
                break;
 
        case TAB:
-               *colp |= 07;
-               (*colp)++;
+               col = (col + 8) &~ 0x7;
                break;
 
        case RETURN:
                break;
 
        case RETURN:
-               *colp = 0;
+               col = 0;
        }
        }
+       tp->t_col = col;
        return (-1);
 }
 
 /*
        return (-1);
 }
 
 /*
- * Called from device's read routine after it has
- * calculated the tty-structure given as argument.
+ * Process a read call on a tty device.
  */
 ttread(tp, uio, flag)
        register struct tty *tp;
        struct uio *uio;
  */
 ttread(tp, uio, flag)
        register struct tty *tp;
        struct uio *uio;
+       int flag;
 {
        register struct clist *qp;
        register int c, t_flags;
 {
        register struct clist *qp;
        register int c, t_flags;
@@ -1138,6 +1196,7 @@ loop:
         * Look to unblock output now that (presumably)
         * the input queue has gone down.
         */
         * Look to unblock output now that (presumably)
         * the input queue has gone down.
         */
+       s = spltty();
        if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
                if (cc[VSTART] != POSIX_V_DISABLE 
                   && putc(cc[VSTART], &tp->t_outq) == 0) {
        if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
                if (cc[VSTART] != POSIX_V_DISABLE 
                   && putc(cc[VSTART], &tp->t_outq) == 0) {
@@ -1145,6 +1204,7 @@ loop:
                        ttstart(tp);
                }
        }
                        ttstart(tp);
                }
        }
+       splx(s);
        }
        return (error);
 }
        }
        return (error);
 }
@@ -1165,7 +1225,7 @@ ttycheckoutq(tp, wait)
 
        hiwat = tp->t_hiwat;
        s = spltty();
 
        hiwat = tp->t_hiwat;
        s = spltty();
-       oldsig = curproc->p_sig;
+       oldsig = wait ? curproc->p_sig : 0;
        if (tp->t_outq.c_cc > hiwat + 200)
                while (tp->t_outq.c_cc > hiwat) {
                        ttstart(tp);
        if (tp->t_outq.c_cc > hiwat + 200)
                while (tp->t_outq.c_cc > hiwat) {
                        ttstart(tp);
@@ -1173,7 +1233,8 @@ ttycheckoutq(tp, wait)
                                splx(s);
                                return (0);
                        }
                                splx(s);
                                return (0);
                        }
-                       timeout(wakeup, (caddr_t)&tp->t_outq, hz);
+                       timeout((void (*)__P((void *)))wakeup,
+                           (void *)&tp->t_outq, hz);
                        tp->t_state |= TS_ASLEEP;
                        sleep((caddr_t)&tp->t_outq, PZERO - 1);
                }
                        tp->t_state |= TS_ASLEEP;
                        sleep((caddr_t)&tp->t_outq, PZERO - 1);
                }
@@ -1182,12 +1243,12 @@ ttycheckoutq(tp, wait)
 }
 
 /*
 }
 
 /*
- * Called from the device's write routine after it has
- * calculated the tty-structure given as argument.
+ * Process a write call on a tty device.
  */
 ttwrite(tp, uio, flag)
        register struct tty *tp;
        register struct uio *uio;
  */
 ttwrite(tp, uio, flag)
        register struct tty *tp;
        register struct uio *uio;
+       int flag;
 {
        register char *cp;
        register int cc = 0, ce;
 {
        register char *cp;
        register int cc = 0, ce;
@@ -1276,7 +1337,7 @@ loop:
                                ce = cc;
                        else {
                                ce = cc - scanc((unsigned)cc, (u_char *)cp,
                                ce = cc;
                        else {
                                ce = cc - scanc((unsigned)cc, (u_char *)cp,
-                                  (u_char *)partab, 077);
+                                  (u_char *)partab, CCLASSMASK);
                                /*
                                 * If ce is zero, then we're processing
                                 * a special character through ttyoutput.
                                /*
                                 * If ce is zero, then we're processing
                                 * a special character through ttyoutput.
@@ -1389,7 +1450,7 @@ ttyrub(c, tp)
                /* if tab or newline was escaped  - XXX - not 8bit */
                if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE))
                        ttyrubo(tp, 2);
                /* if tab or newline was escaped  - XXX - not 8bit */
                if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE))
                        ttyrubo(tp, 2);
-               else switch (partab[c&=0377]&077) {
+               else switch (CCLASS(c &= TTY_CHARMASK)) {
 
                case ORDINARY:
 #ifdef notdef
 
                case ORDINARY:
 #ifdef notdef
@@ -1436,7 +1497,7 @@ ttyrub(c, tp)
                default:
                        /* XXX */
                        printf("ttyrub: would panic c = %d, val = %d\n",
                default:
                        /* XXX */
                        printf("ttyrub: would panic c = %d, val = %d\n",
-                               c, partab[c&=0377]&077);
+                               c, CCLASS(c));
                        /*panic("ttyrub");*/
                }
        } else if (tp->t_lflag&ECHOPRT) {
                        /*panic("ttyrub");*/
                }
        } else if (tp->t_lflag&ECHOPRT) {
@@ -1507,7 +1568,7 @@ ttyecho(c, tp)
                return;
        if (tp->t_lflag&ECHOCTL) {
                if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
                return;
        if (tp->t_lflag&ECHOCTL) {
                if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
-                   c == 0177) {
+                   (c&TTY_CHARMASK) == 0177) {
                        (void) ttyoutput('^', tp);
                        c &= TTY_CHARMASK;
                        if (c == 0177)
                        (void) ttyoutput('^', tp);
                        c &= TTY_CHARMASK;
                        if (c == 0177)
@@ -1531,20 +1592,34 @@ ttyoutstr(cp, tp)
                (void) ttyoutput(c, tp);
 }
 
                (void) ttyoutput(c, tp);
 }
 
+/*
+ * Wake up any readers on a tty.
+ */
 ttwakeup(tp)
        register struct tty *tp;
 {
 
 ttwakeup(tp)
        register struct tty *tp;
 {
 
-       if (tp->t_rsel) {
-               selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
-               tp->t_state &= ~TS_RCOLL;
-               tp->t_rsel = 0;
-       }
+       selwakeup(&tp->t_rsel);
        if (tp->t_state & TS_ASYNC)
                pgsignal(tp->t_pgrp, SIGIO, 1); 
        wakeup((caddr_t)&tp->t_rawq);
 }
 
        if (tp->t_state & TS_ASYNC)
                pgsignal(tp->t_pgrp, SIGIO, 1); 
        wakeup((caddr_t)&tp->t_rawq);
 }
 
+/*
+ * Look up a code for a specified speed in a conversion table;
+ * used by drivers to map software speed values to hardware parameters.
+ */
+ttspeedtab(speed, table)
+       int speed;
+       register struct speedtab *table;
+{
+
+       for ( ; table->sp_speed != -1; table++)
+               if (table->sp_speed == speed)
+                       return (table->sp_code);
+       return (-1);
+}
+
 /*
  * set tty hi and low water marks
  *
 /*
  * set tty hi and low water marks
  *
@@ -1567,90 +1642,57 @@ ttsetwater(tp)
 }
 
 /*
 }
 
 /*
- * (^T)
  * Report on state of foreground process group.
  */
 ttyinfo(tp)
  * Report on state of foreground process group.
  */
 ttyinfo(tp)
-       struct tty *tp;
+       register struct tty *tp;
 {
 {
-       register struct proc *p, *pick = NULL;
-       int x, s;
+       register struct proc *p, *pick;
        struct timeval utime, stime;
        struct timeval utime, stime;
-#define        pgtok(a)        (((a)*NBPG)/1024)
+       int tmp;
 
        if (ttycheckoutq(tp,0) == 0) 
                return;
 
        if (ttycheckoutq(tp,0) == 0) 
                return;
-       /* 
-        * load average 
-        */
-       x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT;
-       ttyprintf(tp, "load: %d.", x/100);
-       ttyoutint(x%100, 10, 2, tp);
+
+       /* Print load average. */
+       tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
+       ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
+
        if (tp->t_session == NULL)
        if (tp->t_session == NULL)
-               ttyprintf(tp, " not a controlling terminal\n");
+               ttyprintf(tp, "not a controlling terminal\n");
        else if (tp->t_pgrp == NULL)
        else if (tp->t_pgrp == NULL)
-               ttyprintf(tp, " no foreground process group\n");
+               ttyprintf(tp, "no foreground process group\n");
        else if ((p = tp->t_pgrp->pg_mem) == NULL)
        else if ((p = tp->t_pgrp->pg_mem) == NULL)
-               ttyprintf(tp, " empty foreground process group\n");
+               ttyprintf(tp, "empty foreground process group\n");
        else {
        else {
-               /* pick interesting process */
-               for (; p != NULL; p = p->p_pgrpnxt) {
+               /* Pick interesting process. */
+               for (pick = NULL; p != NULL; p = p->p_pgrpnxt)
                        if (proc_compare(pick, p))
                                pick = p;
                        if (proc_compare(pick, p))
                                pick = p;
-               }
-               ttyprintf(tp, "  cmd: %s %d [%s] ",
-                       pick->p_comm, pick->p_pid,
-                       pick->p_wmesg ? pick->p_wmesg : "running");
-               /* 
-                * cpu time 
-                */
-               if (curproc == pick)
-                       s = splclock();
-               utime = pick->p_utime;
-               stime = pick->p_stime;
-               if (curproc == pick)
-                       splx(s);
-               /* user time */
-               x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */
-               ttyoutint(utime.tv_sec, 10, 1, tp);
-               tputchar('.', tp);
-               ttyoutint(x, 10, 2, tp);
-               tputchar('u', tp);
-               tputchar(' ', tp);
-               /* system time */
-               x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */
-               ttyoutint(stime.tv_sec, 10, 1, tp);
-               tputchar('.', tp);
-               ttyoutint(x, 10, 2, tp);
-               tputchar('s', tp);
-               tputchar(' ', tp);
-               /* 
-                * pctcpu 
-                */
-               x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT;
-               ttyoutint(x/100, 10, 1, tp);
-#ifdef notdef  /* do we really want this ??? */
-               tputchar('.', tp);
-               ttyoutint(x%100, 10, 2, tp);
-#endif
-               ttyprintf(tp, "%% %dk\n", pgtok(pick->p_vmspace->vm_rssize));
-       }
-       tp->t_rocount = 0;      /* so pending input will be retyped if BS */
-}
 
 
-ttyoutint(n, base, min, tp)
-       register int n, base, min;
-       register struct tty *tp;
-{
-       char info[16];
-       register char *p = info;
+               ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
+                   pick->p_stat == SRUN ? "running" :
+                   pick->p_wmesg ? pick->p_wmesg : "iowait");
+
+               calcru(pick, &utime, &stime, NULL);
 
 
-       while (--min >= 0 || n) {
-               *p++ = "0123456789abcdef"[n%base];
-               n /= base;
+               /* Print user time. */
+               ttyprintf(tp, "%d.%02du ",
+                   utime.tv_sec, (utime.tv_usec + 5000) / 10000);
+
+               /* Print system time. */
+               ttyprintf(tp, "%d.%02ds ",
+                   stime.tv_sec, (stime.tv_usec + 5000) / 10000);
+
+#define        pgtok(a)        (((a) * NBPG) / 1024)
+               /* Print percentage cpu, resident set size. */
+               tmp = pick->p_pctcpu * 10000 + FSCALE / 2 >> FSHIFT;
+               ttyprintf(tp, "%d%% %dk\n",
+                   tmp / 100,
+                   pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 :
+                       pgtok(pick->p_vmspace->vm_rssize));
        }
        }
-       while (p > info)
-               ttyoutput(*--p, tp);
+       tp->t_rocount = 0;      /* so pending input will be retyped if BS */
 }
 
 /*
 }
 
 /*
@@ -1676,6 +1718,7 @@ ttyoutint(n, base, min, tp)
 #define ONLYB   1
 #define BOTH    3
 
 #define ONLYB   1
 #define BOTH    3
 
+static int
 proc_compare(p1, p2)
        register struct proc *p1, *p2;
 {
 proc_compare(p1, p2)
        register struct proc *p1, *p2;
 {
@@ -1728,17 +1771,6 @@ proc_compare(p1, p2)
        return (p2->p_pid > p1->p_pid);         /* tie - return highest pid */
 }
 
        return (p2->p_pid > p1->p_pid);         /* tie - return highest pid */
 }
 
-/* XXX move to subr_prf.c */
-#define TOTTY  0x2     /* XXX should be in header */
-/*VARARGS2*/
-ttyprintf(tp, fmt, x1)
-       struct tty *tp;
-       char *fmt;
-       unsigned x1;
-{
-       prf(fmt, &x1, TOTTY, (caddr_t)tp);
-}
-
 /*
  * Output char to tty; console putchar style.
  */
 /*
  * Output char to tty; console putchar style.
  */
@@ -1761,9 +1793,10 @@ tputchar(c, tp)
 }
 
 /*
 }
 
 /*
- * Sleep on chan.
- *
- * Return ERESTART if tty changed while we napped.
+ * Sleep on chan, returning ERESTART if tty changed
+ * while we napped and returning any errors (e.g. EINTR/ETIMEDOUT)
+ * reported by tsleep.  If the tty is revoked, restarting a pending
+ * call will redo validation done at the start of the call.
  */
 ttysleep(tp, chan, pri, wmesg, timo)
        struct tty *tp;
  */
 ttysleep(tp, chan, pri, wmesg, timo)
        struct tty *tp;