BSD 4_3_Net_2 release
[unix-history] / usr / src / sys / kern / tty.c
index 716818f..1f9b8df 100644 (file)
@@ -1,14 +1,41 @@
-/*
- * 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 The Regents of the University of California.
+ * Copyright (c) 1991 The Regents of the University of California.
+ * 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. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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.
  *
  *
- *     @(#)tty.c       7.35 (Berkeley) 7/27/90
+ *     @(#)tty.c       7.44 (Berkeley) 5/28/91
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "user.h"
 #include "ioctl.h"
 #define TTYDEFCHARS
 #include "tty.h"
 #include "ioctl.h"
 #define TTYDEFCHARS
 #include "tty.h"
@@ -22,7 +49,9 @@
 #include "vnode.h"
 #include "syslog.h"
 
 #include "vnode.h"
 #include "syslog.h"
 
-#include "machine/reg.h"
+#include "vm/vm.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";
@@ -38,49 +67,76 @@ 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 */
-extern char partab[], maptab[];
 
 /*
  * Is 'c' a line delimiter ("break" character)?
 
 /*
  * Is 'c' a line delimiter ("break" character)?
@@ -91,11 +147,12 @@ extern char partab[], maptab[];
 ttychars(tp)
        struct tty *tp;
 {
 ttychars(tp)
        struct tty *tp;
 {
+
        bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
 }
 
 /*
        bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
 }
 
 /*
- * 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;
@@ -128,8 +185,15 @@ ttywait(tp)
        return (error);
 }
 
        return (error);
 }
 
+#define        flushq(qq) { \
+       register struct clist *q = qq; \
+       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;
@@ -138,23 +202,23 @@ ttyflush(tp, rw)
 
        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;
                (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
                tp->t_state &= ~TS_TTSTOP;
                (*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;
+               flushq(&tp->t_outq);
+               wakeup((caddr_t)&tp->t_outq);
+               if (tp->t_wsel) {
+                       selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
+                       tp->t_wsel = 0;
+                       tp->t_state &= ~TS_WCOLL;
+               }
        }
        splx(s);
 }
        }
        splx(s);
 }
@@ -186,13 +250,15 @@ ttyblock(tp)
        }
 }
 
        }
 }
 
-/*
- * Restart typewriter output following a delay
- * timeout.
- * The name of the routine is passed to the timeout
- * subroutine and it is called during a clock interrupt.
- */
-ttrstrt(tp)
+ttstart(tp)
+       struct tty *tp;
+{
+
+       if (tp->t_oproc)                /* kludge for pty */
+               (*tp->t_oproc)(tp);
+}
+
+ttrstrt(tp)                            /* XXX */
        struct tty *tp;
 {
 
        struct tty *tp;
 {
 
@@ -204,28 +270,19 @@ ttrstrt(tp)
        ttstart(tp);
 }
 
        ttstart(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, and after a timeout has finished.
- */
-ttstart(tp)
-       struct tty *tp;
-{
-
-       if (tp->t_oproc)                /* kludge for pty */
-               (*tp->t_oproc)(tp);
-}
 
 /*
 
 /*
- * 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;
        caddr_t data;
 {
  */
 /*ARGSUSED*/
 ttioctl(tp, com, data, flag)
        register struct tty *tp;
        caddr_t data;
 {
+       register struct proc *p = curproc;              /* XXX */
        extern int nldisp;
        int s, error;
 
        extern int nldisp;
        int s, error;
 
@@ -253,12 +310,11 @@ ttioctl(tp, com, data, flag)
        case TIOCLSET:
        case OTIOCSETD:
 #endif
        case TIOCLSET:
        case OTIOCSETD:
 #endif
-               while (isbackground(u.u_procp, tp) && 
-                  u.u_procp->p_pgrp->pg_jobc &&
-                  (u.u_procp->p_flag&SVFORK) == 0 &&
-                  (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 &&
-                  (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0) {
-                       pgsignal(u.u_procp->p_pgrp, SIGTTOU, 1);
+               while (isbackground(curproc, tp) && 
+                  p->p_pgrp->pg_jobc && (p->p_flag&SPPWAIT) == 0 &&
+                  (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
+                  (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
+                       pgsignal(p->p_pgrp, SIGTTOU, 1);
                        if (error = ttysleep(tp, (caddr_t)&lbolt, 
                            TTOPRI | PCATCH, ttybg, 0)) 
                                return (error);
                        if (error = ttysleep(tp, (caddr_t)&lbolt, 
                            TTOPRI | PCATCH, ttybg, 0)) 
                                return (error);
@@ -285,7 +341,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);
@@ -364,9 +420,9 @@ ttioctl(tp, com, data, flag)
         * Simulate typing of a character at the terminal.
         */
        case TIOCSTI:
         * Simulate typing of a character at the terminal.
         */
        case TIOCSTI:
-               if (u.u_uid && (flag & FREAD) == 0)
+               if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
                        return (EPERM);
                        return (EPERM);
-               if (u.u_uid && !isctty(u.u_procp, tp))
+               if (p->p_ucred->cr_uid && !isctty(p, tp))
                        return (EACCES);
                (*linesw[tp->t_line].l_rint)(*(char *)data, tp);
                break;
                        return (EACCES);
                (*linesw[tp->t_line].l_rint)(*(char *)data, tp);
                break;
@@ -447,9 +503,7 @@ ttioctl(tp, com, data, flag)
         * Set controlling terminal.
         * Session ctty vnode pointer set in vnode layer.
         */
         * Set controlling terminal.
         * Session ctty vnode pointer set in vnode layer.
         */
-       case TIOCSCTTY: {
-               register struct proc *p = u.u_procp;
-
+       case TIOCSCTTY:
                if (!SESS_LEADER(p) || 
                   (p->p_session->s_ttyvp || tp->t_session) &&
                   (tp->t_session != p->p_session))
                if (!SESS_LEADER(p) || 
                   (p->p_session->s_ttyvp || tp->t_session) &&
                   (tp->t_session != p->p_session))
@@ -459,13 +513,11 @@ ttioctl(tp, com, data, flag)
                p->p_session->s_ttyp = tp;
                p->p_flag |= SCTTY;
                break;
                p->p_session->s_ttyp = tp;
                p->p_flag |= SCTTY;
                break;
-       }
                
        /*
         * Set terminal process group.
         */
        case TIOCSPGRP: {
                
        /*
         * Set terminal process group.
         */
        case TIOCSPGRP: {
-               register struct proc *p = u.u_procp;
                register struct pgrp *pgrp = pgfind(*(int *)data);
 
                if (!isctty(p, tp))
                register struct pgrp *pgrp = pgfind(*(int *)data);
 
                if (!isctty(p, tp))
@@ -477,7 +529,7 @@ ttioctl(tp, com, data, flag)
        }
 
        case TIOCGPGRP:
        }
 
        case TIOCGPGRP:
-               if (!isctty(u.u_procp, tp))
+               if (!isctty(p, tp))
                        return (ENOTTY);
                *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
                break;
                        return (ENOTTY);
                *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
                break;
@@ -501,7 +553,7 @@ ttioctl(tp, com, data, flag)
                            (TS_CARR_ON|TS_ISOPEN))
                                return (EBUSY);
 #ifndef        UCONSOLE
                            (TS_CARR_ON|TS_ISOPEN))
                                return (EBUSY);
 #ifndef        UCONSOLE
-                       if (error = suser(u.u_cred, &u.u_acflag))
+                       if (error = suser(p->p_ucred, &p->p_acflag))
                                return (error);
 #endif
                        constty = tp;
                                return (error);
 #endif
                        constty = tp;
@@ -509,26 +561,17 @@ ttioctl(tp, com, data, flag)
                        constty = NULL;
                break;
 
                        constty = NULL;
                break;
 
-#ifdef COMPAT_43
-       case TIOCGETP:
-       case TIOCSETP:
-       case TIOCSETN:
-       case TIOCGETC:
-       case TIOCSETC:
-       case TIOCSLTC:
-       case TIOCGLTC:
-       case TIOCLBIS:
-       case TIOCLBIC:
-       case TIOCLSET:
-       case TIOCLGET:
-       case OTIOCGETD:
-       case OTIOCSETD:
-       case OTIOCCONS:
-               return(ttcompat(tp, com, data, flag));
-#endif
+       case TIOCDRAIN:
+               if (error = ttywait(tp))
+                       return (error);
+               break;
 
        default:
 
        default:
+#ifdef COMPAT_43
+               return (ttcompat(tp, com, data, flag));
+#else
                return (-1);
                return (-1);
+#endif
        }
        return (0);
 }
        }
        return (0);
 }
@@ -564,7 +607,7 @@ ttselect(dev, rw)
                if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
                        tp->t_state |= TS_RCOLL;
                else
                if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
                        tp->t_state |= TS_RCOLL;
                else
-                       tp->t_rsel = u.u_procp;
+                       tp->t_rsel = curproc;
                break;
 
        case FWRITE:
                break;
 
        case FWRITE:
@@ -573,7 +616,7 @@ ttselect(dev, rw)
                if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
                        tp->t_state |= TS_WCOLL;
                else
                if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
                        tp->t_state |= TS_WCOLL;
                else
-                       tp->t_wsel = u.u_procp;
+                       tp->t_wsel = curproc;
                break;
        }
        splx(s);
                break;
        }
        splx(s);
