bug fixes from elz and utcsrgv!thomson
[unix-history] / usr / src / sys / kern / tty_subr.c
index 90c2f68..78ce16f 100644 (file)
@@ -1,19 +1,13 @@
-/*     tty_subr.c      3.3     %H%     */
+/*     tty_subr.c      4.15    82/03/14        */
 
 #include "../h/param.h"
 
 #include "../h/param.h"
-#include "../h/tty.h"
 #include "../h/systm.h"
 #include "../h/conf.h"
 #include "../h/buf.h"
 #include "../h/systm.h"
 #include "../h/conf.h"
 #include "../h/buf.h"
+#include "../h/tty.h"
+#include "../h/clist.h"
 
 
-struct cblock {
-       struct cblock *c_next;
-       char    c_info[CBSIZE];
-};
-
-struct cblock  cfree[NCLIST];
-int    cbad;
-struct cblock  *cfreelist;
+char   cwaiting;
 
 /*
  * Character list get/put
 
 /*
  * Character list get/put
@@ -24,7 +18,7 @@ register struct clist *p;
        register struct cblock *bp;
        register int c, s;
 
        register struct cblock *bp;
        register int c, s;
 
-       s = spl6();
+       s = spl5();
        if (p->c_cc <= 0) {
                c = -1;
                p->c_cc = 0;
        if (p->c_cc <= 0) {
                c = -1;
                p->c_cc = 0;
@@ -38,18 +32,29 @@ register struct clist *p;
                        p->c_cl = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                        p->c_cl = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
+                       if (cwaiting) {
+                               wakeup(&cwaiting);
+                               cwaiting = 0;
+                       }
                } else if (((int)p->c_cf & CROUND) == 0){
                        bp = (struct cblock *)(p->c_cf);
                        bp--;
                        p->c_cf = bp->c_next->c_info;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                } else if (((int)p->c_cf & CROUND) == 0){
                        bp = (struct cblock *)(p->c_cf);
                        bp--;
                        p->c_cf = bp->c_next->c_info;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
+                       if (cwaiting) {
+                               wakeup(&cwaiting);
+                               cwaiting = 0;
+                       }
                }
        }
        splx(s);
        return(c);
 }
 
                }
        }
        splx(s);
        return(c);
 }
 
+#if notdef
 /*
  * copy clist to buffer.
  * return number of bytes moved.
 /*
  * copy clist to buffer.
  * return number of bytes moved.
@@ -64,10 +69,11 @@ register char *cp;
 
        if (cc <= 0)
                return(0);
 
        if (cc <= 0)
                return(0);
-       s = spl6();
+       s = spl5();
        if (q->c_cc <= 0) {
                q->c_cc = 0;
                q->c_cf = q->c_cl = NULL;
        if (q->c_cc <= 0) {
                q->c_cc = 0;
                q->c_cf = q->c_cl = NULL;
+               splx(s);
                return(0);
        }
        acp = cp;
                return(0);
        }
        acp = cp;
@@ -81,6 +87,11 @@ register char *cp;
                        q->c_cf = q->c_cl = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                        q->c_cf = q->c_cl = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
+                       if (cwaiting) {
+                               wakeup(&cwaiting);
+                               cwaiting = 0;
+                       }
                        break;
                }
                if (((int)q->c_cf & CROUND) == 0) {
                        break;
                }
                if (((int)q->c_cf & CROUND) == 0) {
@@ -89,11 +100,17 @@ register char *cp;
                        q->c_cf = bp->c_next->c_info;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                        q->c_cf = bp->c_next->c_info;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
+                       if (cwaiting) {
+                               wakeup(&cwaiting);
+                               cwaiting = 0;
+                       }
                }
        }
        splx(s);
        return(cp-acp);
 }
                }
        }
        splx(s);
        return(cp-acp);
 }
+#endif
 
 /*
  * Return count of contiguous characters
 
 /*
  * Return count of contiguous characters
@@ -106,7 +123,7 @@ register struct clist *q;
 register cc;
 int s;
 
 register cc;
 int s;
 
-       s = spl6();
+       s = spl5();
        if (q->c_cc <= 0) {
                cc = -q->c_cc;
                goto out;
        if (q->c_cc <= 0) {
                cc = -q->c_cc;
                goto out;
@@ -123,7 +140,8 @@ int s;
                end += cc;
                while (p < end) {
                        if (*p & flag) {
                end += cc;
                while (p < end) {
                        if (*p & flag) {
-                               cc = (int)p - (int)q->c_cf;
+                               cc = (int)p;
+                               cc -= (int)q->c_cf;
                                break;
                        }
                        p++;
                                break;
                        }
                        p++;
@@ -134,64 +152,71 @@ out:
        return(cc);
 }
 
        return(cc);
 }
 
+
+
 /*
 /*
- * Update clist to show that cc characters
- * were removed.  It is assumed that cc < CBSIZE.
+ * Flush cc bytes from q.
  */
 ndflush(q, cc)
 register struct clist *q;
 register cc;
 {
  */
 ndflush(q, cc)
 register struct clist *q;
 register cc;
 {
+register struct cblock *bp;
+char *end;
+int rem;
 register s;
 
 register s;
 
-       if (cc == 0)
-               return;
-       s = spl6();
+       s = spl5();
        if (q->c_cc < 0) {
        if (q->c_cc < 0) {
-               if (q->c_cf != NULL) {
-                       q->c_cc += cc;
-                       q->c_cf += cc;
-                       goto out;
-               }
-               q->c_cc = 0;
+               printf("neg q flush\n");
                goto out;
        }
        if (q->c_cc == 0) {
                goto out;
        }
                goto out;
        }
        if (q->c_cc == 0) {
                goto out;
        }
-       if (cc > CBSIZE || cc <= 0) {
-               cbad++;
-               goto out;
-       }
-       q->c_cc -= cc;
-       q->c_cf += cc;
-       if (((int)q->c_cf & CROUND) == 0) {
-               register struct cblock *bp;
-
-               bp = (struct cblock *)(q->c_cf) -1;
-               if (bp->c_next) {
+       while (cc>0 && q->c_cc) {
+               bp = (struct cblock *)((int)q->c_cf & ~CROUND);
+               if ((int)bp == (((int)q->c_cl-1) & ~CROUND)) {
+                       end = q->c_cl;
+               } else {
+                       end = (char *)((int)bp + sizeof (struct cblock));
+               }
+               rem = end - q->c_cf;
+               if (cc >= rem) {
+                       cc -= rem;
+                       q->c_cc -= rem;
                        q->c_cf = bp->c_next->c_info;
                        q->c_cf = bp->c_next->c_info;
+                       bp->c_next = cfreelist;
+                       cfreelist = bp;
+                       cfreecount += CBSIZE;
+                       if (cwaiting) {
+                               wakeup(&cwaiting);
+                               cwaiting = 0;
+                       }
                } else {
                } else {
-                       q->c_cf = q->c_cl = NULL;
+                       q->c_cc -= cc;
+                       q->c_cf += cc;
+                       if (q->c_cc <= 0) {
+                               bp->c_next = cfreelist;
+                               cfreelist = bp;
+                               cfreecount += CBSIZE;
+                               if (cwaiting) {
+                                       wakeup(&cwaiting);
+                                       cwaiting = 0;
+                               }
+                       }
+                       break;
                }
                }
-               bp->c_next = cfreelist;
-               cfreelist = bp;
-       } else
-       if (q->c_cc == 0) {
-               register struct cblock *bp;
-               q->c_cf = (char *)((int)q->c_cf & ~CROUND);
-               bp = (struct cblock *)(q->c_cf);
-               bp->c_next = cfreelist;
-               cfreelist = bp;
+       }
+       if (q->c_cc <= 0) {
                q->c_cf = q->c_cl = NULL;
                q->c_cf = q->c_cl = NULL;
+               q->c_cc = 0;
        }
 out:
        splx(s);
 }
 
        }
 out:
        splx(s);
 }
 
