Annotation of Net2/kern/tty_ring.c, revision 1.1.1.2

1.1       root        1: /*
                      2:  * Copyright (c) 1989, 1990, 1991, 1992 William F. Jolitz, TeleMuse
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. All advertising materials mentioning features or use of this software
                     14:  *    must display the following acknowledgement:
                     15:  *     This software is a component of "386BSD" developed by 
1.1.1.2 ! root       16:  *     William F. Jolitz, TeleMuse.
1.1       root       17:  * 4. Neither the name of the developer nor the name "386BSD"
                     18:  *    may be used to endorse or promote products derived from this software
                     19:  *    without specific prior written permission.
                     20:  *
                     21:  * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ 
                     22:  * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS 
                     23:  * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT. 
                     24:  * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT 
1.1.1.2 ! root       25:  * NOT MAKE USE OF THIS WORK.
1.1       root       26:  *
                     27:  * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
                     28:  * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN 
                     29:  * REFERENCES SUCH AS THE  "PORTING UNIX TO THE 386" SERIES 
                     30:  * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING 
                     31:  * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND 
                     32:  * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE 
                     33:  * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS 
                     34:  * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
                     35:  *
                     36:  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
                     37:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     38:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     39:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE DEVELOPER BE LIABLE
                     40:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     41:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     42:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     43:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     44:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     45:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     46:  * SUCH DAMAGE.
                     47:  *
                     48:  */
1.1.1.2 ! root       49: static char rcsid[] = "$Header: /cvsroot/src/sys/kern/Attic/tty_ring.c,v 1.2 1993/03/24 23:55:29 cgd Exp $";
1.1       root       50: 
                     51: #include "param.h"
                     52: #include "systm.h"
                     53: #include "buf.h"
                     54: #include "ioctl.h"
                     55: #include "tty.h"
                     56: 
1.1.1.2 ! root       57: /*
        !            58:  * XXX - put this in tty.h someday.
        !            59:  */
        !            60: size_t rb_write __P((struct ringb *to, char *buf, size_t nfrom));
        !            61: 
