386BSD 0.1 development
[unix-history] / usr / src / sys.386bsd / kern / tty_ring.c
CommitLineData
d0968c4e
WJ
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
16 William F. Jolitz, TeleMuse.
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
25 * NOT MAKE USE THIS WORK.
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 */
49static char rcsid[] = "$Header: /usr/bill/working/sys/kern/RCS/tty_ring.c,v 1.2 92/01/21 21:29:55 william Exp $";
50
51#include "param.h"
52#include "systm.h"
53#include "buf.h"
54#include "ioctl.h"
55#include "tty.h"
56
57putc(c, rbp) struct ringb *rbp;
58{
59 char *nxtp;
60
61 /* ring buffer full? */
62 if ( (nxtp = RB_SUCC(rbp, rbp->rb_tl)) == rbp->rb_hd) return (-1);
63
64 /* stuff character */
65 *rbp->rb_tl = c;
66 rbp->rb_tl = nxtp;
67 return(0);
68}
69
70getc(rbp) struct ringb *rbp;
71{
72 u_char c;
73
74 /* ring buffer empty? */
75 if (rbp->rb_hd == rbp->rb_tl) return(-1);
76
77 /* fetch character, locate next character */
78 c = *(u_char *) rbp->rb_hd;
79 rbp->rb_hd = RB_SUCC(rbp, rbp->rb_hd);
80 return (c);
81}
82
83nextc(cpp, rbp) struct ringb *rbp; char **cpp; {
84
85 if (*cpp == rbp->rb_tl) return (0);
86 else { char *cp;
87 cp = *cpp;
88 *cpp = RB_SUCC(rbp, cp);
89 return(*cp);
90 }
91}
92
93ungetc(c, rbp) struct ringb *rbp;
94{
95 char *backp;
96
97 /* ring buffer full? */
98 if ( (backp = RB_PRED(rbp, rbp->rb_hd)) == rbp->rb_tl) return (-1);
99 rbp->rb_hd = backp;
100
101 /* stuff character */
102 *rbp->rb_hd = c;
103 return(0);
104}
105
106unputc(rbp) struct ringb *rbp;
107{
108 char *backp;
109 int c;
110
111 /* ring buffer empty? */
112 if (rbp->rb_hd == rbp->rb_tl) return(-1);
113
114 /* backup buffer and dig out previous character */
115 backp = RB_PRED(rbp, rbp->rb_tl);
116 c = *(u_char *)backp;
117 rbp->rb_tl = backp;
118
119 return(c);
120}
121
122#define peekc(rbp) (*(rbp)->rb_hd)
123
124initrb(rbp) struct ringb *rbp; {
125 rbp->rb_hd = rbp->rb_tl = rbp->rb_buf;
126}
127
128/*
129 * Example code for contiguous operations:
130 ...
131 nc = RB_CONTIGPUT(&rb);
132 if (nc) {
133 if (nc > 9) nc = 9;
134 bcopy("ABCDEFGHI", rb.rb_tl, nc);
135 rb.rb_tl += nc;
136 rb.rb_tl = RB_ROLLOVER(&rb, rb.rb_tl);
137 }
138 ...
139 ...
140 nc = RB_CONTIGGET(&rb);
141 if (nc) {
142 if (nc > 79) nc = 79;
143 bcopy(rb.rb_hd, stringbuf, nc);
144 rb.rb_hd += nc;
145 rb.rb_hd = RB_ROLLOVER(&rb, rb.rb_hd);
146 stringbuf[nc] = 0;
147 printf("%s|", stringbuf);
148 }
149 ...
150 */
151
152/*
153 * Concatinate ring buffers.
154 */
155catb(from, to)
156 struct ringb *from, *to;
157{
158 char c;
159
160 while ((c = getc(from)) >= 0)
161 putc(c, to);
162}