use modem entry in linesw to signal carrier change; split line-disc close
[unix-history] / usr / src / sys / kern / tty.c
index 8ab633d..08baeb4 100644 (file)
@@ -1,4 +1,10 @@
-/*     tty.c   6.15    85/04/17        */
+/*
+ * Copyright (c) 1982 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *     @(#)tty.c       6.22 (Berkeley) %G%
+ */
 
 #include "../machine/reg.h"
 
 
 #include "../machine/reg.h"
 
@@ -126,7 +132,7 @@ ttywait(tp)
        register int s = spltty();
 
        while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
        register int s = spltty();
 
        while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
-           tp->t_state&TS_CARR_ON && tp->t_oproc) {    /* kludge for pty */
+           tp->t_state&TS_CARR_ON) {
                (*tp->t_oproc)(tp);
                tp->t_state |= TS_ASLEEP;
                sleep((caddr_t)&tp->t_outq, TTOPRI);
                (*tp->t_oproc)(tp);
                tp->t_state |= TS_ASLEEP;
                sleep((caddr_t)&tp->t_outq, TTOPRI);
@@ -256,18 +262,16 @@ ttioctl(tp, com, data, flag)
        case TIOCLSET:
        case TIOCSTI:
        case TIOCSWINSZ:
        case TIOCLSET:
        case TIOCSTI:
        case TIOCSWINSZ:
-#define bit(a) (1<<(a-1))
                while (tp->t_line == NTTYDISC &&
                   u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
                   (u.u_procp->p_flag&SVFORK) == 0 &&
                while (tp->t_line == NTTYDISC &&
                   u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
                   (u.u_procp->p_flag&SVFORK) == 0 &&
-                  !(u.u_procp->p_sigignore & bit(SIGTTOU)) &&
-                  !(u.u_procp->p_sigmask & bit(SIGTTOU))) {
+                  !(u.u_procp->p_sigignore & sigmask(SIGTTOU)) &&
+                  !(u.u_procp->p_sigmask & sigmask(SIGTTOU))) {
                        gsignal(u.u_procp->p_pgrp, SIGTTOU);
                        sleep((caddr_t)&lbolt, TTOPRI);
                }
                break;
        }
                        gsignal(u.u_procp->p_pgrp, SIGTTOU);
                        sleep((caddr_t)&lbolt, TTOPRI);
                }
                break;
        }
-#undef bit
 
        /*
         * Process the ioctl.
 
        /*
         * Process the ioctl.
@@ -287,13 +291,10 @@ ttioctl(tp, com, data, flag)
                if ((unsigned) t >= nldisp)
                        return (ENXIO);
                s = spltty();
                if ((unsigned) t >= nldisp)
                        return (ENXIO);
                s = spltty();
-               if (tp->t_line)
-                       (*linesw[tp->t_line].l_close)(tp);
-               if (t)
-                       error = (*linesw[t].l_open)(dev, tp);
+               (*linesw[tp->t_line].l_close)(tp);
+               error = (*linesw[t].l_open)(dev, tp);
                if (error) {
                if (error) {
-                       if (tp->t_line)
-                               (void) (*linesw[tp->t_line].l_open)(dev, tp);
+                       (void) (*linesw[tp->t_line].l_open)(dev, tp);
                        splx(s);
                        return (error);
                }
                        splx(s);
                        return (error);
                }
@@ -532,7 +533,7 @@ ttselect(dev, rw)
 
        case FREAD:
                nread = ttnread(tp);
 
        case FREAD:
                nread = ttnread(tp);
-               if (nread > 0)
+               if ((nread > 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;
                        goto win;
                if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
                        tp->t_state |= TS_RCOLL;
@@ -557,6 +558,7 @@ win:
 }
 
 /*
 }
 
 /*
+ * Initial open of tty, or (re)entry to line discipline.
  * Establish a process group for distribution of
  * quits and interrupts from the tty.
  */
  * Establish a process group for distribution of
  * quits and interrupts from the tty.
  */
@@ -585,6 +587,17 @@ ttyopen(dev, tp)
        return (0);
 }
 
        return (0);
 }
 
+/*
+ * "close" a line discipline
+ */
+ttylclose(tp)
+       register struct tty *tp;
+{
+
+       ttywflush(tp);
+       tp->t_line = 0;
+}
+
 /*
  * clean tp on last close
  */
 /*
  * clean tp on last close
  */
