Media table reorganization.
[unix-history] / sys / kern / tty_ring.c
CommitLineData
15637ed4
RG
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 OF 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 *
4c45483e 48 * $Id: tty_ring.c,v 1.2 1993/10/16 15:25:01 rgrimes Exp $
15637ed4 49 */
15637ed4
RG
50
51#include "param.h"
52#include "systm.h"
53#include "buf.h"
54#include "ioctl.h"
55#include "tty.h"
56
57/*
58 * XXX - put this in tty.h someday.
59 */
60size_t rb_write __P((struct ringb *to, char *buf, size_t nfrom));
61
4c45483e
GW
62int
63putc(c, rbp)
64 int c;
65 struct ringb *rbp;
15637ed4
RG
66{
67 char *nxtp;
68
69 /* ring buffer full? */
70 if ( (nxtp = RB_SUCC(rbp, rbp->rb_tl)) == rbp->rb_hd) return (-1);
71
72 /* stuff character */
73 *rbp->rb_tl = c;
74 rbp->rb_tl = nxtp;
75 return(0);
76}
77
4c45483e
GW
78int
79getc(rbp)
80 struct ringb *rbp;
15637ed4
RG
81{
82 u_char c;
83
84 /* ring buffer empty? */
85 if (rbp->rb_hd == rbp->rb_tl) return(-1);
86
87 /* fetch character, locate next character */
88 c = *(u_char *) rbp->rb_hd;
89 rbp->rb_hd = RB_SUCC(rbp, rbp->rb_hd);
90 return (c);
91}
92
4c45483e
GW
93int
94nextc(cpp, rbp)
95 char **cpp;
96 struct ringb *rbp;
97{
15637ed4
RG
98
99 if (*cpp == rbp->rb_tl) return (0);
100 else { char *cp;
101 cp = *cpp;
102 *cpp = RB_SUCC(rbp, cp);
103 return(*cp);
104 }
105}
106
4c45483e
GW
107int
108ungetc(c, rbp)
109 int c;
110 struct ringb *rbp;
15637ed4
RG
111{
112 char *backp;
113
114 /* ring buffer full? */
115 if ( (backp = RB_PRED(rbp, rbp->rb_hd)) == rbp->rb_tl) return (-1);
116 rbp->rb_hd = backp;
117
118 /* stuff character */
119 *rbp->rb_hd = c;
120 return(0);
121}
122
4c45483e
GW
123int
124unputc(rbp)
125 struct ringb *rbp;
15637ed4
RG
126{
127 char *backp;
128 int c;
129
130 /* ring buffer empty? */
131 if (rbp->rb_hd == rbp->rb_tl) return(-1);
132
133 /* backup buffer and dig out previous character */
134 backp = RB_PRED(rbp, rbp->rb_tl);
135 c = *(u_char *)backp;
136 rbp->rb_tl = backp;
137
138 return(c);
139}
140
141#define peekc(rbp) (*(rbp)->rb_hd)
142
4c45483e
GW
143void
144initrb(rbp)
145 struct ringb *rbp;
146{
15637ed4
RG
147 rbp->rb_hd = rbp->rb_tl = rbp->rb_buf;
148}
149
150/*
151 * Example code for contiguous operations:
152 ...
153 nc = RB_CONTIGPUT(&rb);
154 if (nc) {
155 if (nc > 9) nc = 9;
156 bcopy("ABCDEFGHI", rb.rb_tl, nc);
157 rb.rb_tl += nc;
158 rb.rb_tl = RB_ROLLOVER(&rb, rb.rb_tl);
159 }
160 ...
161 ...
162 nc = RB_CONTIGGET(&rb);
163 if (nc) {
164 if (nc > 79) nc = 79;
165 bcopy(rb.rb_hd, stringbuf, nc);
166 rb.rb_hd += nc;
167 rb.rb_hd = RB_ROLLOVER(&rb, rb.rb_hd);
168 stringbuf[nc] = 0;
169 printf("%s|", stringbuf);
170 }
171 ...
172 */
173
174/*
175 * Concatenate ring buffers.
176 */
4c45483e 177void
15637ed4
RG
178catb(from, to)
179 struct ringb *from, *to;
180{
181 size_t nfromleft;
182 size_t nfromright;
183
184 nfromright = RB_CONTIGGET(from);
185 rb_write(to, from->rb_hd, nfromright);
186 from->rb_hd += nfromright;
187 from->rb_hd = RB_ROLLOVER(from, from->rb_hd);
188 nfromleft = RB_CONTIGGET(from);
189 rb_write(to, from->rb_hd, nfromleft);
190 from->rb_hd += nfromleft;
191}
192
193/*
194 * Copy ordinary buffer to ring buffer, return count of what fitted.
195 */
196size_t rb_write(to, buf, nfrom)
197 struct ringb *to;
198 char *buf;
199 size_t nfrom;
200{
201 char *toleft;
202 size_t ntoleft;
203 size_t ntoright;
204
205 ntoright = RB_CONTIGPUT(to);
206 if (nfrom < ntoright) {
207 bcopy(buf, to->rb_tl, nfrom);
208 to->rb_tl += nfrom;
209 return (nfrom);
210 }
211 bcopy(buf, to->rb_tl, ntoright);
212 nfrom -= ntoright;
213 toleft = to->rb_buf; /* fast RB_ROLLOVER */
214 ntoleft = to->rb_hd - toleft; /* fast RB_CONTIGPUT */
215 if (nfrom > ntoleft)
216 nfrom = ntoleft;
217 bcopy(buf + ntoright, toleft, nfrom);
218 to->rb_tl = toleft + nfrom;
219 return (ntoright + nfrom);
220}