@@ -584,7 +627,7 @@ 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;
  */
 ttyopen(dev, tp)
        dev_t dev;
@@ -604,15 +647,21 @@ ttyopen(dev, tp)
 /*
  * "close" a line discipline
  */
 /*
  * "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;
@@ -711,12 +760,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;
@@ -754,11 +798,8 @@ ttyinput(c, tp)
                        else if (iflag&BRKINT && lflag&ISIG && 
                                (cc[VINTR] != _POSIX_VDISABLE))
                                c = cc[VINTR];
                        else if (iflag&BRKINT && lflag&ISIG && 
                                (cc[VINTR] != _POSIX_VDISABLE))
                                c = cc[VINTR];
-                       else {
-                               c = 0;
-                               if (iflag&PARMRK)
-                                       goto parmrk;
-                       }
+                       else if (iflag&PARMRK)
+                               goto parmrk;
                } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) {
                        if (iflag&IGNPAR)
                                goto endcase;
                } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) {
                        if (iflag&IGNPAR)
                                goto endcase;
@@ -778,7 +819,7 @@ parmrk:
        if (iflag&IXOFF)
                ttyblock(tp);
        if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
        if (iflag&IXOFF)
                ttyblock(tp);
        if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
-               c &= 0177;
+               c &= ~0x80;
        if ((tp->t_lflag&EXTPROC) == 0) {
                /*
                 * Check for literal nexting very first
        if ((tp->t_lflag&EXTPROC) == 0) {
                /*
                 * Check for literal nexting very first
@@ -875,27 +916,7 @@ parmrk:
                } else if (c == '\n' && iflag&INLCR)
                        c = '\r';
        }
                } else if (c == '\n' && iflag&INLCR)
                        c = '\r';
        }
-       /*
-        * Non canonical mode; don't process line editing
-        * characters; check high water mark for wakeup.
-        * 
-        */
-       if ((lflag&ICANON) == 0) {
-               if (tp->t_rawq.c_cc > TTYHOG) {
-                       if (iflag&IMAXBEL) {
-                               if (tp->t_outq.c_cc < tp->t_hiwat)
-                                       (void) ttyoutput(CTRL('g'), tp);
-                       } else
-                               ttyflush(tp, FREAD | FWRITE);
-               } else {
-                       if (putc(c, &tp->t_rawq) >= 0) {
-                               ttwakeup(tp);
-                               ttyecho(c, tp);
-                       }
-               }
-               goto endcase;
-       }
-       if ((tp->t_lflag&EXTPROC) == 0) {
+       if ((tp->t_lflag&EXTPROC) == 0 && lflag&ICANON) {
                /*
                 * From here on down canonical mode character
                 * processing takes place.
                /*
                 * From here on down canonical mode character
                 * processing takes place.
@@ -932,8 +953,8 @@ parmrk:
                 */
                if (CCEQ(cc[VWERASE], c)) {     
                        int ctype;
                 */
                if (CCEQ(cc[VWERASE], c)) {     
                        int ctype;
+                       int alt = lflag&ALTWERASE;
 
 
-#define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0)
                        /* 
                         * erase whitespace 
                         */
                        /* 
                         * erase whitespace 
                         */
@@ -942,28 +963,26 @@ parmrk:
                        if (c == -1)
                                goto endcase;
                        /*
                        if (c == -1)
                                goto endcase;
                        /*
-                        * special case last char of token
+                        * erase last char of word and remember the
+                        * next chars type (for ALTWERASE)
                         */
                        ttyrub(c, tp);
                        c = unputc(&tp->t_rawq);
                         */
                        ttyrub(c, tp);
                        c = unputc(&tp->t_rawq);
-                       if (c == -1 || c == ' ' || c == '\t') {
-                               if (c != -1)
-                                       (void) putc(c, &tp->t_rawq);
+                       if (c == -1)
                                goto endcase;
                                goto endcase;
-                       }
+                       ctype = ISALPHA(c);
                        /*
                        /*
-                        * erase rest of token
+                        * erase rest of word
                         */
                         */
-                       ctype = CTYPE(c);
                        do {
                                ttyrub(c, tp);
                                c = unputc(&tp->t_rawq);
                                if (c == -1)
                                        goto endcase;
                        do {
                                ttyrub(c, tp);
                                c = unputc(&tp->t_rawq);
                                if (c == -1)
                                        goto endcase;
-                       } while (c != ' ' && c != '\t' && CTYPE(c) == ctype);
+                       } while (c != ' ' && c != '\t' && 
+                               (alt == 0 || ISALPHA(c) == ctype));
                        (void) putc(c, &tp->t_rawq);
                        goto endcase;
                        (void) putc(c, &tp->t_rawq);
                        goto endcase;
-#undef CTYPE
                }
                /*
                 * reprint line (^R)
                }
                /*
                 * reprint line (^R)
@@ -985,7 +1004,7 @@ parmrk:
        /*
         * Check for input buffer overflow
         */
        /*
         * Check for input buffer overflow
         */
-       if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) {
+       if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
                if (iflag&IMAXBEL) {
                        if (tp->t_outq.c_cc < tp->t_hiwat)
                                (void) ttyoutput(CTRL('g'), tp);
                if (iflag&IMAXBEL) {
                        if (tp->t_outq.c_cc < tp->t_hiwat)
                                (void) ttyoutput(CTRL('g'), tp);
@@ -998,6 +1017,11 @@ parmrk:
         * wakeup on seeing a line delimiter.
         */
        if (putc(c, &tp->t_rawq) >= 0) {
         * wakeup on seeing a line delimiter.
         */
        if (putc(c, &tp->t_rawq) >= 0) {
+               if ((lflag&ICANON) == 0) {
+                       ttwakeup(tp);
+                       ttyecho(c, tp);
+                       goto endcase;
+               }
                if (ttbreakc(c)) {
                        tp->t_rocount = 0;
                        catq(&tp->t_rawq, &tp->t_canq);
                if (ttbreakc(c)) {
                        tp->t_rocount = 0;
                        catq(&tp->t_rawq, &tp->t_canq);
@@ -1039,20 +1063,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;
        register long oflag = tp->t_oflag;
        
        if ((oflag&OPOST) == 0) {
        register long oflag = tp->t_oflag;
        
        if ((oflag&OPOST) == 0) {
@@ -1066,15 +1086,13 @@ ttyoutput(c, tp)
        }
        c &= TTY_CHARMASK;
        /*
        }
        c &= TTY_CHARMASK;
        /*
-        * 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
         * happen externally.
         */
         * 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
         * happen externally.
         */
-       if ((tp->t_lflag&EXTPROC) == 0 &&
-           c == '\t' && oflag&OXTABS ) {
+       if (c == '\t' && oflag&OXTABS && (tp->t_lflag&EXTPROC) == 0) {
                register int s;
 
                c = 8 - (tp->t_col&7);
                register int s;
 
                c = 8 - (tp->t_col&7);
@@ -1089,100 +1107,49 @@ ttyoutput(c, tp)
                return (c ? -1 : '\t');
        }
        if (c == CEOT && oflag&ONOEOT)
                return (c ? -1 : '\t');
        }
        if (c == CEOT && oflag&ONOEOT)
-               return(-1);
+               return (-1);
        tk_nout++;
        tp->t_outcc++;
        /*
        tk_nout++;
        tp->t_outcc++;
        /*
-        * turn <nl> to <cr><lf> if desired.
+        * Newline translation: if ONLCR is set,
+        * translate newline into "\r\n".
         */
        if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0)
                return (c);
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
                return (c);
         */
        if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0)
                return (c);
        if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
                return (c);
-       /*
-        * Calculate delays.
-        * The numbers here represent clock ticks
-        * and are not necessarily optimal for all terminals.
-        *
-        * SHOULD JUST ALLOW USER TO SPECIFY DELAYS
-        *
-        * (actually, should THROW AWAY terminals which need delays)
-        */
-       colp = &tp->t_col;
-       ctype = partab[c];
-       c = 0;
-       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;
 
                break;
 
-       /*
-        * This macro is close enough to the correct thing;
-        * it should be replaced by real user settable delays
-        * in any event...
-        */
-#define        mstohz(ms)      (((ms) * hz) >> 10)
        case NEWLINE:
        case NEWLINE:
-               ctype = (tp->t_flags >> 8) & 03;
-               if (ctype == 1) { /* tty 37 */
-                       if (*colp > 0) {
-                               c = (((unsigned)*colp) >> 4) + 3;
-                               if ((unsigned)c > 6)
-                                       c = 6;
-                       }
-               } else if (ctype == 2) /* vt05 */
-                       c = mstohz(100);
-               *colp = 0;
+               col = 0;
                break;
 
        case TAB:
                break;
 
        case TAB:
-               ctype = (tp->t_flags >> 10) & 03;
-               if (ctype == 1) { /* tty 37 */
-                       c = 1 - (*colp | ~07);
-                       if (c < 5)
-                               c = 0;
-               }
-               *colp |= 07;
-               (*colp)++;
-               break;
-
-       case VTAB:
-               if (tp->t_flags&VTDELAY) /* tty 37 */
-                       c = 0177;
+               col = (col + 8) &~ 0x7;
                break;
 
        case RETURN:
                break;
 
        case RETURN:
-               ctype = (tp->t_flags >> 12) & 03;
-               if (ctype == 1) /* tn 300 */
-                       c = mstohz(83);
-               else if (ctype == 2) /* ti 700 */
-                       c = mstohz(166);
-               else if (ctype == 3) { /* concept 100 */
-                       int i;
-
-                       if ((i = *colp) >= 0)
-                               for (; i < 9; i++)
-                                       (void) putc(0177, &tp->t_outq);
-               }
-               *colp = 0;
+               col = 0;
        }
        }
-       if (c && (tp->t_lflag&FLUSHO) == 0)
-               (void) putc(c|TTY_QUOTE, &tp->t_outq);
+       tp->t_col = col;
        return (-1);
 }
        return (-1);
 }
-#undef mstohz
 
 /*
 
 /*
- * 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;
  */
 ttread(tp, uio, flag)
        register struct tty *tp;
@@ -1192,6 +1159,7 @@ ttread(tp, uio, flag)
        register int c;
        register long lflag;
        register u_char *cc = tp->t_cc;
        register int c;
        register long lflag;
        register u_char *cc = tp->t_cc;
+       register struct proc *p = curproc;
        int s, first, error = 0;
 
 loop:
        int s, first, error = 0;
 
 loop:
@@ -1207,12 +1175,12 @@ loop:
        /*
         * Hang process if it's in the background.
         */
        /*
         * Hang process if it's in the background.
         */
-       if (isbackground(u.u_procp, tp)) {
-               if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) ||
-                  (u.u_procp->p_sigmask & sigmask(SIGTTIN)) ||
-                   u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0)
+       if (isbackground(p, tp)) {
+               if ((p->p_sigignore & sigmask(SIGTTIN)) ||
+                  (p->p_sigmask & sigmask(SIGTTIN)) ||
+                   p->p_flag&SPPWAIT || p->p_pgrp->pg_jobc == 0)
                        return (EIO);
                        return (EIO);
-               pgsignal(u.u_procp->p_pgrp, SIGTTIN, 1);
+               pgsignal(p->p_pgrp, SIGTTIN, 1);
                if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 
                    ttybg, 0)) 
                        return (error);
                if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 
                    ttybg, 0)) 
                        return (error);
