2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
35 #include <sys/filio.h>
36 #include <sys/kernel.h>
37 #include <sys/signal.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
41 #include <sys/ttycom.h>
42 #include <sys/ttydefaults.h>
44 #include <sys/vnode.h>
47 * Standard TTYDISC `termios' line discipline.
51 static unsigned long tty_nin = 0;
52 SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
53 &tty_nin, 0, "Total amount of bytes received");
54 static unsigned long tty_nout = 0;
55 SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
56 &tty_nout, 0, "Total amount of bytes transmitted");
58 /* termios comparison macro's. */
59 #define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
60 tp->t_termios.c_cc[v] == (c))
61 #define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
63 /* Characters that cannot be modified through c_cc. */
68 /* Character is a control character. */
69 #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20)
70 /* Control character should be processed on echo. */
71 #define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \
72 (c) == CNL || (c) == CCR))
73 /* Control character should be printed using ^X notation. */
74 #define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
75 ((q) || ((c) != CTAB && (c) != CNL))))
76 /* Character is whitespace. */
77 #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB)
78 /* Character is alphanumeric. */
79 #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \
80 ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
82 #define TTY_STACKBUF 256
85 ttydisc_open(struct tty *tp)
91 ttydisc_close(struct tty *tp)
94 /* Clean up our flags when leaving the discipline. */
95 tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
97 /* POSIX states we should flush when close() is called. */
98 ttyinq_flush(&tp->t_inq);
99 ttyoutq_flush(&tp->t_outq);
102 ttydevsw_inwakeup(tp);
103 ttydevsw_outwakeup(tp);
106 if (ttyhook_hashook(tp, close))
111 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
113 char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
115 size_t clen, flen = 0, n = 1;
116 unsigned char lastc = _POSIX_VDISABLE;
118 #define BREAK_ADD(c) do { \
119 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
120 breakc[n++] = tp->t_termios.c_cc[c]; \
122 /* Determine which characters we should trigger on. */
130 * Quite a tricky case: unlike the old TTY
131 * implementation, this implementation copies data back
132 * to userspace in large chunks. Unfortunately, we can't
133 * calculate the line length on beforehand if it crosses
134 * ttyinq_block boundaries, because multiple reads could
135 * then make this code read beyond the newline.
137 * This is why we limit the read to:
138 * - The size the user has requested
139 * - The blocksize (done in tty_inq.c)
140 * - The amount of bytes until the newline
142 * This causes the line length to be recalculated after
143 * each block has been copied to userspace. This will
144 * cause the TTY layer to return data in chunks using
145 * the blocksize (except the first and last blocks).
147 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
152 if (ioflag & IO_NDELAY)
153 return (EWOULDBLOCK);
154 else if (tp->t_flags & TF_ZOMBIE)
157 error = tty_wait(tp, &tp->t_inwait);
163 /* Don't send the EOF char back to userspace. */
164 if (CMP_CC(VEOF, lastc))
169 /* Read and throw away the EOF character. */
170 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
174 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
180 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
182 size_t vmin = tp->t_termios.c_cc[VMIN];
183 int oresid = uio->uio_resid;
186 MPASS(tp->t_termios.c_cc[VTIME] == 0);
189 * This routine implements the easy cases of read()s while in
190 * non-canonical mode, namely case B and D, where we don't have
195 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
199 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
202 /* We have to wait for more. */
203 if (ioflag & IO_NDELAY)
204 return (EWOULDBLOCK);
205 else if (tp->t_flags & TF_ZOMBIE)
208 error = tty_wait(tp, &tp->t_inwait);
215 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
218 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
219 unsigned int vtime = tp->t_termios.c_cc[VTIME];
220 struct timeval end, now, left;
223 MPASS(tp->t_termios.c_cc[VTIME] != 0);
225 /* Determine when the read should be expired. */
226 end.tv_sec = vtime / 10;
227 end.tv_usec = (vtime % 10) * 100000;
229 timevaladd(&end, &now);
232 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
236 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
239 /* Calculate how long we should wait. */
241 if (timevalcmp(&now, &end, >))
244 timevalsub(&left, &now);
248 * We have to wait for more. If the timer expires, we
249 * should return a 0-byte read.
251 if (ioflag & IO_NDELAY)
252 return (EWOULDBLOCK);
253 else if (tp->t_flags & TF_ZOMBIE)
256 error = tty_timedwait(tp, &tp->t_inwait, hz);
258 return (error == EWOULDBLOCK ? 0 : error);
265 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
267 size_t vmin = tp->t_termios.c_cc[VMIN];
268 int oresid = uio->uio_resid;
271 MPASS(tp->t_termios.c_cc[VMIN] != 0);
272 MPASS(tp->t_termios.c_cc[VTIME] != 0);
275 * When using the interbyte timer, the timer should be started
276 * after the first byte has been received. We just call into the
277 * generic read timer code after we've received the first byte.
281 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
285 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
289 * Not enough data, but we did receive some, which means
290 * we'll now start using the interbyte timer.
292 if (oresid != uio->uio_resid)
295 /* We have to wait for more. */
296 if (ioflag & IO_NDELAY)
297 return (EWOULDBLOCK);
298 else if (tp->t_flags & TF_ZOMBIE)
301 error = tty_wait(tp, &tp->t_inwait);
306 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
310 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
314 tty_lock_assert(tp, MA_OWNED);
316 if (uio->uio_resid == 0)
319 if (CMP_FLAG(l, ICANON))
320 error = ttydisc_read_canonical(tp, uio, ioflag);
321 else if (tp->t_termios.c_cc[VTIME] == 0)
322 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
323 else if (tp->t_termios.c_cc[VMIN] == 0)
324 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
327 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
329 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
330 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
331 /* Unset the input watermark when we've got enough space. */
332 tty_hiwat_in_unblock(tp);
338 static __inline unsigned int
339 ttydisc_findchar(const char *obstart, unsigned int oblen)
341 const char *c = obstart;
349 return (c - obstart);
353 ttydisc_write_oproc(struct tty *tp, char c)
355 unsigned int scnt, error;
357 MPASS(CMP_FLAG(o, OPOST));
360 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
363 /* End-of-text dropping. */
364 if (CMP_FLAG(o, ONOEOT))
366 return PRINT_NORMAL();
369 /* Handle backspace to fix tab expansion. */
370 if (PRINT_NORMAL() != 0)
372 if (tp->t_column > 0)
378 scnt = 8 - (tp->t_column & 7);
379 if (CMP_FLAG(o, TAB3)) {
380 error = ttyoutq_write_nofrag(&tp->t_outq,
383 error = PRINT_NORMAL();
388 tp->t_column += scnt;
389 MPASS((tp->t_column % 8) == 0);
393 /* Newline conversion. */
394 if (CMP_FLAG(o, ONLCR)) {
395 /* Convert \n to \r\n. */
396 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
398 error = PRINT_NORMAL();
403 if (CMP_FLAG(o, ONLCR|ONLRET)) {
404 tp->t_column = tp->t_writepos = 0;
405 ttyinq_reprintpos_set(&tp->t_inq);
410 /* Carriage return to newline conversion. */
411 if (CMP_FLAG(o, OCRNL))
413 /* Omit carriage returns on column 0. */
414 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
416 if (PRINT_NORMAL() != 0)
419 tp->t_column = tp->t_writepos = 0;
420 ttyinq_reprintpos_set(&tp->t_inq);
425 * Invisible control character. Print it, but don't
426 * increase the column count.
428 return PRINT_NORMAL();
433 * Just like the old TTY implementation, we need to copy data in chunks
434 * into a temporary buffer. One of the reasons why we need to do this,
435 * is because output processing (only TAB3 though) may allow the buffer
436 * to grow eight times.
439 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
441 char ob[TTY_STACKBUF];
444 unsigned int oblen = 0;
446 tty_lock_assert(tp, MA_OWNED);
448 if (tp->t_flags & TF_ZOMBIE)
452 * We don't need to check whether the process is the foreground
453 * process group or if we have a carrier. This is already done
457 while (uio->uio_resid > 0) {
462 /* Step 1: read data. */
464 nlen = MIN(uio->uio_resid, sizeof ob);
466 error = uiomove(ob, nlen, uio);
479 /* Step 2: process data. */
481 unsigned int plen, wlen;
483 /* Search for special characters for post processing. */
484 if (CMP_FLAG(o, OPOST)) {
485 plen = ttydisc_findchar(obstart, oblen);
492 * We're going to process a character
493 * that needs processing
495 if (ttydisc_write_oproc(tp, *obstart) == 0) {
499 tp->t_writepos = tp->t_column;
500 ttyinq_reprintpos_set(&tp->t_inq);
504 /* We're going to write regular data. */
505 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
508 tp->t_column += wlen;
510 tp->t_writepos = tp->t_column;
511 ttyinq_reprintpos_set(&tp->t_inq);
517 /* Watermark reached. Try to sleep. */
518 tp->t_flags |= TF_HIWAT_OUT;
520 if (ioflag & IO_NDELAY) {
526 * The driver may write back the data
527 * synchronously. Be sure to check the high
528 * water mark before going to sleep.
530 ttydevsw_outwakeup(tp);
531 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
534 error = tty_wait(tp, &tp->t_outwait);
538 if (tp->t_flags & TF_ZOMBIE) {
547 ttydevsw_outwakeup(tp);
550 * Add the amount of bytes that we didn't process back to the
551 * uio counters. We need to do this to make sure write() doesn't
552 * count the bytes we didn't store in the queue.
554 uio->uio_resid += oblen;
559 ttydisc_optimize(struct tty *tp)
561 tty_lock_assert(tp, MA_OWNED);
563 if (ttyhook_hashook(tp, rint_bypass)) {
564 tp->t_flags |= TF_BYPASS;
565 } else if (ttyhook_hashook(tp, rint)) {
566 tp->t_flags &= ~TF_BYPASS;
567 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
568 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
569 (!CMP_FLAG(i, PARMRK) ||
570 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
571 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
572 tp->t_flags |= TF_BYPASS;
574 tp->t_flags &= ~TF_BYPASS;
579 ttydisc_modem(struct tty *tp, int open)
582 tty_lock_assert(tp, MA_OWNED);
585 cv_broadcast(&tp->t_dcdwait);
588 * Ignore modem status lines when CLOCAL is turned on, but don't
589 * enter the zombie state when the TTY isn't opened, because
590 * that would cause the TTY to be in zombie state after being
593 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
600 tp->t_flags |= TF_ZOMBIE;
602 tty_signal_sessleader(tp, SIGHUP);
603 tty_flush(tp, FREAD|FWRITE);
606 * Carrier is back again.
609 /* XXX: what should we do here? */
614 ttydisc_echo_force(struct tty *tp, char c, int quote)
617 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
619 * Only perform postprocessing when OPOST is turned on
620 * and the character is an unquoted BS/TB/NL/CR.
622 return ttydisc_write_oproc(tp, c);
623 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
625 * Only use ^X notation when ECHOCTL is turned on and
626 * we've got an quoted control character.
628 * Print backspaces when echoing an end-of-file.
630 char ob[4] = "^?\b\b";
632 /* Print ^X notation. */
636 if (!quote && CMP_CC(VEOF, c)) {
637 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
640 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
643 /* Can just be printed. */
645 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
650 ttydisc_echo(struct tty *tp, char c, int quote)
654 * Only echo characters when ECHO is turned on, or ECHONL when
655 * the character is an unquoted newline.
657 if (!CMP_FLAG(l, ECHO) &&
658 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
661 return ttydisc_echo_force(tp, c, quote);
666 ttydisc_reprint_char(void *d, char c, int quote)
670 ttydisc_echo(tp, c, quote);
674 ttydisc_reprint(struct tty *tp)
678 /* Print ^R\n, followed by the line. */
679 c = tp->t_termios.c_cc[VREPRINT];
680 if (c != _POSIX_VDISABLE)
681 ttydisc_echo(tp, c, 0);
682 ttydisc_echo(tp, CNL, 0);
683 ttyinq_reprintpos_reset(&tp->t_inq);
685 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
688 struct ttydisc_recalc_length {
694 ttydisc_recalc_charlength(void *d, char c, int quote)
696 struct ttydisc_recalc_length *data = d;
697 struct tty *tp = data->tp;
699 if (CTL_PRINT(c, quote)) {
700 if (CMP_FLAG(l, ECHOCTL))
702 } else if (c == CTAB) {
703 data->curlen += 8 - (data->curlen & 7);
710 ttydisc_recalc_linelength(struct tty *tp)
712 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
714 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
715 ttydisc_recalc_charlength, &data);
716 return (data.curlen);
720 ttydisc_rubchar(struct tty *tp)
724 unsigned int prevpos, tablen;
726 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
728 ttyinq_unputchar(&tp->t_inq);
730 if (CMP_FLAG(l, ECHO)) {
732 * Remove the character from the screen. This is even
733 * safe for characters that span multiple characters
734 * (tabs, quoted, etc).
736 if (tp->t_writepos >= tp->t_column) {
737 /* Retype the sentence. */
739 } else if (CMP_FLAG(l, ECHOE)) {
740 if (CTL_PRINT(c, quote)) {
741 /* Remove ^X formatted chars. */
742 if (CMP_FLAG(l, ECHOCTL)) {
744 ttyoutq_write_nofrag(&tp->t_outq,
747 } else if (c == ' ') {
748 /* Space character needs no rubbing. */
750 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
751 } else if (c == CTAB) {
753 * Making backspace work with tabs is
754 * quite hard. Recalculate the length of
755 * this character and remove it.
757 * Because terminal settings could be
758 * changed while the line is being
759 * inserted, the calculations don't have
760 * to be correct. Make sure we keep the
761 * tab length within proper bounds.
763 prevpos = ttydisc_recalc_linelength(tp);
764 if (prevpos >= tp->t_column)
767 tablen = tp->t_column - prevpos;
771 tp->t_column = prevpos;
772 ttyoutq_write_nofrag(&tp->t_outq,
773 "\b\b\b\b\b\b\b\b", tablen);
777 * Remove a regular character by
778 * punching a space over it.
781 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
784 /* Don't print spaces. */
785 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
793 ttydisc_rubword(struct tty *tp)
798 /* Strip whitespace first. */
800 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
808 * Record whether the last character from the previous iteration
809 * was alphanumeric or not. We need this to implement ALTWERASE.
811 alnum = CTL_ALNUM(c);
815 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
819 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
825 ttydisc_rint(struct tty *tp, char c, int flags)
827 int signal, quote = 0;
828 char ob[3] = { 0xff, 0x00 };
831 tty_lock_assert(tp, MA_OWNED);
833 atomic_add_long(&tty_nin, 1);
835 if (ttyhook_hashook(tp, rint))
836 return ttyhook_rint(tp, c, flags);
838 if (tp->t_flags & TF_BYPASS)
842 if (flags & TRE_BREAK) {
843 if (CMP_FLAG(i, IGNBRK)) {
844 /* Ignore break characters. */
846 } else if (CMP_FLAG(i, BRKINT)) {
847 /* Generate SIGINT on break. */
848 tty_flush(tp, FREAD|FWRITE);
849 tty_signal_pgrp(tp, SIGINT);
855 } else if (flags & TRE_FRAMING ||
856 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
857 if (CMP_FLAG(i, IGNPAR)) {
858 /* Ignore bad characters. */
867 /* Allow any character to perform a wakeup. */
868 if (CMP_FLAG(i, IXANY))
869 tp->t_flags &= ~TF_STOPPED;
871 /* Remove the top bit. */
872 if (CMP_FLAG(i, ISTRIP))
875 /* Skip input processing when we want to print it literally. */
876 if (tp->t_flags & TF_LITERAL) {
877 tp->t_flags &= ~TF_LITERAL;
882 /* Special control characters that are implementation dependent. */
883 if (CMP_FLAG(l, IEXTEN)) {
884 /* Accept the next character as literal. */
885 if (CMP_CC(VLNEXT, c)) {
886 if (CMP_FLAG(l, ECHO)) {
887 if (CMP_FLAG(l, ECHOE))
888 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
890 ttydisc_echo(tp, c, 0);
892 tp->t_flags |= TF_LITERAL;
898 * Handle signal processing.
900 if (CMP_FLAG(l, ISIG)) {
901 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
902 if (CMP_CC(VSTATUS, c)) {
903 tty_signal_pgrp(tp, SIGINFO);
909 * When compared to the old implementation, this
910 * implementation also flushes the output queue. POSIX
911 * is really brief about this, but does makes us assume
915 if (CMP_CC(VINTR, c)) {
917 } else if (CMP_CC(VQUIT, c)) {
919 } else if (CMP_CC(VSUSP, c)) {
925 * Echo the character before signalling the
928 if (!CMP_FLAG(l, NOFLSH))
929 tty_flush(tp, FREAD|FWRITE);
930 ttydisc_echo(tp, c, 0);
931 tty_signal_pgrp(tp, signal);
937 * Handle start/stop characters.
939 if (CMP_FLAG(i, IXON)) {
940 if (CMP_CC(VSTOP, c)) {
941 /* Stop it if we aren't stopped yet. */
942 if ((tp->t_flags & TF_STOPPED) == 0) {
943 tp->t_flags |= TF_STOPPED;
948 * When VSTART == VSTOP, we should make this key
951 if (!CMP_CC(VSTART, c))
954 if (CMP_CC(VSTART, c)) {
955 tp->t_flags &= ~TF_STOPPED;
960 /* Conversion of CR and NL. */
963 if (CMP_FLAG(i, IGNCR))
965 if (CMP_FLAG(i, ICRNL))
969 if (CMP_FLAG(i, INLCR))
974 /* Canonical line editing. */
975 if (CMP_FLAG(l, ICANON)) {
976 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
979 } else if (CMP_CC(VKILL, c)) {
980 while (ttydisc_rubchar(tp) == 0);
982 } else if (CMP_FLAG(l, IEXTEN)) {
983 if (CMP_CC(VWERASE, c)) {
986 } else if (CMP_CC(VREPRINT, c)) {
994 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
995 /* Print 0xff 0xff. */
1007 if (CMP_FLAG(i, PARMRK)) {
1008 /* Prepend 0xff 0x00 0x.. */
1018 /* See if we can store this on the input queue. */
1019 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1020 if (CMP_FLAG(i, IMAXBEL))
1021 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1024 * Prevent a deadlock here. It may be possible that a
1025 * user has entered so much data, there is no data
1026 * available to read(), but the buffers are full anyway.
1028 * Only enter the high watermark if the device driver
1029 * can actually transmit something.
1031 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1034 tty_hiwat_in_block(tp);
1039 * In raw mode, we canonicalize after receiving a single
1040 * character. Otherwise, we canonicalize when we receive a
1041 * newline, VEOL or VEOF, but only when it isn't quoted.
1043 if (!CMP_FLAG(l, ICANON) ||
1044 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1045 ttyinq_canonicalize(&tp->t_inq);
1048 ttydisc_echo(tp, c, quote);
1054 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1058 tty_lock_assert(tp, MA_OWNED);
1060 MPASS(tp->t_flags & TF_BYPASS);
1062 atomic_add_long(&tty_nin, len);
1064 if (ttyhook_hashook(tp, rint_bypass))
1065 return ttyhook_rint_bypass(tp, buf, len);
1067 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1068 ttyinq_canonicalize(&tp->t_inq);
1070 tty_hiwat_in_block(tp);
1076 ttydisc_rint_done(struct tty *tp)
1079 tty_lock_assert(tp, MA_OWNED);
1081 if (ttyhook_hashook(tp, rint_done))
1082 ttyhook_rint_done(tp);
1084 /* Wake up readers. */
1085 tty_wakeup(tp, FREAD);
1086 /* Wake up driver for echo. */
1087 ttydevsw_outwakeup(tp);
1091 ttydisc_rint_poll(struct tty *tp)
1095 tty_lock_assert(tp, MA_OWNED);
1097 if (ttyhook_hashook(tp, rint_poll))
1098 return ttyhook_rint_poll(tp);
1101 * XXX: Still allow character input when there's no space in the
1102 * buffers, but we haven't entered the high watermark. This is
1103 * to allow backspace characters to be inserted when in
1106 l = ttyinq_bytesleft(&tp->t_inq);
1107 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1114 ttydisc_wakeup_watermark(struct tty *tp)
1118 c = ttyoutq_bytesleft(&tp->t_outq);
1119 if (tp->t_flags & TF_HIWAT_OUT) {
1120 /* Only allow us to run when we're below the watermark. */
1121 if (c < tp->t_outlow)
1124 /* Reset the watermark. */
1125 tp->t_flags &= ~TF_HIWAT_OUT;
1127 /* Only run when we have data at all. */
1131 tty_wakeup(tp, FWRITE);
1135 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1138 tty_lock_assert(tp, MA_OWNED);
1140 if (tp->t_flags & TF_STOPPED)
1143 if (ttyhook_hashook(tp, getc_inject))
1144 return ttyhook_getc_inject(tp, buf, len);
1146 len = ttyoutq_read(&tp->t_outq, buf, len);
1148 if (ttyhook_hashook(tp, getc_capture))
1149 ttyhook_getc_capture(tp, buf, len);
1151 ttydisc_wakeup_watermark(tp);
1152 atomic_add_long(&tty_nout, len);
1158 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1161 int obytes = uio->uio_resid;
1163 char buf[TTY_STACKBUF];
1165 tty_lock_assert(tp, MA_OWNED);
1167 if (tp->t_flags & TF_STOPPED)
1171 * When a TTY hook is attached, we cannot perform unbuffered
1172 * copying to userspace. Just call ttydisc_getc() and
1173 * temporarily store data in a shadow buffer.
1175 if (ttyhook_hashook(tp, getc_capture) ||
1176 ttyhook_hashook(tp, getc_inject)) {
1177 while (uio->uio_resid > 0) {
1178 /* Read to shadow buffer. */
1179 len = ttydisc_getc(tp, buf,
1180 MIN(uio->uio_resid, sizeof buf));
1184 /* Copy to userspace. */
1186 error = uiomove(buf, len, uio);
1193 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1195 ttydisc_wakeup_watermark(tp);
1196 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1203 ttydisc_getc_poll(struct tty *tp)
1206 tty_lock_assert(tp, MA_OWNED);
1208 if (tp->t_flags & TF_STOPPED)
1211 if (ttyhook_hashook(tp, getc_poll))
1212 return ttyhook_getc_poll(tp);
1214 return ttyoutq_bytesused(&tp->t_outq);
1218 * XXX: not really related to the TTYDISC, but we'd better put
1219 * tty_putchar() here, because we need to perform proper output
1224 tty_putchar(struct tty *tp, char c)
1226 tty_lock_assert(tp, MA_OWNED);
1231 ttydisc_echo_force(tp, c, 0);
1232 tp->t_writepos = tp->t_column;
1233 ttyinq_reprintpos_set(&tp->t_inq);
1235 ttydevsw_outwakeup(tp);