-/*
- * Put character c in queue p.
- */
+
 putc(c, p)
 register struct clist *p;
 {
 putc(c, p)
 register struct clist *p;
 {
@@ -199,13 +224,14 @@ register struct clist *p;
        register char *cp;
        register s;
 
        register char *cp;
        register s;
 
-       s = spl6();
+       s = spl5();
        if ((cp = p->c_cl) == NULL || p->c_cc < 0 ) {
                if ((bp = cfreelist) == NULL) {
                        splx(s);
                        return(-1);
                }
                cfreelist = bp->c_next;
        if ((cp = p->c_cl) == NULL || p->c_cc < 0 ) {
                if ((bp = cfreelist) == NULL) {
                        splx(s);
                        return(-1);
                }
                cfreelist = bp->c_next;
+               cfreecount -= CBSIZE;
                bp->c_next = NULL;
                p->c_cf = cp = bp->c_info;
        } else if (((int)cp & CROUND) == 0) {
                bp->c_next = NULL;
                p->c_cf = cp = bp->c_info;
        } else if (((int)cp & CROUND) == 0) {
@@ -216,6 +242,7 @@ register struct clist *p;
                }
                bp = bp->c_next;
                cfreelist = bp->c_next;
                }
                bp = bp->c_next;
                cfreelist = bp->c_next;
+               cfreecount -= CBSIZE;
                bp->c_next = NULL;
                cp = bp->c_info;
        }
                bp->c_next = NULL;
                cp = bp->c_info;
        }