@@ -1223,8 +1191,7 @@ loop:
         * If canonical, use the canonical queue,
         * else use the raw queue.
         *
         * If canonical, use the canonical queue,
         * else use the raw queue.
         *
-        * XXX - should get rid of canonical queue.
-        * (actually, should get rid of clists...)
+        * (should get rid of clists...)
         */
        qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq;
 
         */
        qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq;
 
@@ -1299,8 +1266,8 @@ loop:
         * the input queue has gone down.
         */
        if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
         * the input queue has gone down.
         */
        if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
-               if (cc[VSTART] != _POSIX_VDISABLE 
-                  && putc(cc[VSTART], &tp->t_outq) == 0) {
+               if (cc[VSTART] != _POSIX_VDISABLE &&
+                   putc(cc[VSTART], &tp->t_outq) == 0) {
                        tp->t_state &= ~TS_TBLOCK;
                        ttstart(tp);
                }
                        tp->t_state &= ~TS_TBLOCK;
                        ttstart(tp);
                }
@@ -1321,14 +1288,15 @@ ttycheckoutq(tp, wait)
        int wait;
 {
        int hiwat, s, oldsig;
        int wait;
 {
        int hiwat, s, oldsig;
+       extern int wakeup();
 
        hiwat = tp->t_hiwat;
        s = spltty();
 
        hiwat = tp->t_hiwat;
        s = spltty();
-       oldsig = u.u_procp->p_sig;
+       oldsig = curproc->p_sig;
        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);
-                       if (wait == 0 || u.u_procp->p_sig != oldsig) {
+                       if (wait == 0 || curproc->p_sig != oldsig) {
                                splx(s);
                                return (0);
                        }
                                splx(s);
                                return (0);
                        }
@@ -1341,8 +1309,7 @@ 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;
  */
 ttwrite(tp, uio, flag)
        register struct tty *tp;
@@ -1350,6 +1317,7 @@ ttwrite(tp, uio, flag)
 {
        register char *cp;
        register int cc = 0, ce;
 {
        register char *cp;
        register int cc = 0, ce;
+       register struct proc *p = curproc;
        int i, hiwat, cnt, error, s;
        char obuf[OBUFSIZ];
 
        int i, hiwat, cnt, error, s;
        char obuf[OBUFSIZ];
 
@@ -1382,12 +1350,12 @@ loop:
        /*
         * Hang the process if it's in the background.
         */
        /*
         * Hang the process if it's in the background.
         */
-       if (isbackground(u.u_procp, tp) && 
-           (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
-           (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 &&
-           (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0 &&
-            u.u_procp->p_pgrp->pg_jobc) {
-               pgsignal(u.u_procp->p_pgrp, SIGTTOU, 1);
+       if (isbackground(p, tp) && 
+           tp->t_lflag&TOSTOP && (p->p_flag&SPPWAIT) == 0 &&
+           (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
+           (p->p_sigmask & sigmask(SIGTTOU)) == 0 &&
+            p->p_pgrp->pg_jobc) {
+               pgsignal(p->p_pgrp, SIGTTOU, 1);
                if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 
                    ttybg, 0))
                        goto out;
                if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 
                    ttybg, 0))
                        goto out;
@@ -1433,7 +1401,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.
@@ -1545,7 +1513,7 @@ ttyrub(c, tp)
                }
                if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE))
                        ttyrubo(tp, 2);
                }
                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:
                        ttyrubo(tp, 1);
 
                case ORDINARY:
                        ttyrubo(tp, 1);
@@ -1555,7 +1523,7 @@ ttyrub(c, tp)
                case BACKSPACE:
                case CONTROL:
                case RETURN:
                case BACKSPACE:
                case CONTROL:
                case RETURN:
-               case NEWLINE:   /* XXX can't happen ? */
+               case NEWLINE:
                        if (tp->t_lflag&ECHOCTL)
                                ttyrubo(tp, 2);
                        break;
                        if (tp->t_lflag&ECHOCTL)
                                ttyrubo(tp, 2);
                        break;
@@ -1595,7 +1563,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) {
@@ -1660,8 +1628,8 @@ ttyecho(c, tp)
 {
        if ((tp->t_state&TS_CNTTB) == 0)
                tp->t_lflag &= ~FLUSHO;
 {
        if ((tp->t_state&TS_CNTTB) == 0)
                tp->t_lflag &= ~FLUSHO;
-       if (((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 ||
-                                         c == '\n')) || (tp->t_lflag&EXTPROC))
+       if (((tp->t_lflag&ECHO) == 0 &&
+           ((tp->t_lflag&ECHONL) == 0 || c == '\n')) || (tp->t_lflag&EXTPROC))
                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' ||
@@ -1690,8 +1658,11 @@ ttyoutstr(cp, tp)
                (void) ttyoutput(c, tp);
 }
 
                (void) ttyoutput(c, tp);
 }
 
+/*
+ * Wake up any readers on a tty.
+ */
 ttwakeup(tp)
 ttwakeup(tp)
-       struct tty *tp;
+       register struct tty *tp;
 {
 
        if (tp->t_rsel) {
 {
 
        if (tp->t_rsel) {
@@ -1704,6 +1675,20 @@ ttwakeup(tp)
        wakeup((caddr_t)&tp->t_rawq);
 }
 
        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)
+       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
  *
@@ -1725,117 +1710,67 @@ ttsetwater(tp)
 #undef clamp
 }
 
 #undef clamp
 }
 
-ttspeedtab(speed, table)
-       struct speedtab table[];
-{
-       register int i;
-
-       for (i = 0; table[i].sp_speed != -1; i++)
-               if (table[i].sp_speed == speed)
-                       return(table[i].sp_code);
-       return(-1);
-}
-
-int ttyhostname = 0;
 /*
 /*
- * (^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;
-       register char *cp = hostname;
-       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;
-       /* 
-        * hostname 
-        */
-       if (ttyhostname) {
-               if (*cp == '\0')
-                       ttyprintf(tp, "amnesia");
-               else
-                       while (*cp && *cp != '.')
-                               tputchar(*cp++, tp);
-               tputchar(' ');
-       }
-       /* 
-        * 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[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 
+
+               ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
+                   pick->p_stat == SRUN ? "running" :
+                   pick->p_wmesg ? pick->p_wmesg : "iowait");
+
+               /*
+                * Lock out clock if process is running; get user/system
+                * cpu time.
                 */
                 */
-               if (u.u_procp == pick)
-                       s = splclock();
+               if (curproc == pick)
+                       tmp = splclock();
                utime = pick->p_utime;
                stime = pick->p_stime;
                utime = pick->p_utime;
                stime = pick->p_stime;
-               if (u.u_procp == 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_ssize + pick->p_dsize));
+               if (curproc == pick)
+                       splx(tmp);
+
+               /* 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, pgtok(pick->p_vmspace->vm_rssize));
        }
        tp->t_rocount = 0;      /* so pending input will be retyped if BS */
 }
 
        }
        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;
