Annotation of xinu/sys/ttyread.c, revision 1.1

1.1     ! root        1: /* ttyread.c - ttyread, readcopy */
        !             2: 
        !             3: #include <conf.h>
        !             4: #include <kernel.h>
        !             5: #include <tty.h>
        !             6: #include <io.h>
        !             7: 
        !             8: /*------------------------------------------------------------------------
        !             9:  *  ttyread - read one or more characters from a tty device
        !            10:  *------------------------------------------------------------------------
        !            11:  */
        !            12: ttyread(devptr, buff, count)
        !            13: struct devsw   *devptr;
        !            14: int count;
        !            15: char *buff;
        !            16: {
        !            17:        PStype ps;
        !            18:        register struct tty *iptr;
        !            19:        int avail, nread;
        !            20:        char ch, eofch;
        !            21:        int     donow, dolater;
        !            22: 
        !            23:        if (count < 0)
        !            24:                return(SYSERR);
        !            25:        disable(ps);
        !            26:        if ( (avail=scount((iptr= &tty[devptr->dvminor])->isem)) < 0)
        !            27:                avail = 0;
        !            28:        if (count == 0) {       /* read whatever is available */
        !            29:                if (avail == 0) {
        !            30:                        restore(ps);
        !            31:                        return(0);
        !            32:                }
        !            33:                count = avail;
        !            34:        }
        !            35:        if (count < avail) {
        !            36:                donow = count;
        !            37:                dolater = 0;
        !            38:        } else {
        !            39:                donow = avail;
        !            40:                dolater = count - avail;
        !            41:        }
        !            42:        nread = 0;
        !            43:        if (donow > 0) {
        !            44:                ch = iptr->ibuff[iptr->itail++];
        !            45:                if (iptr->itail >= IBUFLEN)
        !            46:                        iptr->itail = 0;
        !            47:                if ( ((eofch=iptr->ieofc) == ch) && iptr->ieof) {
        !            48:                                sreset(iptr->isem, avail-1);
        !            49:                                restore(ps);
        !            50:                                return(EOF);
        !            51:                }
        !            52:                *buff++ = ch;
        !            53:                for (nread=1 ; nread < donow ; ) {
        !            54:                        ch = iptr->ibuff[iptr->itail];
        !            55:                        if ( (ch==eofch) && iptr->ieof) {
        !            56:                                sreset(iptr->isem, avail - nread);
        !            57:                                restore(ps);    
        !            58:                                return(nread);
        !            59:                        }
        !            60:                        *buff++ = ch;
        !            61:                        if (++iptr->itail >= IBUFLEN)
        !            62:                                iptr->itail = 0;
        !            63:                        nread++;
        !            64:                        if (ch == NEWLINE || ch == RETURN) {
        !            65:                                sreset(iptr->isem, avail - nread);
        !            66:                                restore(ps);    
        !            67:                                return(nread);
        !            68:                        }
        !            69:                }
        !            70:                sreset(iptr->isem, avail - nread);
        !            71:        }
        !            72:        donow = nread;
        !            73:        for (nread=0 ; nread < dolater ; ) {
        !            74:                wait(iptr->isem);
        !            75:                ch = iptr->ibuff[iptr->itail];
        !            76:                if (ch == iptr->ieofc && iptr->ieof) {
        !            77:                        if (nread == 0 && donow == 0) {
        !            78:                                if (++iptr->itail >= IBUFLEN)
        !            79:                                iptr->itail = 0;
        !            80:                                restore(ps);
        !            81:                                return(EOF);
        !            82:                        }
        !            83:                        signal(iptr->isem);
        !            84:                        break;
        !            85:                }
        !            86:                *buff++ = ch;
        !            87:                nread++;
        !            88:                if (++iptr->itail >= IBUFLEN)
        !            89:                        iptr->itail = 0;
        !            90:                if (ch == NEWLINE || ch == RETURN)
        !            91:                        break;
        !            92:        }
        !            93:        restore(ps);
        !            94:        return(donow + nread);
        !            95: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.