@@ -226,6 +253,8 @@ register struct clist *p;
        return(0);
 }
 
        return(0);
 }
 
+
+
 /*
  * copy buffer to clist.
  * return number of bytes not transfered.
 /*
  * copy buffer to clist.
  * return number of bytes not transfered.
@@ -242,11 +271,14 @@ register int cc;
        if (cc <= 0)
                return(0);
        acc = cc;
        if (cc <= 0)
                return(0);
        acc = cc;
-       s = spl6();
+
+
+       s = spl5();
        if ((cq = q->c_cl) == NULL || q->c_cc < 0) {
                if ((bp = cfreelist) == NULL) 
                        goto out;
                cfreelist = bp->c_next;
        if ((cq = q->c_cl) == NULL || q->c_cc < 0) {
                if ((bp = cfreelist) == NULL) 
                        goto out;
                cfreelist = bp->c_next;
+               cfreecount -= CBSIZE;
                bp->c_next = NULL;
                q->c_cf = cq = bp->c_info;
        }
                bp->c_next = NULL;
                q->c_cf = cq = bp->c_info;
        }
@@ -258,6 +290,7 @@ register int cc;
                                goto out;
                        bp = bp->c_next;
                        cfreelist = bp->c_next;
                                goto out;
                        bp = bp->c_next;
                        cfreelist = bp->c_next;
+                       cfreecount -= CBSIZE;
                        bp->c_next = NULL;
                        cq = bp->c_info;
                }
                        bp->c_next = NULL;
                        cq = bp->c_info;
                }
@@ -303,7 +336,7 @@ register struct clist *p;
        register int c, s;
        struct cblock *obp;
 
        register int c, s;
        struct cblock *obp;
 
-       s = spl6();
+       s = spl5();
        if (p->c_cc <= 0)
                c = -1;
        else {
        if (p->c_cc <= 0)
                c = -1;
        else {
@@ -314,6 +347,7 @@ register struct clist *p;
                        p->c_cl = p->c_cf = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                        p->c_cl = p->c_cf = NULL;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
                } else if (((int)p->c_cl & CROUND) == sizeof(bp->c_next)) {
                        p->c_cl = (char *)((int)p->c_cl & ~CROUND);
                        bp = (struct cblock *)p->c_cf;
                } else if (((int)p->c_cl & CROUND) == sizeof(bp->c_next)) {
                        p->c_cl = (char *)((int)p->c_cl & ~CROUND);
                        bp = (struct cblock *)p->c_cf;
@@ -325,6 +359,7 @@ register struct clist *p;
                        bp = bp->c_next;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
                        bp = bp->c_next;
                        bp->c_next = cfreelist;
                        cfreelist = bp;
+                       cfreecount += CBSIZE;
                        obp->c_next = NULL;
                }
        }
                        obp->c_next = NULL;
                }
        }
@@ -347,33 +382,12 @@ struct clist *from, *to;
                (void) putc(c, to);
 }
 
                (void) putc(c, to);
 }
 
-/*
- * Initialize clist by freeing all character blocks, then count
- * number of character devices. (Once-only routine)
- */
-cinit()
-{
-       register int ccp;
-       register struct cblock *cp;
-       register struct cdevsw *cdp;
-
-       ccp = (int)cfree;
-       ccp = (ccp+CROUND) & ~CROUND;
-       for(cp=(struct cblock *)ccp; cp <= &cfree[NCLIST-1]; cp++) {
-               cp->c_next = cfreelist;
-               cfreelist = cp;
-       }
-       ccp = 0;
-       for(cdp = cdevsw; cdp->d_open; cdp++)
-               ccp++;
-       nchrdev = ccp;
-}
-
+#include "dmc.h"
+#if NDMC > 0
 /*
  * integer (2-byte) get/put
  * using clists
  */
 /*
  * integer (2-byte) get/put
  * using clists
  */
-/*
 getw(p)
 register struct clist *p;
 {
 getw(p)
 register struct clist *p;
 {
@@ -384,14 +398,13 @@ register struct clist *p;
        s = getc(p);
        return(s | (getc(p)<<8));
 }
        s = getc(p);
        return(s | (getc(p)<<8));
 }
-*/
 
 putw(c, p)
 register struct clist *p;
 {
        register s;
 
 
 putw(c, p)
 register struct clist *p;
 {
        register s;
 
-       s = spl6();
+       s = spl5();
        if (cfreelist==NULL) {
                splx(s);
                return(-1);
        if (cfreelist==NULL) {
                splx(s);
                return(-1);
@@ -401,3 +414,4 @@ register struct clist *p;
        splx(s);
        return(0);
 }
        splx(s);
        return(0);
 }
+#endif