@@ -592,16 +605,54 @@ ttyclose(tp)
        register struct tty *tp;
 {
 
        register struct tty *tp;
 {
 
-       if (tp->t_line) {
-               ttywflush(tp);
-               tp->t_line = 0;
-               return;
-       }
+       ttyflush(tp, FREAD|FWRITE);
        tp->t_pgrp = 0;
        tp->t_pgrp = 0;
-       ttywflush(tp);
        tp->t_state = 0;
 }
 
        tp->t_state = 0;
 }
 
+/*
+ * Handle modem control transition on a tty.
+ * Flag indicates new state of carrier.
+ * Returns 0 if the line should be turned off, otherwise 1.
+ */
+ttymodem(tp, flag)
+       register struct tty *tp;
+{
+
+       if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_flags & MDMBUF)) {
+               /*
+                * MDMBUF: do flow control according to carrier flag
+                */
+               if (flag) {
+                       tp->t_state &= ~TS_TTSTOP;
+                       ttstart(tp);
+               } else if ((tp->t_state&TS_TTSTOP) == 0) {
+                       tp->t_state |= TS_TTSTOP;
+                       (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
+               }
+       } else if (flag == 0) {
+               /*
+                * Lost carrier.
+                */
+               tp->t_state &= ~TS_CARR_ON;
+               if (tp->t_state & TS_ISOPEN) {
+                       if ((tp->t_flags & NOHANG) == 0) {
+                               gsignal(tp->t_pgrp, SIGHUP);
+                               gsignal(tp->t_pgrp, SIGCONT);
+                               ttyflush(tp, FREAD|FWRITE);
+                               return (0);
+                       }
+               }
+       } else {
+               /*
+                * Carrier now on.
+                */
+               tp->t_state |= TS_CARR_ON;
+               wakeup((caddr_t)&tp->t_rawq);
+       }
+       return (1);
+}
+
 /*
  * reinput pending characters after state switch
  * call at spltty().
 /*
  * reinput pending characters after state switch
  * call at spltty().
@@ -669,7 +720,7 @@ ttyinput(c, tp)
         * Ignore any high bit added during
         * previous ttyinput processing.
         */
         * Ignore any high bit added during
         * previous ttyinput processing.
         */
