xref: /csrg-svn/sys/kern/tty.c (revision 23165)
1 /*	tty.c	6.18	85/06/08	*/
2 
3 #include "../machine/reg.h"
4 
5 #include "param.h"
6 #include "systm.h"
7 #include "dir.h"
8 #include "user.h"
9 #include "ioctl.h"
10 #include "tty.h"
11 #include "proc.h"
12 #include "inode.h"
13 #include "file.h"
14 #include "conf.h"
15 #include "buf.h"
16 #include "dk.h"
17 #include "uio.h"
18 #include "kernel.h"
19 
20 /*
21  * Table giving parity for characters and indicating
22  * character classes to tty driver.  In particular,
23  * if the low 6 bits are 0, then the character needs
24  * no special processing on output.
25  */
26 
27 char partab[] = {
28 	0001,0201,0201,0001,0201,0001,0001,0201,
29 	0202,0004,0003,0201,0005,0206,0201,0001,
30 	0201,0001,0001,0201,0001,0201,0201,0001,
31 	0001,0201,0201,0001,0201,0001,0001,0201,
32 	0200,0000,0000,0200,0000,0200,0200,0000,
33 	0000,0200,0200,0000,0200,0000,0000,0200,
34 	0000,0200,0200,0000,0200,0000,0000,0200,
35 	0200,0000,0000,0200,0000,0200,0200,0000,
36 	0200,0000,0000,0200,0000,0200,0200,0000,
37 	0000,0200,0200,0000,0200,0000,0000,0200,
38 	0000,0200,0200,0000,0200,0000,0000,0200,
39 	0200,0000,0000,0200,0000,0200,0200,0000,
40 	0000,0200,0200,0000,0200,0000,0000,0200,
41 	0200,0000,0000,0200,0000,0200,0200,0000,
42 	0200,0000,0000,0200,0000,0200,0200,0000,
43 	0000,0200,0200,0000,0200,0000,0000,0201,
44 
45 	/*
46 	 * 7 bit ascii ends with the last character above,
47 	 * but we contine through all 256 codes for the sake
48 	 * of the tty output routines which use special vax
49 	 * instructions which need a 256 character trt table.
50 	 */
51 
52 	0007,0007,0007,0007,0007,0007,0007,0007,
53 	0007,0007,0007,0007,0007,0007,0007,0007,
54 	0007,0007,0007,0007,0007,0007,0007,0007,
55 	0007,0007,0007,0007,0007,0007,0007,0007,
56 	0007,0007,0007,0007,0007,0007,0007,0007,
57 	0007,0007,0007,0007,0007,0007,0007,0007,
58 	0007,0007,0007,0007,0007,0007,0007,0007,
59 	0007,0007,0007,0007,0007,0007,0007,0007,
60 	0007,0007,0007,0007,0007,0007,0007,0007,
61 	0007,0007,0007,0007,0007,0007,0007,0007,
62 	0007,0007,0007,0007,0007,0007,0007,0007,
63 	0007,0007,0007,0007,0007,0007,0007,0007,
64 	0007,0007,0007,0007,0007,0007,0007,0007,
65 	0007,0007,0007,0007,0007,0007,0007,0007,
66 	0007,0007,0007,0007,0007,0007,0007,0007,
67 	0007,0007,0007,0007,0007,0007,0007,0007
68 };
69 
70 /*
71  * Input mapping table-- if an entry is non-zero, when the
72  * corresponding character is typed preceded by "\" the escape
73  * sequence is replaced by the table value.  Mostly used for
74  * upper-case only terminals.
75  */
76 char	maptab[] ={
77 	000,000,000,000,000,000,000,000,
78 	000,000,000,000,000,000,000,000,
79 	000,000,000,000,000,000,000,000,
80 	000,000,000,000,000,000,000,000,
81 	000,'|',000,000,000,000,000,'`',
82 	'{','}',000,000,000,000,000,000,
83 	000,000,000,000,000,000,000,000,
84 	000,000,000,000,000,000,000,000,
85 	000,000,000,000,000,000,000,000,
86 	000,000,000,000,000,000,000,000,
87 	000,000,000,000,000,000,000,000,
88 	000,000,000,000,000,000,'~',000,
89 	000,'A','B','C','D','E','F','G',
90 	'H','I','J','K','L','M','N','O',
91 	'P','Q','R','S','T','U','V','W',
92 	'X','Y','Z',000,000,000,000,000,
93 };
94 
95 short	tthiwat[16] =
96    { 100,100,100,100,100,100,100,200,200,400,400,400,650,650,1300,2000 };
97 short	ttlowat[16] =
98    {  30, 30, 30, 30, 30, 30, 30, 50, 50,120,120,120,125,125,125,125 };
99 
100 struct	ttychars ttydefaults = {
101 	CERASE,	CKILL,	CINTR,	CQUIT,	CSTART,	CSTOP,	CEOF,
102 	CBRK,	CSUSP,	CDSUSP, CRPRNT, CFLUSH, CWERASE,CLNEXT
103 };
104 
105 ttychars(tp)
106 	struct tty *tp;
107 {
108 
109 	tp->t_chars = ttydefaults;
110 }
111 
112 /*
113  * Wait for output to drain, then flush input waiting.
114  */
115 ttywflush(tp)
116 	register struct tty *tp;
117 {
118 
119 	ttywait(tp);
120 	ttyflush(tp, FREAD);
121 }
122 
123 ttywait(tp)
124 	register struct tty *tp;
125 {
126 	register int s = spltty();
127 
128 	while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
129 	    tp->t_state&TS_CARR_ON && tp->t_oproc) {	/* kludge for pty */
130 		(*tp->t_oproc)(tp);
131 		tp->t_state |= TS_ASLEEP;
132 		sleep((caddr_t)&tp->t_outq, TTOPRI);
133 	}
134 	splx(s);
135 }
136 
137 /*
138  * Flush all TTY queues
139  */
140 ttyflush(tp, rw)
141 	register struct tty *tp;
142 {
143 	register s;
144 
145 	s = spltty();
146 	if (rw & FREAD) {
147 		while (getc(&tp->t_canq) >= 0)
148 			;
149 		wakeup((caddr_t)&tp->t_rawq);
150 	}
151 	if (rw & FWRITE) {
152 		wakeup((caddr_t)&tp->t_outq);
153 		tp->t_state &= ~TS_TTSTOP;
154 		(*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
155 		while (getc(&tp->t_outq) >= 0)
156 			;
157 	}
158 	if (rw & FREAD) {
159 		while (getc(&tp->t_rawq) >= 0)
160 			;
161 		tp->t_rocount = 0;
162 		tp->t_rocol = 0;
163 		tp->t_state &= ~TS_LOCAL;
164 	}
165 	splx(s);
166 }
167 
168 /*
169  * Send stop character on input overflow.
170  */
171 ttyblock(tp)
172 	register struct tty *tp;
173 {
174 	register x;
175 
176 	x = tp->t_rawq.c_cc + tp->t_canq.c_cc;
177 	if (tp->t_rawq.c_cc > TTYHOG) {
178 		ttyflush(tp, FREAD|FWRITE);
179 		tp->t_state &= ~TS_TBLOCK;
180 	}
181 	/*
182 	 * Block further input iff:
183 	 * Current input > threshold AND input is available to user program
184 	 */
185 	if (x >= TTYHOG/2 &&
186 	    ((tp->t_flags & (RAW|CBREAK)) || (tp->t_canq.c_cc > 0))) {
187 		if (putc(tp->t_stopc, &tp->t_outq)==0) {
188 			tp->t_state |= TS_TBLOCK;
189 			ttstart(tp);
190 		}
191 	}
192 }
193 
194 /*
195  * Restart typewriter output following a delay
196  * timeout.
197  * The name of the routine is passed to the timeout
198  * subroutine and it is called during a clock interrupt.
199  */
200 ttrstrt(tp)
201 	register struct tty *tp;
202 {
203 
204 	if (tp == 0)
205 		panic("ttrstrt");
206 	tp->t_state &= ~TS_TIMEOUT;
207 	ttstart(tp);
208 }
209 
210 /*
211  * Start output on the typewriter. It is used from the top half
212  * after some characters have been put on the output queue,
213  * from the interrupt routine to transmit the next
214  * character, and after a timeout has finished.
215  */
216 ttstart(tp)
217 	register struct tty *tp;
218 {
219 	register s;
220 
221 	s = spltty();
222 	if ((tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) == 0 &&
223 	    tp->t_oproc)		/* kludge for pty */
224 		(*tp->t_oproc)(tp);
225 	splx(s);
226 }
227 
228 /*
229  * Common code for tty ioctls.
230  */
231 /*ARGSUSED*/
232 ttioctl(tp, com, data, flag)
233 	register struct tty *tp;
234 	caddr_t data;
235 {
236 	int dev = tp->t_dev;
237 	extern int nldisp;
238 	int s;
239 	register int newflags;
240 
241 	/*
242 	 * If the ioctl involves modification,
243 	 * hang if in the background.
244 	 */
245 	switch (com) {
246 
247 	case TIOCSETD:
248 	case TIOCSETP:
249 	case TIOCSETN:
250 	case TIOCFLUSH:
251 	case TIOCSETC:
252 	case TIOCSLTC:
253 	case TIOCSPGRP:
254 	case TIOCLBIS:
255 	case TIOCLBIC:
256 	case TIOCLSET:
257 	case TIOCSTI:
258 	case TIOCSWINSZ:
259 #define bit(a) (1<<(a-1))
260 		while (tp->t_line == NTTYDISC &&
261 		   u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
262 		   (u.u_procp->p_flag&SVFORK) == 0 &&
263 		   !(u.u_procp->p_sigignore & bit(SIGTTOU)) &&
264 		   !(u.u_procp->p_sigmask & bit(SIGTTOU))) {
265 			gsignal(u.u_procp->p_pgrp, SIGTTOU);
266 			sleep((caddr_t)&lbolt, TTOPRI);
267 		}
268 		break;
269 	}
270 #undef	bit
271 
272 	/*
273 	 * Process the ioctl.
274 	 */
275 	switch (com) {
276 
277 	/* get discipline number */
278 	case TIOCGETD:
279 		*(int *)data = tp->t_line;
280 		break;
281 
282 	/* set line discipline */
283 	case TIOCSETD: {
284 		register int t = *(int *)data;
285 		int error = 0;
286 
287 		if ((unsigned) t >= nldisp)
288 			return (ENXIO);
289 		s = spltty();
290 		if (tp->t_line)
291 			(*linesw[tp->t_line].l_close)(tp);
292 		if (t)
293 			error = (*linesw[t].l_open)(dev, tp);
294 		if (error) {
295 			if (tp->t_line)
296 				(void) (*linesw[tp->t_line].l_open)(dev, tp);
297 			splx(s);
298 			return (error);
299 		}
300 		tp->t_line = t;
301 		splx(s);
302 		break;
303 	}
304 
305 	/* prevent more opens on channel */
306 	case TIOCEXCL:
307 		tp->t_state |= TS_XCLUDE;
308 		break;
309 
310 	case TIOCNXCL:
311 		tp->t_state &= ~TS_XCLUDE;
312 		break;
313 
314 	/* hang up line on last close */
315 	case TIOCHPCL:
316 		tp->t_state |= TS_HUPCLS;
317 		break;
318 
319 	case TIOCFLUSH: {
320 		register int flags = *(int *)data;
321 
322 		if (flags == 0)
323 			flags = FREAD|FWRITE;
324 		else
325 			flags &= FREAD|FWRITE;
326 		ttyflush(tp, flags);
327 		break;
328 	}
329 
330 	/* return number of characters immediately available */
331 	case FIONREAD:
332 		*(off_t *)data = ttnread(tp);
333 		break;
334 
335 	case TIOCOUTQ:
336 		*(int *)data = tp->t_outq.c_cc;
337 		break;
338 
339 	case TIOCSTOP:
340 		s = spltty();
341 		if ((tp->t_state&TS_TTSTOP) == 0) {
342 			tp->t_state |= TS_TTSTOP;
343 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
344 		}
345 		splx(s);
346 		break;
347 
348 	case TIOCSTART:
349 		s = spltty();
350 		if ((tp->t_state&TS_TTSTOP) || (tp->t_flags&FLUSHO)) {
351 			tp->t_state &= ~TS_TTSTOP;
352 			tp->t_flags &= ~FLUSHO;
353 			ttstart(tp);
354 		}
355 		splx(s);
356 		break;
357 
358 	/*
359 	 * Simulate typing of a character at the terminal.
360 	 */
361 	case TIOCSTI:
362 		if (u.u_uid && (flag & FREAD) == 0)
363 			return (EPERM);
364 		if (u.u_uid && u.u_ttyp != tp)
365 			return (EACCES);
366 		(*linesw[tp->t_line].l_rint)(*(char *)data, tp);
367 		break;
368 
369 	case TIOCSETP:
370 	case TIOCSETN: {
371 		register struct sgttyb *sg = (struct sgttyb *)data;
372 
373 		tp->t_erase = sg->sg_erase;
374 		tp->t_kill = sg->sg_kill;
375 		tp->t_ispeed = sg->sg_ispeed;
376 		tp->t_ospeed = sg->sg_ospeed;
377 		newflags = (tp->t_flags&0xffff0000) | (sg->sg_flags&0xffff);
378 		s = spltty();
379 		if (tp->t_flags&RAW || newflags&RAW || com == TIOCSETP) {
380 			ttywait(tp);
381 			ttyflush(tp, FREAD);
382 		} else if ((tp->t_flags&CBREAK) != (newflags&CBREAK)) {
383 			if (newflags&CBREAK) {
384 				struct clist tq;
385 
386 				catq(&tp->t_rawq, &tp->t_canq);
387 				tq = tp->t_rawq;
388 				tp->t_rawq = tp->t_canq;
389 				tp->t_canq = tq;
390 			} else {
391 				tp->t_flags |= PENDIN;
392 				newflags |= PENDIN;
393 				ttwakeup(tp);
394 			}
395 		}
396 		tp->t_flags = newflags;
397 		if (tp->t_flags&RAW) {
398 			tp->t_state &= ~TS_TTSTOP;
399 			ttstart(tp);
400 		}
401 		splx(s);
402 		break;
403 	}
404 
405 	/* send current parameters to user */
406 	case TIOCGETP: {
407 		register struct sgttyb *sg = (struct sgttyb *)data;
408 
409 		sg->sg_ispeed = tp->t_ispeed;
410 		sg->sg_ospeed = tp->t_ospeed;
411 		sg->sg_erase = tp->t_erase;
412 		sg->sg_kill = tp->t_kill;
413 		sg->sg_flags = tp->t_flags;
414 		break;
415 	}
416 
417 	case FIONBIO:
418 		if (*(int *)data)
419 			tp->t_state |= TS_NBIO;
420 		else
421 			tp->t_state &= ~TS_NBIO;
422 		break;
423 
424 	case FIOASYNC:
425 		if (*(int *)data)
426 			tp->t_state |= TS_ASYNC;
427 		else
428 			tp->t_state &= ~TS_ASYNC;
429 		break;
430 
431 	case TIOCGETC:
432 		bcopy((caddr_t)&tp->t_intrc, data, sizeof (struct tchars));
433 		break;
434 
435 	case TIOCSETC:
436 		bcopy(data, (caddr_t)&tp->t_intrc, sizeof (struct tchars));
437 		break;
438 
439 	/* set/get local special characters */
440 	case TIOCSLTC:
441 		bcopy(data, (caddr_t)&tp->t_suspc, sizeof (struct ltchars));
442 		break;
443 
444 	case TIOCGLTC:
445 		bcopy((caddr_t)&tp->t_suspc, data, sizeof (struct ltchars));
446 		break;
447 
448 	/*
449 	 * Modify local mode word.
450 	 */
451 	case TIOCLBIS:
452 		tp->t_flags |= *(int *)data << 16;
453 		break;
454 
455 	case TIOCLBIC:
456 		tp->t_flags &= ~(*(int *)data << 16);
457 		break;
458 
459 	case TIOCLSET:
460 		tp->t_flags &= 0xffff;
461 		tp->t_flags |= *(int *)data << 16;
462 		break;
463 
464 	case TIOCLGET:
465 		*(int *)data = ((unsigned) tp->t_flags) >> 16;
466 		break;
467 
468 	/*
469 	 * Allow SPGRP only if tty is open for reading.
470 	 * Quick check: if we can find a process in the new pgrp,
471 	 * this user must own that process.
472 	 * SHOULD VERIFY THAT PGRP IS IN USE AND IS THIS USER'S.
473 	 */
474 	case TIOCSPGRP: {
475 		struct proc *p;
476 		int pgrp = *(int *)data;
477 
478 		if (u.u_uid && (flag & FREAD) == 0)
479 			return (EPERM);
480 		p = pfind(pgrp);
481 		if (p && p->p_pgrp == pgrp &&
482 		    p->p_uid != u.u_uid && u.u_uid && !inferior(p))
483 			return (EPERM);
484 		tp->t_pgrp = pgrp;
485 		break;
486 	}
487 
488 	case TIOCGPGRP:
489 		*(int *)data = tp->t_pgrp;
490 		break;
491 
492 	case TIOCSWINSZ:
493 		if (bcmp((caddr_t)&tp->t_winsize, data,
494 		    sizeof (struct winsize))) {
495 			tp->t_winsize = *(struct winsize *)data;
496 			gsignal(tp->t_pgrp, SIGWINCH);
497 		}
498 		break;
499 
500 	case TIOCGWINSZ:
501 		*(struct winsize *)data = tp->t_winsize;
502 		break;
503 
504 	default:
505 		return (-1);
506 	}
507 	return (0);
508 }
509 
510 ttnread(tp)
511 	struct tty *tp;
512 {
513 	int nread = 0;
514 
515 	if (tp->t_flags & PENDIN)
516 		ttypend(tp);
517 	nread = tp->t_canq.c_cc;
518 	if (tp->t_flags & (RAW|CBREAK))
519 		nread += tp->t_rawq.c_cc;
520 	return (nread);
521 }
522 
523 ttselect(dev, rw)
524 	dev_t dev;
525 	int rw;
526 {
527 	register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
528 	int nread;
529 	int s = spltty();
530 
531 	switch (rw) {
532 
533 	case FREAD:
534 		nread = ttnread(tp);
535 		if ((nread > 0) || ((tp->t_state & TS_CARR_ON) == 0))
536 			goto win;
537 		if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
538 			tp->t_state |= TS_RCOLL;
539 		else
540 			tp->t_rsel = u.u_procp;
541 		break;
542 
543 	case FWRITE:
544 		if (tp->t_outq.c_cc <= TTLOWAT(tp))
545 			goto win;
546 		if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
547 			tp->t_state |= TS_WCOLL;
548 		else
549 			tp->t_wsel = u.u_procp;
550 		break;
551 	}
552 	splx(s);
553 	return (0);
554 win:
555 	splx(s);
556 	return (1);
557 }
558 
559 /*
560  * Establish a process group for distribution of
561  * quits and interrupts from the tty.
562  */
563 ttyopen(dev, tp)
564 	dev_t dev;
565 	register struct tty *tp;
566 {
567 	register struct proc *pp;
568 
569 	pp = u.u_procp;
570 	tp->t_dev = dev;
571 	if (pp->p_pgrp == 0) {
572 		u.u_ttyp = tp;
573 		u.u_ttyd = dev;
574 		if (tp->t_pgrp == 0)
575 			tp->t_pgrp = pp->p_pid;
576 		pp->p_pgrp = tp->t_pgrp;
577 	}
578 	tp->t_state &= ~TS_WOPEN;
579 	if ((tp->t_state & TS_ISOPEN) == 0) {
580 		tp->t_state |= TS_ISOPEN;
581 		bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize));
582 		if (tp->t_line != NTTYDISC)
583 			ttywflush(tp);
584 	}
585 	return (0);
586 }
587 
588 /*
589  * clean tp on last close
590  */
591 ttyclose(tp)
592 	register struct tty *tp;
593 {
594 
595 	if (tp->t_line) {
596 		ttywflush(tp);
597 		tp->t_line = 0;
598 		return;
599 	}
600 	tp->t_pgrp = 0;
601 	ttywflush(tp);
602 	tp->t_state = 0;
603 }
604 
605 /*
606  * reinput pending characters after state switch
607  * call at spltty().
608  */
609 ttypend(tp)
610 	register struct tty *tp;
611 {
612 	struct clist tq;
613 	register c;
614 
615 	tp->t_flags &= ~PENDIN;
616 	tp->t_state |= TS_TYPEN;
617 	tq = tp->t_rawq;
618 	tp->t_rawq.c_cc = 0;
619 	tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
620 	while ((c = getc(&tq)) >= 0)
621 		ttyinput(c, tp);
622 	tp->t_state &= ~TS_TYPEN;
623 }
624 
625 /*
626  * Place a character on raw TTY input queue,
627  * putting in delimiters and waking up top
628  * half as needed.  Also echo if required.
629  * The arguments are the character and the
630  * appropriate tty structure.
631  */
632 ttyinput(c, tp)
633 	register c;
634 	register struct tty *tp;
635 {
636 	register int t_flags = tp->t_flags;
637 	int i;
638 
639 	/*
640 	 * If input is pending take it first.
641 	 */
642 	if (t_flags&PENDIN)
643 		ttypend(tp);
644 	tk_nin++;
645 	c &= 0377;
646 
647 	/*
648 	 * In tandem mode, check high water mark.
649 	 */
650 	if (t_flags&TANDEM)
651 		ttyblock(tp);
652 
653 	if (t_flags&RAW) {
654 		/*
655 		 * Raw mode, just put character
656 		 * in input q w/o interpretation.
657 		 */
658 		if (tp->t_rawq.c_cc > TTYHOG)
659 			ttyflush(tp, FREAD|FWRITE);
660 		else {
661 			if (putc(c, &tp->t_rawq) >= 0)
662 				ttwakeup(tp);
663 			ttyecho(c, tp);
664 		}
665 		goto endcase;
666 	}
667 
668 	/*
669 	 * Ignore any high bit added during
670 	 * previous ttyinput processing.
671 	 */
672 	if ((tp->t_state&TS_TYPEN) == 0)
673 		c &= 0177;
674 	/*
675 	 * Check for literal nexting very first
676 	 */
677 	if (tp->t_state&TS_LNCH) {
678 		c |= 0200;
679 		tp->t_state &= ~TS_LNCH;
680 	}
681 
682 	/*
683 	 * Scan for special characters.  This code
684 	 * is really just a big case statement with
685 	 * non-constant cases.  The bottom of the
686 	 * case statement is labeled ``endcase'', so goto
687 	 * it after a case match, or similar.
688 	 */
689 	if (tp->t_line == NTTYDISC) {
690 		if (c == tp->t_lnextc) {
691 			if (t_flags&ECHO)
692 				ttyout("^\b", tp);
693 			tp->t_state |= TS_LNCH;
694 			goto endcase;
695 		}
696 		if (c == tp->t_flushc) {
697 			if (t_flags&FLUSHO)
698 				tp->t_flags &= ~FLUSHO;
699 			else {
700 				ttyflush(tp, FWRITE);
701 				ttyecho(c, tp);
702 				if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
703 					ttyretype(tp);
704 				tp->t_flags |= FLUSHO;
705 			}
706 			goto startoutput;
707 		}
708 		if (c == tp->t_suspc) {
709 			if ((t_flags&NOFLSH) == 0)
710 				ttyflush(tp, FREAD);
711 			ttyecho(c, tp);
712 			gsignal(tp->t_pgrp, SIGTSTP);
713 			goto endcase;
714 		}
715 	}
716 
717 	/*
718 	 * Handle start/stop characters.
719 	 */
720 	if (c == tp->t_stopc) {
721 		if ((tp->t_state&TS_TTSTOP) == 0) {
722 			tp->t_state |= TS_TTSTOP;
723 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
724 			return;
725 		}
726 		if (c != tp->t_startc)
727 			return;
728 		goto endcase;
729 	}
730 	if (c == tp->t_startc)
731 		goto restartoutput;
732 
733 	/*
734 	 * Look for interrupt/quit chars.
735 	 */
736 	if (c == tp->t_intrc || c == tp->t_quitc) {
737 		if ((t_flags&NOFLSH) == 0)
738 			ttyflush(tp, FREAD|FWRITE);
739 		ttyecho(c, tp);
740 		gsignal(tp->t_pgrp, c == tp->t_intrc ? SIGINT : SIGQUIT);
741 		goto endcase;
742 	}
743 
744 	if (tp->t_flags & LCASE && c <= 0177) {
745 		if (tp->t_state&TS_BKSL) {
746 			ttyrub(unputc(&tp->t_rawq), tp);
747 			if (maptab[c])
748 				c = maptab[c];
749 			c |= 0200;
750 			tp->t_state &= ~(TS_BKSL|TS_QUOT);
751 		} else if (c >= 'A' && c <= 'Z')
752 			c += 'a' - 'A';
753 		else if (c == '\\')
754 			tp->t_state |= TS_BKSL;
755 	}
756 
757 	/*
758 	 * Cbreak mode, don't process line editing
759 	 * characters; check high water mark for wakeup.
760 	 */
761 	if (t_flags&CBREAK) {
762 		if (tp->t_rawq.c_cc > TTYHOG) {
763 			if (tp->t_outq.c_cc < TTHIWAT(tp) &&
764 			    tp->t_line == NTTYDISC)
765 				(void) ttyoutput(CTRL(g), tp);
766 		} else if (putc(c, &tp->t_rawq) >= 0) {
767 			ttwakeup(tp);
768 			ttyecho(c, tp);
769 		}
770 		goto endcase;
771 	}
772 
773 	/*
774 	 * From here on down cooked mode character
775 	 * processing takes place.
776 	 */
777 	if ((tp->t_state&TS_QUOT) &&
778 	    (c == tp->t_erase || c == tp->t_kill)) {
779 		ttyrub(unputc(&tp->t_rawq), tp);
780 		c |= 0200;
781 	}
782 	if (c == tp->t_erase) {
783 		if (tp->t_rawq.c_cc)
784 			ttyrub(unputc(&tp->t_rawq), tp);
785 		goto endcase;
786 	}
787 	if (c == tp->t_kill) {
788 		if (t_flags&CRTKIL &&
789 		    tp->t_rawq.c_cc == tp->t_rocount) {
790 			while (tp->t_rawq.c_cc)
791 				ttyrub(unputc(&tp->t_rawq), tp);
792 		} else {
793 			ttyecho(c, tp);
794 			ttyecho('\n', tp);
795 			while (getc(&tp->t_rawq) > 0)
796 				;
797 			tp->t_rocount = 0;
798 		}
799 		tp->t_state &= ~TS_LOCAL;
800 		goto endcase;
801 	}
802 
803 	/*
804 	 * New line discipline,
805 	 * check word erase/reprint line.
806 	 */
807 	if (tp->t_line == NTTYDISC) {
808 		if (c == tp->t_werasc) {
809 			if (tp->t_rawq.c_cc == 0)
810 				goto endcase;
811 			do {
812 				c = unputc(&tp->t_rawq);
813 				if (c != ' ' && c != '\t')
814 					goto erasenb;
815 				ttyrub(c, tp);
816 			} while (tp->t_rawq.c_cc);
817 			goto endcase;
818 	erasenb:
819 			do {
820 				ttyrub(c, tp);
821 				if (tp->t_rawq.c_cc == 0)
822 					goto endcase;
823 				c = unputc(&tp->t_rawq);
824 			} while (c != ' ' && c != '\t');
825 			(void) putc(c, &tp->t_rawq);
826 			goto endcase;
827 		}
828 		if (c == tp->t_rprntc) {
829 			ttyretype(tp);
830 			goto endcase;
831 		}
832 	}
833 
834 	/*
835 	 * Check for input buffer overflow
836 	 */
837 	if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) {
838 		if (tp->t_line == NTTYDISC)
839 			(void) ttyoutput(CTRL(g), tp);
840 		goto endcase;
841 	}
842 
843 	/*
844 	 * Put data char in q for user and
845 	 * wakeup on seeing a line delimiter.
846 	 */
847 	if (putc(c, &tp->t_rawq) >= 0) {
848 		if (ttbreakc(c, tp)) {
849 			tp->t_rocount = 0;
850 			catq(&tp->t_rawq, &tp->t_canq);
851 			ttwakeup(tp);
852 		} else if (tp->t_rocount++ == 0)
853 			tp->t_rocol = tp->t_col;
854 		tp->t_state &= ~TS_QUOT;
855 		if (c == '\\')
856 			tp->t_state |= TS_QUOT;
857 		if (tp->t_state&TS_ERASE) {
858 			tp->t_state &= ~TS_ERASE;
859 			(void) ttyoutput('/', tp);
860 		}
861 		i = tp->t_col;
862 		ttyecho(c, tp);
863 		if (c == tp->t_eofc && t_flags&ECHO) {
864 			i = MIN(2, tp->t_col - i);
865 			while (i > 0) {
866 				(void) ttyoutput('\b', tp);
867 				i--;
868 			}
869 		}
870 	}
871 endcase:
872 	/*
873 	 * If DEC-style start/stop is enabled don't restart
874 	 * output until seeing the start character.
875 	 */
876 	if (t_flags&DECCTQ && tp->t_state&TS_TTSTOP &&
877 	    tp->t_startc != tp->t_stopc)
878 		return;
879 restartoutput:
880 	tp->t_state &= ~TS_TTSTOP;
881 	tp->t_flags &= ~FLUSHO;
882 startoutput:
883 	ttstart(tp);
884 }
885 
886 /*
887  * Put character on TTY output queue, adding delays,
888  * expanding tabs, and handling the CR/NL bit.
889  * This is called both from the top half for output,
890  * and from interrupt level for echoing.
891  * The arguments are the character and the tty structure.
892  * Returns < 0 if putc succeeds, otherwise returns char to resend
893  * Must be recursive.
894  */
895 ttyoutput(c, tp)
896 	register c;
897 	register struct tty *tp;
898 {
899 	register char *colp;
900 	register ctype;
901 
902 	if (tp->t_flags & (RAW|LITOUT)) {
903 		if (tp->t_flags&FLUSHO)
904 			return (-1);
905 		if (putc(c, &tp->t_outq))
906 			return (c);
907 		tk_nout++;
908 		return (-1);
909 	}
910 
911 	/*
912 	 * Ignore EOT in normal mode to avoid
913 	 * hanging up certain terminals.
914 	 */
915 	c &= 0177;
916 	if (c == CEOT && (tp->t_flags&CBREAK) == 0)
917 		return (-1);
918 	/*
919 	 * Turn tabs to spaces as required
920 	 */
921 	if (c == '\t' && (tp->t_flags&TBDELAY) == XTABS) {
922 		register int s;
923 
924 		c = 8 - (tp->t_col&7);
925 		if ((tp->t_flags&FLUSHO) == 0) {
926 			s = spltty();		/* don't interrupt tabs */
927 			c -= b_to_q("        ", c, &tp->t_outq);
928 			tk_nout += c;
929 			splx(s);
930 		}
931 		tp->t_col += c;
932 		return (c ? -1 : '\t');
933 	}
934 	tk_nout++;
935 	/*
936 	 * for upper-case-only terminals,
937 	 * generate escapes.
938 	 */
939 	if (tp->t_flags&LCASE) {
940 		colp = "({)}!|^~'`";
941 		while (*colp++)
942 			if (c == *colp++) {
943 				if (ttyoutput('\\', tp) >= 0)
944 					return (c);
945 				c = colp[-2];
946 				break;
947 			}
948 		if ('A' <= c && c <= 'Z') {
949 			if (ttyoutput('\\', tp) >= 0)
950 				return (c);
951 		} else if ('a' <= c && c <= 'z')
952 			c += 'A' - 'a';
953 	}
954 
955 	/*
956 	 * turn <nl> to <cr><lf> if desired.
957 	 */
958 	if (c == '\n' && tp->t_flags&CRMOD)
959 		if (ttyoutput('\r', tp) >= 0)
960 			return (c);
961 	if (c == '~' && tp->t_flags&TILDE)
962 		c = '`';
963 	if ((tp->t_flags&FLUSHO) == 0 && putc(c, &tp->t_outq))
964 		return (c);
965 	/*
966 	 * Calculate delays.
967 	 * The numbers here represent clock ticks
968 	 * and are not necessarily optimal for all terminals.
969 	 * The delays are indicated by characters above 0200.
970 	 * In raw mode there are no delays and the
971 	 * transmission path is 8 bits wide.
972 	 *
973 	 * SHOULD JUST ALLOW USER TO SPECIFY DELAYS
974 	 */
975 	colp = &tp->t_col;
976 	ctype = partab[c];
977 	c = 0;
978 	switch (ctype&077) {
979 
980 	case ORDINARY:
981 		(*colp)++;
982 
983 	case CONTROL:
984 		break;
985 
986 	case BACKSPACE:
987 		if (*colp)
988 			(*colp)--;
989 		break;
990 
991 	/*
992 	 * This macro is close enough to the correct thing;
993 	 * it should be replaced by real user settable delays
994 	 * in any event...
995 	 */
996 #define	mstohz(ms)	(((ms) * hz) >> 10)
997 	case NEWLINE:
998 		ctype = (tp->t_flags >> 8) & 03;
999 		if (ctype == 1) { /* tty 37 */
1000 			if (*colp > 0)
1001 				c = max((((unsigned)*colp) >> 4) + 3,
1002 				    (unsigned)6);
1003 		} else if (ctype == 2) /* vt05 */
1004 			c = mstohz(100);
1005 		*colp = 0;
1006 		break;
1007 
1008 	case TAB:
1009 		ctype = (tp->t_flags >> 10) & 03;
1010 		if (ctype == 1) { /* tty 37 */
1011 			c = 1 - (*colp | ~07);
1012 			if (c < 5)
1013 				c = 0;
1014 		}
1015 		*colp |= 07;
1016 		(*colp)++;
1017 		break;
1018 
1019 	case VTAB:
1020 		if (tp->t_flags&VTDELAY) /* tty 37 */
1021 			c = 0177;
1022 		break;
1023 
1024 	case RETURN:
1025 		ctype = (tp->t_flags >> 12) & 03;
1026 		if (ctype == 1) /* tn 300 */
1027 			c = mstohz(83);
1028 		else if (ctype == 2) /* ti 700 */
1029 			c = mstohz(166);
1030 		else if (ctype == 3) { /* concept 100 */
1031 			int i;
1032 
1033 			if ((i = *colp) >= 0)
1034 				for (; i < 9; i++)
1035 					(void) putc(0177, &tp->t_outq);
1036 		}
1037 		*colp = 0;
1038 	}
1039 	if (c && (tp->t_flags&FLUSHO) == 0)
1040 		(void) putc(c|0200, &tp->t_outq);
1041 	return (-1);
1042 }
1043 #undef mstohz
1044 
1045 /*
1046  * Called from device's read routine after it has
1047  * calculated the tty-structure given as argument.
1048  */
1049 ttread(tp, uio)
1050 	register struct tty *tp;
1051 	struct uio *uio;
1052 {
1053 	register struct clist *qp;
1054 	register c, t_flags;
1055 	int s, first, error = 0;
1056 
1057 loop:
1058 	/*
1059 	 * Take any pending input first.
1060 	 */
1061 	s = spltty();
1062 	if (tp->t_flags&PENDIN)
1063 		ttypend(tp);
1064 	splx(s);
1065 
1066 	if ((tp->t_state&TS_CARR_ON)==0)
1067 		return (EIO);
1068 
1069 	/*
1070 	 * Hang process if it's in the background.
1071 	 */
1072 #define bit(a) (1<<(a-1))
1073 	if (tp == u.u_ttyp && u.u_procp->p_pgrp != tp->t_pgrp) {
1074 		if ((u.u_procp->p_sigignore & bit(SIGTTIN)) ||
1075 		   (u.u_procp->p_sigmask & bit(SIGTTIN)) ||
1076 		    u.u_procp->p_flag&SVFORK)
1077 			return (EIO);
1078 		gsignal(u.u_procp->p_pgrp, SIGTTIN);
1079 		sleep((caddr_t)&lbolt, TTIPRI);
1080 		goto loop;
1081 	}
1082 	t_flags = tp->t_flags;
1083 #undef	bit
1084 
1085 	/*
1086 	 * In raw mode take characters directly from the
1087 	 * raw queue w/o processing.  Interlock against
1088 	 * device interrupts when interrogating rawq.
1089 	 */
1090 	if (t_flags&RAW) {
1091 		s = spltty();
1092 		if (tp->t_rawq.c_cc <= 0) {
1093 			if ((tp->t_state&TS_CARR_ON) == 0 ||
1094 			    (tp->t_state&TS_NBIO)) {
1095 				splx(s);
1096 				return (EWOULDBLOCK);
1097 			}
1098 			sleep((caddr_t)&tp->t_rawq, TTIPRI);
1099 			splx(s);
1100 			goto loop;
1101 		}
1102 		splx(s);
1103  		while (!error && tp->t_rawq.c_cc && uio->uio_resid)
1104  			error = ureadc(getc(&tp->t_rawq), uio);
1105 		goto checktandem;
1106 	}
1107 
1108 	/*
1109 	 * In cbreak mode use the rawq, otherwise
1110 	 * take characters from the canonicalized q.
1111 	 */
1112 	qp = t_flags&CBREAK ? &tp->t_rawq : &tp->t_canq;
1113 
1114 	/*
1115 	 * No input, sleep on rawq awaiting hardware
1116 	 * receipt and notification.
1117 	 */
1118 	s = spltty();
1119 	if (qp->c_cc <= 0) {
1120 		if ((tp->t_state&TS_CARR_ON) == 0 ||
1121 		    (tp->t_state&TS_NBIO)) {
1122 			splx(s);
1123 			return (EWOULDBLOCK);
1124 		}
1125 		sleep((caddr_t)&tp->t_rawq, TTIPRI);
1126 		splx(s);
1127 		goto loop;
1128 	}
1129 	splx(s);
1130 
1131 	/*
1132 	 * Input present, perform input mapping
1133 	 * and processing (we're not in raw mode).
1134 	 */
1135 	first = 1;
1136 	while ((c = getc(qp)) >= 0) {
1137 		if (t_flags&CRMOD && c == '\r')
1138 			c = '\n';
1139 		/*
1140 		 * Check for delayed suspend character.
1141 		 */
1142 		if (tp->t_line == NTTYDISC && c == tp->t_dsuspc) {
1143 			gsignal(tp->t_pgrp, SIGTSTP);
1144 			if (first) {
1145 				sleep((caddr_t)&lbolt, TTIPRI);
1146 				goto loop;
1147 			}
1148 			break;
1149 		}
1150 		/*
1151 		 * Interpret EOF only in cooked mode.
1152 		 */
1153 		if (c == tp->t_eofc && (t_flags&CBREAK) == 0)
1154 			break;
1155 		/*
1156 		 * Give user character.
1157 		 */
1158  		error = ureadc(c & 0177, uio);
1159 		if (error)
1160 			break;
1161  		if (uio->uio_resid == 0)
1162 			break;
1163 		/*
1164 		 * In cooked mode check for a "break character"
1165 		 * marking the end of a "line of input".
1166 		 */
1167 		if ((t_flags&CBREAK) == 0 && ttbreakc(c, tp))
1168 			break;
1169 		first = 0;
1170 	}
1171 
1172 checktandem:
1173 	/*
1174 	 * Look to unblock output now that (presumably)
1175 	 * the input queue has gone down.
1176 	 */
1177 	if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5)
1178 		if (putc(tp->t_startc, &tp->t_outq) == 0) {
1179 			tp->t_state &= ~TS_TBLOCK;
1180 			ttstart(tp);
1181 		}
1182 	return (error);
1183 }
1184 
1185 /*
1186  * Called from the device's write routine after it has
1187  * calculated the tty-structure given as argument.
1188  */
1189 ttwrite(tp, uio)
1190 	register struct tty *tp;
1191 	register struct uio *uio;
1192 {
1193 	register char *cp;
1194 	register int cc, ce, c;
1195 	int i, hiwat, cnt, error, s;
1196 	char obuf[OBUFSIZ];
1197 
1198 	hiwat = TTHIWAT(tp);
1199 	cnt = uio->uio_resid;
1200 	error = 0;
1201 loop:
1202 	if ((tp->t_state&TS_CARR_ON) == 0)
1203 		return (EIO);
1204 	/*
1205 	 * Hang the process if it's in the background.
1206 	 */
1207 #define bit(a) (1<<(a-1))
1208 	if (u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
1209 	    (tp->t_flags&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
1210 	    !(u.u_procp->p_sigignore & bit(SIGTTOU)) &&
1211 	    !(u.u_procp->p_sigmask & bit(SIGTTOU))) {
1212 		gsignal(u.u_procp->p_pgrp, SIGTTOU);
1213 		sleep((caddr_t)&lbolt, TTIPRI);
1214 		goto loop;
1215 	}
1216 #undef	bit
1217 
1218 	/*
1219 	 * Process the user's data in at most OBUFSIZ
1220 	 * chunks.  Perform lower case simulation and
1221 	 * similar hacks.  Keep track of high water
1222 	 * mark, sleep on overflow awaiting device aid
1223 	 * in acquiring new space.
1224 	 */
1225 	while (uio->uio_resid > 0) {
1226 		/*
1227 		 * Grab a hunk of data from the user.
1228 		 */
1229 		cc = uio->uio_iov->iov_len;
1230 		if (cc == 0) {
1231 			uio->uio_iovcnt--;
1232 			uio->uio_iov++;
1233 			if (uio->uio_iovcnt <= 0)
1234 				panic("ttwrite");
1235 			continue;
1236 		}
1237 		if (cc > OBUFSIZ)
1238 			cc = OBUFSIZ;
1239 		cp = obuf;
1240 		error = uiomove(cp, cc, UIO_WRITE, uio);
1241 		if (error)
1242 			break;
1243 		if (tp->t_outq.c_cc > hiwat)
1244 			goto ovhiwat;
1245 		if (tp->t_flags&FLUSHO)
1246 			continue;
1247 		/*
1248 		 * If we're mapping lower case or kludging tildes,
1249 		 * then we've got to look at each character, so
1250 		 * just feed the stuff to ttyoutput...
1251 		 */
1252 		if (tp->t_flags & (LCASE|TILDE)) {
1253 			while (cc > 0) {
1254 				c = *cp++;
1255 				tp->t_rocount = 0;
1256 				while ((c = ttyoutput(c, tp)) >= 0) {
1257 					/* out of clists, wait a bit */
1258 					ttstart(tp);
1259 					sleep((caddr_t)&lbolt, TTOPRI);
1260 					tp->t_rocount = 0;
1261 					if (cc != 0) {
1262 						uio->uio_iov->iov_base -= cc;
1263 						uio->uio_iov->iov_len += cc;
1264 						uio->uio_resid += cc;
1265 						uio->uio_offset -= cc;
1266 					}
1267 					goto loop;
1268 				}
1269 				--cc;
1270 				if (tp->t_outq.c_cc > hiwat)
1271 					goto ovhiwat;
1272 			}
1273 			continue;
1274 		}
1275 		/*
1276 		 * If nothing fancy need be done, grab those characters we
1277 		 * can handle without any of ttyoutput's processing and
1278 		 * just transfer them to the output q.  For those chars
1279 		 * which require special processing (as indicated by the
1280 		 * bits in partab), call ttyoutput.  After processing
1281 		 * a hunk of data, look for FLUSHO so ^O's will take effect
1282 		 * immediately.
1283 		 */
1284 		while (cc > 0) {
1285 			if (tp->t_flags & (RAW|LITOUT))
1286 				ce = cc;
1287 			else {
1288 				ce = cc - scanc((unsigned)cc, (caddr_t)cp,
1289 				   (caddr_t)partab, 077);
1290 				/*
1291 				 * If ce is zero, then we're processing
1292 				 * a special character through ttyoutput.
1293 				 */
1294 				if (ce == 0) {
1295 					tp->t_rocount = 0;
1296 					if (ttyoutput(*cp, tp) >= 0) {
1297 					    /* no c-lists, wait a bit */
1298 					    ttstart(tp);
1299 					    sleep((caddr_t)&lbolt, TTOPRI);
1300 					    if (cc != 0) {
1301 					        uio->uio_iov->iov_base -= cc;
1302 					        uio->uio_iov->iov_len += cc;
1303 					        uio->uio_resid += cc;
1304 						uio->uio_offset -= cc;
1305 					    }
1306 					    goto loop;
1307 					}
1308 					cp++, cc--;
1309 					if (tp->t_flags&FLUSHO ||
1310 					    tp->t_outq.c_cc > hiwat)
1311 						goto ovhiwat;
1312 					continue;
1313 				}
1314 			}
1315 			/*
1316 			 * A bunch of normal characters have been found,
1317 			 * transfer them en masse to the output queue and
1318 			 * continue processing at the top of the loop.
1319 			 * If there are any further characters in this
1320 			 * <= OBUFSIZ chunk, the first should be a character
1321 			 * requiring special handling by ttyoutput.
1322 			 */
1323 			tp->t_rocount = 0;
1324 			i = b_to_q(cp, ce, &tp->t_outq);
1325 			ce -= i;
1326 			tp->t_col += ce;
1327 			cp += ce, cc -= ce, tk_nout += ce;
1328 			if (i > 0) {
1329 				/* out of c-lists, wait a bit */
1330 				ttstart(tp);
1331 				sleep((caddr_t)&lbolt, TTOPRI);
1332 				uio->uio_iov->iov_base -= cc;
1333 				uio->uio_iov->iov_len += cc;
1334 				uio->uio_resid += cc;
1335 				uio->uio_offset -= cc;
1336 				goto loop;
1337 			}
1338 			if (tp->t_flags&FLUSHO || tp->t_outq.c_cc > hiwat)
1339 				goto ovhiwat;
1340 		}
1341 	}
1342 	ttstart(tp);
1343 	return (error);
1344 
1345 ovhiwat:
1346 	s = spltty();
1347 	if (cc != 0) {
1348 		uio->uio_iov->iov_base -= cc;
1349 		uio->uio_iov->iov_len += cc;
1350 		uio->uio_resid += cc;
1351 		uio->uio_offset -= cc;
1352 	}
1353 	/*
1354 	 * This can only occur if FLUSHO
1355 	 * is also set in t_flags.
1356 	 */
1357 	if (tp->t_outq.c_cc <= hiwat) {
1358 		splx(s);
1359 		goto loop;
1360 	}
1361 	ttstart(tp);
1362 	if (tp->t_state&TS_NBIO) {
1363 		splx(s);
1364 		if (uio->uio_resid == cnt)
1365 			return (EWOULDBLOCK);
1366 		return (0);
1367 	}
1368 	tp->t_state |= TS_ASLEEP;
1369 	sleep((caddr_t)&tp->t_outq, TTOPRI);
1370 	splx(s);
1371 	goto loop;
1372 }
1373 
1374 /*
1375  * Rubout one character from the rawq of tp
1376  * as cleanly as possible.
1377  */
1378 ttyrub(c, tp)
1379 	register c;
1380 	register struct tty *tp;
1381 {
1382 	register char *cp;
1383 	register int savecol;
1384 	int s;
1385 	char *nextc();
1386 
1387 	if ((tp->t_flags&ECHO) == 0)
1388 		return;
1389 	tp->t_flags &= ~FLUSHO;
1390 	c &= 0377;
1391 	if (tp->t_flags&CRTBS) {
1392 		if (tp->t_rocount == 0) {
1393 			/*
1394 			 * Screwed by ttwrite; retype
1395 			 */
1396 			ttyretype(tp);
1397 			return;
1398 		}
1399 		if (c == ('\t'|0200) || c == ('\n'|0200))
1400 			ttyrubo(tp, 2);
1401 		else switch (partab[c&=0177]&0177) {
1402 
1403 		case ORDINARY:
1404 			if (tp->t_flags&LCASE && c >= 'A' && c <= 'Z')
1405 				ttyrubo(tp, 2);
1406 			else
1407 				ttyrubo(tp, 1);
1408 			break;
1409 
1410 		case VTAB:
1411 		case BACKSPACE:
1412 		case CONTROL:
1413 		case RETURN:
1414 			if (tp->t_flags&CTLECH)
1415 				ttyrubo(tp, 2);
1416 			break;
1417 
1418 		case TAB:
1419 			if (tp->t_rocount < tp->t_rawq.c_cc) {
1420 				ttyretype(tp);
1421 				return;
1422 			}
1423 			s = spltty();
1424 			savecol = tp->t_col;
1425 			tp->t_state |= TS_CNTTB;
1426 			tp->t_flags |= FLUSHO;
1427 			tp->t_col = tp->t_rocol;
1428 			cp = tp->t_rawq.c_cf;
1429 			for (; cp; cp = nextc(&tp->t_rawq, cp))
1430 				ttyecho(*cp, tp);
1431 			tp->t_flags &= ~FLUSHO;
1432 			tp->t_state &= ~TS_CNTTB;
1433 			splx(s);
1434 			/*
1435 			 * savecol will now be length of the tab
1436 			 */
1437 			savecol -= tp->t_col;
1438 			tp->t_col += savecol;
1439 			if (savecol > 8)
1440 				savecol = 8;		/* overflow screw */
1441 			while (--savecol >= 0)
1442 				(void) ttyoutput('\b', tp);
1443 			break;
1444 
1445 		default:
1446 			panic("ttyrub");
1447 		}
1448 	} else if (tp->t_flags&PRTERA) {
1449 		if ((tp->t_state&TS_ERASE) == 0) {
1450 			(void) ttyoutput('\\', tp);
1451 			tp->t_state |= TS_ERASE;
1452 		}
1453 		ttyecho(c, tp);
1454 	} else
1455 		ttyecho(tp->t_erase, tp);
1456 	tp->t_rocount--;
1457 }
1458 
1459 /*
1460  * Crt back over cnt chars perhaps
1461  * erasing them.
1462  */
1463 ttyrubo(tp, cnt)
1464 	register struct tty *tp;
1465 	int cnt;
1466 {
1467 	register char *rubostring = tp->t_flags&CRTERA ? "\b \b" : "\b";
1468 
1469 	while (--cnt >= 0)
1470 		ttyout(rubostring, tp);
1471 }
1472 
1473 /*
1474  * Reprint the rawq line.
1475  * We assume c_cc has already been checked.
1476  */
1477 ttyretype(tp)
1478 	register struct tty *tp;
1479 {
1480 	register char *cp;
1481 	char *nextc();
1482 	int s;
1483 
1484 	if (tp->t_rprntc != 0377)
1485 		ttyecho(tp->t_rprntc, tp);
1486 	(void) ttyoutput('\n', tp);
1487 	s = spltty();
1488 	for (cp = tp->t_canq.c_cf; cp; cp = nextc(&tp->t_canq, cp))
1489 		ttyecho(*cp, tp);
1490 	for (cp = tp->t_rawq.c_cf; cp; cp = nextc(&tp->t_rawq, cp))
1491 		ttyecho(*cp, tp);
1492 	tp->t_state &= ~TS_ERASE;
1493 	splx(s);
1494 	tp->t_rocount = tp->t_rawq.c_cc;
1495 	tp->t_rocol = 0;
1496 }
1497 
1498 /*
1499  * Echo a typed character to the terminal
1500  */
1501 ttyecho(c, tp)
1502 	register c;
1503 	register struct tty *tp;
1504 {
1505 
1506 	if ((tp->t_state&TS_CNTTB) == 0)
1507 		tp->t_flags &= ~FLUSHO;
1508 	if ((tp->t_flags&ECHO) == 0)
1509 		return;
1510 	c &= 0377;
1511 	if (tp->t_flags&RAW) {
1512 		(void) ttyoutput(c, tp);
1513 		return;
1514 	}
1515 	if (c == '\r' && tp->t_flags&CRMOD)
1516 		c = '\n';
1517 	if (tp->t_flags&CTLECH) {
1518 		if ((c&0177) <= 037 && c!='\t' && c!='\n' || (c&0177)==0177) {
1519 			(void) ttyoutput('^', tp);
1520 			c &= 0177;
1521 			if (c == 0177)
1522 				c = '?';
1523 			else if (tp->t_flags&LCASE)
1524 				c += 'a' - 1;
1525 			else
1526 				c += 'A' - 1;
1527 		}
1528 	}
1529 	(void) ttyoutput(c&0177, tp);
1530 }
1531 
1532 /*
1533  * Is c a break char for tp?
1534  */
1535 ttbreakc(c, tp)
1536 	register c;
1537 	register struct tty *tp;
1538 {
1539 	return (c == '\n' || c == tp->t_eofc || c == tp->t_brkc ||
1540 		c == '\r' && (tp->t_flags&CRMOD));
1541 }
1542 
1543 /*
1544  * send string cp to tp
1545  */
1546 ttyout(cp, tp)
1547 	register char *cp;
1548 	register struct tty *tp;
1549 {
1550 	register char c;
1551 
1552 	while (c = *cp++)
1553 		(void) ttyoutput(c, tp);
1554 }
1555 
1556 ttwakeup(tp)
1557 	struct tty *tp;
1558 {
1559 
1560 	if (tp->t_rsel) {
1561 		selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
1562 		tp->t_state &= ~TS_RCOLL;
1563 		tp->t_rsel = 0;
1564 	}
1565 	if (tp->t_state & TS_ASYNC)
1566 		gsignal(tp->t_pgrp, SIGIO);
1567 	wakeup((caddr_t)&tp->t_rawq);
1568 }
1569