xref: /freebsd/sys/kern/tty_ttydisc.c (revision 522083ff)
1bc093719SEd Schouten /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
38a36da99SPedro F. Giffuni  *
4bc093719SEd Schouten  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5bc093719SEd Schouten  * All rights reserved.
6bc093719SEd Schouten  *
7bc093719SEd Schouten  * Portions of this software were developed under sponsorship from Snow
8bc093719SEd Schouten  * B.V., the Netherlands.
9bc093719SEd Schouten  *
10bc093719SEd Schouten  * Redistribution and use in source and binary forms, with or without
11bc093719SEd Schouten  * modification, are permitted provided that the following conditions
12bc093719SEd Schouten  * are met:
13bc093719SEd Schouten  * 1. Redistributions of source code must retain the above copyright
14bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer.
15bc093719SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
16bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
17bc093719SEd Schouten  *    documentation and/or other materials provided with the distribution.
18bc093719SEd Schouten  *
19bc093719SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20bc093719SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21bc093719SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22bc093719SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23bc093719SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24bc093719SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25bc093719SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26bc093719SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27bc093719SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28bc093719SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29bc093719SEd Schouten  * SUCH DAMAGE.
30bc093719SEd Schouten  */
31bc093719SEd Schouten 
32bc093719SEd Schouten #include <sys/param.h>
33bc093719SEd Schouten #include <sys/fcntl.h>
34bc093719SEd Schouten #include <sys/filio.h>
35bc093719SEd Schouten #include <sys/kernel.h>
36bc093719SEd Schouten #include <sys/signal.h>
37bc093719SEd Schouten #include <sys/sysctl.h>
38bc093719SEd Schouten #include <sys/systm.h>
39bc093719SEd Schouten #include <sys/tty.h>
40bc093719SEd Schouten #include <sys/ttycom.h>
41bc093719SEd Schouten #include <sys/ttydefaults.h>
42bc093719SEd Schouten #include <sys/uio.h>
43bc093719SEd Schouten #include <sys/vnode.h>
44bc093719SEd Schouten 
459e589b09SBojan Novković #include <teken/teken.h>
469e589b09SBojan Novković #include <teken/teken_wcwidth.h>
479e589b09SBojan Novković 
48bc093719SEd Schouten /*
49bc093719SEd Schouten  * Standard TTYDISC `termios' line discipline.
50bc093719SEd Schouten  */
51bc093719SEd Schouten 
52bc093719SEd Schouten /* Statistics. */
531d952ed2SEd Schouten static unsigned long tty_nin = 0;
541d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
55bc093719SEd Schouten 	&tty_nin, 0, "Total amount of bytes received");
561d952ed2SEd Schouten static unsigned long tty_nout = 0;
571d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
58bc093719SEd Schouten 	&tty_nout, 0, "Total amount of bytes transmitted");
59bc093719SEd Schouten 
60bc093719SEd Schouten /* termios comparison macro's. */
61bc093719SEd Schouten #define	CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
62bc093719SEd Schouten 			tp->t_termios.c_cc[v] == (c))
63bc093719SEd Schouten #define	CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
64bc093719SEd Schouten 
65bc093719SEd Schouten /* Characters that cannot be modified through c_cc. */
66bc093719SEd Schouten #define CTAB	'\t'
67bc093719SEd Schouten #define CNL	'\n'
68bc093719SEd Schouten #define CCR	'\r'
69bc093719SEd Schouten 
70bc093719SEd Schouten /* Character is a control character. */
71bc093719SEd Schouten #define CTL_VALID(c)	((c) == 0x7f || (unsigned char)(c) < 0x20)
72bc093719SEd Schouten /* Control character should be processed on echo. */
73bc093719SEd Schouten #define CTL_ECHO(c,q)	(!(q) && ((c) == CERASE2 || (c) == CTAB || \
74bc093719SEd Schouten     (c) == CNL || (c) == CCR))
75bc093719SEd Schouten /* Control character should be printed using ^X notation. */
76bc093719SEd Schouten #define CTL_PRINT(c,q)	((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
77bc093719SEd Schouten     ((q) || ((c) != CTAB && (c) != CNL))))
78bc093719SEd Schouten /* Character is whitespace. */
79bc093719SEd Schouten #define CTL_WHITE(c)	((c) == ' ' || (c) == CTAB)
80bc093719SEd Schouten /* Character is alphanumeric. */
81bc093719SEd Schouten #define CTL_ALNUM(c)	(((c) >= '0' && (c) <= '9') || \
82bc093719SEd Schouten     ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
839e589b09SBojan Novković /* Character is UTF8-encoded. */
849e589b09SBojan Novković #define CTL_UTF8(c) (!!((c) & 0x80))
859e589b09SBojan Novković /* Character is a UTF8 continuation byte. */
869e589b09SBojan Novković #define CTL_UTF8_CONT(c) (((c) & 0xc0) == 0x80)
87bc093719SEd Schouten 
88a1215e37SEd Schouten #define	TTY_STACKBUF	256
899e589b09SBojan Novković #define UTF8_STACKBUF 4
90a1215e37SEd Schouten 
91bc093719SEd Schouten void
ttydisc_open(struct tty * tp)92bc093719SEd Schouten ttydisc_open(struct tty *tp)
93bc093719SEd Schouten {
94bc093719SEd Schouten 	ttydisc_optimize(tp);
95bc093719SEd Schouten }
96bc093719SEd Schouten 
97bc093719SEd Schouten void
ttydisc_close(struct tty * tp)98bc093719SEd Schouten ttydisc_close(struct tty *tp)
99bc093719SEd Schouten {
100bc093719SEd Schouten 
101bc093719SEd Schouten 	/* Clean up our flags when leaving the discipline. */
102bc093719SEd Schouten 	tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
103cbda6f66SWarner Losh 	tp->t_termios.c_lflag &= ~FLUSHO;
104bc093719SEd Schouten 
1052cd5358aSKonstantin Belousov 	/*
1062cd5358aSKonstantin Belousov 	 * POSIX states that we must drain output and flush input on
1072cd5358aSKonstantin Belousov 	 * last close.  Draining has already been done if possible.
1082cd5358aSKonstantin Belousov 	 */
1092cd5358aSKonstantin Belousov 	tty_flush(tp, FREAD | FWRITE);
110a1215e37SEd Schouten 
111a1215e37SEd Schouten 	if (ttyhook_hashook(tp, close))
112a1215e37SEd Schouten 		ttyhook_close(tp);
113bc093719SEd Schouten }
114bc093719SEd Schouten 
115d51dac5fSKyle Evans /*
116d51dac5fSKyle Evans  * Populate our break array; it should likely be at least 4 bytes in size to
117d51dac5fSKyle Evans  * allow for \n, VEOF, and VEOL.
118d51dac5fSKyle Evans  */
119d51dac5fSKyle Evans static void
ttydisc_read_break(struct tty * tp,char * breakc,size_t breaksz)120d51dac5fSKyle Evans ttydisc_read_break(struct tty *tp, char *breakc, size_t breaksz)
121bc093719SEd Schouten {
122d51dac5fSKyle Evans 	size_t n = 0;
123bc093719SEd Schouten 
124d51dac5fSKyle Evans 	MPASS(breaksz != 0);
125d51dac5fSKyle Evans 
126d51dac5fSKyle Evans 	breakc[n++] = CNL;
127bc093719SEd Schouten #define BREAK_ADD(c) do { \
128d51dac5fSKyle Evans 	MPASS(n < breaksz - 1);	/* NUL terminated */	\
129bc093719SEd Schouten 	if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE)	\
130bc093719SEd Schouten 		breakc[n++] = tp->t_termios.c_cc[c];	\
131bc093719SEd Schouten } while (0)
132bc093719SEd Schouten 	/* Determine which characters we should trigger on. */
133bc093719SEd Schouten 	BREAK_ADD(VEOF);
134bc093719SEd Schouten 	BREAK_ADD(VEOL);
135bc093719SEd Schouten #undef BREAK_ADD
136d51dac5fSKyle Evans 
137bc093719SEd Schouten 	breakc[n] = '\0';
138d51dac5fSKyle Evans }
139d51dac5fSKyle Evans 
140d51dac5fSKyle Evans size_t
ttydisc_bytesavail(struct tty * tp)141d51dac5fSKyle Evans ttydisc_bytesavail(struct tty *tp)
142d51dac5fSKyle Evans {
143d51dac5fSKyle Evans 	size_t clen;
144d51dac5fSKyle Evans 	char breakc[4];
145d51dac5fSKyle Evans 	unsigned char lastc = _POSIX_VDISABLE;
146d51dac5fSKyle Evans 
147d51dac5fSKyle Evans 	clen = ttyinq_bytescanonicalized(&tp->t_inq);
148d51dac5fSKyle Evans 	if (!CMP_FLAG(l, ICANON) || clen == 0)
149d51dac5fSKyle Evans 		return (clen);
150d51dac5fSKyle Evans 
151d51dac5fSKyle Evans 	ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
152d51dac5fSKyle Evans 	clen = ttyinq_findchar(&tp->t_inq, breakc, clen, &lastc);
153d51dac5fSKyle Evans 
154d51dac5fSKyle Evans 	/*
155d51dac5fSKyle Evans 	 * We might have a partial line canonicalized in the input queue if we,
156d51dac5fSKyle Evans 	 * for instance, switched to ICANON after taking some input in raw mode.
157d51dac5fSKyle Evans 	 * In this case, read(2) will block because we only have a partial line.
158d51dac5fSKyle Evans 	 */
159d51dac5fSKyle Evans 	if (lastc == _POSIX_VDISABLE)
160d51dac5fSKyle Evans 		return (0);
161d51dac5fSKyle Evans 
162d51dac5fSKyle Evans 	/* If VEOF was our terminal, it must be discarded (not counted). */
163d51dac5fSKyle Evans 	if (CMP_CC(VEOF, lastc))
164d51dac5fSKyle Evans 		clen--;
165d51dac5fSKyle Evans 
166d51dac5fSKyle Evans 	return (clen);
167d51dac5fSKyle Evans }
168d51dac5fSKyle Evans 
169522083ffSKyle Evans void
ttydisc_canonicalize(struct tty * tp)170522083ffSKyle Evans ttydisc_canonicalize(struct tty *tp)
171522083ffSKyle Evans {
172522083ffSKyle Evans 	char breakc[4];
173522083ffSKyle Evans 
174522083ffSKyle Evans 	/*
175522083ffSKyle Evans 	 * If we're in non-canonical mode, it's as easy as just canonicalizing
176522083ffSKyle Evans 	 * the current partial line.
177522083ffSKyle Evans 	 */
178522083ffSKyle Evans 	if (!CMP_FLAG(l, ICANON)) {
179522083ffSKyle Evans 		ttyinq_canonicalize(&tp->t_inq);
180522083ffSKyle Evans 		return;
181522083ffSKyle Evans 	}
182522083ffSKyle Evans 
183522083ffSKyle Evans 	/*
184522083ffSKyle Evans 	 * For canonical mode, we need to rescan the buffer for the last EOL
185522083ffSKyle Evans 	 * indicator.
186522083ffSKyle Evans 	 */
187522083ffSKyle Evans 	ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
188522083ffSKyle Evans 	ttyinq_canonicalize_break(&tp->t_inq, breakc);
189522083ffSKyle Evans }
190522083ffSKyle Evans 
191d51dac5fSKyle Evans static int
ttydisc_read_canonical(struct tty * tp,struct uio * uio,int ioflag)192d51dac5fSKyle Evans ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
193d51dac5fSKyle Evans {
194d51dac5fSKyle Evans 	char breakc[4]; /* enough to hold \n, VEOF and VEOL. */
195d51dac5fSKyle Evans 	int error;
196d51dac5fSKyle Evans 	size_t clen, flen = 0;
197d51dac5fSKyle Evans 	unsigned char lastc = _POSIX_VDISABLE;
198d51dac5fSKyle Evans 
199d51dac5fSKyle Evans 	ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
200bc093719SEd Schouten 
201bc093719SEd Schouten 	do {
2021da7bb41SEd Schouten 		error = tty_wait_background(tp, curthread, SIGTTIN);
2031da7bb41SEd Schouten 		if (error)
2041da7bb41SEd Schouten 			return (error);
2051da7bb41SEd Schouten 
206bc093719SEd Schouten 		/*
207bc093719SEd Schouten 		 * Quite a tricky case: unlike the old TTY
208bc093719SEd Schouten 		 * implementation, this implementation copies data back
209bc093719SEd Schouten 		 * to userspace in large chunks. Unfortunately, we can't
210bc093719SEd Schouten 		 * calculate the line length on beforehand if it crosses
211bc093719SEd Schouten 		 * ttyinq_block boundaries, because multiple reads could
212bc093719SEd Schouten 		 * then make this code read beyond the newline.
213bc093719SEd Schouten 		 *
214bc093719SEd Schouten 		 * This is why we limit the read to:
215bc093719SEd Schouten 		 * - The size the user has requested
216bc093719SEd Schouten 		 * - The blocksize (done in tty_inq.c)
217bc093719SEd Schouten 		 * - The amount of bytes until the newline
218bc093719SEd Schouten 		 *
219bc093719SEd Schouten 		 * This causes the line length to be recalculated after
220bc093719SEd Schouten 		 * each block has been copied to userspace. This will
221bc093719SEd Schouten 		 * cause the TTY layer to return data in chunks using
222bc093719SEd Schouten 		 * the blocksize (except the first and last blocks).
223bc093719SEd Schouten 		 */
224d51dac5fSKyle Evans 		clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid + 1,
225bc093719SEd Schouten 		    &lastc);
226bc093719SEd Schouten 
227bc093719SEd Schouten 		/* No more data. */
228bc093719SEd Schouten 		if (clen == 0) {
2296b1b791dSEd Schouten 			if (tp->t_flags & TF_ZOMBIE)
230bc093719SEd Schouten 				return (0);
2316b1b791dSEd Schouten 			else if (ioflag & IO_NDELAY)
2326b1b791dSEd Schouten 				return (EWOULDBLOCK);
233bc093719SEd Schouten 
234bc093719SEd Schouten 			error = tty_wait(tp, &tp->t_inwait);
235bc093719SEd Schouten 			if (error)
236bc093719SEd Schouten 				return (error);
237bc093719SEd Schouten 			continue;
238bc093719SEd Schouten 		}
239bc093719SEd Schouten 
240d51dac5fSKyle Evans 		/*
241d51dac5fSKyle Evans 		 * Don't send the EOF char back to userspace.  Our above call to
242d51dac5fSKyle Evans 		 * ttyinq_findchar overreads by 1 character in case we would
243d51dac5fSKyle Evans 		 * otherwise be leaving an EOF for the next read().  We'll trim
244d51dac5fSKyle Evans 		 * clen back down to uio_resid whether we find our EOF or not.
245d51dac5fSKyle Evans 		 */
246bc093719SEd Schouten 		if (CMP_CC(VEOF, lastc))
247bc093719SEd Schouten 			flen = 1;
248bc093719SEd Schouten 
249d51dac5fSKyle Evans 		/*
250d51dac5fSKyle Evans 		 * Trim clen back down to the buffer size, since we had
251d51dac5fSKyle Evans 		 * intentionally over-read.
252d51dac5fSKyle Evans 		 */
253d51dac5fSKyle Evans 		clen = MIN(uio->uio_resid + flen, clen);
254bc093719SEd Schouten 		MPASS(flen <= clen);
255bc093719SEd Schouten 
256bc093719SEd Schouten 		/* Read and throw away the EOF character. */
257bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
258bc093719SEd Schouten 		if (error)
259bc093719SEd Schouten 			return (error);
260bc093719SEd Schouten 
261bc093719SEd Schouten 	} while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
262bc093719SEd Schouten 
263bc093719SEd Schouten 	return (0);
264bc093719SEd Schouten }
265bc093719SEd Schouten 
266bc093719SEd Schouten static int
ttydisc_read_raw_no_timer(struct tty * tp,struct uio * uio,int ioflag)267bc093719SEd Schouten ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
268bc093719SEd Schouten {
269bc093719SEd Schouten 	size_t vmin = tp->t_termios.c_cc[VMIN];
270526d0bd5SKonstantin Belousov 	ssize_t oresid = uio->uio_resid;
271bc093719SEd Schouten 	int error;
272bc093719SEd Schouten 
273bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] == 0);
274bc093719SEd Schouten 
275bc093719SEd Schouten 	/*
276bc093719SEd Schouten 	 * This routine implements the easy cases of read()s while in
277bc093719SEd Schouten 	 * non-canonical mode, namely case B and D, where we don't have
278bc093719SEd Schouten 	 * any timers at all.
279bc093719SEd Schouten 	 */
280bc093719SEd Schouten 
281bc093719SEd Schouten 	for (;;) {
2821da7bb41SEd Schouten 		error = tty_wait_background(tp, curthread, SIGTTIN);
2831da7bb41SEd Schouten 		if (error)
2841da7bb41SEd Schouten 			return (error);
2851da7bb41SEd Schouten 
286bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
287bc093719SEd Schouten 		    uio->uio_resid, 0);
288bc093719SEd Schouten 		if (error)
289bc093719SEd Schouten 			return (error);
290bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
291bc093719SEd Schouten 			return (0);
292bc093719SEd Schouten 
293bc093719SEd Schouten 		/* We have to wait for more. */
2946b1b791dSEd Schouten 		if (tp->t_flags & TF_ZOMBIE)
295bc093719SEd Schouten 			return (0);
2966b1b791dSEd Schouten 		else if (ioflag & IO_NDELAY)
2976b1b791dSEd Schouten 			return (EWOULDBLOCK);
298bc093719SEd Schouten 
299bc093719SEd Schouten 		error = tty_wait(tp, &tp->t_inwait);
300bc093719SEd Schouten 		if (error)
301bc093719SEd Schouten 			return (error);
302bc093719SEd Schouten 	}
303bc093719SEd Schouten }
304bc093719SEd Schouten 
305bc093719SEd Schouten static int
ttydisc_read_raw_read_timer(struct tty * tp,struct uio * uio,int ioflag,int oresid)306bc093719SEd Schouten ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
307bc093719SEd Schouten     int oresid)
308bc093719SEd Schouten {
309bc093719SEd Schouten 	size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
310bc093719SEd Schouten 	unsigned int vtime = tp->t_termios.c_cc[VTIME];
311bc093719SEd Schouten 	struct timeval end, now, left;
312bc093719SEd Schouten 	int error, hz;
313bc093719SEd Schouten 
314bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] != 0);
315bc093719SEd Schouten 
316bc093719SEd Schouten 	/* Determine when the read should be expired. */
317bc093719SEd Schouten 	end.tv_sec = vtime / 10;
318bc093719SEd Schouten 	end.tv_usec = (vtime % 10) * 100000;
319bc093719SEd Schouten 	getmicrotime(&now);
320bc093719SEd Schouten 	timevaladd(&end, &now);
321bc093719SEd Schouten 
322bc093719SEd Schouten 	for (;;) {
3231da7bb41SEd Schouten 		error = tty_wait_background(tp, curthread, SIGTTIN);
3241da7bb41SEd Schouten 		if (error)
3251da7bb41SEd Schouten 			return (error);
3261da7bb41SEd Schouten 
327bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
328bc093719SEd Schouten 		    uio->uio_resid, 0);
329bc093719SEd Schouten 		if (error)
330bc093719SEd Schouten 			return (error);
331bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
332bc093719SEd Schouten 			return (0);
333bc093719SEd Schouten 
334bc093719SEd Schouten 		/* Calculate how long we should wait. */
335bc093719SEd Schouten 		getmicrotime(&now);
336bc093719SEd Schouten 		if (timevalcmp(&now, &end, >))
337bc093719SEd Schouten 			return (0);
338bc093719SEd Schouten 		left = end;
339bc093719SEd Schouten 		timevalsub(&left, &now);
340bc093719SEd Schouten 		hz = tvtohz(&left);
341bc093719SEd Schouten 
342bc093719SEd Schouten 		/*
343bc093719SEd Schouten 		 * We have to wait for more. If the timer expires, we
344bc093719SEd Schouten 		 * should return a 0-byte read.
345bc093719SEd Schouten 		 */
3466b1b791dSEd Schouten 		if (tp->t_flags & TF_ZOMBIE)
347bc093719SEd Schouten 			return (0);
3486b1b791dSEd Schouten 		else if (ioflag & IO_NDELAY)
3496b1b791dSEd Schouten 			return (EWOULDBLOCK);
350bc093719SEd Schouten 
351bc093719SEd Schouten 		error = tty_timedwait(tp, &tp->t_inwait, hz);
352bc093719SEd Schouten 		if (error)
353bc093719SEd Schouten 			return (error == EWOULDBLOCK ? 0 : error);
354bc093719SEd Schouten 	}
355bc093719SEd Schouten 
356bc093719SEd Schouten 	return (0);
357bc093719SEd Schouten }
358bc093719SEd Schouten 
359bc093719SEd Schouten static int
ttydisc_read_raw_interbyte_timer(struct tty * tp,struct uio * uio,int ioflag)360bc093719SEd Schouten ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
361bc093719SEd Schouten {
362bc093719SEd Schouten 	size_t vmin = tp->t_termios.c_cc[VMIN];
363526d0bd5SKonstantin Belousov 	ssize_t oresid = uio->uio_resid;
364bc093719SEd Schouten 	int error;
365bc093719SEd Schouten 
366bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VMIN] != 0);
367bc093719SEd Schouten 	MPASS(tp->t_termios.c_cc[VTIME] != 0);
368bc093719SEd Schouten 
369bc093719SEd Schouten 	/*
370bc093719SEd Schouten 	 * When using the interbyte timer, the timer should be started
371bc093719SEd Schouten 	 * after the first byte has been received. We just call into the
372bc093719SEd Schouten 	 * generic read timer code after we've received the first byte.
373bc093719SEd Schouten 	 */
374bc093719SEd Schouten 
375bc093719SEd Schouten 	for (;;) {
3761da7bb41SEd Schouten 		error = tty_wait_background(tp, curthread, SIGTTIN);
3771da7bb41SEd Schouten 		if (error)
3781da7bb41SEd Schouten 			return (error);
3791da7bb41SEd Schouten 
380bc093719SEd Schouten 		error = ttyinq_read_uio(&tp->t_inq, tp, uio,
381bc093719SEd Schouten 		    uio->uio_resid, 0);
382bc093719SEd Schouten 		if (error)
383bc093719SEd Schouten 			return (error);
384bc093719SEd Schouten 		if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
385bc093719SEd Schouten 			return (0);
386bc093719SEd Schouten 
387bc093719SEd Schouten 		/*
388bc093719SEd Schouten 		 * Not enough data, but we did receive some, which means
389bc093719SEd Schouten 		 * we'll now start using the interbyte timer.
390bc093719SEd Schouten 		 */
391bc093719SEd Schouten 		if (oresid != uio->uio_resid)
392bc093719SEd Schouten 			break;
393bc093719SEd Schouten 
394bc093719SEd Schouten 		/* We have to wait for more. */
3956b1b791dSEd Schouten 		if (tp->t_flags & TF_ZOMBIE)
396bc093719SEd Schouten 			return (0);
3976b1b791dSEd Schouten 		else if (ioflag & IO_NDELAY)
3986b1b791dSEd Schouten 			return (EWOULDBLOCK);
399bc093719SEd Schouten 
400bc093719SEd Schouten 		error = tty_wait(tp, &tp->t_inwait);
401bc093719SEd Schouten 		if (error)
402bc093719SEd Schouten 			return (error);
403bc093719SEd Schouten 	}
404bc093719SEd Schouten 
405bc093719SEd Schouten 	return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
406bc093719SEd Schouten }
407bc093719SEd Schouten 
408bc093719SEd Schouten int
ttydisc_read(struct tty * tp,struct uio * uio,int ioflag)409bc093719SEd Schouten ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
410bc093719SEd Schouten {
411bc093719SEd Schouten 	int error;
412bc093719SEd Schouten 
41323d53268SKyle Evans 	tty_assert_locked(tp);
414bc093719SEd Schouten 
415bc093719SEd Schouten 	if (uio->uio_resid == 0)
416bc093719SEd Schouten 		return (0);
417bc093719SEd Schouten 
418bc093719SEd Schouten 	if (CMP_FLAG(l, ICANON))
419bc093719SEd Schouten 		error = ttydisc_read_canonical(tp, uio, ioflag);
420bc093719SEd Schouten 	else if (tp->t_termios.c_cc[VTIME] == 0)
421bc093719SEd Schouten 		error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
422bc093719SEd Schouten 	else if (tp->t_termios.c_cc[VMIN] == 0)
423bc093719SEd Schouten 		error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
424bc093719SEd Schouten 		    uio->uio_resid);
425bc093719SEd Schouten 	else
426bc093719SEd Schouten 		error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
427bc093719SEd Schouten 
428a15ec0a5SEd Schouten 	if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
429a15ec0a5SEd Schouten 	    ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
430bc093719SEd Schouten 		/* Unset the input watermark when we've got enough space. */
431bc093719SEd Schouten 		tty_hiwat_in_unblock(tp);
432bc093719SEd Schouten 	}
433bc093719SEd Schouten 
434bc093719SEd Schouten 	return (error);
435bc093719SEd Schouten }
436bc093719SEd Schouten 
437bc093719SEd Schouten static __inline unsigned int
ttydisc_findchar(const char * obstart,unsigned int oblen)438bc093719SEd Schouten ttydisc_findchar(const char *obstart, unsigned int oblen)
439bc093719SEd Schouten {
440bc093719SEd Schouten 	const char *c = obstart;
441bc093719SEd Schouten 
442bc093719SEd Schouten 	while (oblen--) {
443bc093719SEd Schouten 		if (CTL_VALID(*c))
444bc093719SEd Schouten 			break;
445bc093719SEd Schouten 		c++;
446bc093719SEd Schouten 	}
447bc093719SEd Schouten 
448bc093719SEd Schouten 	return (c - obstart);
449bc093719SEd Schouten }
450bc093719SEd Schouten 
451bc093719SEd Schouten static int
ttydisc_write_oproc(struct tty * tp,char c)452bc093719SEd Schouten ttydisc_write_oproc(struct tty *tp, char c)
453bc093719SEd Schouten {
454bc093719SEd Schouten 	unsigned int scnt, error;
455bc093719SEd Schouten 
456bc093719SEd Schouten 	MPASS(CMP_FLAG(o, OPOST));
457bc093719SEd Schouten 	MPASS(CTL_VALID(c));
458bc093719SEd Schouten 
459bc093719SEd Schouten #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
460bc093719SEd Schouten 	switch (c) {
461bc093719SEd Schouten 	case CEOF:
462bc093719SEd Schouten 		/* End-of-text dropping. */
463bc093719SEd Schouten 		if (CMP_FLAG(o, ONOEOT))
464bc093719SEd Schouten 			return (0);
465bc093719SEd Schouten 		return PRINT_NORMAL();
466bc093719SEd Schouten 
467bc093719SEd Schouten 	case CERASE2:
468bc093719SEd Schouten 		/* Handle backspace to fix tab expansion. */
469bc093719SEd Schouten 		if (PRINT_NORMAL() != 0)
470bc093719SEd Schouten 			return (-1);
471bc093719SEd Schouten 		if (tp->t_column > 0)
472bc093719SEd Schouten 			tp->t_column--;
473bc093719SEd Schouten 		return (0);
474bc093719SEd Schouten 
475bc093719SEd Schouten 	case CTAB:
476bc093719SEd Schouten 		/* Tab expansion. */
477bc093719SEd Schouten 		scnt = 8 - (tp->t_column & 7);
478bc093719SEd Schouten 		if (CMP_FLAG(o, TAB3)) {
479bc093719SEd Schouten 			error = ttyoutq_write_nofrag(&tp->t_outq,
480bc093719SEd Schouten 			    "        ", scnt);
481bc093719SEd Schouten 		} else {
482bc093719SEd Schouten 			error = PRINT_NORMAL();
483bc093719SEd Schouten 		}
484bc093719SEd Schouten 		if (error)
485bc093719SEd Schouten 			return (-1);
486bc093719SEd Schouten 
487bc093719SEd Schouten 		tp->t_column += scnt;
488bc093719SEd Schouten 		MPASS((tp->t_column % 8) == 0);
489bc093719SEd Schouten 		return (0);
490bc093719SEd Schouten 
491bc093719SEd Schouten 	case CNL:
492bc093719SEd Schouten 		/* Newline conversion. */
493bc093719SEd Schouten 		if (CMP_FLAG(o, ONLCR)) {
494bc093719SEd Schouten 			/* Convert \n to \r\n. */
495bc093719SEd Schouten 			error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
496bc093719SEd Schouten 		} else {
497bc093719SEd Schouten 			error = PRINT_NORMAL();
498bc093719SEd Schouten 		}
499bc093719SEd Schouten 		if (error)
500bc093719SEd Schouten 			return (-1);
501bc093719SEd Schouten 
502bc093719SEd Schouten 		if (CMP_FLAG(o, ONLCR|ONLRET)) {
503bc093719SEd Schouten 			tp->t_column = tp->t_writepos = 0;
504bc093719SEd Schouten 			ttyinq_reprintpos_set(&tp->t_inq);
505bc093719SEd Schouten 		}
506bc093719SEd Schouten 		return (0);
507bc093719SEd Schouten 
508bc093719SEd Schouten 	case CCR:
509bc093719SEd Schouten 		/* Carriage return to newline conversion. */
510bc093719SEd Schouten 		if (CMP_FLAG(o, OCRNL))
511bc093719SEd Schouten 			c = CNL;
512bc093719SEd Schouten 		/* Omit carriage returns on column 0. */
513bc093719SEd Schouten 		if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
514bc093719SEd Schouten 			return (0);
515bc093719SEd Schouten 		if (PRINT_NORMAL() != 0)
516bc093719SEd Schouten 			return (-1);
517bc093719SEd Schouten 
518bc093719SEd Schouten 		tp->t_column = tp->t_writepos = 0;
519bc093719SEd Schouten 		ttyinq_reprintpos_set(&tp->t_inq);
520bc093719SEd Schouten 		return (0);
521bc093719SEd Schouten 	}
522bc093719SEd Schouten 
523bc093719SEd Schouten 	/*
524bc093719SEd Schouten 	 * Invisible control character. Print it, but don't
525bc093719SEd Schouten 	 * increase the column count.
526bc093719SEd Schouten 	 */
527bc093719SEd Schouten 	return PRINT_NORMAL();
528bc093719SEd Schouten #undef PRINT_NORMAL
529bc093719SEd Schouten }
530bc093719SEd Schouten 
531bc093719SEd Schouten /*
532bc093719SEd Schouten  * Just like the old TTY implementation, we need to copy data in chunks
533bc093719SEd Schouten  * into a temporary buffer. One of the reasons why we need to do this,
534bc093719SEd Schouten  * is because output processing (only TAB3 though) may allow the buffer
535bc093719SEd Schouten  * to grow eight times.
536bc093719SEd Schouten  */
537bc093719SEd Schouten int
ttydisc_write(struct tty * tp,struct uio * uio,int ioflag)538bc093719SEd Schouten ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
539bc093719SEd Schouten {
540a1215e37SEd Schouten 	char ob[TTY_STACKBUF];
541bc093719SEd Schouten 	char *obstart;
542bc093719SEd Schouten 	int error = 0;
543bc093719SEd Schouten 	unsigned int oblen = 0;
544bc093719SEd Schouten 
54523d53268SKyle Evans 	tty_assert_locked(tp);
546bc093719SEd Schouten 
547bc093719SEd Schouten 	if (tp->t_flags & TF_ZOMBIE)
548bc093719SEd Schouten 		return (EIO);
549bc093719SEd Schouten 
550bc093719SEd Schouten 	/*
551bc093719SEd Schouten 	 * We don't need to check whether the process is the foreground
552bc093719SEd Schouten 	 * process group or if we have a carrier. This is already done
553bc093719SEd Schouten 	 * in ttydev_write().
554bc093719SEd Schouten 	 */
555bc093719SEd Schouten 
556bc093719SEd Schouten 	while (uio->uio_resid > 0) {
557bc093719SEd Schouten 		unsigned int nlen;
558bc093719SEd Schouten 
559bc093719SEd Schouten 		MPASS(oblen == 0);
560bc093719SEd Schouten 
561cbda6f66SWarner Losh 		if (CMP_FLAG(l, FLUSHO)) {
562cbda6f66SWarner Losh 			uio->uio_offset += uio->uio_resid;
563cbda6f66SWarner Losh 			uio->uio_resid = 0;
564cbda6f66SWarner Losh 			return (0);
565cbda6f66SWarner Losh 		}
566cbda6f66SWarner Losh 
567bc093719SEd Schouten 		/* Step 1: read data. */
568bc093719SEd Schouten 		obstart = ob;
569bc093719SEd Schouten 		nlen = MIN(uio->uio_resid, sizeof ob);
57087fe0fa8SEd Schouten 		tty_unlock(tp);
571bc093719SEd Schouten 		error = uiomove(ob, nlen, uio);
57287fe0fa8SEd Schouten 		tty_lock(tp);
573bc093719SEd Schouten 		if (error != 0)
574bc093719SEd Schouten 			break;
575bc093719SEd Schouten 		oblen = nlen;
576bc093719SEd Schouten 
577bc093719SEd Schouten 		if (tty_gone(tp)) {
578bc093719SEd Schouten 			error = ENXIO;
579bc093719SEd Schouten 			break;
580bc093719SEd Schouten 		}
581bc093719SEd Schouten 
582bc093719SEd Schouten 		MPASS(oblen > 0);
583bc093719SEd Schouten 
584bc093719SEd Schouten 		/* Step 2: process data. */
585bc093719SEd Schouten 		do {
586bc093719SEd Schouten 			unsigned int plen, wlen;
587bc093719SEd Schouten 
588cbda6f66SWarner Losh 			if (CMP_FLAG(l, FLUSHO)) {
589cbda6f66SWarner Losh 				uio->uio_offset += uio->uio_resid;
590cbda6f66SWarner Losh 				uio->uio_resid = 0;
591cbda6f66SWarner Losh 				return (0);
592cbda6f66SWarner Losh 			}
593cbda6f66SWarner Losh 
594bc093719SEd Schouten 			/* Search for special characters for post processing. */
595bc093719SEd Schouten 			if (CMP_FLAG(o, OPOST)) {
596bc093719SEd Schouten 				plen = ttydisc_findchar(obstart, oblen);
597bc093719SEd Schouten 			} else {
598bc093719SEd Schouten 				plen = oblen;
599bc093719SEd Schouten 			}
600bc093719SEd Schouten 
601bc093719SEd Schouten 			if (plen == 0) {
602bc093719SEd Schouten 				/*
603bc093719SEd Schouten 				 * We're going to process a character
604bc093719SEd Schouten 				 * that needs processing
605bc093719SEd Schouten 				 */
606bc093719SEd Schouten 				if (ttydisc_write_oproc(tp, *obstart) == 0) {
607bc093719SEd Schouten 					obstart++;
608bc093719SEd Schouten 					oblen--;
609bc093719SEd Schouten 
610bc093719SEd Schouten 					tp->t_writepos = tp->t_column;
611bc093719SEd Schouten 					ttyinq_reprintpos_set(&tp->t_inq);
612bc093719SEd Schouten 					continue;
613bc093719SEd Schouten 				}
614bc093719SEd Schouten 			} else {
615bc093719SEd Schouten 				/* We're going to write regular data. */
616bc093719SEd Schouten 				wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
617bc093719SEd Schouten 				obstart += wlen;
618bc093719SEd Schouten 				oblen -= wlen;
619bc093719SEd Schouten 				tp->t_column += wlen;
620bc093719SEd Schouten 
621bc093719SEd Schouten 				tp->t_writepos = tp->t_column;
622bc093719SEd Schouten 				ttyinq_reprintpos_set(&tp->t_inq);
623bc093719SEd Schouten 
624bc093719SEd Schouten 				if (wlen == plen)
625bc093719SEd Schouten 					continue;
626bc093719SEd Schouten 			}
627bc093719SEd Schouten 
628bc093719SEd Schouten 			/* Watermark reached. Try to sleep. */
629bc093719SEd Schouten 			tp->t_flags |= TF_HIWAT_OUT;
630bc093719SEd Schouten 
631bc093719SEd Schouten 			if (ioflag & IO_NDELAY) {
632bc093719SEd Schouten 				error = EWOULDBLOCK;
633bc093719SEd Schouten 				goto done;
634bc093719SEd Schouten 			}
635bc093719SEd Schouten 
636bc093719SEd Schouten 			/*
637bc093719SEd Schouten 			 * The driver may write back the data
638bc093719SEd Schouten 			 * synchronously. Be sure to check the high
639bc093719SEd Schouten 			 * water mark before going to sleep.
640bc093719SEd Schouten 			 */
641bc093719SEd Schouten 			ttydevsw_outwakeup(tp);
642bc093719SEd Schouten 			if ((tp->t_flags & TF_HIWAT_OUT) == 0)
643bc093719SEd Schouten 				continue;
644bc093719SEd Schouten 
645bc093719SEd Schouten 			error = tty_wait(tp, &tp->t_outwait);
646bc093719SEd Schouten 			if (error)
647bc093719SEd Schouten 				goto done;
648bc093719SEd Schouten 
649bc093719SEd Schouten 			if (tp->t_flags & TF_ZOMBIE) {
650bc093719SEd Schouten 				error = EIO;
651bc093719SEd Schouten 				goto done;
652bc093719SEd Schouten 			}
653bc093719SEd Schouten 		} while (oblen > 0);
654bc093719SEd Schouten 	}
655bc093719SEd Schouten 
656bc093719SEd Schouten done:
657856ebf85SChristian S.J. Peron 	if (!tty_gone(tp))
658bc093719SEd Schouten 		ttydevsw_outwakeup(tp);
659bc093719SEd Schouten 
660bc093719SEd Schouten 	/*
661bc093719SEd Schouten 	 * Add the amount of bytes that we didn't process back to the
662bc093719SEd Schouten 	 * uio counters. We need to do this to make sure write() doesn't
663bc093719SEd Schouten 	 * count the bytes we didn't store in the queue.
664bc093719SEd Schouten 	 */
665bc093719SEd Schouten 	uio->uio_resid += oblen;
666bc093719SEd Schouten 	return (error);
667bc093719SEd Schouten }
668bc093719SEd Schouten 
669bc093719SEd Schouten void
ttydisc_optimize(struct tty * tp)670bc093719SEd Schouten ttydisc_optimize(struct tty *tp)
671bc093719SEd Schouten {
67223d53268SKyle Evans 	tty_assert_locked(tp);
673bc093719SEd Schouten 
67414358b0fSEd Schouten 	if (ttyhook_hashook(tp, rint_bypass)) {
67514358b0fSEd Schouten 		tp->t_flags |= TF_BYPASS;
67614358b0fSEd Schouten 	} else if (ttyhook_hashook(tp, rint)) {
67714358b0fSEd Schouten 		tp->t_flags &= ~TF_BYPASS;
67814358b0fSEd Schouten 	} else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
679bc093719SEd Schouten 	    (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
680bc093719SEd Schouten 	    (!CMP_FLAG(i, PARMRK) ||
681bc093719SEd Schouten 		CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
68214358b0fSEd Schouten 	    !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
683bc093719SEd Schouten 		tp->t_flags |= TF_BYPASS;
684bc093719SEd Schouten 	} else {
685bc093719SEd Schouten 		tp->t_flags &= ~TF_BYPASS;
686bc093719SEd Schouten 	}
687bc093719SEd Schouten }
688bc093719SEd Schouten 
689bc093719SEd Schouten void
ttydisc_modem(struct tty * tp,int open)690bc093719SEd Schouten ttydisc_modem(struct tty *tp, int open)
691bc093719SEd Schouten {
692bc093719SEd Schouten 
69323d53268SKyle Evans 	tty_assert_locked(tp);
694bc093719SEd Schouten 
695bc093719SEd Schouten 	if (open)
696bc093719SEd Schouten 		cv_broadcast(&tp->t_dcdwait);
697bc093719SEd Schouten 
698bc093719SEd Schouten 	/*
699bc093719SEd Schouten 	 * Ignore modem status lines when CLOCAL is turned on, but don't
700bc093719SEd Schouten 	 * enter the zombie state when the TTY isn't opened, because
701bc093719SEd Schouten 	 * that would cause the TTY to be in zombie state after being
702bc093719SEd Schouten 	 * opened.
703bc093719SEd Schouten 	 */
704bc093719SEd Schouten 	if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
705bc093719SEd Schouten 		return;
706bc093719SEd Schouten 
707bc093719SEd Schouten 	if (open == 0) {
708bc093719SEd Schouten 		/*
709bc093719SEd Schouten 		 * Lost carrier.
710bc093719SEd Schouten 		 */
711bc093719SEd Schouten 		tp->t_flags |= TF_ZOMBIE;
712bc093719SEd Schouten 
713bc093719SEd Schouten 		tty_signal_sessleader(tp, SIGHUP);
714bc093719SEd Schouten 		tty_flush(tp, FREAD|FWRITE);
715bc093719SEd Schouten 	} else {
716bc093719SEd Schouten 		/*
717bc093719SEd Schouten 		 * Carrier is back again.
718bc093719SEd Schouten 		 */
719bc093719SEd Schouten 
720bc093719SEd Schouten 		/* XXX: what should we do here? */
721bc093719SEd Schouten 	}
722bc093719SEd Schouten }
723bc093719SEd Schouten 
724bc093719SEd Schouten static int
ttydisc_echo_force(struct tty * tp,char c,int quote)725bc093719SEd Schouten ttydisc_echo_force(struct tty *tp, char c, int quote)
726bc093719SEd Schouten {
727bc093719SEd Schouten 
728cbda6f66SWarner Losh 	if (CMP_FLAG(l, FLUSHO))
729cbda6f66SWarner Losh 		return 0;
730cbda6f66SWarner Losh 
731bc093719SEd Schouten 	if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
732bc093719SEd Schouten 		/*
733bc093719SEd Schouten 		 * Only perform postprocessing when OPOST is turned on
734bc093719SEd Schouten 		 * and the character is an unquoted BS/TB/NL/CR.
735bc093719SEd Schouten 		 */
736bc093719SEd Schouten 		return ttydisc_write_oproc(tp, c);
737bc093719SEd Schouten 	} else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
738bc093719SEd Schouten 		/*
739bc093719SEd Schouten 		 * Only use ^X notation when ECHOCTL is turned on and
740bc093719SEd Schouten 		 * we've got an quoted control character.
74139410373SEd Schouten 		 *
74239410373SEd Schouten 		 * Print backspaces when echoing an end-of-file.
743bc093719SEd Schouten 		 */
74439410373SEd Schouten 		char ob[4] = "^?\b\b";
745bc093719SEd Schouten 
746bc093719SEd Schouten 		/* Print ^X notation. */
747bc093719SEd Schouten 		if (c != 0x7f)
748bc093719SEd Schouten 			ob[1] = c + 'A' - 1;
749bc093719SEd Schouten 
75039410373SEd Schouten 		if (!quote && CMP_CC(VEOF, c)) {
75139410373SEd Schouten 			return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
75239410373SEd Schouten 		} else {
753bc093719SEd Schouten 			tp->t_column += 2;
754bc093719SEd Schouten 			return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
75539410373SEd Schouten 		}
756bc093719SEd Schouten 	} else {
757bc093719SEd Schouten 		/* Can just be printed. */
758bc093719SEd Schouten 		tp->t_column++;
759bc093719SEd Schouten 		return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
760bc093719SEd Schouten 	}
761bc093719SEd Schouten }
762bc093719SEd Schouten 
763bc093719SEd Schouten static int
ttydisc_echo(struct tty * tp,char c,int quote)764bc093719SEd Schouten ttydisc_echo(struct tty *tp, char c, int quote)
765bc093719SEd Schouten {
766bc093719SEd Schouten 
767bc093719SEd Schouten 	/*
768bc093719SEd Schouten 	 * Only echo characters when ECHO is turned on, or ECHONL when
769bc093719SEd Schouten 	 * the character is an unquoted newline.
770bc093719SEd Schouten 	 */
771bc093719SEd Schouten 	if (!CMP_FLAG(l, ECHO) &&
772bc093719SEd Schouten 	    (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
773bc093719SEd Schouten 		return (0);
774bc093719SEd Schouten 
775bc093719SEd Schouten 	return ttydisc_echo_force(tp, c, quote);
776bc093719SEd Schouten }
777bc093719SEd Schouten 
778bc093719SEd Schouten static void
ttydisc_reprint_char(void * d,char c,int quote)779bc093719SEd Schouten ttydisc_reprint_char(void *d, char c, int quote)
780bc093719SEd Schouten {
781bc093719SEd Schouten 	struct tty *tp = d;
782bc093719SEd Schouten 
783bc093719SEd Schouten 	ttydisc_echo(tp, c, quote);
784bc093719SEd Schouten }
785bc093719SEd Schouten 
786bc093719SEd Schouten static void
ttydisc_reprint(struct tty * tp)787bc093719SEd Schouten ttydisc_reprint(struct tty *tp)
788bc093719SEd Schouten {
789bc093719SEd Schouten 	cc_t c;
790bc093719SEd Schouten 
791bc093719SEd Schouten 	/* Print  ^R\n, followed by the line. */
792bc093719SEd Schouten 	c = tp->t_termios.c_cc[VREPRINT];
793bc093719SEd Schouten 	if (c != _POSIX_VDISABLE)
794bc093719SEd Schouten 		ttydisc_echo(tp, c, 0);
795bc093719SEd Schouten 	ttydisc_echo(tp, CNL, 0);
796bc093719SEd Schouten 	ttyinq_reprintpos_reset(&tp->t_inq);
797bc093719SEd Schouten 
798bc093719SEd Schouten 	ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
799bc093719SEd Schouten }
800bc093719SEd Schouten 
801bc093719SEd Schouten struct ttydisc_recalc_length {
802bc093719SEd Schouten 	struct tty *tp;
803bc093719SEd Schouten 	unsigned int curlen;
804bc093719SEd Schouten };
805bc093719SEd Schouten 
806bc093719SEd Schouten static void
ttydisc_recalc_charlength(void * d,char c,int quote)807bc093719SEd Schouten ttydisc_recalc_charlength(void *d, char c, int quote)
808bc093719SEd Schouten {
809bc093719SEd Schouten 	struct ttydisc_recalc_length *data = d;
810bc093719SEd Schouten 	struct tty *tp = data->tp;
811bc093719SEd Schouten 
812bc093719SEd Schouten 	if (CTL_PRINT(c, quote)) {
813bc093719SEd Schouten 		if (CMP_FLAG(l, ECHOCTL))
814bc093719SEd Schouten 			data->curlen += 2;
815bc093719SEd Schouten 	} else if (c == CTAB) {
816bc093719SEd Schouten 		data->curlen += 8 - (data->curlen & 7);
817bc093719SEd Schouten 	} else {
818bc093719SEd Schouten 		data->curlen++;
819bc093719SEd Schouten 	}
820bc093719SEd Schouten }
821bc093719SEd Schouten 
822bc093719SEd Schouten static unsigned int
ttydisc_recalc_linelength(struct tty * tp)823bc093719SEd Schouten ttydisc_recalc_linelength(struct tty *tp)
824bc093719SEd Schouten {
825bc093719SEd Schouten 	struct ttydisc_recalc_length data = { tp, tp->t_writepos };
826bc093719SEd Schouten 
827bc093719SEd Schouten 	ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
828bc093719SEd Schouten 	    ttydisc_recalc_charlength, &data);
829bc093719SEd Schouten 	return (data.curlen);
830bc093719SEd Schouten }
831bc093719SEd Schouten 
832bc093719SEd Schouten static int
ttydisc_rubchar(struct tty * tp)833bc093719SEd Schouten ttydisc_rubchar(struct tty *tp)
834bc093719SEd Schouten {
835bc093719SEd Schouten 	char c;
836bc093719SEd Schouten 	int quote;
837bc093719SEd Schouten 	unsigned int prevpos, tablen;
838bc093719SEd Schouten 
839bc093719SEd Schouten 	if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
840bc093719SEd Schouten 		return (-1);
841bc093719SEd Schouten 	ttyinq_unputchar(&tp->t_inq);
842bc093719SEd Schouten 
843bc093719SEd Schouten 	if (CMP_FLAG(l, ECHO)) {
844bc093719SEd Schouten 		/*
845bc093719SEd Schouten 		 * Remove the character from the screen. This is even
846bc093719SEd Schouten 		 * safe for characters that span multiple characters
847bc093719SEd Schouten 		 * (tabs, quoted, etc).
848bc093719SEd Schouten 		 */
849bc093719SEd Schouten 		if (tp->t_writepos >= tp->t_column) {
850bc093719SEd Schouten 			/* Retype the sentence. */
851bc093719SEd Schouten 			ttydisc_reprint(tp);
852bc093719SEd Schouten 		} else if (CMP_FLAG(l, ECHOE)) {
853bc093719SEd Schouten 			if (CTL_PRINT(c, quote)) {
854bc093719SEd Schouten 				/* Remove ^X formatted chars. */
855bc093719SEd Schouten 				if (CMP_FLAG(l, ECHOCTL)) {
856bc093719SEd Schouten 					tp->t_column -= 2;
857bc093719SEd Schouten 					ttyoutq_write_nofrag(&tp->t_outq,
858bc093719SEd Schouten 					    "\b\b  \b\b", 6);
859bc093719SEd Schouten 				}
860bc093719SEd Schouten 			} else if (c == ' ') {
861bc093719SEd Schouten 				/* Space character needs no rubbing. */
862bc093719SEd Schouten 				tp->t_column -= 1;
863bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
864bc093719SEd Schouten 			} else if (c == CTAB) {
865bc093719SEd Schouten 				/*
866bc093719SEd Schouten 				 * Making backspace work with tabs is
867bc093719SEd Schouten 				 * quite hard. Recalculate the length of
868bc093719SEd Schouten 				 * this character and remove it.
869bc093719SEd Schouten 				 *
870bc093719SEd Schouten 				 * Because terminal settings could be
871bc093719SEd Schouten 				 * changed while the line is being
872bc093719SEd Schouten 				 * inserted, the calculations don't have
873bc093719SEd Schouten 				 * to be correct. Make sure we keep the
874bc093719SEd Schouten 				 * tab length within proper bounds.
875bc093719SEd Schouten 				 */
876bc093719SEd Schouten 				prevpos = ttydisc_recalc_linelength(tp);
877bc093719SEd Schouten 				if (prevpos >= tp->t_column)
878bc093719SEd Schouten 					tablen = 1;
879bc093719SEd Schouten 				else
880bc093719SEd Schouten 					tablen = tp->t_column - prevpos;
881bc093719SEd Schouten 				if (tablen > 8)
882bc093719SEd Schouten 					tablen = 8;
883bc093719SEd Schouten 
884bc093719SEd Schouten 				tp->t_column = prevpos;
885bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq,
886bc093719SEd Schouten 				    "\b\b\b\b\b\b\b\b", tablen);
887bc093719SEd Schouten 				return (0);
8889e589b09SBojan Novković 			} else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
8899e589b09SBojan Novković 			    CTL_UTF8(c)) {
8909e589b09SBojan Novković 				uint8_t bytes[UTF8_STACKBUF] = { 0 };
8919e589b09SBojan Novković 				int curidx = UTF8_STACKBUF - 1, cwidth = 1,
8929e589b09SBojan Novković 				    nb = 0;
8939e589b09SBojan Novković 				teken_char_t codepoint;
8949e589b09SBojan Novković 
8959e589b09SBojan Novković 				/* Save current byte. */
8969e589b09SBojan Novković 				bytes[curidx] = c;
8979e589b09SBojan Novković 				curidx--;
8989e589b09SBojan Novković 				nb++;
8999e589b09SBojan Novković 				/* Loop back through inq until we hit the
9009e589b09SBojan Novković 				 * leading byte. */
9019e589b09SBojan Novković 				while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
902c6d7be21SBojan Novković 					/*
903c6d7be21SBojan Novković 					 * Check if we've reached the beginning
904c6d7be21SBojan Novković 					 * of the line.
905c6d7be21SBojan Novković 					 */
906c6d7be21SBojan Novković 					if (ttyinq_peekchar(&tp->t_inq, &c,
907c6d7be21SBojan Novković 					    &quote) != 0)
908c6d7be21SBojan Novković 						break;
9099e589b09SBojan Novković 					ttyinq_unputchar(&tp->t_inq);
9109e589b09SBojan Novković 					bytes[curidx] = c;
9119e589b09SBojan Novković 					curidx--;
9129e589b09SBojan Novković 					nb++;
9139e589b09SBojan Novković 				}
9149e589b09SBojan Novković 				/*
9159e589b09SBojan Novković 				 * Shift array so that the leading
9169e589b09SBojan Novković 				 * byte ends up at idx 0.
9179e589b09SBojan Novković 				 */
9189e589b09SBojan Novković 				if (nb < UTF8_STACKBUF)
9199e589b09SBojan Novković 					memmove(&bytes[0], &bytes[curidx + 1],
9209e589b09SBojan Novković 					    nb * sizeof(uint8_t));
9219e589b09SBojan Novković 				/* Check for malformed UTF8 characters. */
9229e589b09SBojan Novković 				if (nb == UTF8_STACKBUF &&
9239e589b09SBojan Novković 				    CTL_UTF8_CONT(bytes[0])) {
9249e589b09SBojan Novković 					/*
9259e589b09SBojan Novković 					 * Place all bytes back into the inq and
9269e589b09SBojan Novković 					 * delete the last byte only.
9279e589b09SBojan Novković 					 */
9289e589b09SBojan Novković 					ttyinq_write(&tp->t_inq, bytes,
9299e589b09SBojan Novković 					    UTF8_STACKBUF, 0);
9302fed1c57SBojan Novković 					ttyinq_unputchar(&tp->t_inq);
9319e589b09SBojan Novković 				} else {
9329e589b09SBojan Novković 					/* Find codepoint and width. */
9339e589b09SBojan Novković 					codepoint =
9349e589b09SBojan Novković 					    teken_utf8_bytes_to_codepoint(bytes,
9359e589b09SBojan Novković 						nb);
9362fed1c57SBojan Novković 					if (codepoint ==
9372fed1c57SBojan Novković 						TEKEN_UTF8_INVALID_CODEPOINT ||
9382fed1c57SBojan Novković 					    (cwidth = teken_wcwidth(
9392fed1c57SBojan Novković 						 codepoint)) == -1) {
9409e589b09SBojan Novković 						/*
9419e589b09SBojan Novković 						 * Place all bytes back into the
9429e589b09SBojan Novković 						 * inq and fall back to
9439e589b09SBojan Novković 						 * default behaviour.
9449e589b09SBojan Novković 						 */
9452fed1c57SBojan Novković 						cwidth = 1;
9469e589b09SBojan Novković 						ttyinq_write(&tp->t_inq, bytes,
9479e589b09SBojan Novković 						    nb, 0);
9482fed1c57SBojan Novković 						ttyinq_unputchar(&tp->t_inq);
9499e589b09SBojan Novković 					}
9509e589b09SBojan Novković 				}
9519e589b09SBojan Novković 				tp->t_column -= cwidth;
9529e589b09SBojan Novković 				/*
9539e589b09SBojan Novković 				 * Delete character by punching
9549e589b09SBojan Novković 				 * 'cwidth' spaces over it.
9559e589b09SBojan Novković 				 */
9569e589b09SBojan Novković 				if (cwidth == 1)
9579e589b09SBojan Novković 					ttyoutq_write_nofrag(&tp->t_outq,
9589e589b09SBojan Novković 					    "\b \b", 3);
9599e589b09SBojan Novković 				else if (cwidth == 2)
9609e589b09SBojan Novković 					ttyoutq_write_nofrag(&tp->t_outq,
9619e589b09SBojan Novković 					    "\b\b  \b\b", 6);
962bc093719SEd Schouten 			} else {
963bc093719SEd Schouten 				/*
964bc093719SEd Schouten 				 * Remove a regular character by
965bc093719SEd Schouten 				 * punching a space over it.
966bc093719SEd Schouten 				 */
967bc093719SEd Schouten 				tp->t_column -= 1;
968bc093719SEd Schouten 				ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
969bc093719SEd Schouten 			}
970bc093719SEd Schouten 		} else {
971bc093719SEd Schouten 			/* Don't print spaces. */
972bc093719SEd Schouten 			ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
973bc093719SEd Schouten 		}
974bc093719SEd Schouten 	}
975bc093719SEd Schouten 
976bc093719SEd Schouten 	return (0);
977bc093719SEd Schouten }
978bc093719SEd Schouten 
979bc093719SEd Schouten static void
ttydisc_rubword(struct tty * tp)980bc093719SEd Schouten ttydisc_rubword(struct tty *tp)
981bc093719SEd Schouten {
982bc093719SEd Schouten 	char c;
983bc093719SEd Schouten 	int quote, alnum;
984bc093719SEd Schouten 
985bc093719SEd Schouten 	/* Strip whitespace first. */
986bc093719SEd Schouten 	for (;;) {
987bc093719SEd Schouten 		if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
988bc093719SEd Schouten 			return;
989bc093719SEd Schouten 		if (!CTL_WHITE(c))
990bc093719SEd Schouten 			break;
991bc093719SEd Schouten 		ttydisc_rubchar(tp);
992bc093719SEd Schouten 	}
993bc093719SEd Schouten 
994bc093719SEd Schouten 	/*
995bc093719SEd Schouten 	 * Record whether the last character from the previous iteration
996bc093719SEd Schouten 	 * was alphanumeric or not. We need this to implement ALTWERASE.
997bc093719SEd Schouten 	 */
998bc093719SEd Schouten 	alnum = CTL_ALNUM(c);
999bc093719SEd Schouten 	for (;;) {
1000bc093719SEd Schouten 		ttydisc_rubchar(tp);
1001bc093719SEd Schouten 
1002bc093719SEd Schouten 		if (ttyinq_peekchar(&tp->t_inq, &c, &quote) != 0)
1003bc093719SEd Schouten 			return;
1004bc093719SEd Schouten 		if (CTL_WHITE(c))
1005bc093719SEd Schouten 			return;
1006bc093719SEd Schouten 		if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
1007bc093719SEd Schouten 			return;
1008bc093719SEd Schouten 	}
1009bc093719SEd Schouten }
1010bc093719SEd Schouten 
1011bc093719SEd Schouten int
ttydisc_rint(struct tty * tp,char c,int flags)1012bc093719SEd Schouten ttydisc_rint(struct tty *tp, char c, int flags)
1013bc093719SEd Schouten {
1014bc093719SEd Schouten 	int signal, quote = 0;
1015bc093719SEd Schouten 	char ob[3] = { 0xff, 0x00 };
1016bc093719SEd Schouten 	size_t ol;
1017bc093719SEd Schouten 
101823d53268SKyle Evans 	tty_assert_locked(tp);
1019bc093719SEd Schouten 
1020bc093719SEd Schouten 	atomic_add_long(&tty_nin, 1);
1021bc093719SEd Schouten 
1022a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint))
1023a1215e37SEd Schouten 		return ttyhook_rint(tp, c, flags);
1024a1215e37SEd Schouten 
1025bc093719SEd Schouten 	if (tp->t_flags & TF_BYPASS)
1026bc093719SEd Schouten 		goto processed;
1027bc093719SEd Schouten 
1028bc093719SEd Schouten 	if (flags) {
1029bc093719SEd Schouten 		if (flags & TRE_BREAK) {
1030bc093719SEd Schouten 			if (CMP_FLAG(i, IGNBRK)) {
1031bc093719SEd Schouten 				/* Ignore break characters. */
1032bc093719SEd Schouten 				return (0);
1033bc093719SEd Schouten 			} else if (CMP_FLAG(i, BRKINT)) {
1034bc093719SEd Schouten 				/* Generate SIGINT on break. */
1035bc093719SEd Schouten 				tty_flush(tp, FREAD|FWRITE);
1036bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGINT);
1037bc093719SEd Schouten 				return (0);
1038bc093719SEd Schouten 			} else {
1039bc093719SEd Schouten 				/* Just print it. */
1040bc093719SEd Schouten 				goto parmrk;
1041bc093719SEd Schouten 			}
1042bc093719SEd Schouten 		} else if (flags & TRE_FRAMING ||
1043bc093719SEd Schouten 		    (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
1044bc093719SEd Schouten 			if (CMP_FLAG(i, IGNPAR)) {
1045bc093719SEd Schouten 				/* Ignore bad characters. */
1046bc093719SEd Schouten 				return (0);
1047bc093719SEd Schouten 			} else {
1048bc093719SEd Schouten 				/* Just print it. */
1049bc093719SEd Schouten 				goto parmrk;
1050bc093719SEd Schouten 			}
1051bc093719SEd Schouten 		}
1052bc093719SEd Schouten 	}
1053bc093719SEd Schouten 
1054bc093719SEd Schouten 	/* Allow any character to perform a wakeup. */
1055cbda6f66SWarner Losh 	if (CMP_FLAG(i, IXANY)) {
1056bc093719SEd Schouten 		tp->t_flags &= ~TF_STOPPED;
1057cbda6f66SWarner Losh 		tp->t_termios.c_lflag &= ~FLUSHO;
1058cbda6f66SWarner Losh 	}
1059bc093719SEd Schouten 
1060bc093719SEd Schouten 	/* Remove the top bit. */
1061bc093719SEd Schouten 	if (CMP_FLAG(i, ISTRIP))
1062bc093719SEd Schouten 		c &= ~0x80;
1063bc093719SEd Schouten 
1064bc093719SEd Schouten 	/* Skip input processing when we want to print it literally. */
1065bc093719SEd Schouten 	if (tp->t_flags & TF_LITERAL) {
1066bc093719SEd Schouten 		tp->t_flags &= ~TF_LITERAL;
1067bc093719SEd Schouten 		quote = 1;
1068bc093719SEd Schouten 		goto processed;
1069bc093719SEd Schouten 	}
1070bc093719SEd Schouten 
1071bc093719SEd Schouten 	/* Special control characters that are implementation dependent. */
1072bc093719SEd Schouten 	if (CMP_FLAG(l, IEXTEN)) {
1073bc093719SEd Schouten 		/* Accept the next character as literal. */
1074bc093719SEd Schouten 		if (CMP_CC(VLNEXT, c)) {
1075bc093719SEd Schouten 			if (CMP_FLAG(l, ECHO)) {
1076bc093719SEd Schouten 				if (CMP_FLAG(l, ECHOE))
1077bc093719SEd Schouten 					ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
1078bc093719SEd Schouten 				else
1079bc093719SEd Schouten 					ttydisc_echo(tp, c, 0);
1080bc093719SEd Schouten 			}
1081bc093719SEd Schouten 			tp->t_flags |= TF_LITERAL;
1082bc093719SEd Schouten 			return (0);
1083bc093719SEd Schouten 		}
1084cbda6f66SWarner Losh 		/* Discard processing */
1085cbda6f66SWarner Losh 		if (CMP_CC(VDISCARD, c)) {
1086cbda6f66SWarner Losh 			if (CMP_FLAG(l, FLUSHO)) {
1087cbda6f66SWarner Losh 				tp->t_termios.c_lflag &= ~FLUSHO;
1088cbda6f66SWarner Losh 			} else {
1089cbda6f66SWarner Losh 				tty_flush(tp, FWRITE);
1090cbda6f66SWarner Losh 				ttydisc_echo(tp, c, 0);
1091cbda6f66SWarner Losh 				if (tp->t_inq.ti_end > 0)
1092cbda6f66SWarner Losh 					ttydisc_reprint(tp);
1093cbda6f66SWarner Losh 				tp->t_termios.c_lflag |= FLUSHO;
1094cbda6f66SWarner Losh 			}
1095cbda6f66SWarner Losh 		}
1096bc093719SEd Schouten 	}
1097bc093719SEd Schouten 
1098bc093719SEd Schouten 	/*
1099bc093719SEd Schouten 	 * Handle signal processing.
1100bc093719SEd Schouten 	 */
1101bc093719SEd Schouten 	if (CMP_FLAG(l, ISIG)) {
1102bc093719SEd Schouten 		if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
1103bc093719SEd Schouten 			if (CMP_CC(VSTATUS, c)) {
1104bc093719SEd Schouten 				tty_signal_pgrp(tp, SIGINFO);
1105bc093719SEd Schouten 				return (0);
1106bc093719SEd Schouten 			}
1107bc093719SEd Schouten 		}
1108bc093719SEd Schouten 
1109bc093719SEd Schouten 		/*
1110bc093719SEd Schouten 		 * When compared to the old implementation, this
1111bc093719SEd Schouten 		 * implementation also flushes the output queue. POSIX
1112bc093719SEd Schouten 		 * is really brief about this, but does makes us assume
1113bc093719SEd Schouten 		 * we have to do so.
1114bc093719SEd Schouten 		 */
1115bc093719SEd Schouten 		signal = 0;
1116bc093719SEd Schouten 		if (CMP_CC(VINTR, c)) {
1117bc093719SEd Schouten 			signal = SIGINT;
1118bc093719SEd Schouten 		} else if (CMP_CC(VQUIT, c)) {
1119bc093719SEd Schouten 			signal = SIGQUIT;
1120bc093719SEd Schouten 		} else if (CMP_CC(VSUSP, c)) {
1121bc093719SEd Schouten 			signal = SIGTSTP;
1122bc093719SEd Schouten 		}
1123bc093719SEd Schouten 
1124bc093719SEd Schouten 		if (signal != 0) {
1125bc093719SEd Schouten 			/*
1126bc093719SEd Schouten 			 * Echo the character before signalling the
1127bc093719SEd Schouten 			 * processes.
1128bc093719SEd Schouten 			 */
1129bc093719SEd Schouten 			if (!CMP_FLAG(l, NOFLSH))
1130bc093719SEd Schouten 				tty_flush(tp, FREAD|FWRITE);
1131bc093719SEd Schouten 			ttydisc_echo(tp, c, 0);
1132bc093719SEd Schouten 			tty_signal_pgrp(tp, signal);
1133bc093719SEd Schouten 			return (0);
1134bc093719SEd Schouten 		}
1135bc093719SEd Schouten 	}
1136bc093719SEd Schouten 
1137bc093719SEd Schouten 	/*
1138bc093719SEd Schouten 	 * Handle start/stop characters.
1139bc093719SEd Schouten 	 */
1140bc093719SEd Schouten 	if (CMP_FLAG(i, IXON)) {
1141bc093719SEd Schouten 		if (CMP_CC(VSTOP, c)) {
1142bc093719SEd Schouten 			/* Stop it if we aren't stopped yet. */
1143bc093719SEd Schouten 			if ((tp->t_flags & TF_STOPPED) == 0) {
1144bc093719SEd Schouten 				tp->t_flags |= TF_STOPPED;
1145bc093719SEd Schouten 				return (0);
1146bc093719SEd Schouten 			}
1147bc093719SEd Schouten 			/*
1148bc093719SEd Schouten 			 * Fallthrough:
1149bc093719SEd Schouten 			 * When VSTART == VSTOP, we should make this key
1150bc093719SEd Schouten 			 * toggle it.
1151bc093719SEd Schouten 			 */
1152bc093719SEd Schouten 			if (!CMP_CC(VSTART, c))
1153bc093719SEd Schouten 				return (0);
1154bc093719SEd Schouten 		}
1155bc093719SEd Schouten 		if (CMP_CC(VSTART, c)) {
1156bc093719SEd Schouten 			tp->t_flags &= ~TF_STOPPED;
1157bc093719SEd Schouten 			return (0);
1158bc093719SEd Schouten 		}
1159bc093719SEd Schouten 	}
1160bc093719SEd Schouten 
1161bc093719SEd Schouten 	/* Conversion of CR and NL. */
1162bc093719SEd Schouten 	switch (c) {
1163bc093719SEd Schouten 	case CCR:
1164bc093719SEd Schouten 		if (CMP_FLAG(i, IGNCR))
1165bc093719SEd Schouten 			return (0);
1166bc093719SEd Schouten 		if (CMP_FLAG(i, ICRNL))
1167bc093719SEd Schouten 			c = CNL;
1168bc093719SEd Schouten 		break;
1169bc093719SEd Schouten 	case CNL:
1170bc093719SEd Schouten 		if (CMP_FLAG(i, INLCR))
1171bc093719SEd Schouten 			c = CCR;
1172bc093719SEd Schouten 		break;
1173bc093719SEd Schouten 	}
1174bc093719SEd Schouten 
1175bc093719SEd Schouten 	/* Canonical line editing. */
1176bc093719SEd Schouten 	if (CMP_FLAG(l, ICANON)) {
1177bc093719SEd Schouten 		if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1178bc093719SEd Schouten 			ttydisc_rubchar(tp);
1179bc093719SEd Schouten 			return (0);
1180bc093719SEd Schouten 		} else if (CMP_CC(VKILL, c)) {
1181bc093719SEd Schouten 			while (ttydisc_rubchar(tp) == 0);
1182bc093719SEd Schouten 			return (0);
1183bc093719SEd Schouten 		} else if (CMP_FLAG(l, IEXTEN)) {
1184bc093719SEd Schouten 			if (CMP_CC(VWERASE, c)) {
1185bc093719SEd Schouten 				ttydisc_rubword(tp);
1186bc093719SEd Schouten 				return (0);
1187bc093719SEd Schouten 			} else if (CMP_CC(VREPRINT, c)) {
1188bc093719SEd Schouten 				ttydisc_reprint(tp);
1189bc093719SEd Schouten 				return (0);
1190bc093719SEd Schouten 			}
1191bc093719SEd Schouten 		}
1192bc093719SEd Schouten 	}
1193bc093719SEd Schouten 
1194bc093719SEd Schouten processed:
1195bc093719SEd Schouten 	if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1196bc093719SEd Schouten 		/* Print 0xff 0xff. */
1197bc093719SEd Schouten 		ob[1] = 0xff;
1198bc093719SEd Schouten 		ol = 2;
1199bc093719SEd Schouten 		quote = 1;
1200bc093719SEd Schouten 	} else {
1201bc093719SEd Schouten 		ob[0] = c;
1202bc093719SEd Schouten 		ol = 1;
1203bc093719SEd Schouten 	}
1204bc093719SEd Schouten 
1205bc093719SEd Schouten 	goto print;
1206bc093719SEd Schouten 
1207bc093719SEd Schouten parmrk:
1208bc093719SEd Schouten 	if (CMP_FLAG(i, PARMRK)) {
1209bc093719SEd Schouten 		/* Prepend 0xff 0x00 0x.. */
1210bc093719SEd Schouten 		ob[2] = c;
1211bc093719SEd Schouten 		ol = 3;
1212bc093719SEd Schouten 		quote = 1;
1213bc093719SEd Schouten 	} else {
1214bc093719SEd Schouten 		ob[0] = c;
1215bc093719SEd Schouten 		ol = 1;
1216bc093719SEd Schouten 	}
1217bc093719SEd Schouten 
1218bc093719SEd Schouten print:
1219bc093719SEd Schouten 	/* See if we can store this on the input queue. */
1220bc093719SEd Schouten 	if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1221a15ec0a5SEd Schouten 		if (CMP_FLAG(i, IMAXBEL))
1222a15ec0a5SEd Schouten 			ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1223a15ec0a5SEd Schouten 
1224a15ec0a5SEd Schouten 		/*
1225a15ec0a5SEd Schouten 		 * Prevent a deadlock here. It may be possible that a
1226a15ec0a5SEd Schouten 		 * user has entered so much data, there is no data
1227a15ec0a5SEd Schouten 		 * available to read(), but the buffers are full anyway.
1228a15ec0a5SEd Schouten 		 *
1229a15ec0a5SEd Schouten 		 * Only enter the high watermark if the device driver
1230a15ec0a5SEd Schouten 		 * can actually transmit something.
1231a15ec0a5SEd Schouten 		 */
1232a15ec0a5SEd Schouten 		if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1233a15ec0a5SEd Schouten 			return (0);
1234a15ec0a5SEd Schouten 
1235bc093719SEd Schouten 		tty_hiwat_in_block(tp);
1236bc093719SEd Schouten 		return (-1);
1237bc093719SEd Schouten 	}
1238bc093719SEd Schouten 
1239bc093719SEd Schouten 	/*
1240bc093719SEd Schouten 	 * In raw mode, we canonicalize after receiving a single
1241bc093719SEd Schouten 	 * character. Otherwise, we canonicalize when we receive a
1242bc093719SEd Schouten 	 * newline, VEOL or VEOF, but only when it isn't quoted.
1243bc093719SEd Schouten 	 */
1244bc093719SEd Schouten 	if (!CMP_FLAG(l, ICANON) ||
1245bc093719SEd Schouten 	    (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1246bc093719SEd Schouten 		ttyinq_canonicalize(&tp->t_inq);
1247bc093719SEd Schouten 	}
1248bc093719SEd Schouten 
1249bc093719SEd Schouten 	ttydisc_echo(tp, c, quote);
1250bc093719SEd Schouten 
1251bc093719SEd Schouten 	return (0);
1252bc093719SEd Schouten }
1253bc093719SEd Schouten 
1254bc093719SEd Schouten size_t
ttydisc_rint_simple(struct tty * tp,const void * buf,size_t len)12555c67885aSEd Schouten ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
12565c67885aSEd Schouten {
12575c67885aSEd Schouten 	const char *cbuf;
12585c67885aSEd Schouten 
12595c67885aSEd Schouten 	if (ttydisc_can_bypass(tp))
12605c67885aSEd Schouten 		return (ttydisc_rint_bypass(tp, buf, len));
12615c67885aSEd Schouten 
12625c67885aSEd Schouten 	for (cbuf = buf; len-- > 0; cbuf++) {
12635c67885aSEd Schouten 		if (ttydisc_rint(tp, *cbuf, 0) != 0)
12645c67885aSEd Schouten 			break;
12655c67885aSEd Schouten 	}
12665c67885aSEd Schouten 
12675c67885aSEd Schouten 	return (cbuf - (const char *)buf);
12685c67885aSEd Schouten }
12695c67885aSEd Schouten 
12705c67885aSEd Schouten size_t
ttydisc_rint_bypass(struct tty * tp,const void * buf,size_t len)1271d344ffe5SEd Schouten ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1272bc093719SEd Schouten {
1273bc093719SEd Schouten 	size_t ret;
1274bc093719SEd Schouten 
127523d53268SKyle Evans 	tty_assert_locked(tp);
1276bc093719SEd Schouten 
1277bc093719SEd Schouten 	MPASS(tp->t_flags & TF_BYPASS);
1278bc093719SEd Schouten 
1279bc093719SEd Schouten 	atomic_add_long(&tty_nin, len);
1280bc093719SEd Schouten 
1281a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_bypass))
1282a1215e37SEd Schouten 		return ttyhook_rint_bypass(tp, buf, len);
1283a1215e37SEd Schouten 
1284bc093719SEd Schouten 	ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1285bc093719SEd Schouten 	ttyinq_canonicalize(&tp->t_inq);
1286d40b91cbSEd Schouten 	if (ret < len)
1287d40b91cbSEd Schouten 		tty_hiwat_in_block(tp);
1288bc093719SEd Schouten 
1289bc093719SEd Schouten 	return (ret);
1290bc093719SEd Schouten }
1291bc093719SEd Schouten 
1292bc093719SEd Schouten void
ttydisc_rint_done(struct tty * tp)1293bc093719SEd Schouten ttydisc_rint_done(struct tty *tp)
1294bc093719SEd Schouten {
1295bc093719SEd Schouten 
129623d53268SKyle Evans 	tty_assert_locked(tp);
1297bc093719SEd Schouten 
1298a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_done))
1299a1215e37SEd Schouten 		ttyhook_rint_done(tp);
1300a1215e37SEd Schouten 
1301bc093719SEd Schouten 	/* Wake up readers. */
1302bc093719SEd Schouten 	tty_wakeup(tp, FREAD);
1303bc093719SEd Schouten 	/* Wake up driver for echo. */
1304bc093719SEd Schouten 	ttydevsw_outwakeup(tp);
1305bc093719SEd Schouten }
1306bc093719SEd Schouten 
1307a1215e37SEd Schouten size_t
ttydisc_rint_poll(struct tty * tp)1308a1215e37SEd Schouten ttydisc_rint_poll(struct tty *tp)
1309a1215e37SEd Schouten {
1310a1215e37SEd Schouten 	size_t l;
1311a1215e37SEd Schouten 
131223d53268SKyle Evans 	tty_assert_locked(tp);
1313a1215e37SEd Schouten 
1314a1215e37SEd Schouten 	if (ttyhook_hashook(tp, rint_poll))
1315a1215e37SEd Schouten 		return ttyhook_rint_poll(tp);
1316a1215e37SEd Schouten 
1317a1215e37SEd Schouten 	/*
1318a1215e37SEd Schouten 	 * XXX: Still allow character input when there's no space in the
1319a1215e37SEd Schouten 	 * buffers, but we haven't entered the high watermark. This is
1320a1215e37SEd Schouten 	 * to allow backspace characters to be inserted when in
1321a1215e37SEd Schouten 	 * canonical mode.
1322a1215e37SEd Schouten 	 */
1323a1215e37SEd Schouten 	l = ttyinq_bytesleft(&tp->t_inq);
1324a1215e37SEd Schouten 	if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1325a1215e37SEd Schouten 		return (1);
1326a1215e37SEd Schouten 
1327a1215e37SEd Schouten 	return (l);
1328a1215e37SEd Schouten }
1329a1215e37SEd Schouten 
1330bc093719SEd Schouten static void
ttydisc_wakeup_watermark(struct tty * tp)1331bc093719SEd Schouten ttydisc_wakeup_watermark(struct tty *tp)
1332bc093719SEd Schouten {
1333bc093719SEd Schouten 	size_t c;
1334bc093719SEd Schouten 
1335bc093719SEd Schouten 	c = ttyoutq_bytesleft(&tp->t_outq);
1336bc093719SEd Schouten 	if (tp->t_flags & TF_HIWAT_OUT) {
1337bc093719SEd Schouten 		/* Only allow us to run when we're below the watermark. */
1338bc093719SEd Schouten 		if (c < tp->t_outlow)
1339bc093719SEd Schouten 			return;
1340bc093719SEd Schouten 
1341bc093719SEd Schouten 		/* Reset the watermark. */
1342bc093719SEd Schouten 		tp->t_flags &= ~TF_HIWAT_OUT;
1343bc093719SEd Schouten 	} else {
1344bc093719SEd Schouten 		/* Only run when we have data at all. */
1345bc093719SEd Schouten 		if (c == 0)
1346bc093719SEd Schouten 			return;
1347bc093719SEd Schouten 	}
1348bc093719SEd Schouten 	tty_wakeup(tp, FWRITE);
1349bc093719SEd Schouten }
1350bc093719SEd Schouten 
1351bc093719SEd Schouten size_t
ttydisc_getc(struct tty * tp,void * buf,size_t len)1352bc093719SEd Schouten ttydisc_getc(struct tty *tp, void *buf, size_t len)
1353bc093719SEd Schouten {
1354bc093719SEd Schouten 
135523d53268SKyle Evans 	tty_assert_locked(tp);
1356bc093719SEd Schouten 
1357bc093719SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1358bc093719SEd Schouten 		return (0);
1359bc093719SEd Schouten 
1360a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_inject))
1361a1215e37SEd Schouten 		return ttyhook_getc_inject(tp, buf, len);
1362bc093719SEd Schouten 
1363a1215e37SEd Schouten 	len = ttyoutq_read(&tp->t_outq, buf, len);
1364a1215e37SEd Schouten 
1365a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_capture))
1366a1215e37SEd Schouten 		ttyhook_getc_capture(tp, buf, len);
1367a1215e37SEd Schouten 
1368a1215e37SEd Schouten 	ttydisc_wakeup_watermark(tp);
1369ffffa83bSEd Schouten 	atomic_add_long(&tty_nout, len);
1370bc093719SEd Schouten 
1371ffffa83bSEd Schouten 	return (len);
1372bc093719SEd Schouten }
1373bc093719SEd Schouten 
1374bc093719SEd Schouten int
ttydisc_getc_uio(struct tty * tp,struct uio * uio)1375bc093719SEd Schouten ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1376bc093719SEd Schouten {
1377a1215e37SEd Schouten 	int error = 0;
1378526d0bd5SKonstantin Belousov 	ssize_t obytes = uio->uio_resid;
1379a1215e37SEd Schouten 	size_t len;
1380a1215e37SEd Schouten 	char buf[TTY_STACKBUF];
1381bc093719SEd Schouten 
138223d53268SKyle Evans 	tty_assert_locked(tp);
1383bc093719SEd Schouten 
1384bc093719SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1385bc093719SEd Schouten 		return (0);
1386bc093719SEd Schouten 
1387a1215e37SEd Schouten 	/*
1388a1215e37SEd Schouten 	 * When a TTY hook is attached, we cannot perform unbuffered
1389a1215e37SEd Schouten 	 * copying to userspace. Just call ttydisc_getc() and
1390a1215e37SEd Schouten 	 * temporarily store data in a shadow buffer.
1391a1215e37SEd Schouten 	 */
1392a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_capture) ||
1393a1215e37SEd Schouten 	    ttyhook_hashook(tp, getc_inject)) {
1394a1215e37SEd Schouten 		while (uio->uio_resid > 0) {
1395a1215e37SEd Schouten 			/* Read to shadow buffer. */
1396a1215e37SEd Schouten 			len = ttydisc_getc(tp, buf,
1397a1215e37SEd Schouten 			    MIN(uio->uio_resid, sizeof buf));
1398a1215e37SEd Schouten 			if (len == 0)
1399a1215e37SEd Schouten 				break;
1400bc093719SEd Schouten 
1401a1215e37SEd Schouten 			/* Copy to userspace. */
1402a1215e37SEd Schouten 			tty_unlock(tp);
1403a1215e37SEd Schouten 			error = uiomove(buf, len, uio);
1404a1215e37SEd Schouten 			tty_lock(tp);
1405a1215e37SEd Schouten 
1406a1215e37SEd Schouten 			if (error != 0)
1407a1215e37SEd Schouten 				break;
1408a1215e37SEd Schouten 		}
1409a1215e37SEd Schouten 	} else {
1410a1215e37SEd Schouten 		error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1411a1215e37SEd Schouten 
1412a1215e37SEd Schouten 		ttydisc_wakeup_watermark(tp);
1413bc093719SEd Schouten 		atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1414a1215e37SEd Schouten 	}
1415bc093719SEd Schouten 
1416bc093719SEd Schouten 	return (error);
1417bc093719SEd Schouten }
1418bc093719SEd Schouten 
1419a1215e37SEd Schouten size_t
ttydisc_getc_poll(struct tty * tp)1420a1215e37SEd Schouten ttydisc_getc_poll(struct tty *tp)
1421a1215e37SEd Schouten {
1422a1215e37SEd Schouten 
142323d53268SKyle Evans 	tty_assert_locked(tp);
1424a1215e37SEd Schouten 
1425a1215e37SEd Schouten 	if (tp->t_flags & TF_STOPPED)
1426a1215e37SEd Schouten 		return (0);
1427a1215e37SEd Schouten 
1428a1215e37SEd Schouten 	if (ttyhook_hashook(tp, getc_poll))
1429a1215e37SEd Schouten 		return ttyhook_getc_poll(tp);
1430a1215e37SEd Schouten 
1431a1215e37SEd Schouten 	return ttyoutq_bytesused(&tp->t_outq);
1432a1215e37SEd Schouten }
1433a1215e37SEd Schouten 
1434bc093719SEd Schouten /*
1435bc093719SEd Schouten  * XXX: not really related to the TTYDISC, but we'd better put
1436bc093719SEd Schouten  * tty_putchar() here, because we need to perform proper output
1437bc093719SEd Schouten  * processing.
1438bc093719SEd Schouten  */
1439bc093719SEd Schouten 
1440bc093719SEd Schouten int
tty_putstrn(struct tty * tp,const char * p,size_t n)14416858c2ccSConrad Meyer tty_putstrn(struct tty *tp, const char *p, size_t n)
1442bc093719SEd Schouten {
14436858c2ccSConrad Meyer 	size_t i;
14446858c2ccSConrad Meyer 
144523d53268SKyle Evans 	tty_assert_locked(tp);
1446bc093719SEd Schouten 
1447bc093719SEd Schouten 	if (tty_gone(tp))
1448bc093719SEd Schouten 		return (-1);
1449bc093719SEd Schouten 
14506858c2ccSConrad Meyer 	for (i = 0; i < n; i++)
14516858c2ccSConrad Meyer 		ttydisc_echo_force(tp, p[i], 0);
14526858c2ccSConrad Meyer 
1453bc093719SEd Schouten 	tp->t_writepos = tp->t_column;
1454bc093719SEd Schouten 	ttyinq_reprintpos_set(&tp->t_inq);
1455bc093719SEd Schouten 
1456bc093719SEd Schouten 	ttydevsw_outwakeup(tp);
1457bc093719SEd Schouten 	return (0);
1458bc093719SEd Schouten }
14596858c2ccSConrad Meyer 
14606858c2ccSConrad Meyer int
tty_putchar(struct tty * tp,char c)14616858c2ccSConrad Meyer tty_putchar(struct tty *tp, char c)
14626858c2ccSConrad Meyer {
14636858c2ccSConrad Meyer 	return (tty_putstrn(tp, &c, 1));
14646858c2ccSConrad Meyer }
1465