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