#include <sys/param.h>
#include <sys/fcntl.h>
#include <sys/filio.h>
#include <sys/kernel.h>
#include <sys/signal.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/ttycom.h>
#include <sys/ttydefaults.h>
#include <sys/uio.h>
#include <sys/vnode.h>
#include <teken/teken.h>
#include <teken/teken_wcwidth.h>
static unsigned long tty_nin = 0;
SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD,
&tty_nin, 0, "Total amount of bytes received");
static unsigned long tty_nout = 0;
SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD,
&tty_nout, 0, "Total amount of bytes transmitted");
#define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \
tp->t_termios.c_cc[v] == (c))
#define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt))
#define CTAB '\t'
#define CNL '\n'
#define CCR '\r'
#define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20)
#define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \
(c) == CNL || (c) == CCR))
#define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \
((q) || ((c) != CTAB && (c) != CNL))))
#define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB)
#define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \
((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z'))
#define CTL_UTF8(c) (!!((c) & 0x80))
#define CTL_UTF8_CONT(c) (((c) & 0xc0) == 0x80)
#define TTY_STACKBUF 256
#define UTF8_STACKBUF 4
void
ttydisc_open(struct tty *tp)
{
ttydisc_optimize(tp);
}
void
ttydisc_close(struct tty *tp)
{
tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE);
tp->t_termios.c_lflag &= ~FLUSHO;
tty_flush(tp, FREAD | FWRITE);
if (ttyhook_hashook(tp, close))
ttyhook_close(tp);
}
static void
ttydisc_read_break(struct tty *tp, char *breakc, size_t breaksz)
{
size_t n = 0;
MPASS(breaksz != 0);
breakc[n++] = CNL;
#define BREAK_ADD(c) do { \
MPASS(n < breaksz - 1); \
if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \
breakc[n++] = tp->t_termios.c_cc[c]; \
} while (0)
BREAK_ADD(VEOF);
BREAK_ADD(VEOL);
#undef BREAK_ADD
breakc[n] = '\0';
}
size_t
ttydisc_bytesavail(struct tty *tp)
{
size_t clen;
char breakc[4];
unsigned char lastc = _POSIX_VDISABLE;
clen = ttyinq_bytescanonicalized(&tp->t_inq);
if (!CMP_FLAG(l, ICANON) || clen == 0)
return (clen);
ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
clen = ttyinq_findchar(&tp->t_inq, breakc, clen, &lastc);
if (lastc == _POSIX_VDISABLE)
return (0);
if (CMP_CC(VEOF, lastc))
clen--;
return (clen);
}
void
ttydisc_canonicalize(struct tty *tp)
{
char breakc[4];
if (!CMP_FLAG(l, ICANON)) {
ttyinq_canonicalize(&tp->t_inq);
return;
}
ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
ttyinq_canonicalize_break(&tp->t_inq, breakc);
}
static int
ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag)
{
char breakc[4];
int error;
size_t clen, flen = 0;
unsigned char lastc = _POSIX_VDISABLE;
ttydisc_read_break(tp, &breakc[0], sizeof(breakc));
do {
error = tty_wait_background(tp, curthread, SIGTTIN);
if (error)
return (error);
clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid + 1,
&lastc);
if (clen == 0) {
if (tp->t_flags & TF_ZOMBIE)
return (0);
else if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
error = tty_wait(tp, &tp->t_inwait);
if (error)
return (error);
continue;
}
if (CMP_CC(VEOF, lastc))
flen = 1;
clen = MIN(uio->uio_resid + flen, clen);
MPASS(flen <= clen);
error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen);
if (error)
return (error);
} while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE);
return (0);
}
static int
ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag)
{
size_t vmin = tp->t_termios.c_cc[VMIN];
ssize_t oresid = uio->uio_resid;
int error;
MPASS(tp->t_termios.c_cc[VTIME] == 0);
for (;;) {
error = tty_wait_background(tp, curthread, SIGTTIN);
if (error)
return (error);
error = ttyinq_read_uio(&tp->t_inq, tp, uio,
uio->uio_resid, 0);
if (error)
return (error);
if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
return (0);
if (tp->t_flags & TF_ZOMBIE)
return (0);
else if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
error = tty_wait(tp, &tp->t_inwait);
if (error)
return (error);
}
}
static int
ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag,
int oresid)
{
size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1);
unsigned int vtime = tp->t_termios.c_cc[VTIME];
struct timeval end, now, left;
int error, hz;
MPASS(tp->t_termios.c_cc[VTIME] != 0);
end.tv_sec = vtime / 10;
end.tv_usec = (vtime % 10) * 100000;
getmicrotime(&now);
timevaladd(&end, &now);
for (;;) {
error = tty_wait_background(tp, curthread, SIGTTIN);
if (error)
return (error);
error = ttyinq_read_uio(&tp->t_inq, tp, uio,
uio->uio_resid, 0);
if (error)
return (error);
if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
return (0);
getmicrotime(&now);
if (timevalcmp(&now, &end, >))
return (0);
left = end;
timevalsub(&left, &now);
hz = tvtohz(&left);
if (tp->t_flags & TF_ZOMBIE)
return (0);
else if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
error = tty_timedwait(tp, &tp->t_inwait, hz);
if (error)
return (error == EWOULDBLOCK ? 0 : error);
}
return (0);
}
static int
ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag)
{
size_t vmin = tp->t_termios.c_cc[VMIN];
ssize_t oresid = uio->uio_resid;
int error;
MPASS(tp->t_termios.c_cc[VMIN] != 0);
MPASS(tp->t_termios.c_cc[VTIME] != 0);
for (;;) {
error = tty_wait_background(tp, curthread, SIGTTIN);
if (error)
return (error);
error = ttyinq_read_uio(&tp->t_inq, tp, uio,
uio->uio_resid, 0);
if (error)
return (error);
if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin)
return (0);
if (oresid != uio->uio_resid)
break;
if (tp->t_flags & TF_ZOMBIE)
return (0);
else if (ioflag & IO_NDELAY)
return (EWOULDBLOCK);
error = tty_wait(tp, &tp->t_inwait);
if (error)
return (error);
}
return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid);
}
int
ttydisc_read(struct tty *tp, struct uio *uio, int ioflag)
{
int error;
tty_assert_locked(tp);
if (uio->uio_resid == 0)
return (0);
if (CMP_FLAG(l, ICANON))
error = ttydisc_read_canonical(tp, uio, ioflag);
else if (tp->t_termios.c_cc[VTIME] == 0)
error = ttydisc_read_raw_no_timer(tp, uio, ioflag);
else if (tp->t_termios.c_cc[VMIN] == 0)
error = ttydisc_read_raw_read_timer(tp, uio, ioflag,
uio->uio_resid);
else
error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag);
if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow ||
ttyinq_bytescanonicalized(&tp->t_inq) == 0) {
tty_hiwat_in_unblock(tp);
}
return (error);
}
static __inline unsigned int
ttydisc_findchar(const char *obstart, unsigned int oblen)
{
const char *c = obstart;
while (oblen--) {
if (CTL_VALID(*c))
break;
c++;
}
return (c - obstart);
}
static int
ttydisc_write_oproc(struct tty *tp, char c)
{
unsigned int scnt, error;
MPASS(CMP_FLAG(o, OPOST));
MPASS(CTL_VALID(c));
#define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1)
switch (c) {
case CEOF:
if (CMP_FLAG(o, ONOEOT))
return (0);
return PRINT_NORMAL();
case CERASE2:
if (PRINT_NORMAL() != 0)
return (-1);
if (tp->t_column > 0)
tp->t_column--;
return (0);
case CTAB:
scnt = 8 - (tp->t_column & 7);
if (CMP_FLAG(o, TAB3)) {
error = ttyoutq_write_nofrag(&tp->t_outq,
" ", scnt);
} else {
error = PRINT_NORMAL();
}
if (error)
return (-1);
tp->t_column += scnt;
MPASS((tp->t_column % 8) == 0);
return (0);
case CNL:
if (CMP_FLAG(o, ONLCR)) {
error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2);
} else {
error = PRINT_NORMAL();
}
if (error)
return (-1);
if (CMP_FLAG(o, ONLCR|ONLRET)) {
tp->t_column = tp->t_writepos = 0;
ttyinq_reprintpos_set(&tp->t_inq);
}
return (0);
case CCR:
if (CMP_FLAG(o, OCRNL))
c = CNL;
if (CMP_FLAG(o, ONOCR) && tp->t_column == 0)
return (0);
if (PRINT_NORMAL() != 0)
return (-1);
tp->t_column = tp->t_writepos = 0;
ttyinq_reprintpos_set(&tp->t_inq);
return (0);
}
return PRINT_NORMAL();
#undef PRINT_NORMAL
}
int
ttydisc_write(struct tty *tp, struct uio *uio, int ioflag)
{
char ob[TTY_STACKBUF];
char *obstart;
int error = 0;
unsigned int oblen = 0;
tty_assert_locked(tp);
if (tp->t_flags & TF_ZOMBIE)
return (EIO);
while (uio->uio_resid > 0) {
unsigned int nlen;
MPASS(oblen == 0);
if (CMP_FLAG(l, FLUSHO)) {
uio->uio_offset += uio->uio_resid;
uio->uio_resid = 0;
return (0);
}
obstart = ob;
nlen = MIN(uio->uio_resid, sizeof ob);
tty_unlock(tp);
error = uiomove(ob, nlen, uio);
tty_lock(tp);
if (error != 0)
break;
oblen = nlen;
if (tty_gone(tp)) {
error = ENXIO;
break;
}
MPASS(oblen > 0);
do {
unsigned int plen, wlen;
if (CMP_FLAG(l, FLUSHO)) {
uio->uio_offset += uio->uio_resid;
uio->uio_resid = 0;
return (0);
}
if (CMP_FLAG(o, OPOST)) {
plen = ttydisc_findchar(obstart, oblen);
} else {
plen = oblen;
}
if (plen == 0) {
if (ttydisc_write_oproc(tp, *obstart) == 0) {
obstart++;
oblen--;
tp->t_writepos = tp->t_column;
ttyinq_reprintpos_set(&tp->t_inq);
continue;
}
} else {
wlen = ttyoutq_write(&tp->t_outq, obstart, plen);
obstart += wlen;
oblen -= wlen;
tp->t_column += wlen;
tp->t_writepos = tp->t_column;
ttyinq_reprintpos_set(&tp->t_inq);
if (wlen == plen)
continue;
}
tp->t_flags |= TF_HIWAT_OUT;
if (ioflag & IO_NDELAY) {
error = EWOULDBLOCK;
goto done;
}
ttydevsw_outwakeup(tp);
if ((tp->t_flags & TF_HIWAT_OUT) == 0)
continue;
error = tty_wait(tp, &tp->t_outwait);
if (error)
goto done;
if (tp->t_flags & TF_ZOMBIE) {
error = EIO;
goto done;
}
} while (oblen > 0);
}
done:
if (!tty_gone(tp))
ttydevsw_outwakeup(tp);
uio->uio_resid += oblen;
return (error);
}
void
ttydisc_optimize(struct tty *tp)
{
tty_assert_locked(tp);
if (ttyhook_hashook(tp, rint_bypass)) {
tp->t_flags |= TF_BYPASS;
} else if (ttyhook_hashook(tp, rint)) {
tp->t_flags &= ~TF_BYPASS;
} else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) &&
(!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) &&
(!CMP_FLAG(i, PARMRK) ||
CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) &&
!CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) {
tp->t_flags |= TF_BYPASS;
} else {
tp->t_flags &= ~TF_BYPASS;
}
}
void
ttydisc_modem(struct tty *tp, int open)
{
tty_assert_locked(tp);
if (open)
cv_broadcast(&tp->t_dcdwait);
if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL))
return;
if (open == 0) {
tp->t_flags |= TF_ZOMBIE;
tty_signal_sessleader(tp, SIGHUP);
tty_flush(tp, FREAD|FWRITE);
} else {
}
}
static int
ttydisc_echo_force(struct tty *tp, char c, int quote)
{
if (CMP_FLAG(l, FLUSHO))
return 0;
if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) {
return ttydisc_write_oproc(tp, c);
} else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) {
char ob[4] = "^?\b\b";
if (c != 0x7f)
ob[1] = c + 'A' - 1;
if (!quote && CMP_CC(VEOF, c)) {
return ttyoutq_write_nofrag(&tp->t_outq, ob, 4);
} else {
tp->t_column += 2;
return ttyoutq_write_nofrag(&tp->t_outq, ob, 2);
}
} else {
tp->t_column++;
return ttyoutq_write_nofrag(&tp->t_outq, &c, 1);
}
}
static int
ttydisc_echo(struct tty *tp, char c, int quote)
{
if (!CMP_FLAG(l, ECHO) &&
(!CMP_FLAG(l, ECHONL) || c != CNL || quote))
return (0);
return ttydisc_echo_force(tp, c, quote);
}
static void
ttydisc_reprint_char(void *d, char c, int quote)
{
struct tty *tp = d;
ttydisc_echo(tp, c, quote);
}
static void
ttydisc_reprint(struct tty *tp)
{
cc_t c;
c = tp->t_termios.c_cc[VREPRINT];
if (c != _POSIX_VDISABLE)
ttydisc_echo(tp, c, 0);
ttydisc_echo(tp, CNL, 0);
ttyinq_reprintpos_reset(&tp->t_inq);
ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp);
}
struct ttydisc_recalc_length {
struct tty *tp;
unsigned int curlen;
};
static void
ttydisc_recalc_charlength(void *d, char c, int quote)
{
struct ttydisc_recalc_length *data = d;
struct tty *tp = data->tp;
if (CTL_PRINT(c, quote)) {
if (CMP_FLAG(l, ECHOCTL))
data->curlen += 2;
} else if (c == CTAB) {
data->curlen += 8 - (data->curlen & 7);
} else {
data->curlen++;
}
}
static unsigned int
ttydisc_recalc_linelength(struct tty *tp)
{
struct ttydisc_recalc_length data = { tp, tp->t_writepos };
ttyinq_line_iterate_from_reprintpos(&tp->t_inq,
ttydisc_recalc_charlength, &data);
return (data.curlen);
}
static int
ttydisc_rubchar(struct tty *tp)
{
char c;
int quote;
unsigned int prevpos, tablen;
if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
return (-1);
ttyinq_unputchar(&tp->t_inq);
if (CMP_FLAG(l, ECHO)) {
if (tp->t_writepos >= tp->t_column) {
ttydisc_reprint(tp);
} else if (CMP_FLAG(l, ECHOE)) {
if (CTL_PRINT(c, quote)) {
if (CMP_FLAG(l, ECHOCTL)) {
tp->t_column -= 2;
ttyoutq_write_nofrag(&tp->t_outq,
"\b\b \b\b", 6);
}
} else if (c == ' ') {
tp->t_column -= 1;
ttyoutq_write_nofrag(&tp->t_outq, "\b", 1);
} else if (c == CTAB) {
prevpos = ttydisc_recalc_linelength(tp);
if (prevpos >= tp->t_column)
tablen = 1;
else
tablen = tp->t_column - prevpos;
if (tablen > 8)
tablen = 8;
tp->t_column = prevpos;
ttyoutq_write_nofrag(&tp->t_outq,
"\b\b\b\b\b\b\b\b", tablen);
return (0);
} else if ((tp->t_termios.c_iflag & IUTF8) != 0 &&
CTL_UTF8(c)) {
uint8_t bytes[UTF8_STACKBUF] = { 0 };
int curidx = UTF8_STACKBUF - 1, cwidth = 1,
nb = 0;
teken_char_t codepoint;
bytes[curidx] = c;
curidx--;
nb++;
while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) {
if (ttyinq_peekchar(&tp->t_inq, &c,
"e) != 0)
break;
ttyinq_unputchar(&tp->t_inq);
bytes[curidx] = c;
curidx--;
nb++;
}
if (nb < UTF8_STACKBUF)
memmove(&bytes[0], &bytes[curidx + 1],
nb * sizeof(uint8_t));
if (nb == UTF8_STACKBUF &&
CTL_UTF8_CONT(bytes[0])) {
ttyinq_write(&tp->t_inq, bytes,
UTF8_STACKBUF, 0);
ttyinq_unputchar(&tp->t_inq);
} else {
codepoint =
teken_utf8_bytes_to_codepoint(bytes,
nb);
if (codepoint ==
TEKEN_UTF8_INVALID_CODEPOINT ||
(cwidth = teken_wcwidth(
codepoint)) == -1) {
cwidth = 1;
ttyinq_write(&tp->t_inq, bytes,
nb, 0);
ttyinq_unputchar(&tp->t_inq);
}
}
tp->t_column -= cwidth;
if (cwidth == 1)
ttyoutq_write_nofrag(&tp->t_outq,
"\b \b", 3);
else if (cwidth == 2)
ttyoutq_write_nofrag(&tp->t_outq,
"\b\b \b\b", 6);
} else {
tp->t_column -= 1;
ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3);
}
} else {
ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0);
}
}
return (0);
}
static void
ttydisc_rubword(struct tty *tp)
{
char c;
int quote, alnum;
for (;;) {
if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
return;
if (!CTL_WHITE(c))
break;
ttydisc_rubchar(tp);
}
alnum = CTL_ALNUM(c);
for (;;) {
ttydisc_rubchar(tp);
if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0)
return;
if (CTL_WHITE(c))
return;
if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum)
return;
}
}
int
ttydisc_rint(struct tty *tp, char c, int flags)
{
int signal, quote = 0;
char ob[3] = { 0xff, 0x00 };
size_t ol;
tty_assert_locked(tp);
atomic_add_long(&tty_nin, 1);
if (ttyhook_hashook(tp, rint))
return ttyhook_rint(tp, c, flags);
if (tp->t_flags & TF_BYPASS)
goto processed;
if (flags) {
if (flags & TRE_BREAK) {
if (CMP_FLAG(i, IGNBRK)) {
return (0);
} else if (CMP_FLAG(i, BRKINT)) {
tty_flush(tp, FREAD|FWRITE);
tty_signal_pgrp(tp, SIGINT);
return (0);
} else {
goto parmrk;
}
} else if (flags & TRE_FRAMING ||
(flags & TRE_PARITY && CMP_FLAG(i, INPCK))) {
if (CMP_FLAG(i, IGNPAR)) {
return (0);
} else {
goto parmrk;
}
}
}
if (CMP_FLAG(i, IXANY)) {
tp->t_flags &= ~TF_STOPPED;
tp->t_termios.c_lflag &= ~FLUSHO;
}
if (CMP_FLAG(i, ISTRIP))
c &= ~0x80;
if (tp->t_flags & TF_LITERAL) {
tp->t_flags &= ~TF_LITERAL;
quote = 1;
goto processed;
}
if (CMP_FLAG(l, IEXTEN)) {
if (CMP_CC(VLNEXT, c)) {
if (CMP_FLAG(l, ECHO)) {
if (CMP_FLAG(l, ECHOE))
ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2);
else
ttydisc_echo(tp, c, 0);
}
tp->t_flags |= TF_LITERAL;
return (0);
}
if (CMP_CC(VDISCARD, c)) {
if (CMP_FLAG(l, FLUSHO)) {
tp->t_termios.c_lflag &= ~FLUSHO;
} else {
tty_flush(tp, FWRITE);
ttydisc_echo(tp, c, 0);
if (tp->t_inq.ti_end > 0)
ttydisc_reprint(tp);
tp->t_termios.c_lflag |= FLUSHO;
}
}
}
if (CMP_FLAG(l, ISIG)) {
if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) {
if (CMP_CC(VSTATUS, c)) {
tty_signal_pgrp(tp, SIGINFO);
return (0);
}
}
signal = 0;
if (CMP_CC(VINTR, c)) {
signal = SIGINT;
} else if (CMP_CC(VQUIT, c)) {
signal = SIGQUIT;
} else if (CMP_CC(VSUSP, c)) {
signal = SIGTSTP;
}
if (signal != 0) {
if (!CMP_FLAG(l, NOFLSH))
tty_flush(tp, FREAD|FWRITE);
ttydisc_echo(tp, c, 0);
tty_signal_pgrp(tp, signal);
return (0);
}
}
if (CMP_FLAG(i, IXON)) {
if (CMP_CC(VSTOP, c)) {
if ((tp->t_flags & TF_STOPPED) == 0) {
tp->t_flags |= TF_STOPPED;
return (0);
}
if (!CMP_CC(VSTART, c))
return (0);
}
if (CMP_CC(VSTART, c)) {
tp->t_flags &= ~TF_STOPPED;
return (0);
}
}
switch (c) {
case CCR:
if (CMP_FLAG(i, IGNCR))
return (0);
if (CMP_FLAG(i, ICRNL))
c = CNL;
break;
case CNL:
if (CMP_FLAG(i, INLCR))
c = CCR;
break;
}
if (CMP_FLAG(l, ICANON)) {
if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) {
ttydisc_rubchar(tp);
return (0);
} else if (CMP_CC(VKILL, c)) {
while (ttydisc_rubchar(tp) == 0);
return (0);
} else if (CMP_FLAG(l, IEXTEN)) {
if (CMP_CC(VWERASE, c)) {
ttydisc_rubword(tp);
return (0);
} else if (CMP_CC(VREPRINT, c)) {
ttydisc_reprint(tp);
return (0);
}
}
}
processed:
if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) {
ob[1] = 0xff;
ol = 2;
quote = 1;
} else {
ob[0] = c;
ol = 1;
}
goto print;
parmrk:
if (CMP_FLAG(i, PARMRK)) {
ob[2] = c;
ol = 3;
quote = 1;
} else {
ob[0] = c;
ol = 1;
}
print:
if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) {
if (CMP_FLAG(i, IMAXBEL))
ttyoutq_write_nofrag(&tp->t_outq, "\a", 1);
if (ttyinq_bytescanonicalized(&tp->t_inq) == 0)
return (0);
tty_hiwat_in_block(tp);
return (-1);
}
if (!CMP_FLAG(l, ICANON) ||
(!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) {
ttyinq_canonicalize(&tp->t_inq);
}
ttydisc_echo(tp, c, quote);
return (0);
}
size_t
ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len)
{
const char *cbuf;
if (ttydisc_can_bypass(tp))
return (ttydisc_rint_bypass(tp, buf, len));
for (cbuf = buf; len-- > 0; cbuf++) {
if (ttydisc_rint(tp, *cbuf, 0) != 0)
break;
}
return (cbuf - (const char *)buf);
}
size_t
ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len)
{
size_t ret;
tty_assert_locked(tp);
MPASS(tp->t_flags & TF_BYPASS);
atomic_add_long(&tty_nin, len);
if (ttyhook_hashook(tp, rint_bypass))
return ttyhook_rint_bypass(tp, buf, len);
ret = ttyinq_write(&tp->t_inq, buf, len, 0);
ttyinq_canonicalize(&tp->t_inq);
if (ret < len)
tty_hiwat_in_block(tp);
return (ret);
}
void
ttydisc_rint_done(struct tty *tp)
{
tty_assert_locked(tp);
if (ttyhook_hashook(tp, rint_done))
ttyhook_rint_done(tp);
tty_wakeup(tp, FREAD);
ttydevsw_outwakeup(tp);
}
size_t
ttydisc_rint_poll(struct tty *tp)
{
size_t l;
tty_assert_locked(tp);
if (ttyhook_hashook(tp, rint_poll))
return ttyhook_rint_poll(tp);
l = ttyinq_bytesleft(&tp->t_inq);
if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0)
return (1);
return (l);
}
static void
ttydisc_wakeup_watermark(struct tty *tp)
{
size_t c;
c = ttyoutq_bytesleft(&tp->t_outq);
if (tp->t_flags & TF_HIWAT_OUT) {
if (c < tp->t_outlow)
return;
tp->t_flags &= ~TF_HIWAT_OUT;
} else {
if (c == 0)
return;
}
tty_wakeup(tp, FWRITE);
}
size_t
ttydisc_getc(struct tty *tp, void *buf, size_t len)
{
tty_assert_locked(tp);
if (tp->t_flags & TF_STOPPED)
return (0);
if (ttyhook_hashook(tp, getc_inject))
return ttyhook_getc_inject(tp, buf, len);
len = ttyoutq_read(&tp->t_outq, buf, len);
if (ttyhook_hashook(tp, getc_capture))
ttyhook_getc_capture(tp, buf, len);
ttydisc_wakeup_watermark(tp);
atomic_add_long(&tty_nout, len);
return (len);
}
int
ttydisc_getc_uio(struct tty *tp, struct uio *uio)
{
int error = 0;
ssize_t obytes = uio->uio_resid;
size_t len;
char buf[TTY_STACKBUF];
tty_assert_locked(tp);
if (tp->t_flags & TF_STOPPED)
return (0);
if (ttyhook_hashook(tp, getc_capture) ||
ttyhook_hashook(tp, getc_inject)) {
while (uio->uio_resid > 0) {
len = ttydisc_getc(tp, buf,
MIN(uio->uio_resid, sizeof buf));
if (len == 0)
break;
tty_unlock(tp);
error = uiomove(buf, len, uio);
tty_lock(tp);
if (error != 0)
break;
}
} else {
error = ttyoutq_read_uio(&tp->t_outq, tp, uio);
ttydisc_wakeup_watermark(tp);
atomic_add_long(&tty_nout, obytes - uio->uio_resid);
}
return (error);
}
size_t
ttydisc_getc_poll(struct tty *tp)
{
tty_assert_locked(tp);
if (tp->t_flags & TF_STOPPED)
return (0);
if (ttyhook_hashook(tp, getc_poll))
return ttyhook_getc_poll(tp);
return ttyoutq_bytesused(&tp->t_outq);
}
int
tty_putstrn(struct tty *tp, const char *p, size_t n)
{
size_t i;
tty_assert_locked(tp);
if (tty_gone(tp))
return (-1);
for (i = 0; i < n; i++)
ttydisc_echo_force(tp, p[i], 0);
tp->t_writepos = tp->t_column;
ttyinq_reprintpos_set(&tp->t_inq);
ttydevsw_outwakeup(tp);
return (0);
}
int
tty_putchar(struct tty *tp, char c)
{
return (tty_putstrn(tp, &c, 1));
}