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