2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/fcntl.h>
37 #include <sys/filio.h>
38 #include <sys/kernel.h>
39 #include <sys/signal.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
43 #include <sys/ttycom.h>
44 #include <sys/ttydefaults.h>
46 #include <sys/vnode.h>
49 * Standard TTYDISC `termios' line discipline.
53 static unsigned long tty_nin = 0;
54 SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
55 &tty_nin, 0, "Total amount of bytes received");
56 static unsigned long tty_nout = 0;
57 SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
58 &tty_nout, 0, "Total amount of bytes transmitted");
60 /* termios comparison macro's. */
61 #define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
62 tp->t_termios.c_cc[v] == (c))
63 #define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
65 /* Characters that cannot be modified through c_cc. */
70 /* Character is a control character. */
71 #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20)
72 /* Control character should be processed on echo. */
73 #define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \
74 (c) == CNL || (c) == CCR))
75 /* Control character should be printed using ^X notation. */
76 #define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
77 ((q) || ((c) != CTAB && (c) != CNL))))
78 /* Character is whitespace. */
79 #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB)
80 /* Character is alphanumeric. */
81 #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \
82 ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
84 #define TTY_STACKBUF 256
87 ttydisc_open(struct tty *tp)
93 ttydisc_close(struct tty *tp)
96 /* Clean up our flags when leaving the discipline. */
97 tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
100 * POSIX states that we must drain output and flush input on
101 * last close. Draining has already been done if possible.
103 tty_flush(tp, FREAD | FWRITE);
105 if (ttyhook_hashook(tp, close))
110 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
112 char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
114 size_t clen, flen = 0, n = 1;
115 unsigned char lastc = _POSIX_VDISABLE;
117 #define BREAK_ADD(c) do { \
118 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
119 breakc[n++] = tp->t_termios.c_cc[c]; \
121 /* Determine which characters we should trigger on. */
128 error = tty_wait_background(tp, curthread, SIGTTIN);
133 * Quite a tricky case: unlike the old TTY
134 * implementation, this implementation copies data back
135 * to userspace in large chunks. Unfortunately, we can't
136 * calculate the line length on beforehand if it crosses
137 * ttyinq_block boundaries, because multiple reads could
138 * then make this code read beyond the newline.
140 * This is why we limit the read to:
141 * - The size the user has requested
142 * - The blocksize (done in tty_inq.c)
143 * - The amount of bytes until the newline
145 * This causes the line length to be recalculated after
146 * each block has been copied to userspace. This will
147 * cause the TTY layer to return data in chunks using
148 * the blocksize (except the first and last blocks).
150 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
155 if (tp->t_flags & TF_ZOMBIE)
157 else if (ioflag & IO_NDELAY)
158 return (EWOULDBLOCK);
160 error = tty_wait(tp, &tp->t_inwait);
166 /* Don't send the EOF char back to userspace. */
167 if (CMP_CC(VEOF, lastc))
172 /* Read and throw away the EOF character. */
173 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
177 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
183 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
185 size_t vmin = tp->t_termios.c_cc[VMIN];
186 ssize_t oresid = uio->uio_resid;
189 MPASS(tp->t_termios.c_cc[VTIME] == 0);
192 * This routine implements the easy cases of read()s while in
193 * non-canonical mode, namely case B and D, where we don't have
198 error = tty_wait_background(tp, curthread, SIGTTIN);
202 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
206 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
209 /* We have to wait for more. */
210 if (tp->t_flags & TF_ZOMBIE)
212 else if (ioflag & IO_NDELAY)
213 return (EWOULDBLOCK);
215 error = tty_wait(tp, &tp->t_inwait);
222 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
225 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
226 unsigned int vtime = tp->t_termios.c_cc[VTIME];
227 struct timeval end, now, left;
230 MPASS(tp->t_termios.c_cc[VTIME] != 0);
232 /* Determine when the read should be expired. */
233 end.tv_sec = vtime / 10;
234 end.tv_usec = (vtime % 10) * 100000;
236 timevaladd(&end, &now);
239 error = tty_wait_background(tp, curthread, SIGTTIN);
243 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
247 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
250 /* Calculate how long we should wait. */
252 if (timevalcmp(&now, &end, >))
255 timevalsub(&left, &now);
259 * We have to wait for more. If the timer expires, we
260 * should return a 0-byte read.
262 if (tp->t_flags & TF_ZOMBIE)
264 else if (ioflag & IO_NDELAY)
265 return (EWOULDBLOCK);
267 error = tty_timedwait(tp, &tp->t_inwait, hz);
269 return (error == EWOULDBLOCK ? 0 : error);
276 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
278 size_t vmin = tp->t_termios.c_cc[VMIN];
279 ssize_t oresid = uio->uio_resid;
282 MPASS(tp->t_termios.c_cc[VMIN] != 0);
283 MPASS(tp->t_termios.c_cc[VTIME] != 0);
286 * When using the interbyte timer, the timer should be started
287 * after the first byte has been received. We just call into the
288 * generic read timer code after we've received the first byte.
292 error = tty_wait_background(tp, curthread, SIGTTIN);
296 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
300 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
304 * Not enough data, but we did receive some, which means
305 * we'll now start using the interbyte timer.
307 if (oresid != uio->uio_resid)
310 /* We have to wait for more. */
311 if (tp->t_flags & TF_ZOMBIE)
313 else if (ioflag & IO_NDELAY)
314 return (EWOULDBLOCK);
316 error = tty_wait(tp, &tp->t_inwait);
321 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
325 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
329 tty_lock_assert(tp, MA_OWNED);
331 if (uio->uio_resid == 0)
334 if (CMP_FLAG(l, ICANON))
335 error = ttydisc_read_canonical(tp, uio, ioflag);
336 else if (tp->t_termios.c_cc[VTIME] == 0)
337 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
338 else if (tp->t_termios.c_cc[VMIN] == 0)
339 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
342 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
344 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
345 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
346 /* Unset the input watermark when we've got enough space. */
347 tty_hiwat_in_unblock(tp);
353 static __inline unsigned int
354 ttydisc_findchar(const char *obstart, unsigned int oblen)
356 const char *c = obstart;
364 return (c - obstart);
368 ttydisc_write_oproc(struct tty *tp, char c)
370 unsigned int scnt, error;
372 MPASS(CMP_FLAG(o, OPOST));
375 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
378 /* End-of-text dropping. */
379 if (CMP_FLAG(o, ONOEOT))
381 return PRINT_NORMAL();
384 /* Handle backspace to fix tab expansion. */
385 if (PRINT_NORMAL() != 0)
387 if (tp->t_column > 0)
393 scnt = 8 - (tp->t_column & 7);
394 if (CMP_FLAG(o, TAB3)) {
395 error = ttyoutq_write_nofrag(&tp->t_outq,
398 error = PRINT_NORMAL();
403 tp->t_column += scnt;
404 MPASS((tp->t_column % 8) == 0);
408 /* Newline conversion. */
409 if (CMP_FLAG(o, ONLCR)) {
410 /* Convert \n to \r\n. */
411 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
413 error = PRINT_NORMAL();
418 if (CMP_FLAG(o, ONLCR|ONLRET)) {
419 tp->t_column = tp->t_writepos = 0;
420 ttyinq_reprintpos_set(&tp->t_inq);
425 /* Carriage return to newline conversion. */
426 if (CMP_FLAG(o, OCRNL))
428 /* Omit carriage returns on column 0. */
429 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
431 if (PRINT_NORMAL() != 0)
434 tp->t_column = tp->t_writepos = 0;
435 ttyinq_reprintpos_set(&tp->t_inq);
440 * Invisible control character. Print it, but don't
441 * increase the column count.
443 return PRINT_NORMAL();
448 * Just like the old TTY implementation, we need to copy data in chunks
449 * into a temporary buffer. One of the reasons why we need to do this,
450 * is because output processing (only TAB3 though) may allow the buffer
451 * to grow eight times.
454 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
456 char ob[TTY_STACKBUF];
459 unsigned int oblen = 0;
461 tty_lock_assert(tp, MA_OWNED);
463 if (tp->t_flags & TF_ZOMBIE)
467 * We don't need to check whether the process is the foreground
468 * process group or if we have a carrier. This is already done
472 while (uio->uio_resid > 0) {
477 /* Step 1: read data. */
479 nlen = MIN(uio->uio_resid, sizeof ob);
481 error = uiomove(ob, nlen, uio);
494 /* Step 2: process data. */
496 unsigned int plen, wlen;
498 /* Search for special characters for post processing. */
499 if (CMP_FLAG(o, OPOST)) {
500 plen = ttydisc_findchar(obstart, oblen);
507 * We're going to process a character
508 * that needs processing
510 if (ttydisc_write_oproc(tp, *obstart) == 0) {
514 tp->t_writepos = tp->t_column;
515 ttyinq_reprintpos_set(&tp->t_inq);
519 /* We're going to write regular data. */
520 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
523 tp->t_column += wlen;
525 tp->t_writepos = tp->t_column;
526 ttyinq_reprintpos_set(&tp->t_inq);
532 /* Watermark reached. Try to sleep. */
533 tp->t_flags |= TF_HIWAT_OUT;
535 if (ioflag & IO_NDELAY) {
541 * The driver may write back the data
542 * synchronously. Be sure to check the high
543 * water mark before going to sleep.
545 ttydevsw_outwakeup(tp);
546 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
549 error = tty_wait(tp, &tp->t_outwait);
553 if (tp->t_flags & TF_ZOMBIE) {
562 ttydevsw_outwakeup(tp);
565 * Add the amount of bytes that we didn't process back to the
566 * uio counters. We need to do this to make sure write() doesn't
567 * count the bytes we didn't store in the queue.
569 uio->uio_resid += oblen;
574 ttydisc_optimize(struct tty *tp)
576 tty_lock_assert(tp, MA_OWNED);
578 if (ttyhook_hashook(tp, rint_bypass)) {
579 tp->t_flags |= TF_BYPASS;
580 } else if (ttyhook_hashook(tp, rint)) {
581 tp->t_flags &= ~TF_BYPASS;
582 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
583 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
584 (!CMP_FLAG(i, PARMRK) ||
585 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
586 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
587 tp->t_flags |= TF_BYPASS;
589 tp->t_flags &= ~TF_BYPASS;
594 ttydisc_modem(struct tty *tp, int open)
597 tty_lock_assert(tp, MA_OWNED);
600 cv_broadcast(&tp->t_dcdwait);
603 * Ignore modem status lines when CLOCAL is turned on, but don't
604 * enter the zombie state when the TTY isn't opened, because
605 * that would cause the TTY to be in zombie state after being
608 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
615 tp->t_flags |= TF_ZOMBIE;
617 tty_signal_sessleader(tp, SIGHUP);
618 tty_flush(tp, FREAD|FWRITE);
621 * Carrier is back again.
624 /* XXX: what should we do here? */
629 ttydisc_echo_force(struct tty *tp, char c, int quote)
632 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
634 * Only perform postprocessing when OPOST is turned on
635 * and the character is an unquoted BS/TB/NL/CR.
637 return ttydisc_write_oproc(tp, c);
638 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
640 * Only use ^X notation when ECHOCTL is turned on and
641 * we've got an quoted control character.
643 * Print backspaces when echoing an end-of-file.
645 char ob[4] = "^?\b\b";
647 /* Print ^X notation. */
651 if (!quote && CMP_CC(VEOF, c)) {
652 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
655 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
658 /* Can just be printed. */
660 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
665 ttydisc_echo(struct tty *tp, char c, int quote)
669 * Only echo characters when ECHO is turned on, or ECHONL when
670 * the character is an unquoted newline.
672 if (!CMP_FLAG(l, ECHO) &&
673 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
676 return ttydisc_echo_force(tp, c, quote);
680 ttydisc_reprint_char(void *d, char c, int quote)
684 ttydisc_echo(tp, c, quote);
688 ttydisc_reprint(struct tty *tp)
692 /* Print ^R\n, followed by the line. */
693 c = tp->t_termios.c_cc[VREPRINT];
694 if (c != _POSIX_VDISABLE)
695 ttydisc_echo(tp, c, 0);
696 ttydisc_echo(tp, CNL, 0);
697 ttyinq_reprintpos_reset(&tp->t_inq);
699 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
702 struct ttydisc_recalc_length {
708 ttydisc_recalc_charlength(void *d, char c, int quote)
710 struct ttydisc_recalc_length *data = d;
711 struct tty *tp = data->tp;
713 if (CTL_PRINT(c, quote)) {
714 if (CMP_FLAG(l, ECHOCTL))
716 } else if (c == CTAB) {
717 data->curlen += 8 - (data->curlen & 7);
724 ttydisc_recalc_linelength(struct tty *tp)
726 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
728 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
729 ttydisc_recalc_charlength, &data);
730 return (data.curlen);
734 ttydisc_rubchar(struct tty *tp)
738 unsigned int prevpos, tablen;
740 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
742 ttyinq_unputchar(&tp->t_inq);
744 if (CMP_FLAG(l, ECHO)) {
746 * Remove the character from the screen. This is even
747 * safe for characters that span multiple characters
748 * (tabs, quoted, etc).
750 if (tp->t_writepos >= tp->t_column) {
751 /* Retype the sentence. */
753 } else if (CMP_FLAG(l, ECHOE)) {
754 if (CTL_PRINT(c, quote)) {
755 /* Remove ^X formatted chars. */
756 if (CMP_FLAG(l, ECHOCTL)) {
758 ttyoutq_write_nofrag(&tp->t_outq,
761 } else if (c == ' ') {
762 /* Space character needs no rubbing. */
764 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
765 } else if (c == CTAB) {
767 * Making backspace work with tabs is
768 * quite hard. Recalculate the length of
769 * this character and remove it.
771 * Because terminal settings could be
772 * changed while the line is being
773 * inserted, the calculations don't have
774 * to be correct. Make sure we keep the
775 * tab length within proper bounds.
777 prevpos = ttydisc_recalc_linelength(tp);
778 if (prevpos >= tp->t_column)
781 tablen = tp->t_column - prevpos;
785 tp->t_column = prevpos;
786 ttyoutq_write_nofrag(&tp->t_outq,
787 "\b\b\b\b\b\b\b\b", tablen);
791 * Remove a regular character by
792 * punching a space over it.
795 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
798 /* Don't print spaces. */
799 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
807 ttydisc_rubword(struct tty *tp)
812 /* Strip whitespace first. */
814 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
822 * Record whether the last character from the previous iteration
823 * was alphanumeric or not. We need this to implement ALTWERASE.
825 alnum = CTL_ALNUM(c);
829 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
833 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
839 ttydisc_rint(struct tty *tp, char c, int flags)
841 int signal, quote = 0;
842 char ob[3] = { 0xff, 0x00 };
845 tty_lock_assert(tp, MA_OWNED);
847 atomic_add_long(&tty_nin, 1);
849 if (ttyhook_hashook(tp, rint))
850 return ttyhook_rint(tp, c, flags);
852 if (tp->t_flags & TF_BYPASS)
856 if (flags & TRE_BREAK) {
857 if (CMP_FLAG(i, IGNBRK)) {
858 /* Ignore break characters. */
860 } else if (CMP_FLAG(i, BRKINT)) {
861 /* Generate SIGINT on break. */
862 tty_flush(tp, FREAD|FWRITE);
863 tty_signal_pgrp(tp, SIGINT);
869 } else if (flags & TRE_FRAMING ||
870 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
871 if (CMP_FLAG(i, IGNPAR)) {
872 /* Ignore bad characters. */
881 /* Allow any character to perform a wakeup. */
882 if (CMP_FLAG(i, IXANY))
883 tp->t_flags &= ~TF_STOPPED;
885 /* Remove the top bit. */
886 if (CMP_FLAG(i, ISTRIP))
889 /* Skip input processing when we want to print it literally. */
890 if (tp->t_flags & TF_LITERAL) {
891 tp->t_flags &= ~TF_LITERAL;
896 /* Special control characters that are implementation dependent. */
897 if (CMP_FLAG(l, IEXTEN)) {
898 /* Accept the next character as literal. */
899 if (CMP_CC(VLNEXT, c)) {
900 if (CMP_FLAG(l, ECHO)) {
901 if (CMP_FLAG(l, ECHOE))
902 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
904 ttydisc_echo(tp, c, 0);
906 tp->t_flags |= TF_LITERAL;
912 * Handle signal processing.
914 if (CMP_FLAG(l, ISIG)) {
915 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
916 if (CMP_CC(VSTATUS, c)) {
917 tty_signal_pgrp(tp, SIGINFO);
923 * When compared to the old implementation, this
924 * implementation also flushes the output queue. POSIX
925 * is really brief about this, but does makes us assume
929 if (CMP_CC(VINTR, c)) {
931 } else if (CMP_CC(VQUIT, c)) {
933 } else if (CMP_CC(VSUSP, c)) {
939 * Echo the character before signalling the
942 if (!CMP_FLAG(l, NOFLSH))
943 tty_flush(tp, FREAD|FWRITE);
944 ttydisc_echo(tp, c, 0);
945 tty_signal_pgrp(tp, signal);
951 * Handle start/stop characters.
953 if (CMP_FLAG(i, IXON)) {
954 if (CMP_CC(VSTOP, c)) {
955 /* Stop it if we aren't stopped yet. */
956 if ((tp->t_flags & TF_STOPPED) == 0) {
957 tp->t_flags |= TF_STOPPED;
962 * When VSTART == VSTOP, we should make this key
965 if (!CMP_CC(VSTART, c))
968 if (CMP_CC(VSTART, c)) {
969 tp->t_flags &= ~TF_STOPPED;
974 /* Conversion of CR and NL. */
977 if (CMP_FLAG(i, IGNCR))
979 if (CMP_FLAG(i, ICRNL))
983 if (CMP_FLAG(i, INLCR))
988 /* Canonical line editing. */
989 if (CMP_FLAG(l, ICANON)) {
990 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
993 } else if (CMP_CC(VKILL, c)) {
994 while (ttydisc_rubchar(tp) == 0);
996 } else if (CMP_FLAG(l, IEXTEN)) {
997 if (CMP_CC(VWERASE, c)) {
1000 } else if (CMP_CC(VREPRINT, c)) {
1001 ttydisc_reprint(tp);
1008 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1009 /* Print 0xff 0xff. */
1021 if (CMP_FLAG(i, PARMRK)) {
1022 /* Prepend 0xff 0x00 0x.. */
1032 /* See if we can store this on the input queue. */
1033 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1034 if (CMP_FLAG(i, IMAXBEL))
1035 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1038 * Prevent a deadlock here. It may be possible that a
1039 * user has entered so much data, there is no data
1040 * available to read(), but the buffers are full anyway.
1042 * Only enter the high watermark if the device driver
1043 * can actually transmit something.
1045 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1048 tty_hiwat_in_block(tp);
1053 * In raw mode, we canonicalize after receiving a single
1054 * character. Otherwise, we canonicalize when we receive a
1055 * newline, VEOL or VEOF, but only when it isn't quoted.
1057 if (!CMP_FLAG(l, ICANON) ||
1058 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1059 ttyinq_canonicalize(&tp->t_inq);
1062 ttydisc_echo(tp, c, quote);
1068 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1072 if (ttydisc_can_bypass(tp))
1073 return (ttydisc_rint_bypass(tp, buf, len));
1075 for (cbuf = buf; len-- > 0; cbuf++) {
1076 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1080 return (cbuf - (const char *)buf);
1084 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1088 tty_lock_assert(tp, MA_OWNED);
1090 MPASS(tp->t_flags & TF_BYPASS);
1092 atomic_add_long(&tty_nin, len);
1094 if (ttyhook_hashook(tp, rint_bypass))
1095 return ttyhook_rint_bypass(tp, buf, len);
1097 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1098 ttyinq_canonicalize(&tp->t_inq);
1100 tty_hiwat_in_block(tp);
1106 ttydisc_rint_done(struct tty *tp)
1109 tty_lock_assert(tp, MA_OWNED);
1111 if (ttyhook_hashook(tp, rint_done))
1112 ttyhook_rint_done(tp);
1114 /* Wake up readers. */
1115 tty_wakeup(tp, FREAD);
1116 /* Wake up driver for echo. */
1117 ttydevsw_outwakeup(tp);
1121 ttydisc_rint_poll(struct tty *tp)
1125 tty_lock_assert(tp, MA_OWNED);
1127 if (ttyhook_hashook(tp, rint_poll))
1128 return ttyhook_rint_poll(tp);
1131 * XXX: Still allow character input when there's no space in the
1132 * buffers, but we haven't entered the high watermark. This is
1133 * to allow backspace characters to be inserted when in
1136 l = ttyinq_bytesleft(&tp->t_inq);
1137 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1144 ttydisc_wakeup_watermark(struct tty *tp)
1148 c = ttyoutq_bytesleft(&tp->t_outq);
1149 if (tp->t_flags & TF_HIWAT_OUT) {
1150 /* Only allow us to run when we're below the watermark. */
1151 if (c < tp->t_outlow)
1154 /* Reset the watermark. */
1155 tp->t_flags &= ~TF_HIWAT_OUT;
1157 /* Only run when we have data at all. */
1161 tty_wakeup(tp, FWRITE);
1165 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1168 tty_lock_assert(tp, MA_OWNED);
1170 if (tp->t_flags & TF_STOPPED)
1173 if (ttyhook_hashook(tp, getc_inject))
1174 return ttyhook_getc_inject(tp, buf, len);
1176 len = ttyoutq_read(&tp->t_outq, buf, len);
1178 if (ttyhook_hashook(tp, getc_capture))
1179 ttyhook_getc_capture(tp, buf, len);
1181 ttydisc_wakeup_watermark(tp);
1182 atomic_add_long(&tty_nout, len);
1188 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1191 ssize_t obytes = uio->uio_resid;
1193 char buf[TTY_STACKBUF];
1195 tty_lock_assert(tp, MA_OWNED);
1197 if (tp->t_flags & TF_STOPPED)
1201 * When a TTY hook is attached, we cannot perform unbuffered
1202 * copying to userspace. Just call ttydisc_getc() and
1203 * temporarily store data in a shadow buffer.
1205 if (ttyhook_hashook(tp, getc_capture) ||
1206 ttyhook_hashook(tp, getc_inject)) {
1207 while (uio->uio_resid > 0) {
1208 /* Read to shadow buffer. */
1209 len = ttydisc_getc(tp, buf,
1210 MIN(uio->uio_resid, sizeof buf));
1214 /* Copy to userspace. */
1216 error = uiomove(buf, len, uio);
1223 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1225 ttydisc_wakeup_watermark(tp);
1226 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1233 ttydisc_getc_poll(struct tty *tp)
1236 tty_lock_assert(tp, MA_OWNED);
1238 if (tp->t_flags & TF_STOPPED)
1241 if (ttyhook_hashook(tp, getc_poll))
1242 return ttyhook_getc_poll(tp);
1244 return ttyoutq_bytesused(&tp->t_outq);
1248 * XXX: not really related to the TTYDISC, but we'd better put
1249 * tty_putchar() here, because we need to perform proper output
1254 tty_putstrn(struct tty *tp, const char *p, size_t n)
1258 tty_lock_assert(tp, MA_OWNED);
1263 for (i = 0; i < n; i++)
1264 ttydisc_echo_force(tp, p[i], 0);
1266 tp->t_writepos = tp->t_column;
1267 ttyinq_reprintpos_set(&tp->t_inq);
1269 ttydevsw_outwakeup(tp);
1274 tty_putchar(struct tty *tp, char c)
1276 return (tty_putstrn(tp, &c, 1));