xref: /freebsd/sys/kern/tty_inq.c (revision 41ba7e9b13c4c50e6110d72e5a9eb9b91fe981fd)
1bc093719SEd Schouten /*-
2bc093719SEd Schouten  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3bc093719SEd Schouten  * All rights reserved.
4bc093719SEd Schouten  *
5bc093719SEd Schouten  * Portions of this software were developed under sponsorship from Snow
6bc093719SEd Schouten  * B.V., the Netherlands.
7bc093719SEd Schouten  *
8bc093719SEd Schouten  * Redistribution and use in source and binary forms, with or without
9bc093719SEd Schouten  * modification, are permitted provided that the following conditions
10bc093719SEd Schouten  * are met:
11bc093719SEd Schouten  * 1. Redistributions of source code must retain the above copyright
12bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer.
13bc093719SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
14bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
15bc093719SEd Schouten  *    documentation and/or other materials provided with the distribution.
16bc093719SEd Schouten  *
17bc093719SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18bc093719SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bc093719SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bc093719SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bc093719SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bc093719SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bc093719SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bc093719SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bc093719SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bc093719SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bc093719SEd Schouten  * SUCH DAMAGE.
28bc093719SEd Schouten  */
29bc093719SEd Schouten 
30bc093719SEd Schouten #include <sys/cdefs.h>
31bc093719SEd Schouten __FBSDID("$FreeBSD$");
32bc093719SEd Schouten 
33bc093719SEd Schouten #include <sys/param.h>
34bc093719SEd Schouten #include <sys/kernel.h>
35bc093719SEd Schouten #include <sys/lock.h>
36bc093719SEd Schouten #include <sys/queue.h>
37bc093719SEd Schouten #include <sys/sysctl.h>
38bc093719SEd Schouten #include <sys/systm.h>
39bc093719SEd Schouten #include <sys/tty.h>
40bc093719SEd Schouten #include <sys/uio.h>
41bc093719SEd Schouten 
42bc093719SEd Schouten #include <vm/uma.h>
43bc093719SEd Schouten 
44bc093719SEd Schouten /*
45bc093719SEd Schouten  * TTY input queue buffering.
46bc093719SEd Schouten  *
47bc093719SEd Schouten  * Unlike the output queue, the input queue has more features that are
48bc093719SEd Schouten  * needed to properly implement various features offered by the TTY
49bc093719SEd Schouten  * interface:
50bc093719SEd Schouten  *
51bc093719SEd Schouten  * - Data can be removed from the tail of the queue, which is used to
52bc093719SEd Schouten  *   implement backspace.
53bc093719SEd Schouten  * - Once in a while, input has to be `canonicalized'. When ICANON is
54bc093719SEd Schouten  *   turned on, this will be done after a CR has been inserted.
55bc093719SEd Schouten  *   Otherwise, it should be done after any character has been inserted.
56bc093719SEd Schouten  * - The input queue can store one bit per byte, called the quoting bit.
57bc093719SEd Schouten  *   This bit is used by TTYDISC to make backspace work on quoted
58bc093719SEd Schouten  *   characters.
59bc093719SEd Schouten  *
60bc093719SEd Schouten  * In most cases, there is probably less input than output, so unlike
61bc093719SEd Schouten  * the outq, we'll stick to 128 byte blocks here.
62bc093719SEd Schouten  */
63bc093719SEd Schouten 
64bc093719SEd Schouten /* Statistics. */
65bc093719SEd Schouten static long ttyinq_nfast = 0;
66bc093719SEd Schouten SYSCTL_LONG(_kern, OID_AUTO, tty_inq_nfast, CTLFLAG_RD,
67bc093719SEd Schouten 	&ttyinq_nfast, 0, "Unbuffered reads to userspace on input");
68bc093719SEd Schouten static long ttyinq_nslow = 0;
69bc093719SEd Schouten SYSCTL_LONG(_kern, OID_AUTO, tty_inq_nslow, CTLFLAG_RD,
70bc093719SEd Schouten 	&ttyinq_nslow, 0, "Buffered reads to userspace on input");
71bc093719SEd Schouten 
72bc093719SEd Schouten #define TTYINQ_QUOTESIZE	(TTYINQ_DATASIZE / BMSIZE)
73bc093719SEd Schouten #define BMSIZE			32
74bc093719SEd Schouten #define GETBIT(tib,boff) \
75bc093719SEd Schouten 	((tib)->tib_quotes[(boff) / BMSIZE] & (1 << ((boff) % BMSIZE)))
76bc093719SEd Schouten #define SETBIT(tib,boff) \
77bc093719SEd Schouten 	((tib)->tib_quotes[(boff) / BMSIZE] |= (1 << ((boff) % BMSIZE)))
78bc093719SEd Schouten #define CLRBIT(tib,boff) \
79bc093719SEd Schouten 	((tib)->tib_quotes[(boff) / BMSIZE] &= ~(1 << ((boff) % BMSIZE)))
80bc093719SEd Schouten 
81bc093719SEd Schouten struct ttyinq_block {
8241ba7e9bSEd Schouten 	struct ttyinq_block	*tib_prev;
8341ba7e9bSEd Schouten 	struct ttyinq_block	*tib_next;
84bc093719SEd Schouten 	uint32_t		tib_quotes[TTYINQ_QUOTESIZE];
85bc093719SEd Schouten 	char			tib_data[TTYINQ_DATASIZE];
86bc093719SEd Schouten };
87bc093719SEd Schouten 
88bc093719SEd Schouten static uma_zone_t ttyinq_zone;
89bc093719SEd Schouten 
9041ba7e9bSEd Schouten #define	TTYINQ_INSERT_TAIL(ti, tib) do {				\
9141ba7e9bSEd Schouten 	if (ti->ti_end == 0) {						\
9241ba7e9bSEd Schouten 		tib->tib_prev = NULL;					\
9341ba7e9bSEd Schouten 		tib->tib_next = ti->ti_firstblock;			\
9441ba7e9bSEd Schouten 		ti->ti_firstblock = tib;				\
9541ba7e9bSEd Schouten 	} else {							\
9641ba7e9bSEd Schouten 		tib->tib_prev = ti->ti_lastblock;			\
9741ba7e9bSEd Schouten 		tib->tib_next = ti->ti_lastblock->tib_next;		\
9841ba7e9bSEd Schouten 		ti->ti_lastblock->tib_next = tib;			\
9941ba7e9bSEd Schouten 	}								\
10041ba7e9bSEd Schouten 	if (tib->tib_next != NULL)					\
10141ba7e9bSEd Schouten 		tib->tib_next->tib_prev = tib;				\
10241ba7e9bSEd Schouten 	ti->ti_nblocks++;						\
10341ba7e9bSEd Schouten } while (0)
10441ba7e9bSEd Schouten 
10541ba7e9bSEd Schouten #define	TTYINQ_REMOVE_HEAD(ti) do {					\
10641ba7e9bSEd Schouten 	ti->ti_firstblock = ti->ti_firstblock->tib_next;		\
10741ba7e9bSEd Schouten 	if (ti->ti_firstblock != NULL)					\
10841ba7e9bSEd Schouten 		ti->ti_firstblock->tib_prev = NULL;			\
10941ba7e9bSEd Schouten 	ti->ti_nblocks--;						\
11041ba7e9bSEd Schouten } while (0)
11141ba7e9bSEd Schouten 
11241ba7e9bSEd Schouten #define	TTYINQ_RECYCLE(ti, tib) do {					\
11341ba7e9bSEd Schouten 	if (ti->ti_quota <= ti->ti_nblocks)				\
11441ba7e9bSEd Schouten 		uma_zfree(ttyinq_zone, tib);				\
11541ba7e9bSEd Schouten 	else								\
11641ba7e9bSEd Schouten 		TTYINQ_INSERT_TAIL(ti, tib);				\
11741ba7e9bSEd Schouten } while (0)
11841ba7e9bSEd Schouten 
119bc093719SEd Schouten void
120bc093719SEd Schouten ttyinq_setsize(struct ttyinq *ti, struct tty *tp, size_t size)
121bc093719SEd Schouten {
122bc093719SEd Schouten 	struct ttyinq_block *tib;
123bc093719SEd Schouten 
12474bb9e3aSEd Schouten 	ti->ti_quota = howmany(size, TTYINQ_DATASIZE);
125bc093719SEd Schouten 
12674bb9e3aSEd Schouten 	while (ti->ti_quota > ti->ti_nblocks) {
127bc093719SEd Schouten 		/*
128bc093719SEd Schouten 		 * List is getting bigger.
129bc093719SEd Schouten 		 * Add new blocks to the tail of the list.
130bc093719SEd Schouten 		 *
131bc093719SEd Schouten 		 * We must unlock the TTY temporarily, because we need
132bc093719SEd Schouten 		 * to allocate memory. This won't be a problem, because
133bc093719SEd Schouten 		 * in the worst case, another thread ends up here, which
134bc093719SEd Schouten 		 * may cause us to allocate too many blocks, but this
135bc093719SEd Schouten 		 * will be caught by the loop below.
136bc093719SEd Schouten 		 */
137bc093719SEd Schouten 		tty_unlock(tp);
138bc093719SEd Schouten 		tib = uma_zalloc(ttyinq_zone, M_WAITOK);
139bc093719SEd Schouten 		tty_lock(tp);
140bc093719SEd Schouten 
14141ba7e9bSEd Schouten 		TTYINQ_INSERT_TAIL(ti, tib);
142bc093719SEd Schouten 	}
14374bb9e3aSEd Schouten }
144bc093719SEd Schouten 
14574bb9e3aSEd Schouten void
14674bb9e3aSEd Schouten ttyinq_free(struct ttyinq *ti)
14774bb9e3aSEd Schouten {
14874bb9e3aSEd Schouten 	struct ttyinq_block *tib;
149bc093719SEd Schouten 
15074bb9e3aSEd Schouten 	ttyinq_flush(ti);
15174bb9e3aSEd Schouten 	ti->ti_quota = 0;
15274bb9e3aSEd Schouten 
15341ba7e9bSEd Schouten 	while ((tib = ti->ti_firstblock) != NULL) {
15441ba7e9bSEd Schouten 		TTYINQ_REMOVE_HEAD(ti);
155bc093719SEd Schouten 		uma_zfree(ttyinq_zone, tib);
156bc093719SEd Schouten 	}
15774bb9e3aSEd Schouten 
15874bb9e3aSEd Schouten 	MPASS(ti->ti_nblocks == 0);
159bc093719SEd Schouten }
160bc093719SEd Schouten 
161bc093719SEd Schouten int
162bc093719SEd Schouten ttyinq_read_uio(struct ttyinq *ti, struct tty *tp, struct uio *uio,
163bc093719SEd Schouten     size_t rlen, size_t flen)
164bc093719SEd Schouten {
165bc093719SEd Schouten 
166bc093719SEd Schouten 	MPASS(rlen <= uio->uio_resid);
167bc093719SEd Schouten 
168bc093719SEd Schouten 	while (rlen > 0) {
169bc093719SEd Schouten 		int error;
170bc093719SEd Schouten 		struct ttyinq_block *tib;
171bc093719SEd Schouten 		size_t cbegin, cend, clen;
172bc093719SEd Schouten 
173bc093719SEd Schouten 		/* See if there still is data. */
174bc093719SEd Schouten 		if (ti->ti_begin == ti->ti_linestart)
175bc093719SEd Schouten 			return (0);
17641ba7e9bSEd Schouten 		tib = ti->ti_firstblock;
177bc093719SEd Schouten 		if (tib == NULL)
178bc093719SEd Schouten 			return (0);
179bc093719SEd Schouten 
180bc093719SEd Schouten 		/*
181bc093719SEd Schouten 		 * The end address should be the lowest of these three:
182bc093719SEd Schouten 		 * - The write pointer
183bc093719SEd Schouten 		 * - The blocksize - we can't read beyond the block
184bc093719SEd Schouten 		 * - The end address if we could perform the full read
185bc093719SEd Schouten 		 */
186bc093719SEd Schouten 		cbegin = ti->ti_begin;
187bc093719SEd Schouten 		cend = MIN(MIN(ti->ti_linestart, ti->ti_begin + rlen),
188bc093719SEd Schouten 		    TTYINQ_DATASIZE);
189bc093719SEd Schouten 		clen = cend - cbegin;
190bc093719SEd Schouten 		MPASS(clen >= flen);
191bc093719SEd Schouten 		rlen -= clen;
192bc093719SEd Schouten 
193bc093719SEd Schouten 		/*
194bc093719SEd Schouten 		 * We can prevent buffering in some cases:
195bc093719SEd Schouten 		 * - We need to read the block until the end.
196bc093719SEd Schouten 		 * - We don't need to read the block until the end, but
197bc093719SEd Schouten 		 *   there is no data beyond it, which allows us to move
198bc093719SEd Schouten 		 *   the write pointer to a new block.
199bc093719SEd Schouten 		 */
200bc093719SEd Schouten 		if (cend == TTYINQ_DATASIZE || cend == ti->ti_end) {
201bc093719SEd Schouten 			atomic_add_long(&ttyinq_nfast, 1);
202bc093719SEd Schouten 
203bc093719SEd Schouten 			/*
204bc093719SEd Schouten 			 * Fast path: zero copy. Remove the first block,
205bc093719SEd Schouten 			 * so we can unlock the TTY temporarily.
206bc093719SEd Schouten 			 */
20741ba7e9bSEd Schouten 			TTYINQ_REMOVE_HEAD(ti);
208bc093719SEd Schouten 			ti->ti_begin = 0;
209bc093719SEd Schouten 
210bc093719SEd Schouten 			/*
211bc093719SEd Schouten 			 * Because we remove the first block, we must
212bc093719SEd Schouten 			 * fix up the block offsets.
213bc093719SEd Schouten 			 */
214bc093719SEd Schouten #define CORRECT_BLOCK(t) do {			\
21541ba7e9bSEd Schouten 	if (t <= TTYINQ_DATASIZE)		\
216bc093719SEd Schouten 		t = 0;				\
21741ba7e9bSEd Schouten 	else					\
218bc093719SEd Schouten 		t -= TTYINQ_DATASIZE;		\
219bc093719SEd Schouten } while (0)
220bc093719SEd Schouten 			CORRECT_BLOCK(ti->ti_linestart);
221bc093719SEd Schouten 			CORRECT_BLOCK(ti->ti_reprint);
222bc093719SEd Schouten 			CORRECT_BLOCK(ti->ti_end);
223bc093719SEd Schouten #undef CORRECT_BLOCK
224bc093719SEd Schouten 
225bc093719SEd Schouten 			/*
226bc093719SEd Schouten 			 * Temporary unlock and copy the data to
227bc093719SEd Schouten 			 * userspace. We may need to flush trailing
228bc093719SEd Schouten 			 * bytes, like EOF characters.
229bc093719SEd Schouten 			 */
230bc093719SEd Schouten 			tty_unlock(tp);
231bc093719SEd Schouten 			error = uiomove(tib->tib_data + cbegin,
232bc093719SEd Schouten 			    clen - flen, uio);
233bc093719SEd Schouten 			tty_lock(tp);
234bc093719SEd Schouten 
235bc093719SEd Schouten 			/* Block can now be readded to the list. */
23641ba7e9bSEd Schouten 			TTYINQ_RECYCLE(ti, tib);
237bc093719SEd Schouten 		} else {
238bc093719SEd Schouten 			char ob[TTYINQ_DATASIZE - 1];
239bc093719SEd Schouten 			atomic_add_long(&ttyinq_nslow, 1);
240bc093719SEd Schouten 
241bc093719SEd Schouten 			/*
242bc093719SEd Schouten 			 * Slow path: store data in a temporary buffer.
243bc093719SEd Schouten 			 */
244bc093719SEd Schouten 			memcpy(ob, tib->tib_data + cbegin, clen - flen);
245bc093719SEd Schouten 			ti->ti_begin += clen;
246bc093719SEd Schouten 			MPASS(ti->ti_begin < TTYINQ_DATASIZE);
247bc093719SEd Schouten 
248bc093719SEd Schouten 			/* Temporary unlock and copy the data to userspace. */
249bc093719SEd Schouten 			tty_unlock(tp);
250bc093719SEd Schouten 			error = uiomove(ob, clen - flen, uio);
251bc093719SEd Schouten 			tty_lock(tp);
25274bb9e3aSEd Schouten 		}
253bc093719SEd Schouten 
254bc093719SEd Schouten 		if (error != 0)
255bc093719SEd Schouten 			return (error);
256bc093719SEd Schouten 		if (tty_gone(tp))
257bc093719SEd Schouten 			return (ENXIO);
258bc093719SEd Schouten 	}
259bc093719SEd Schouten 
260bc093719SEd Schouten 	return (0);
261bc093719SEd Schouten }
262bc093719SEd Schouten 
263bc093719SEd Schouten static __inline void
264bc093719SEd Schouten ttyinq_set_quotes(struct ttyinq_block *tib, size_t offset,
265bc093719SEd Schouten     size_t length, int value)
266bc093719SEd Schouten {
267bc093719SEd Schouten 
268bc093719SEd Schouten 	if (value) {
269bc093719SEd Schouten 		/* Set the bits. */
270bc093719SEd Schouten 		for (; length > 0; length--, offset++)
271bc093719SEd Schouten 			SETBIT(tib, offset);
272bc093719SEd Schouten 	} else {
273bc093719SEd Schouten 		/* Unset the bits. */
274bc093719SEd Schouten 		for (; length > 0; length--, offset++)
275bc093719SEd Schouten 			CLRBIT(tib, offset);
276bc093719SEd Schouten 	}
277bc093719SEd Schouten }
278bc093719SEd Schouten 
279bc093719SEd Schouten size_t
280bc093719SEd Schouten ttyinq_write(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
281bc093719SEd Schouten {
282bc093719SEd Schouten 	const char *cbuf = buf;
283bc093719SEd Schouten 	struct ttyinq_block *tib;
284bc093719SEd Schouten 	unsigned int boff;
285bc093719SEd Schouten 	size_t l;
286bc093719SEd Schouten 
287bc093719SEd Schouten 	while (nbytes > 0) {
288bc093719SEd Schouten 		boff = ti->ti_end % TTYINQ_DATASIZE;
289bc093719SEd Schouten 
290bc093719SEd Schouten 		if (ti->ti_end == 0) {
291bc093719SEd Schouten 			/* First time we're being used or drained. */
292bc093719SEd Schouten 			MPASS(ti->ti_begin == 0);
29341ba7e9bSEd Schouten 			tib = ti->ti_firstblock;
294bc093719SEd Schouten 			if (tib == NULL) {
295bc093719SEd Schouten 				/* Queue has no blocks. */
296bc093719SEd Schouten 				break;
297bc093719SEd Schouten 			}
29841ba7e9bSEd Schouten 			ti->ti_lastblock = tib;
299bc093719SEd Schouten 		} else if (boff == 0) {
300bc093719SEd Schouten 			/* We reached the end of this block on last write. */
30141ba7e9bSEd Schouten 			tib = ti->ti_lastblock->tib_next;
302bc093719SEd Schouten 			if (tib == NULL) {
303bc093719SEd Schouten 				/* We've reached the watermark. */
304bc093719SEd Schouten 				break;
305bc093719SEd Schouten 			}
306bc093719SEd Schouten 			ti->ti_lastblock = tib;
30741ba7e9bSEd Schouten 		} else {
30841ba7e9bSEd Schouten 			tib = ti->ti_lastblock;
309bc093719SEd Schouten 		}
310bc093719SEd Schouten 
311bc093719SEd Schouten 		/* Don't copy more than was requested. */
312bc093719SEd Schouten 		l = MIN(nbytes, TTYINQ_DATASIZE - boff);
313bc093719SEd Schouten 		MPASS(l > 0);
314bc093719SEd Schouten 		memcpy(tib->tib_data + boff, cbuf, l);
315bc093719SEd Schouten 
316bc093719SEd Schouten 		/* Set the quoting bits for the proper region. */
317bc093719SEd Schouten 		ttyinq_set_quotes(tib, boff, l, quote);
318bc093719SEd Schouten 
319bc093719SEd Schouten 		cbuf += l;
320bc093719SEd Schouten 		nbytes -= l;
321bc093719SEd Schouten 		ti->ti_end += l;
322bc093719SEd Schouten 	}
323bc093719SEd Schouten 
324bc093719SEd Schouten 	return (cbuf - (const char *)buf);
325bc093719SEd Schouten }
326bc093719SEd Schouten 
327bc093719SEd Schouten int
328bc093719SEd Schouten ttyinq_write_nofrag(struct ttyinq *ti, const void *buf, size_t nbytes, int quote)
329bc093719SEd Schouten {
330bc093719SEd Schouten 	size_t ret;
331bc093719SEd Schouten 
332bc093719SEd Schouten 	if (ttyinq_bytesleft(ti) < nbytes)
333bc093719SEd Schouten 		return (-1);
334bc093719SEd Schouten 
335bc093719SEd Schouten 	/* We should always be able to write it back. */
336bc093719SEd Schouten 	ret = ttyinq_write(ti, buf, nbytes, quote);
337bc093719SEd Schouten 	MPASS(ret == nbytes);
338bc093719SEd Schouten 
339bc093719SEd Schouten 	return (0);
340bc093719SEd Schouten }
341bc093719SEd Schouten 
342bc093719SEd Schouten void
343bc093719SEd Schouten ttyinq_canonicalize(struct ttyinq *ti)
344bc093719SEd Schouten {
345bc093719SEd Schouten 
346bc093719SEd Schouten 	ti->ti_linestart = ti->ti_reprint = ti->ti_end;
347bc093719SEd Schouten 	ti->ti_startblock = ti->ti_reprintblock = ti->ti_lastblock;
348bc093719SEd Schouten }
349bc093719SEd Schouten 
350bc093719SEd Schouten size_t
351bc093719SEd Schouten ttyinq_findchar(struct ttyinq *ti, const char *breakc, size_t maxlen,
352bc093719SEd Schouten     char *lastc)
353bc093719SEd Schouten {
35441ba7e9bSEd Schouten 	struct ttyinq_block *tib = ti->ti_firstblock;
355bc093719SEd Schouten 	unsigned int boff = ti->ti_begin;
356bc093719SEd Schouten 	unsigned int bend = MIN(MIN(TTYINQ_DATASIZE, ti->ti_linestart),
357bc093719SEd Schouten 	    ti->ti_begin + maxlen);
358bc093719SEd Schouten 
359bc093719SEd Schouten 	MPASS(maxlen > 0);
360bc093719SEd Schouten 
361bc093719SEd Schouten 	if (tib == NULL)
362bc093719SEd Schouten 		return (0);
363bc093719SEd Schouten 
364bc093719SEd Schouten 	while (boff < bend) {
365bc093719SEd Schouten 		if (index(breakc, tib->tib_data[boff]) && !GETBIT(tib, boff)) {
366bc093719SEd Schouten 			*lastc = tib->tib_data[boff];
367bc093719SEd Schouten 			return (boff - ti->ti_begin + 1);
368bc093719SEd Schouten 		}
369bc093719SEd Schouten 		boff++;
370bc093719SEd Schouten 	}
371bc093719SEd Schouten 
372bc093719SEd Schouten 	/* Not found - just process the entire block. */
373bc093719SEd Schouten 	return (bend - ti->ti_begin);
374bc093719SEd Schouten }
375bc093719SEd Schouten 
376bc093719SEd Schouten void
377bc093719SEd Schouten ttyinq_flush(struct ttyinq *ti)
378bc093719SEd Schouten {
379bc093719SEd Schouten 
380bc093719SEd Schouten 	ti->ti_begin = 0;
381bc093719SEd Schouten 	ti->ti_linestart = 0;
382bc093719SEd Schouten 	ti->ti_reprint = 0;
383bc093719SEd Schouten 	ti->ti_end = 0;
384bc093719SEd Schouten }
385bc093719SEd Schouten 
386bc093719SEd Schouten #if 0
387bc093719SEd Schouten void
388bc093719SEd Schouten ttyinq_flush_safe(struct ttyinq *ti)
389bc093719SEd Schouten {
390bc093719SEd Schouten 	struct ttyinq_block *tib;
391bc093719SEd Schouten 
392bc093719SEd Schouten 	ttyinq_flush(ti);
393bc093719SEd Schouten 
394bc093719SEd Schouten 	/* Zero all data in the input queue to make it more safe */
395bc093719SEd Schouten 	TAILQ_FOREACH(tib, &ti->ti_list, tib_list) {
396bc093719SEd Schouten 		bzero(&tib->tib_quotes, sizeof tib->tib_quotes);
397bc093719SEd Schouten 		bzero(&tib->tib_data, sizeof tib->tib_data);
398bc093719SEd Schouten 	}
399bc093719SEd Schouten }
400bc093719SEd Schouten #endif
401bc093719SEd Schouten 
402bc093719SEd Schouten int
403bc093719SEd Schouten ttyinq_peekchar(struct ttyinq *ti, char *c, int *quote)
404bc093719SEd Schouten {
405bc093719SEd Schouten 	unsigned int boff;
406bc093719SEd Schouten 	struct ttyinq_block *tib = ti->ti_lastblock;
407bc093719SEd Schouten 
408bc093719SEd Schouten 	if (ti->ti_linestart == ti->ti_end)
409bc093719SEd Schouten 		return (-1);
410bc093719SEd Schouten 
411bc093719SEd Schouten 	MPASS(ti->ti_end > 0);
412bc093719SEd Schouten 	boff = (ti->ti_end - 1) % TTYINQ_DATASIZE;
413bc093719SEd Schouten 
414bc093719SEd Schouten 	*c = tib->tib_data[boff];
415bc093719SEd Schouten 	*quote = GETBIT(tib, boff);
416bc093719SEd Schouten 
417bc093719SEd Schouten 	return (0);
418bc093719SEd Schouten }
419bc093719SEd Schouten 
420bc093719SEd Schouten void
421bc093719SEd Schouten ttyinq_unputchar(struct ttyinq *ti)
422bc093719SEd Schouten {
423bc093719SEd Schouten 
424bc093719SEd Schouten 	MPASS(ti->ti_linestart < ti->ti_end);
425bc093719SEd Schouten 
426bc093719SEd Schouten 	if (--ti->ti_end % TTYINQ_DATASIZE == 0) {
427bc093719SEd Schouten 		/* Roll back to the previous block. */
42841ba7e9bSEd Schouten 		ti->ti_lastblock = ti->ti_lastblock->tib_prev;
429bc093719SEd Schouten 		/*
430bc093719SEd Schouten 		 * This can only fail if we are unputchar()'ing the
431bc093719SEd Schouten 		 * first character in the queue.
432bc093719SEd Schouten 		 */
433bc093719SEd Schouten 		MPASS((ti->ti_lastblock == NULL) == (ti->ti_end == 0));
434bc093719SEd Schouten 	}
435bc093719SEd Schouten }
436bc093719SEd Schouten 
437bc093719SEd Schouten void
438bc093719SEd Schouten ttyinq_reprintpos_set(struct ttyinq *ti)
439bc093719SEd Schouten {
440bc093719SEd Schouten 
441bc093719SEd Schouten 	ti->ti_reprint = ti->ti_end;
442bc093719SEd Schouten 	ti->ti_reprintblock = ti->ti_lastblock;
443bc093719SEd Schouten }
444bc093719SEd Schouten 
445bc093719SEd Schouten void
446bc093719SEd Schouten ttyinq_reprintpos_reset(struct ttyinq *ti)
447bc093719SEd Schouten {
448bc093719SEd Schouten 
449bc093719SEd Schouten 	ti->ti_reprint = ti->ti_linestart;
450bc093719SEd Schouten 	ti->ti_reprintblock = ti->ti_startblock;
451bc093719SEd Schouten }
452bc093719SEd Schouten 
453bc093719SEd Schouten static void
454bc093719SEd Schouten ttyinq_line_iterate(struct ttyinq *ti,
455bc093719SEd Schouten     ttyinq_line_iterator_t *iterator, void *data,
456bc093719SEd Schouten     unsigned int offset, struct ttyinq_block *tib)
457bc093719SEd Schouten {
458bc093719SEd Schouten 	unsigned int boff;
459bc093719SEd Schouten 
460bc093719SEd Schouten 	/* Use the proper block when we're at the queue head. */
461bc093719SEd Schouten 	if (offset == 0)
46241ba7e9bSEd Schouten 		tib = ti->ti_firstblock;
463bc093719SEd Schouten 
464bc093719SEd Schouten 	/* Iterate all characters and call the iterator function. */
465bc093719SEd Schouten 	for (; offset < ti->ti_end; offset++) {
466bc093719SEd Schouten 		boff = offset % TTYINQ_DATASIZE;
467bc093719SEd Schouten 		MPASS(tib != NULL);
468bc093719SEd Schouten 
469bc093719SEd Schouten 		/* Call back the iterator function. */
470bc093719SEd Schouten 		iterator(data, tib->tib_data[boff], GETBIT(tib, boff));
471bc093719SEd Schouten 
472bc093719SEd Schouten 		/* Last byte iterated - go to the next block. */
473bc093719SEd Schouten 		if (boff == TTYINQ_DATASIZE - 1)
47441ba7e9bSEd Schouten 			tib = tib->tib_next;
475bc093719SEd Schouten 		MPASS(tib != NULL);
476bc093719SEd Schouten 	}
477bc093719SEd Schouten }
478bc093719SEd Schouten 
479bc093719SEd Schouten void
480bc093719SEd Schouten ttyinq_line_iterate_from_linestart(struct ttyinq *ti,
481bc093719SEd Schouten     ttyinq_line_iterator_t *iterator, void *data)
482bc093719SEd Schouten {
483bc093719SEd Schouten 
484bc093719SEd Schouten 	ttyinq_line_iterate(ti, iterator, data,
485bc093719SEd Schouten 	    ti->ti_linestart, ti->ti_startblock);
486bc093719SEd Schouten }
487bc093719SEd Schouten 
488bc093719SEd Schouten void
489bc093719SEd Schouten ttyinq_line_iterate_from_reprintpos(struct ttyinq *ti,
490bc093719SEd Schouten     ttyinq_line_iterator_t *iterator, void *data)
491bc093719SEd Schouten {
492bc093719SEd Schouten 
493bc093719SEd Schouten 	ttyinq_line_iterate(ti, iterator, data,
494bc093719SEd Schouten 	    ti->ti_reprint, ti->ti_reprintblock);
495bc093719SEd Schouten }
496bc093719SEd Schouten 
497bc093719SEd Schouten static void
498bc093719SEd Schouten ttyinq_startup(void *dummy)
499bc093719SEd Schouten {
500bc093719SEd Schouten 
501bc093719SEd Schouten 	ttyinq_zone = uma_zcreate("ttyinq", sizeof(struct ttyinq_block),
502bc093719SEd Schouten 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
503bc093719SEd Schouten }
504bc093719SEd Schouten 
505bc093719SEd Schouten SYSINIT(ttyinq, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyinq_startup, NULL);
506