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 * Populate our break array; it should likely be at least 4 bytes in size to
117 * allow for \n, VEOF, and VEOL.
120 ttydisc_read_break(struct tty *tp, char *breakc, size_t breaksz)
127 #define BREAK_ADD(c) do { \
128 MPASS(n < breaksz - 1); /* NUL terminated */ \
129 if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
130 breakc[n++] = tp->t_termios.c_cc[c]; \
132 /* Determine which characters we should trigger on. */
141 ttydisc_bytesavail(struct tty *tp)
145 unsigned char lastc = _POSIX_VDISABLE;
147 clen = ttyinq_bytescanonicalized(&tp->t_inq);
148 if (!CMP_FLAG(l, ICANON) || clen == 0)
151 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
152 clen = ttyinq_findchar(&tp->t_inq, breakc, clen, &lastc);
155 * We might have a partial line canonicalized in the input queue if we,
156 * for instance, switched to ICANON after taking some input in raw mode.
157 * In this case, read(2) will block because we only have a partial line.
159 if (lastc == _POSIX_VDISABLE)
162 /* If VEOF was our terminal, it must be discarded (not counted). */
163 if (CMP_CC(VEOF, lastc))
170 ttydisc_canonicalize(struct tty *tp)
175 * If we're in non-canonical mode, it's as easy as just canonicalizing
176 * the current partial line.
178 if (!CMP_FLAG(l, ICANON)) {
179 ttyinq_canonicalize(&tp->t_inq);
184 * For canonical mode, we need to rescan the buffer for the last EOL
187 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
188 ttyinq_canonicalize_break(&tp->t_inq, breakc);
192 ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
194 char breakc[4]; /* enough to hold \n, VEOF and VEOL. */
196 size_t clen, flen = 0;
197 unsigned char lastc = _POSIX_VDISABLE;
199 ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
202 error = tty_wait_background(tp, curthread, SIGTTIN);
207 * Quite a tricky case: unlike the old TTY
208 * implementation, this implementation copies data back
209 * to userspace in large chunks. Unfortunately, we can't
210 * calculate the line length on beforehand if it crosses
211 * ttyinq_block boundaries, because multiple reads could
212 * then make this code read beyond the newline.
214 * This is why we limit the read to:
215 * - The size the user has requested
216 * - The blocksize (done in tty_inq.c)
217 * - The amount of bytes until the newline
219 * This causes the line length to be recalculated after
220 * each block has been copied to userspace. This will
221 * cause the TTY layer to return data in chunks using
222 * the blocksize (except the first and last blocks).
224 clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid + 1,
229 if (tp->t_flags & TF_ZOMBIE)
231 else if (ioflag & IO_NDELAY)
232 return (EWOULDBLOCK);
234 error = tty_wait(tp, &tp->t_inwait);
241 * Don't send the EOF char back to userspace. Our above call to
242 * ttyinq_findchar overreads by 1 character in case we would
243 * otherwise be leaving an EOF for the next read(). We'll trim
244 * clen back down to uio_resid whether we find our EOF or not.
246 if (CMP_CC(VEOF, lastc))
250 * Trim clen back down to the buffer size, since we had
251 * intentionally over-read.
253 clen = MIN(uio->uio_resid + flen, clen);
256 /* Read and throw away the EOF character. */
257 error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
261 } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
267 ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
269 size_t vmin = tp->t_termios.c_cc[VMIN];
270 ssize_t oresid = uio->uio_resid;
273 MPASS(tp->t_termios.c_cc[VTIME] == 0);
276 * This routine implements the easy cases of read()s while in
277 * non-canonical mode, namely case B and D, where we don't have
282 error = tty_wait_background(tp, curthread, SIGTTIN);
286 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
290 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
293 /* We have to wait for more. */
294 if (tp->t_flags & TF_ZOMBIE)
296 else if (ioflag & IO_NDELAY)
297 return (EWOULDBLOCK);
299 error = tty_wait(tp, &tp->t_inwait);
306 ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
309 size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
310 unsigned int vtime = tp->t_termios.c_cc[VTIME];
311 struct timeval end, now, left;
314 MPASS(tp->t_termios.c_cc[VTIME] != 0);
316 /* Determine when the read should be expired. */
317 end.tv_sec = vtime / 10;
318 end.tv_usec = (vtime % 10) * 100000;
320 timevaladd(&end, &now);
323 error = tty_wait_background(tp, curthread, SIGTTIN);
327 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
331 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
334 /* Calculate how long we should wait. */
336 if (timevalcmp(&now, &end, >))
339 timevalsub(&left, &now);
343 * We have to wait for more. If the timer expires, we
344 * should return a 0-byte read.
346 if (tp->t_flags & TF_ZOMBIE)
348 else if (ioflag & IO_NDELAY)
349 return (EWOULDBLOCK);
351 error = tty_timedwait(tp, &tp->t_inwait, hz);
353 return (error == EWOULDBLOCK ? 0 : error);
360 ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
362 size_t vmin = tp->t_termios.c_cc[VMIN];
363 ssize_t oresid = uio->uio_resid;
366 MPASS(tp->t_termios.c_cc[VMIN] != 0);
367 MPASS(tp->t_termios.c_cc[VTIME] != 0);
370 * When using the interbyte timer, the timer should be started
371 * after the first byte has been received. We just call into the
372 * generic read timer code after we've received the first byte.
376 error = tty_wait_background(tp, curthread, SIGTTIN);
380 error = ttyinq_read_uio(&tp->t_inq, tp, uio,
384 if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
388 * Not enough data, but we did receive some, which means
389 * we'll now start using the interbyte timer.
391 if (oresid != uio->uio_resid)
394 /* We have to wait for more. */
395 if (tp->t_flags & TF_ZOMBIE)
397 else if (ioflag & IO_NDELAY)
398 return (EWOULDBLOCK);
400 error = tty_wait(tp, &tp->t_inwait);
405 return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
409 ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
413 tty_assert_locked(tp);
415 if (uio->uio_resid == 0)
418 if (CMP_FLAG(l, ICANON))
419 error = ttydisc_read_canonical(tp, uio, ioflag);
420 else if (tp->t_termios.c_cc[VTIME] == 0)
421 error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
422 else if (tp->t_termios.c_cc[VMIN] == 0)
423 error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
426 error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
428 if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
429 ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
430 /* Unset the input watermark when we've got enough space. */
431 tty_hiwat_in_unblock(tp);
437 static __inline unsigned int
438 ttydisc_findchar(const char *obstart, unsigned int oblen)
440 const char *c = obstart;
448 return (c - obstart);
452 ttydisc_write_oproc(struct tty *tp, char c)
454 unsigned int scnt, error;
456 MPASS(CMP_FLAG(o, OPOST));
459 #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
462 /* End-of-text dropping. */
463 if (CMP_FLAG(o, ONOEOT))
465 return PRINT_NORMAL();
468 /* Handle backspace to fix tab expansion. */
469 if (PRINT_NORMAL() != 0)
471 if (tp->t_column > 0)
477 scnt = 8 - (tp->t_column & 7);
478 if (CMP_FLAG(o, TAB3)) {
479 error = ttyoutq_write_nofrag(&tp->t_outq,
482 error = PRINT_NORMAL();
487 tp->t_column += scnt;
488 MPASS((tp->t_column % 8) == 0);
492 /* Newline conversion. */
493 if (CMP_FLAG(o, ONLCR)) {
494 /* Convert \n to \r\n. */
495 error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
497 error = PRINT_NORMAL();
502 if (CMP_FLAG(o, ONLCR|ONLRET)) {
503 tp->t_column = tp->t_writepos = 0;
504 ttyinq_reprintpos_set(&tp->t_inq);
509 /* Carriage return to newline conversion. */
510 if (CMP_FLAG(o, OCRNL))
512 /* Omit carriage returns on column 0. */
513 if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
515 if (PRINT_NORMAL() != 0)
518 tp->t_column = tp->t_writepos = 0;
519 ttyinq_reprintpos_set(&tp->t_inq);
524 * Invisible control character. Print it, but don't
525 * increase the column count.
527 return PRINT_NORMAL();
532 * Just like the old TTY implementation, we need to copy data in chunks
533 * into a temporary buffer. One of the reasons why we need to do this,
534 * is because output processing (only TAB3 though) may allow the buffer
535 * to grow eight times.
538 ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
540 char ob[TTY_STACKBUF];
543 unsigned int oblen = 0;
545 tty_assert_locked(tp);
547 if (tp->t_flags & TF_ZOMBIE)
551 * We don't need to check whether the process is the foreground
552 * process group or if we have a carrier. This is already done
556 while (uio->uio_resid > 0) {
561 if (CMP_FLAG(l, FLUSHO)) {
562 uio->uio_offset += uio->uio_resid;
567 /* Step 1: read data. */
569 nlen = MIN(uio->uio_resid, sizeof ob);
571 error = uiomove(ob, nlen, uio);
584 /* Step 2: process data. */
586 unsigned int plen, wlen;
588 if (CMP_FLAG(l, FLUSHO)) {
589 uio->uio_offset += uio->uio_resid;
594 /* Search for special characters for post processing. */
595 if (CMP_FLAG(o, OPOST)) {
596 plen = ttydisc_findchar(obstart, oblen);
603 * We're going to process a character
604 * that needs processing
606 if (ttydisc_write_oproc(tp, *obstart) == 0) {
610 tp->t_writepos = tp->t_column;
611 ttyinq_reprintpos_set(&tp->t_inq);
615 /* We're going to write regular data. */
616 wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
619 tp->t_column += wlen;
621 tp->t_writepos = tp->t_column;
622 ttyinq_reprintpos_set(&tp->t_inq);
628 /* Watermark reached. Try to sleep. */
629 tp->t_flags |= TF_HIWAT_OUT;
631 if (ioflag & IO_NDELAY) {
637 * The driver may write back the data
638 * synchronously. Be sure to check the high
639 * water mark before going to sleep.
641 ttydevsw_outwakeup(tp);
642 if ((tp->t_flags & TF_HIWAT_OUT) == 0)
645 error = tty_wait(tp, &tp->t_outwait);
649 if (tp->t_flags & TF_ZOMBIE) {
658 ttydevsw_outwakeup(tp);
661 * Add the amount of bytes that we didn't process back to the
662 * uio counters. We need to do this to make sure write() doesn't
663 * count the bytes we didn't store in the queue.
665 uio->uio_resid += oblen;
670 ttydisc_optimize(struct tty *tp)
672 tty_assert_locked(tp);
674 if (ttyhook_hashook(tp, rint_bypass)) {
675 tp->t_flags |= TF_BYPASS;
676 } else if (ttyhook_hashook(tp, rint)) {
677 tp->t_flags &= ~TF_BYPASS;
678 } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
679 (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
680 (!CMP_FLAG(i, PARMRK) ||
681 CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
682 !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
683 tp->t_flags |= TF_BYPASS;
685 tp->t_flags &= ~TF_BYPASS;
690 ttydisc_modem(struct tty *tp, int open)
693 tty_assert_locked(tp);
696 cv_broadcast(&tp->t_dcdwait);
699 * Ignore modem status lines when CLOCAL is turned on, but don't
700 * enter the zombie state when the TTY isn't opened, because
701 * that would cause the TTY to be in zombie state after being
704 if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
711 tp->t_flags |= TF_ZOMBIE;
713 tty_signal_sessleader(tp, SIGHUP);
714 tty_flush(tp, FREAD|FWRITE);
717 * Carrier is back again.
720 /* XXX: what should we do here? */
725 ttydisc_echo_force(struct tty *tp, char c, int quote)
728 if (CMP_FLAG(l, FLUSHO))
731 if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
733 * Only perform postprocessing when OPOST is turned on
734 * and the character is an unquoted BS/TB/NL/CR.
736 return ttydisc_write_oproc(tp, c);
737 } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
739 * Only use ^X notation when ECHOCTL is turned on and
740 * we've got an quoted control character.
742 * Print backspaces when echoing an end-of-file.
744 char ob[4] = "^?\b\b";
746 /* Print ^X notation. */
750 if (!quote && CMP_CC(VEOF, c)) {
751 return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
754 return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
757 /* Can just be printed. */
759 return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
764 ttydisc_echo(struct tty *tp, char c, int quote)
768 * Only echo characters when ECHO is turned on, or ECHONL when
769 * the character is an unquoted newline.
771 if (!CMP_FLAG(l, ECHO) &&
772 (!CMP_FLAG(l, ECHONL) || c != CNL || quote))
775 return ttydisc_echo_force(tp, c, quote);
779 ttydisc_reprint_char(void *d, char c, int quote)
783 ttydisc_echo(tp, c, quote);
787 ttydisc_reprint(struct tty *tp)
791 /* Print ^R\n, followed by the line. */
792 c = tp->t_termios.c_cc[VREPRINT];
793 if (c != _POSIX_VDISABLE)
794 ttydisc_echo(tp, c, 0);
795 ttydisc_echo(tp, CNL, 0);
796 ttyinq_reprintpos_reset(&tp->t_inq);
798 ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
801 struct ttydisc_recalc_length {
807 ttydisc_recalc_charlength(void *d, char c, int quote)
809 struct ttydisc_recalc_length *data = d;
810 struct tty *tp = data->tp;
812 if (CTL_PRINT(c, quote)) {
813 if (CMP_FLAG(l, ECHOCTL))
815 } else if (c == CTAB) {
816 data->curlen += 8 - (data->curlen & 7);
823 ttydisc_recalc_linelength(struct tty *tp)
825 struct ttydisc_recalc_length data = { tp, tp->t_writepos };
827 ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
828 ttydisc_recalc_charlength, &data);
829 return (data.curlen);
833 ttydisc_rubchar(struct tty *tp)
837 unsigned int prevpos, tablen;
839 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
841 ttyinq_unputchar(&tp->t_inq);
843 if (CMP_FLAG(l, ECHO)) {
845 * Remove the character from the screen. This is even
846 * safe for characters that span multiple characters
847 * (tabs, quoted, etc).
849 if (tp->t_writepos >= tp->t_column) {
850 /* Retype the sentence. */
852 } else if (CMP_FLAG(l, ECHOE)) {
853 if (CTL_PRINT(c, quote)) {
854 /* Remove ^X formatted chars. */
855 if (CMP_FLAG(l, ECHOCTL)) {
857 ttyoutq_write_nofrag(&tp->t_outq,
860 } else if (c == ' ') {
861 /* Space character needs no rubbing. */
863 ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
864 } else if (c == CTAB) {
866 * Making backspace work with tabs is
867 * quite hard. Recalculate the length of
868 * this character and remove it.
870 * Because terminal settings could be
871 * changed while the line is being
872 * inserted, the calculations don't have
873 * to be correct. Make sure we keep the
874 * tab length within proper bounds.
876 prevpos = ttydisc_recalc_linelength(tp);
877 if (prevpos >= tp->t_column)
880 tablen = tp->t_column - prevpos;
884 tp->t_column = prevpos;
885 ttyoutq_write_nofrag(&tp->t_outq,
886 "\b\b\b\b\b\b\b\b", tablen);
888 } else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
890 uint8_t bytes[UTF8_STACKBUF] = { 0 };
891 int curidx = UTF8_STACKBUF - 1, cwidth = 1,
893 teken_char_t codepoint;
895 /* Save current byte. */
899 /* Loop back through inq until we hit the
901 while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
903 * Check if we've reached the beginning
906 if (ttyinq_peekchar(&tp->t_inq, &c,
909 ttyinq_unputchar(&tp->t_inq);
915 * Shift array so that the leading
916 * byte ends up at idx 0.
918 if (nb < UTF8_STACKBUF)
919 memmove(&bytes[0], &bytes[curidx + 1],
920 nb * sizeof(uint8_t));
921 /* Check for malformed UTF8 characters. */
922 if (nb == UTF8_STACKBUF &&
923 CTL_UTF8_CONT(bytes[0])) {
925 * Place all bytes back into the inq and
926 * delete the last byte only.
928 ttyinq_write(&tp->t_inq, bytes,
930 ttyinq_unputchar(&tp->t_inq);
932 /* Find codepoint and width. */
934 teken_utf8_bytes_to_codepoint(bytes,
937 TEKEN_UTF8_INVALID_CODEPOINT ||
938 (cwidth = teken_wcwidth(
941 * Place all bytes back into the
942 * inq and fall back to
946 ttyinq_write(&tp->t_inq, bytes,
948 ttyinq_unputchar(&tp->t_inq);
951 tp->t_column -= cwidth;
953 * Delete character by punching
954 * 'cwidth' spaces over it.
957 ttyoutq_write_nofrag(&tp->t_outq,
959 else if (cwidth == 2)
960 ttyoutq_write_nofrag(&tp->t_outq,
964 * Remove a regular character by
965 * punching a space over it.
968 ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
971 /* Don't print spaces. */
972 ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
980 ttydisc_rubword(struct tty *tp)
985 /* Strip whitespace first. */
987 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
995 * Record whether the last character from the previous iteration
996 * was alphanumeric or not. We need this to implement ALTWERASE.
998 alnum = CTL_ALNUM(c);
1000 ttydisc_rubchar(tp);
1002 if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
1006 if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
1012 ttydisc_rint(struct tty *tp, char c, int flags)
1014 int signal, quote = 0;
1015 char ob[3] = { 0xff, 0x00 };
1018 tty_assert_locked(tp);
1020 atomic_add_long(&tty_nin, 1);
1022 if (ttyhook_hashook(tp, rint))
1023 return ttyhook_rint(tp, c, flags);
1025 if (tp->t_flags & TF_BYPASS)
1029 if (flags & TRE_BREAK) {
1030 if (CMP_FLAG(i, IGNBRK)) {
1031 /* Ignore break characters. */
1033 } else if (CMP_FLAG(i, BRKINT)) {
1034 /* Generate SIGINT on break. */
1035 tty_flush(tp, FREAD|FWRITE);
1036 tty_signal_pgrp(tp, SIGINT);
1039 /* Just print it. */
1042 } else if (flags & TRE_FRAMING ||
1043 (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
1044 if (CMP_FLAG(i, IGNPAR)) {
1045 /* Ignore bad characters. */
1048 /* Just print it. */
1054 /* Allow any character to perform a wakeup. */
1055 if (CMP_FLAG(i, IXANY)) {
1056 tp->t_flags &= ~TF_STOPPED;
1057 tp->t_termios.c_lflag &= ~FLUSHO;
1060 /* Remove the top bit. */
1061 if (CMP_FLAG(i, ISTRIP))
1064 /* Skip input processing when we want to print it literally. */
1065 if (tp->t_flags & TF_LITERAL) {
1066 tp->t_flags &= ~TF_LITERAL;
1071 /* Special control characters that are implementation dependent. */
1072 if (CMP_FLAG(l, IEXTEN)) {
1073 /* Accept the next character as literal. */
1074 if (CMP_CC(VLNEXT, c)) {
1075 if (CMP_FLAG(l, ECHO)) {
1076 if (CMP_FLAG(l, ECHOE))
1077 ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
1079 ttydisc_echo(tp, c, 0);
1081 tp->t_flags |= TF_LITERAL;
1084 /* Discard processing */
1085 if (CMP_CC(VDISCARD, c)) {
1086 if (CMP_FLAG(l, FLUSHO)) {
1087 tp->t_termios.c_lflag &= ~FLUSHO;
1089 tty_flush(tp, FWRITE);
1090 ttydisc_echo(tp, c, 0);
1091 if (tp->t_inq.ti_end > 0)
1092 ttydisc_reprint(tp);
1093 tp->t_termios.c_lflag |= FLUSHO;
1099 * Handle signal processing.
1101 if (CMP_FLAG(l, ISIG)) {
1102 if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
1103 if (CMP_CC(VSTATUS, c)) {
1104 tty_signal_pgrp(tp, SIGINFO);
1110 * When compared to the old implementation, this
1111 * implementation also flushes the output queue. POSIX
1112 * is really brief about this, but does makes us assume
1116 if (CMP_CC(VINTR, c)) {
1118 } else if (CMP_CC(VQUIT, c)) {
1120 } else if (CMP_CC(VSUSP, c)) {
1126 * Echo the character before signalling the
1129 if (!CMP_FLAG(l, NOFLSH))
1130 tty_flush(tp, FREAD|FWRITE);
1131 ttydisc_echo(tp, c, 0);
1132 tty_signal_pgrp(tp, signal);
1138 * Handle start/stop characters.
1140 if (CMP_FLAG(i, IXON)) {
1141 if (CMP_CC(VSTOP, c)) {
1142 /* Stop it if we aren't stopped yet. */
1143 if ((tp->t_flags & TF_STOPPED) == 0) {
1144 tp->t_flags |= TF_STOPPED;
1149 * When VSTART == VSTOP, we should make this key
1152 if (!CMP_CC(VSTART, c))
1155 if (CMP_CC(VSTART, c)) {
1156 tp->t_flags &= ~TF_STOPPED;
1161 /* Conversion of CR and NL. */
1164 if (CMP_FLAG(i, IGNCR))
1166 if (CMP_FLAG(i, ICRNL))
1170 if (CMP_FLAG(i, INLCR))
1175 /* Canonical line editing. */
1176 if (CMP_FLAG(l, ICANON)) {
1177 if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
1178 ttydisc_rubchar(tp);
1180 } else if (CMP_CC(VKILL, c)) {
1181 while (ttydisc_rubchar(tp) == 0);
1183 } else if (CMP_FLAG(l, IEXTEN)) {
1184 if (CMP_CC(VWERASE, c)) {
1185 ttydisc_rubword(tp);
1187 } else if (CMP_CC(VREPRINT, c)) {
1188 ttydisc_reprint(tp);
1195 if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
1196 /* Print 0xff 0xff. */
1208 if (CMP_FLAG(i, PARMRK)) {
1209 /* Prepend 0xff 0x00 0x.. */
1219 /* See if we can store this on the input queue. */
1220 if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
1221 if (CMP_FLAG(i, IMAXBEL))
1222 ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
1225 * Prevent a deadlock here. It may be possible that a
1226 * user has entered so much data, there is no data
1227 * available to read(), but the buffers are full anyway.
1229 * Only enter the high watermark if the device driver
1230 * can actually transmit something.
1232 if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
1235 tty_hiwat_in_block(tp);
1240 * In raw mode, we canonicalize after receiving a single
1241 * character. Otherwise, we canonicalize when we receive a
1242 * newline, VEOL or VEOF, but only when it isn't quoted.
1244 if (!CMP_FLAG(l, ICANON) ||
1245 (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
1246 ttyinq_canonicalize(&tp->t_inq);
1249 ttydisc_echo(tp, c, quote);
1255 ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
1259 if (ttydisc_can_bypass(tp))
1260 return (ttydisc_rint_bypass(tp, buf, len));
1262 for (cbuf = buf; len-- > 0; cbuf++) {
1263 if (ttydisc_rint(tp, *cbuf, 0) != 0)
1267 return (cbuf - (const char *)buf);
1271 ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
1275 tty_assert_locked(tp);
1277 MPASS(tp->t_flags & TF_BYPASS);
1279 atomic_add_long(&tty_nin, len);
1281 if (ttyhook_hashook(tp, rint_bypass))
1282 return ttyhook_rint_bypass(tp, buf, len);
1284 ret = ttyinq_write(&tp->t_inq, buf, len, 0);
1285 ttyinq_canonicalize(&tp->t_inq);
1287 tty_hiwat_in_block(tp);
1293 ttydisc_rint_done(struct tty *tp)
1296 tty_assert_locked(tp);
1298 if (ttyhook_hashook(tp, rint_done))
1299 ttyhook_rint_done(tp);
1301 /* Wake up readers. */
1302 tty_wakeup(tp, FREAD);
1303 /* Wake up driver for echo. */
1304 ttydevsw_outwakeup(tp);
1308 ttydisc_rint_poll(struct tty *tp)
1312 tty_assert_locked(tp);
1314 if (ttyhook_hashook(tp, rint_poll))
1315 return ttyhook_rint_poll(tp);
1318 * XXX: Still allow character input when there's no space in the
1319 * buffers, but we haven't entered the high watermark. This is
1320 * to allow backspace characters to be inserted when in
1323 l = ttyinq_bytesleft(&tp->t_inq);
1324 if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
1331 ttydisc_wakeup_watermark(struct tty *tp)
1335 c = ttyoutq_bytesleft(&tp->t_outq);
1336 if (tp->t_flags & TF_HIWAT_OUT) {
1337 /* Only allow us to run when we're below the watermark. */
1338 if (c < tp->t_outlow)
1341 /* Reset the watermark. */
1342 tp->t_flags &= ~TF_HIWAT_OUT;
1344 /* Only run when we have data at all. */
1348 tty_wakeup(tp, FWRITE);
1352 ttydisc_getc(struct tty *tp, void *buf, size_t len)
1355 tty_assert_locked(tp);
1357 if (tp->t_flags & TF_STOPPED)
1360 if (ttyhook_hashook(tp, getc_inject))
1361 return ttyhook_getc_inject(tp, buf, len);
1363 len = ttyoutq_read(&tp->t_outq, buf, len);
1365 if (ttyhook_hashook(tp, getc_capture))
1366 ttyhook_getc_capture(tp, buf, len);
1368 ttydisc_wakeup_watermark(tp);
1369 atomic_add_long(&tty_nout, len);
1375 ttydisc_getc_uio(struct tty *tp, struct uio *uio)
1378 ssize_t obytes = uio->uio_resid;
1380 char buf[TTY_STACKBUF];
1382 tty_assert_locked(tp);
1384 if (tp->t_flags & TF_STOPPED)
1388 * When a TTY hook is attached, we cannot perform unbuffered
1389 * copying to userspace. Just call ttydisc_getc() and
1390 * temporarily store data in a shadow buffer.
1392 if (ttyhook_hashook(tp, getc_capture) ||
1393 ttyhook_hashook(tp, getc_inject)) {
1394 while (uio->uio_resid > 0) {
1395 /* Read to shadow buffer. */
1396 len = ttydisc_getc(tp, buf,
1397 MIN(uio->uio_resid, sizeof buf));
1401 /* Copy to userspace. */
1403 error = uiomove(buf, len, uio);
1410 error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
1412 ttydisc_wakeup_watermark(tp);
1413 atomic_add_long(&tty_nout, obytes - uio->uio_resid);
1420 ttydisc_getc_poll(struct tty *tp)
1423 tty_assert_locked(tp);
1425 if (tp->t_flags & TF_STOPPED)
1428 if (ttyhook_hashook(tp, getc_poll))
1429 return ttyhook_getc_poll(tp);
1431 return ttyoutq_bytesused(&tp->t_outq);
1435 * XXX: not really related to the TTYDISC, but we'd better put
1436 * tty_putchar() here, because we need to perform proper output
1441 tty_putstrn(struct tty *tp, const char *p, size_t n)
1445 tty_assert_locked(tp);
1450 for (i = 0; i < n; i++)
1451 ttydisc_echo_force(tp, p[i], 0);
1453 tp->t_writepos = tp->t_column;
1454 ttyinq_reprintpos_set(&tp->t_inq);
1456 ttydevsw_outwakeup(tp);
1461 tty_putchar(struct tty *tp, char c)
1463 return (tty_putstrn(tp, &c, 1));