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>
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);
96 tp->t_termios.c_lflag &= ~FLUSHO;
99 * POSIX states that we must drain output and flush input on
100 * last close. Draining has already been done if possible.
102 tty_flush(tp, FREAD | FWRITE);
104 if (ttyhook_hashook(tp, close))
109 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
111 char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
113 size_t clen, flen = 0, n = 1;
114 unsigned char lastc = _POSIX_VDISABLE;
116 #define BREAK_ADD(c) do { \
117 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
118 breakc[n++] = tp->t_termios.c_cc[c]; \
120 /* Determine which characters we should trigger on. */
127 error = tty_wait_background(tp, curthread, SIGTTIN);
132 * Quite a tricky case: unlike the old TTY
133 * implementation, this implementation copies data back
134 * to userspace in large chunks. Unfortunately, we can't
135 * calculate the line length on beforehand if it crosses
136 * ttyinq_block boundaries, because multiple reads could
137 * then make this code read beyond the newline.
139 * This is why we limit the read to:
140 * - The size the user has requested
141 * - The blocksize (done in tty_inq.c)
142 * - The amount of bytes until the newline
144 * This causes the line length to be recalculated after
145 * each block has been copied to userspace. This will
146 * cause the TTY layer to return data in chunks using
147 * the blocksize (except the first and last blocks).
149 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
154 if (tp->t_flags & TF_ZOMBIE)
156 else if (ioflag & IO_NDELAY)
157 return (EWOULDBLOCK);
159 error = tty_wait(tp, &tp->t_inwait);
165 /* Don't send the EOF char back to userspace. */
166 if (CMP_CC(VEOF, lastc))
171 /* Read and throw away the EOF character. */
172 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
176 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
182 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
184 size_t vmin = tp->t_termios.c_cc[VMIN];
185 ssize_t oresid = uio->uio_resid;
188 MPASS(tp->t_termios.c_cc[VTIME] == 0);
191 * This routine implements the easy cases of read()s while in
192 * non-canonical mode, namely case B and D, where we don't have
197 error = tty_wait_background(tp, curthread, SIGTTIN);
201 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
205 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
208 /* We have to wait for more. */
209 if (tp->t_flags & TF_ZOMBIE)
211 else if (ioflag & IO_NDELAY)
212 return (EWOULDBLOCK);
214 error = tty_wait(tp, &tp->t_inwait);
221 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
224 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
225 unsigned int vtime = tp->t_termios.c_cc[VTIME];
226 struct timeval end, now, left;
229 MPASS(tp->t_termios.c_cc[VTIME] != 0);
231 /* Determine when the read should be expired. */
232 end.tv_sec = vtime / 10;
233 end.tv_usec = (vtime % 10) * 100000;
235 timevaladd(&end, &now);
238 error = tty_wait_background(tp, curthread, SIGTTIN);
242 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
246 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
249 /* Calculate how long we should wait. */
251 if (timevalcmp(&now, &end, >))
254 timevalsub(&left, &now);
258 * We have to wait for more. If the timer expires, we
259 * should return a 0-byte read.
261 if (tp->t_flags & TF_ZOMBIE)
263 else if (ioflag & IO_NDELAY)
264 return (EWOULDBLOCK);
266 error = tty_timedwait(tp, &tp->t_inwait, hz);
268 return (error == EWOULDBLOCK ? 0 : error);
275 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
277 size_t vmin = tp->t_termios.c_cc[VMIN];
278 ssize_t oresid = uio->uio_resid;
281 MPASS(tp->t_termios.c_cc[VMIN] != 0);
282 MPASS(tp->t_termios.c_cc[VTIME] != 0);
285 * When using the interbyte timer, the timer should be started
286 * after the first byte has been received. We just call into the
287 * generic read timer code after we've received the first byte.
291 error = tty_wait_background(tp, curthread, SIGTTIN);
295 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
299 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
303 * Not enough data, but we did receive some, which means
304 * we'll now start using the interbyte timer.
306 if (oresid != uio->uio_resid)
309 /* We have to wait for more. */
310 if (tp->t_flags & TF_ZOMBIE)
312 else if (ioflag & IO_NDELAY)
313 return (EWOULDBLOCK);
315 error = tty_wait(tp, &tp->t_inwait);
320 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
324 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
328 tty_assert_locked(tp);
330 if (uio->uio_resid == 0)
333 if (CMP_FLAG(l, ICANON))
334 error = ttydisc_read_canonical(tp, uio, ioflag);
335 else if (tp->t_termios.c_cc[VTIME] == 0)
336 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
337 else if (tp->t_termios.c_cc[VMIN] == 0)
338 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
341 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
343 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
344 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
345 /* Unset the input watermark when we've got enough space. */
346 tty_hiwat_in_unblock(tp);
352 static __inline unsigned int
353 ttydisc_findchar(const char *obstart, unsigned int oblen)
355 const char *c = obstart;
363 return (c - obstart);
367 ttydisc_write_oproc(struct tty *tp, char c)
369 unsigned int scnt, error;
371 MPASS(CMP_FLAG(o, OPOST));
374 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
377 /* End-of-text dropping. */
378 if (CMP_FLAG(o, ONOEOT))
380 return PRINT_NORMAL();
383 /* Handle backspace to fix tab expansion. */
384 if (PRINT_NORMAL() != 0)
386 if (tp->t_column > 0)
392 scnt = 8 - (tp->t_column & 7);
393 if (CMP_FLAG(o, TAB3)) {
394 error = ttyoutq_write_nofrag(&tp->t_outq,
397 error = PRINT_NORMAL();
402 tp->t_column += scnt;
403 MPASS((tp->t_column % 8) == 0);
407 /* Newline conversion. */
408 if (CMP_FLAG(o, ONLCR)) {
409 /* Convert \n to \r\n. */
410 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
412 error = PRINT_NORMAL();
417 if (CMP_FLAG(o, ONLCR|ONLRET)) {
418 tp->t_column = tp->t_writepos = 0;
419 ttyinq_reprintpos_set(&tp->t_inq);
424 /* Carriage return to newline conversion. */
425 if (CMP_FLAG(o, OCRNL))
427 /* Omit carriage returns on column 0. */
428 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
430 if (PRINT_NORMAL() != 0)
433 tp->t_column = tp->t_writepos = 0;
434 ttyinq_reprintpos_set(&tp->t_inq);
439 * Invisible control character. Print it, but don't
440 * increase the column count.
442 return PRINT_NORMAL();
447 * Just like the old TTY implementation, we need to copy data in chunks
448 * into a temporary buffer. One of the reasons why we need to do this,
449 * is because output processing (only TAB3 though) may allow the buffer
450 * to grow eight times.
453 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
455 char ob[TTY_STACKBUF];
458 unsigned int oblen = 0;
460 tty_assert_locked(tp);
462 if (tp->t_flags & TF_ZOMBIE)
466 * We don't need to check whether the process is the foreground
467 * process group or if we have a carrier. This is already done
471 while (uio->uio_resid > 0) {
476 if (CMP_FLAG(l, FLUSHO)) {
477 uio->uio_offset += uio->uio_resid;
482 /* Step 1: read data. */
484 nlen = MIN(uio->uio_resid, sizeof ob);
486 error = uiomove(ob, nlen, uio);
499 /* Step 2: process data. */
501 unsigned int plen, wlen;
503 if (CMP_FLAG(l, FLUSHO)) {
504 uio->uio_offset += uio->uio_resid;
509 /* Search for special characters for post processing. */
510 if (CMP_FLAG(o, OPOST)) {
511 plen = ttydisc_findchar(obstart, oblen);
518 * We're going to process a character
519 * that needs processing
521 if (ttydisc_write_oproc(tp, *obstart) == 0) {
525 tp->t_writepos = tp->t_column;
526 ttyinq_reprintpos_set(&tp->t_inq);
530 /* We're going to write regular data. */
531 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
534 tp->t_column += wlen;
536 tp->t_writepos = tp->t_column;
537 ttyinq_reprintpos_set(&tp->t_inq);
543 /* Watermark reached. Try to sleep. */
544 tp->t_flags |= TF_HIWAT_OUT;
546 if (ioflag & IO_NDELAY) {
552 * The driver may write back the data
553 * synchronously. Be sure to check the high
554 * water mark before going to sleep.
556 ttydevsw_outwakeup(tp);
557 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
560 error = tty_wait(tp, &tp->t_outwait);
564 if (tp->t_flags & TF_ZOMBIE) {
573 ttydevsw_outwakeup(tp);
576 * Add the amount of bytes that we didn't process back to the
577 * uio counters. We need to do this to make sure write() doesn't
578 * count the bytes we didn't store in the queue.
580 uio->uio_resid += oblen;
585 ttydisc_optimize(struct tty *tp)
587 tty_assert_locked(tp);
589 if (ttyhook_hashook(tp, rint_bypass)) {
590 tp->t_flags |= TF_BYPASS;
591 } else if (ttyhook_hashook(tp, rint)) {
592 tp->t_flags &= ~TF_BYPASS;
593 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
594 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
595 (!CMP_FLAG(i, PARMRK) ||
596 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
597 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
598 tp->t_flags |= TF_BYPASS;
600 tp->t_flags &= ~TF_BYPASS;
605 ttydisc_modem(struct tty *tp, int open)
608 tty_assert_locked(tp);
611 cv_broadcast(&tp->t_dcdwait);
614 * Ignore modem status lines when CLOCAL is turned on, but don't
615 * enter the zombie state when the TTY isn't opened, because
616 * that would cause the TTY to be in zombie state after being
619 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
626 tp->t_flags |= TF_ZOMBIE;
628 tty_signal_sessleader(tp, SIGHUP);
629 tty_flush(tp, FREAD|FWRITE);
632 * Carrier is back again.
635 /* XXX: what should we do here? */
640 ttydisc_echo_force(struct tty *tp, char c, int quote)
643 if (CMP_FLAG(l, FLUSHO))
646 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
648 * Only perform postprocessing when OPOST is turned on
649 * and the character is an unquoted BS/TB/NL/CR.
651 return ttydisc_write_oproc(tp, c);
652 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
654 * Only use ^X notation when ECHOCTL is turned on and
655 * we've got an quoted control character.
657 * Print backspaces when echoing an end-of-file.
659 char ob[4] = "^?\b\b";
661 /* Print ^X notation. */
665 if (!quote && CMP_CC(VEOF, c)) {
666 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
669 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
672 /* Can just be printed. */
674 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
679 ttydisc_echo(struct tty *tp, char c, int quote)
683 * Only echo characters when ECHO is turned on, or ECHONL when
684 * the character is an unquoted newline.
686 if (!CMP_FLAG(l, ECHO) &&
687 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
690 return ttydisc_echo_force(tp, c, quote);
694 ttydisc_reprint_char(void *d, char c, int quote)
698 ttydisc_echo(tp, c, quote);
702 ttydisc_reprint(struct tty *tp)
706 /* Print ^R\n, followed by the line. */
707 c = tp->t_termios.c_cc[VREPRINT];
708 if (c != _POSIX_VDISABLE)
709 ttydisc_echo(tp, c, 0);
710 ttydisc_echo(tp, CNL, 0);
711 ttyinq_reprintpos_reset(&tp->t_inq);
713 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
716 struct ttydisc_recalc_length {
722 ttydisc_recalc_charlength(void *d, char c, int quote)
724 struct ttydisc_recalc_length *data = d;
725 struct tty *tp = data->tp;
727 if (CTL_PRINT(c, quote)) {
728 if (CMP_FLAG(l, ECHOCTL))
730 } else if (c == CTAB) {
731 data->curlen += 8 - (data->curlen & 7);
738 ttydisc_recalc_linelength(struct tty *tp)
740 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
742 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
743 ttydisc_recalc_charlength, &data);
744 return (data.curlen);
748 ttydisc_rubchar(struct tty *tp)
752 unsigned int prevpos, tablen;
754 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
756 ttyinq_unputchar(&tp->t_inq);
758 if (CMP_FLAG(l, ECHO)) {
760 * Remove the character from the screen. This is even
761 * safe for characters that span multiple characters
762 * (tabs, quoted, etc).
764 if (tp->t_writepos >= tp->t_column) {
765 /* Retype the sentence. */
767 } else if (CMP_FLAG(l, ECHOE)) {
768 if (CTL_PRINT(c, quote)) {
769 /* Remove ^X formatted chars. */
770 if (CMP_FLAG(l, ECHOCTL)) {
772 ttyoutq_write_nofrag(&tp->t_outq,
775 } else if (c == ' ') {
776 /* Space character needs no rubbing. */
778 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
779 } else if (c == CTAB) {
781 * Making backspace work with tabs is
782 * quite hard. Recalculate the length of
783 * this character and remove it.
785 * Because terminal settings could be
786 * changed while the line is being
787 * inserted, the calculations don't have
788 * to be correct. Make sure we keep the
789 * tab length within proper bounds.
791 prevpos = ttydisc_recalc_linelength(tp);
792 if (prevpos >= tp->t_column)
795 tablen = tp->t_column - prevpos;
799 tp->t_column = prevpos;
800 ttyoutq_write_nofrag(&tp->t_outq,
801 "\b\b\b\b\b\b\b\b", tablen);
805 * Remove a regular character by
806 * punching a space over it.
809 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
812 /* Don't print spaces. */
813 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
821 ttydisc_rubword(struct tty *tp)
826 /* Strip whitespace first. */
828 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
836 * Record whether the last character from the previous iteration
837 * was alphanumeric or not. We need this to implement ALTWERASE.
839 alnum = CTL_ALNUM(c);
843 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
847 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
853 ttydisc_rint(struct tty *tp, char c, int flags)
855 int signal, quote = 0;
856 char ob[3] = { 0xff, 0x00 };
859 tty_assert_locked(tp);
861 atomic_add_long(&tty_nin, 1);
863 if (ttyhook_hashook(tp, rint))
864 return ttyhook_rint(tp, c, flags);
866 if (tp->t_flags & TF_BYPASS)
870 if (flags & TRE_BREAK) {
871 if (CMP_FLAG(i, IGNBRK)) {
872 /* Ignore break characters. */
874 } else if (CMP_FLAG(i, BRKINT)) {
875 /* Generate SIGINT on break. */
876 tty_flush(tp, FREAD|FWRITE);
877 tty_signal_pgrp(tp, SIGINT);
883 } else if (flags & TRE_FRAMING ||
884 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
885 if (CMP_FLAG(i, IGNPAR)) {
886 /* Ignore bad characters. */
895 /* Allow any character to perform a wakeup. */
896 if (CMP_FLAG(i, IXANY)) {
897 tp->t_flags &= ~TF_STOPPED;
898 tp->t_termios.c_lflag &= ~FLUSHO;
901 /* Remove the top bit. */
902 if (CMP_FLAG(i, ISTRIP))
905 /* Skip input processing when we want to print it literally. */
906 if (tp->t_flags & TF_LITERAL) {
907 tp->t_flags &= ~TF_LITERAL;
912 /* Special control characters that are implementation dependent. */
913 if (CMP_FLAG(l, IEXTEN)) {
914 /* Accept the next character as literal. */
915 if (CMP_CC(VLNEXT, c)) {
916 if (CMP_FLAG(l, ECHO)) {
917 if (CMP_FLAG(l, ECHOE))
918 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
920 ttydisc_echo(tp, c, 0);
922 tp->t_flags |= TF_LITERAL;
925 /* Discard processing */
926 if (CMP_CC(VDISCARD, c)) {
927 if (CMP_FLAG(l, FLUSHO)) {
928 tp->t_termios.c_lflag &= ~FLUSHO;
930 tty_flush(tp, FWRITE);
931 ttydisc_echo(tp, c, 0);
932 if (tp->t_inq.ti_end > 0)
934 tp->t_termios.c_lflag |= FLUSHO;
940 * Handle signal processing.
942 if (CMP_FLAG(l, ISIG)) {
943 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
944 if (CMP_CC(VSTATUS, c)) {
945 tty_signal_pgrp(tp, SIGINFO);
951 * When compared to the old implementation, this
952 * implementation also flushes the output queue. POSIX
953 * is really brief about this, but does makes us assume
957 if (CMP_CC(VINTR, c)) {
959 } else if (CMP_CC(VQUIT, c)) {
961 } else if (CMP_CC(VSUSP, c)) {
967 * Echo the character before signalling the
970 if (!CMP_FLAG(l, NOFLSH))
971 tty_flush(tp, FREAD|FWRITE);
972 ttydisc_echo(tp, c, 0);
973 tty_signal_pgrp(tp, signal);
979 * Handle start/stop characters.
981 if (CMP_FLAG(i, IXON)) {
982 if (CMP_CC(VSTOP, c)) {
983 /* Stop it if we aren't stopped yet. */
984 if ((tp->t_flags & TF_STOPPED) == 0) {
985 tp->t_flags |= TF_STOPPED;
990 * When VSTART == VSTOP, we should make this key
993 if (!CMP_CC(VSTART, c))
996 if (CMP_CC(VSTART, c)) {
997 tp->t_flags &= ~TF_STOPPED;
1002 /* Conversion of CR and NL. */
1005 if (CMP_FLAG(i, IGNCR))
1007 if (CMP_FLAG(i, ICRNL))
1011 if (CMP_FLAG(i, INLCR))
1016 /* Canonical line editing. */
1017 if (CMP_FLAG(l, ICANON)) {
1018 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1019 ttydisc_rubchar(tp);
1021 } else if (CMP_CC(VKILL, c)) {
1022 while (ttydisc_rubchar(tp) == 0);
1024 } else if (CMP_FLAG(l, IEXTEN)) {
1025 if (CMP_CC(VWERASE, c)) {
1026 ttydisc_rubword(tp);
1028 } else if (CMP_CC(VREPRINT, c)) {
1029 ttydisc_reprint(tp);
1036 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1037 /* Print 0xff 0xff. */
1049 if (CMP_FLAG(i, PARMRK)) {
1050 /* Prepend 0xff 0x00 0x.. */
1060 /* See if we can store this on the input queue. */
1061 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1062 if (CMP_FLAG(i, IMAXBEL))
1063 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1066 * Prevent a deadlock here. It may be possible that a
1067 * user has entered so much data, there is no data
1068 * available to read(), but the buffers are full anyway.
1070 * Only enter the high watermark if the device driver
1071 * can actually transmit something.
1073 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1076 tty_hiwat_in_block(tp);
1081 * In raw mode, we canonicalize after receiving a single
1082 * character. Otherwise, we canonicalize when we receive a
1083 * newline, VEOL or VEOF, but only when it isn't quoted.
1085 if (!CMP_FLAG(l, ICANON) ||
1086 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1087 ttyinq_canonicalize(&tp->t_inq);
1090 ttydisc_echo(tp, c, quote);
1096 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1100 if (ttydisc_can_bypass(tp))
1101 return (ttydisc_rint_bypass(tp, buf, len));
1103 for (cbuf = buf; len-- > 0; cbuf++) {
1104 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1108 return (cbuf - (const char *)buf);
1112 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1116 tty_assert_locked(tp);
1118 MPASS(tp->t_flags & TF_BYPASS);
1120 atomic_add_long(&tty_nin, len);
1122 if (ttyhook_hashook(tp, rint_bypass))
1123 return ttyhook_rint_bypass(tp, buf, len);
1125 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1126 ttyinq_canonicalize(&tp->t_inq);
1128 tty_hiwat_in_block(tp);
1134 ttydisc_rint_done(struct tty *tp)
1137 tty_assert_locked(tp);
1139 if (ttyhook_hashook(tp, rint_done))
1140 ttyhook_rint_done(tp);
1142 /* Wake up readers. */
1143 tty_wakeup(tp, FREAD);
1144 /* Wake up driver for echo. */
1145 ttydevsw_outwakeup(tp);
1149 ttydisc_rint_poll(struct tty *tp)
1153 tty_assert_locked(tp);
1155 if (ttyhook_hashook(tp, rint_poll))
1156 return ttyhook_rint_poll(tp);
1159 * XXX: Still allow character input when there's no space in the
1160 * buffers, but we haven't entered the high watermark. This is
1161 * to allow backspace characters to be inserted when in
1164 l = ttyinq_bytesleft(&tp->t_inq);
1165 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1172 ttydisc_wakeup_watermark(struct tty *tp)
1176 c = ttyoutq_bytesleft(&tp->t_outq);
1177 if (tp->t_flags & TF_HIWAT_OUT) {
1178 /* Only allow us to run when we're below the watermark. */
1179 if (c < tp->t_outlow)
1182 /* Reset the watermark. */
1183 tp->t_flags &= ~TF_HIWAT_OUT;
1185 /* Only run when we have data at all. */
1189 tty_wakeup(tp, FWRITE);
1193 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1196 tty_assert_locked(tp);
1198 if (tp->t_flags & TF_STOPPED)
1201 if (ttyhook_hashook(tp, getc_inject))
1202 return ttyhook_getc_inject(tp, buf, len);
1204 len = ttyoutq_read(&tp->t_outq, buf, len);
1206 if (ttyhook_hashook(tp, getc_capture))
1207 ttyhook_getc_capture(tp, buf, len);
1209 ttydisc_wakeup_watermark(tp);
1210 atomic_add_long(&tty_nout, len);
1216 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1219 ssize_t obytes = uio->uio_resid;
1221 char buf[TTY_STACKBUF];
1223 tty_assert_locked(tp);
1225 if (tp->t_flags & TF_STOPPED)
1229 * When a TTY hook is attached, we cannot perform unbuffered
1230 * copying to userspace. Just call ttydisc_getc() and
1231 * temporarily store data in a shadow buffer.
1233 if (ttyhook_hashook(tp, getc_capture) ||
1234 ttyhook_hashook(tp, getc_inject)) {
1235 while (uio->uio_resid > 0) {
1236 /* Read to shadow buffer. */
1237 len = ttydisc_getc(tp, buf,
1238 MIN(uio->uio_resid, sizeof buf));
1242 /* Copy to userspace. */
1244 error = uiomove(buf, len, uio);
1251 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1253 ttydisc_wakeup_watermark(tp);
1254 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1261 ttydisc_getc_poll(struct tty *tp)
1264 tty_assert_locked(tp);
1266 if (tp->t_flags & TF_STOPPED)
1269 if (ttyhook_hashook(tp, getc_poll))
1270 return ttyhook_getc_poll(tp);
1272 return ttyoutq_bytesused(&tp->t_outq);
1276 * XXX: not really related to the TTYDISC, but we'd better put
1277 * tty_putchar() here, because we need to perform proper output
1282 tty_putstrn(struct tty *tp, const char *p, size_t n)
1286 tty_assert_locked(tp);
1291 for (i = 0; i < n; i++)
1292 ttydisc_echo_force(tp, p[i], 0);
1294 tp->t_writepos = tp->t_column;
1295 ttyinq_reprintpos_set(&tp->t_inq);
1297 ttydevsw_outwakeup(tp);
1302 tty_putchar(struct tty *tp, char c)
1304 return (tty_putstrn(tp, &c, 1));