-
-       while (--min >= 0 || n) {
-               *p++ = "0123456789abcdef"[n%base];
-               n /= base;
-       }
-       while (p > info)
-               ttyoutput(*--p, tp);
-}
-
 /*
  * Returns 1 if p2 is "better" than p1
  *
 /*
  * Returns 1 if p2 is "better" than p1
  *
@@ -1854,6 +1789,12 @@ ttyoutint(n, base, min, tp)
  *
  */
 #define isrun(p)       (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
  *
  */
 #define isrun(p)       (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
+#define TESTAB(a, b)    ((a)<<1 | (b))
+#define ONLYA   2
+#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;
 {
@@ -1863,12 +1804,12 @@ proc_compare(p1, p2)
        /*
         * see if at least one of them is runnable
         */
        /*
         * see if at least one of them is runnable
         */
-       switch (isrun(p1)<<1 | isrun(p2)) {
-       case 0x01:
-               return (1);
-       case 0x10:
+       switch (TESTAB(isrun(p1), isrun(p2))) {
+       case ONLYA:
                return (0);
                return (0);
-       case 0x11:
+       case ONLYB:
+               return (1);
+       case BOTH:
                /*
                 * tie - favor one with highest recent cpu utilization
                 */
                /*
                 * tie - favor one with highest recent cpu utilization
                 */
@@ -1878,6 +1819,17 @@ proc_compare(p1, p2)
                        return (0);
                return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
        }
                        return (0);
                return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
        }
+       /*
+        * weed out zombies
+        */
+       switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
+       case ONLYA:
+               return (1);
+       case ONLYB:
+               return (0);
+       case BOTH:
+               return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
+       }
        /* 
         * pick the one with the smallest sleep time
         */
        /* 
         * pick the one with the smallest sleep time
         */
@@ -1892,16 +1844,7 @@ proc_compare(p1, p2)
                return (1);
        if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
                return (0);
                return (1);
        if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
                return (0);
-       return(p2->p_pid > p1->p_pid);          /* tie - return highest pid */
-}
-#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);
+       return (p2->p_pid > p1->p_pid);         /* tie - return highest pid */
 }
 
 /*
 }
 
 /*
@@ -1913,8 +1856,7 @@ tputchar(c, tp)
 {
        register s = spltty();
 
 {
        register s = spltty();
 
-       if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN)) 
-           == (TS_CARR_ON | TS_ISOPEN)) {
+       if ((tp->t_state & (TS_CARR_ON|TS_ISOPEN)) == (TS_CARR_ON|TS_ISOPEN)) {
                if (c == '\n')
                        (void) ttyoutput('\r', tp);
                (void) ttyoutput(c, tp);
                if (c == '\n')
                        (void) ttyoutput('\r', tp);
                (void) ttyoutput(c, tp);
@@ -1927,9 +1869,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;