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 * Populate our break array; it should likely be at least 4 bytes in size to
118 * allow for \n, VEOF, and VEOL.
121 ttydisc_read_break(struct tty *tp, char *breakc, size_t breaksz)
128 #define BREAK_ADD(c) do { \
129 MPASS(n < breaksz - 1); /* NUL terminated */ \
130 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
131 breakc[n++] = tp->t_termios.c_cc[c]; \
133 /* Determine which characters we should trigger on. */
142 ttydisc_bytesavail(struct tty *tp)
146 unsigned char lastc = _POSIX_VDISABLE;
148 clen = ttyinq_bytescanonicalized(&tp->t_inq);
149 if (!CMP_FLAG(l, ICANON) || clen == 0)
152 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
153 clen = ttyinq_findchar(&tp->t_inq, breakc, clen, &lastc);
156 * We might have a partial line canonicalized in the input queue if we,
157 * for instance, switched to ICANON after taking some input in raw mode.
158 * In this case, read(2) will block because we only have a partial line.
160 if (lastc == _POSIX_VDISABLE)
163 /* If VEOF was our terminal, it must be discarded (not counted). */
164 if (CMP_CC(VEOF, lastc))
171 ttydisc_canonicalize(struct tty *tp)
176 * If we're in non-canonical mode, it's as easy as just canonicalizing
177 * the current partial line.
179 if (!CMP_FLAG(l, ICANON)) {
180 ttyinq_canonicalize(&tp->t_inq);
185 * For canonical mode, we need to rescan the buffer for the last EOL
188 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
189 ttyinq_canonicalize_break(&tp->t_inq, breakc);
193 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
195 char breakc[4]; /* enough to hold \n, VEOF and VEOL. */
197 size_t clen, flen = 0;
198 unsigned char lastc = _POSIX_VDISABLE;
200 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
203 error = tty_wait_background(tp, curthread, SIGTTIN);
208 * Quite a tricky case: unlike the old TTY
209 * implementation, this implementation copies data back
210 * to userspace in large chunks. Unfortunately, we can't
211 * calculate the line length on beforehand if it crosses
212 * ttyinq_block boundaries, because multiple reads could
213 * then make this code read beyond the newline.
215 * This is why we limit the read to:
216 * - The size the user has requested
217 * - The blocksize (done in tty_inq.c)
218 * - The amount of bytes until the newline
220 * This causes the line length to be recalculated after
221 * each block has been copied to userspace. This will
222 * cause the TTY layer to return data in chunks using
223 * the blocksize (except the first and last blocks).
225 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid + 1,
230 if (tp->t_flags & TF_ZOMBIE)
232 else if (ioflag & IO_NDELAY)
233 return (EWOULDBLOCK);
235 error = tty_wait(tp, &tp->t_inwait);
242 * Don't send the EOF char back to userspace. Our above call to
243 * ttyinq_findchar overreads by 1 character in case we would
244 * otherwise be leaving an EOF for the next read(). We'll trim
245 * clen back down to uio_resid whether we find our EOF or not.
247 if (CMP_CC(VEOF, lastc))
251 * Trim clen back down to the buffer size, since we had
252 * intentionally over-read.
254 clen = MIN(uio->uio_resid + flen, clen);
257 /* Read and throw away the EOF character. */
258 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
262 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
268 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
270 size_t vmin = tp->t_termios.c_cc[VMIN];
271 ssize_t oresid = uio->uio_resid;
274 MPASS(tp->t_termios.c_cc[VTIME] == 0);
277 * This routine implements the easy cases of read()s while in
278 * non-canonical mode, namely case B and D, where we don't have
283 error = tty_wait_background(tp, curthread, SIGTTIN);
287 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
291 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
294 /* We have to wait for more. */
295 if (tp->t_flags & TF_ZOMBIE)
297 else if (ioflag & IO_NDELAY)
298 return (EWOULDBLOCK);
300 error = tty_wait(tp, &tp->t_inwait);
307 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
310 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
311 unsigned int vtime = tp->t_termios.c_cc[VTIME];
312 struct timeval end, now, left;
315 MPASS(tp->t_termios.c_cc[VTIME] != 0);
317 /* Determine when the read should be expired. */
318 end.tv_sec = vtime / 10;
319 end.tv_usec = (vtime % 10) * 100000;
321 timevaladd(&end, &now);
324 error = tty_wait_background(tp, curthread, SIGTTIN);
328 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
332 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
335 /* Calculate how long we should wait. */
337 if (timevalcmp(&now, &end, >))
340 timevalsub(&left, &now);
344 * We have to wait for more. If the timer expires, we
345 * should return a 0-byte read.
347 if (tp->t_flags & TF_ZOMBIE)
349 else if (ioflag & IO_NDELAY)
350 return (EWOULDBLOCK);
352 error = tty_timedwait(tp, &tp->t_inwait, hz);
354 return (error == EWOULDBLOCK ? 0 : error);
361 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
363 size_t vmin = tp->t_termios.c_cc[VMIN];
364 ssize_t oresid = uio->uio_resid;
367 MPASS(tp->t_termios.c_cc[VMIN] != 0);
368 MPASS(tp->t_termios.c_cc[VTIME] != 0);
371 * When using the interbyte timer, the timer should be started
372 * after the first byte has been received. We just call into the
373 * generic read timer code after we've received the first byte.
377 error = tty_wait_background(tp, curthread, SIGTTIN);
381 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
385 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
389 * Not enough data, but we did receive some, which means
390 * we'll now start using the interbyte timer.
392 if (oresid != uio->uio_resid)
395 /* We have to wait for more. */
396 if (tp->t_flags & TF_ZOMBIE)
398 else if (ioflag & IO_NDELAY)
399 return (EWOULDBLOCK);
401 error = tty_wait(tp, &tp->t_inwait);
406 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
410 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
414 tty_assert_locked(tp);
416 if (uio->uio_resid == 0)
419 if (CMP_FLAG(l, ICANON))
420 error = ttydisc_read_canonical(tp, uio, ioflag);
421 else if (tp->t_termios.c_cc[VTIME] == 0)
422 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
423 else if (tp->t_termios.c_cc[VMIN] == 0)
424 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
427 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
429 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
430 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
431 /* Unset the input watermark when we've got enough space. */
432 tty_hiwat_in_unblock(tp);
438 static __inline unsigned int
439 ttydisc_findchar(const char *obstart, unsigned int oblen)
441 const char *c = obstart;
449 return (c - obstart);
453 ttydisc_write_oproc(struct tty *tp, char c)
455 unsigned int scnt, error;
457 MPASS(CMP_FLAG(o, OPOST));
460 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
463 /* End-of-text dropping. */
464 if (CMP_FLAG(o, ONOEOT))
466 return PRINT_NORMAL();
469 /* Handle backspace to fix tab expansion. */
470 if (PRINT_NORMAL() != 0)
472 if (tp->t_column > 0)
478 scnt = 8 - (tp->t_column & 7);
479 if (CMP_FLAG(o, TAB3)) {
480 error = ttyoutq_write_nofrag(&tp->t_outq,
483 error = PRINT_NORMAL();
488 tp->t_column += scnt;
489 MPASS((tp->t_column % 8) == 0);
493 /* Newline conversion. */
494 if (CMP_FLAG(o, ONLCR)) {
495 /* Convert \n to \r\n. */
496 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
498 error = PRINT_NORMAL();
503 if (CMP_FLAG(o, ONLCR|ONLRET)) {
504 tp->t_column = tp->t_writepos = 0;
505 ttyinq_reprintpos_set(&tp->t_inq);
510 /* Carriage return to newline conversion. */
511 if (CMP_FLAG(o, OCRNL))
513 /* Omit carriage returns on column 0. */
514 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
516 if (PRINT_NORMAL() != 0)
519 tp->t_column = tp->t_writepos = 0;
520 ttyinq_reprintpos_set(&tp->t_inq);
525 * Invisible control character. Print it, but don't
526 * increase the column count.
528 return PRINT_NORMAL();
533 * Just like the old TTY implementation, we need to copy data in chunks
534 * into a temporary buffer. One of the reasons why we need to do this,
535 * is because output processing (only TAB3 though) may allow the buffer
536 * to grow eight times.
539 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
541 char ob[TTY_STACKBUF];
544 unsigned int oblen = 0;
546 tty_assert_locked(tp);
548 if (tp->t_flags & TF_ZOMBIE)
552 * We don't need to check whether the process is the foreground
553 * process group or if we have a carrier. This is already done
557 while (uio->uio_resid > 0) {
562 if (CMP_FLAG(l, FLUSHO)) {
563 uio->uio_offset += uio->uio_resid;
568 /* Step 1: read data. */
570 nlen = MIN(uio->uio_resid, sizeof ob);
572 error = uiomove(ob, nlen, uio);
585 /* Step 2: process data. */
587 unsigned int plen, wlen;
589 if (CMP_FLAG(l, FLUSHO)) {
590 uio->uio_offset += uio->uio_resid;
595 /* Search for special characters for post processing. */
596 if (CMP_FLAG(o, OPOST)) {
597 plen = ttydisc_findchar(obstart, oblen);
604 * We're going to process a character
605 * that needs processing
607 if (ttydisc_write_oproc(tp, *obstart) == 0) {
611 tp->t_writepos = tp->t_column;
612 ttyinq_reprintpos_set(&tp->t_inq);
616 /* We're going to write regular data. */
617 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
620 tp->t_column += wlen;
622 tp->t_writepos = tp->t_column;
623 ttyinq_reprintpos_set(&tp->t_inq);
629 /* Watermark reached. Try to sleep. */
630 tp->t_flags |= TF_HIWAT_OUT;
632 if (ioflag & IO_NDELAY) {
638 * The driver may write back the data
639 * synchronously. Be sure to check the high
640 * water mark before going to sleep.
642 ttydevsw_outwakeup(tp);
643 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
646 error = tty_wait(tp, &tp->t_outwait);
650 if (tp->t_flags & TF_ZOMBIE) {
659 ttydevsw_outwakeup(tp);
662 * Add the amount of bytes that we didn't process back to the
663 * uio counters. We need to do this to make sure write() doesn't
664 * count the bytes we didn't store in the queue.
666 uio->uio_resid += oblen;
671 ttydisc_optimize(struct tty *tp)
673 tty_assert_locked(tp);
675 if (ttyhook_hashook(tp, rint_bypass)) {
676 tp->t_flags |= TF_BYPASS;
677 } else if (ttyhook_hashook(tp, rint)) {
678 tp->t_flags &= ~TF_BYPASS;
679 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
680 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
681 (!CMP_FLAG(i, PARMRK) ||
682 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
683 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
684 tp->t_flags |= TF_BYPASS;
686 tp->t_flags &= ~TF_BYPASS;
691 ttydisc_modem(struct tty *tp, int open)
694 tty_assert_locked(tp);
697 cv_broadcast(&tp->t_dcdwait);
700 * Ignore modem status lines when CLOCAL is turned on, but don't
701 * enter the zombie state when the TTY isn't opened, because
702 * that would cause the TTY to be in zombie state after being
705 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
712 tp->t_flags |= TF_ZOMBIE;
714 tty_signal_sessleader(tp, SIGHUP);
715 tty_flush(tp, FREAD|FWRITE);
718 * Carrier is back again.
721 /* XXX: what should we do here? */
726 ttydisc_echo_force(struct tty *tp, char c, int quote)
729 if (CMP_FLAG(l, FLUSHO))
732 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
734 * Only perform postprocessing when OPOST is turned on
735 * and the character is an unquoted BS/TB/NL/CR.
737 return ttydisc_write_oproc(tp, c);
738 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
740 * Only use ^X notation when ECHOCTL is turned on and
741 * we've got an quoted control character.
743 * Print backspaces when echoing an end-of-file.
745 char ob[4] = "^?\b\b";
747 /* Print ^X notation. */
751 if (!quote && CMP_CC(VEOF, c)) {
752 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
755 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
758 /* Can just be printed. */
760 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
765 ttydisc_echo(struct tty *tp, char c, int quote)
769 * Only echo characters when ECHO is turned on, or ECHONL when
770 * the character is an unquoted newline.
772 if (!CMP_FLAG(l, ECHO) &&
773 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
776 return ttydisc_echo_force(tp, c, quote);
780 ttydisc_reprint_char(void *d, char c, int quote)
784 ttydisc_echo(tp, c, quote);
788 ttydisc_reprint(struct tty *tp)
792 /* Print ^R\n, followed by the line. */
793 c = tp->t_termios.c_cc[VREPRINT];
794 if (c != _POSIX_VDISABLE)
795 ttydisc_echo(tp, c, 0);
796 ttydisc_echo(tp, CNL, 0);
797 ttyinq_reprintpos_reset(&tp->t_inq);
799 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
802 struct ttydisc_recalc_length {
808 ttydisc_recalc_charlength(void *d, char c, int quote)
810 struct ttydisc_recalc_length *data = d;
811 struct tty *tp = data->tp;
813 if (CTL_PRINT(c, quote)) {
814 if (CMP_FLAG(l, ECHOCTL))
816 } else if (c == CTAB) {
817 data->curlen += 8 - (data->curlen & 7);
824 ttydisc_recalc_linelength(struct tty *tp)
826 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
828 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
829 ttydisc_recalc_charlength, &data);
830 return (data.curlen);
834 ttydisc_rubchar(struct tty *tp)
838 unsigned int prevpos, tablen;
840 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
842 ttyinq_unputchar(&tp->t_inq);
844 if (CMP_FLAG(l, ECHO)) {
846 * Remove the character from the screen. This is even
847 * safe for characters that span multiple characters
848 * (tabs, quoted, etc).
850 if (tp->t_writepos >= tp->t_column) {
851 /* Retype the sentence. */
853 } else if (CMP_FLAG(l, ECHOE)) {
854 if (CTL_PRINT(c, quote)) {
855 /* Remove ^X formatted chars. */
856 if (CMP_FLAG(l, ECHOCTL)) {
858 ttyoutq_write_nofrag(&tp->t_outq,
861 } else if (c == ' ') {
862 /* Space character needs no rubbing. */
864 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
865 } else if (c == CTAB) {
867 * Making backspace work with tabs is
868 * quite hard. Recalculate the length of
869 * this character and remove it.
871 * Because terminal settings could be
872 * changed while the line is being
873 * inserted, the calculations don't have
874 * to be correct. Make sure we keep the
875 * tab length within proper bounds.
877 prevpos = ttydisc_recalc_linelength(tp);
878 if (prevpos >= tp->t_column)
881 tablen = tp->t_column - prevpos;
885 tp->t_column = prevpos;
886 ttyoutq_write_nofrag(&tp->t_outq,
887 "\b\b\b\b\b\b\b\b", tablen);
889 } else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
891 uint8_t bytes[UTF8_STACKBUF] = { 0 };
892 int curidx = UTF8_STACKBUF - 1, cwidth = 1,
894 teken_char_t codepoint;
896 /* Save current byte. */
900 /* Loop back through inq until we hit the
902 while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
904 * Check if we've reached the beginning
907 if (ttyinq_peekchar(&tp->t_inq, &c,
910 ttyinq_unputchar(&tp->t_inq);
916 * Shift array so that the leading
917 * byte ends up at idx 0.
919 if (nb < UTF8_STACKBUF)
920 memmove(&bytes[0], &bytes[curidx + 1],
921 nb * sizeof(uint8_t));
922 /* Check for malformed UTF8 characters. */
923 if (nb == UTF8_STACKBUF &&
924 CTL_UTF8_CONT(bytes[0])) {
926 * Place all bytes back into the inq and
927 * delete the last byte only.
929 ttyinq_write(&tp->t_inq, bytes,
931 ttyinq_unputchar(&tp->t_inq);
933 /* Find codepoint and width. */
935 teken_utf8_bytes_to_codepoint(bytes,
938 TEKEN_UTF8_INVALID_CODEPOINT ||
939 (cwidth = teken_wcwidth(
942 * Place all bytes back into the
943 * inq and fall back to
947 ttyinq_write(&tp->t_inq, bytes,
949 ttyinq_unputchar(&tp->t_inq);
952 tp->t_column -= cwidth;
954 * Delete character by punching
955 * 'cwidth' spaces over it.
958 ttyoutq_write_nofrag(&tp->t_outq,
960 else if (cwidth == 2)
961 ttyoutq_write_nofrag(&tp->t_outq,
965 * Remove a regular character by
966 * punching a space over it.
969 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
972 /* Don't print spaces. */
973 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
981 ttydisc_rubword(struct tty *tp)
986 /* Strip whitespace first. */
988 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
996 * Record whether the last character from the previous iteration
997 * was alphanumeric or not. We need this to implement ALTWERASE.
999 alnum = CTL_ALNUM(c);
1001 ttydisc_rubchar(tp);
1003 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
1007 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
1013 ttydisc_rint(struct tty *tp, char c, int flags)
1015 int signal, quote = 0;
1016 char ob[3] = { 0xff, 0x00 };
1019 tty_assert_locked(tp);
1021 atomic_add_long(&tty_nin, 1);
1023 if (ttyhook_hashook(tp, rint))
1024 return ttyhook_rint(tp, c, flags);
1026 if (tp->t_flags & TF_BYPASS)
1030 if (flags & TRE_BREAK) {
1031 if (CMP_FLAG(i, IGNBRK)) {
1032 /* Ignore break characters. */
1034 } else if (CMP_FLAG(i, BRKINT)) {
1035 /* Generate SIGINT on break. */
1036 tty_flush(tp, FREAD|FWRITE);
1037 tty_signal_pgrp(tp, SIGINT);
1040 /* Just print it. */
1043 } else if (flags & TRE_FRAMING ||
1044 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
1045 if (CMP_FLAG(i, IGNPAR)) {
1046 /* Ignore bad characters. */
1049 /* Just print it. */
1055 /* Allow any character to perform a wakeup. */
1056 if (CMP_FLAG(i, IXANY)) {
1057 tp->t_flags &= ~TF_STOPPED;
1058 tp->t_termios.c_lflag &= ~FLUSHO;
1061 /* Remove the top bit. */
1062 if (CMP_FLAG(i, ISTRIP))
1065 /* Skip input processing when we want to print it literally. */
1066 if (tp->t_flags & TF_LITERAL) {
1067 tp->t_flags &= ~TF_LITERAL;
1072 /* Special control characters that are implementation dependent. */
1073 if (CMP_FLAG(l, IEXTEN)) {
1074 /* Accept the next character as literal. */
1075 if (CMP_CC(VLNEXT, c)) {
1076 if (CMP_FLAG(l, ECHO)) {
1077 if (CMP_FLAG(l, ECHOE))
1078 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
1080 ttydisc_echo(tp, c, 0);
1082 tp->t_flags |= TF_LITERAL;
1085 /* Discard processing */
1086 if (CMP_CC(VDISCARD, c)) {
1087 if (CMP_FLAG(l, FLUSHO)) {
1088 tp->t_termios.c_lflag &= ~FLUSHO;
1090 tty_flush(tp, FWRITE);
1091 ttydisc_echo(tp, c, 0);
1092 if (tp->t_inq.ti_end > 0)
1093 ttydisc_reprint(tp);
1094 tp->t_termios.c_lflag |= FLUSHO;
1100 * Handle signal processing.
1102 if (CMP_FLAG(l, ISIG)) {
1103 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
1104 if (CMP_CC(VSTATUS, c)) {
1105 tty_signal_pgrp(tp, SIGINFO);
1111 * When compared to the old implementation, this
1112 * implementation also flushes the output queue. POSIX
1113 * is really brief about this, but does makes us assume
1117 if (CMP_CC(VINTR, c)) {
1119 } else if (CMP_CC(VQUIT, c)) {
1121 } else if (CMP_CC(VSUSP, c)) {
1127 * Echo the character before signalling the
1130 if (!CMP_FLAG(l, NOFLSH))
1131 tty_flush(tp, FREAD|FWRITE);
1132 ttydisc_echo(tp, c, 0);
1133 tty_signal_pgrp(tp, signal);
1139 * Handle start/stop characters.
1141 if (CMP_FLAG(i, IXON)) {
1142 if (CMP_CC(VSTOP, c)) {
1143 /* Stop it if we aren't stopped yet. */
1144 if ((tp->t_flags & TF_STOPPED) == 0) {
1145 tp->t_flags |= TF_STOPPED;
1150 * When VSTART == VSTOP, we should make this key
1153 if (!CMP_CC(VSTART, c))
1156 if (CMP_CC(VSTART, c)) {
1157 tp->t_flags &= ~TF_STOPPED;
1162 /* Conversion of CR and NL. */
1165 if (CMP_FLAG(i, IGNCR))
1167 if (CMP_FLAG(i, ICRNL))
1171 if (CMP_FLAG(i, INLCR))
1176 /* Canonical line editing. */
1177 if (CMP_FLAG(l, ICANON)) {
1178 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1179 ttydisc_rubchar(tp);
1181 } else if (CMP_CC(VKILL, c)) {
1182 while (ttydisc_rubchar(tp) == 0);
1184 } else if (CMP_FLAG(l, IEXTEN)) {
1185 if (CMP_CC(VWERASE, c)) {
1186 ttydisc_rubword(tp);
1188 } else if (CMP_CC(VREPRINT, c)) {
1189 ttydisc_reprint(tp);
1196 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1197 /* Print 0xff 0xff. */
1209 if (CMP_FLAG(i, PARMRK)) {
1210 /* Prepend 0xff 0x00 0x.. */
1220 /* See if we can store this on the input queue. */
1221 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1222 if (CMP_FLAG(i, IMAXBEL))
1223 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1226 * Prevent a deadlock here. It may be possible that a
1227 * user has entered so much data, there is no data
1228 * available to read(), but the buffers are full anyway.
1230 * Only enter the high watermark if the device driver
1231 * can actually transmit something.
1233 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1236 tty_hiwat_in_block(tp);
1241 * In raw mode, we canonicalize after receiving a single
1242 * character. Otherwise, we canonicalize when we receive a
1243 * newline, VEOL or VEOF, but only when it isn't quoted.
1245 if (!CMP_FLAG(l, ICANON) ||
1246 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1247 ttyinq_canonicalize(&tp->t_inq);
1250 ttydisc_echo(tp, c, quote);
1256 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1260 if (ttydisc_can_bypass(tp))
1261 return (ttydisc_rint_bypass(tp, buf, len));
1263 for (cbuf = buf; len-- > 0; cbuf++) {
1264 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1268 return (cbuf - (const char *)buf);
1272 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1276 tty_assert_locked(tp);
1278 MPASS(tp->t_flags & TF_BYPASS);
1280 atomic_add_long(&tty_nin, len);
1282 if (ttyhook_hashook(tp, rint_bypass))
1283 return ttyhook_rint_bypass(tp, buf, len);
1285 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1286 ttyinq_canonicalize(&tp->t_inq);
1288 tty_hiwat_in_block(tp);
1294 ttydisc_rint_done(struct tty *tp)
1297 tty_assert_locked(tp);
1299 if (ttyhook_hashook(tp, rint_done))
1300 ttyhook_rint_done(tp);
1302 /* Wake up readers. */
1303 tty_wakeup(tp, FREAD);
1304 /* Wake up driver for echo. */
1305 ttydevsw_outwakeup(tp);
1309 ttydisc_rint_poll(struct tty *tp)
1313 tty_assert_locked(tp);
1315 if (ttyhook_hashook(tp, rint_poll))
1316 return ttyhook_rint_poll(tp);
1319 * XXX: Still allow character input when there's no space in the
1320 * buffers, but we haven't entered the high watermark. This is
1321 * to allow backspace characters to be inserted when in
1324 l = ttyinq_bytesleft(&tp->t_inq);
1325 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1332 ttydisc_wakeup_watermark(struct tty *tp)
1336 c = ttyoutq_bytesleft(&tp->t_outq);
1337 if (tp->t_flags & TF_HIWAT_OUT) {
1338 /* Only allow us to run when we're below the watermark. */
1339 if (c < tp->t_outlow)
1342 /* Reset the watermark. */
1343 tp->t_flags &= ~TF_HIWAT_OUT;
1345 /* Only run when we have data at all. */
1349 tty_wakeup(tp, FWRITE);
1353 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1356 tty_assert_locked(tp);
1358 if (tp->t_flags & TF_STOPPED)
1361 if (ttyhook_hashook(tp, getc_inject))
1362 return ttyhook_getc_inject(tp, buf, len);
1364 len = ttyoutq_read(&tp->t_outq, buf, len);
1366 if (ttyhook_hashook(tp, getc_capture))
1367 ttyhook_getc_capture(tp, buf, len);
1369 ttydisc_wakeup_watermark(tp);
1370 atomic_add_long(&tty_nout, len);
1376 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1379 ssize_t obytes = uio->uio_resid;
1381 char buf[TTY_STACKBUF];
1383 tty_assert_locked(tp);
1385 if (tp->t_flags & TF_STOPPED)
1389 * When a TTY hook is attached, we cannot perform unbuffered
1390 * copying to userspace. Just call ttydisc_getc() and
1391 * temporarily store data in a shadow buffer.
1393 if (ttyhook_hashook(tp, getc_capture) ||
1394 ttyhook_hashook(tp, getc_inject)) {
1395 while (uio->uio_resid > 0) {
1396 /* Read to shadow buffer. */
1397 len = ttydisc_getc(tp, buf,
1398 MIN(uio->uio_resid, sizeof buf));
1402 /* Copy to userspace. */
1404 error = uiomove(buf, len, uio);
1411 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1413 ttydisc_wakeup_watermark(tp);
1414 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1421 ttydisc_getc_poll(struct tty *tp)
1424 tty_assert_locked(tp);
1426 if (tp->t_flags & TF_STOPPED)
1429 if (ttyhook_hashook(tp, getc_poll))
1430 return ttyhook_getc_poll(tp);
1432 return ttyoutq_bytesused(&tp->t_outq);
1436 * XXX: not really related to the TTYDISC, but we'd better put
1437 * tty_putchar() here, because we need to perform proper output
1442 tty_putstrn(struct tty *tp, const char *p, size_t n)
1446 tty_assert_locked(tp);
1451 for (i = 0; i < n; i++)
1452 ttydisc_echo_force(tp, p[i], 0);
1454 tp->t_writepos = tp->t_column;
1455 ttyinq_reprintpos_set(&tp->t_inq);
1457 ttydevsw_outwakeup(tp);
1462 tty_putchar(struct tty *tp, char c)
1464 return (tty_putstrn(tp, &c, 1));