2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
7 * Portions of this software were developed under sponsorship from Snow
8 * B.V., the Netherlands.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
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>
46 #include <teken/teken.h>
47 #include <teken/teken_wcwidth.h>
50 * Standard TTYDISC `termios' line discipline.
54 static unsigned long tty_nin = 0;
55 SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
56 &tty_nin, 0, "Total amount of bytes received");
57 static unsigned long tty_nout = 0;
58 SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
59 &tty_nout, 0, "Total amount of bytes transmitted");
61 /* termios comparison macro's. */
62 #define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
63 tp->t_termios.c_cc[v] == (c))
64 #define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
66 /* Characters that cannot be modified through c_cc. */
71 /* Character is a control character. */
72 #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20)
73 /* Control character should be processed on echo. */
74 #define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \
75 (c) == CNL || (c) == CCR))
76 /* Control character should be printed using ^X notation. */
77 #define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
78 ((q) || ((c) != CTAB && (c) != CNL))))
79 /* Character is whitespace. */
80 #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB)
81 /* Character is alphanumeric. */
82 #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \
83 ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
84 /* Character is UTF8-encoded. */
85 #define CTL_UTF8(c) (!!((c) & 0x80))
86 /* Character is a UTF8 continuation byte. */
87 #define CTL_UTF8_CONT(c) (((c) & 0xc0) == 0x80)
89 #define TTY_STACKBUF 256
90 #define UTF8_STACKBUF 4
93 ttydisc_open(struct tty *tp)
99 ttydisc_close(struct tty *tp)
102 /* Clean up our flags when leaving the discipline. */
103 tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
104 tp->t_termios.c_lflag &= ~FLUSHO;
107 * POSIX states that we must drain output and flush input on
108 * last close. Draining has already been done if possible.
110 tty_flush(tp, FREAD | FWRITE);
112 if (ttyhook_hashook(tp, close))
117 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
119 char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
121 size_t clen, flen = 0, n = 1;
122 unsigned char lastc = _POSIX_VDISABLE;
124 #define BREAK_ADD(c) do { \
125 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
126 breakc[n++] = tp->t_termios.c_cc[c]; \
128 /* Determine which characters we should trigger on. */
135 error = tty_wait_background(tp, curthread, SIGTTIN);
140 * Quite a tricky case: unlike the old TTY
141 * implementation, this implementation copies data back
142 * to userspace in large chunks. Unfortunately, we can't
143 * calculate the line length on beforehand if it crosses
144 * ttyinq_block boundaries, because multiple reads could
145 * then make this code read beyond the newline.
147 * This is why we limit the read to:
148 * - The size the user has requested
149 * - The blocksize (done in tty_inq.c)
150 * - The amount of bytes until the newline
152 * This causes the line length to be recalculated after
153 * each block has been copied to userspace. This will
154 * cause the TTY layer to return data in chunks using
155 * the blocksize (except the first and last blocks).
157 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
162 if (tp->t_flags & TF_ZOMBIE)
164 else if (ioflag & IO_NDELAY)
165 return (EWOULDBLOCK);
167 error = tty_wait(tp, &tp->t_inwait);
173 /* Don't send the EOF char back to userspace. */
174 if (CMP_CC(VEOF, lastc))
179 /* Read and throw away the EOF character. */
180 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
184 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
190 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
192 size_t vmin = tp->t_termios.c_cc[VMIN];
193 ssize_t oresid = uio->uio_resid;
196 MPASS(tp->t_termios.c_cc[VTIME] == 0);
199 * This routine implements the easy cases of read()s while in
200 * non-canonical mode, namely case B and D, where we don't have
205 error = tty_wait_background(tp, curthread, SIGTTIN);
209 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
213 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
216 /* We have to wait for more. */
217 if (tp->t_flags & TF_ZOMBIE)
219 else if (ioflag & IO_NDELAY)
220 return (EWOULDBLOCK);
222 error = tty_wait(tp, &tp->t_inwait);
229 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
232 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
233 unsigned int vtime = tp->t_termios.c_cc[VTIME];
234 struct timeval end, now, left;
237 MPASS(tp->t_termios.c_cc[VTIME] != 0);
239 /* Determine when the read should be expired. */
240 end.tv_sec = vtime / 10;
241 end.tv_usec = (vtime % 10) * 100000;
243 timevaladd(&end, &now);
246 error = tty_wait_background(tp, curthread, SIGTTIN);
250 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
254 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
257 /* Calculate how long we should wait. */
259 if (timevalcmp(&now, &end, >))
262 timevalsub(&left, &now);
266 * We have to wait for more. If the timer expires, we
267 * should return a 0-byte read.
269 if (tp->t_flags & TF_ZOMBIE)
271 else if (ioflag & IO_NDELAY)
272 return (EWOULDBLOCK);
274 error = tty_timedwait(tp, &tp->t_inwait, hz);
276 return (error == EWOULDBLOCK ? 0 : error);
283 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
285 size_t vmin = tp->t_termios.c_cc[VMIN];
286 ssize_t oresid = uio->uio_resid;
289 MPASS(tp->t_termios.c_cc[VMIN] != 0);
290 MPASS(tp->t_termios.c_cc[VTIME] != 0);
293 * When using the interbyte timer, the timer should be started
294 * after the first byte has been received. We just call into the
295 * generic read timer code after we've received the first byte.
299 error = tty_wait_background(tp, curthread, SIGTTIN);
303 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
307 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
311 * Not enough data, but we did receive some, which means
312 * we'll now start using the interbyte timer.
314 if (oresid != uio->uio_resid)
317 /* We have to wait for more. */
318 if (tp->t_flags & TF_ZOMBIE)
320 else if (ioflag & IO_NDELAY)
321 return (EWOULDBLOCK);
323 error = tty_wait(tp, &tp->t_inwait);
328 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
332 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
336 tty_assert_locked(tp);
338 if (uio->uio_resid == 0)
341 if (CMP_FLAG(l, ICANON))
342 error = ttydisc_read_canonical(tp, uio, ioflag);
343 else if (tp->t_termios.c_cc[VTIME] == 0)
344 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
345 else if (tp->t_termios.c_cc[VMIN] == 0)
346 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
349 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
351 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
352 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
353 /* Unset the input watermark when we've got enough space. */
354 tty_hiwat_in_unblock(tp);
360 static __inline unsigned int
361 ttydisc_findchar(const char *obstart, unsigned int oblen)
363 const char *c = obstart;
371 return (c - obstart);
375 ttydisc_write_oproc(struct tty *tp, char c)
377 unsigned int scnt, error;
379 MPASS(CMP_FLAG(o, OPOST));
382 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
385 /* End-of-text dropping. */
386 if (CMP_FLAG(o, ONOEOT))
388 return PRINT_NORMAL();
391 /* Handle backspace to fix tab expansion. */
392 if (PRINT_NORMAL() != 0)
394 if (tp->t_column > 0)
400 scnt = 8 - (tp->t_column & 7);
401 if (CMP_FLAG(o, TAB3)) {
402 error = ttyoutq_write_nofrag(&tp->t_outq,
405 error = PRINT_NORMAL();
410 tp->t_column += scnt;
411 MPASS((tp->t_column % 8) == 0);
415 /* Newline conversion. */
416 if (CMP_FLAG(o, ONLCR)) {
417 /* Convert \n to \r\n. */
418 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
420 error = PRINT_NORMAL();
425 if (CMP_FLAG(o, ONLCR|ONLRET)) {
426 tp->t_column = tp->t_writepos = 0;
427 ttyinq_reprintpos_set(&tp->t_inq);
432 /* Carriage return to newline conversion. */
433 if (CMP_FLAG(o, OCRNL))
435 /* Omit carriage returns on column 0. */
436 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
438 if (PRINT_NORMAL() != 0)
441 tp->t_column = tp->t_writepos = 0;
442 ttyinq_reprintpos_set(&tp->t_inq);
447 * Invisible control character. Print it, but don't
448 * increase the column count.
450 return PRINT_NORMAL();
455 * Just like the old TTY implementation, we need to copy data in chunks
456 * into a temporary buffer. One of the reasons why we need to do this,
457 * is because output processing (only TAB3 though) may allow the buffer
458 * to grow eight times.
461 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
463 char ob[TTY_STACKBUF];
466 unsigned int oblen = 0;
468 tty_assert_locked(tp);
470 if (tp->t_flags & TF_ZOMBIE)
474 * We don't need to check whether the process is the foreground
475 * process group or if we have a carrier. This is already done
479 while (uio->uio_resid > 0) {
484 if (CMP_FLAG(l, FLUSHO)) {
485 uio->uio_offset += uio->uio_resid;
490 /* Step 1: read data. */
492 nlen = MIN(uio->uio_resid, sizeof ob);
494 error = uiomove(ob, nlen, uio);
507 /* Step 2: process data. */
509 unsigned int plen, wlen;
511 if (CMP_FLAG(l, FLUSHO)) {
512 uio->uio_offset += uio->uio_resid;
517 /* Search for special characters for post processing. */
518 if (CMP_FLAG(o, OPOST)) {
519 plen = ttydisc_findchar(obstart, oblen);
526 * We're going to process a character
527 * that needs processing
529 if (ttydisc_write_oproc(tp, *obstart) == 0) {
533 tp->t_writepos = tp->t_column;
534 ttyinq_reprintpos_set(&tp->t_inq);
538 /* We're going to write regular data. */
539 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
542 tp->t_column += wlen;
544 tp->t_writepos = tp->t_column;
545 ttyinq_reprintpos_set(&tp->t_inq);
551 /* Watermark reached. Try to sleep. */
552 tp->t_flags |= TF_HIWAT_OUT;
554 if (ioflag & IO_NDELAY) {
560 * The driver may write back the data
561 * synchronously. Be sure to check the high
562 * water mark before going to sleep.
564 ttydevsw_outwakeup(tp);
565 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
568 error = tty_wait(tp, &tp->t_outwait);
572 if (tp->t_flags & TF_ZOMBIE) {
581 ttydevsw_outwakeup(tp);
584 * Add the amount of bytes that we didn't process back to the
585 * uio counters. We need to do this to make sure write() doesn't
586 * count the bytes we didn't store in the queue.
588 uio->uio_resid += oblen;
593 ttydisc_optimize(struct tty *tp)
595 tty_assert_locked(tp);
597 if (ttyhook_hashook(tp, rint_bypass)) {
598 tp->t_flags |= TF_BYPASS;
599 } else if (ttyhook_hashook(tp, rint)) {
600 tp->t_flags &= ~TF_BYPASS;
601 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
602 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
603 (!CMP_FLAG(i, PARMRK) ||
604 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
605 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
606 tp->t_flags |= TF_BYPASS;
608 tp->t_flags &= ~TF_BYPASS;
613 ttydisc_modem(struct tty *tp, int open)
616 tty_assert_locked(tp);
619 cv_broadcast(&tp->t_dcdwait);
622 * Ignore modem status lines when CLOCAL is turned on, but don't
623 * enter the zombie state when the TTY isn't opened, because
624 * that would cause the TTY to be in zombie state after being
627 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
634 tp->t_flags |= TF_ZOMBIE;
636 tty_signal_sessleader(tp, SIGHUP);
637 tty_flush(tp, FREAD|FWRITE);
640 * Carrier is back again.
643 /* XXX: what should we do here? */
648 ttydisc_echo_force(struct tty *tp, char c, int quote)
651 if (CMP_FLAG(l, FLUSHO))
654 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
656 * Only perform postprocessing when OPOST is turned on
657 * and the character is an unquoted BS/TB/NL/CR.
659 return ttydisc_write_oproc(tp, c);
660 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
662 * Only use ^X notation when ECHOCTL is turned on and
663 * we've got an quoted control character.
665 * Print backspaces when echoing an end-of-file.
667 char ob[4] = "^?\b\b";
669 /* Print ^X notation. */
673 if (!quote && CMP_CC(VEOF, c)) {
674 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
677 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
680 /* Can just be printed. */
682 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
687 ttydisc_echo(struct tty *tp, char c, int quote)
691 * Only echo characters when ECHO is turned on, or ECHONL when
692 * the character is an unquoted newline.
694 if (!CMP_FLAG(l, ECHO) &&
695 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
698 return ttydisc_echo_force(tp, c, quote);
702 ttydisc_reprint_char(void *d, char c, int quote)
706 ttydisc_echo(tp, c, quote);
710 ttydisc_reprint(struct tty *tp)
714 /* Print ^R\n, followed by the line. */
715 c = tp->t_termios.c_cc[VREPRINT];
716 if (c != _POSIX_VDISABLE)
717 ttydisc_echo(tp, c, 0);
718 ttydisc_echo(tp, CNL, 0);
719 ttyinq_reprintpos_reset(&tp->t_inq);
721 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
724 struct ttydisc_recalc_length {
730 ttydisc_recalc_charlength(void *d, char c, int quote)
732 struct ttydisc_recalc_length *data = d;
733 struct tty *tp = data->tp;
735 if (CTL_PRINT(c, quote)) {
736 if (CMP_FLAG(l, ECHOCTL))
738 } else if (c == CTAB) {
739 data->curlen += 8 - (data->curlen & 7);
746 ttydisc_recalc_linelength(struct tty *tp)
748 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
750 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
751 ttydisc_recalc_charlength, &data);
752 return (data.curlen);
756 ttydisc_rubchar(struct tty *tp)
760 unsigned int prevpos, tablen;
762 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
764 ttyinq_unputchar(&tp->t_inq);
766 if (CMP_FLAG(l, ECHO)) {
768 * Remove the character from the screen. This is even
769 * safe for characters that span multiple characters
770 * (tabs, quoted, etc).
772 if (tp->t_writepos >= tp->t_column) {
773 /* Retype the sentence. */
775 } else if (CMP_FLAG(l, ECHOE)) {
776 if (CTL_PRINT(c, quote)) {
777 /* Remove ^X formatted chars. */
778 if (CMP_FLAG(l, ECHOCTL)) {
780 ttyoutq_write_nofrag(&tp->t_outq,
783 } else if (c == ' ') {
784 /* Space character needs no rubbing. */
786 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
787 } else if (c == CTAB) {
789 * Making backspace work with tabs is
790 * quite hard. Recalculate the length of
791 * this character and remove it.
793 * Because terminal settings could be
794 * changed while the line is being
795 * inserted, the calculations don't have
796 * to be correct. Make sure we keep the
797 * tab length within proper bounds.
799 prevpos = ttydisc_recalc_linelength(tp);
800 if (prevpos >= tp->t_column)
803 tablen = tp->t_column - prevpos;
807 tp->t_column = prevpos;
808 ttyoutq_write_nofrag(&tp->t_outq,
809 "\b\b\b\b\b\b\b\b", tablen);
811 } else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
813 uint8_t bytes[UTF8_STACKBUF] = { 0 };
814 int curidx = UTF8_STACKBUF - 1, cwidth = 1,
816 teken_char_t codepoint;
818 /* Save current byte. */
822 /* Loop back through inq until we hit the
824 while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
825 ttyinq_peekchar(&tp->t_inq, &c, "e);
826 ttyinq_unputchar(&tp->t_inq);
832 * Shift array so that the leading
833 * byte ends up at idx 0.
835 if (nb < UTF8_STACKBUF)
836 memmove(&bytes[0], &bytes[curidx + 1],
837 nb * sizeof(uint8_t));
838 /* Check for malformed UTF8 characters. */
839 if (nb == UTF8_STACKBUF &&
840 CTL_UTF8_CONT(bytes[0])) {
842 * Place all bytes back into the inq and
843 * delete the last byte only.
845 ttyinq_write(&tp->t_inq, bytes,
848 /* Find codepoint and width. */
850 teken_utf8_bytes_to_codepoint(bytes,
853 TEKEN_UTF8_INVALID_CODEPOINT) {
854 cwidth = teken_wcwidth(
858 * Place all bytes back into the
859 * inq and fall back to
862 ttyinq_write(&tp->t_inq, bytes,
866 tp->t_column -= cwidth;
868 * Delete character by punching
869 * 'cwidth' spaces over it.
872 ttyoutq_write_nofrag(&tp->t_outq,
874 else if (cwidth == 2)
875 ttyoutq_write_nofrag(&tp->t_outq,
879 * Remove a regular character by
880 * punching a space over it.
883 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
886 /* Don't print spaces. */
887 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
895 ttydisc_rubword(struct tty *tp)
900 /* Strip whitespace first. */
902 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
910 * Record whether the last character from the previous iteration
911 * was alphanumeric or not. We need this to implement ALTWERASE.
913 alnum = CTL_ALNUM(c);
917 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
921 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
927 ttydisc_rint(struct tty *tp, char c, int flags)
929 int signal, quote = 0;
930 char ob[3] = { 0xff, 0x00 };
933 tty_assert_locked(tp);
935 atomic_add_long(&tty_nin, 1);
937 if (ttyhook_hashook(tp, rint))
938 return ttyhook_rint(tp, c, flags);
940 if (tp->t_flags & TF_BYPASS)
944 if (flags & TRE_BREAK) {
945 if (CMP_FLAG(i, IGNBRK)) {
946 /* Ignore break characters. */
948 } else if (CMP_FLAG(i, BRKINT)) {
949 /* Generate SIGINT on break. */
950 tty_flush(tp, FREAD|FWRITE);
951 tty_signal_pgrp(tp, SIGINT);
957 } else if (flags & TRE_FRAMING ||
958 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
959 if (CMP_FLAG(i, IGNPAR)) {
960 /* Ignore bad characters. */
969 /* Allow any character to perform a wakeup. */
970 if (CMP_FLAG(i, IXANY)) {
971 tp->t_flags &= ~TF_STOPPED;
972 tp->t_termios.c_lflag &= ~FLUSHO;
975 /* Remove the top bit. */
976 if (CMP_FLAG(i, ISTRIP))
979 /* Skip input processing when we want to print it literally. */
980 if (tp->t_flags & TF_LITERAL) {
981 tp->t_flags &= ~TF_LITERAL;
986 /* Special control characters that are implementation dependent. */
987 if (CMP_FLAG(l, IEXTEN)) {
988 /* Accept the next character as literal. */
989 if (CMP_CC(VLNEXT, c)) {
990 if (CMP_FLAG(l, ECHO)) {
991 if (CMP_FLAG(l, ECHOE))
992 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
994 ttydisc_echo(tp, c, 0);
996 tp->t_flags |= TF_LITERAL;
999 /* Discard processing */
1000 if (CMP_CC(VDISCARD, c)) {
1001 if (CMP_FLAG(l, FLUSHO)) {
1002 tp->t_termios.c_lflag &= ~FLUSHO;
1004 tty_flush(tp, FWRITE);
1005 ttydisc_echo(tp, c, 0);
1006 if (tp->t_inq.ti_end > 0)
1007 ttydisc_reprint(tp);
1008 tp->t_termios.c_lflag |= FLUSHO;
1014 * Handle signal processing.
1016 if (CMP_FLAG(l, ISIG)) {
1017 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
1018 if (CMP_CC(VSTATUS, c)) {
1019 tty_signal_pgrp(tp, SIGINFO);
1025 * When compared to the old implementation, this
1026 * implementation also flushes the output queue. POSIX
1027 * is really brief about this, but does makes us assume
1031 if (CMP_CC(VINTR, c)) {
1033 } else if (CMP_CC(VQUIT, c)) {
1035 } else if (CMP_CC(VSUSP, c)) {
1041 * Echo the character before signalling the
1044 if (!CMP_FLAG(l, NOFLSH))
1045 tty_flush(tp, FREAD|FWRITE);
1046 ttydisc_echo(tp, c, 0);
1047 tty_signal_pgrp(tp, signal);
1053 * Handle start/stop characters.
1055 if (CMP_FLAG(i, IXON)) {
1056 if (CMP_CC(VSTOP, c)) {
1057 /* Stop it if we aren't stopped yet. */
1058 if ((tp->t_flags & TF_STOPPED) == 0) {
1059 tp->t_flags |= TF_STOPPED;
1064 * When VSTART == VSTOP, we should make this key
1067 if (!CMP_CC(VSTART, c))
1070 if (CMP_CC(VSTART, c)) {
1071 tp->t_flags &= ~TF_STOPPED;
1076 /* Conversion of CR and NL. */
1079 if (CMP_FLAG(i, IGNCR))
1081 if (CMP_FLAG(i, ICRNL))
1085 if (CMP_FLAG(i, INLCR))
1090 /* Canonical line editing. */
1091 if (CMP_FLAG(l, ICANON)) {
1092 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1093 ttydisc_rubchar(tp);
1095 } else if (CMP_CC(VKILL, c)) {
1096 while (ttydisc_rubchar(tp) == 0);
1098 } else if (CMP_FLAG(l, IEXTEN)) {
1099 if (CMP_CC(VWERASE, c)) {
1100 ttydisc_rubword(tp);
1102 } else if (CMP_CC(VREPRINT, c)) {
1103 ttydisc_reprint(tp);
1110 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1111 /* Print 0xff 0xff. */
1123 if (CMP_FLAG(i, PARMRK)) {
1124 /* Prepend 0xff 0x00 0x.. */
1134 /* See if we can store this on the input queue. */
1135 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1136 if (CMP_FLAG(i, IMAXBEL))
1137 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1140 * Prevent a deadlock here. It may be possible that a
1141 * user has entered so much data, there is no data
1142 * available to read(), but the buffers are full anyway.
1144 * Only enter the high watermark if the device driver
1145 * can actually transmit something.
1147 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1150 tty_hiwat_in_block(tp);
1155 * In raw mode, we canonicalize after receiving a single
1156 * character. Otherwise, we canonicalize when we receive a
1157 * newline, VEOL or VEOF, but only when it isn't quoted.
1159 if (!CMP_FLAG(l, ICANON) ||
1160 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1161 ttyinq_canonicalize(&tp->t_inq);
1164 ttydisc_echo(tp, c, quote);
1170 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1174 if (ttydisc_can_bypass(tp))
1175 return (ttydisc_rint_bypass(tp, buf, len));
1177 for (cbuf = buf; len-- > 0; cbuf++) {
1178 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1182 return (cbuf - (const char *)buf);
1186 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1190 tty_assert_locked(tp);
1192 MPASS(tp->t_flags & TF_BYPASS);
1194 atomic_add_long(&tty_nin, len);
1196 if (ttyhook_hashook(tp, rint_bypass))
1197 return ttyhook_rint_bypass(tp, buf, len);
1199 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1200 ttyinq_canonicalize(&tp->t_inq);
1202 tty_hiwat_in_block(tp);
1208 ttydisc_rint_done(struct tty *tp)
1211 tty_assert_locked(tp);
1213 if (ttyhook_hashook(tp, rint_done))
1214 ttyhook_rint_done(tp);
1216 /* Wake up readers. */
1217 tty_wakeup(tp, FREAD);
1218 /* Wake up driver for echo. */
1219 ttydevsw_outwakeup(tp);
1223 ttydisc_rint_poll(struct tty *tp)
1227 tty_assert_locked(tp);
1229 if (ttyhook_hashook(tp, rint_poll))
1230 return ttyhook_rint_poll(tp);
1233 * XXX: Still allow character input when there's no space in the
1234 * buffers, but we haven't entered the high watermark. This is
1235 * to allow backspace characters to be inserted when in
1238 l = ttyinq_bytesleft(&tp->t_inq);
1239 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1246 ttydisc_wakeup_watermark(struct tty *tp)
1250 c = ttyoutq_bytesleft(&tp->t_outq);
1251 if (tp->t_flags & TF_HIWAT_OUT) {
1252 /* Only allow us to run when we're below the watermark. */
1253 if (c < tp->t_outlow)
1256 /* Reset the watermark. */
1257 tp->t_flags &= ~TF_HIWAT_OUT;
1259 /* Only run when we have data at all. */
1263 tty_wakeup(tp, FWRITE);
1267 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1270 tty_assert_locked(tp);
1272 if (tp->t_flags & TF_STOPPED)
1275 if (ttyhook_hashook(tp, getc_inject))
1276 return ttyhook_getc_inject(tp, buf, len);
1278 len = ttyoutq_read(&tp->t_outq, buf, len);
1280 if (ttyhook_hashook(tp, getc_capture))
1281 ttyhook_getc_capture(tp, buf, len);
1283 ttydisc_wakeup_watermark(tp);
1284 atomic_add_long(&tty_nout, len);
1290 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1293 ssize_t obytes = uio->uio_resid;
1295 char buf[TTY_STACKBUF];
1297 tty_assert_locked(tp);
1299 if (tp->t_flags & TF_STOPPED)
1303 * When a TTY hook is attached, we cannot perform unbuffered
1304 * copying to userspace. Just call ttydisc_getc() and
1305 * temporarily store data in a shadow buffer.
1307 if (ttyhook_hashook(tp, getc_capture) ||
1308 ttyhook_hashook(tp, getc_inject)) {
1309 while (uio->uio_resid > 0) {
1310 /* Read to shadow buffer. */
1311 len = ttydisc_getc(tp, buf,
1312 MIN(uio->uio_resid, sizeof buf));
1316 /* Copy to userspace. */
1318 error = uiomove(buf, len, uio);
1325 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1327 ttydisc_wakeup_watermark(tp);
1328 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1335 ttydisc_getc_poll(struct tty *tp)
1338 tty_assert_locked(tp);
1340 if (tp->t_flags & TF_STOPPED)
1343 if (ttyhook_hashook(tp, getc_poll))
1344 return ttyhook_getc_poll(tp);
1346 return ttyoutq_bytesused(&tp->t_outq);
1350 * XXX: not really related to the TTYDISC, but we'd better put
1351 * tty_putchar() here, because we need to perform proper output
1356 tty_putstrn(struct tty *tp, const char *p, size_t n)
1360 tty_assert_locked(tp);
1365 for (i = 0; i < n; i++)
1366 ttydisc_echo_force(tp, p[i], 0);
1368 tp->t_writepos = tp->t_column;
1369 ttyinq_reprintpos_set(&tp->t_inq);
1371 ttydevsw_outwakeup(tp);
1376 tty_putchar(struct tty *tp, char c)
1378 return (tty_putstrn(tp, &c, 1));