1.1       root       62: putc(c, rbp) struct ringb *rbp;
                     63: {
                     64:        char *nxtp;
                     65: 
                     66:        /* ring buffer full? */
                     67:        if ( (nxtp = RB_SUCC(rbp, rbp->rb_tl)) == rbp->rb_hd) return (-1);
                     68: 
                     69:        /* stuff character */
                     70:        *rbp->rb_tl = c;
                     71:        rbp->rb_tl = nxtp;
                     72:        return(0);
                     73: }
                     74: 
                     75: getc(rbp) struct ringb *rbp;
                     76: {
                     77:        u_char c;
                     78: 
                     79:        /* ring buffer empty? */
                     80:        if (rbp->rb_hd == rbp->rb_tl) return(-1);
                     81: 
                     82:        /* fetch character, locate next character */
                     83:        c = *(u_char *) rbp->rb_hd;
                     84:        rbp->rb_hd = RB_SUCC(rbp, rbp->rb_hd);
                     85:        return (c);
                     86: }
                     87: 
                     88: nextc(cpp, rbp) struct ringb *rbp; char **cpp; {
                     89: 
                     90:        if (*cpp == rbp->rb_tl) return (0);
                     91:        else {  char *cp;
                     92:                cp = *cpp;
                     93:                *cpp = RB_SUCC(rbp, cp);
                     94:                return(*cp);
                     95:        }
                     96: }
                     97: 
                     98: ungetc(c, rbp) struct ringb *rbp;
                     99: {
                    100:        char    *backp;
                    101: 
                    102:        /* ring buffer full? */
                    103:        if ( (backp = RB_PRED(rbp, rbp->rb_hd)) == rbp->rb_tl) return (-1);
                    104:        rbp->rb_hd = backp;
                    105: 
                    106:        /* stuff character */
                    107:        *rbp->rb_hd = c;
                    108:        return(0);
                    109: }
                    110: 
                    111: unputc(rbp) struct ringb *rbp;
                    112: {
                    113:        char    *backp;
                    114:        int c;
                    115: 
                    116:        /* ring buffer empty? */
                    117:        if (rbp->rb_hd == rbp->rb_tl) return(-1);
                    118: 
                    119:        /* backup buffer and dig out previous character */
                    120:        backp = RB_PRED(rbp, rbp->rb_tl);
                    121:        c = *(u_char *)backp;
                    122:        rbp->rb_tl = backp;
                    123: 
                    124:        return(c);
                    125: }
                    126: 
                    127: #define        peekc(rbp)      (*(rbp)->rb_hd)
                    128: 
                    129: initrb(rbp) struct ringb *rbp; {
                    130:        rbp->rb_hd = rbp->rb_tl = rbp->rb_buf;
                    131: }
                    132: 
                    133: /*
                    134:  * Example code for contiguous operations:
                    135:        ...
                    136:        nc = RB_CONTIGPUT(&rb);
                    137:        if (nc) {
                    138:        if (nc > 9) nc = 9;
                    139:                bcopy("ABCDEFGHI", rb.rb_tl, nc);
                    140:                rb.rb_tl += nc;
                    141:                rb.rb_tl = RB_ROLLOVER(&rb, rb.rb_tl);
                    142:        }
                    143:        ...
                    144:        ...
                    145:        nc = RB_CONTIGGET(&rb);
                    146:        if (nc) {
                    147:                if (nc > 79) nc = 79;
                    148:                bcopy(rb.rb_hd, stringbuf, nc);
                    149:                rb.rb_hd += nc;
                    150:                rb.rb_hd = RB_ROLLOVER(&rb, rb.rb_hd);
                    151:                stringbuf[nc] = 0;
                    152:                printf("%s|", stringbuf);
                    153:        }
                    154:        ...
                    155:  */
                    156: 
                    157: /*
1.1.1.2 ! root      158:  * Concatenate ring buffers.
1.1       root      159:  */
                    160: catb(from, to)
                    161:        struct ringb *from, *to;
                    162: {
1.1.1.2 ! root      163:        size_t nfromleft;
        !           164:        size_t nfromright;
        !           165: 
        !           166:        nfromright = RB_CONTIGGET(from);
        !           167:        rb_write(to, from->rb_hd, nfromright);
        !           168:        from->rb_hd += nfromright;
        !           169:        from->rb_hd = RB_ROLLOVER(from, from->rb_hd);
        !           170:        nfromleft = RB_CONTIGGET(from);
        !           171:        rb_write(to, from->rb_hd, nfromleft);
        !           172:        from->rb_hd += nfromleft;
        !           173: }
1.1       root      174: 
1.1.1.2 ! root      175: /*
        !           176:  * Copy ordinary buffer to ring buffer, return count of what fitted.
        !           177:  */
        !           178: size_t rb_write(to, buf, nfrom)
        !           179:        struct ringb *to;
        !           180:        char *buf;
        !           181:        size_t nfrom;
        !           182: {
        !           183:        char *toleft;
        !           184:        size_t ntoleft;
        !           185:        size_t ntoright;
        !           186: 
        !           187:        ntoright = RB_CONTIGPUT(to);
        !           188:        if (nfrom < ntoright) {
        !           189:                bcopy(buf, to->rb_tl, nfrom);
        !           190:                to->rb_tl += nfrom;
        !           191:                return (nfrom);
        !           192:        }
        !           193:        bcopy(buf, to->rb_tl, ntoright);
        !           194:        nfrom -= ntoright;
        !           195:        toleft = to->rb_buf;    /* fast RB_ROLLOVER */
        !           196:        ntoleft = to->rb_hd - toleft;   /* fast RB_CONTIGPUT */
        !           197:        if (nfrom > ntoleft)
        !           198:                nfrom = ntoleft;
        !           199:        bcopy(buf + ntoright, toleft, nfrom);
        !           200:        to->rb_tl = toleft + nfrom;
        !           201:        return (ntoright + nfrom);
1.1       root      202: }

unix.superglobalmegacorp.com

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