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