1bc093719SEd Schouten /*- 2*8a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*8a36da99SPedro 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 __FBSDID("$FreeBSD$"); 34bc093719SEd Schouten 35bc093719SEd Schouten #include <sys/param.h> 36bc093719SEd Schouten #include <sys/fcntl.h> 37bc093719SEd Schouten #include <sys/filio.h> 38bc093719SEd Schouten #include <sys/kernel.h> 39bc093719SEd Schouten #include <sys/signal.h> 40bc093719SEd Schouten #include <sys/sysctl.h> 41bc093719SEd Schouten #include <sys/systm.h> 42bc093719SEd Schouten #include <sys/tty.h> 43bc093719SEd Schouten #include <sys/ttycom.h> 44bc093719SEd Schouten #include <sys/ttydefaults.h> 45bc093719SEd Schouten #include <sys/uio.h> 46bc093719SEd Schouten #include <sys/vnode.h> 47bc093719SEd Schouten 48bc093719SEd Schouten /* 49bc093719SEd Schouten * Standard TTYDISC `termios' line discipline. 50bc093719SEd Schouten */ 51bc093719SEd Schouten 52bc093719SEd Schouten /* Statistics. */ 531d952ed2SEd Schouten static unsigned long tty_nin = 0; 541d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nin, CTLFLAG_RD, 55bc093719SEd Schouten &tty_nin, 0, "Total amount of bytes received"); 561d952ed2SEd Schouten static unsigned long tty_nout = 0; 571d952ed2SEd Schouten SYSCTL_ULONG(_kern, OID_AUTO, tty_nout, CTLFLAG_RD, 58bc093719SEd Schouten &tty_nout, 0, "Total amount of bytes transmitted"); 59bc093719SEd Schouten 60bc093719SEd Schouten /* termios comparison macro's. */ 61bc093719SEd Schouten #define CMP_CC(v,c) (tp->t_termios.c_cc[v] != _POSIX_VDISABLE && \ 62bc093719SEd Schouten tp->t_termios.c_cc[v] == (c)) 63bc093719SEd Schouten #define CMP_FLAG(field,opt) (tp->t_termios.c_ ## field ## flag & (opt)) 64bc093719SEd Schouten 65bc093719SEd Schouten /* Characters that cannot be modified through c_cc. */ 66bc093719SEd Schouten #define CTAB '\t' 67bc093719SEd Schouten #define CNL '\n' 68bc093719SEd Schouten #define CCR '\r' 69bc093719SEd Schouten 70bc093719SEd Schouten /* Character is a control character. */ 71bc093719SEd Schouten #define CTL_VALID(c) ((c) == 0x7f || (unsigned char)(c) < 0x20) 72bc093719SEd Schouten /* Control character should be processed on echo. */ 73bc093719SEd Schouten #define CTL_ECHO(c,q) (!(q) && ((c) == CERASE2 || (c) == CTAB || \ 74bc093719SEd Schouten (c) == CNL || (c) == CCR)) 75bc093719SEd Schouten /* Control character should be printed using ^X notation. */ 76bc093719SEd Schouten #define CTL_PRINT(c,q) ((c) == 0x7f || ((unsigned char)(c) < 0x20 && \ 77bc093719SEd Schouten ((q) || ((c) != CTAB && (c) != CNL)))) 78bc093719SEd Schouten /* Character is whitespace. */ 79bc093719SEd Schouten #define CTL_WHITE(c) ((c) == ' ' || (c) == CTAB) 80bc093719SEd Schouten /* Character is alphanumeric. */ 81bc093719SEd Schouten #define CTL_ALNUM(c) (((c) >= '0' && (c) <= '9') || \ 82bc093719SEd Schouten ((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) 83bc093719SEd Schouten 84a1215e37SEd Schouten #define TTY_STACKBUF 256 85a1215e37SEd Schouten 86bc093719SEd Schouten void 87bc093719SEd Schouten ttydisc_open(struct tty *tp) 88bc093719SEd Schouten { 89bc093719SEd Schouten ttydisc_optimize(tp); 90bc093719SEd Schouten } 91bc093719SEd Schouten 92bc093719SEd Schouten void 93bc093719SEd Schouten ttydisc_close(struct tty *tp) 94bc093719SEd Schouten { 95bc093719SEd Schouten 96bc093719SEd Schouten /* Clean up our flags when leaving the discipline. */ 97bc093719SEd Schouten tp->t_flags &= ~(TF_STOPPED|TF_HIWAT|TF_ZOMBIE); 98bc093719SEd Schouten 992cd5358aSKonstantin Belousov /* 1002cd5358aSKonstantin Belousov * POSIX states that we must drain output and flush input on 1012cd5358aSKonstantin Belousov * last close. Draining has already been done if possible. 1022cd5358aSKonstantin Belousov */ 1032cd5358aSKonstantin Belousov tty_flush(tp, FREAD | FWRITE); 104a1215e37SEd Schouten 105a1215e37SEd Schouten if (ttyhook_hashook(tp, close)) 106a1215e37SEd Schouten ttyhook_close(tp); 107bc093719SEd Schouten } 108bc093719SEd Schouten 109bc093719SEd Schouten static int 110bc093719SEd Schouten ttydisc_read_canonical(struct tty *tp, struct uio *uio, int ioflag) 111bc093719SEd Schouten { 112bc093719SEd Schouten char breakc[4] = { CNL }; /* enough to hold \n, VEOF and VEOL. */ 113bc093719SEd Schouten int error; 114bc093719SEd Schouten size_t clen, flen = 0, n = 1; 115bc093719SEd Schouten unsigned char lastc = _POSIX_VDISABLE; 116bc093719SEd Schouten 117bc093719SEd Schouten #define BREAK_ADD(c) do { \ 118bc093719SEd Schouten if (tp->t_termios.c_cc[c] != _POSIX_VDISABLE) \ 119bc093719SEd Schouten breakc[n++] = tp->t_termios.c_cc[c]; \ 120bc093719SEd Schouten } while (0) 121bc093719SEd Schouten /* Determine which characters we should trigger on. */ 122bc093719SEd Schouten BREAK_ADD(VEOF); 123bc093719SEd Schouten BREAK_ADD(VEOL); 124bc093719SEd Schouten #undef BREAK_ADD 125bc093719SEd Schouten breakc[n] = '\0'; 126bc093719SEd Schouten 127bc093719SEd Schouten do { 1281da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 1291da7bb41SEd Schouten if (error) 1301da7bb41SEd Schouten return (error); 1311da7bb41SEd Schouten 132bc093719SEd Schouten /* 133bc093719SEd Schouten * Quite a tricky case: unlike the old TTY 134bc093719SEd Schouten * implementation, this implementation copies data back 135bc093719SEd Schouten * to userspace in large chunks. Unfortunately, we can't 136bc093719SEd Schouten * calculate the line length on beforehand if it crosses 137bc093719SEd Schouten * ttyinq_block boundaries, because multiple reads could 138bc093719SEd Schouten * then make this code read beyond the newline. 139bc093719SEd Schouten * 140bc093719SEd Schouten * This is why we limit the read to: 141bc093719SEd Schouten * - The size the user has requested 142bc093719SEd Schouten * - The blocksize (done in tty_inq.c) 143bc093719SEd Schouten * - The amount of bytes until the newline 144bc093719SEd Schouten * 145bc093719SEd Schouten * This causes the line length to be recalculated after 146bc093719SEd Schouten * each block has been copied to userspace. This will 147bc093719SEd Schouten * cause the TTY layer to return data in chunks using 148bc093719SEd Schouten * the blocksize (except the first and last blocks). 149bc093719SEd Schouten */ 150bc093719SEd Schouten clen = ttyinq_findchar(&tp->t_inq, breakc, uio->uio_resid, 151bc093719SEd Schouten &lastc); 152bc093719SEd Schouten 153bc093719SEd Schouten /* No more data. */ 154bc093719SEd Schouten if (clen == 0) { 1556b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 156bc093719SEd Schouten return (0); 1576b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 1586b1b791dSEd Schouten return (EWOULDBLOCK); 159bc093719SEd Schouten 160bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 161bc093719SEd Schouten if (error) 162bc093719SEd Schouten return (error); 163bc093719SEd Schouten continue; 164bc093719SEd Schouten } 165bc093719SEd Schouten 166bc093719SEd Schouten /* Don't send the EOF char back to userspace. */ 167bc093719SEd Schouten if (CMP_CC(VEOF, lastc)) 168bc093719SEd Schouten flen = 1; 169bc093719SEd Schouten 170bc093719SEd Schouten MPASS(flen <= clen); 171bc093719SEd Schouten 172bc093719SEd Schouten /* Read and throw away the EOF character. */ 173bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, clen, flen); 174bc093719SEd Schouten if (error) 175bc093719SEd Schouten return (error); 176bc093719SEd Schouten 177bc093719SEd Schouten } while (uio->uio_resid > 0 && lastc == _POSIX_VDISABLE); 178bc093719SEd Schouten 179bc093719SEd Schouten return (0); 180bc093719SEd Schouten } 181bc093719SEd Schouten 182bc093719SEd Schouten static int 183bc093719SEd Schouten ttydisc_read_raw_no_timer(struct tty *tp, struct uio *uio, int ioflag) 184bc093719SEd Schouten { 185bc093719SEd Schouten size_t vmin = tp->t_termios.c_cc[VMIN]; 186526d0bd5SKonstantin Belousov ssize_t oresid = uio->uio_resid; 187bc093719SEd Schouten int error; 188bc093719SEd Schouten 189bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] == 0); 190bc093719SEd Schouten 191bc093719SEd Schouten /* 192bc093719SEd Schouten * This routine implements the easy cases of read()s while in 193bc093719SEd Schouten * non-canonical mode, namely case B and D, where we don't have 194bc093719SEd Schouten * any timers at all. 195bc093719SEd Schouten */ 196bc093719SEd Schouten 197bc093719SEd Schouten for (;;) { 1981da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 1991da7bb41SEd Schouten if (error) 2001da7bb41SEd Schouten return (error); 2011da7bb41SEd Schouten 202bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 203bc093719SEd Schouten uio->uio_resid, 0); 204bc093719SEd Schouten if (error) 205bc093719SEd Schouten return (error); 206bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 207bc093719SEd Schouten return (0); 208bc093719SEd Schouten 209bc093719SEd Schouten /* We have to wait for more. */ 2106b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 211bc093719SEd Schouten return (0); 2126b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 2136b1b791dSEd Schouten return (EWOULDBLOCK); 214bc093719SEd Schouten 215bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 216bc093719SEd Schouten if (error) 217bc093719SEd Schouten return (error); 218bc093719SEd Schouten } 219bc093719SEd Schouten } 220bc093719SEd Schouten 221bc093719SEd Schouten static int 222bc093719SEd Schouten ttydisc_read_raw_read_timer(struct tty *tp, struct uio *uio, int ioflag, 223bc093719SEd Schouten int oresid) 224bc093719SEd Schouten { 225bc093719SEd Schouten size_t vmin = MAX(tp->t_termios.c_cc[VMIN], 1); 226bc093719SEd Schouten unsigned int vtime = tp->t_termios.c_cc[VTIME]; 227bc093719SEd Schouten struct timeval end, now, left; 228bc093719SEd Schouten int error, hz; 229bc093719SEd Schouten 230bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] != 0); 231bc093719SEd Schouten 232bc093719SEd Schouten /* Determine when the read should be expired. */ 233bc093719SEd Schouten end.tv_sec = vtime / 10; 234bc093719SEd Schouten end.tv_usec = (vtime % 10) * 100000; 235bc093719SEd Schouten getmicrotime(&now); 236bc093719SEd Schouten timevaladd(&end, &now); 237bc093719SEd Schouten 238bc093719SEd Schouten for (;;) { 2391da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 2401da7bb41SEd Schouten if (error) 2411da7bb41SEd Schouten return (error); 2421da7bb41SEd Schouten 243bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 244bc093719SEd Schouten uio->uio_resid, 0); 245bc093719SEd Schouten if (error) 246bc093719SEd Schouten return (error); 247bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 248bc093719SEd Schouten return (0); 249bc093719SEd Schouten 250bc093719SEd Schouten /* Calculate how long we should wait. */ 251bc093719SEd Schouten getmicrotime(&now); 252bc093719SEd Schouten if (timevalcmp(&now, &end, >)) 253bc093719SEd Schouten return (0); 254bc093719SEd Schouten left = end; 255bc093719SEd Schouten timevalsub(&left, &now); 256bc093719SEd Schouten hz = tvtohz(&left); 257bc093719SEd Schouten 258bc093719SEd Schouten /* 259bc093719SEd Schouten * We have to wait for more. If the timer expires, we 260bc093719SEd Schouten * should return a 0-byte read. 261bc093719SEd Schouten */ 2626b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 263bc093719SEd Schouten return (0); 2646b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 2656b1b791dSEd Schouten return (EWOULDBLOCK); 266bc093719SEd Schouten 267bc093719SEd Schouten error = tty_timedwait(tp, &tp->t_inwait, hz); 268bc093719SEd Schouten if (error) 269bc093719SEd Schouten return (error == EWOULDBLOCK ? 0 : error); 270bc093719SEd Schouten } 271bc093719SEd Schouten 272bc093719SEd Schouten return (0); 273bc093719SEd Schouten } 274bc093719SEd Schouten 275bc093719SEd Schouten static int 276bc093719SEd Schouten ttydisc_read_raw_interbyte_timer(struct tty *tp, struct uio *uio, int ioflag) 277bc093719SEd Schouten { 278bc093719SEd Schouten size_t vmin = tp->t_termios.c_cc[VMIN]; 279526d0bd5SKonstantin Belousov ssize_t oresid = uio->uio_resid; 280bc093719SEd Schouten int error; 281bc093719SEd Schouten 282bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VMIN] != 0); 283bc093719SEd Schouten MPASS(tp->t_termios.c_cc[VTIME] != 0); 284bc093719SEd Schouten 285bc093719SEd Schouten /* 286bc093719SEd Schouten * When using the interbyte timer, the timer should be started 287bc093719SEd Schouten * after the first byte has been received. We just call into the 288bc093719SEd Schouten * generic read timer code after we've received the first byte. 289bc093719SEd Schouten */ 290bc093719SEd Schouten 291bc093719SEd Schouten for (;;) { 2921da7bb41SEd Schouten error = tty_wait_background(tp, curthread, SIGTTIN); 2931da7bb41SEd Schouten if (error) 2941da7bb41SEd Schouten return (error); 2951da7bb41SEd Schouten 296bc093719SEd Schouten error = ttyinq_read_uio(&tp->t_inq, tp, uio, 297bc093719SEd Schouten uio->uio_resid, 0); 298bc093719SEd Schouten if (error) 299bc093719SEd Schouten return (error); 300bc093719SEd Schouten if (uio->uio_resid == 0 || (oresid - uio->uio_resid) >= vmin) 301bc093719SEd Schouten return (0); 302bc093719SEd Schouten 303bc093719SEd Schouten /* 304bc093719SEd Schouten * Not enough data, but we did receive some, which means 305bc093719SEd Schouten * we'll now start using the interbyte timer. 306bc093719SEd Schouten */ 307bc093719SEd Schouten if (oresid != uio->uio_resid) 308bc093719SEd Schouten break; 309bc093719SEd Schouten 310bc093719SEd Schouten /* We have to wait for more. */ 3116b1b791dSEd Schouten if (tp->t_flags & TF_ZOMBIE) 312bc093719SEd Schouten return (0); 3136b1b791dSEd Schouten else if (ioflag & IO_NDELAY) 3146b1b791dSEd Schouten return (EWOULDBLOCK); 315bc093719SEd Schouten 316bc093719SEd Schouten error = tty_wait(tp, &tp->t_inwait); 317bc093719SEd Schouten if (error) 318bc093719SEd Schouten return (error); 319bc093719SEd Schouten } 320bc093719SEd Schouten 321bc093719SEd Schouten return ttydisc_read_raw_read_timer(tp, uio, ioflag, oresid); 322bc093719SEd Schouten } 323bc093719SEd Schouten 324bc093719SEd Schouten int 325bc093719SEd Schouten ttydisc_read(struct tty *tp, struct uio *uio, int ioflag) 326bc093719SEd Schouten { 327bc093719SEd Schouten int error; 328bc093719SEd Schouten 329bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 330bc093719SEd Schouten 331bc093719SEd Schouten if (uio->uio_resid == 0) 332bc093719SEd Schouten return (0); 333bc093719SEd Schouten 334bc093719SEd Schouten if (CMP_FLAG(l, ICANON)) 335bc093719SEd Schouten error = ttydisc_read_canonical(tp, uio, ioflag); 336bc093719SEd Schouten else if (tp->t_termios.c_cc[VTIME] == 0) 337bc093719SEd Schouten error = ttydisc_read_raw_no_timer(tp, uio, ioflag); 338bc093719SEd Schouten else if (tp->t_termios.c_cc[VMIN] == 0) 339bc093719SEd Schouten error = ttydisc_read_raw_read_timer(tp, uio, ioflag, 340bc093719SEd Schouten uio->uio_resid); 341bc093719SEd Schouten else 342bc093719SEd Schouten error = ttydisc_read_raw_interbyte_timer(tp, uio, ioflag); 343bc093719SEd Schouten 344a15ec0a5SEd Schouten if (ttyinq_bytesleft(&tp->t_inq) >= tp->t_inlow || 345a15ec0a5SEd Schouten ttyinq_bytescanonicalized(&tp->t_inq) == 0) { 346bc093719SEd Schouten /* Unset the input watermark when we've got enough space. */ 347bc093719SEd Schouten tty_hiwat_in_unblock(tp); 348bc093719SEd Schouten } 349bc093719SEd Schouten 350bc093719SEd Schouten return (error); 351bc093719SEd Schouten } 352bc093719SEd Schouten 353bc093719SEd Schouten static __inline unsigned int 354bc093719SEd Schouten ttydisc_findchar(const char *obstart, unsigned int oblen) 355bc093719SEd Schouten { 356bc093719SEd Schouten const char *c = obstart; 357bc093719SEd Schouten 358bc093719SEd Schouten while (oblen--) { 359bc093719SEd Schouten if (CTL_VALID(*c)) 360bc093719SEd Schouten break; 361bc093719SEd Schouten c++; 362bc093719SEd Schouten } 363bc093719SEd Schouten 364bc093719SEd Schouten return (c - obstart); 365bc093719SEd Schouten } 366bc093719SEd Schouten 367bc093719SEd Schouten static int 368bc093719SEd Schouten ttydisc_write_oproc(struct tty *tp, char c) 369bc093719SEd Schouten { 370bc093719SEd Schouten unsigned int scnt, error; 371bc093719SEd Schouten 372bc093719SEd Schouten MPASS(CMP_FLAG(o, OPOST)); 373bc093719SEd Schouten MPASS(CTL_VALID(c)); 374bc093719SEd Schouten 375bc093719SEd Schouten #define PRINT_NORMAL() ttyoutq_write_nofrag(&tp->t_outq, &c, 1) 376bc093719SEd Schouten switch (c) { 377bc093719SEd Schouten case CEOF: 378bc093719SEd Schouten /* End-of-text dropping. */ 379bc093719SEd Schouten if (CMP_FLAG(o, ONOEOT)) 380bc093719SEd Schouten return (0); 381bc093719SEd Schouten return PRINT_NORMAL(); 382bc093719SEd Schouten 383bc093719SEd Schouten case CERASE2: 384bc093719SEd Schouten /* Handle backspace to fix tab expansion. */ 385bc093719SEd Schouten if (PRINT_NORMAL() != 0) 386bc093719SEd Schouten return (-1); 387bc093719SEd Schouten if (tp->t_column > 0) 388bc093719SEd Schouten tp->t_column--; 389bc093719SEd Schouten return (0); 390bc093719SEd Schouten 391bc093719SEd Schouten case CTAB: 392bc093719SEd Schouten /* Tab expansion. */ 393bc093719SEd Schouten scnt = 8 - (tp->t_column & 7); 394bc093719SEd Schouten if (CMP_FLAG(o, TAB3)) { 395bc093719SEd Schouten error = ttyoutq_write_nofrag(&tp->t_outq, 396bc093719SEd Schouten " ", scnt); 397bc093719SEd Schouten } else { 398bc093719SEd Schouten error = PRINT_NORMAL(); 399bc093719SEd Schouten } 400bc093719SEd Schouten if (error) 401bc093719SEd Schouten return (-1); 402bc093719SEd Schouten 403bc093719SEd Schouten tp->t_column += scnt; 404bc093719SEd Schouten MPASS((tp->t_column % 8) == 0); 405bc093719SEd Schouten return (0); 406bc093719SEd Schouten 407bc093719SEd Schouten case CNL: 408bc093719SEd Schouten /* Newline conversion. */ 409bc093719SEd Schouten if (CMP_FLAG(o, ONLCR)) { 410bc093719SEd Schouten /* Convert \n to \r\n. */ 411bc093719SEd Schouten error = ttyoutq_write_nofrag(&tp->t_outq, "\r\n", 2); 412bc093719SEd Schouten } else { 413bc093719SEd Schouten error = PRINT_NORMAL(); 414bc093719SEd Schouten } 415bc093719SEd Schouten if (error) 416bc093719SEd Schouten return (-1); 417bc093719SEd Schouten 418bc093719SEd Schouten if (CMP_FLAG(o, ONLCR|ONLRET)) { 419bc093719SEd Schouten tp->t_column = tp->t_writepos = 0; 420bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 421bc093719SEd Schouten } 422bc093719SEd Schouten return (0); 423bc093719SEd Schouten 424bc093719SEd Schouten case CCR: 425bc093719SEd Schouten /* Carriage return to newline conversion. */ 426bc093719SEd Schouten if (CMP_FLAG(o, OCRNL)) 427bc093719SEd Schouten c = CNL; 428bc093719SEd Schouten /* Omit carriage returns on column 0. */ 429bc093719SEd Schouten if (CMP_FLAG(o, ONOCR) && tp->t_column == 0) 430bc093719SEd Schouten return (0); 431bc093719SEd Schouten if (PRINT_NORMAL() != 0) 432bc093719SEd Schouten return (-1); 433bc093719SEd Schouten 434bc093719SEd Schouten tp->t_column = tp->t_writepos = 0; 435bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 436bc093719SEd Schouten return (0); 437bc093719SEd Schouten } 438bc093719SEd Schouten 439bc093719SEd Schouten /* 440bc093719SEd Schouten * Invisible control character. Print it, but don't 441bc093719SEd Schouten * increase the column count. 442bc093719SEd Schouten */ 443bc093719SEd Schouten return PRINT_NORMAL(); 444bc093719SEd Schouten #undef PRINT_NORMAL 445bc093719SEd Schouten } 446bc093719SEd Schouten 447bc093719SEd Schouten /* 448bc093719SEd Schouten * Just like the old TTY implementation, we need to copy data in chunks 449bc093719SEd Schouten * into a temporary buffer. One of the reasons why we need to do this, 450bc093719SEd Schouten * is because output processing (only TAB3 though) may allow the buffer 451bc093719SEd Schouten * to grow eight times. 452bc093719SEd Schouten */ 453bc093719SEd Schouten int 454bc093719SEd Schouten ttydisc_write(struct tty *tp, struct uio *uio, int ioflag) 455bc093719SEd Schouten { 456a1215e37SEd Schouten char ob[TTY_STACKBUF]; 457bc093719SEd Schouten char *obstart; 458bc093719SEd Schouten int error = 0; 459bc093719SEd Schouten unsigned int oblen = 0; 460bc093719SEd Schouten 461bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 462bc093719SEd Schouten 463bc093719SEd Schouten if (tp->t_flags & TF_ZOMBIE) 464bc093719SEd Schouten return (EIO); 465bc093719SEd Schouten 466bc093719SEd Schouten /* 467bc093719SEd Schouten * We don't need to check whether the process is the foreground 468bc093719SEd Schouten * process group or if we have a carrier. This is already done 469bc093719SEd Schouten * in ttydev_write(). 470bc093719SEd Schouten */ 471bc093719SEd Schouten 472bc093719SEd Schouten while (uio->uio_resid > 0) { 473bc093719SEd Schouten unsigned int nlen; 474bc093719SEd Schouten 475bc093719SEd Schouten MPASS(oblen == 0); 476bc093719SEd Schouten 477bc093719SEd Schouten /* Step 1: read data. */ 478bc093719SEd Schouten obstart = ob; 479bc093719SEd Schouten nlen = MIN(uio->uio_resid, sizeof ob); 48087fe0fa8SEd Schouten tty_unlock(tp); 481bc093719SEd Schouten error = uiomove(ob, nlen, uio); 48287fe0fa8SEd Schouten tty_lock(tp); 483bc093719SEd Schouten if (error != 0) 484bc093719SEd Schouten break; 485bc093719SEd Schouten oblen = nlen; 486bc093719SEd Schouten 487bc093719SEd Schouten if (tty_gone(tp)) { 488bc093719SEd Schouten error = ENXIO; 489bc093719SEd Schouten break; 490bc093719SEd Schouten } 491bc093719SEd Schouten 492bc093719SEd Schouten MPASS(oblen > 0); 493bc093719SEd Schouten 494bc093719SEd Schouten /* Step 2: process data. */ 495bc093719SEd Schouten do { 496bc093719SEd Schouten unsigned int plen, wlen; 497bc093719SEd Schouten 498bc093719SEd Schouten /* Search for special characters for post processing. */ 499bc093719SEd Schouten if (CMP_FLAG(o, OPOST)) { 500bc093719SEd Schouten plen = ttydisc_findchar(obstart, oblen); 501bc093719SEd Schouten } else { 502bc093719SEd Schouten plen = oblen; 503bc093719SEd Schouten } 504bc093719SEd Schouten 505bc093719SEd Schouten if (plen == 0) { 506bc093719SEd Schouten /* 507bc093719SEd Schouten * We're going to process a character 508bc093719SEd Schouten * that needs processing 509bc093719SEd Schouten */ 510bc093719SEd Schouten if (ttydisc_write_oproc(tp, *obstart) == 0) { 511bc093719SEd Schouten obstart++; 512bc093719SEd Schouten oblen--; 513bc093719SEd Schouten 514bc093719SEd Schouten tp->t_writepos = tp->t_column; 515bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 516bc093719SEd Schouten continue; 517bc093719SEd Schouten } 518bc093719SEd Schouten } else { 519bc093719SEd Schouten /* We're going to write regular data. */ 520bc093719SEd Schouten wlen = ttyoutq_write(&tp->t_outq, obstart, plen); 521bc093719SEd Schouten obstart += wlen; 522bc093719SEd Schouten oblen -= wlen; 523bc093719SEd Schouten tp->t_column += wlen; 524bc093719SEd Schouten 525bc093719SEd Schouten tp->t_writepos = tp->t_column; 526bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 527bc093719SEd Schouten 528bc093719SEd Schouten if (wlen == plen) 529bc093719SEd Schouten continue; 530bc093719SEd Schouten } 531bc093719SEd Schouten 532bc093719SEd Schouten /* Watermark reached. Try to sleep. */ 533bc093719SEd Schouten tp->t_flags |= TF_HIWAT_OUT; 534bc093719SEd Schouten 535bc093719SEd Schouten if (ioflag & IO_NDELAY) { 536bc093719SEd Schouten error = EWOULDBLOCK; 537bc093719SEd Schouten goto done; 538bc093719SEd Schouten } 539bc093719SEd Schouten 540bc093719SEd Schouten /* 541bc093719SEd Schouten * The driver may write back the data 542bc093719SEd Schouten * synchronously. Be sure to check the high 543bc093719SEd Schouten * water mark before going to sleep. 544bc093719SEd Schouten */ 545bc093719SEd Schouten ttydevsw_outwakeup(tp); 546bc093719SEd Schouten if ((tp->t_flags & TF_HIWAT_OUT) == 0) 547bc093719SEd Schouten continue; 548bc093719SEd Schouten 549bc093719SEd Schouten error = tty_wait(tp, &tp->t_outwait); 550bc093719SEd Schouten if (error) 551bc093719SEd Schouten goto done; 552bc093719SEd Schouten 553bc093719SEd Schouten if (tp->t_flags & TF_ZOMBIE) { 554bc093719SEd Schouten error = EIO; 555bc093719SEd Schouten goto done; 556bc093719SEd Schouten } 557bc093719SEd Schouten } while (oblen > 0); 558bc093719SEd Schouten } 559bc093719SEd Schouten 560bc093719SEd Schouten done: 561856ebf85SChristian S.J. Peron if (!tty_gone(tp)) 562bc093719SEd Schouten ttydevsw_outwakeup(tp); 563bc093719SEd Schouten 564bc093719SEd Schouten /* 565bc093719SEd Schouten * Add the amount of bytes that we didn't process back to the 566bc093719SEd Schouten * uio counters. We need to do this to make sure write() doesn't 567bc093719SEd Schouten * count the bytes we didn't store in the queue. 568bc093719SEd Schouten */ 569bc093719SEd Schouten uio->uio_resid += oblen; 570bc093719SEd Schouten return (error); 571bc093719SEd Schouten } 572bc093719SEd Schouten 573bc093719SEd Schouten void 574bc093719SEd Schouten ttydisc_optimize(struct tty *tp) 575bc093719SEd Schouten { 576bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 577bc093719SEd Schouten 57814358b0fSEd Schouten if (ttyhook_hashook(tp, rint_bypass)) { 57914358b0fSEd Schouten tp->t_flags |= TF_BYPASS; 58014358b0fSEd Schouten } else if (ttyhook_hashook(tp, rint)) { 58114358b0fSEd Schouten tp->t_flags &= ~TF_BYPASS; 58214358b0fSEd Schouten } else if (!CMP_FLAG(i, ICRNL|IGNCR|IMAXBEL|INLCR|ISTRIP|IXON) && 583bc093719SEd Schouten (!CMP_FLAG(i, BRKINT) || CMP_FLAG(i, IGNBRK)) && 584bc093719SEd Schouten (!CMP_FLAG(i, PARMRK) || 585bc093719SEd Schouten CMP_FLAG(i, IGNPAR|IGNBRK) == (IGNPAR|IGNBRK)) && 58614358b0fSEd Schouten !CMP_FLAG(l, ECHO|ICANON|IEXTEN|ISIG|PENDIN)) { 587bc093719SEd Schouten tp->t_flags |= TF_BYPASS; 588bc093719SEd Schouten } else { 589bc093719SEd Schouten tp->t_flags &= ~TF_BYPASS; 590bc093719SEd Schouten } 591bc093719SEd Schouten } 592bc093719SEd Schouten 593bc093719SEd Schouten void 594bc093719SEd Schouten ttydisc_modem(struct tty *tp, int open) 595bc093719SEd Schouten { 596bc093719SEd Schouten 597bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 598bc093719SEd Schouten 599bc093719SEd Schouten if (open) 600bc093719SEd Schouten cv_broadcast(&tp->t_dcdwait); 601bc093719SEd Schouten 602bc093719SEd Schouten /* 603bc093719SEd Schouten * Ignore modem status lines when CLOCAL is turned on, but don't 604bc093719SEd Schouten * enter the zombie state when the TTY isn't opened, because 605bc093719SEd Schouten * that would cause the TTY to be in zombie state after being 606bc093719SEd Schouten * opened. 607bc093719SEd Schouten */ 608bc093719SEd Schouten if (!tty_opened(tp) || CMP_FLAG(c, CLOCAL)) 609bc093719SEd Schouten return; 610bc093719SEd Schouten 611bc093719SEd Schouten if (open == 0) { 612bc093719SEd Schouten /* 613bc093719SEd Schouten * Lost carrier. 614bc093719SEd Schouten */ 615bc093719SEd Schouten tp->t_flags |= TF_ZOMBIE; 616bc093719SEd Schouten 617bc093719SEd Schouten tty_signal_sessleader(tp, SIGHUP); 618bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 619bc093719SEd Schouten } else { 620bc093719SEd Schouten /* 621bc093719SEd Schouten * Carrier is back again. 622bc093719SEd Schouten */ 623bc093719SEd Schouten 624bc093719SEd Schouten /* XXX: what should we do here? */ 625bc093719SEd Schouten } 626bc093719SEd Schouten } 627bc093719SEd Schouten 628bc093719SEd Schouten static int 629bc093719SEd Schouten ttydisc_echo_force(struct tty *tp, char c, int quote) 630bc093719SEd Schouten { 631bc093719SEd Schouten 632bc093719SEd Schouten if (CMP_FLAG(o, OPOST) && CTL_ECHO(c, quote)) { 633bc093719SEd Schouten /* 634bc093719SEd Schouten * Only perform postprocessing when OPOST is turned on 635bc093719SEd Schouten * and the character is an unquoted BS/TB/NL/CR. 636bc093719SEd Schouten */ 637bc093719SEd Schouten return ttydisc_write_oproc(tp, c); 638bc093719SEd Schouten } else if (CMP_FLAG(l, ECHOCTL) && CTL_PRINT(c, quote)) { 639bc093719SEd Schouten /* 640bc093719SEd Schouten * Only use ^X notation when ECHOCTL is turned on and 641bc093719SEd Schouten * we've got an quoted control character. 64239410373SEd Schouten * 64339410373SEd Schouten * Print backspaces when echoing an end-of-file. 644bc093719SEd Schouten */ 64539410373SEd Schouten char ob[4] = "^?\b\b"; 646bc093719SEd Schouten 647bc093719SEd Schouten /* Print ^X notation. */ 648bc093719SEd Schouten if (c != 0x7f) 649bc093719SEd Schouten ob[1] = c + 'A' - 1; 650bc093719SEd Schouten 65139410373SEd Schouten if (!quote && CMP_CC(VEOF, c)) { 65239410373SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, ob, 4); 65339410373SEd Schouten } else { 654bc093719SEd Schouten tp->t_column += 2; 655bc093719SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, ob, 2); 65639410373SEd Schouten } 657bc093719SEd Schouten } else { 658bc093719SEd Schouten /* Can just be printed. */ 659bc093719SEd Schouten tp->t_column++; 660bc093719SEd Schouten return ttyoutq_write_nofrag(&tp->t_outq, &c, 1); 661bc093719SEd Schouten } 662bc093719SEd Schouten } 663bc093719SEd Schouten 664bc093719SEd Schouten static int 665bc093719SEd Schouten ttydisc_echo(struct tty *tp, char c, int quote) 666bc093719SEd Schouten { 667bc093719SEd Schouten 668bc093719SEd Schouten /* 669bc093719SEd Schouten * Only echo characters when ECHO is turned on, or ECHONL when 670bc093719SEd Schouten * the character is an unquoted newline. 671bc093719SEd Schouten */ 672bc093719SEd Schouten if (!CMP_FLAG(l, ECHO) && 673bc093719SEd Schouten (!CMP_FLAG(l, ECHONL) || c != CNL || quote)) 674bc093719SEd Schouten return (0); 675bc093719SEd Schouten 676bc093719SEd Schouten return ttydisc_echo_force(tp, c, quote); 677bc093719SEd Schouten } 678bc093719SEd Schouten 679bc093719SEd Schouten static void 680bc093719SEd Schouten ttydisc_reprint_char(void *d, char c, int quote) 681bc093719SEd Schouten { 682bc093719SEd Schouten struct tty *tp = d; 683bc093719SEd Schouten 684bc093719SEd Schouten ttydisc_echo(tp, c, quote); 685bc093719SEd Schouten } 686bc093719SEd Schouten 687bc093719SEd Schouten static void 688bc093719SEd Schouten ttydisc_reprint(struct tty *tp) 689bc093719SEd Schouten { 690bc093719SEd Schouten cc_t c; 691bc093719SEd Schouten 692bc093719SEd Schouten /* Print ^R\n, followed by the line. */ 693bc093719SEd Schouten c = tp->t_termios.c_cc[VREPRINT]; 694bc093719SEd Schouten if (c != _POSIX_VDISABLE) 695bc093719SEd Schouten ttydisc_echo(tp, c, 0); 696bc093719SEd Schouten ttydisc_echo(tp, CNL, 0); 697bc093719SEd Schouten ttyinq_reprintpos_reset(&tp->t_inq); 698bc093719SEd Schouten 699bc093719SEd Schouten ttyinq_line_iterate_from_linestart(&tp->t_inq, ttydisc_reprint_char, tp); 700bc093719SEd Schouten } 701bc093719SEd Schouten 702bc093719SEd Schouten struct ttydisc_recalc_length { 703bc093719SEd Schouten struct tty *tp; 704bc093719SEd Schouten unsigned int curlen; 705bc093719SEd Schouten }; 706bc093719SEd Schouten 707bc093719SEd Schouten static void 708bc093719SEd Schouten ttydisc_recalc_charlength(void *d, char c, int quote) 709bc093719SEd Schouten { 710bc093719SEd Schouten struct ttydisc_recalc_length *data = d; 711bc093719SEd Schouten struct tty *tp = data->tp; 712bc093719SEd Schouten 713bc093719SEd Schouten if (CTL_PRINT(c, quote)) { 714bc093719SEd Schouten if (CMP_FLAG(l, ECHOCTL)) 715bc093719SEd Schouten data->curlen += 2; 716bc093719SEd Schouten } else if (c == CTAB) { 717bc093719SEd Schouten data->curlen += 8 - (data->curlen & 7); 718bc093719SEd Schouten } else { 719bc093719SEd Schouten data->curlen++; 720bc093719SEd Schouten } 721bc093719SEd Schouten } 722bc093719SEd Schouten 723bc093719SEd Schouten static unsigned int 724bc093719SEd Schouten ttydisc_recalc_linelength(struct tty *tp) 725bc093719SEd Schouten { 726bc093719SEd Schouten struct ttydisc_recalc_length data = { tp, tp->t_writepos }; 727bc093719SEd Schouten 728bc093719SEd Schouten ttyinq_line_iterate_from_reprintpos(&tp->t_inq, 729bc093719SEd Schouten ttydisc_recalc_charlength, &data); 730bc093719SEd Schouten return (data.curlen); 731bc093719SEd Schouten } 732bc093719SEd Schouten 733bc093719SEd Schouten static int 734bc093719SEd Schouten ttydisc_rubchar(struct tty *tp) 735bc093719SEd Schouten { 736bc093719SEd Schouten char c; 737bc093719SEd Schouten int quote; 738bc093719SEd Schouten unsigned int prevpos, tablen; 739bc093719SEd Schouten 740bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 741bc093719SEd Schouten return (-1); 742bc093719SEd Schouten ttyinq_unputchar(&tp->t_inq); 743bc093719SEd Schouten 744bc093719SEd Schouten if (CMP_FLAG(l, ECHO)) { 745bc093719SEd Schouten /* 746bc093719SEd Schouten * Remove the character from the screen. This is even 747bc093719SEd Schouten * safe for characters that span multiple characters 748bc093719SEd Schouten * (tabs, quoted, etc). 749bc093719SEd Schouten */ 750bc093719SEd Schouten if (tp->t_writepos >= tp->t_column) { 751bc093719SEd Schouten /* Retype the sentence. */ 752bc093719SEd Schouten ttydisc_reprint(tp); 753bc093719SEd Schouten } else if (CMP_FLAG(l, ECHOE)) { 754bc093719SEd Schouten if (CTL_PRINT(c, quote)) { 755bc093719SEd Schouten /* Remove ^X formatted chars. */ 756bc093719SEd Schouten if (CMP_FLAG(l, ECHOCTL)) { 757bc093719SEd Schouten tp->t_column -= 2; 758bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, 759bc093719SEd Schouten "\b\b \b\b", 6); 760bc093719SEd Schouten } 761bc093719SEd Schouten } else if (c == ' ') { 762bc093719SEd Schouten /* Space character needs no rubbing. */ 763bc093719SEd Schouten tp->t_column -= 1; 764bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\b", 1); 765bc093719SEd Schouten } else if (c == CTAB) { 766bc093719SEd Schouten /* 767bc093719SEd Schouten * Making backspace work with tabs is 768bc093719SEd Schouten * quite hard. Recalculate the length of 769bc093719SEd Schouten * this character and remove it. 770bc093719SEd Schouten * 771bc093719SEd Schouten * Because terminal settings could be 772bc093719SEd Schouten * changed while the line is being 773bc093719SEd Schouten * inserted, the calculations don't have 774bc093719SEd Schouten * to be correct. Make sure we keep the 775bc093719SEd Schouten * tab length within proper bounds. 776bc093719SEd Schouten */ 777bc093719SEd Schouten prevpos = ttydisc_recalc_linelength(tp); 778bc093719SEd Schouten if (prevpos >= tp->t_column) 779bc093719SEd Schouten tablen = 1; 780bc093719SEd Schouten else 781bc093719SEd Schouten tablen = tp->t_column - prevpos; 782bc093719SEd Schouten if (tablen > 8) 783bc093719SEd Schouten tablen = 8; 784bc093719SEd Schouten 785bc093719SEd Schouten tp->t_column = prevpos; 786bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, 787bc093719SEd Schouten "\b\b\b\b\b\b\b\b", tablen); 788bc093719SEd Schouten return (0); 789bc093719SEd Schouten } else { 790bc093719SEd Schouten /* 791bc093719SEd Schouten * Remove a regular character by 792bc093719SEd Schouten * punching a space over it. 793bc093719SEd Schouten */ 794bc093719SEd Schouten tp->t_column -= 1; 795bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\b \b", 3); 796bc093719SEd Schouten } 797bc093719SEd Schouten } else { 798bc093719SEd Schouten /* Don't print spaces. */ 799bc093719SEd Schouten ttydisc_echo(tp, tp->t_termios.c_cc[VERASE], 0); 800bc093719SEd Schouten } 801bc093719SEd Schouten } 802bc093719SEd Schouten 803bc093719SEd Schouten return (0); 804bc093719SEd Schouten } 805bc093719SEd Schouten 806bc093719SEd Schouten static void 807bc093719SEd Schouten ttydisc_rubword(struct tty *tp) 808bc093719SEd Schouten { 809bc093719SEd Schouten char c; 810bc093719SEd Schouten int quote, alnum; 811bc093719SEd Schouten 812bc093719SEd Schouten /* Strip whitespace first. */ 813bc093719SEd Schouten for (;;) { 814bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 815bc093719SEd Schouten return; 816bc093719SEd Schouten if (!CTL_WHITE(c)) 817bc093719SEd Schouten break; 818bc093719SEd Schouten ttydisc_rubchar(tp); 819bc093719SEd Schouten } 820bc093719SEd Schouten 821bc093719SEd Schouten /* 822bc093719SEd Schouten * Record whether the last character from the previous iteration 823bc093719SEd Schouten * was alphanumeric or not. We need this to implement ALTWERASE. 824bc093719SEd Schouten */ 825bc093719SEd Schouten alnum = CTL_ALNUM(c); 826bc093719SEd Schouten for (;;) { 827bc093719SEd Schouten ttydisc_rubchar(tp); 828bc093719SEd Schouten 829bc093719SEd Schouten if (ttyinq_peekchar(&tp->t_inq, &c, "e) != 0) 830bc093719SEd Schouten return; 831bc093719SEd Schouten if (CTL_WHITE(c)) 832bc093719SEd Schouten return; 833bc093719SEd Schouten if (CMP_FLAG(l, ALTWERASE) && CTL_ALNUM(c) != alnum) 834bc093719SEd Schouten return; 835bc093719SEd Schouten } 836bc093719SEd Schouten } 837bc093719SEd Schouten 838bc093719SEd Schouten int 839bc093719SEd Schouten ttydisc_rint(struct tty *tp, char c, int flags) 840bc093719SEd Schouten { 841bc093719SEd Schouten int signal, quote = 0; 842bc093719SEd Schouten char ob[3] = { 0xff, 0x00 }; 843bc093719SEd Schouten size_t ol; 844bc093719SEd Schouten 845bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 846bc093719SEd Schouten 847bc093719SEd Schouten atomic_add_long(&tty_nin, 1); 848bc093719SEd Schouten 849a1215e37SEd Schouten if (ttyhook_hashook(tp, rint)) 850a1215e37SEd Schouten return ttyhook_rint(tp, c, flags); 851a1215e37SEd Schouten 852bc093719SEd Schouten if (tp->t_flags & TF_BYPASS) 853bc093719SEd Schouten goto processed; 854bc093719SEd Schouten 855bc093719SEd Schouten if (flags) { 856bc093719SEd Schouten if (flags & TRE_BREAK) { 857bc093719SEd Schouten if (CMP_FLAG(i, IGNBRK)) { 858bc093719SEd Schouten /* Ignore break characters. */ 859bc093719SEd Schouten return (0); 860bc093719SEd Schouten } else if (CMP_FLAG(i, BRKINT)) { 861bc093719SEd Schouten /* Generate SIGINT on break. */ 862bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 863bc093719SEd Schouten tty_signal_pgrp(tp, SIGINT); 864bc093719SEd Schouten return (0); 865bc093719SEd Schouten } else { 866bc093719SEd Schouten /* Just print it. */ 867bc093719SEd Schouten goto parmrk; 868bc093719SEd Schouten } 869bc093719SEd Schouten } else if (flags & TRE_FRAMING || 870bc093719SEd Schouten (flags & TRE_PARITY && CMP_FLAG(i, INPCK))) { 871bc093719SEd Schouten if (CMP_FLAG(i, IGNPAR)) { 872bc093719SEd Schouten /* Ignore bad characters. */ 873bc093719SEd Schouten return (0); 874bc093719SEd Schouten } else { 875bc093719SEd Schouten /* Just print it. */ 876bc093719SEd Schouten goto parmrk; 877bc093719SEd Schouten } 878bc093719SEd Schouten } 879bc093719SEd Schouten } 880bc093719SEd Schouten 881bc093719SEd Schouten /* Allow any character to perform a wakeup. */ 882bc093719SEd Schouten if (CMP_FLAG(i, IXANY)) 883bc093719SEd Schouten tp->t_flags &= ~TF_STOPPED; 884bc093719SEd Schouten 885bc093719SEd Schouten /* Remove the top bit. */ 886bc093719SEd Schouten if (CMP_FLAG(i, ISTRIP)) 887bc093719SEd Schouten c &= ~0x80; 888bc093719SEd Schouten 889bc093719SEd Schouten /* Skip input processing when we want to print it literally. */ 890bc093719SEd Schouten if (tp->t_flags & TF_LITERAL) { 891bc093719SEd Schouten tp->t_flags &= ~TF_LITERAL; 892bc093719SEd Schouten quote = 1; 893bc093719SEd Schouten goto processed; 894bc093719SEd Schouten } 895bc093719SEd Schouten 896bc093719SEd Schouten /* Special control characters that are implementation dependent. */ 897bc093719SEd Schouten if (CMP_FLAG(l, IEXTEN)) { 898bc093719SEd Schouten /* Accept the next character as literal. */ 899bc093719SEd Schouten if (CMP_CC(VLNEXT, c)) { 900bc093719SEd Schouten if (CMP_FLAG(l, ECHO)) { 901bc093719SEd Schouten if (CMP_FLAG(l, ECHOE)) 902bc093719SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "^\b", 2); 903bc093719SEd Schouten else 904bc093719SEd Schouten ttydisc_echo(tp, c, 0); 905bc093719SEd Schouten } 906bc093719SEd Schouten tp->t_flags |= TF_LITERAL; 907bc093719SEd Schouten return (0); 908bc093719SEd Schouten } 909bc093719SEd Schouten } 910bc093719SEd Schouten 911bc093719SEd Schouten /* 912bc093719SEd Schouten * Handle signal processing. 913bc093719SEd Schouten */ 914bc093719SEd Schouten if (CMP_FLAG(l, ISIG)) { 915bc093719SEd Schouten if (CMP_FLAG(l, ICANON|IEXTEN) == (ICANON|IEXTEN)) { 916bc093719SEd Schouten if (CMP_CC(VSTATUS, c)) { 917bc093719SEd Schouten tty_signal_pgrp(tp, SIGINFO); 918bc093719SEd Schouten return (0); 919bc093719SEd Schouten } 920bc093719SEd Schouten } 921bc093719SEd Schouten 922bc093719SEd Schouten /* 923bc093719SEd Schouten * When compared to the old implementation, this 924bc093719SEd Schouten * implementation also flushes the output queue. POSIX 925bc093719SEd Schouten * is really brief about this, but does makes us assume 926bc093719SEd Schouten * we have to do so. 927bc093719SEd Schouten */ 928bc093719SEd Schouten signal = 0; 929bc093719SEd Schouten if (CMP_CC(VINTR, c)) { 930bc093719SEd Schouten signal = SIGINT; 931bc093719SEd Schouten } else if (CMP_CC(VQUIT, c)) { 932bc093719SEd Schouten signal = SIGQUIT; 933bc093719SEd Schouten } else if (CMP_CC(VSUSP, c)) { 934bc093719SEd Schouten signal = SIGTSTP; 935bc093719SEd Schouten } 936bc093719SEd Schouten 937bc093719SEd Schouten if (signal != 0) { 938bc093719SEd Schouten /* 939bc093719SEd Schouten * Echo the character before signalling the 940bc093719SEd Schouten * processes. 941bc093719SEd Schouten */ 942bc093719SEd Schouten if (!CMP_FLAG(l, NOFLSH)) 943bc093719SEd Schouten tty_flush(tp, FREAD|FWRITE); 944bc093719SEd Schouten ttydisc_echo(tp, c, 0); 945bc093719SEd Schouten tty_signal_pgrp(tp, signal); 946bc093719SEd Schouten return (0); 947bc093719SEd Schouten } 948bc093719SEd Schouten } 949bc093719SEd Schouten 950bc093719SEd Schouten /* 951bc093719SEd Schouten * Handle start/stop characters. 952bc093719SEd Schouten */ 953bc093719SEd Schouten if (CMP_FLAG(i, IXON)) { 954bc093719SEd Schouten if (CMP_CC(VSTOP, c)) { 955bc093719SEd Schouten /* Stop it if we aren't stopped yet. */ 956bc093719SEd Schouten if ((tp->t_flags & TF_STOPPED) == 0) { 957bc093719SEd Schouten tp->t_flags |= TF_STOPPED; 958bc093719SEd Schouten return (0); 959bc093719SEd Schouten } 960bc093719SEd Schouten /* 961bc093719SEd Schouten * Fallthrough: 962bc093719SEd Schouten * When VSTART == VSTOP, we should make this key 963bc093719SEd Schouten * toggle it. 964bc093719SEd Schouten */ 965bc093719SEd Schouten if (!CMP_CC(VSTART, c)) 966bc093719SEd Schouten return (0); 967bc093719SEd Schouten } 968bc093719SEd Schouten if (CMP_CC(VSTART, c)) { 969bc093719SEd Schouten tp->t_flags &= ~TF_STOPPED; 970bc093719SEd Schouten return (0); 971bc093719SEd Schouten } 972bc093719SEd Schouten } 973bc093719SEd Schouten 974bc093719SEd Schouten /* Conversion of CR and NL. */ 975bc093719SEd Schouten switch (c) { 976bc093719SEd Schouten case CCR: 977bc093719SEd Schouten if (CMP_FLAG(i, IGNCR)) 978bc093719SEd Schouten return (0); 979bc093719SEd Schouten if (CMP_FLAG(i, ICRNL)) 980bc093719SEd Schouten c = CNL; 981bc093719SEd Schouten break; 982bc093719SEd Schouten case CNL: 983bc093719SEd Schouten if (CMP_FLAG(i, INLCR)) 984bc093719SEd Schouten c = CCR; 985bc093719SEd Schouten break; 986bc093719SEd Schouten } 987bc093719SEd Schouten 988bc093719SEd Schouten /* Canonical line editing. */ 989bc093719SEd Schouten if (CMP_FLAG(l, ICANON)) { 990bc093719SEd Schouten if (CMP_CC(VERASE, c) || CMP_CC(VERASE2, c)) { 991bc093719SEd Schouten ttydisc_rubchar(tp); 992bc093719SEd Schouten return (0); 993bc093719SEd Schouten } else if (CMP_CC(VKILL, c)) { 994bc093719SEd Schouten while (ttydisc_rubchar(tp) == 0); 995bc093719SEd Schouten return (0); 996bc093719SEd Schouten } else if (CMP_FLAG(l, IEXTEN)) { 997bc093719SEd Schouten if (CMP_CC(VWERASE, c)) { 998bc093719SEd Schouten ttydisc_rubword(tp); 999bc093719SEd Schouten return (0); 1000bc093719SEd Schouten } else if (CMP_CC(VREPRINT, c)) { 1001bc093719SEd Schouten ttydisc_reprint(tp); 1002bc093719SEd Schouten return (0); 1003bc093719SEd Schouten } 1004bc093719SEd Schouten } 1005bc093719SEd Schouten } 1006bc093719SEd Schouten 1007bc093719SEd Schouten processed: 1008bc093719SEd Schouten if (CMP_FLAG(i, PARMRK) && (unsigned char)c == 0xff) { 1009bc093719SEd Schouten /* Print 0xff 0xff. */ 1010bc093719SEd Schouten ob[1] = 0xff; 1011bc093719SEd Schouten ol = 2; 1012bc093719SEd Schouten quote = 1; 1013bc093719SEd Schouten } else { 1014bc093719SEd Schouten ob[0] = c; 1015bc093719SEd Schouten ol = 1; 1016bc093719SEd Schouten } 1017bc093719SEd Schouten 1018bc093719SEd Schouten goto print; 1019bc093719SEd Schouten 1020bc093719SEd Schouten parmrk: 1021bc093719SEd Schouten if (CMP_FLAG(i, PARMRK)) { 1022bc093719SEd Schouten /* Prepend 0xff 0x00 0x.. */ 1023bc093719SEd Schouten ob[2] = c; 1024bc093719SEd Schouten ol = 3; 1025bc093719SEd Schouten quote = 1; 1026bc093719SEd Schouten } else { 1027bc093719SEd Schouten ob[0] = c; 1028bc093719SEd Schouten ol = 1; 1029bc093719SEd Schouten } 1030bc093719SEd Schouten 1031bc093719SEd Schouten print: 1032bc093719SEd Schouten /* See if we can store this on the input queue. */ 1033bc093719SEd Schouten if (ttyinq_write_nofrag(&tp->t_inq, ob, ol, quote) != 0) { 1034a15ec0a5SEd Schouten if (CMP_FLAG(i, IMAXBEL)) 1035a15ec0a5SEd Schouten ttyoutq_write_nofrag(&tp->t_outq, "\a", 1); 1036a15ec0a5SEd Schouten 1037a15ec0a5SEd Schouten /* 1038a15ec0a5SEd Schouten * Prevent a deadlock here. It may be possible that a 1039a15ec0a5SEd Schouten * user has entered so much data, there is no data 1040a15ec0a5SEd Schouten * available to read(), but the buffers are full anyway. 1041a15ec0a5SEd Schouten * 1042a15ec0a5SEd Schouten * Only enter the high watermark if the device driver 1043a15ec0a5SEd Schouten * can actually transmit something. 1044a15ec0a5SEd Schouten */ 1045a15ec0a5SEd Schouten if (ttyinq_bytescanonicalized(&tp->t_inq) == 0) 1046a15ec0a5SEd Schouten return (0); 1047a15ec0a5SEd Schouten 1048bc093719SEd Schouten tty_hiwat_in_block(tp); 1049bc093719SEd Schouten return (-1); 1050bc093719SEd Schouten } 1051bc093719SEd Schouten 1052bc093719SEd Schouten /* 1053bc093719SEd Schouten * In raw mode, we canonicalize after receiving a single 1054bc093719SEd Schouten * character. Otherwise, we canonicalize when we receive a 1055bc093719SEd Schouten * newline, VEOL or VEOF, but only when it isn't quoted. 1056bc093719SEd Schouten */ 1057bc093719SEd Schouten if (!CMP_FLAG(l, ICANON) || 1058bc093719SEd Schouten (!quote && (c == CNL || CMP_CC(VEOL, c) || CMP_CC(VEOF, c)))) { 1059bc093719SEd Schouten ttyinq_canonicalize(&tp->t_inq); 1060bc093719SEd Schouten } 1061bc093719SEd Schouten 1062bc093719SEd Schouten ttydisc_echo(tp, c, quote); 1063bc093719SEd Schouten 1064bc093719SEd Schouten return (0); 1065bc093719SEd Schouten } 1066bc093719SEd Schouten 1067bc093719SEd Schouten size_t 10685c67885aSEd Schouten ttydisc_rint_simple(struct tty *tp, const void *buf, size_t len) 10695c67885aSEd Schouten { 10705c67885aSEd Schouten const char *cbuf; 10715c67885aSEd Schouten 10725c67885aSEd Schouten if (ttydisc_can_bypass(tp)) 10735c67885aSEd Schouten return (ttydisc_rint_bypass(tp, buf, len)); 10745c67885aSEd Schouten 10755c67885aSEd Schouten for (cbuf = buf; len-- > 0; cbuf++) { 10765c67885aSEd Schouten if (ttydisc_rint(tp, *cbuf, 0) != 0) 10775c67885aSEd Schouten break; 10785c67885aSEd Schouten } 10795c67885aSEd Schouten 10805c67885aSEd Schouten return (cbuf - (const char *)buf); 10815c67885aSEd Schouten } 10825c67885aSEd Schouten 10835c67885aSEd Schouten size_t 1084d344ffe5SEd Schouten ttydisc_rint_bypass(struct tty *tp, const void *buf, size_t len) 1085bc093719SEd Schouten { 1086bc093719SEd Schouten size_t ret; 1087bc093719SEd Schouten 1088bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 1089bc093719SEd Schouten 1090bc093719SEd Schouten MPASS(tp->t_flags & TF_BYPASS); 1091bc093719SEd Schouten 1092bc093719SEd Schouten atomic_add_long(&tty_nin, len); 1093bc093719SEd Schouten 1094a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_bypass)) 1095a1215e37SEd Schouten return ttyhook_rint_bypass(tp, buf, len); 1096a1215e37SEd Schouten 1097bc093719SEd Schouten ret = ttyinq_write(&tp->t_inq, buf, len, 0); 1098bc093719SEd Schouten ttyinq_canonicalize(&tp->t_inq); 1099d40b91cbSEd Schouten if (ret < len) 1100d40b91cbSEd Schouten tty_hiwat_in_block(tp); 1101bc093719SEd Schouten 1102bc093719SEd Schouten return (ret); 1103bc093719SEd Schouten } 1104bc093719SEd Schouten 1105bc093719SEd Schouten void 1106bc093719SEd Schouten ttydisc_rint_done(struct tty *tp) 1107bc093719SEd Schouten { 1108bc093719SEd Schouten 1109bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 1110bc093719SEd Schouten 1111a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_done)) 1112a1215e37SEd Schouten ttyhook_rint_done(tp); 1113a1215e37SEd Schouten 1114bc093719SEd Schouten /* Wake up readers. */ 1115bc093719SEd Schouten tty_wakeup(tp, FREAD); 1116bc093719SEd Schouten /* Wake up driver for echo. */ 1117bc093719SEd Schouten ttydevsw_outwakeup(tp); 1118bc093719SEd Schouten } 1119bc093719SEd Schouten 1120a1215e37SEd Schouten size_t 1121a1215e37SEd Schouten ttydisc_rint_poll(struct tty *tp) 1122a1215e37SEd Schouten { 1123a1215e37SEd Schouten size_t l; 1124a1215e37SEd Schouten 1125a1215e37SEd Schouten tty_lock_assert(tp, MA_OWNED); 1126a1215e37SEd Schouten 1127a1215e37SEd Schouten if (ttyhook_hashook(tp, rint_poll)) 1128a1215e37SEd Schouten return ttyhook_rint_poll(tp); 1129a1215e37SEd Schouten 1130a1215e37SEd Schouten /* 1131a1215e37SEd Schouten * XXX: Still allow character input when there's no space in the 1132a1215e37SEd Schouten * buffers, but we haven't entered the high watermark. This is 1133a1215e37SEd Schouten * to allow backspace characters to be inserted when in 1134a1215e37SEd Schouten * canonical mode. 1135a1215e37SEd Schouten */ 1136a1215e37SEd Schouten l = ttyinq_bytesleft(&tp->t_inq); 1137a1215e37SEd Schouten if (l == 0 && (tp->t_flags & TF_HIWAT_IN) == 0) 1138a1215e37SEd Schouten return (1); 1139a1215e37SEd Schouten 1140a1215e37SEd Schouten return (l); 1141a1215e37SEd Schouten } 1142a1215e37SEd Schouten 1143bc093719SEd Schouten static void 1144bc093719SEd Schouten ttydisc_wakeup_watermark(struct tty *tp) 1145bc093719SEd Schouten { 1146bc093719SEd Schouten size_t c; 1147bc093719SEd Schouten 1148bc093719SEd Schouten c = ttyoutq_bytesleft(&tp->t_outq); 1149bc093719SEd Schouten if (tp->t_flags & TF_HIWAT_OUT) { 1150bc093719SEd Schouten /* Only allow us to run when we're below the watermark. */ 1151bc093719SEd Schouten if (c < tp->t_outlow) 1152bc093719SEd Schouten return; 1153bc093719SEd Schouten 1154bc093719SEd Schouten /* Reset the watermark. */ 1155bc093719SEd Schouten tp->t_flags &= ~TF_HIWAT_OUT; 1156bc093719SEd Schouten } else { 1157bc093719SEd Schouten /* Only run when we have data at all. */ 1158bc093719SEd Schouten if (c == 0) 1159bc093719SEd Schouten return; 1160bc093719SEd Schouten } 1161bc093719SEd Schouten tty_wakeup(tp, FWRITE); 1162bc093719SEd Schouten } 1163bc093719SEd Schouten 1164bc093719SEd Schouten size_t 1165bc093719SEd Schouten ttydisc_getc(struct tty *tp, void *buf, size_t len) 1166bc093719SEd Schouten { 1167bc093719SEd Schouten 1168bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 1169bc093719SEd Schouten 1170bc093719SEd Schouten if (tp->t_flags & TF_STOPPED) 1171bc093719SEd Schouten return (0); 1172bc093719SEd Schouten 1173a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_inject)) 1174a1215e37SEd Schouten return ttyhook_getc_inject(tp, buf, len); 1175bc093719SEd Schouten 1176a1215e37SEd Schouten len = ttyoutq_read(&tp->t_outq, buf, len); 1177a1215e37SEd Schouten 1178a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_capture)) 1179a1215e37SEd Schouten ttyhook_getc_capture(tp, buf, len); 1180a1215e37SEd Schouten 1181a1215e37SEd Schouten ttydisc_wakeup_watermark(tp); 1182ffffa83bSEd Schouten atomic_add_long(&tty_nout, len); 1183bc093719SEd Schouten 1184ffffa83bSEd Schouten return (len); 1185bc093719SEd Schouten } 1186bc093719SEd Schouten 1187bc093719SEd Schouten int 1188bc093719SEd Schouten ttydisc_getc_uio(struct tty *tp, struct uio *uio) 1189bc093719SEd Schouten { 1190a1215e37SEd Schouten int error = 0; 1191526d0bd5SKonstantin Belousov ssize_t obytes = uio->uio_resid; 1192a1215e37SEd Schouten size_t len; 1193a1215e37SEd Schouten char buf[TTY_STACKBUF]; 1194bc093719SEd Schouten 1195bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 1196bc093719SEd Schouten 1197bc093719SEd Schouten if (tp->t_flags & TF_STOPPED) 1198bc093719SEd Schouten return (0); 1199bc093719SEd Schouten 1200a1215e37SEd Schouten /* 1201a1215e37SEd Schouten * When a TTY hook is attached, we cannot perform unbuffered 1202a1215e37SEd Schouten * copying to userspace. Just call ttydisc_getc() and 1203a1215e37SEd Schouten * temporarily store data in a shadow buffer. 1204a1215e37SEd Schouten */ 1205a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_capture) || 1206a1215e37SEd Schouten ttyhook_hashook(tp, getc_inject)) { 1207a1215e37SEd Schouten while (uio->uio_resid > 0) { 1208a1215e37SEd Schouten /* Read to shadow buffer. */ 1209a1215e37SEd Schouten len = ttydisc_getc(tp, buf, 1210a1215e37SEd Schouten MIN(uio->uio_resid, sizeof buf)); 1211a1215e37SEd Schouten if (len == 0) 1212a1215e37SEd Schouten break; 1213bc093719SEd Schouten 1214a1215e37SEd Schouten /* Copy to userspace. */ 1215a1215e37SEd Schouten tty_unlock(tp); 1216a1215e37SEd Schouten error = uiomove(buf, len, uio); 1217a1215e37SEd Schouten tty_lock(tp); 1218a1215e37SEd Schouten 1219a1215e37SEd Schouten if (error != 0) 1220a1215e37SEd Schouten break; 1221a1215e37SEd Schouten } 1222a1215e37SEd Schouten } else { 1223a1215e37SEd Schouten error = ttyoutq_read_uio(&tp->t_outq, tp, uio); 1224a1215e37SEd Schouten 1225a1215e37SEd Schouten ttydisc_wakeup_watermark(tp); 1226bc093719SEd Schouten atomic_add_long(&tty_nout, obytes - uio->uio_resid); 1227a1215e37SEd Schouten } 1228bc093719SEd Schouten 1229bc093719SEd Schouten return (error); 1230bc093719SEd Schouten } 1231bc093719SEd Schouten 1232a1215e37SEd Schouten size_t 1233a1215e37SEd Schouten ttydisc_getc_poll(struct tty *tp) 1234a1215e37SEd Schouten { 1235a1215e37SEd Schouten 1236a1215e37SEd Schouten tty_lock_assert(tp, MA_OWNED); 1237a1215e37SEd Schouten 1238a1215e37SEd Schouten if (tp->t_flags & TF_STOPPED) 1239a1215e37SEd Schouten return (0); 1240a1215e37SEd Schouten 1241a1215e37SEd Schouten if (ttyhook_hashook(tp, getc_poll)) 1242a1215e37SEd Schouten return ttyhook_getc_poll(tp); 1243a1215e37SEd Schouten 1244a1215e37SEd Schouten return ttyoutq_bytesused(&tp->t_outq); 1245a1215e37SEd Schouten } 1246a1215e37SEd Schouten 1247bc093719SEd Schouten /* 1248bc093719SEd Schouten * XXX: not really related to the TTYDISC, but we'd better put 1249bc093719SEd Schouten * tty_putchar() here, because we need to perform proper output 1250bc093719SEd Schouten * processing. 1251bc093719SEd Schouten */ 1252bc093719SEd Schouten 1253bc093719SEd Schouten int 1254bc093719SEd Schouten tty_putchar(struct tty *tp, char c) 1255bc093719SEd Schouten { 1256bc093719SEd Schouten tty_lock_assert(tp, MA_OWNED); 1257bc093719SEd Schouten 1258bc093719SEd Schouten if (tty_gone(tp)) 1259bc093719SEd Schouten return (-1); 1260bc093719SEd Schouten 1261bc093719SEd Schouten ttydisc_echo_force(tp, c, 0); 1262bc093719SEd Schouten tp->t_writepos = tp->t_column; 1263bc093719SEd Schouten ttyinq_reprintpos_set(&tp->t_inq); 1264bc093719SEd Schouten 1265bc093719SEd Schouten ttydevsw_outwakeup(tp); 1266bc093719SEd Schouten return (0); 1267bc093719SEd Schouten } 1268