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/param.h>
33 #include <sys/fcntl.h>
34 #include <sys/filio.h>
35 #include <sys/kernel.h>
36 #include <sys/signal.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
40 #include <sys/ttycom.h>
41 #include <sys/ttydefaults.h>
43 #include <sys/vnode.h>
45 #include <teken/teken.h>
46 #include <teken/teken_wcwidth.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'))
83 /* Character is UTF8-encoded. */
84 #define CTL_UTF8(c) (!!((c) & 0x80))
85 /* Character is a UTF8 continuation byte. */
86 #define CTL_UTF8_CONT(c) (((c) & 0xc0) == 0x80)
88 #define TTY_STACKBUF 256
89 #define UTF8_STACKBUF 4
92 ttydisc_open(struct tty *tp)
98 ttydisc_close(struct tty *tp)
101 /* Clean up our flags when leaving the discipline. */
102 tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
103 tp->t_termios.c_lflag &= ~FLUSHO;
106 * POSIX states that we must drain output and flush input on
107 * last close. Draining has already been done if possible.
109 tty_flush(tp, FREAD | FWRITE);
111 if (ttyhook_hashook(tp, close))
116 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
118 char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */
120 size_t clen, flen = 0, n = 1;
121 unsigned char lastc = _POSIX_VDISABLE;
123 #define BREAK_ADD(c) do { \
124 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
125 breakc[n++] = tp->t_termios.c_cc[c]; \
127 /* Determine which characters we should trigger on. */
134 error = tty_wait_background(tp, curthread, SIGTTIN);
139 * Quite a tricky case: unlike the old TTY
140 * implementation, this implementation copies data back
141 * to userspace in large chunks. Unfortunately, we can't
142 * calculate the line length on beforehand if it crosses
143 * ttyinq_block boundaries, because multiple reads could
144 * then make this code read beyond the newline.
146 * This is why we limit the read to:
147 * - The size the user has requested
148 * - The blocksize (done in tty_inq.c)
149 * - The amount of bytes until the newline
151 * This causes the line length to be recalculated after
152 * each block has been copied to userspace. This will
153 * cause the TTY layer to return data in chunks using
154 * the blocksize (except the first and last blocks).
156 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid,
161 if (tp->t_flags & TF_ZOMBIE)
163 else if (ioflag & IO_NDELAY)
164 return (EWOULDBLOCK);
166 error = tty_wait(tp, &tp->t_inwait);
172 /* Don't send the EOF char back to userspace. */
173 if (CMP_CC(VEOF, lastc))
178 /* Read and throw away the EOF character. */
179 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
183 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
189 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
191 size_t vmin = tp->t_termios.c_cc[VMIN];
192 ssize_t oresid = uio->uio_resid;
195 MPASS(tp->t_termios.c_cc[VTIME] == 0);
198 * This routine implements the easy cases of read()s while in
199 * non-canonical mode, namely case B and D, where we don't have
204 error = tty_wait_background(tp, curthread, SIGTTIN);
208 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
212 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
215 /* We have to wait for more. */
216 if (tp->t_flags & TF_ZOMBIE)
218 else if (ioflag & IO_NDELAY)
219 return (EWOULDBLOCK);
221 error = tty_wait(tp, &tp->t_inwait);
228 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
231 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
232 unsigned int vtime = tp->t_termios.c_cc[VTIME];
233 struct timeval end, now, left;
236 MPASS(tp->t_termios.c_cc[VTIME] != 0);
238 /* Determine when the read should be expired. */
239 end.tv_sec = vtime / 10;
240 end.tv_usec = (vtime % 10) * 100000;
242 timevaladd(&end, &now);
245 error = tty_wait_background(tp, curthread, SIGTTIN);
249 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
253 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
256 /* Calculate how long we should wait. */
258 if (timevalcmp(&now, &end, >))
261 timevalsub(&left, &now);
265 * We have to wait for more. If the timer expires, we
266 * should return a 0-byte read.
268 if (tp->t_flags & TF_ZOMBIE)
270 else if (ioflag & IO_NDELAY)
271 return (EWOULDBLOCK);
273 error = tty_timedwait(tp, &tp->t_inwait, hz);
275 return (error == EWOULDBLOCK ? 0 : error);
282 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
284 size_t vmin = tp->t_termios.c_cc[VMIN];
285 ssize_t oresid = uio->uio_resid;
288 MPASS(tp->t_termios.c_cc[VMIN] != 0);
289 MPASS(tp->t_termios.c_cc[VTIME] != 0);
292 * When using the interbyte timer, the timer should be started
293 * after the first byte has been received. We just call into the
294 * generic read timer code after we've received the first byte.
298 error = tty_wait_background(tp, curthread, SIGTTIN);
302 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
306 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
310 * Not enough data, but we did receive some, which means
311 * we'll now start using the interbyte timer.
313 if (oresid != uio->uio_resid)
316 /* We have to wait for more. */
317 if (tp->t_flags & TF_ZOMBIE)
319 else if (ioflag & IO_NDELAY)
320 return (EWOULDBLOCK);
322 error = tty_wait(tp, &tp->t_inwait);
327 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
331 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
335 tty_assert_locked(tp);
337 if (uio->uio_resid == 0)
340 if (CMP_FLAG(l, ICANON))
341 error = ttydisc_read_canonical(tp, uio, ioflag);
342 else if (tp->t_termios.c_cc[VTIME] == 0)
343 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
344 else if (tp->t_termios.c_cc[VMIN] == 0)
345 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
348 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
350 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
351 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
352 /* Unset the input watermark when we've got enough space. */
353 tty_hiwat_in_unblock(tp);
359 static __inline unsigned int
360 ttydisc_findchar(const char *obstart, unsigned int oblen)
362 const char *c = obstart;
370 return (c - obstart);
374 ttydisc_write_oproc(struct tty *tp, char c)
376 unsigned int scnt, error;
378 MPASS(CMP_FLAG(o, OPOST));
381 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
384 /* End-of-text dropping. */
385 if (CMP_FLAG(o, ONOEOT))
387 return PRINT_NORMAL();
390 /* Handle backspace to fix tab expansion. */
391 if (PRINT_NORMAL() != 0)
393 if (tp->t_column > 0)
399 scnt = 8 - (tp->t_column & 7);
400 if (CMP_FLAG(o, TAB3)) {
401 error = ttyoutq_write_nofrag(&tp->t_outq,
404 error = PRINT_NORMAL();
409 tp->t_column += scnt;
410 MPASS((tp->t_column % 8) == 0);
414 /* Newline conversion. */
415 if (CMP_FLAG(o, ONLCR)) {
416 /* Convert \n to \r\n. */
417 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
419 error = PRINT_NORMAL();
424 if (CMP_FLAG(o, ONLCR|ONLRET)) {
425 tp->t_column = tp->t_writepos = 0;
426 ttyinq_reprintpos_set(&tp->t_inq);
431 /* Carriage return to newline conversion. */
432 if (CMP_FLAG(o, OCRNL))
434 /* Omit carriage returns on column 0. */
435 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
437 if (PRINT_NORMAL() != 0)
440 tp->t_column = tp->t_writepos = 0;
441 ttyinq_reprintpos_set(&tp->t_inq);
446 * Invisible control character. Print it, but don't
447 * increase the column count.
449 return PRINT_NORMAL();
454 * Just like the old TTY implementation, we need to copy data in chunks
455 * into a temporary buffer. One of the reasons why we need to do this,
456 * is because output processing (only TAB3 though) may allow the buffer
457 * to grow eight times.
460 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
462 char ob[TTY_STACKBUF];
465 unsigned int oblen = 0;
467 tty_assert_locked(tp);
469 if (tp->t_flags & TF_ZOMBIE)
473 * We don't need to check whether the process is the foreground
474 * process group or if we have a carrier. This is already done
478 while (uio->uio_resid > 0) {
483 if (CMP_FLAG(l, FLUSHO)) {
484 uio->uio_offset += uio->uio_resid;
489 /* Step 1: read data. */
491 nlen = MIN(uio->uio_resid, sizeof ob);
493 error = uiomove(ob, nlen, uio);
506 /* Step 2: process data. */
508 unsigned int plen, wlen;
510 if (CMP_FLAG(l, FLUSHO)) {
511 uio->uio_offset += uio->uio_resid;
516 /* Search for special characters for post processing. */
517 if (CMP_FLAG(o, OPOST)) {
518 plen = ttydisc_findchar(obstart, oblen);
525 * We're going to process a character
526 * that needs processing
528 if (ttydisc_write_oproc(tp, *obstart) == 0) {
532 tp->t_writepos = tp->t_column;
533 ttyinq_reprintpos_set(&tp->t_inq);
537 /* We're going to write regular data. */
538 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
541 tp->t_column += wlen;
543 tp->t_writepos = tp->t_column;
544 ttyinq_reprintpos_set(&tp->t_inq);
550 /* Watermark reached. Try to sleep. */
551 tp->t_flags |= TF_HIWAT_OUT;
553 if (ioflag & IO_NDELAY) {
559 * The driver may write back the data
560 * synchronously. Be sure to check the high
561 * water mark before going to sleep.
563 ttydevsw_outwakeup(tp);
564 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
567 error = tty_wait(tp, &tp->t_outwait);
571 if (tp->t_flags & TF_ZOMBIE) {
580 ttydevsw_outwakeup(tp);
583 * Add the amount of bytes that we didn't process back to the
584 * uio counters. We need to do this to make sure write() doesn't
585 * count the bytes we didn't store in the queue.
587 uio->uio_resid += oblen;
592 ttydisc_optimize(struct tty *tp)
594 tty_assert_locked(tp);
596 if (ttyhook_hashook(tp, rint_bypass)) {
597 tp->t_flags |= TF_BYPASS;
598 } else if (ttyhook_hashook(tp, rint)) {
599 tp->t_flags &= ~TF_BYPASS;
600 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
601 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
602 (!CMP_FLAG(i, PARMRK) ||
603 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
604 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
605 tp->t_flags |= TF_BYPASS;
607 tp->t_flags &= ~TF_BYPASS;
612 ttydisc_modem(struct tty *tp, int open)
615 tty_assert_locked(tp);
618 cv_broadcast(&tp->t_dcdwait);
621 * Ignore modem status lines when CLOCAL is turned on, but don't
622 * enter the zombie state when the TTY isn't opened, because
623 * that would cause the TTY to be in zombie state after being
626 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
633 tp->t_flags |= TF_ZOMBIE;
635 tty_signal_sessleader(tp, SIGHUP);
636 tty_flush(tp, FREAD|FWRITE);
639 * Carrier is back again.
642 /* XXX: what should we do here? */
647 ttydisc_echo_force(struct tty *tp, char c, int quote)
650 if (CMP_FLAG(l, FLUSHO))
653 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
655 * Only perform postprocessing when OPOST is turned on
656 * and the character is an unquoted BS/TB/NL/CR.
658 return ttydisc_write_oproc(tp, c);
659 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
661 * Only use ^X notation when ECHOCTL is turned on and
662 * we've got an quoted control character.
664 * Print backspaces when echoing an end-of-file.
666 char ob[4] = "^?\b\b";
668 /* Print ^X notation. */
672 if (!quote && CMP_CC(VEOF, c)) {
673 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
676 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
679 /* Can just be printed. */
681 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
686 ttydisc_echo(struct tty *tp, char c, int quote)
690 * Only echo characters when ECHO is turned on, or ECHONL when
691 * the character is an unquoted newline.
693 if (!CMP_FLAG(l, ECHO) &&
694 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
697 return ttydisc_echo_force(tp, c, quote);
701 ttydisc_reprint_char(void *d, char c, int quote)
705 ttydisc_echo(tp, c, quote);
709 ttydisc_reprint(struct tty *tp)
713 /* Print ^R\n, followed by the line. */
714 c = tp->t_termios.c_cc[VREPRINT];
715 if (c != _POSIX_VDISABLE)
716 ttydisc_echo(tp, c, 0);
717 ttydisc_echo(tp, CNL, 0);
718 ttyinq_reprintpos_reset(&tp->t_inq);
720 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
723 struct ttydisc_recalc_length {
729 ttydisc_recalc_charlength(void *d, char c, int quote)
731 struct ttydisc_recalc_length *data = d;
732 struct tty *tp = data->tp;
734 if (CTL_PRINT(c, quote)) {
735 if (CMP_FLAG(l, ECHOCTL))
737 } else if (c == CTAB) {
738 data->curlen += 8 - (data->curlen & 7);
745 ttydisc_recalc_linelength(struct tty *tp)
747 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
749 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
750 ttydisc_recalc_charlength, &data);
751 return (data.curlen);
755 ttydisc_rubchar(struct tty *tp)
759 unsigned int prevpos, tablen;
761 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
763 ttyinq_unputchar(&tp->t_inq);
765 if (CMP_FLAG(l, ECHO)) {
767 * Remove the character from the screen. This is even
768 * safe for characters that span multiple characters
769 * (tabs, quoted, etc).
771 if (tp->t_writepos >= tp->t_column) {
772 /* Retype the sentence. */
774 } else if (CMP_FLAG(l, ECHOE)) {
775 if (CTL_PRINT(c, quote)) {
776 /* Remove ^X formatted chars. */
777 if (CMP_FLAG(l, ECHOCTL)) {
779 ttyoutq_write_nofrag(&tp->t_outq,
782 } else if (c == ' ') {
783 /* Space character needs no rubbing. */
785 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
786 } else if (c == CTAB) {
788 * Making backspace work with tabs is
789 * quite hard. Recalculate the length of
790 * this character and remove it.
792 * Because terminal settings could be
793 * changed while the line is being
794 * inserted, the calculations don't have
795 * to be correct. Make sure we keep the
796 * tab length within proper bounds.
798 prevpos = ttydisc_recalc_linelength(tp);
799 if (prevpos >= tp->t_column)
802 tablen = tp->t_column - prevpos;
806 tp->t_column = prevpos;
807 ttyoutq_write_nofrag(&tp->t_outq,
808 "\b\b\b\b\b\b\b\b", tablen);
810 } else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
812 uint8_t bytes[UTF8_STACKBUF] = { 0 };
813 int curidx = UTF8_STACKBUF - 1, cwidth = 1,
815 teken_char_t codepoint;
817 /* Save current byte. */
821 /* Loop back through inq until we hit the
823 while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
825 * Check if we've reached the beginning
828 if (ttyinq_peekchar(&tp->t_inq, &c,
831 ttyinq_unputchar(&tp->t_inq);
837 * Shift array so that the leading
838 * byte ends up at idx 0.
840 if (nb < UTF8_STACKBUF)
841 memmove(&bytes[0], &bytes[curidx + 1],
842 nb * sizeof(uint8_t));
843 /* Check for malformed UTF8 characters. */
844 if (nb == UTF8_STACKBUF &&
845 CTL_UTF8_CONT(bytes[0])) {
847 * Place all bytes back into the inq and
848 * delete the last byte only.
850 ttyinq_write(&tp->t_inq, bytes,
852 ttyinq_unputchar(&tp->t_inq);
854 /* Find codepoint and width. */
856 teken_utf8_bytes_to_codepoint(bytes,
859 TEKEN_UTF8_INVALID_CODEPOINT ||
860 (cwidth = teken_wcwidth(
863 * Place all bytes back into the
864 * inq and fall back to
868 ttyinq_write(&tp->t_inq, bytes,
870 ttyinq_unputchar(&tp->t_inq);
873 tp->t_column -= cwidth;
875 * Delete character by punching
876 * 'cwidth' spaces over it.
879 ttyoutq_write_nofrag(&tp->t_outq,
881 else if (cwidth == 2)
882 ttyoutq_write_nofrag(&tp->t_outq,
886 * Remove a regular character by
887 * punching a space over it.
890 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
893 /* Don't print spaces. */
894 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
902 ttydisc_rubword(struct tty *tp)
907 /* Strip whitespace first. */
909 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
917 * Record whether the last character from the previous iteration
918 * was alphanumeric or not. We need this to implement ALTWERASE.
920 alnum = CTL_ALNUM(c);
924 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
928 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
934 ttydisc_rint(struct tty *tp, char c, int flags)
936 int signal, quote = 0;
937 char ob[3] = { 0xff, 0x00 };
940 tty_assert_locked(tp);
942 atomic_add_long(&tty_nin, 1);
944 if (ttyhook_hashook(tp, rint))
945 return ttyhook_rint(tp, c, flags);
947 if (tp->t_flags & TF_BYPASS)
951 if (flags & TRE_BREAK) {
952 if (CMP_FLAG(i, IGNBRK)) {
953 /* Ignore break characters. */
955 } else if (CMP_FLAG(i, BRKINT)) {
956 /* Generate SIGINT on break. */
957 tty_flush(tp, FREAD|FWRITE);
958 tty_signal_pgrp(tp, SIGINT);
964 } else if (flags & TRE_FRAMING ||
965 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
966 if (CMP_FLAG(i, IGNPAR)) {
967 /* Ignore bad characters. */
976 /* Allow any character to perform a wakeup. */
977 if (CMP_FLAG(i, IXANY)) {
978 tp->t_flags &= ~TF_STOPPED;
979 tp->t_termios.c_lflag &= ~FLUSHO;
982 /* Remove the top bit. */
983 if (CMP_FLAG(i, ISTRIP))
986 /* Skip input processing when we want to print it literally. */
987 if (tp->t_flags & TF_LITERAL) {
988 tp->t_flags &= ~TF_LITERAL;
993 /* Special control characters that are implementation dependent. */
994 if (CMP_FLAG(l, IEXTEN)) {
995 /* Accept the next character as literal. */
996 if (CMP_CC(VLNEXT, c)) {
997 if (CMP_FLAG(l, ECHO)) {
998 if (CMP_FLAG(l, ECHOE))
999 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
1001 ttydisc_echo(tp, c, 0);
1003 tp->t_flags |= TF_LITERAL;
1006 /* Discard processing */
1007 if (CMP_CC(VDISCARD, c)) {
1008 if (CMP_FLAG(l, FLUSHO)) {
1009 tp->t_termios.c_lflag &= ~FLUSHO;
1011 tty_flush(tp, FWRITE);
1012 ttydisc_echo(tp, c, 0);
1013 if (tp->t_inq.ti_end > 0)
1014 ttydisc_reprint(tp);
1015 tp->t_termios.c_lflag |= FLUSHO;
1021 * Handle signal processing.
1023 if (CMP_FLAG(l, ISIG)) {
1024 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
1025 if (CMP_CC(VSTATUS, c)) {
1026 tty_signal_pgrp(tp, SIGINFO);
1032 * When compared to the old implementation, this
1033 * implementation also flushes the output queue. POSIX
1034 * is really brief about this, but does makes us assume
1038 if (CMP_CC(VINTR, c)) {
1040 } else if (CMP_CC(VQUIT, c)) {
1042 } else if (CMP_CC(VSUSP, c)) {
1048 * Echo the character before signalling the
1051 if (!CMP_FLAG(l, NOFLSH))
1052 tty_flush(tp, FREAD|FWRITE);
1053 ttydisc_echo(tp, c, 0);
1054 tty_signal_pgrp(tp, signal);
1060 * Handle start/stop characters.
1062 if (CMP_FLAG(i, IXON)) {
1063 if (CMP_CC(VSTOP, c)) {
1064 /* Stop it if we aren't stopped yet. */
1065 if ((tp->t_flags & TF_STOPPED) == 0) {
1066 tp->t_flags |= TF_STOPPED;
1071 * When VSTART == VSTOP, we should make this key
1074 if (!CMP_CC(VSTART, c))
1077 if (CMP_CC(VSTART, c)) {
1078 tp->t_flags &= ~TF_STOPPED;
1083 /* Conversion of CR and NL. */
1086 if (CMP_FLAG(i, IGNCR))
1088 if (CMP_FLAG(i, ICRNL))
1092 if (CMP_FLAG(i, INLCR))
1097 /* Canonical line editing. */
1098 if (CMP_FLAG(l, ICANON)) {
1099 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1100 ttydisc_rubchar(tp);
1102 } else if (CMP_CC(VKILL, c)) {
1103 while (ttydisc_rubchar(tp) == 0);
1105 } else if (CMP_FLAG(l, IEXTEN)) {
1106 if (CMP_CC(VWERASE, c)) {
1107 ttydisc_rubword(tp);
1109 } else if (CMP_CC(VREPRINT, c)) {
1110 ttydisc_reprint(tp);
1117 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1118 /* Print 0xff 0xff. */
1130 if (CMP_FLAG(i, PARMRK)) {
1131 /* Prepend 0xff 0x00 0x.. */
1141 /* See if we can store this on the input queue. */
1142 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1143 if (CMP_FLAG(i, IMAXBEL))
1144 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1147 * Prevent a deadlock here. It may be possible that a
1148 * user has entered so much data, there is no data
1149 * available to read(), but the buffers are full anyway.
1151 * Only enter the high watermark if the device driver
1152 * can actually transmit something.
1154 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1157 tty_hiwat_in_block(tp);
1162 * In raw mode, we canonicalize after receiving a single
1163 * character. Otherwise, we canonicalize when we receive a
1164 * newline, VEOL or VEOF, but only when it isn't quoted.
1166 if (!CMP_FLAG(l, ICANON) ||
1167 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1168 ttyinq_canonicalize(&tp->t_inq);
1171 ttydisc_echo(tp, c, quote);
1177 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1181 if (ttydisc_can_bypass(tp))
1182 return (ttydisc_rint_bypass(tp, buf, len));
1184 for (cbuf = buf; len-- > 0; cbuf++) {
1185 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1189 return (cbuf - (const char *)buf);
1193 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1197 tty_assert_locked(tp);
1199 MPASS(tp->t_flags & TF_BYPASS);
1201 atomic_add_long(&tty_nin, len);
1203 if (ttyhook_hashook(tp, rint_bypass))
1204 return ttyhook_rint_bypass(tp, buf, len);
1206 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1207 ttyinq_canonicalize(&tp->t_inq);
1209 tty_hiwat_in_block(tp);
1215 ttydisc_rint_done(struct tty *tp)
1218 tty_assert_locked(tp);
1220 if (ttyhook_hashook(tp, rint_done))
1221 ttyhook_rint_done(tp);
1223 /* Wake up readers. */
1224 tty_wakeup(tp, FREAD);
1225 /* Wake up driver for echo. */
1226 ttydevsw_outwakeup(tp);
1230 ttydisc_rint_poll(struct tty *tp)
1234 tty_assert_locked(tp);
1236 if (ttyhook_hashook(tp, rint_poll))
1237 return ttyhook_rint_poll(tp);
1240 * XXX: Still allow character input when there's no space in the
1241 * buffers, but we haven't entered the high watermark. This is
1242 * to allow backspace characters to be inserted when in
1245 l = ttyinq_bytesleft(&tp->t_inq);
1246 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1253 ttydisc_wakeup_watermark(struct tty *tp)
1257 c = ttyoutq_bytesleft(&tp->t_outq);
1258 if (tp->t_flags & TF_HIWAT_OUT) {
1259 /* Only allow us to run when we're below the watermark. */
1260 if (c < tp->t_outlow)
1263 /* Reset the watermark. */
1264 tp->t_flags &= ~TF_HIWAT_OUT;
1266 /* Only run when we have data at all. */
1270 tty_wakeup(tp, FWRITE);
1274 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1277 tty_assert_locked(tp);
1279 if (tp->t_flags & TF_STOPPED)
1282 if (ttyhook_hashook(tp, getc_inject))
1283 return ttyhook_getc_inject(tp, buf, len);
1285 len = ttyoutq_read(&tp->t_outq, buf, len);
1287 if (ttyhook_hashook(tp, getc_capture))
1288 ttyhook_getc_capture(tp, buf, len);
1290 ttydisc_wakeup_watermark(tp);
1291 atomic_add_long(&tty_nout, len);
1297 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1300 ssize_t obytes = uio->uio_resid;
1302 char buf[TTY_STACKBUF];
1304 tty_assert_locked(tp);
1306 if (tp->t_flags & TF_STOPPED)
1310 * When a TTY hook is attached, we cannot perform unbuffered
1311 * copying to userspace. Just call ttydisc_getc() and
1312 * temporarily store data in a shadow buffer.
1314 if (ttyhook_hashook(tp, getc_capture) ||
1315 ttyhook_hashook(tp, getc_inject)) {
1316 while (uio->uio_resid > 0) {
1317 /* Read to shadow buffer. */
1318 len = ttydisc_getc(tp, buf,
1319 MIN(uio->uio_resid, sizeof buf));
1323 /* Copy to userspace. */
1325 error = uiomove(buf, len, uio);
1332 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1334 ttydisc_wakeup_watermark(tp);
1335 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1342 ttydisc_getc_poll(struct tty *tp)
1345 tty_assert_locked(tp);
1347 if (tp->t_flags & TF_STOPPED)
1350 if (ttyhook_hashook(tp, getc_poll))
1351 return ttyhook_getc_poll(tp);
1353 return ttyoutq_bytesused(&tp->t_outq);
1357 * XXX: not really related to the TTYDISC, but we'd better put
1358 * tty_putchar() here, because we need to perform proper output
1363 tty_putstrn(struct tty *tp, const char *p, size_t n)
1367 tty_assert_locked(tp);
1372 for (i = 0; i < n; i++)
1373 ttydisc_echo_force(tp, p[i], 0);
1375 tp->t_writepos = tp->t_column;
1376 ttyinq_reprintpos_set(&tp->t_inq);
1378 ttydevsw_outwakeup(tp);
1383 tty_putchar(struct tty *tp, char c)
1385 return (tty_putstrn(tp, &c, 1));