xref: /freebsd/sys/kern/tty_ttydisc.c (revision a1215e37a4a98f22c84d4512dd380761376e8231)
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/fcntl.h>
35bc093719SEd Schouten #include <sys/filio.h>
36bc093719SEd Schouten #include <sys/kernel.h>
37bc093719SEd Schouten #include <sys/signal.h>
38bc093719SEd Schouten #include <sys/sysctl.h>
39bc093719SEd Schouten #include <sys/systm.h>
40bc093719SEd Schouten #include <sys/tty.h>
41bc093719SEd Schouten #include <sys/ttycom.h>
42bc093719SEd Schouten #include <sys/ttydefaults.h>
43bc093719SEd Schouten #include <sys/uio.h>
44bc093719SEd Schouten #include <sys/vnode.h>
45bc093719SEd Schouten 
46bc093719SEd Schouten /*
47bc093719SEd Schouten  * Standard TTYDISC `termios' line discipline.
48bc093719SEd Schouten  */
49bc093719SEd Schouten 
50bc093719SEd Schouten /* Statistics. */
51bc093719SEd Schouten static long tty_nin = 0;
52bc093719SEd Schouten SYSCTL_LONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
53bc093719SEd Schouten 	&tty_nin, 0, "Total amount of bytes received");
54bc093719SEd Schouten static long tty_nout = 0;
55bc093719SEd Schouten SYSCTL_LONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
56bc093719SEd Schouten 	&tty_nout, 0, "Total amount of bytes transmitted");
57bc093719SEd Schouten 
58bc093719SEd Schouten /* termios comparison macro's. */
59bc093719SEd Schouten #define	CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
60bc093719SEd Schouten 			tp->t_termios.c_cc[v] == (c))
61bc093719SEd Schouten #define	CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
62bc093719SEd Schouten 
63bc093719SEd Schouten /* Characters that cannot be modified through c_cc. */
64bc093719SEd Schouten #define CTAB	'\t'
65bc093719SEd Schouten #define CNL	'\n'
66bc093719SEd Schouten #define CCR	'\r'
67bc093719SEd Schouten 
68bc093719SEd Schouten /* Character is a control character. */
69bc093719SEd Schouten #define CTL_VALID(c)	((c) == 0x7f || (unsigned char)(c) < 0x20)
70bc093719SEd Schouten /* Control character should be processed on echo. */
71bc093719SEd Schouten #define CTL_ECHO(c,q)	(!(q) && ((c) == CERASE2 || (c) == CTAB || \
72bc093719SEd Schouten     (c) == CNL || (c) == CCR))
73bc093719SEd Schouten /* Control character should be printed using ^X notation. */
74bc093719SEd Schouten #define CTL_PRINT(c,q)	((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
75bc093719SEd Schouten     ((q) || ((c) != CTAB && (c) != CNL))))
76bc093719SEd Schouten /* Character is whitespace. */
77bc093719SEd Schouten #define CTL_WHITE(c)	((c) == ' ' || (c) == CTAB)
78bc093719SEd Schouten /* Character is alphanumeric. */
79bc093719SEd Schouten #define CTL_ALNUM(c)	(((c) >= '0' && (c) <= '9') || \
80bc093719SEd Schouten     ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
81bc093719SEd Schouten 
82a1215e37SEd Schouten #define	TTY_STACKBUF	256
83a1215e37SEd Schouten 
84bc093719SEd Schouten void
85bc093719SEd Schouten ttydisc_open(struct tty *tp)
86bc093719SEd Schouten {
87bc093719SEd Schouten 	ttydisc_optimize(tp);
88bc093719SEd Schouten }
89bc093719SEd Schouten 
90bc093719SEd Schouten void
91bc093719SEd Schouten ttydisc_close(struct tty *tp)
92bc093719SEd Schouten {
93bc093719SEd Schouten 
94bc093719SEd Schouten 	/* Clean up our flags when leaving the discipline. */
95bc093719SEd Schouten 	tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
96bc093719SEd Schouten 
97bc093719SEd Schouten 	/* POSIX states we should flush when close() is called. */
98bc093719SEd Schouten 	ttyinq_flush(&tp->t_inq);
99bc093719SEd Schouten 	ttyoutq_flush(&tp->t_outq);
100bc093719SEd Schouten 
101bc093719SEd Schouten 	if (!tty_gone(tp)) {
102bc093719SEd Schouten 		ttydevsw_inwakeup(tp);
103bc093719SEd Schouten 		ttydevsw_outwakeup(tp);
104bc093719SEd Schouten 	}
105a1215e37SEd Schouten 
106a1215e37SEd Schouten 	if (ttyhook_hashook(tp, close))
107a1215e37SEd Schouten 		ttyhook_close(tp);
108bc093719SEd Schouten }
109bc093719SEd Schouten 
110bc093719SEd Schouten static int
111bc093719SEd Schouten ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
112bc093719SEd Schouten {
113bc093719SEd Schouten 	char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
114bc093719SEd Schouten 	int error;
115bc093719SEd Schouten 	size_t clen, flen = 0, n = 1;
116bc093719SEd Schouten 	unsigned char lastc = _POSIX_VDISABLE;
117bc093719SEd Schouten 
118bc093719SEd Schouten #define BREAK_ADD(c) do { \
119bc093719SEd Schouten 	if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE)	\
120bc093719SEd Schouten 		breakc[n++] = tp->t_termios.c_cc[c];	\
121bc093719SEd Schouten } while (0)
122bc093719SEd Schouten 	/* Determine which characters we should trigger on. */
123bc093719SEd Schouten 	BREAK_ADD(VEOF);
124bc093719SEd Schouten 	BREAK_ADD(VEOL);
125bc093719SEd Schouten #undef BREAK_ADD
126bc093719SEd Schouten 	breakc[n] = '\0';
127bc093719SEd Schouten 
128bc093719SEd Schouten 	do {
129bc093719SEd Schouten 		/*
130bc093719SEd Schouten 		 * Quite a tricky case: unlike the old TTY
131bc093719SEd Schouten 		 * implementation, this implementation copies data back
132bc093719SEd Schouten 		 * to userspace in large chunks. Unfortunately, we can't
133bc093719SEd Schouten 		 * calculate the line length on beforehand if it crosses
134bc093719SEd Schouten 		 * ttyinq_block boundaries, because multiple reads could
135bc093719SEd Schouten 		 * then make this code read beyond the newline.
136bc093719SEd Schouten 		 *
137bc093719SEd Schouten 		 * This is why we limit the read to:
138bc093719SEd Schouten 		 * - The size the user has requested
139bc093719SEd Schouten 		 * - The blocksize (done in tty_inq.c)
140bc093719SEd Schouten 		 * - The amount of bytes until the newline
141bc093719SEd Schouten 		 *
142bc093719SEd Schouten 		 * This causes the line length to be recalculated after
143bc093719SEd Schouten 		 * each block has been copied to userspace. This will
144bc093719SEd Schouten 		 * cause the TTY layer to return data in chunks using
145bc093719SEd Schouten 		 * the blocksize (except the first and last blocks).
146bc093719SEd Schouten 		 */
147bc093719SEd Schouten 		clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
148bc093719SEd Schouten 		    &lastc);
149bc093719SEd Schouten 
150bc093719SEd Schouten 		/* No more data. */
151bc093719SEd Schouten 		if (clen == 0) {
152bc093719SEd Schouten 			if (ioflag & IO_NDELAY)
153bc093719SEd Schouten 				return (EWOULDBLOCK);
154bc093719SEd Schouten 			else if (tp->t_flags & TF_ZOMBIE)
155bc093719SEd Schouten 				return (0);
156bc093719SEd Schouten 
157bc093719SEd Schouten 			error = tty_wait(tp, &tp->t_inwait);
158bc093719SEd Schouten 			if (error)
159bc093719SEd Schouten 				return (error);
160bc093719SEd Schouten 			continue;
161bc093719SEd Schouten 		}
162bc093719SEd Schouten 
163bc093719SEd Schouten 		/* Don't send the EOF char back to userspace. */
164bc093719SEd Schouten 		if (CMP_CC(VEOF, lastc))
165bc093719SEd Schouten 			flen = 1;
166bc093719SEd Schouten 
167bc093719SEd Schouten 		MPASS(flen <= clen);
168bc093719SEd Schouten 
169bc093719SEd Schouten 		/* Read and throw away the EOF character. */
170bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
171bc093719SEd Schouten 		if (error)
172bc093719SEd Schouten 			return (error);
173bc093719SEd Schouten 
174bc093719SEd Schouten 	} while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
175bc093719SEd Schouten 
176bc093719SEd Schouten 	return (0);
177bc093719SEd Schouten }
178bc093719SEd Schouten 
179bc093719SEd Schouten static int
180bc093719SEd Schouten ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
181bc093719SEd Schouten {
182bc093719SEd Schouten 	size_t vmin = tp->t_termios.c_cc[VMIN];
183bc093719SEd Schouten 	int oresid = uio->uio_resid;
184bc093719SEd Schouten 	int error;
185bc093719SEd Schouten 
186bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] == 0);
187bc093719SEd Schouten 
188bc093719SEd Schouten 	/*
189bc093719SEd Schouten 	 * This routine implements the easy cases of read()s while in
190bc093719SEd Schouten 	 * non-canonical mode, namely case B and D, where we don't have
191bc093719SEd Schouten 	 * any timers at all.
192bc093719SEd Schouten 	 */
193bc093719SEd Schouten 
194bc093719SEd Schouten 	for (;;) {
195bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
196bc093719SEd Schouten 		    uio->uio_resid, 0);
197bc093719SEd Schouten 		if (error)
198bc093719SEd Schouten 			return (error);
199bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
200bc093719SEd Schouten 			return (0);
201bc093719SEd Schouten 
202bc093719SEd Schouten 		/* We have to wait for more. */
203bc093719SEd Schouten 		if (ioflag & IO_NDELAY)
204bc093719SEd Schouten 			return (EWOULDBLOCK);
205bc093719SEd Schouten 		else if (tp->t_flags & TF_ZOMBIE)
206bc093719SEd Schouten 			return (0);
207bc093719SEd Schouten 
208bc093719SEd Schouten 		error = tty_wait(tp, &tp->t_inwait);
209bc093719SEd Schouten 		if (error)
210bc093719SEd Schouten 			return (error);
211bc093719SEd Schouten 	}
212bc093719SEd Schouten }
213bc093719SEd Schouten 
214bc093719SEd Schouten static int
215bc093719SEd Schouten ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
216bc093719SEd Schouten     int oresid)
217bc093719SEd Schouten {
218bc093719SEd Schouten 	size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
219bc093719SEd Schouten 	unsigned int vtime = tp->t_termios.c_cc[VTIME];
220bc093719SEd Schouten 	struct timeval end, now, left;
221bc093719SEd Schouten 	int error, hz;
222bc093719SEd Schouten 
223bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] != 0);
224bc093719SEd Schouten 
225bc093719SEd Schouten 	/* Determine when the read should be expired. */
226bc093719SEd Schouten 	end.tv_sec = vtime / 10;
227bc093719SEd Schouten 	end.tv_usec = (vtime % 10) * 100000;
228bc093719SEd Schouten 	getmicrotime(&now);
229bc093719SEd Schouten 	timevaladd(&end, &now);
230bc093719SEd Schouten 
231bc093719SEd Schouten 	for (;;) {
232bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
233bc093719SEd Schouten 		    uio->uio_resid, 0);
234bc093719SEd Schouten 		if (error)
235bc093719SEd Schouten 			return (error);
236bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
237bc093719SEd Schouten 			return (0);
238bc093719SEd Schouten 
239bc093719SEd Schouten 		/* Calculate how long we should wait. */
240bc093719SEd Schouten 		getmicrotime(&now);
241bc093719SEd Schouten 		if (timevalcmp(&now, &end, >))
242bc093719SEd Schouten 			return (0);
243bc093719SEd Schouten 		left = end;
244bc093719SEd Schouten 		timevalsub(&left, &now);
245bc093719SEd Schouten 		hz = tvtohz(&left);
246bc093719SEd Schouten 
247bc093719SEd Schouten 		/*
248bc093719SEd Schouten 		 * We have to wait for more. If the timer expires, we
249bc093719SEd Schouten 		 * should return a 0-byte read.
250bc093719SEd Schouten 		 */
251bc093719SEd Schouten 		if (ioflag & IO_NDELAY)
252bc093719SEd Schouten 			return (EWOULDBLOCK);
253bc093719SEd Schouten 		else if (tp->t_flags & TF_ZOMBIE)
254bc093719SEd Schouten 			return (0);
255bc093719SEd Schouten 
256bc093719SEd Schouten 		error = tty_timedwait(tp, &tp->t_inwait, hz);
257bc093719SEd Schouten 		if (error)
258bc093719SEd Schouten 			return (error == EWOULDBLOCK ? 0 : error);
259bc093719SEd Schouten 	}
260bc093719SEd Schouten 
261bc093719SEd Schouten 	return (0);
262bc093719SEd Schouten }
263bc093719SEd Schouten 
264bc093719SEd Schouten static int
265bc093719SEd Schouten ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
266bc093719SEd Schouten {
267bc093719SEd Schouten 	size_t vmin = tp->t_termios.c_cc[VMIN];
268bc093719SEd Schouten 	int oresid = uio->uio_resid;
269bc093719SEd Schouten 	int error;
270bc093719SEd Schouten 
271bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VMIN] != 0);
272bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] != 0);
273bc093719SEd Schouten 
274bc093719SEd Schouten 	/*
275bc093719SEd Schouten 	 * When using the interbyte timer, the timer should be started
276bc093719SEd Schouten 	 * after the first byte has been received. We just call into the
277bc093719SEd Schouten 	 * generic read timer code after we've received the first byte.
278bc093719SEd Schouten 	 */
279bc093719SEd Schouten 
280bc093719SEd Schouten 	for (;;) {
281bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
282bc093719SEd Schouten 		    uio->uio_resid, 0);
283bc093719SEd Schouten 		if (error)
284bc093719SEd Schouten 			return (error);
285bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
286bc093719SEd Schouten 			return (0);
287bc093719SEd Schouten 
288bc093719SEd Schouten 		/*
289bc093719SEd Schouten 		 * Not enough data, but we did receive some, which means
290bc093719SEd Schouten 		 * we'll now start using the interbyte timer.
291bc093719SEd Schouten 		 */
292bc093719SEd Schouten 		if (oresid != uio->uio_resid)
293bc093719SEd Schouten 			break;
294bc093719SEd Schouten 
295bc093719SEd Schouten 		/* We have to wait for more. */
296bc093719SEd Schouten 		if (ioflag & IO_NDELAY)
297bc093719SEd Schouten 			return (EWOULDBLOCK);
298bc093719SEd Schouten 		else if (tp->t_flags & TF_ZOMBIE)
299bc093719SEd Schouten 			return (0);
300bc093719SEd Schouten 
301bc093719SEd Schouten 		error = tty_wait(tp, &tp->t_inwait);
302bc093719SEd Schouten 		if (error)
303bc093719SEd Schouten 			return (error);
304bc093719SEd Schouten 	}
305bc093719SEd Schouten 
306bc093719SEd Schouten 	return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
307bc093719SEd Schouten }
308bc093719SEd Schouten 
309bc093719SEd Schouten int
310bc093719SEd Schouten ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
311bc093719SEd Schouten {
312bc093719SEd Schouten 	int error;
313bc093719SEd Schouten 
314bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
315bc093719SEd Schouten 
316bc093719SEd Schouten 	if (uio->uio_resid == 0)
317bc093719SEd Schouten 		return (0);
318bc093719SEd Schouten 
319bc093719SEd Schouten 	if (CMP_FLAG(l, ICANON))
320bc093719SEd Schouten 		error = ttydisc_read_canonical(tp, uio, ioflag);
321bc093719SEd Schouten 	else if (tp->t_termios.c_cc[VTIME] == 0)
322bc093719SEd Schouten 		error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
323bc093719SEd Schouten 	else if (tp->t_termios.c_cc[VMIN] == 0)
324bc093719SEd Schouten 		error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
325bc093719SEd Schouten 		    uio->uio_resid);
326bc093719SEd Schouten 	else
327bc093719SEd Schouten 		error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
328bc093719SEd Schouten 
329a15ec0a5SEd Schouten 	if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
330a15ec0a5SEd Schouten 	    ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
331bc093719SEd Schouten 		/* Unset the input watermark when we've got enough space. */
332bc093719SEd Schouten 		tty_hiwat_in_unblock(tp);
333bc093719SEd Schouten 	}
334bc093719SEd Schouten 
335bc093719SEd Schouten 	return (error);
336bc093719SEd Schouten }
337bc093719SEd Schouten 
338bc093719SEd Schouten static __inline unsigned int
339bc093719SEd Schouten ttydisc_findchar(const char *obstart, unsigned int oblen)
340bc093719SEd Schouten {
341bc093719SEd Schouten 	const char *c = obstart;
342bc093719SEd Schouten 
343bc093719SEd Schouten 	while (oblen--) {
344bc093719SEd Schouten 		if (CTL_VALID(*c))
345bc093719SEd Schouten 			break;
346bc093719SEd Schouten 		c++;
347bc093719SEd Schouten 	}
348bc093719SEd Schouten 
349bc093719SEd Schouten 	return (c - obstart);
350bc093719SEd Schouten }
351bc093719SEd Schouten 
352bc093719SEd Schouten static int
353bc093719SEd Schouten ttydisc_write_oproc(struct tty *tp, char c)
354bc093719SEd Schouten {
355bc093719SEd Schouten 	unsigned int scnt, error;
356bc093719SEd Schouten 
357bc093719SEd Schouten 	MPASS(CMP_FLAG(o, OPOST));
358bc093719SEd Schouten 	MPASS(CTL_VALID(c));
359bc093719SEd Schouten 
360bc093719SEd Schouten #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
361bc093719SEd Schouten 	switch (c) {
362bc093719SEd Schouten 	case CEOF:
363bc093719SEd Schouten 		/* End-of-text dropping. */
364bc093719SEd Schouten 		if (CMP_FLAG(o, ONOEOT))
365bc093719SEd Schouten 			return (0);
366bc093719SEd Schouten 		return PRINT_NORMAL();
367bc093719SEd Schouten 
368bc093719SEd Schouten 	case CERASE2:
369bc093719SEd Schouten 		/* Handle backspace to fix tab expansion. */
370bc093719SEd Schouten 		if (PRINT_NORMAL() != 0)
371bc093719SEd Schouten 			return (-1);
372bc093719SEd Schouten 		if (tp->t_column > 0)
373bc093719SEd Schouten 			tp->t_column--;
374bc093719SEd Schouten 		return (0);
375bc093719SEd Schouten 
376bc093719SEd Schouten 	case CTAB:
377bc093719SEd Schouten 		/* Tab expansion. */
378bc093719SEd Schouten 		scnt = 8 - (tp->t_column & 7);
379bc093719SEd Schouten 		if (CMP_FLAG(o, TAB3)) {
380bc093719SEd Schouten 			error = ttyoutq_write_nofrag(&tp->t_outq,
381bc093719SEd Schouten 			    "        ", scnt);
382bc093719SEd Schouten 		} else {
383bc093719SEd Schouten 			error = PRINT_NORMAL();
384bc093719SEd Schouten 		}
385bc093719SEd Schouten 		if (error)
386bc093719SEd Schouten 			return (-1);
387bc093719SEd Schouten 
388bc093719SEd Schouten 		tp->t_column += scnt;
389bc093719SEd Schouten 		MPASS((tp->t_column % 8) == 0);
390bc093719SEd Schouten 		return (0);
391bc093719SEd Schouten 
392bc093719SEd Schouten 	case CNL:
393bc093719SEd Schouten 		/* Newline conversion. */
394bc093719SEd Schouten 		if (CMP_FLAG(o, ONLCR)) {
395bc093719SEd Schouten 			/* Convert \n to \r\n. */
396bc093719SEd Schouten 			error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
397bc093719SEd Schouten 		} else {
398bc093719SEd Schouten 			error = PRINT_NORMAL();
399bc093719SEd Schouten 		}
400bc093719SEd Schouten 		if (error)
401bc093719SEd Schouten 			return (-1);
402bc093719SEd Schouten 
403bc093719SEd Schouten 		if (CMP_FLAG(o, ONLCR|ONLRET)) {
404bc093719SEd Schouten 			tp->t_column = tp->t_writepos = 0;
405bc093719SEd Schouten 			ttyinq_reprintpos_set(&tp->t_inq);
406bc093719SEd Schouten 		}
407bc093719SEd Schouten 		return (0);
408bc093719SEd Schouten 
409bc093719SEd Schouten 	case CCR:
410bc093719SEd Schouten 		/* Carriage return to newline conversion. */
411bc093719SEd Schouten 		if (CMP_FLAG(o, OCRNL))
412bc093719SEd Schouten 			c = CNL;
413bc093719SEd Schouten 		/* Omit carriage returns on column 0. */
414bc093719SEd Schouten 		if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
415bc093719SEd Schouten 			return (0);
416bc093719SEd Schouten 		if (PRINT_NORMAL() != 0)
417bc093719SEd Schouten 			return (-1);
418bc093719SEd Schouten 
419bc093719SEd Schouten 		tp->t_column = tp->t_writepos = 0;
420bc093719SEd Schouten 		ttyinq_reprintpos_set(&tp->t_inq);
421bc093719SEd Schouten 		return (0);
422bc093719SEd Schouten 	}
423bc093719SEd Schouten 
424bc093719SEd Schouten 	/*
425bc093719SEd Schouten 	 * Invisible control character. Print it, but don't
426bc093719SEd Schouten 	 * increase the column count.
427bc093719SEd Schouten 	 */
428bc093719SEd Schouten 	return PRINT_NORMAL();
429bc093719SEd Schouten #undef PRINT_NORMAL
430bc093719SEd Schouten }
431bc093719SEd Schouten 
432bc093719SEd Schouten /*
433bc093719SEd Schouten  * Just like the old TTY implementation, we need to copy data in chunks
434bc093719SEd Schouten  * into a temporary buffer. One of the reasons why we need to do this,
435bc093719SEd Schouten  * is because output processing (only TAB3 though) may allow the buffer
436bc093719SEd Schouten  * to grow eight times.
437bc093719SEd Schouten  */
438bc093719SEd Schouten int
439bc093719SEd Schouten ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
440bc093719SEd Schouten {
441a1215e37SEd Schouten 	char ob[TTY_STACKBUF];
442bc093719SEd Schouten 	char *obstart;
443bc093719SEd Schouten 	int error = 0;
444bc093719SEd Schouten 	unsigned int oblen = 0;
445bc093719SEd Schouten 
446bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
447bc093719SEd Schouten 
448bc093719SEd Schouten 	if (tp->t_flags & TF_ZOMBIE)
449bc093719SEd Schouten 		return (EIO);
450bc093719SEd Schouten 
451bc093719SEd Schouten 	/*
452bc093719SEd Schouten 	 * We don't need to check whether the process is the foreground
453bc093719SEd Schouten 	 * process group or if we have a carrier. This is already done
454bc093719SEd Schouten 	 * in ttydev_write().
455bc093719SEd Schouten 	 */
456bc093719SEd Schouten 
457bc093719SEd Schouten 	while (uio->uio_resid > 0) {
458bc093719SEd Schouten 		unsigned int nlen;
459bc093719SEd Schouten 
460bc093719SEd Schouten 		MPASS(oblen == 0);
461bc093719SEd Schouten 
462bc093719SEd Schouten 		/* Step 1: read data. */
463bc093719SEd Schouten 
464bc093719SEd Schouten 		tty_unlock(tp);
465bc093719SEd Schouten 
466bc093719SEd Schouten 		obstart = ob;
467bc093719SEd Schouten 		nlen = MIN(uio->uio_resid, sizeof ob);
468bc093719SEd Schouten 		error = uiomove(ob, nlen, uio);
469bc093719SEd Schouten 		if (error != 0)
470bc093719SEd Schouten 			break;
471bc093719SEd Schouten 		oblen = nlen;
472bc093719SEd Schouten 
473bc093719SEd Schouten 		tty_lock(tp);
474bc093719SEd Schouten 		if (tty_gone(tp)) {
475bc093719SEd Schouten 			error = ENXIO;
476bc093719SEd Schouten 			break;
477bc093719SEd Schouten 		}
478bc093719SEd Schouten 
479bc093719SEd Schouten 		MPASS(oblen > 0);
480bc093719SEd Schouten 
481bc093719SEd Schouten 		/* Step 2: process data. */
482bc093719SEd Schouten 		do {
483bc093719SEd Schouten 			unsigned int plen, wlen;
484bc093719SEd Schouten 
485bc093719SEd Schouten 			/* Search for special characters for post processing. */
486bc093719SEd Schouten 			if (CMP_FLAG(o, OPOST)) {
487bc093719SEd Schouten 				plen = ttydisc_findchar(obstart, oblen);
488bc093719SEd Schouten 			} else {
489bc093719SEd Schouten 				plen = oblen;
490bc093719SEd Schouten 			}
491bc093719SEd Schouten 
492bc093719SEd Schouten 			if (plen == 0) {
493bc093719SEd Schouten 				/*
494bc093719SEd Schouten 				 * We're going to process a character
495bc093719SEd Schouten 				 * that needs processing
496bc093719SEd Schouten 				 */
497bc093719SEd Schouten 				if (ttydisc_write_oproc(tp, *obstart) == 0) {
498bc093719SEd Schouten 					obstart++;
499bc093719SEd Schouten 					oblen--;
500bc093719SEd Schouten 
501bc093719SEd Schouten 					tp->t_writepos = tp->t_column;
502bc093719SEd Schouten 					ttyinq_reprintpos_set(&tp->t_inq);
503bc093719SEd Schouten 					continue;
504bc093719SEd Schouten 				}
505bc093719SEd Schouten 			} else {
506bc093719SEd Schouten 				/* We're going to write regular data. */
507bc093719SEd Schouten 				wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
508bc093719SEd Schouten 				obstart += wlen;
509bc093719SEd Schouten 				oblen -= wlen;
510bc093719SEd Schouten 				tp->t_column += wlen;
511bc093719SEd Schouten 
512bc093719SEd Schouten 				tp->t_writepos = tp->t_column;
513bc093719SEd Schouten 				ttyinq_reprintpos_set(&tp->t_inq);
514bc093719SEd Schouten 
515bc093719SEd Schouten 				if (wlen == plen)
516bc093719SEd Schouten 					continue;
517bc093719SEd Schouten 			}
518bc093719SEd Schouten 
519bc093719SEd Schouten 			/* Watermark reached. Try to sleep. */
520bc093719SEd Schouten 			tp->t_flags |= TF_HIWAT_OUT;
521bc093719SEd Schouten 
522bc093719SEd Schouten 			if (ioflag & IO_NDELAY) {
523bc093719SEd Schouten 				error = EWOULDBLOCK;
524bc093719SEd Schouten 				goto done;
525bc093719SEd Schouten 			}
526bc093719SEd Schouten 
527bc093719SEd Schouten 			/*
528bc093719SEd Schouten 			 * The driver may write back the data
529bc093719SEd Schouten 			 * synchronously. Be sure to check the high
530bc093719SEd Schouten 			 * water mark before going to sleep.
531bc093719SEd Schouten 			 */
532bc093719SEd Schouten 			ttydevsw_outwakeup(tp);
533bc093719SEd Schouten 			if ((tp->t_flags & TF_HIWAT_OUT) == 0)
534bc093719SEd Schouten 				continue;
535bc093719SEd Schouten 
536bc093719SEd Schouten 			error = tty_wait(tp, &tp->t_outwait);
537bc093719SEd Schouten 			if (error)
538bc093719SEd Schouten 				goto done;
539bc093719SEd Schouten 
540bc093719SEd Schouten 			if (tp->t_flags & TF_ZOMBIE) {
541bc093719SEd Schouten 				error = EIO;
542bc093719SEd Schouten 				goto done;
543bc093719SEd Schouten 			}
544bc093719SEd Schouten 		} while (oblen > 0);
545bc093719SEd Schouten 	}
546bc093719SEd Schouten 
547bc093719SEd Schouten done:
548856ebf85SChristian S.J. Peron 	if (!tty_gone(tp))
549bc093719SEd Schouten 		ttydevsw_outwakeup(tp);
550bc093719SEd Schouten 
551bc093719SEd Schouten 	/*
552bc093719SEd Schouten 	 * Add the amount of bytes that we didn't process back to the
553bc093719SEd Schouten 	 * uio counters. We need to do this to make sure write() doesn't
554bc093719SEd Schouten 	 * count the bytes we didn't store in the queue.
555bc093719SEd Schouten 	 */
556bc093719SEd Schouten 	uio->uio_resid += oblen;
557bc093719SEd Schouten 	return (error);
558bc093719SEd Schouten }
559bc093719SEd Schouten 
560bc093719SEd Schouten void
561bc093719SEd Schouten ttydisc_optimize(struct tty *tp)
562bc093719SEd Schouten {
563bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
564bc093719SEd Schouten 
565a1215e37SEd Schouten 	if ((!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
566bc093719SEd Schouten 	    (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
567bc093719SEd Schouten 	    (!CMP_FLAG(i, PARMRK) ||
568bc093719SEd Schouten 	        CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
569a1215e37SEd Schouten 	    !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) ||
570a1215e37SEd Schouten 	    ttyhook_hashook(tp, rint_bypass)) {
571bc093719SEd Schouten 		tp->t_flags |= TF_BYPASS;
572bc093719SEd Schouten 	} else {
573bc093719SEd Schouten 		tp->t_flags &= ~TF_BYPASS;
574bc093719SEd Schouten 	}
575bc093719SEd Schouten }
576bc093719SEd Schouten 
577bc093719SEd Schouten void
578bc093719SEd Schouten ttydisc_modem(struct tty *tp, int open)
579bc093719SEd Schouten {
580bc093719SEd Schouten 
581bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
582bc093719SEd Schouten 
583bc093719SEd Schouten 	if (open)
584bc093719SEd Schouten 		cv_broadcast(&tp->t_dcdwait);
585bc093719SEd Schouten 
586bc093719SEd Schouten 	/*
587bc093719SEd Schouten 	 * Ignore modem status lines when CLOCAL is turned on, but don't
588bc093719SEd Schouten 	 * enter the zombie state when the TTY isn't opened, because
589bc093719SEd Schouten 	 * that would cause the TTY to be in zombie state after being
590bc093719SEd Schouten 	 * opened.
591bc093719SEd Schouten 	 */
592bc093719SEd Schouten 	if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
593bc093719SEd Schouten 		return;
594bc093719SEd Schouten 
595bc093719SEd Schouten 	if (open == 0) {
596bc093719SEd Schouten 		/*
597bc093719SEd Schouten 		 * Lost carrier.
598bc093719SEd Schouten 		 */
599bc093719SEd Schouten 		tp->t_flags |= TF_ZOMBIE;
600bc093719SEd Schouten 
601bc093719SEd Schouten 		tty_signal_sessleader(tp, SIGHUP);
602bc093719SEd Schouten 		tty_flush(tp, FREAD|FWRITE);
603bc093719SEd Schouten 	} else {
604bc093719SEd Schouten 		/*
605bc093719SEd Schouten 		 * Carrier is back again.
606bc093719SEd Schouten 		 */
607bc093719SEd Schouten 
608bc093719SEd Schouten 		/* XXX: what should we do here? */
609bc093719SEd Schouten 	}
610bc093719SEd Schouten }
611bc093719SEd Schouten 
612bc093719SEd Schouten static int
613bc093719SEd Schouten ttydisc_echo_force(struct tty *tp, char c, int quote)
614bc093719SEd Schouten {
615bc093719SEd Schouten 
616bc093719SEd Schouten 	if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
617bc093719SEd Schouten 		/*
618bc093719SEd Schouten 		 * Only perform postprocessing when OPOST is turned on
619bc093719SEd Schouten 		 * and the character is an unquoted BS/TB/NL/CR.
620bc093719SEd Schouten 		 */
621bc093719SEd Schouten 		return ttydisc_write_oproc(tp, c);
622bc093719SEd Schouten 	} else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
623bc093719SEd Schouten 		/*
624bc093719SEd Schouten 		 * Only use ^X notation when ECHOCTL is turned on and
625bc093719SEd Schouten 		 * we've got an quoted control character.
626bc093719SEd Schouten 		 */
627bc093719SEd Schouten 		char ob[2] = { '^', '?' };
628bc093719SEd Schouten 
629bc093719SEd Schouten 		/* Print ^X notation. */
630bc093719SEd Schouten 		if (c != 0x7f)
631bc093719SEd Schouten 			ob[1] = c + 'A' - 1;
632bc093719SEd Schouten 
633bc093719SEd Schouten 		tp->t_column += 2;
634bc093719SEd Schouten 		return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
635bc093719SEd Schouten 	} else {
636bc093719SEd Schouten 		/* Can just be printed. */
637bc093719SEd Schouten 		tp->t_column++;
638bc093719SEd Schouten 		return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
639bc093719SEd Schouten 	}
640bc093719SEd Schouten }
641bc093719SEd Schouten 
642bc093719SEd Schouten static int
643bc093719SEd Schouten ttydisc_echo(struct tty *tp, char c, int quote)
644bc093719SEd Schouten {
645bc093719SEd Schouten 
646bc093719SEd Schouten 	/*
647bc093719SEd Schouten 	 * Only echo characters when ECHO is turned on, or ECHONL when
648bc093719SEd Schouten 	 * the character is an unquoted newline.
649bc093719SEd Schouten 	 */
650bc093719SEd Schouten 	if (!CMP_FLAG(l, ECHO) &&
651bc093719SEd Schouten 	    (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
652bc093719SEd Schouten 		return (0);
653bc093719SEd Schouten 
654bc093719SEd Schouten 	return ttydisc_echo_force(tp, c, quote);
655bc093719SEd Schouten }
656bc093719SEd Schouten 
657bc093719SEd Schouten 
658bc093719SEd Schouten static void
659bc093719SEd Schouten ttydisc_reprint_char(void *d, char c, int quote)
660bc093719SEd Schouten {
661bc093719SEd Schouten 	struct tty *tp = d;
662bc093719SEd Schouten 
663bc093719SEd Schouten 	ttydisc_echo(tp, c, quote);
664bc093719SEd Schouten }
665bc093719SEd Schouten 
666bc093719SEd Schouten static void
667bc093719SEd Schouten ttydisc_reprint(struct tty *tp)
668bc093719SEd Schouten {
669bc093719SEd Schouten 	cc_t c;
670bc093719SEd Schouten 
671bc093719SEd Schouten 	/* Print  ^R\n, followed by the line. */
672bc093719SEd Schouten 	c = tp->t_termios.c_cc[VREPRINT];
673bc093719SEd Schouten 	if (c != _POSIX_VDISABLE)
674bc093719SEd Schouten 		ttydisc_echo(tp, c, 0);
675bc093719SEd Schouten 	ttydisc_echo(tp, CNL, 0);
676bc093719SEd Schouten 	ttyinq_reprintpos_reset(&tp->t_inq);
677bc093719SEd Schouten 
678bc093719SEd Schouten 	ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
679bc093719SEd Schouten }
680bc093719SEd Schouten 
681bc093719SEd Schouten struct ttydisc_recalc_length {
682bc093719SEd Schouten 	struct tty *tp;
683bc093719SEd Schouten 	unsigned int curlen;
684bc093719SEd Schouten };
685bc093719SEd Schouten 
686bc093719SEd Schouten static void
687bc093719SEd Schouten ttydisc_recalc_charlength(void *d, char c, int quote)
688bc093719SEd Schouten {
689bc093719SEd Schouten 	struct ttydisc_recalc_length *data = d;
690bc093719SEd Schouten 	struct tty *tp = data->tp;
691bc093719SEd Schouten 
692bc093719SEd Schouten 	if (CTL_PRINT(c, quote)) {
693bc093719SEd Schouten 		if (CMP_FLAG(l, ECHOCTL))
694bc093719SEd Schouten 			data->curlen += 2;
695bc093719SEd Schouten 	} else if (c == CTAB) {
696bc093719SEd Schouten 		data->curlen += 8 - (data->curlen & 7);
697bc093719SEd Schouten 	} else {
698bc093719SEd Schouten 		data->curlen++;
699bc093719SEd Schouten 	}
700bc093719SEd Schouten }
701bc093719SEd Schouten 
702bc093719SEd Schouten static unsigned int
703bc093719SEd Schouten ttydisc_recalc_linelength(struct tty *tp)
704bc093719SEd Schouten {
705bc093719SEd Schouten 	struct ttydisc_recalc_length data = { tp, tp->t_writepos };
706bc093719SEd Schouten 
707bc093719SEd Schouten 	ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
708bc093719SEd Schouten 	    ttydisc_recalc_charlength, &data);
709bc093719SEd Schouten 	return (data.curlen);
710bc093719SEd Schouten }
711bc093719SEd Schouten 
712bc093719SEd Schouten static int
713bc093719SEd Schouten ttydisc_rubchar(struct tty *tp)
714bc093719SEd Schouten {
715bc093719SEd Schouten 	char c;
716bc093719SEd Schouten 	int quote;
717bc093719SEd Schouten 	unsigned int prevpos, tablen;
718bc093719SEd Schouten 
719bc093719SEd Schouten 	if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
720bc093719SEd Schouten 		return (-1);
721bc093719SEd Schouten 	ttyinq_unputchar(&tp->t_inq);
722bc093719SEd Schouten 
723bc093719SEd Schouten 	if (CMP_FLAG(l, ECHO)) {
724bc093719SEd Schouten 		/*
725bc093719SEd Schouten 		 * Remove the character from the screen. This is even
726bc093719SEd Schouten 		 * safe for characters that span multiple characters
727bc093719SEd Schouten 		 * (tabs, quoted, etc).
728bc093719SEd Schouten 		 */
729bc093719SEd Schouten 		if (tp->t_writepos >= tp->t_column) {
730bc093719SEd Schouten 			/* Retype the sentence. */
731bc093719SEd Schouten 			ttydisc_reprint(tp);
732bc093719SEd Schouten 		} else if (CMP_FLAG(l, ECHOE)) {
733bc093719SEd Schouten 			if (CTL_PRINT(c, quote)) {
734bc093719SEd Schouten 				/* Remove ^X formatted chars. */
735bc093719SEd Schouten 				if (CMP_FLAG(l, ECHOCTL)) {
736bc093719SEd Schouten 					tp->t_column -= 2;
737bc093719SEd Schouten 					ttyoutq_write_nofrag(&tp->t_outq,
738bc093719SEd Schouten 					    "\b\b  \b\b", 6);
739bc093719SEd Schouten 				}
740bc093719SEd Schouten 			} else if (c == ' ') {
741bc093719SEd Schouten 				/* Space character needs no rubbing. */
742bc093719SEd Schouten 				tp->t_column -= 1;
743bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
744bc093719SEd Schouten 			} else if (c == CTAB) {
745bc093719SEd Schouten 				/*
746bc093719SEd Schouten 				 * Making backspace work with tabs is
747bc093719SEd Schouten 				 * quite hard. Recalculate the length of
748bc093719SEd Schouten 				 * this character and remove it.
749bc093719SEd Schouten 				 *
750bc093719SEd Schouten 				 * Because terminal settings could be
751bc093719SEd Schouten 				 * changed while the line is being
752bc093719SEd Schouten 				 * inserted, the calculations don't have
753bc093719SEd Schouten 				 * to be correct. Make sure we keep the
754bc093719SEd Schouten 				 * tab length within proper bounds.
755bc093719SEd Schouten 				 */
756bc093719SEd Schouten 				prevpos = ttydisc_recalc_linelength(tp);
757bc093719SEd Schouten 				if (prevpos >= tp->t_column)
758bc093719SEd Schouten 					tablen = 1;
759bc093719SEd Schouten 				else
760bc093719SEd Schouten 					tablen = tp->t_column - prevpos;
761bc093719SEd Schouten 				if (tablen > 8)
762bc093719SEd Schouten 					tablen = 8;
763bc093719SEd Schouten 
764bc093719SEd Schouten 				tp->t_column = prevpos;
765bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq,
766bc093719SEd Schouten 				    "\b\b\b\b\b\b\b\b", tablen);
767bc093719SEd Schouten 				return (0);
768bc093719SEd Schouten 			} else {
769bc093719SEd Schouten 				/*
770bc093719SEd Schouten 				 * Remove a regular character by
771bc093719SEd Schouten 				 * punching a space over it.
772bc093719SEd Schouten 				 */
773bc093719SEd Schouten 				tp->t_column -= 1;
774bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
775bc093719SEd Schouten 			}
776bc093719SEd Schouten 		} else {
777bc093719SEd Schouten 			/* Don't print spaces. */
778bc093719SEd Schouten 			ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
779bc093719SEd Schouten 		}
780bc093719SEd Schouten 	}
781bc093719SEd Schouten 
782bc093719SEd Schouten 	return (0);
783bc093719SEd Schouten }
784bc093719SEd Schouten 
785bc093719SEd Schouten static void
786bc093719SEd Schouten ttydisc_rubword(struct tty *tp)
787bc093719SEd Schouten {
788bc093719SEd Schouten 	char c;
789bc093719SEd Schouten 	int quote, alnum;
790bc093719SEd Schouten 
791bc093719SEd Schouten 	/* Strip whitespace first. */
792bc093719SEd Schouten 	for (;;) {
793bc093719SEd Schouten 		if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
794bc093719SEd Schouten 			return;
795bc093719SEd Schouten 		if (!CTL_WHITE(c))
796bc093719SEd Schouten 			break;
797bc093719SEd Schouten 		ttydisc_rubchar(tp);
798bc093719SEd Schouten 	}
799bc093719SEd Schouten 
800bc093719SEd Schouten 	/*
801bc093719SEd Schouten 	 * Record whether the last character from the previous iteration
802bc093719SEd Schouten 	 * was alphanumeric or not. We need this to implement ALTWERASE.
803bc093719SEd Schouten 	 */
804bc093719SEd Schouten 	alnum = CTL_ALNUM(c);
805bc093719SEd Schouten 	for (;;) {
806bc093719SEd Schouten 		ttydisc_rubchar(tp);
807bc093719SEd Schouten 
808bc093719SEd Schouten 		if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
809bc093719SEd Schouten 			return;
810bc093719SEd Schouten 		if (CTL_WHITE(c))
811bc093719SEd Schouten 			return;
812bc093719SEd Schouten 		if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
813bc093719SEd Schouten 			return;
814bc093719SEd Schouten 	}
815bc093719SEd Schouten }
816bc093719SEd Schouten 
817bc093719SEd Schouten int
818bc093719SEd Schouten ttydisc_rint(struct tty *tp, char c, int flags)
819bc093719SEd Schouten {
820bc093719SEd Schouten 	int signal, quote = 0;
821bc093719SEd Schouten 	char ob[3] = { 0xff, 0x00 };
822bc093719SEd Schouten 	size_t ol;
823bc093719SEd Schouten 
824bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
825bc093719SEd Schouten 
826bc093719SEd Schouten 	atomic_add_long(&tty_nin, 1);
827bc093719SEd Schouten 
828a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint))
829a1215e37SEd Schouten 		return ttyhook_rint(tp, c, flags);
830a1215e37SEd Schouten 
831bc093719SEd Schouten 	if (tp->t_flags & TF_BYPASS)
832bc093719SEd Schouten 		goto processed;
833bc093719SEd Schouten 
834bc093719SEd Schouten 	if (flags) {
835bc093719SEd Schouten 		if (flags & TRE_BREAK) {
836bc093719SEd Schouten 			if (CMP_FLAG(i, IGNBRK)) {
837bc093719SEd Schouten 				/* Ignore break characters. */
838bc093719SEd Schouten 				return (0);
839bc093719SEd Schouten 			} else if (CMP_FLAG(i, BRKINT)) {
840bc093719SEd Schouten 				/* Generate SIGINT on break. */
841bc093719SEd Schouten 				tty_flush(tp, FREAD|FWRITE);
842bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGINT);
843bc093719SEd Schouten 				return (0);
844bc093719SEd Schouten 			} else {
845bc093719SEd Schouten 				/* Just print it. */
846bc093719SEd Schouten 				goto parmrk;
847bc093719SEd Schouten 			}
848bc093719SEd Schouten 		} else if (flags & TRE_FRAMING ||
849bc093719SEd Schouten 		    (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
850bc093719SEd Schouten 			if (CMP_FLAG(i, IGNPAR)) {
851bc093719SEd Schouten 				/* Ignore bad characters. */
852bc093719SEd Schouten 				return (0);
853bc093719SEd Schouten 			} else {
854bc093719SEd Schouten 				/* Just print it. */
855bc093719SEd Schouten 				goto parmrk;
856bc093719SEd Schouten 			}
857bc093719SEd Schouten 		}
858bc093719SEd Schouten 	}
859bc093719SEd Schouten 
860bc093719SEd Schouten 	/* Allow any character to perform a wakeup. */
861bc093719SEd Schouten 	if (CMP_FLAG(i, IXANY))
862bc093719SEd Schouten 		tp->t_flags &= ~TF_STOPPED;
863bc093719SEd Schouten 
864bc093719SEd Schouten 	/* Remove the top bit. */
865bc093719SEd Schouten 	if (CMP_FLAG(i, ISTRIP))
866bc093719SEd Schouten 		c &= ~0x80;
867bc093719SEd Schouten 
868bc093719SEd Schouten 	/* Skip input processing when we want to print it literally. */
869bc093719SEd Schouten 	if (tp->t_flags & TF_LITERAL) {
870bc093719SEd Schouten 		tp->t_flags &= ~TF_LITERAL;
871bc093719SEd Schouten 		quote = 1;
872bc093719SEd Schouten 		goto processed;
873bc093719SEd Schouten 	}
874bc093719SEd Schouten 
875bc093719SEd Schouten 	/* Special control characters that are implementation dependent. */
876bc093719SEd Schouten 	if (CMP_FLAG(l, IEXTEN)) {
877bc093719SEd Schouten 		/* Accept the next character as literal. */
878bc093719SEd Schouten 		if (CMP_CC(VLNEXT, c)) {
879bc093719SEd Schouten 			if (CMP_FLAG(l, ECHO)) {
880bc093719SEd Schouten 				if (CMP_FLAG(l, ECHOE))
881bc093719SEd Schouten 					ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
882bc093719SEd Schouten 				else
883bc093719SEd Schouten 					ttydisc_echo(tp, c, 0);
884bc093719SEd Schouten 			}
885bc093719SEd Schouten 			tp->t_flags |= TF_LITERAL;
886bc093719SEd Schouten 			return (0);
887bc093719SEd Schouten 		}
888bc093719SEd Schouten 	}
889bc093719SEd Schouten 
890bc093719SEd Schouten 	/*
891bc093719SEd Schouten 	 * Handle signal processing.
892bc093719SEd Schouten 	 */
893bc093719SEd Schouten 	if (CMP_FLAG(l, ISIG)) {
894bc093719SEd Schouten 		if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
895bc093719SEd Schouten 			if (CMP_CC(VSTATUS, c)) {
896bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGINFO);
897bc093719SEd Schouten 				return (0);
898bc093719SEd Schouten 			}
899bc093719SEd Schouten 		}
900bc093719SEd Schouten 
901bc093719SEd Schouten 		/*
902bc093719SEd Schouten 		 * When compared to the old implementation, this
903bc093719SEd Schouten 		 * implementation also flushes the output queue. POSIX
904bc093719SEd Schouten 		 * is really brief about this, but does makes us assume
905bc093719SEd Schouten 		 * we have to do so.
906bc093719SEd Schouten 		 */
907bc093719SEd Schouten 		signal = 0;
908bc093719SEd Schouten 		if (CMP_CC(VINTR, c)) {
909bc093719SEd Schouten 			signal = SIGINT;
910bc093719SEd Schouten 		} else if (CMP_CC(VQUIT, c)) {
911bc093719SEd Schouten 			signal = SIGQUIT;
912bc093719SEd Schouten 		} else if (CMP_CC(VSUSP, c)) {
913bc093719SEd Schouten 			signal = SIGTSTP;
914bc093719SEd Schouten 		}
915bc093719SEd Schouten 
916bc093719SEd Schouten 		if (signal != 0) {
917bc093719SEd Schouten 			/*
918bc093719SEd Schouten 			 * Echo the character before signalling the
919bc093719SEd Schouten 			 * processes.
920bc093719SEd Schouten 			 */
921bc093719SEd Schouten 			if (!CMP_FLAG(l, NOFLSH))
922bc093719SEd Schouten 				tty_flush(tp, FREAD|FWRITE);
923bc093719SEd Schouten 			ttydisc_echo(tp, c, 0);
924bc093719SEd Schouten 			tty_signal_pgrp(tp, signal);
925bc093719SEd Schouten 			return (0);
926bc093719SEd Schouten 		}
927bc093719SEd Schouten 	}
928bc093719SEd Schouten 
929bc093719SEd Schouten 	/*
930bc093719SEd Schouten 	 * Handle start/stop characters.
931bc093719SEd Schouten 	 */
932bc093719SEd Schouten 	if (CMP_FLAG(i, IXON)) {
933bc093719SEd Schouten 		if (CMP_CC(VSTOP, c)) {
934bc093719SEd Schouten 			/* Stop it if we aren't stopped yet. */
935bc093719SEd Schouten 			if ((tp->t_flags & TF_STOPPED) == 0) {
936bc093719SEd Schouten 				tp->t_flags |= TF_STOPPED;
937bc093719SEd Schouten 				return (0);
938bc093719SEd Schouten 			}
939bc093719SEd Schouten 			/*
940bc093719SEd Schouten 			 * Fallthrough:
941bc093719SEd Schouten 			 * When VSTART == VSTOP, we should make this key
942bc093719SEd Schouten 			 * toggle it.
943bc093719SEd Schouten 			 */
944bc093719SEd Schouten 			if (!CMP_CC(VSTART, c))
945bc093719SEd Schouten 				return (0);
946bc093719SEd Schouten 		}
947bc093719SEd Schouten 		if (CMP_CC(VSTART, c)) {
948bc093719SEd Schouten 			tp->t_flags &= ~TF_STOPPED;
949bc093719SEd Schouten 			return (0);
950bc093719SEd Schouten 		}
951bc093719SEd Schouten 	}
952bc093719SEd Schouten 
953bc093719SEd Schouten 	/* Conversion of CR and NL. */
954bc093719SEd Schouten 	switch (c) {
955bc093719SEd Schouten 	case CCR:
956bc093719SEd Schouten 		if (CMP_FLAG(i, IGNCR))
957bc093719SEd Schouten 			return (0);
958bc093719SEd Schouten 		if (CMP_FLAG(i, ICRNL))
959bc093719SEd Schouten 			c = CNL;
960bc093719SEd Schouten 		break;
961bc093719SEd Schouten 	case CNL:
962bc093719SEd Schouten 		if (CMP_FLAG(i, INLCR))
963bc093719SEd Schouten 			c = CCR;
964bc093719SEd Schouten 		break;
965bc093719SEd Schouten 	}
966bc093719SEd Schouten 
967bc093719SEd Schouten 	/* Canonical line editing. */
968bc093719SEd Schouten 	if (CMP_FLAG(l, ICANON)) {
969bc093719SEd Schouten 		if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
970bc093719SEd Schouten 			ttydisc_rubchar(tp);
971bc093719SEd Schouten 			return (0);
972bc093719SEd Schouten 		} else if (CMP_CC(VKILL, c)) {
973bc093719SEd Schouten 			while (ttydisc_rubchar(tp) == 0);
974bc093719SEd Schouten 			return (0);
975bc093719SEd Schouten 		} else if (CMP_FLAG(l, IEXTEN)) {
976bc093719SEd Schouten 			if (CMP_CC(VWERASE, c)) {
977bc093719SEd Schouten 				ttydisc_rubword(tp);
978bc093719SEd Schouten 				return (0);
979bc093719SEd Schouten 			} else if (CMP_CC(VREPRINT, c)) {
980bc093719SEd Schouten 				ttydisc_reprint(tp);
981bc093719SEd Schouten 				return (0);
982bc093719SEd Schouten 			}
983bc093719SEd Schouten 		}
984bc093719SEd Schouten 	}
985bc093719SEd Schouten 
986bc093719SEd Schouten processed:
987bc093719SEd Schouten 	if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
988bc093719SEd Schouten 		/* Print 0xff 0xff. */
989bc093719SEd Schouten 		ob[1] = 0xff;
990bc093719SEd Schouten 		ol = 2;
991bc093719SEd Schouten 		quote = 1;
992bc093719SEd Schouten 	} else {
993bc093719SEd Schouten 		ob[0] = c;
994bc093719SEd Schouten 		ol = 1;
995bc093719SEd Schouten 	}
996bc093719SEd Schouten 
997bc093719SEd Schouten 	goto print;
998bc093719SEd Schouten 
999bc093719SEd Schouten parmrk:
1000bc093719SEd Schouten 	if (CMP_FLAG(i, PARMRK)) {
1001bc093719SEd Schouten 		/* Prepend 0xff 0x00 0x.. */
1002bc093719SEd Schouten 		ob[2] = c;
1003bc093719SEd Schouten 		ol = 3;
1004bc093719SEd Schouten 		quote = 1;
1005bc093719SEd Schouten 	} else {
1006bc093719SEd Schouten 		ob[0] = c;
1007bc093719SEd Schouten 		ol = 1;
1008bc093719SEd Schouten 	}
1009bc093719SEd Schouten 
1010bc093719SEd Schouten print:
1011bc093719SEd Schouten 	/* See if we can store this on the input queue. */
1012bc093719SEd Schouten 	if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1013a15ec0a5SEd Schouten 		if (CMP_FLAG(i, IMAXBEL))
1014a15ec0a5SEd Schouten 			ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1015a15ec0a5SEd Schouten 
1016a15ec0a5SEd Schouten 		/*
1017a15ec0a5SEd Schouten 		 * Prevent a deadlock here. It may be possible that a
1018a15ec0a5SEd Schouten 		 * user has entered so much data, there is no data
1019a15ec0a5SEd Schouten 		 * available to read(), but the buffers are full anyway.
1020a15ec0a5SEd Schouten 		 *
1021a15ec0a5SEd Schouten 		 * Only enter the high watermark if the device driver
1022a15ec0a5SEd Schouten 		 * can actually transmit something.
1023a15ec0a5SEd Schouten 		 */
1024a15ec0a5SEd Schouten 		if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1025a15ec0a5SEd Schouten 			return (0);
1026a15ec0a5SEd Schouten 
1027bc093719SEd Schouten 		tty_hiwat_in_block(tp);
1028bc093719SEd Schouten 		return (-1);
1029bc093719SEd Schouten 	}
1030bc093719SEd Schouten 
1031bc093719SEd Schouten 	/*
1032bc093719SEd Schouten 	 * In raw mode, we canonicalize after receiving a single
1033bc093719SEd Schouten 	 * character. Otherwise, we canonicalize when we receive a
1034bc093719SEd Schouten 	 * newline, VEOL or VEOF, but only when it isn't quoted.
1035bc093719SEd Schouten 	 */
1036bc093719SEd Schouten 	if (!CMP_FLAG(l, ICANON) ||
1037bc093719SEd Schouten 	    (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1038bc093719SEd Schouten 		ttyinq_canonicalize(&tp->t_inq);
1039bc093719SEd Schouten 	}
1040bc093719SEd Schouten 
1041bc093719SEd Schouten 	ttydisc_echo(tp, c, quote);
1042bc093719SEd Schouten 
1043bc093719SEd Schouten 	return (0);
1044bc093719SEd Schouten }
1045bc093719SEd Schouten 
1046bc093719SEd Schouten size_t
1047d344ffe5SEd Schouten ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1048bc093719SEd Schouten {
1049bc093719SEd Schouten 	size_t ret;
1050bc093719SEd Schouten 
1051bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1052bc093719SEd Schouten 
1053bc093719SEd Schouten 	MPASS(tp->t_flags & TF_BYPASS);
1054bc093719SEd Schouten 
1055bc093719SEd Schouten 	atomic_add_long(&tty_nin, len);
1056bc093719SEd Schouten 
1057a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_bypass))
1058a1215e37SEd Schouten 		return ttyhook_rint_bypass(tp, buf, len);
1059a1215e37SEd Schouten 
1060bc093719SEd Schouten 	ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1061bc093719SEd Schouten 	ttyinq_canonicalize(&tp->t_inq);
1062bc093719SEd Schouten 
1063bc093719SEd Schouten 	return (ret);
1064bc093719SEd Schouten }
1065bc093719SEd Schouten 
1066bc093719SEd Schouten void
1067bc093719SEd Schouten ttydisc_rint_done(struct tty *tp)
1068bc093719SEd Schouten {
1069bc093719SEd Schouten 
1070bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1071bc093719SEd Schouten 
1072a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_done))
1073a1215e37SEd Schouten 		ttyhook_rint_done(tp);
1074a1215e37SEd Schouten 
1075bc093719SEd Schouten 	/* Wake up readers. */
1076bc093719SEd Schouten 	tty_wakeup(tp, FREAD);
1077bc093719SEd Schouten 	/* Wake up driver for echo. */
1078bc093719SEd Schouten 	ttydevsw_outwakeup(tp);
1079bc093719SEd Schouten }
1080bc093719SEd Schouten 
1081a1215e37SEd Schouten size_t
1082a1215e37SEd Schouten ttydisc_rint_poll(struct tty *tp)
1083a1215e37SEd Schouten {
1084a1215e37SEd Schouten 	size_t l;
1085a1215e37SEd Schouten 
1086a1215e37SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1087a1215e37SEd Schouten 
1088a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_poll))
1089a1215e37SEd Schouten 		return ttyhook_rint_poll(tp);
1090a1215e37SEd Schouten 
1091a1215e37SEd Schouten 	/*
1092a1215e37SEd Schouten 	 * XXX: Still allow character input when there's no space in the
1093a1215e37SEd Schouten 	 * buffers, but we haven't entered the high watermark. This is
1094a1215e37SEd Schouten 	 * to allow backspace characters to be inserted when in
1095a1215e37SEd Schouten 	 * canonical mode.
1096a1215e37SEd Schouten 	 */
1097a1215e37SEd Schouten 	l = ttyinq_bytesleft(&tp->t_inq);
1098a1215e37SEd Schouten 	if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1099a1215e37SEd Schouten 		return (1);
1100a1215e37SEd Schouten 
1101a1215e37SEd Schouten 	return (l);
1102a1215e37SEd Schouten }
1103a1215e37SEd Schouten 
1104bc093719SEd Schouten static void
1105bc093719SEd Schouten ttydisc_wakeup_watermark(struct tty *tp)
1106bc093719SEd Schouten {
1107bc093719SEd Schouten 	size_t c;
1108bc093719SEd Schouten 
1109bc093719SEd Schouten 	c = ttyoutq_bytesleft(&tp->t_outq);
1110bc093719SEd Schouten 	if (tp->t_flags & TF_HIWAT_OUT) {
1111bc093719SEd Schouten 		/* Only allow us to run when we're below the watermark. */
1112bc093719SEd Schouten 		if (c < tp->t_outlow)
1113bc093719SEd Schouten 			return;
1114bc093719SEd Schouten 
1115bc093719SEd Schouten 		/* Reset the watermark. */
1116bc093719SEd Schouten 		tp->t_flags &= ~TF_HIWAT_OUT;
1117bc093719SEd Schouten 	} else {
1118bc093719SEd Schouten 		/* Only run when we have data at all. */
1119bc093719SEd Schouten 		if (c == 0)
1120bc093719SEd Schouten 			return;
1121bc093719SEd Schouten 	}
1122bc093719SEd Schouten 	tty_wakeup(tp, FWRITE);
1123bc093719SEd Schouten }
1124bc093719SEd Schouten 
1125bc093719SEd Schouten size_t
1126bc093719SEd Schouten ttydisc_getc(struct tty *tp, void *buf, size_t len)
1127bc093719SEd Schouten {
1128bc093719SEd Schouten 
1129bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1130bc093719SEd Schouten 
1131bc093719SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1132bc093719SEd Schouten 		return (0);
1133bc093719SEd Schouten 
1134a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_inject))
1135a1215e37SEd Schouten 		return ttyhook_getc_inject(tp, buf, len);
1136bc093719SEd Schouten 
1137a1215e37SEd Schouten 	len = ttyoutq_read(&tp->t_outq, buf, len);
1138a1215e37SEd Schouten 
1139a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_capture))
1140a1215e37SEd Schouten 		ttyhook_getc_capture(tp, buf, len);
1141a1215e37SEd Schouten 
1142a1215e37SEd Schouten 	ttydisc_wakeup_watermark(tp);
1143ffffa83bSEd Schouten 	atomic_add_long(&tty_nout, len);
1144bc093719SEd Schouten 
1145ffffa83bSEd Schouten 	return (len);
1146bc093719SEd Schouten }
1147bc093719SEd Schouten 
1148bc093719SEd Schouten int
1149bc093719SEd Schouten ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1150bc093719SEd Schouten {
1151a1215e37SEd Schouten 	int error = 0;
1152bc093719SEd Schouten 	int obytes = uio->uio_resid;
1153a1215e37SEd Schouten 	size_t len;
1154a1215e37SEd Schouten 	char buf[TTY_STACKBUF];
1155bc093719SEd Schouten 
1156bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1157bc093719SEd Schouten 
1158bc093719SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1159bc093719SEd Schouten 		return (0);
1160bc093719SEd Schouten 
1161a1215e37SEd Schouten 	/*
1162a1215e37SEd Schouten 	 * When a TTY hook is attached, we cannot perform unbuffered
1163a1215e37SEd Schouten 	 * copying to userspace. Just call ttydisc_getc() and
1164a1215e37SEd Schouten 	 * temporarily store data in a shadow buffer.
1165a1215e37SEd Schouten 	 */
1166a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_capture) ||
1167a1215e37SEd Schouten 	    ttyhook_hashook(tp, getc_inject)) {
1168a1215e37SEd Schouten 		while (uio->uio_resid > 0) {
1169a1215e37SEd Schouten 			/* Read to shadow buffer. */
1170a1215e37SEd Schouten 			len = ttydisc_getc(tp, buf,
1171a1215e37SEd Schouten 			    MIN(uio->uio_resid, sizeof buf));
1172a1215e37SEd Schouten 			if (len == 0)
1173a1215e37SEd Schouten 				break;
1174bc093719SEd Schouten 
1175a1215e37SEd Schouten 			/* Copy to userspace. */
1176a1215e37SEd Schouten 			tty_unlock(tp);
1177a1215e37SEd Schouten 			error = uiomove(buf, len, uio);
1178a1215e37SEd Schouten 			tty_lock(tp);
1179a1215e37SEd Schouten 
1180a1215e37SEd Schouten 			if (error != 0)
1181a1215e37SEd Schouten 				break;
1182a1215e37SEd Schouten 		}
1183a1215e37SEd Schouten 	} else {
1184a1215e37SEd Schouten 		error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1185a1215e37SEd Schouten 
1186a1215e37SEd Schouten 		ttydisc_wakeup_watermark(tp);
1187bc093719SEd Schouten 		atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1188a1215e37SEd Schouten 	}
1189bc093719SEd Schouten 
1190bc093719SEd Schouten 	return (error);
1191bc093719SEd Schouten }
1192bc093719SEd Schouten 
1193a1215e37SEd Schouten size_t
1194a1215e37SEd Schouten ttydisc_getc_poll(struct tty *tp)
1195a1215e37SEd Schouten {
1196a1215e37SEd Schouten 
1197a1215e37SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1198a1215e37SEd Schouten 
1199a1215e37SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1200a1215e37SEd Schouten 		return (0);
1201a1215e37SEd Schouten 
1202a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_poll))
1203a1215e37SEd Schouten 		return ttyhook_getc_poll(tp);
1204a1215e37SEd Schouten 
1205a1215e37SEd Schouten 	return ttyoutq_bytesused(&tp->t_outq);
1206a1215e37SEd Schouten }
1207a1215e37SEd Schouten 
1208bc093719SEd Schouten /*
1209bc093719SEd Schouten  * XXX: not really related to the TTYDISC, but we'd better put
1210bc093719SEd Schouten  * tty_putchar() here, because we need to perform proper output
1211bc093719SEd Schouten  * processing.
1212bc093719SEd Schouten  */
1213bc093719SEd Schouten 
1214bc093719SEd Schouten int
1215bc093719SEd Schouten tty_putchar(struct tty *tp, char c)
1216bc093719SEd Schouten {
1217bc093719SEd Schouten 	tty_lock_assert(tp, MA_OWNED);
1218bc093719SEd Schouten 
1219bc093719SEd Schouten 	if (tty_gone(tp))
1220bc093719SEd Schouten 		return (-1);
1221bc093719SEd Schouten 
1222bc093719SEd Schouten 	ttydisc_echo_force(tp, c, 0);
1223bc093719SEd Schouten 	tp->t_writepos = tp->t_column;
1224bc093719SEd Schouten 	ttyinq_reprintpos_set(&tp->t_inq);
1225bc093719SEd Schouten 
1226bc093719SEd Schouten 	ttydevsw_outwakeup(tp);
1227bc093719SEd Schouten 	return (0);
1228bc093719SEd Schouten }
1229