-       if ((tp->t_state&TS_TYPEN) == 0)
+       if ((tp->t_state&TS_TYPEN) == 0 && (t_flags&PASS8) == 0)
                c &= 0177;
        /*
         * Check for literal nexting very first
                c &= 0177;
        /*
         * Check for literal nexting very first
@@ -688,13 +739,13 @@ ttyinput(c, tp)
         */
        if (tp->t_line == NTTYDISC) {
                if (c == tp->t_lnextc) {
         */
        if (tp->t_line == NTTYDISC) {
                if (c == tp->t_lnextc) {
-                       if (tp->t_flags&ECHO)
+                       if (t_flags&ECHO)
                                ttyout("^\b", tp);
                        tp->t_state |= TS_LNCH;
                        goto endcase;
                }
                if (c == tp->t_flushc) {
                                ttyout("^\b", tp);
                        tp->t_state |= TS_LNCH;
                        goto endcase;
                }
                if (c == tp->t_flushc) {
-                       if (tp->t_flags&FLUSHO)
+                       if (t_flags&FLUSHO)
                                tp->t_flags &= ~FLUSHO;
                        else {
                                ttyflush(tp, FWRITE);
                                tp->t_flags &= ~FLUSHO;
                        else {
                                ttyflush(tp, FWRITE);
@@ -706,7 +757,7 @@ ttyinput(c, tp)
                        goto startoutput;
                }
                if (c == tp->t_suspc) {
                        goto startoutput;
                }
                if (c == tp->t_suspc) {
-                       if ((tp->t_flags&NOFLSH) == 0)
+                       if ((t_flags&NOFLSH) == 0)
                                ttyflush(tp, FREAD);
                        ttyecho(c, tp);
                        gsignal(tp->t_pgrp, SIGTSTP);
                                ttyflush(tp, FREAD);
                        ttyecho(c, tp);
                        gsignal(tp->t_pgrp, SIGTSTP);
@@ -734,13 +785,26 @@ ttyinput(c, tp)
         * Look for interrupt/quit chars.
         */
        if (c == tp->t_intrc || c == tp->t_quitc) {
         * Look for interrupt/quit chars.
         */
        if (c == tp->t_intrc || c == tp->t_quitc) {
-               if ((tp->t_flags&NOFLSH) == 0)
+               if ((t_flags&NOFLSH) == 0)
                        ttyflush(tp, FREAD|FWRITE);
                ttyecho(c, tp);
                gsignal(tp->t_pgrp, c == tp->t_intrc ? SIGINT : SIGQUIT);
                goto endcase;
        }
 
                        ttyflush(tp, FREAD|FWRITE);
                ttyecho(c, tp);
                gsignal(tp->t_pgrp, c == tp->t_intrc ? SIGINT : SIGQUIT);
                goto endcase;
        }
 
+       if (tp->t_flags & LCASE && c <= 0177) {
+               if (tp->t_state&TS_BKSL) {
+                       ttyrub(unputc(&tp->t_rawq), tp);
+                       if (maptab[c])
+                               c = maptab[c];
+                       c |= 0200;
+                       tp->t_state &= ~(TS_BKSL|TS_QUOT);
+               } else if (c >= 'A' && c <= 'Z')
+                       c += 'a' - 'A';
+               else if (c == '\\')
+                       tp->t_state |= TS_BKSL;
+       }
+
        /*
         * Cbreak mode, don't process line editing
         * characters; check high water mark for wakeup.
        /*
         * Cbreak mode, don't process line editing
         * characters; check high water mark for wakeup.
@@ -772,7 +836,7 @@ ttyinput(c, tp)
                goto endcase;
        }
        if (c == tp->t_kill) {
                goto endcase;
        }
        if (c == tp->t_kill) {
-               if (tp->t_flags&CRTKIL &&
+               if (t_flags&CRTKIL &&
                    tp->t_rawq.c_cc == tp->t_rocount) {
                        while (tp->t_rawq.c_cc)
                                ttyrub(unputc(&tp->t_rawq), tp);
                    tp->t_rawq.c_cc == tp->t_rocount) {
                        while (tp->t_rawq.c_cc)
                                ttyrub(unputc(&tp->t_rawq), tp);
@@ -847,7 +911,7 @@ ttyinput(c, tp)
                }
                i = tp->t_col;
                ttyecho(c, tp);
                }
                i = tp->t_col;
                ttyecho(c, tp);
-               if (c == tp->t_eofc && tp->t_flags&ECHO) {
+               if (c == tp->t_eofc && t_flags&ECHO) {
                        i = MIN(2, tp->t_col - i);
                        while (i > 0) {
                                (void) ttyoutput('\b', tp);
                        i = MIN(2, tp->t_col - i);
                        while (i > 0) {
                                (void) ttyoutput('\b', tp);
@@ -860,7 +924,7 @@ endcase:
         * If DEC-style start/stop is enabled don't restart
         * output until seeing the start character.
         */
         * If DEC-style start/stop is enabled don't restart
         * output until seeing the start character.
         */
-       if (tp->t_flags&DECCTQ && tp->t_state&TS_TTSTOP &&
+       if (t_flags&DECCTQ && tp->t_state&TS_TTSTOP &&
            tp->t_startc != tp->t_stopc)
                return;
 restartoutput:
            tp->t_startc != tp->t_stopc)
                return;
 restartoutput:
@@ -1041,8 +1105,6 @@ ttread(tp, uio)
        register c, t_flags;
        int s, first, error = 0;
 
        register c, t_flags;
        int s, first, error = 0;
 
-       if ((tp->t_state&TS_CARR_ON)==0)
-               return (EIO);
 loop:
        /*
         * Take any pending input first.
 loop:
        /*
         * Take any pending input first.
@@ -1052,23 +1114,22 @@ loop:
                ttypend(tp);
        splx(s);
 
                ttypend(tp);
        splx(s);
 
+       if ((tp->t_state&TS_CARR_ON)==0)
+               return (EIO);
+
        /*
         * Hang process if it's in the background.
         */
        /*
         * Hang process if it's in the background.
         */
-#define bit(a) (1<<(a-1))
-       while (tp == u.u_ttyp && u.u_procp->p_pgrp != tp->t_pgrp) {
-               if ((u.u_procp->p_sigignore & bit(SIGTTIN)) ||
-                  (u.u_procp->p_sigmask & bit(SIGTTIN)) ||
-/*
-                   (u.u_procp->p_flag&SDETACH) ||
-*/
+       if (tp == u.u_ttyp && u.u_procp->p_pgrp != tp->t_pgrp) {
+               if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) ||
+                  (u.u_procp->p_sigmask & sigmask(SIGTTIN)) ||
                    u.u_procp->p_flag&SVFORK)
                        return (EIO);
                gsignal(u.u_procp->p_pgrp, SIGTTIN);
                sleep((caddr_t)&lbolt, TTIPRI);
                    u.u_procp->p_flag&SVFORK)
                        return (EIO);
                gsignal(u.u_procp->p_pgrp, SIGTTIN);
                sleep((caddr_t)&lbolt, TTIPRI);
+               goto loop;
        }
        t_flags = tp->t_flags;
        }
        t_flags = tp->t_flags;
-#undef bit
 
        /*
         * In raw mode take characters directly from the
 
        /*
         * In raw mode take characters directly from the
@@ -1124,21 +1185,6 @@ loop:
        while ((c = getc(qp)) >= 0) {
                if (t_flags&CRMOD && c == '\r')
                        c = '\n';
        while ((c = getc(qp)) >= 0) {
                if (t_flags&CRMOD && c == '\r')
                        c = '\n';
-               /*
-                * Hack lower case simulation on
-                * upper case only terminals.
-                */
-               if (t_flags&LCASE && c <= 0177)
-                       if (tp->t_state&TS_BKSL) {
-                               if (maptab[c])
-                                       c = maptab[c];
-                               tp->t_state &= ~TS_BKSL;
-                       } else if (c >= 'A' && c <= 'Z')
-                               c += 'a' - 'A';
-                       else if (c == '\\') {
-                               tp->t_state |= TS_BKSL;
-                               continue;
-                       }
                /*
                 * Check for delayed suspend character.
                 */
                /*
                 * Check for delayed suspend character.
                 */
@@ -1158,7 +1204,7 @@ loop:
                /*
                 * Give user character.
                 */
                /*
                 * Give user character.
                 */
-               error = ureadc(c & 0177, uio);
+               error = ureadc(t_flags&PASS8 ? c : c & 0177, uio);
                if (error)
                        break;
                if (uio->uio_resid == 0)
                if (error)
                        break;
                if (uio->uio_resid == 0)
@@ -1171,7 +1217,6 @@ loop:
                        break;
                first = 0;
        }
                        break;
                first = 0;
        }
-       tp->t_state &= ~TS_BKSL;
 
 checktandem:
        /*
 
 checktandem:
        /*
@@ -1186,6 +1231,34 @@ checktandem:
        return (error);
 }
 
        return (error);
 }
 
+/*
+ * Check the output queue on tp for space for a kernel message
+ * (from uprintf/tprintf).  Allow some space over the normal
+ * hiwater mark so we don't lose messages due to normal flow
+ * control, but don't let the tty run amok.
+ */
+ttycheckoutq(tp, wait)
+       register struct tty *tp;
+       int wait;
+{
+       int hiwat, s;
+
+       hiwat = TTHIWAT(tp);
+       s = spltty();
+       if (tp->t_outq.c_cc > hiwat + 200)
+           while (tp->t_outq.c_cc > hiwat) {
+               ttstart(tp);
+               if (wait == 0) {
+                       splx(s);
+                       return (0);
+               }
+               tp->t_state |= TS_ASLEEP;
+               sleep((caddr_t)&tp->t_outq, TTOPRI);
+       }
+       splx(s);
+       return (1);
+}
+
 /*
  * Called from the device's write routine after it has
  * calculated the tty-structure given as argument.
 /*
  * Called from the device's write routine after it has
  * calculated the tty-structure given as argument.
@@ -1199,29 +1272,23 @@ ttwrite(tp, uio)
        int i, hiwat, cnt, error, s;
        char obuf[OBUFSIZ];
 
        int i, hiwat, cnt, error, s;
        char obuf[OBUFSIZ];
 
-       if ((tp->t_state&TS_CARR_ON) == 0)
-               return (EIO);
        hiwat = TTHIWAT(tp);
        cnt = uio->uio_resid;
        error = 0;
 loop:
        hiwat = TTHIWAT(tp);
        cnt = uio->uio_resid;
        error = 0;
 loop:
+       if ((tp->t_state&TS_CARR_ON) == 0)
+               return (EIO);
        /*
         * Hang the process if it's in the background.
         */
        /*
         * Hang the process if it's in the background.
         */
-#define bit(a) (1<<(a-1))
-       while (u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
+       if (u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
            (tp->t_flags&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
            (tp->t_flags&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
-           !(u.u_procp->p_sigignore & bit(SIGTTOU)) &&
-           !(u.u_procp->p_sigmask & bit(SIGTTOU))
-/*
-                                            &&
-           (u.u_procp->p_flag&SDETACH)==0) {
-*/
-           ) {
+           !(u.u_procp->p_sigignore & sigmask(SIGTTOU)) &&
+           !(u.u_procp->p_sigmask & sigmask(SIGTTOU))) {
                gsignal(u.u_procp->p_pgrp, SIGTTOU);
                sleep((caddr_t)&lbolt, TTIPRI);
                gsignal(u.u_procp->p_pgrp, SIGTTOU);
                sleep((caddr_t)&lbolt, TTIPRI);
+               goto loop;
        }
        }
-#undef bit
 
        /*
         * Process the user's data in at most OBUFSIZ
 
        /*
         * Process the user's data in at most OBUFSIZ
@@ -1238,7 +1305,7 @@ loop:
                if (cc == 0) {
                        uio->uio_iovcnt--;
                        uio->uio_iov++;
                if (cc == 0) {
                        uio->uio_iovcnt--;
                        uio->uio_iov++;
-                       if (uio->uio_iovcnt < 0)
+                       if (uio->uio_iovcnt <= 0)
                                panic("ttwrite");
                        continue;
                }
                                panic("ttwrite");
                        continue;
                }
@@ -1266,6 +1333,13 @@ loop:
                                        ttstart(tp);
                                        sleep((caddr_t)&lbolt, TTOPRI);
                                        tp->t_rocount = 0;
                                        ttstart(tp);
                                        sleep((caddr_t)&lbolt, TTOPRI);
                                        tp->t_rocount = 0;
+                                       if (cc != 0) {
+                                               uio->uio_iov->iov_base -= cc;
+                                               uio->uio_iov->iov_len += cc;
+                                               uio->uio_resid += cc;
+                                               uio->uio_offset -= cc;
+                                       }
+                                       goto loop;
                                }
                                --cc;
                                if (tp->t_outq.c_cc > hiwat)
                                }
                                --cc;
                                if (tp->t_outq.c_cc > hiwat)
@@ -1295,10 +1369,16 @@ loop:
                                if (ce == 0) {
                                        tp->t_rocount = 0;
                                        if (ttyoutput(*cp, tp) >= 0) {
                                if (ce == 0) {
                                        tp->t_rocount = 0;
                                        if (ttyoutput(*cp, tp) >= 0) {
-                                               /* no c-lists, wait a bit */
-                                               ttstart(tp);
-                                               sleep((caddr_t)&lbolt, TTOPRI);
-                                               continue;
+                                           /* no c-lists, wait a bit */
+                                           ttstart(tp);
+                                           sleep((caddr_t)&lbolt, TTOPRI);
+                                           if (cc != 0) {
+                                               uio->uio_iov->iov_base -= cc;
+                                               uio->uio_iov->iov_len += cc;
+                                               uio->uio_resid += cc;
+                                               uio->uio_offset -= cc;
+                                           }
+                                           goto loop;
                                        }
                                        cp++, cc--;
                                        if (tp->t_flags&FLUSHO ||
                                        }
                                        cp++, cc--;
                                        if (tp->t_flags&FLUSHO ||
@@ -1324,6 +1404,11 @@ loop:
                                /* out of c-lists, wait a bit */
                                ttstart(tp);
                                sleep((caddr_t)&lbolt, TTOPRI);
                                /* out of c-lists, wait a bit */
                                ttstart(tp);
                                sleep((caddr_t)&lbolt, TTOPRI);
+                               uio->uio_iov->iov_base -= cc;
+                               uio->uio_iov->iov_len += cc;
+                               uio->uio_resid += cc;
+                               uio->uio_offset -= cc;
+                               goto loop;
                        }
                        if (tp->t_flags&FLUSHO || tp->t_outq.c_cc > hiwat)
                                goto ovhiwat;
                        }
                        if (tp->t_flags&FLUSHO || tp->t_outq.c_cc > hiwat)
                                goto ovhiwat;
@@ -1516,8 +1601,6 @@ ttyecho(c, tp)
                                c += 'A' - 1;
                }
        }
                                c += 'A' - 1;
                }
        }
-       if ((tp->t_flags&LCASE) && (c >= 'A' && c <= 'Z'))
-               c += 'a' - 'A';
        (void) ttyoutput(c&0177, tp);
 }
 
        (void) ttyoutput(c&0177, tp);
 }
 
@@ -1558,17 +1641,3 @@ ttwakeup(tp)
                gsignal(tp->t_pgrp, SIGIO); 
        wakeup((caddr_t)&tp->t_rawq);
 }
                gsignal(tp->t_pgrp, SIGIO); 
        wakeup((caddr_t)&tp->t_rawq);
 }
-
-#if !defined(vax)
-scanc(size, cp, table, mask)
-       register int size;
-       register char *cp, table[];
-       register int mask;
-{
-       register int i = 0;
-
-       while ((table[*(u_char *)(cp + i)]&mask) == 0 && i < size)
-               i++;
-       return (size - i);
-}
-#endif