1bc093719SEd Schouten /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 38a36da99SPedro F. Giffuni * 4bc093719SEd Schouten * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 5bc093719SEd Schouten * All rights reserved. 6bc093719SEd Schouten * 7bc093719SEd Schouten * Portions of this software were developed under sponsorship from Snow 8bc093719SEd Schouten * B.V., the Netherlands. 9bc093719SEd Schouten * 10bc093719SEd Schouten * Redistribution and use in source and binary forms, with or without 11bc093719SEd Schouten * modification, are permitted provided that the following conditions 12bc093719SEd Schouten * are met: 13bc093719SEd Schouten * 1. Redistributions of source code must retain the above copyright 14bc093719SEd Schouten * notice, this list of conditions and the following disclaimer. 15bc093719SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 16bc093719SEd Schouten * notice, this list of conditions and the following disclaimer in the 17bc093719SEd Schouten * documentation and/or other materials provided with the distribution. 18bc093719SEd Schouten * 19bc093719SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20bc093719SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21bc093719SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22bc093719SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23bc093719SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24bc093719SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25bc093719SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26bc093719SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27bc093719SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28bc093719SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29bc093719SEd Schouten * SUCH DAMAGE. 30bc093719SEd Schouten */ 31bc093719SEd Schouten 32bc093719SEd Schouten #include <sys/cdefs.h> 33bc093719SEd Schouten #include <sys/param.h> 34bc093719SEd Schouten #include <sys/fcntl.h> 35bc093719SEd Schouten #include <sys/filio.h> 36bc093719SEd Schouten #include <sys/kernel.h> 37bc093719SEd Schouten #include <sys/signal.h> 38bc093719SEd Schouten #include <sys/sysctl.h> 39bc093719SEd Schouten #include <sys/systm.h> 40bc093719SEd Schouten #include <sys/tty.h> 41bc093719SEd Schouten #include <sys/ttycom.h> 42bc093719SEd Schouten #include <sys/ttydefaults.h> 43bc093719SEd Schouten #include <sys/uio.h> 44bc093719SEd Schouten #include <sys/vnode.h> 45bc093719SEd Schouten 469e589b09SBojan Novković #include <teken/teken.h> 479e589b09SBojan Novković #include <teken/teken_wcwidth.h> 489e589b09SBojan Novković 49bc093719SEd Schouten /* 50bc093719SEd Schouten * Standard TTYDISC `termios' line discipline. 51bc093719SEd Schouten */ 52bc093719SEd Schouten 53bc093719SEd Schouten /* Statistics. */ 541d952ed2SEd Schouten static unsigned long tty_nin = 0; 551d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD, 56bc093719SEd Schouten &tty_nin, 0, "Total amount of bytes received"); 571d952ed2SEd Schouten static unsigned long tty_nout = 0; 581d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, 59bc093719SEd Schouten &tty_nout, 0, "Total amount of bytes transmitted"); 60bc093719SEd Schouten 61bc093719SEd Schouten /* termios comparison macro's. */ 62bc093719SEd Schouten #define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \ 63bc093719SEd Schouten tp->t_termios.c_cc[v] == (c)) 64bc093719SEd Schouten #define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt)) 65bc093719SEd Schouten 66bc093719SEd Schouten /* Characters that cannot be modified through c_cc. */ 67bc093719SEd Schouten #define CTAB '\t' 68bc093719SEd Schouten #define CNL '\n' 69bc093719SEd Schouten #define CCR '\r' 70bc093719SEd Schouten 71bc093719SEd Schouten /* Character is a control character. */ 72bc093719SEd Schouten #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20) 73bc093719SEd Schouten /* Control character should be processed on echo. */ 74bc093719SEd Schouten #define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \ 75bc093719SEd Schouten (c) == CNL || (c) == CCR)) 76bc093719SEd Schouten /* Control character should be printed using ^X notation. */ 77bc093719SEd Schouten #define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \ 78bc093719SEd Schouten ((q) || ((c) != CTAB && (c) != CNL)))) 79bc093719SEd Schouten /* Character is whitespace. */ 80bc093719SEd Schouten #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB) 81bc093719SEd Schouten /* Character is alphanumeric. */ 82bc093719SEd Schouten #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \ 83bc093719SEd Schouten ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 849e589b09SBojan Novković /* Character is UTF8-encoded. */ 859e589b09SBojan Novković #define CTL_UTF8(c) (!!((c) & 0x80)) 869e589b09SBojan Novković /* Character is a UTF8 continuation byte. */ 879e589b09SBojan Novković #define CTL_UTF8_CONT(c) (((c) & 0xc0) == 0x80) 88bc093719SEd Schouten 89a1215e37SEd Schouten #define TTY_STACKBUF 256 909e589b09SBojan Novković #define UTF8_STACKBUF 4 91a1215e37SEd Schouten 92bc093719SEd Schouten void 93bc093719SEd Schouten ttydisc_open(struct tty *tp) 94bc093719SEd Schouten { 95bc093719SEd Schouten ttydisc_optimize(tp); 96bc093719SEd Schouten } 97bc093719SEd Schouten 98bc093719SEd Schouten void 99bc093719SEd Schouten ttydisc_close(struct tty *tp) 100bc093719SEd Schouten { 101bc093719SEd Schouten 102bc093719SEd Schouten /* Clean up our flags when leaving the discipline. */ 103bc093719SEd Schouten tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE); 104cbda6f66SWarner Losh tp->t_termios.c_lflag &= ~FLUSHO; 105bc093719SEd Schouten 1062cd5358aSKonstantin Belousov /* 1072cd5358aSKonstantin Belousov * POSIX states that we must drain output and flush input on 1082cd5358aSKonstantin Belousov * last close. Draining has already been done if possible. 1092cd5358aSKonstantin Belousov */ 1102cd5358aSKonstantin Belousov tty_flush(tp, FREAD | FWRITE); 111a1215e37SEd Schouten 112a1215e37SEd Schouten if (ttyhook_hashook(tp, close)) 113a1215e37SEd Schouten ttyhook_close(tp); 114bc093719SEd Schouten } 115bc093719SEd Schouten 116bc093719SEd Schouten static int 117bc093719SEd Schouten ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) 118bc093719SEd Schouten { 119bc093719SEd Schouten char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */ 120bc093719SEd Schouten int error; 121bc093719SEd Schouten size_t clen, flen = 0, n = 1; 122bc093719SEd Schouten unsigned char lastc = _POSIX_VDISABLE; 123bc093719SEd Schouten 124bc093719SEd Schouten #define BREAK_ADD(c) do { \ 125bc093719SEd Schouten if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \ 126bc093719SEd Schouten breakc[n++] = tp->t_termios.c_cc[c]; \ 127bc093719SEd Schouten } while (0) 128bc093719SEd Schouten /* Determine which characters we should trigger on. */ 129bc093719SEd Schouten BREAK_ADD(VEOF); 130bc093719SEd Schouten BREAK_ADD(VEOL); 131bc093719SEd Schouten #undef BREAK_ADD 132bc093719SEd Schouten breakc[n] = '\0'; 133bc093719SEd Schouten 134bc093719SEd Schouten do { 1351da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 1361da7bb41SEd Schouten if (error) 1371da7bb41SEd Schouten return (error); 1381da7bb41SEd Schouten 139bc093719SEd Schouten /* 140bc093719SEd Schouten * Quite a tricky case: unlike the old TTY 141bc093719SEd Schouten * implementation, this implementation copies data back 142bc093719SEd Schouten * to userspace in large chunks. Unfortunately, we can't 143bc093719SEd Schouten * calculate the line length on beforehand if it crosses 144bc093719SEd Schouten * ttyinq_block boundaries, because multiple reads could 145bc093719SEd Schouten * then make this code read beyond the newline. 146bc093719SEd Schouten * 147bc093719SEd Schouten * This is why we limit the read to: 148bc093719SEd Schouten * - The size the user has requested 149bc093719SEd Schouten * - The blocksize (done in tty_inq.c) 150bc093719SEd Schouten * - The amount of bytes until the newline 151bc093719SEd Schouten * 152bc093719SEd Schouten * This causes the line length to be recalculated after 153bc093719SEd Schouten * each block has been copied to userspace. This will 154bc093719SEd Schouten * cause the TTY layer to return data in chunks using 155bc093719SEd Schouten * the blocksize (except the first and last blocks). 156bc093719SEd Schouten */ 157bc093719SEd Schouten clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid, 158bc093719SEd Schouten &lastc); 159bc093719SEd Schouten 160bc093719SEd Schouten /* No more data. */ 161bc093719SEd Schouten if (clen == 0) { 1626b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 163bc093719SEd Schouten return (0); 1646b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 1656b1b791dSEd Schouten return (EWOULDBLOCK); 166bc093719SEd Schouten 167bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 168bc093719SEd Schouten if (error) 169bc093719SEd Schouten return (error); 170bc093719SEd Schouten continue; 171bc093719SEd Schouten } 172bc093719SEd Schouten 173bc093719SEd Schouten /* Don't send the EOF char back to userspace. */ 174bc093719SEd Schouten if (CMP_CC(VEOF, lastc)) 175bc093719SEd Schouten flen = 1; 176bc093719SEd Schouten 177bc093719SEd Schouten MPASS(flen <= clen); 178bc093719SEd Schouten 179bc093719SEd Schouten /* Read and throw away the EOF character. */ 180bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen); 181bc093719SEd Schouten if (error) 182bc093719SEd Schouten return (error); 183bc093719SEd Schouten 184bc093719SEd Schouten } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE); 185bc093719SEd Schouten 186bc093719SEd Schouten return (0); 187bc093719SEd Schouten } 188bc093719SEd Schouten 189bc093719SEd Schouten static int 190bc093719SEd Schouten ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag) 191bc093719SEd Schouten { 192bc093719SEd Schouten size_t vmin = tp->t_termios.c_cc[VMIN]; 193526d0bd5SKonstantin Belousov ssize_t oresid = uio->uio_resid; 194bc093719SEd Schouten int error; 195bc093719SEd Schouten 196bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] == 0); 197bc093719SEd Schouten 198bc093719SEd Schouten /* 199bc093719SEd Schouten * This routine implements the easy cases of read()s while in 200bc093719SEd Schouten * non-canonical mode, namely case B and D, where we don't have 201bc093719SEd Schouten * any timers at all. 202bc093719SEd Schouten */ 203bc093719SEd Schouten 204bc093719SEd Schouten for (;;) { 2051da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 2061da7bb41SEd Schouten if (error) 2071da7bb41SEd Schouten return (error); 2081da7bb41SEd Schouten 209bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 210bc093719SEd Schouten uio->uio_resid, 0); 211bc093719SEd Schouten if (error) 212bc093719SEd Schouten return (error); 213bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 214bc093719SEd Schouten return (0); 215bc093719SEd Schouten 216bc093719SEd Schouten /* We have to wait for more. */ 2176b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 218bc093719SEd Schouten return (0); 2196b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 2206b1b791dSEd Schouten return (EWOULDBLOCK); 221bc093719SEd Schouten 222bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 223bc093719SEd Schouten if (error) 224bc093719SEd Schouten return (error); 225bc093719SEd Schouten } 226bc093719SEd Schouten } 227bc093719SEd Schouten 228bc093719SEd Schouten static int 229bc093719SEd Schouten ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag, 230bc093719SEd Schouten int oresid) 231bc093719SEd Schouten { 232bc093719SEd Schouten size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1); 233bc093719SEd Schouten unsigned int vtime = tp->t_termios.c_cc[VTIME]; 234bc093719SEd Schouten struct timeval end, now, left; 235bc093719SEd Schouten int error, hz; 236bc093719SEd Schouten 237bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] != 0); 238bc093719SEd Schouten 239bc093719SEd Schouten /* Determine when the read should be expired. */ 240bc093719SEd Schouten end.tv_sec = vtime / 10; 241bc093719SEd Schouten end.tv_usec = (vtime % 10) * 100000; 242bc093719SEd Schouten getmicrotime(&now); 243bc093719SEd Schouten timevaladd(&end, &now); 244bc093719SEd Schouten 245bc093719SEd Schouten for (;;) { 2461da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 2471da7bb41SEd Schouten if (error) 2481da7bb41SEd Schouten return (error); 2491da7bb41SEd Schouten 250bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 251bc093719SEd Schouten uio->uio_resid, 0); 252bc093719SEd Schouten if (error) 253bc093719SEd Schouten return (error); 254bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 255bc093719SEd Schouten return (0); 256bc093719SEd Schouten 257bc093719SEd Schouten /* Calculate how long we should wait. */ 258bc093719SEd Schouten getmicrotime(&now); 259bc093719SEd Schouten if (timevalcmp(&now, &end, >)) 260bc093719SEd Schouten return (0); 261bc093719SEd Schouten left = end; 262bc093719SEd Schouten timevalsub(&left, &now); 263bc093719SEd Schouten hz = tvtohz(&left); 264bc093719SEd Schouten 265bc093719SEd Schouten /* 266bc093719SEd Schouten * We have to wait for more. If the timer expires, we 267bc093719SEd Schouten * should return a 0-byte read. 268bc093719SEd Schouten */ 2696b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 270bc093719SEd Schouten return (0); 2716b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 2726b1b791dSEd Schouten return (EWOULDBLOCK); 273bc093719SEd Schouten 274bc093719SEd Schouten error = tty_timedwait(tp, &tp->t_inwait, hz); 275bc093719SEd Schouten if (error) 276bc093719SEd Schouten return (error == EWOULDBLOCK ? 0 : error); 277bc093719SEd Schouten } 278bc093719SEd Schouten 279bc093719SEd Schouten return (0); 280bc093719SEd Schouten } 281bc093719SEd Schouten 282bc093719SEd Schouten static int 283bc093719SEd Schouten ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag) 284bc093719SEd Schouten { 285bc093719SEd Schouten size_t vmin = tp->t_termios.c_cc[VMIN]; 286526d0bd5SKonstantin Belousov ssize_t oresid = uio->uio_resid; 287bc093719SEd Schouten int error; 288bc093719SEd Schouten 289bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VMIN] != 0); 290bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] != 0); 291bc093719SEd Schouten 292bc093719SEd Schouten /* 293bc093719SEd Schouten * When using the interbyte timer, the timer should be started 294bc093719SEd Schouten * after the first byte has been received. We just call into the 295bc093719SEd Schouten * generic read timer code after we've received the first byte. 296bc093719SEd Schouten */ 297bc093719SEd Schouten 298bc093719SEd Schouten for (;;) { 2991da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 3001da7bb41SEd Schouten if (error) 3011da7bb41SEd Schouten return (error); 3021da7bb41SEd Schouten 303bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 304bc093719SEd Schouten uio->uio_resid, 0); 305bc093719SEd Schouten if (error) 306bc093719SEd Schouten return (error); 307bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 308bc093719SEd Schouten return (0); 309bc093719SEd Schouten 310bc093719SEd Schouten /* 311bc093719SEd Schouten * Not enough data, but we did receive some, which means 312bc093719SEd Schouten * we'll now start using the interbyte timer. 313bc093719SEd Schouten */ 314bc093719SEd Schouten if (oresid != uio->uio_resid) 315bc093719SEd Schouten break; 316bc093719SEd Schouten 317bc093719SEd Schouten /* We have to wait for more. */ 3186b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 319bc093719SEd Schouten return (0); 3206b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 3216b1b791dSEd Schouten return (EWOULDBLOCK); 322bc093719SEd Schouten 323bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 324bc093719SEd Schouten if (error) 325bc093719SEd Schouten return (error); 326bc093719SEd Schouten } 327bc093719SEd Schouten 328bc093719SEd Schouten return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid); 329bc093719SEd Schouten } 330bc093719SEd Schouten 331bc093719SEd Schouten int 332bc093719SEd Schouten ttydisc_read(struct tty *tp, struct uio *uio, int ioflag) 333bc093719SEd Schouten { 334bc093719SEd Schouten int error; 335bc093719SEd Schouten 33623d53268SKyle Evans tty_assert_locked(tp); 337bc093719SEd Schouten 338bc093719SEd Schouten if (uio->uio_resid == 0) 339bc093719SEd Schouten return (0); 340bc093719SEd Schouten 341bc093719SEd Schouten if (CMP_FLAG(l, ICANON)) 342bc093719SEd Schouten error = ttydisc_read_canonical(tp, uio, ioflag); 343bc093719SEd Schouten else if (tp->t_termios.c_cc[VTIME] == 0) 344bc093719SEd Schouten error = ttydisc_read_raw_no_timer(tp, uio, ioflag); 345bc093719SEd Schouten else if (tp->t_termios.c_cc[VMIN] == 0) 346bc093719SEd Schouten error = ttydisc_read_raw_read_timer(tp, uio, ioflag, 347bc093719SEd Schouten uio->uio_resid); 348bc093719SEd Schouten else 349bc093719SEd Schouten error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag); 350bc093719SEd Schouten 351a15ec0a5SEd Schouten if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow || 352a15ec0a5SEd Schouten ttyinq_bytescanonicalized(&tp->t_inq) == 0) { 353bc093719SEd Schouten /* Unset the input watermark when we've got enough space. */ 354bc093719SEd Schouten tty_hiwat_in_unblock(tp); 355bc093719SEd Schouten } 356bc093719SEd Schouten 357bc093719SEd Schouten return (error); 358bc093719SEd Schouten } 359bc093719SEd Schouten 360bc093719SEd Schouten static __inline unsigned int 361bc093719SEd Schouten ttydisc_findchar(const char *obstart, unsigned int oblen) 362bc093719SEd Schouten { 363bc093719SEd Schouten const char *c = obstart; 364bc093719SEd Schouten 365bc093719SEd Schouten while (oblen--) { 366bc093719SEd Schouten if (CTL_VALID(*c)) 367bc093719SEd Schouten break; 368bc093719SEd Schouten c++; 369bc093719SEd Schouten } 370bc093719SEd Schouten 371bc093719SEd Schouten return (c - obstart); 372bc093719SEd Schouten } 373bc093719SEd Schouten 374bc093719SEd Schouten static int 375bc093719SEd Schouten ttydisc_write_oproc(struct tty *tp, char c) 376bc093719SEd Schouten { 377bc093719SEd Schouten unsigned int scnt, error; 378bc093719SEd Schouten 379bc093719SEd Schouten MPASS(CMP_FLAG(o, OPOST)); 380bc093719SEd Schouten MPASS(CTL_VALID(c)); 381bc093719SEd Schouten 382bc093719SEd Schouten #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1) 383bc093719SEd Schouten switch (c) { 384bc093719SEd Schouten case CEOF: 385bc093719SEd Schouten /* End-of-text dropping. */ 386bc093719SEd Schouten if (CMP_FLAG(o, ONOEOT)) 387bc093719SEd Schouten return (0); 388bc093719SEd Schouten return PRINT_NORMAL(); 389bc093719SEd Schouten 390bc093719SEd Schouten case CERASE2: 391bc093719SEd Schouten /* Handle backspace to fix tab expansion. */ 392bc093719SEd Schouten if (PRINT_NORMAL() != 0) 393bc093719SEd Schouten return (-1); 394bc093719SEd Schouten if (tp->t_column > 0) 395bc093719SEd Schouten tp->t_column--; 396bc093719SEd Schouten return (0); 397bc093719SEd Schouten 398bc093719SEd Schouten case CTAB: 399bc093719SEd Schouten /* Tab expansion. */ 400bc093719SEd Schouten scnt = 8 - (tp->t_column & 7); 401bc093719SEd Schouten if (CMP_FLAG(o, TAB3)) { 402bc093719SEd Schouten error = ttyoutq_write_nofrag(&tp->t_outq, 403bc093719SEd Schouten " ", scnt); 404bc093719SEd Schouten } else { 405bc093719SEd Schouten error = PRINT_NORMAL(); 406bc093719SEd Schouten } 407bc093719SEd Schouten if (error) 408bc093719SEd Schouten return (-1); 409bc093719SEd Schouten 410bc093719SEd Schouten tp->t_column += scnt; 411bc093719SEd Schouten MPASS((tp->t_column % 8) == 0); 412bc093719SEd Schouten return (0); 413bc093719SEd Schouten 414bc093719SEd Schouten case CNL: 415bc093719SEd Schouten /* Newline conversion. */ 416bc093719SEd Schouten if (CMP_FLAG(o, ONLCR)) { 417bc093719SEd Schouten /* Convert \n to \r\n. */ 418bc093719SEd Schouten error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2); 419bc093719SEd Schouten } else { 420bc093719SEd Schouten error = PRINT_NORMAL(); 421bc093719SEd Schouten } 422bc093719SEd Schouten if (error) 423bc093719SEd Schouten return (-1); 424bc093719SEd Schouten 425bc093719SEd Schouten if (CMP_FLAG(o, ONLCR|ONLRET)) { 426bc093719SEd Schouten tp->t_column = tp->t_writepos = 0; 427bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 428bc093719SEd Schouten } 429bc093719SEd Schouten return (0); 430bc093719SEd Schouten 431bc093719SEd Schouten case CCR: 432bc093719SEd Schouten /* Carriage return to newline conversion. */ 433bc093719SEd Schouten if (CMP_FLAG(o, OCRNL)) 434bc093719SEd Schouten c = CNL; 435bc093719SEd Schouten /* Omit carriage returns on column 0. */ 436bc093719SEd Schouten if (CMP_FLAG(o, ONOCR) && tp->t_column == 0) 437bc093719SEd Schouten return (0); 438bc093719SEd Schouten if (PRINT_NORMAL() != 0) 439bc093719SEd Schouten return (-1); 440bc093719SEd Schouten 441bc093719SEd Schouten tp->t_column = tp->t_writepos = 0; 442bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 443bc093719SEd Schouten return (0); 444bc093719SEd Schouten } 445bc093719SEd Schouten 446bc093719SEd Schouten /* 447bc093719SEd Schouten * Invisible control character. Print it, but don't 448bc093719SEd Schouten * increase the column count. 449bc093719SEd Schouten */ 450bc093719SEd Schouten return PRINT_NORMAL(); 451bc093719SEd Schouten #undef PRINT_NORMAL 452bc093719SEd Schouten } 453bc093719SEd Schouten 454bc093719SEd Schouten /* 455bc093719SEd Schouten * Just like the old TTY implementation, we need to copy data in chunks 456bc093719SEd Schouten * into a temporary buffer. One of the reasons why we need to do this, 457bc093719SEd Schouten * is because output processing (only TAB3 though) may allow the buffer 458bc093719SEd Schouten * to grow eight times. 459bc093719SEd Schouten */ 460bc093719SEd Schouten int 461bc093719SEd Schouten ttydisc_write(struct tty *tp, struct uio *uio, int ioflag) 462bc093719SEd Schouten { 463a1215e37SEd Schouten char ob[TTY_STACKBUF]; 464bc093719SEd Schouten char *obstart; 465bc093719SEd Schouten int error = 0; 466bc093719SEd Schouten unsigned int oblen = 0; 467bc093719SEd Schouten 46823d53268SKyle Evans tty_assert_locked(tp); 469bc093719SEd Schouten 470bc093719SEd Schouten if (tp->t_flags & TF_ZOMBIE) 471bc093719SEd Schouten return (EIO); 472bc093719SEd Schouten 473bc093719SEd Schouten /* 474bc093719SEd Schouten * We don't need to check whether the process is the foreground 475bc093719SEd Schouten * process group or if we have a carrier. This is already done 476bc093719SEd Schouten * in ttydev_write(). 477bc093719SEd Schouten */ 478bc093719SEd Schouten 479bc093719SEd Schouten while (uio->uio_resid > 0) { 480bc093719SEd Schouten unsigned int nlen; 481bc093719SEd Schouten 482bc093719SEd Schouten MPASS(oblen == 0); 483bc093719SEd Schouten 484cbda6f66SWarner Losh if (CMP_FLAG(l, FLUSHO)) { 485cbda6f66SWarner Losh uio->uio_offset += uio->uio_resid; 486cbda6f66SWarner Losh uio->uio_resid = 0; 487cbda6f66SWarner Losh return (0); 488cbda6f66SWarner Losh } 489cbda6f66SWarner Losh 490bc093719SEd Schouten /* Step 1: read data. */ 491bc093719SEd Schouten obstart = ob; 492bc093719SEd Schouten nlen = MIN(uio->uio_resid, sizeof ob); 49387fe0fa8SEd Schouten tty_unlock(tp); 494bc093719SEd Schouten error = uiomove(ob, nlen, uio); 49587fe0fa8SEd Schouten tty_lock(tp); 496bc093719SEd Schouten if (error != 0) 497bc093719SEd Schouten break; 498bc093719SEd Schouten oblen = nlen; 499bc093719SEd Schouten 500bc093719SEd Schouten if (tty_gone(tp)) { 501bc093719SEd Schouten error = ENXIO; 502bc093719SEd Schouten break; 503bc093719SEd Schouten } 504bc093719SEd Schouten 505bc093719SEd Schouten MPASS(oblen > 0); 506bc093719SEd Schouten 507bc093719SEd Schouten /* Step 2: process data. */ 508bc093719SEd Schouten do { 509bc093719SEd Schouten unsigned int plen, wlen; 510bc093719SEd Schouten 511cbda6f66SWarner Losh if (CMP_FLAG(l, FLUSHO)) { 512cbda6f66SWarner Losh uio->uio_offset += uio->uio_resid; 513cbda6f66SWarner Losh uio->uio_resid = 0; 514cbda6f66SWarner Losh return (0); 515cbda6f66SWarner Losh } 516cbda6f66SWarner Losh 517bc093719SEd Schouten /* Search for special characters for post processing. */ 518bc093719SEd Schouten if (CMP_FLAG(o, OPOST)) { 519bc093719SEd Schouten plen = ttydisc_findchar(obstart, oblen); 520bc093719SEd Schouten } else { 521bc093719SEd Schouten plen = oblen; 522bc093719SEd Schouten } 523bc093719SEd Schouten 524bc093719SEd Schouten if (plen == 0) { 525bc093719SEd Schouten /* 526bc093719SEd Schouten * We're going to process a character 527bc093719SEd Schouten * that needs processing 528bc093719SEd Schouten */ 529bc093719SEd Schouten if (ttydisc_write_oproc(tp, *obstart) == 0) { 530bc093719SEd Schouten obstart++; 531bc093719SEd Schouten oblen--; 532bc093719SEd Schouten 533bc093719SEd Schouten tp->t_writepos = tp->t_column; 534bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 535bc093719SEd Schouten continue; 536bc093719SEd Schouten } 537bc093719SEd Schouten } else { 538bc093719SEd Schouten /* We're going to write regular data. */ 539bc093719SEd Schouten wlen = ttyoutq_write(&tp->t_outq, obstart, plen); 540bc093719SEd Schouten obstart += wlen; 541bc093719SEd Schouten oblen -= wlen; 542bc093719SEd Schouten tp->t_column += wlen; 543bc093719SEd Schouten 544bc093719SEd Schouten tp->t_writepos = tp->t_column; 545bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 546bc093719SEd Schouten 547bc093719SEd Schouten if (wlen == plen) 548bc093719SEd Schouten continue; 549bc093719SEd Schouten } 550bc093719SEd Schouten 551bc093719SEd Schouten /* Watermark reached. Try to sleep. */ 552bc093719SEd Schouten tp->t_flags |= TF_HIWAT_OUT; 553bc093719SEd Schouten 554bc093719SEd Schouten if (ioflag & IO_NDELAY) { 555bc093719SEd Schouten error = EWOULDBLOCK; 556bc093719SEd Schouten goto done; 557bc093719SEd Schouten } 558bc093719SEd Schouten 559bc093719SEd Schouten /* 560bc093719SEd Schouten * The driver may write back the data 561bc093719SEd Schouten * synchronously. Be sure to check the high 562bc093719SEd Schouten * water mark before going to sleep. 563bc093719SEd Schouten */ 564bc093719SEd Schouten ttydevsw_outwakeup(tp); 565bc093719SEd Schouten if ((tp->t_flags & TF_HIWAT_OUT) == 0) 566bc093719SEd Schouten continue; 567bc093719SEd Schouten 568bc093719SEd Schouten error = tty_wait(tp, &tp->t_outwait); 569bc093719SEd Schouten if (error) 570bc093719SEd Schouten goto done; 571bc093719SEd Schouten 572bc093719SEd Schouten if (tp->t_flags & TF_ZOMBIE) { 573bc093719SEd Schouten error = EIO; 574bc093719SEd Schouten goto done; 575bc093719SEd Schouten } 576bc093719SEd Schouten } while (oblen > 0); 577bc093719SEd Schouten } 578bc093719SEd Schouten 579bc093719SEd Schouten done: 580856ebf85SChristian S.J. Peron if (!tty_gone(tp)) 581bc093719SEd Schouten ttydevsw_outwakeup(tp); 582bc093719SEd Schouten 583bc093719SEd Schouten /* 584bc093719SEd Schouten * Add the amount of bytes that we didn't process back to the 585bc093719SEd Schouten * uio counters. We need to do this to make sure write() doesn't 586bc093719SEd Schouten * count the bytes we didn't store in the queue. 587bc093719SEd Schouten */ 588bc093719SEd Schouten uio->uio_resid += oblen; 589bc093719SEd Schouten return (error); 590bc093719SEd Schouten } 591bc093719SEd Schouten 592bc093719SEd Schouten void 593bc093719SEd Schouten ttydisc_optimize(struct tty *tp) 594bc093719SEd Schouten { 59523d53268SKyle Evans tty_assert_locked(tp); 596bc093719SEd Schouten 59714358b0fSEd Schouten if (ttyhook_hashook(tp, rint_bypass)) { 59814358b0fSEd Schouten tp->t_flags |= TF_BYPASS; 59914358b0fSEd Schouten } else if (ttyhook_hashook(tp, rint)) { 60014358b0fSEd Schouten tp->t_flags &= ~TF_BYPASS; 60114358b0fSEd Schouten } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) && 602bc093719SEd Schouten (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) && 603bc093719SEd Schouten (!CMP_FLAG(i, PARMRK) || 604bc093719SEd Schouten CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) && 60514358b0fSEd Schouten !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) { 606bc093719SEd Schouten tp->t_flags |= TF_BYPASS; 607bc093719SEd Schouten } else { 608bc093719SEd Schouten tp->t_flags &= ~TF_BYPASS; 609bc093719SEd Schouten } 610bc093719SEd Schouten } 611bc093719SEd Schouten 612bc093719SEd Schouten void 613bc093719SEd Schouten ttydisc_modem(struct tty *tp, int open) 614bc093719SEd Schouten { 615bc093719SEd Schouten 61623d53268SKyle Evans tty_assert_locked(tp); 617bc093719SEd Schouten 618bc093719SEd Schouten if (open) 619bc093719SEd Schouten cv_broadcast(&tp->t_dcdwait); 620bc093719SEd Schouten 621bc093719SEd Schouten /* 622bc093719SEd Schouten * Ignore modem status lines when CLOCAL is turned on, but don't 623bc093719SEd Schouten * enter the zombie state when the TTY isn't opened, because 624bc093719SEd Schouten * that would cause the TTY to be in zombie state after being 625bc093719SEd Schouten * opened. 626bc093719SEd Schouten */ 627bc093719SEd Schouten if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL)) 628bc093719SEd Schouten return; 629bc093719SEd Schouten 630bc093719SEd Schouten if (open == 0) { 631bc093719SEd Schouten /* 632bc093719SEd Schouten * Lost carrier. 633bc093719SEd Schouten */ 634bc093719SEd Schouten tp->t_flags |= TF_ZOMBIE; 635bc093719SEd Schouten 636bc093719SEd Schouten tty_signal_sessleader(tp, SIGHUP); 637bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 638bc093719SEd Schouten } else { 639bc093719SEd Schouten /* 640bc093719SEd Schouten * Carrier is back again. 641bc093719SEd Schouten */ 642bc093719SEd Schouten 643bc093719SEd Schouten /* XXX: what should we do here? */ 644bc093719SEd Schouten } 645bc093719SEd Schouten } 646bc093719SEd Schouten 647bc093719SEd Schouten static int 648bc093719SEd Schouten ttydisc_echo_force(struct tty *tp, char c, int quote) 649bc093719SEd Schouten { 650bc093719SEd Schouten 651cbda6f66SWarner Losh if (CMP_FLAG(l, FLUSHO)) 652cbda6f66SWarner Losh return 0; 653cbda6f66SWarner Losh 654bc093719SEd Schouten if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) { 655bc093719SEd Schouten /* 656bc093719SEd Schouten * Only perform postprocessing when OPOST is turned on 657bc093719SEd Schouten * and the character is an unquoted BS/TB/NL/CR. 658bc093719SEd Schouten */ 659bc093719SEd Schouten return ttydisc_write_oproc(tp, c); 660bc093719SEd Schouten } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) { 661bc093719SEd Schouten /* 662bc093719SEd Schouten * Only use ^X notation when ECHOCTL is turned on and 663bc093719SEd Schouten * we've got an quoted control character. 66439410373SEd Schouten * 66539410373SEd Schouten * Print backspaces when echoing an end-of-file. 666bc093719SEd Schouten */ 66739410373SEd Schouten char ob[4] = "^?\b\b"; 668bc093719SEd Schouten 669bc093719SEd Schouten /* Print ^X notation. */ 670bc093719SEd Schouten if (c != 0x7f) 671bc093719SEd Schouten ob[1] = c + 'A' - 1; 672bc093719SEd Schouten 67339410373SEd Schouten if (!quote && CMP_CC(VEOF, c)) { 67439410373SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, ob, 4); 67539410373SEd Schouten } else { 676bc093719SEd Schouten tp->t_column += 2; 677bc093719SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, ob, 2); 67839410373SEd Schouten } 679bc093719SEd Schouten } else { 680bc093719SEd Schouten /* Can just be printed. */ 681bc093719SEd Schouten tp->t_column++; 682bc093719SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, &c, 1); 683bc093719SEd Schouten } 684bc093719SEd Schouten } 685bc093719SEd Schouten 686bc093719SEd Schouten static int 687bc093719SEd Schouten ttydisc_echo(struct tty *tp, char c, int quote) 688bc093719SEd Schouten { 689bc093719SEd Schouten 690bc093719SEd Schouten /* 691bc093719SEd Schouten * Only echo characters when ECHO is turned on, or ECHONL when 692bc093719SEd Schouten * the character is an unquoted newline. 693bc093719SEd Schouten */ 694bc093719SEd Schouten if (!CMP_FLAG(l, ECHO) && 695bc093719SEd Schouten (!CMP_FLAG(l, ECHONL) || c != CNL || quote)) 696bc093719SEd Schouten return (0); 697bc093719SEd Schouten 698bc093719SEd Schouten return ttydisc_echo_force(tp, c, quote); 699bc093719SEd Schouten } 700bc093719SEd Schouten 701bc093719SEd Schouten static void 702bc093719SEd Schouten ttydisc_reprint_char(void *d, char c, int quote) 703bc093719SEd Schouten { 704bc093719SEd Schouten struct tty *tp = d; 705bc093719SEd Schouten 706bc093719SEd Schouten ttydisc_echo(tp, c, quote); 707bc093719SEd Schouten } 708bc093719SEd Schouten 709bc093719SEd Schouten static void 710bc093719SEd Schouten ttydisc_reprint(struct tty *tp) 711bc093719SEd Schouten { 712bc093719SEd Schouten cc_t c; 713bc093719SEd Schouten 714bc093719SEd Schouten /* Print ^R\n, followed by the line. */ 715bc093719SEd Schouten c = tp->t_termios.c_cc[VREPRINT]; 716bc093719SEd Schouten if (c != _POSIX_VDISABLE) 717bc093719SEd Schouten ttydisc_echo(tp, c, 0); 718bc093719SEd Schouten ttydisc_echo(tp, CNL, 0); 719bc093719SEd Schouten ttyinq_reprintpos_reset(&tp->t_inq); 720bc093719SEd Schouten 721bc093719SEd Schouten ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp); 722bc093719SEd Schouten } 723bc093719SEd Schouten 724bc093719SEd Schouten struct ttydisc_recalc_length { 725bc093719SEd Schouten struct tty *tp; 726bc093719SEd Schouten unsigned int curlen; 727bc093719SEd Schouten }; 728bc093719SEd Schouten 729bc093719SEd Schouten static void 730bc093719SEd Schouten ttydisc_recalc_charlength(void *d, char c, int quote) 731bc093719SEd Schouten { 732bc093719SEd Schouten struct ttydisc_recalc_length *data = d; 733bc093719SEd Schouten struct tty *tp = data->tp; 734bc093719SEd Schouten 735bc093719SEd Schouten if (CTL_PRINT(c, quote)) { 736bc093719SEd Schouten if (CMP_FLAG(l, ECHOCTL)) 737bc093719SEd Schouten data->curlen += 2; 738bc093719SEd Schouten } else if (c == CTAB) { 739bc093719SEd Schouten data->curlen += 8 - (data->curlen & 7); 740bc093719SEd Schouten } else { 741bc093719SEd Schouten data->curlen++; 742bc093719SEd Schouten } 743bc093719SEd Schouten } 744bc093719SEd Schouten 745bc093719SEd Schouten static unsigned int 746bc093719SEd Schouten ttydisc_recalc_linelength(struct tty *tp) 747bc093719SEd Schouten { 748bc093719SEd Schouten struct ttydisc_recalc_length data = { tp, tp->t_writepos }; 749bc093719SEd Schouten 750bc093719SEd Schouten ttyinq_line_iterate_from_reprintpos(&tp->t_inq, 751bc093719SEd Schouten ttydisc_recalc_charlength, &data); 752bc093719SEd Schouten return (data.curlen); 753bc093719SEd Schouten } 754bc093719SEd Schouten 755bc093719SEd Schouten static int 756bc093719SEd Schouten ttydisc_rubchar(struct tty *tp) 757bc093719SEd Schouten { 758bc093719SEd Schouten char c; 759bc093719SEd Schouten int quote; 760bc093719SEd Schouten unsigned int prevpos, tablen; 761bc093719SEd Schouten 762bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 763bc093719SEd Schouten return (-1); 764bc093719SEd Schouten ttyinq_unputchar(&tp->t_inq); 765bc093719SEd Schouten 766bc093719SEd Schouten if (CMP_FLAG(l, ECHO)) { 767bc093719SEd Schouten /* 768bc093719SEd Schouten * Remove the character from the screen. This is even 769bc093719SEd Schouten * safe for characters that span multiple characters 770bc093719SEd Schouten * (tabs, quoted, etc). 771bc093719SEd Schouten */ 772bc093719SEd Schouten if (tp->t_writepos >= tp->t_column) { 773bc093719SEd Schouten /* Retype the sentence. */ 774bc093719SEd Schouten ttydisc_reprint(tp); 775bc093719SEd Schouten } else if (CMP_FLAG(l, ECHOE)) { 776bc093719SEd Schouten if (CTL_PRINT(c, quote)) { 777bc093719SEd Schouten /* Remove ^X formatted chars. */ 778bc093719SEd Schouten if (CMP_FLAG(l, ECHOCTL)) { 779bc093719SEd Schouten tp->t_column -= 2; 780bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, 781bc093719SEd Schouten "\b\b \b\b", 6); 782bc093719SEd Schouten } 783bc093719SEd Schouten } else if (c == ' ') { 784bc093719SEd Schouten /* Space character needs no rubbing. */ 785bc093719SEd Schouten tp->t_column -= 1; 786bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\b", 1); 787bc093719SEd Schouten } else if (c == CTAB) { 788bc093719SEd Schouten /* 789bc093719SEd Schouten * Making backspace work with tabs is 790bc093719SEd Schouten * quite hard. Recalculate the length of 791bc093719SEd Schouten * this character and remove it. 792bc093719SEd Schouten * 793bc093719SEd Schouten * Because terminal settings could be 794bc093719SEd Schouten * changed while the line is being 795bc093719SEd Schouten * inserted, the calculations don't have 796bc093719SEd Schouten * to be correct. Make sure we keep the 797bc093719SEd Schouten * tab length within proper bounds. 798bc093719SEd Schouten */ 799bc093719SEd Schouten prevpos = ttydisc_recalc_linelength(tp); 800bc093719SEd Schouten if (prevpos >= tp->t_column) 801bc093719SEd Schouten tablen = 1; 802bc093719SEd Schouten else 803bc093719SEd Schouten tablen = tp->t_column - prevpos; 804bc093719SEd Schouten if (tablen > 8) 805bc093719SEd Schouten tablen = 8; 806bc093719SEd Schouten 807bc093719SEd Schouten tp->t_column = prevpos; 808bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, 809bc093719SEd Schouten "\b\b\b\b\b\b\b\b", tablen); 810bc093719SEd Schouten return (0); 8119e589b09SBojan Novković } else if ((tp->t_termios.c_iflag & IUTF8) != 0 && 8129e589b09SBojan Novković CTL_UTF8(c)) { 8139e589b09SBojan Novković uint8_t bytes[UTF8_STACKBUF] = { 0 }; 8149e589b09SBojan Novković int curidx = UTF8_STACKBUF - 1, cwidth = 1, 8159e589b09SBojan Novković nb = 0; 8169e589b09SBojan Novković teken_char_t codepoint; 8179e589b09SBojan Novković 8189e589b09SBojan Novković /* Save current byte. */ 8199e589b09SBojan Novković bytes[curidx] = c; 8209e589b09SBojan Novković curidx--; 8219e589b09SBojan Novković nb++; 8229e589b09SBojan Novković /* Loop back through inq until we hit the 8239e589b09SBojan Novković * leading byte. */ 8249e589b09SBojan Novković while (CTL_UTF8_CONT(c) && nb < UTF8_STACKBUF) { 8259e589b09SBojan Novković ttyinq_peekchar(&tp->t_inq, &c, "e); 8269e589b09SBojan Novković ttyinq_unputchar(&tp->t_inq); 8279e589b09SBojan Novković bytes[curidx] = c; 8289e589b09SBojan Novković curidx--; 8299e589b09SBojan Novković nb++; 8309e589b09SBojan Novković } 8319e589b09SBojan Novković /* 8329e589b09SBojan Novković * Shift array so that the leading 8339e589b09SBojan Novković * byte ends up at idx 0. 8349e589b09SBojan Novković */ 8359e589b09SBojan Novković if (nb < UTF8_STACKBUF) 8369e589b09SBojan Novković memmove(&bytes[0], &bytes[curidx + 1], 8379e589b09SBojan Novković nb * sizeof(uint8_t)); 8389e589b09SBojan Novković /* Check for malformed UTF8 characters. */ 8399e589b09SBojan Novković if (nb == UTF8_STACKBUF && 8409e589b09SBojan Novković CTL_UTF8_CONT(bytes[0])) { 8419e589b09SBojan Novković /* 8429e589b09SBojan Novković * Place all bytes back into the inq and 8439e589b09SBojan Novković * delete the last byte only. 8449e589b09SBojan Novković */ 8459e589b09SBojan Novković ttyinq_write(&tp->t_inq, bytes, 8469e589b09SBojan Novković UTF8_STACKBUF, 0); 847*2fed1c57SBojan Novković ttyinq_unputchar(&tp->t_inq); 8489e589b09SBojan Novković } else { 8499e589b09SBojan Novković /* Find codepoint and width. */ 8509e589b09SBojan Novković codepoint = 8519e589b09SBojan Novković teken_utf8_bytes_to_codepoint(bytes, 8529e589b09SBojan Novković nb); 853*2fed1c57SBojan Novković if (codepoint == 854*2fed1c57SBojan Novković TEKEN_UTF8_INVALID_CODEPOINT || 855*2fed1c57SBojan Novković (cwidth = teken_wcwidth( 856*2fed1c57SBojan Novković codepoint)) == -1) { 8579e589b09SBojan Novković /* 8589e589b09SBojan Novković * Place all bytes back into the 8599e589b09SBojan Novković * inq and fall back to 8609e589b09SBojan Novković * default behaviour. 8619e589b09SBojan Novković */ 862*2fed1c57SBojan Novković cwidth = 1; 8639e589b09SBojan Novković ttyinq_write(&tp->t_inq, bytes, 8649e589b09SBojan Novković nb, 0); 865*2fed1c57SBojan Novković ttyinq_unputchar(&tp->t_inq); 8669e589b09SBojan Novković } 8679e589b09SBojan Novković } 8689e589b09SBojan Novković tp->t_column -= cwidth; 8699e589b09SBojan Novković /* 8709e589b09SBojan Novković * Delete character by punching 8719e589b09SBojan Novković * 'cwidth' spaces over it. 8729e589b09SBojan Novković */ 8739e589b09SBojan Novković if (cwidth == 1) 8749e589b09SBojan Novković ttyoutq_write_nofrag(&tp->t_outq, 8759e589b09SBojan Novković "\b \b", 3); 8769e589b09SBojan Novković else if (cwidth == 2) 8779e589b09SBojan Novković ttyoutq_write_nofrag(&tp->t_outq, 8789e589b09SBojan Novković "\b\b \b\b", 6); 879bc093719SEd Schouten } else { 880bc093719SEd Schouten /* 881bc093719SEd Schouten * Remove a regular character by 882bc093719SEd Schouten * punching a space over it. 883bc093719SEd Schouten */ 884bc093719SEd Schouten tp->t_column -= 1; 885bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3); 886bc093719SEd Schouten } 887bc093719SEd Schouten } else { 888bc093719SEd Schouten /* Don't print spaces. */ 889bc093719SEd Schouten ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0); 890bc093719SEd Schouten } 891bc093719SEd Schouten } 892bc093719SEd Schouten 893bc093719SEd Schouten return (0); 894bc093719SEd Schouten } 895bc093719SEd Schouten 896bc093719SEd Schouten static void 897bc093719SEd Schouten ttydisc_rubword(struct tty *tp) 898bc093719SEd Schouten { 899bc093719SEd Schouten char c; 900bc093719SEd Schouten int quote, alnum; 901bc093719SEd Schouten 902bc093719SEd Schouten /* Strip whitespace first. */ 903bc093719SEd Schouten for (;;) { 904bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 905bc093719SEd Schouten return; 906bc093719SEd Schouten if (!CTL_WHITE(c)) 907bc093719SEd Schouten break; 908bc093719SEd Schouten ttydisc_rubchar(tp); 909bc093719SEd Schouten } 910bc093719SEd Schouten 911bc093719SEd Schouten /* 912bc093719SEd Schouten * Record whether the last character from the previous iteration 913bc093719SEd Schouten * was alphanumeric or not. We need this to implement ALTWERASE. 914bc093719SEd Schouten */ 915bc093719SEd Schouten alnum = CTL_ALNUM(c); 916bc093719SEd Schouten for (;;) { 917bc093719SEd Schouten ttydisc_rubchar(tp); 918bc093719SEd Schouten 919bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 920bc093719SEd Schouten return; 921bc093719SEd Schouten if (CTL_WHITE(c)) 922bc093719SEd Schouten return; 923bc093719SEd Schouten if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum) 924bc093719SEd Schouten return; 925bc093719SEd Schouten } 926bc093719SEd Schouten } 927bc093719SEd Schouten 928bc093719SEd Schouten int 929bc093719SEd Schouten ttydisc_rint(struct tty *tp, char c, int flags) 930bc093719SEd Schouten { 931bc093719SEd Schouten int signal, quote = 0; 932bc093719SEd Schouten char ob[3] = { 0xff, 0x00 }; 933bc093719SEd Schouten size_t ol; 934bc093719SEd Schouten 93523d53268SKyle Evans tty_assert_locked(tp); 936bc093719SEd Schouten 937bc093719SEd Schouten atomic_add_long(&tty_nin, 1); 938bc093719SEd Schouten 939a1215e37SEd Schouten if (ttyhook_hashook(tp, rint)) 940a1215e37SEd Schouten return ttyhook_rint(tp, c, flags); 941a1215e37SEd Schouten 942bc093719SEd Schouten if (tp->t_flags & TF_BYPASS) 943bc093719SEd Schouten goto processed; 944bc093719SEd Schouten 945bc093719SEd Schouten if (flags) { 946bc093719SEd Schouten if (flags & TRE_BREAK) { 947bc093719SEd Schouten if (CMP_FLAG(i, IGNBRK)) { 948bc093719SEd Schouten /* Ignore break characters. */ 949bc093719SEd Schouten return (0); 950bc093719SEd Schouten } else if (CMP_FLAG(i, BRKINT)) { 951bc093719SEd Schouten /* Generate SIGINT on break. */ 952bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 953bc093719SEd Schouten tty_signal_pgrp(tp, SIGINT); 954bc093719SEd Schouten return (0); 955bc093719SEd Schouten } else { 956bc093719SEd Schouten /* Just print it. */ 957bc093719SEd Schouten goto parmrk; 958bc093719SEd Schouten } 959bc093719SEd Schouten } else if (flags & TRE_FRAMING || 960bc093719SEd Schouten (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) { 961bc093719SEd Schouten if (CMP_FLAG(i, IGNPAR)) { 962bc093719SEd Schouten /* Ignore bad characters. */ 963bc093719SEd Schouten return (0); 964bc093719SEd Schouten } else { 965bc093719SEd Schouten /* Just print it. */ 966bc093719SEd Schouten goto parmrk; 967bc093719SEd Schouten } 968bc093719SEd Schouten } 969bc093719SEd Schouten } 970bc093719SEd Schouten 971bc093719SEd Schouten /* Allow any character to perform a wakeup. */ 972cbda6f66SWarner Losh if (CMP_FLAG(i, IXANY)) { 973bc093719SEd Schouten tp->t_flags &= ~TF_STOPPED; 974cbda6f66SWarner Losh tp->t_termios.c_lflag &= ~FLUSHO; 975cbda6f66SWarner Losh } 976bc093719SEd Schouten 977bc093719SEd Schouten /* Remove the top bit. */ 978bc093719SEd Schouten if (CMP_FLAG(i, ISTRIP)) 979bc093719SEd Schouten c &= ~0x80; 980bc093719SEd Schouten 981bc093719SEd Schouten /* Skip input processing when we want to print it literally. */ 982bc093719SEd Schouten if (tp->t_flags & TF_LITERAL) { 983bc093719SEd Schouten tp->t_flags &= ~TF_LITERAL; 984bc093719SEd Schouten quote = 1; 985bc093719SEd Schouten goto processed; 986bc093719SEd Schouten } 987bc093719SEd Schouten 988bc093719SEd Schouten /* Special control characters that are implementation dependent. */ 989bc093719SEd Schouten if (CMP_FLAG(l, IEXTEN)) { 990bc093719SEd Schouten /* Accept the next character as literal. */ 991bc093719SEd Schouten if (CMP_CC(VLNEXT, c)) { 992bc093719SEd Schouten if (CMP_FLAG(l, ECHO)) { 993bc093719SEd Schouten if (CMP_FLAG(l, ECHOE)) 994bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2); 995bc093719SEd Schouten else 996bc093719SEd Schouten ttydisc_echo(tp, c, 0); 997bc093719SEd Schouten } 998bc093719SEd Schouten tp->t_flags |= TF_LITERAL; 999bc093719SEd Schouten return (0); 1000bc093719SEd Schouten } 1001cbda6f66SWarner Losh /* Discard processing */ 1002cbda6f66SWarner Losh if (CMP_CC(VDISCARD, c)) { 1003cbda6f66SWarner Losh if (CMP_FLAG(l, FLUSHO)) { 1004cbda6f66SWarner Losh tp->t_termios.c_lflag &= ~FLUSHO; 1005cbda6f66SWarner Losh } else { 1006cbda6f66SWarner Losh tty_flush(tp, FWRITE); 1007cbda6f66SWarner Losh ttydisc_echo(tp, c, 0); 1008cbda6f66SWarner Losh if (tp->t_inq.ti_end > 0) 1009cbda6f66SWarner Losh ttydisc_reprint(tp); 1010cbda6f66SWarner Losh tp->t_termios.c_lflag |= FLUSHO; 1011cbda6f66SWarner Losh } 1012cbda6f66SWarner Losh } 1013bc093719SEd Schouten } 1014bc093719SEd Schouten 1015bc093719SEd Schouten /* 1016bc093719SEd Schouten * Handle signal processing. 1017bc093719SEd Schouten */ 1018bc093719SEd Schouten if (CMP_FLAG(l, ISIG)) { 1019bc093719SEd Schouten if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) { 1020bc093719SEd Schouten if (CMP_CC(VSTATUS, c)) { 1021bc093719SEd Schouten tty_signal_pgrp(tp, SIGINFO); 1022bc093719SEd Schouten return (0); 1023bc093719SEd Schouten } 1024bc093719SEd Schouten } 1025bc093719SEd Schouten 1026bc093719SEd Schouten /* 1027bc093719SEd Schouten * When compared to the old implementation, this 1028bc093719SEd Schouten * implementation also flushes the output queue. POSIX 1029bc093719SEd Schouten * is really brief about this, but does makes us assume 1030bc093719SEd Schouten * we have to do so. 1031bc093719SEd Schouten */ 1032bc093719SEd Schouten signal = 0; 1033bc093719SEd Schouten if (CMP_CC(VINTR, c)) { 1034bc093719SEd Schouten signal = SIGINT; 1035bc093719SEd Schouten } else if (CMP_CC(VQUIT, c)) { 1036bc093719SEd Schouten signal = SIGQUIT; 1037bc093719SEd Schouten } else if (CMP_CC(VSUSP, c)) { 1038bc093719SEd Schouten signal = SIGTSTP; 1039bc093719SEd Schouten } 1040bc093719SEd Schouten 1041bc093719SEd Schouten if (signal != 0) { 1042bc093719SEd Schouten /* 1043bc093719SEd Schouten * Echo the character before signalling the 1044bc093719SEd Schouten * processes. 1045bc093719SEd Schouten */ 1046bc093719SEd Schouten if (!CMP_FLAG(l, NOFLSH)) 1047bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 1048bc093719SEd Schouten ttydisc_echo(tp, c, 0); 1049bc093719SEd Schouten tty_signal_pgrp(tp, signal); 1050bc093719SEd Schouten return (0); 1051bc093719SEd Schouten } 1052bc093719SEd Schouten } 1053bc093719SEd Schouten 1054bc093719SEd Schouten /* 1055bc093719SEd Schouten * Handle start/stop characters. 1056bc093719SEd Schouten */ 1057bc093719SEd Schouten if (CMP_FLAG(i, IXON)) { 1058bc093719SEd Schouten if (CMP_CC(VSTOP, c)) { 1059bc093719SEd Schouten /* Stop it if we aren't stopped yet. */ 1060bc093719SEd Schouten if ((tp->t_flags & TF_STOPPED) == 0) { 1061bc093719SEd Schouten tp->t_flags |= TF_STOPPED; 1062bc093719SEd Schouten return (0); 1063bc093719SEd Schouten } 1064bc093719SEd Schouten /* 1065bc093719SEd Schouten * Fallthrough: 1066bc093719SEd Schouten * When VSTART == VSTOP, we should make this key 1067bc093719SEd Schouten * toggle it. 1068bc093719SEd Schouten */ 1069bc093719SEd Schouten if (!CMP_CC(VSTART, c)) 1070bc093719SEd Schouten return (0); 1071bc093719SEd Schouten } 1072bc093719SEd Schouten if (CMP_CC(VSTART, c)) { 1073bc093719SEd Schouten tp->t_flags &= ~TF_STOPPED; 1074bc093719SEd Schouten return (0); 1075bc093719SEd Schouten } 1076bc093719SEd Schouten } 1077bc093719SEd Schouten 1078bc093719SEd Schouten /* Conversion of CR and NL. */ 1079bc093719SEd Schouten switch (c) { 1080bc093719SEd Schouten case CCR: 1081bc093719SEd Schouten if (CMP_FLAG(i, IGNCR)) 1082bc093719SEd Schouten return (0); 1083bc093719SEd Schouten if (CMP_FLAG(i, ICRNL)) 1084bc093719SEd Schouten c = CNL; 1085bc093719SEd Schouten break; 1086bc093719SEd Schouten case CNL: 1087bc093719SEd Schouten if (CMP_FLAG(i, INLCR)) 1088bc093719SEd Schouten c = CCR; 1089bc093719SEd Schouten break; 1090bc093719SEd Schouten } 1091bc093719SEd Schouten 1092bc093719SEd Schouten /* Canonical line editing. */ 1093bc093719SEd Schouten if (CMP_FLAG(l, ICANON)) { 1094bc093719SEd Schouten if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) { 1095bc093719SEd Schouten ttydisc_rubchar(tp); 1096bc093719SEd Schouten return (0); 1097bc093719SEd Schouten } else if (CMP_CC(VKILL, c)) { 1098bc093719SEd Schouten while (ttydisc_rubchar(tp) == 0); 1099bc093719SEd Schouten return (0); 1100bc093719SEd Schouten } else if (CMP_FLAG(l, IEXTEN)) { 1101bc093719SEd Schouten if (CMP_CC(VWERASE, c)) { 1102bc093719SEd Schouten ttydisc_rubword(tp); 1103bc093719SEd Schouten return (0); 1104bc093719SEd Schouten } else if (CMP_CC(VREPRINT, c)) { 1105bc093719SEd Schouten ttydisc_reprint(tp); 1106bc093719SEd Schouten return (0); 1107bc093719SEd Schouten } 1108bc093719SEd Schouten } 1109bc093719SEd Schouten } 1110bc093719SEd Schouten 1111bc093719SEd Schouten processed: 1112bc093719SEd Schouten if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) { 1113bc093719SEd Schouten /* Print 0xff 0xff. */ 1114bc093719SEd Schouten ob[1] = 0xff; 1115bc093719SEd Schouten ol = 2; 1116bc093719SEd Schouten quote = 1; 1117bc093719SEd Schouten } else { 1118bc093719SEd Schouten ob[0] = c; 1119bc093719SEd Schouten ol = 1; 1120bc093719SEd Schouten } 1121bc093719SEd Schouten 1122bc093719SEd Schouten goto print; 1123bc093719SEd Schouten 1124bc093719SEd Schouten parmrk: 1125bc093719SEd Schouten if (CMP_FLAG(i, PARMRK)) { 1126bc093719SEd Schouten /* Prepend 0xff 0x00 0x.. */ 1127bc093719SEd Schouten ob[2] = c; 1128bc093719SEd Schouten ol = 3; 1129bc093719SEd Schouten quote = 1; 1130bc093719SEd Schouten } else { 1131bc093719SEd Schouten ob[0] = c; 1132bc093719SEd Schouten ol = 1; 1133bc093719SEd Schouten } 1134bc093719SEd Schouten 1135bc093719SEd Schouten print: 1136bc093719SEd Schouten /* See if we can store this on the input queue. */ 1137bc093719SEd Schouten if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) { 1138a15ec0a5SEd Schouten if (CMP_FLAG(i, IMAXBEL)) 1139a15ec0a5SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\a", 1); 1140a15ec0a5SEd Schouten 1141a15ec0a5SEd Schouten /* 1142a15ec0a5SEd Schouten * Prevent a deadlock here. It may be possible that a 1143a15ec0a5SEd Schouten * user has entered so much data, there is no data 1144a15ec0a5SEd Schouten * available to read(), but the buffers are full anyway. 1145a15ec0a5SEd Schouten * 1146a15ec0a5SEd Schouten * Only enter the high watermark if the device driver 1147a15ec0a5SEd Schouten * can actually transmit something. 1148a15ec0a5SEd Schouten */ 1149a15ec0a5SEd Schouten if (ttyinq_bytescanonicalized(&tp->t_inq) == 0) 1150a15ec0a5SEd Schouten return (0); 1151a15ec0a5SEd Schouten 1152bc093719SEd Schouten tty_hiwat_in_block(tp); 1153bc093719SEd Schouten return (-1); 1154bc093719SEd Schouten } 1155bc093719SEd Schouten 1156bc093719SEd Schouten /* 1157bc093719SEd Schouten * In raw mode, we canonicalize after receiving a single 1158bc093719SEd Schouten * character. Otherwise, we canonicalize when we receive a 1159bc093719SEd Schouten * newline, VEOL or VEOF, but only when it isn't quoted. 1160bc093719SEd Schouten */ 1161bc093719SEd Schouten if (!CMP_FLAG(l, ICANON) || 1162bc093719SEd Schouten (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) { 1163bc093719SEd Schouten ttyinq_canonicalize(&tp->t_inq); 1164bc093719SEd Schouten } 1165bc093719SEd Schouten 1166bc093719SEd Schouten ttydisc_echo(tp, c, quote); 1167bc093719SEd Schouten 1168bc093719SEd Schouten return (0); 1169bc093719SEd Schouten } 1170bc093719SEd Schouten 1171bc093719SEd Schouten size_t 11725c67885aSEd Schouten ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len) 11735c67885aSEd Schouten { 11745c67885aSEd Schouten const char *cbuf; 11755c67885aSEd Schouten 11765c67885aSEd Schouten if (ttydisc_can_bypass(tp)) 11775c67885aSEd Schouten return (ttydisc_rint_bypass(tp, buf, len)); 11785c67885aSEd Schouten 11795c67885aSEd Schouten for (cbuf = buf; len-- > 0; cbuf++) { 11805c67885aSEd Schouten if (ttydisc_rint(tp, *cbuf, 0) != 0) 11815c67885aSEd Schouten break; 11825c67885aSEd Schouten } 11835c67885aSEd Schouten 11845c67885aSEd Schouten return (cbuf - (const char *)buf); 11855c67885aSEd Schouten } 11865c67885aSEd Schouten 11875c67885aSEd Schouten size_t 1188d344ffe5SEd Schouten ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len) 1189bc093719SEd Schouten { 1190bc093719SEd Schouten size_t ret; 1191bc093719SEd Schouten 119223d53268SKyle Evans tty_assert_locked(tp); 1193bc093719SEd Schouten 1194bc093719SEd Schouten MPASS(tp->t_flags & TF_BYPASS); 1195bc093719SEd Schouten 1196bc093719SEd Schouten atomic_add_long(&tty_nin, len); 1197bc093719SEd Schouten 1198a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_bypass)) 1199a1215e37SEd Schouten return ttyhook_rint_bypass(tp, buf, len); 1200a1215e37SEd Schouten 1201bc093719SEd Schouten ret = ttyinq_write(&tp->t_inq, buf, len, 0); 1202bc093719SEd Schouten ttyinq_canonicalize(&tp->t_inq); 1203d40b91cbSEd Schouten if (ret < len) 1204d40b91cbSEd Schouten tty_hiwat_in_block(tp); 1205bc093719SEd Schouten 1206bc093719SEd Schouten return (ret); 1207bc093719SEd Schouten } 1208bc093719SEd Schouten 1209bc093719SEd Schouten void 1210bc093719SEd Schouten ttydisc_rint_done(struct tty *tp) 1211bc093719SEd Schouten { 1212bc093719SEd Schouten 121323d53268SKyle Evans tty_assert_locked(tp); 1214bc093719SEd Schouten 1215a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_done)) 1216a1215e37SEd Schouten ttyhook_rint_done(tp); 1217a1215e37SEd Schouten 1218bc093719SEd Schouten /* Wake up readers. */ 1219bc093719SEd Schouten tty_wakeup(tp, FREAD); 1220bc093719SEd Schouten /* Wake up driver for echo. */ 1221bc093719SEd Schouten ttydevsw_outwakeup(tp); 1222bc093719SEd Schouten } 1223bc093719SEd Schouten 1224a1215e37SEd Schouten size_t 1225a1215e37SEd Schouten ttydisc_rint_poll(struct tty *tp) 1226a1215e37SEd Schouten { 1227a1215e37SEd Schouten size_t l; 1228a1215e37SEd Schouten 122923d53268SKyle Evans tty_assert_locked(tp); 1230a1215e37SEd Schouten 1231a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_poll)) 1232a1215e37SEd Schouten return ttyhook_rint_poll(tp); 1233a1215e37SEd Schouten 1234a1215e37SEd Schouten /* 1235a1215e37SEd Schouten * XXX: Still allow character input when there's no space in the 1236a1215e37SEd Schouten * buffers, but we haven't entered the high watermark. This is 1237a1215e37SEd Schouten * to allow backspace characters to be inserted when in 1238a1215e37SEd Schouten * canonical mode. 1239a1215e37SEd Schouten */ 1240a1215e37SEd Schouten l = ttyinq_bytesleft(&tp->t_inq); 1241a1215e37SEd Schouten if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0) 1242a1215e37SEd Schouten return (1); 1243a1215e37SEd Schouten 1244a1215e37SEd Schouten return (l); 1245a1215e37SEd Schouten } 1246a1215e37SEd Schouten 1247bc093719SEd Schouten static void 1248bc093719SEd Schouten ttydisc_wakeup_watermark(struct tty *tp) 1249bc093719SEd Schouten { 1250bc093719SEd Schouten size_t c; 1251bc093719SEd Schouten 1252bc093719SEd Schouten c = ttyoutq_bytesleft(&tp->t_outq); 1253bc093719SEd Schouten if (tp->t_flags & TF_HIWAT_OUT) { 1254bc093719SEd Schouten /* Only allow us to run when we're below the watermark. */ 1255bc093719SEd Schouten if (c < tp->t_outlow) 1256bc093719SEd Schouten return; 1257bc093719SEd Schouten 1258bc093719SEd Schouten /* Reset the watermark. */ 1259bc093719SEd Schouten tp->t_flags &= ~TF_HIWAT_OUT; 1260bc093719SEd Schouten } else { 1261bc093719SEd Schouten /* Only run when we have data at all. */ 1262bc093719SEd Schouten if (c == 0) 1263bc093719SEd Schouten return; 1264bc093719SEd Schouten } 1265bc093719SEd Schouten tty_wakeup(tp, FWRITE); 1266bc093719SEd Schouten } 1267bc093719SEd Schouten 1268bc093719SEd Schouten size_t 1269bc093719SEd Schouten ttydisc_getc(struct tty *tp, void *buf, size_t len) 1270bc093719SEd Schouten { 1271bc093719SEd Schouten 127223d53268SKyle Evans tty_assert_locked(tp); 1273bc093719SEd Schouten 1274bc093719SEd Schouten if (tp->t_flags & TF_STOPPED) 1275bc093719SEd Schouten return (0); 1276bc093719SEd Schouten 1277a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_inject)) 1278a1215e37SEd Schouten return ttyhook_getc_inject(tp, buf, len); 1279bc093719SEd Schouten 1280a1215e37SEd Schouten len = ttyoutq_read(&tp->t_outq, buf, len); 1281a1215e37SEd Schouten 1282a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_capture)) 1283a1215e37SEd Schouten ttyhook_getc_capture(tp, buf, len); 1284a1215e37SEd Schouten 1285a1215e37SEd Schouten ttydisc_wakeup_watermark(tp); 1286ffffa83bSEd Schouten atomic_add_long(&tty_nout, len); 1287bc093719SEd Schouten 1288ffffa83bSEd Schouten return (len); 1289bc093719SEd Schouten } 1290bc093719SEd Schouten 1291bc093719SEd Schouten int 1292bc093719SEd Schouten ttydisc_getc_uio(struct tty *tp, struct uio *uio) 1293bc093719SEd Schouten { 1294a1215e37SEd Schouten int error = 0; 1295526d0bd5SKonstantin Belousov ssize_t obytes = uio->uio_resid; 1296a1215e37SEd Schouten size_t len; 1297a1215e37SEd Schouten char buf[TTY_STACKBUF]; 1298bc093719SEd Schouten 129923d53268SKyle Evans tty_assert_locked(tp); 1300bc093719SEd Schouten 1301bc093719SEd Schouten if (tp->t_flags & TF_STOPPED) 1302bc093719SEd Schouten return (0); 1303bc093719SEd Schouten 1304a1215e37SEd Schouten /* 1305a1215e37SEd Schouten * When a TTY hook is attached, we cannot perform unbuffered 1306a1215e37SEd Schouten * copying to userspace. Just call ttydisc_getc() and 1307a1215e37SEd Schouten * temporarily store data in a shadow buffer. 1308a1215e37SEd Schouten */ 1309a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_capture) || 1310a1215e37SEd Schouten ttyhook_hashook(tp, getc_inject)) { 1311a1215e37SEd Schouten while (uio->uio_resid > 0) { 1312a1215e37SEd Schouten /* Read to shadow buffer. */ 1313a1215e37SEd Schouten len = ttydisc_getc(tp, buf, 1314a1215e37SEd Schouten MIN(uio->uio_resid, sizeof buf)); 1315a1215e37SEd Schouten if (len == 0) 1316a1215e37SEd Schouten break; 1317bc093719SEd Schouten 1318a1215e37SEd Schouten /* Copy to userspace. */ 1319a1215e37SEd Schouten tty_unlock(tp); 1320a1215e37SEd Schouten error = uiomove(buf, len, uio); 1321a1215e37SEd Schouten tty_lock(tp); 1322a1215e37SEd Schouten 1323a1215e37SEd Schouten if (error != 0) 1324a1215e37SEd Schouten break; 1325a1215e37SEd Schouten } 1326a1215e37SEd Schouten } else { 1327a1215e37SEd Schouten error = ttyoutq_read_uio(&tp->t_outq, tp, uio); 1328a1215e37SEd Schouten 1329a1215e37SEd Schouten ttydisc_wakeup_watermark(tp); 1330bc093719SEd Schouten atomic_add_long(&tty_nout, obytes - uio->uio_resid); 1331a1215e37SEd Schouten } 1332bc093719SEd Schouten 1333bc093719SEd Schouten return (error); 1334bc093719SEd Schouten } 1335bc093719SEd Schouten 1336a1215e37SEd Schouten size_t 1337a1215e37SEd Schouten ttydisc_getc_poll(struct tty *tp) 1338a1215e37SEd Schouten { 1339a1215e37SEd Schouten 134023d53268SKyle Evans tty_assert_locked(tp); 1341a1215e37SEd Schouten 1342a1215e37SEd Schouten if (tp->t_flags & TF_STOPPED) 1343a1215e37SEd Schouten return (0); 1344a1215e37SEd Schouten 1345a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_poll)) 1346a1215e37SEd Schouten return ttyhook_getc_poll(tp); 1347a1215e37SEd Schouten 1348a1215e37SEd Schouten return ttyoutq_bytesused(&tp->t_outq); 1349a1215e37SEd Schouten } 1350a1215e37SEd Schouten 1351bc093719SEd Schouten /* 1352bc093719SEd Schouten * XXX: not really related to the TTYDISC, but we'd better put 1353bc093719SEd Schouten * tty_putchar() here, because we need to perform proper output 1354bc093719SEd Schouten * processing. 1355bc093719SEd Schouten */ 1356bc093719SEd Schouten 1357bc093719SEd Schouten int 13586858c2ccSConrad Meyer tty_putstrn(struct tty *tp, const char *p, size_t n) 1359bc093719SEd Schouten { 13606858c2ccSConrad Meyer size_t i; 13616858c2ccSConrad Meyer 136223d53268SKyle Evans tty_assert_locked(tp); 1363bc093719SEd Schouten 1364bc093719SEd Schouten if (tty_gone(tp)) 1365bc093719SEd Schouten return (-1); 1366bc093719SEd Schouten 13676858c2ccSConrad Meyer for (i = 0; i < n; i++) 13686858c2ccSConrad Meyer ttydisc_echo_force(tp, p[i], 0); 13696858c2ccSConrad Meyer 1370bc093719SEd Schouten tp->t_writepos = tp->t_column; 1371bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 1372bc093719SEd Schouten 1373bc093719SEd Schouten ttydevsw_outwakeup(tp); 1374bc093719SEd Schouten return (0); 1375bc093719SEd Schouten } 13766858c2ccSConrad Meyer 13776858c2ccSConrad Meyer int 13786858c2ccSConrad Meyer tty_putchar(struct tty *tp, char c) 13796858c2ccSConrad Meyer { 13806858c2ccSConrad Meyer return (tty_putstrn(tp, &c, 1)); 13816858c2ccSConrad Meyer } 1382