xref: /original-bsd/sys/kern/tty.c (revision cc3d99f9)
1 /*
2  * Copyright (c) 1982, 1986, 1990 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)tty.c	7.27 (Berkeley) 05/25/90
7  */
8 
9 #include "param.h"
10 #include "systm.h"
11 #include "user.h"
12 #include "ioctl.h"
13 #define TTYDEFCHARS
14 #include "tty.h"
15 #undef TTYDEFCHARS
16 #include "proc.h"
17 #include "file.h"
18 #include "conf.h"
19 #include "dkstat.h"
20 #include "uio.h"
21 #include "kernel.h"
22 #include "vnode.h"
23 #include "syslog.h"
24 
25 #include "machine/reg.h"
26 
27 /* symbolic sleep message strings */
28 char ttyin[] = "ttyin";
29 char ttyout[] = "ttyout";
30 char ttopen[] = "ttyopn";
31 char ttclos[] = "ttycls";
32 char ttybg[] = "ttybg";
33 char ttybuf[] = "ttybuf";
34 
35 /*
36  * Table giving parity for characters and indicating
37  * character classes to tty driver. The 8th bit
38  * indicates parity, the 7th bit indicates the character
39  * is an alphameric or underscore (for ALTWERASE), and the
40  * low 6 bits indicate delay type.  If the low 6 bits are 0
41  * then the character needs no special processing on output.
42  */
43 
44 char partab[] = {
45 	0001,0201,0201,0001,0201,0001,0001,0201,	/* nul - bel */
46 	0202,0004,0003,0201,0005,0206,0201,0001,	/* bs - si */
47 	0201,0001,0001,0201,0001,0201,0201,0001,	/* dle - etb */
48 	0001,0201,0201,0001,0201,0001,0001,0201,	/* can - us */
49 	0200,0000,0000,0200,0000,0200,0200,0000,	/* sp - ' */
50 	0000,0200,0200,0000,0200,0000,0000,0200,	/* ( - / */
51 	0100,0300,0300,0100,0300,0100,0100,0300,	/* 0 - 7 */
52 	0300,0100,0000,0200,0000,0200,0200,0000,	/* 8 - ? */
53 	0200,0100,0100,0300,0100,0300,0300,0100,	/* @ - G */
54 	0100,0300,0300,0100,0300,0100,0100,0300,	/* H - O */
55 	0100,0300,0300,0100,0300,0100,0100,0300,	/* P - W */
56 	0300,0100,0100,0200,0000,0200,0200,0300,	/* X - _ */
57 	0000,0300,0300,0100,0300,0100,0100,0300,	/* ` - g */
58 	0300,0100,0100,0300,0100,0300,0300,0100,	/* h - o */
59 	0300,0100,0100,0300,0100,0300,0300,0100,	/* p - w */
60 	0100,0300,0300,0000,0200,0000,0000,0201,	/* x - del */
61 	/*
62 	 * meta chars
63 	 */
64 	0001,0201,0201,0001,0201,0001,0001,0201,	/* nul - bel */
65 	0202,0004,0003,0201,0005,0206,0201,0001,	/* bs - si */
66 	0201,0001,0001,0201,0001,0201,0201,0001,	/* dle - etb */
67 	0001,0201,0201,0001,0201,0001,0001,0201,	/* can - us */
68 	0200,0000,0000,0200,0000,0200,0200,0000,	/* sp - ' */
69 	0000,0200,0200,0000,0200,0000,0000,0200,	/* ( - / */
70 	0100,0300,0300,0100,0300,0100,0100,0300,	/* 0 - 7 */
71 	0300,0100,0000,0200,0000,0200,0200,0000,	/* 8 - ? */
72 	0200,0100,0100,0300,0100,0300,0300,0100,	/* @ - G */
73 	0100,0300,0300,0100,0300,0100,0100,0300,	/* H - O */
74 	0100,0300,0300,0100,0300,0100,0100,0300,	/* P - W */
75 	0300,0100,0100,0200,0000,0200,0200,0300,	/* X - _ */
76 	0000,0300,0300,0100,0300,0100,0100,0300,	/* ` - g */
77 	0300,0100,0100,0300,0100,0300,0300,0100,	/* h - o */
78 	0300,0100,0100,0300,0100,0300,0300,0100,	/* p - w */
79 	0100,0300,0300,0000,0200,0000,0000,0201,	/* x - del */
80 };
81 
82 extern struct tty *constty;		/* temporary virtual console */
83 extern char partab[], maptab[];
84 
85 /*
86  * Is 'c' a line delimiter ("break" character)?
87  */
88 #define ttbreakc(c) ((c) == '\n' || ((c) == cc[VEOF] || \
89 	(c) == cc[VEOL] || (c) == cc[VEOL2]) && (c) != _POSIX_VDISABLE)
90 
91 ttychars(tp)
92 	struct tty *tp;
93 {
94 	bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
95 }
96 
97 /*
98  * Wait for output to drain, then flush input waiting.
99  */
100 ttywflush(tp)
101 	struct tty *tp;
102 {
103 	int error;
104 
105 	if ((error = ttywait(tp)) == 0)
106 		ttyflush(tp, FREAD);
107 	return (error);
108 }
109 
110 /*
111  * Wait for output to drain.
112  */
113 ttywait(tp)
114 	register struct tty *tp;
115 {
116 	int error = 0, s = spltty();
117 
118 	while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
119 	    (tp->t_state&TS_CARR_ON || tp->t_cflag&CLOCAL) &&
120 	    tp->t_oproc) {
121 		(*tp->t_oproc)(tp);
122 		tp->t_state |= TS_ASLEEP;
123 		if (error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH,
124 		    ttyout, 0))
125 			break;
126 	}
127 	splx(s);
128 	return (error);
129 }
130 
131 /*
132  * Flush all TTY queues
133  */
134 ttyflush(tp, rw)
135 	register struct tty *tp;
136 {
137 	register s;
138 
139 	s = spltty();
140 	if (rw & FREAD) {
141 		while (getc(&tp->t_canq) >= 0)
142 			;
143 		ttwakeup(tp);
144 	}
145 	if (rw & FWRITE) {
146 		wakeup((caddr_t)&tp->t_outq); /* XXX? what about selwakeup? */
147 		tp->t_state &= ~TS_TTSTOP;
148 		(*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
149 		while (getc(&tp->t_outq) >= 0)
150 			;
151 	}
152 	if (rw & FREAD) {
153 		while (getc(&tp->t_rawq) >= 0)
154 			;
155 		tp->t_rocount = 0;
156 		tp->t_rocol = 0;
157 		tp->t_state &= ~TS_LOCAL;
158 	}
159 	splx(s);
160 }
161 
162 /*
163  * Send stop character on input overflow.
164  */
165 ttyblock(tp)
166 	register struct tty *tp;
167 {
168 	register x;
169 
170 	x = tp->t_rawq.c_cc + tp->t_canq.c_cc;
171 	if (tp->t_rawq.c_cc > TTYHOG) {
172 		ttyflush(tp, FREAD|FWRITE);
173 		tp->t_state &= ~TS_TBLOCK;
174 	}
175 	/*
176 	 * Block further input iff:
177 	 * Current input > threshold AND input is available to user program
178 	 */
179 	if (x >= TTYHOG/2 && (tp->t_state & TS_TBLOCK) == 0 &&
180 	    ((tp->t_lflag&ICANON) == 0) || (tp->t_canq.c_cc > 0) &&
181 	    tp->t_cc[VSTOP] != _POSIX_VDISABLE) {
182 		if (putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
183 			tp->t_state |= TS_TBLOCK;
184 			ttstart(tp);
185 		}
186 	}
187 }
188 
189 /*
190  * Restart typewriter output following a delay
191  * timeout.
192  * The name of the routine is passed to the timeout
193  * subroutine and it is called during a clock interrupt.
194  */
195 ttrstrt(tp)
196 	struct tty *tp;
197 {
198 
199 #ifdef DIAGNOSTIC
200 	if (tp == 0)
201 		panic("ttrstrt");
202 #endif
203 	tp->t_state &= ~TS_TIMEOUT;
204 	ttstart(tp);
205 }
206 
207 /*
208  * Start output on the typewriter. It is used from the top half
209  * after some characters have been put on the output queue,
210  * from the interrupt routine to transmit the next
211  * character, and after a timeout has finished.
212  */
213 ttstart(tp)
214 	struct tty *tp;
215 {
216 
217 	if (tp->t_oproc)		/* kludge for pty */
218 		(*tp->t_oproc)(tp);
219 }
220 
221 /*
222  * Common code for tty ioctls.
223  */
224 /*ARGSUSED*/
225 ttioctl(tp, com, data, flag)
226 	register struct tty *tp;
227 	caddr_t data;
228 {
229 	extern int nldisp;
230 	int s, error;
231 
232 	/*
233 	 * If the ioctl involves modification,
234 	 * hang if in the background.
235 	 */
236 	switch (com) {
237 
238 	case TIOCSETD:
239 	case TIOCFLUSH:
240 	/*case TIOCSPGRP:*/
241 	case TIOCSTI:
242 	case TIOCSWINSZ:
243 	case TIOCSETA:
244 	case TIOCSETAW:
245 	case TIOCSETAF:
246 /**** these get removed ****
247 	case TIOCSETAS:
248 	case TIOCSETAWS:
249 	case TIOCSETAFS:
250 /***************************/
251 #ifdef COMPAT_43
252 	case TIOCSETP:
253 	case TIOCSETN:
254 	case TIOCSETC:
255 	case TIOCSLTC:
256 	case TIOCLBIS:
257 	case TIOCLBIC:
258 	case TIOCLSET:
259 	case OTIOCSETD:
260 #endif
261 		while (isbackground(u.u_procp, tp) &&
262 		   u.u_procp->p_pgrp->pg_jobc &&
263 		   (u.u_procp->p_flag&SVFORK) == 0 &&
264 		   (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 &&
265 		   (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0) {
266 			pgsignal(u.u_procp->p_pgrp, SIGTTOU);
267 			if (error = tsleep((caddr_t)&lbolt, TTOPRI | PCATCH,
268 			    ttybg, 0))
269 				return (error);
270 		}
271 		break;
272 	}
273 
274 	/*
275 	 * Process the ioctl.
276 	 */
277 	switch (com) {
278 
279 	/* get discipline number */
280 	case TIOCGETD:
281 		*(int *)data = tp->t_line;
282 		break;
283 
284 	/* set line discipline */
285 	case TIOCSETD: {
286 		register int t = *(int *)data;
287 		dev_t dev = tp->t_dev;
288 
289 		if ((unsigned)t >= nldisp)
290 			return (ENXIO);
291 		if (t != tp->t_line) {
292 			s = spltty();
293 			(*linesw[tp->t_line].l_close)(tp);
294 			error = (*linesw[t].l_open)(dev, tp);
295 			if (error) {
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 		}
303 		break;
304 	}
305 
306 	/* prevent more opens on channel */
307 	case TIOCEXCL:
308 		tp->t_state |= TS_XCLUDE;
309 		break;
310 
311 	case TIOCNXCL:
312 		tp->t_state &= ~TS_XCLUDE;
313 		break;
314 
315 	case TIOCHPCL:
316 		tp->t_cflag |= HUPCL;
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 	case FIOASYNC:
331 		if (*(int *)data)
332 			tp->t_state |= TS_ASYNC;
333 		else
334 			tp->t_state &= ~TS_ASYNC;
335 		break;
336 
337 	case FIONBIO:
338 		break;	/* XXX remove */
339 
340 	/* return number of characters immediately available */
341 	case FIONREAD:
342 		*(off_t *)data = ttnread(tp);
343 		break;
344 
345 	case TIOCOUTQ:
346 		*(int *)data = tp->t_outq.c_cc;
347 		break;
348 
349 	case TIOCSTOP:
350 		s = spltty();
351 		if ((tp->t_state&TS_TTSTOP) == 0) {
352 			tp->t_state |= TS_TTSTOP;
353 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
354 		}
355 		splx(s);
356 		break;
357 
358 	case TIOCSTART:
359 		s = spltty();
360 		if ((tp->t_state&TS_TTSTOP) || (tp->t_lflag&FLUSHO)) {
361 			tp->t_state &= ~TS_TTSTOP;
362 			tp->t_lflag &= ~FLUSHO;
363 			ttstart(tp);
364 		}
365 		splx(s);
366 		break;
367 
368 	/*
369 	 * Simulate typing of a character at the terminal.
370 	 */
371 	case TIOCSTI:
372 		if (u.u_uid && (flag & FREAD) == 0)
373 			return (EPERM);
374 		if (u.u_uid && !isctty(u.u_procp, tp))
375 			return (EACCES);
376 		(*linesw[tp->t_line].l_rint)(*(char *)data, tp);
377 		break;
378 
379 	case TIOCGETA: {
380 		struct termios *t = (struct termios *)data;
381 
382 		bcopy(&tp->t_termios, t, sizeof(struct termios));
383 		break;
384 	}
385 
386 	/*** THIS ALL GETS REMOVED ***/
387 	case JUNK_TIOCSETAS:
388 	case JUNK_TIOCSETAWS:
389 	case JUNK_TIOCSETAFS:
390 		((struct termios *)data)->c_cflag |= CIGNORE;
391 		switch(com) {
392 		case JUNK_TIOCSETAS:
393 			com = TIOCSETA;
394 			break;
395 		case JUNK_TIOCSETAWS:
396 			com = TIOCSETAW;
397 			break;
398 		case JUNK_TIOCSETAFS:
399 			com = TIOCSETAF;
400 			break;
401 		}
402 	/*******************************/
403 		/*FALLTHROGH*/
404 	case TIOCSETA:
405 	case TIOCSETAW:
406 	case TIOCSETAF: {
407 		register struct termios *t = (struct termios *)data;
408 
409 		s = spltty();
410 		if (com == TIOCSETAW || com == TIOCSETAF) {
411 			if (error = ttywait(tp)) {
412 				splx(s);
413 				return (error);
414 			}
415 			if (com == TIOCSETAF);
416 				ttyflush(tp, FREAD);
417 		}
418 		if ((t->c_cflag&CIGNORE) == 0) {
419 			/*
420 			 * set device hardware
421 			 */
422 			if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
423 				splx(s);
424 				return (error);
425 			} else {
426 				if ((tp->t_state&TS_CARR_ON) == 0 &&
427 				    (tp->t_cflag&CLOCAL) &&
428 				    (t->c_cflag&CLOCAL) == 0) {
429 					tp->t_state &= ~TS_ISOPEN;
430 					tp->t_state |= TS_WOPEN;
431 					ttwakeup(tp);
432 				}
433 				tp->t_cflag = t->c_cflag;
434 				tp->t_ispeed = t->c_ispeed;
435 				tp->t_ospeed = t->c_ospeed;
436 			}
437 			ttsetwater(tp);
438 		}
439 		if (com != TIOCSETAF) {
440 			if ((t->c_lflag&ICANON) != (tp->t_lflag&ICANON))
441 				if (t->c_lflag&ICANON) {
442 					tp->t_lflag |= PENDIN;
443 					ttwakeup(tp);
444 				}
445 				else {
446 					struct clist tq;
447 
448 					catq(&tp->t_rawq, &tp->t_canq);
449 					tq = tp->t_rawq;
450 					tp->t_rawq = tp->t_canq;
451 					tp->t_canq = tq;
452 				}
453 		}
454 		tp->t_iflag = t->c_iflag;
455 		tp->t_oflag = t->c_oflag;
456 		tp->t_lflag = t->c_lflag;
457 		bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
458 		splx(s);
459 		break;
460 	}
461 
462 	/*
463 	 * Set controlling terminal.
464 	 * Session ctty vnode pointer set in vnode layer.
465 	 */
466 	case TIOCSCTTY: {
467 		register struct proc *p = u.u_procp;
468 
469 		if (!SESS_LEADER(p) ||
470 		   (p->p_session->s_ttyvp || tp->t_session) &&
471 		   (tp->t_session != p->p_session))
472 			return (EPERM);
473 		tp->t_session = p->p_session;
474 		tp->t_pgrp = p->p_pgrp;
475 		p->p_session->s_ttyp = tp;
476 		p->p_flag |= SCTTY;
477 		break;
478 	}
479 
480 	/*
481 	 * Set terminal process group.
482 	 */
483 	case TIOCSPGRP: {
484 		register struct proc *p = u.u_procp;
485 		register struct pgrp *pgrp = pgfind(*(int *)data);
486 
487 		if (!isctty(p, tp))
488 			return (ENOTTY);
489 		else if (pgrp == NULL || pgrp->pg_session != p->p_session)
490 			return (EPERM);
491 		tp->t_pgrp = pgrp;
492 		break;
493 	}
494 
495 	case TIOCGPGRP:
496 		if (!isctty(u.u_procp, tp))
497 			return (ENOTTY);
498 		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
499 		break;
500 
501 	case TIOCSWINSZ:
502 		if (bcmp((caddr_t)&tp->t_winsize, data,
503 		    sizeof (struct winsize))) {
504 			tp->t_winsize = *(struct winsize *)data;
505 			pgsignal(tp->t_pgrp, SIGWINCH);
506 		}
507 		break;
508 
509 	case TIOCGWINSZ:
510 		*(struct winsize *)data = tp->t_winsize;
511 		break;
512 
513 	case TIOCCONS:
514 		if (*(int *)data) {
515 			if (constty && constty != tp &&
516 			    (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) ==
517 			    (TS_CARR_ON|TS_ISOPEN))
518 				return (EBUSY);
519 #ifndef	UCONSOLE
520 			if (error = suser(u.u_cred, &u.u_acflag))
521 				return (error);
522 #endif
523 			constty = tp;
524 		} else if (tp == constty)
525 			constty = NULL;
526 		break;
527 
528 #ifdef COMPAT_43
529 	case TIOCGETP:
530 	case TIOCSETP:
531 	case TIOCSETN:
532 	case TIOCGETC:
533 	case TIOCSETC:
534 	case TIOCSLTC:
535 	case TIOCGLTC:
536 	case TIOCLBIS:
537 	case TIOCLBIC:
538 	case TIOCLSET:
539 	case TIOCLGET:
540 	case OTIOCGETD:
541 	case OTIOCSETD:
542 		return(ttcompat(tp, com, data, flag));
543 #endif
544 
545 	default:
546 		return (-1);
547 	}
548 	return (0);
549 }
550 
551 ttnread(tp)
552 	struct tty *tp;
553 {
554 	int nread = 0;
555 
556 	if (tp->t_lflag & PENDIN)
557 		ttypend(tp);
558 	nread = tp->t_canq.c_cc;
559 	if ((tp->t_lflag & ICANON) == 0)
560 		nread += tp->t_rawq.c_cc;
561 	return (nread);
562 }
563 
564 ttselect(dev, rw)
565 	dev_t dev;
566 	int rw;
567 {
568 	register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
569 	int nread;
570 	int s = spltty();
571 
572 	switch (rw) {
573 
574 	case FREAD:
575 		nread = ttnread(tp);
576 		if (nread > 0 ||
577 		   ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
578 			goto win;
579 		if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
580 			tp->t_state |= TS_RCOLL;
581 		else
582 			tp->t_rsel = u.u_procp;
583 		break;
584 
585 	case FWRITE:
586 		if (tp->t_outq.c_cc <= tp->t_lowat)
587 			goto win;
588 		if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
589 			tp->t_state |= TS_WCOLL;
590 		else
591 			tp->t_wsel = u.u_procp;
592 		break;
593 	}
594 	splx(s);
595 	return (0);
596 win:
597 	splx(s);
598 	return (1);
599 }
600 
601 /*
602  * Initial open of tty, or (re)entry to line discipline.
603  */
604 ttyopen(dev, tp)
605 	dev_t dev;
606 	register struct tty *tp;
607 {
608 
609 	tp->t_dev = dev;
610 
611 	tp->t_state &= ~TS_WOPEN;
612 	if ((tp->t_state & TS_ISOPEN) == 0) {
613 		tp->t_state |= TS_ISOPEN;
614 		bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize));
615 	}
616 	return (0);
617 }
618 
619 /*
620  * "close" a line discipline
621  */
622 ttylclose(tp)
623 	register struct tty *tp;
624 {
625 
626 	ttywflush(tp);
627 }
628 
629 /*
630  * clean tp on last close
631  */
632 ttyclose(tp)
633 	register struct tty *tp;
634 {
635 	if (constty == tp)
636 		constty = NULL;
637 	ttyflush(tp, FREAD|FWRITE);
638 	tp->t_session = NULL;
639 	tp->t_pgrp = NULL;
640 	tp->t_state = 0;
641 	return (0);
642 }
643 
644 /*
645  * Handle modem control transition on a tty.
646  * Flag indicates new state of carrier.
647  * Returns 0 if the line should be turned off, otherwise 1.
648  */
649 ttymodem(tp, flag)
650 	register struct tty *tp;
651 {
652 
653 	if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag&MDMBUF)) {
654 		/*
655 		 * MDMBUF: do flow control according to carrier flag
656 		 */
657 		if (flag) {
658 			tp->t_state &= ~TS_TTSTOP;
659 			ttstart(tp);
660 		} else if ((tp->t_state&TS_TTSTOP) == 0) {
661 			tp->t_state |= TS_TTSTOP;
662 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
663 		}
664 	} else if (flag == 0) {
665 		/*
666 		 * Lost carrier.
667 		 */
668 		tp->t_state &= ~TS_CARR_ON;
669 		if (tp->t_state&TS_ISOPEN && (tp->t_cflag&CLOCAL) == 0) {
670 			if (tp->t_session && tp->t_session->s_leader)
671 				psignal(tp->t_session->s_leader, SIGHUP);
672 			ttyflush(tp, FREAD|FWRITE);
673 			return (0);
674 		}
675 	} else {
676 		/*
677 		 * Carrier now on.
678 		 */
679 		tp->t_state |= TS_CARR_ON;
680 		ttwakeup(tp);
681 	}
682 	return (1);
683 }
684 
685 /*
686  * Default modem control routine (for other line disciplines).
687  * Return argument flag, to turn off device on carrier drop.
688  */
689 nullmodem(tp, flag)
690 	register struct tty *tp;
691 	int flag;
692 {
693 
694 	if (flag)
695 		tp->t_state |= TS_CARR_ON;
696 	else {
697 		tp->t_state &= ~TS_CARR_ON;
698 		if ((tp->t_cflag&CLOCAL) == 0) {
699 			if (tp->t_session && tp->t_session->s_leader)
700 				psignal(tp->t_session->s_leader, SIGHUP);
701 			return (0);
702 		}
703 	}
704 	return (1);
705 }
706 
707 /*
708  * reinput pending characters after state switch
709  * call at spltty().
710  */
711 ttypend(tp)
712 	register struct tty *tp;
713 {
714 	struct clist tq;
715 	register c;
716 
717 	tp->t_lflag &= ~PENDIN;
718 	tp->t_state |= TS_TYPEN;
719 	tq = tp->t_rawq;
720 	tp->t_rawq.c_cc = 0;
721 	tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
722 	while ((c = getc(&tq)) >= 0)
723 		ttyinput(c, tp);
724 	tp->t_state &= ~TS_TYPEN;
725 }
726 
727 /*
728  *
729  * Place a character on raw TTY input queue,
730  * putting in delimiters and waking up top
731  * half as needed.  Also echo if required.
732  * The arguments are the character and the
733  * appropriate tty structure.
734  */
735 ttyinput(c, tp)
736 	register c;
737 	register struct tty *tp;
738 {
739 	register int iflag = tp->t_iflag;
740 	register int lflag = tp->t_lflag;
741 	register u_char *cc = tp->t_cc;
742 	int i, err;
743 
744 	/*
745 	 * If input is pending take it first.
746 	 */
747 	if (lflag&PENDIN)
748 		ttypend(tp);
749 	/*
750 	 * Gather stats.
751 	 */
752 	tk_nin++;
753 	if (lflag&ICANON) {
754 		tk_cancc++;
755 		tp->t_cancc++;
756 	} else {
757 		tk_rawcc++;
758 		tp->t_rawcc++;
759 	}
760 	/*
761 	 * Handle exceptional conditions (break, parity, framing).
762 	 */
763 	if (err = (c&TTY_ERRORMASK)) {
764 		c &= ~TTY_ERRORMASK;
765 		if (err&TTY_FE && !c) {		/* break */
766 			if (iflag&IGNBRK)
767 				goto endcase;
768 			else if (iflag&BRKINT && lflag&ISIG &&
769 				(cc[VINTR] != _POSIX_VDISABLE))
770 				c = cc[VINTR];
771 			else {
772 				c = 0;
773 				if (iflag&PARMRK)
774 					goto parmrk;
775 			}
776 		} else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) {
777 			if (iflag&IGNPAR)
778 				goto endcase;
779 			else if (iflag&PARMRK) {
780 parmrk:
781 				putc(0377|TTY_QUOTE, &tp->t_rawq);
782 				putc(0|TTY_QUOTE, &tp->t_rawq);
783 				putc(c|TTY_QUOTE, &tp->t_rawq);
784 				goto endcase;
785 			} else
786 				c = 0;
787 		}
788 	}
789 	/*
790 	 * In tandem mode, check high water mark.
791 	 */
792 	if (iflag&IXOFF)
793 		ttyblock(tp);
794 	if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
795 		c &= 0177;
796 	/*
797 	 * Check for literal nexting very first
798 	 */
799 	if (tp->t_state&TS_LNCH) {
800 		c |= TTY_QUOTE;
801 		tp->t_state &= ~TS_LNCH;
802 	}
803 	/*
804 	 * Scan for special characters.  This code
805 	 * is really just a big case statement with
806 	 * non-constant cases.  The bottom of the
807 	 * case statement is labeled ``endcase'', so goto
808 	 * it after a case match, or similar.
809 	 */
810 	/*
811 	 * Control chars which aren't controlled
812 	 * by ICANON, ISIG, or IXON.
813 	 */
814 	if (lflag&IEXTEN) {
815 		if (CCEQ(cc[VLNEXT], c)) {
816 			if (lflag&ECHO) {
817 				if (lflag&ECHOE)
818 					ttyoutstr("^\b", tp);
819 				else
820 					ttyecho(c, tp);
821 			}
822 			tp->t_state |= TS_LNCH;
823 			goto endcase;
824 		}
825 		if (CCEQ(cc[VFLUSHO], c)) {
826 			if (lflag&FLUSHO)
827 				tp->t_lflag &= ~FLUSHO;
828 			else {
829 				ttyflush(tp, FWRITE);
830 				ttyecho(c, tp);
831 				if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
832 					ttyretype(tp);
833 				tp->t_lflag |= FLUSHO;
834 			}
835 			goto startoutput;
836 		}
837 	}
838 	/*
839 	 * Signals.
840 	 */
841 	if (lflag&ISIG) {
842 		if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
843 			if ((lflag&NOFLSH) == 0)
844 				ttyflush(tp, FREAD|FWRITE);
845 			ttyecho(c, tp);
846 			pgsignal(tp->t_pgrp,
847 			    CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT);
848 			goto endcase;
849 		}
850 		if (CCEQ(cc[VSUSP], c)) {
851 			if ((lflag&NOFLSH) == 0)
852 				ttyflush(tp, FREAD);
853 			ttyecho(c, tp);
854 			pgsignal(tp->t_pgrp, SIGTSTP);
855 			goto endcase;
856 		}
857 	}
858 	/*
859 	 * Handle start/stop characters.
860 	 */
861 	if (iflag&IXON) {
862 		if (CCEQ(cc[VSTOP], c)) {
863 			if ((tp->t_state&TS_TTSTOP) == 0) {
864 				tp->t_state |= TS_TTSTOP;
865 				(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
866 				return;
867 			}
868 			if (!CCEQ(cc[VSTART], c))
869 				return;
870 			/*
871 			 * if VSTART == VSTOP then toggle
872 			 */
873 			goto endcase;
874 		}
875 		if (CCEQ(cc[VSTART], c))
876 			goto restartoutput;
877 	}
878 	/*
879 	 * IGNCR, ICRNL, & INLCR
880 	 */
881 	if (c == '\r') {
882 		if (iflag&IGNCR)
883 			goto endcase;
884 		else if (iflag&ICRNL)
885 			c = '\n';
886 	}
887 	else if (c == '\n' && iflag&INLCR)
888 		c = '\r';
889 	/*
890 	 * Non canonical mode; don't process line editing
891 	 * characters; check high water mark for wakeup.
892 	 *
893 	 */
894 	if ((lflag&ICANON) == 0) {
895 		if (tp->t_rawq.c_cc > TTYHOG) {
896 			if (iflag&IMAXBEL) {
897 				if (tp->t_outq.c_cc < tp->t_hiwat)
898 					(void) ttyoutput(CTRL('g'), tp);
899 			} else
900 				ttyflush(tp, FREAD | FWRITE);
901 		} else {
902 			if (putc(c, &tp->t_rawq) >= 0) {
903 				ttwakeup(tp);
904 				ttyecho(c, tp);
905 			}
906 		}
907 		goto endcase;
908 	}
909 	/*
910 	 * From here on down canonical mode character
911 	 * processing takes place.
912 	 */
913 	/*
914 	 * erase (^H / ^?)
915 	 */
916 	if (CCEQ(cc[VERASE], c)) {
917 		if (tp->t_rawq.c_cc)
918 			ttyrub(unputc(&tp->t_rawq), tp);
919 		goto endcase;
920 	}
921 	/*
922 	 * kill (^U)
923 	 */
924 	if (CCEQ(cc[VKILL], c)) {
925 		if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount &&
926 		    (lflag&ECHOPRT) == 0) {
927 			while (tp->t_rawq.c_cc)
928 				ttyrub(unputc(&tp->t_rawq), tp);
929 		} else {
930 			ttyecho(c, tp);
931 			if (lflag&ECHOK || lflag&ECHOKE)
932 				ttyecho('\n', tp);
933 			while (getc(&tp->t_rawq) > 0)
934 				;
935 			tp->t_rocount = 0;
936 		}
937 		tp->t_state &= ~TS_LOCAL;
938 		goto endcase;
939 	}
940 	/*
941 	 * word erase (^W)
942 	 */
943 	if (CCEQ(cc[VWERASE], c)) {
944 		int ctype;
945 
946 #define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0)
947 		/*
948 		 * erase whitespace
949 		 */
950 		while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
951 			ttyrub(c, tp);
952 		if (c == -1)
953 			goto endcase;
954 		/*
955 		 * special case last char of token
956 		 */
957 		ttyrub(c, tp);
958 		c = unputc(&tp->t_rawq);
959 		if (c == -1 || c == ' ' || c == '\t') {
960 			if (c != -1)
961 				(void) putc(c, &tp->t_rawq);
962 			goto endcase;
963 		}
964 		/*
965 		 * erase rest of token
966 		 */
967 		ctype = CTYPE(c);
968 		do {
969 			ttyrub(c, tp);
970 			c = unputc(&tp->t_rawq);
971 			if (c == -1)
972 				goto endcase;
973 		} while (c != ' ' && c != '\t' && CTYPE(c) == ctype);
974 		(void) putc(c, &tp->t_rawq);
975 		goto endcase;
976 #undef CTYPE
977 	}
978 	/*
979 	 * reprint line (^R)
980 	 */
981 	if (CCEQ(cc[VREPRINT], c)) {
982 		ttyretype(tp);
983 		goto endcase;
984 	}
985 	if (CCEQ(cc[VINFO], c)) {
986 		pgsignal(tp->t_pgrp, SIGINFO);
987 		if ((lflag&NOKERNINFO) == 0)
988 			ttyinfo(tp);
989 		goto endcase;
990 	}
991 	/*
992 	 * Check for input buffer overflow
993 	 */
994 	if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) {
995 		if (iflag&IMAXBEL) {
996 			if (tp->t_outq.c_cc < tp->t_hiwat)
997 				(void) ttyoutput(CTRL('g'), tp);
998 		} else
999 			ttyflush(tp, FREAD | FWRITE);
1000 		goto endcase;
1001 	}
1002 	/*
1003 	 * Put data char in q for user and
1004 	 * wakeup on seeing a line delimiter.
1005 	 */
1006 	if (putc(c, &tp->t_rawq) >= 0) {
1007 		if (ttbreakc(c)) {
1008 			tp->t_rocount = 0;
1009 			catq(&tp->t_rawq, &tp->t_canq);
1010 			ttwakeup(tp);
1011 		} else if (tp->t_rocount++ == 0)
1012 			tp->t_rocol = tp->t_col;
1013 		if (tp->t_state&TS_ERASE) {
1014 			/*
1015 			 * end of prterase \.../
1016 			 */
1017 			tp->t_state &= ~TS_ERASE;
1018 			(void) ttyoutput('/', tp);
1019 		}
1020 		i = tp->t_col;
1021 		ttyecho(c, tp);
1022 		if (CCEQ(cc[VEOF], c) && lflag&ECHO) {
1023 			/*
1024 			 * Place the cursor over the '^' of the ^D.
1025 			 */
1026 			i = MIN(2, tp->t_col - i);
1027 			while (i > 0) {
1028 				(void) ttyoutput('\b', tp);
1029 				i--;
1030 			}
1031 		}
1032 	}
1033 endcase:
1034 	/*
1035 	 * IXANY means allow any character to restart output.
1036 	 */
1037 	if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 &&
1038 	    cc[VSTART] != cc[VSTOP])
1039 		return;
1040 restartoutput:
1041 	tp->t_state &= ~TS_TTSTOP;
1042 	tp->t_lflag &= ~FLUSHO;
1043 startoutput:
1044 	ttstart(tp);
1045 }
1046 
1047 /*
1048  * Put character on TTY output queue, adding delays,
1049  * expanding tabs, and handling the CR/NL bit.
1050  * This is called both from the top half for output,
1051  * and from interrupt level for echoing.
1052  * The arguments are the character and the tty structure.
1053  * Returns < 0 if putc succeeds, otherwise returns char to resend
1054  * Must be recursive.
1055  */
1056 ttyoutput(c, tp)
1057 	register c;
1058 	register struct tty *tp;
1059 {
1060 	register char *colp;
1061 	register ctype;
1062 	register long oflag = tp->t_oflag;
1063 
1064 	if ((oflag&OPOST) == 0) {
1065 		if (tp->t_lflag&FLUSHO)
1066 			return (-1);
1067 		if (putc(c, &tp->t_outq))
1068 			return (c);
1069 		tk_nout++;
1070 		tp->t_outcc++;
1071 		return (-1);
1072 	}
1073 	c &= TTY_CHARMASK;
1074 	/*
1075 	 * Turn tabs to spaces as required
1076 	 */
1077 	if (c == '\t' && oflag&OXTABS ) {
1078 		register int s;
1079 
1080 		c = 8 - (tp->t_col&7);
1081 		if ((tp->t_lflag&FLUSHO) == 0) {
1082 			s = spltty();		/* don't interrupt tabs */
1083 			c -= b_to_q("        ", c, &tp->t_outq);
1084 			tk_nout += c;
1085 			tp->t_outcc += c;
1086 			splx(s);
1087 		}
1088 		tp->t_col += c;
1089 		return (c ? -1 : '\t');
1090 	}
1091 	if (c == CEOT && oflag&ONOEOT)
1092 		return(-1);
1093 	tk_nout++;
1094 	tp->t_outcc++;
1095 	/*
1096 	 * turn <nl> to <cr><lf> if desired.
1097 	 */
1098 	if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0)
1099 		return (c);
1100 	if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
1101 		return (c);
1102 	/*
1103 	 * Calculate delays.
1104 	 * The numbers here represent clock ticks
1105 	 * and are not necessarily optimal for all terminals.
1106 	 *
1107 	 * SHOULD JUST ALLOW USER TO SPECIFY DELAYS
1108 	 *
1109 	 * (actually, should THROW AWAY terminals which need delays)
1110 	 */
1111 	colp = &tp->t_col;
1112 	ctype = partab[c];
1113 	c = 0;
1114 	switch (ctype&077) {
1115 
1116 	case ORDINARY:
1117 		(*colp)++;
1118 
1119 	case CONTROL:
1120 		break;
1121 
1122 	case BACKSPACE:
1123 		if (*colp)
1124 			(*colp)--;
1125 		break;
1126 
1127 	/*
1128 	 * This macro is close enough to the correct thing;
1129 	 * it should be replaced by real user settable delays
1130 	 * in any event...
1131 	 */
1132 #define	mstohz(ms)	(((ms) * hz) >> 10)
1133 	case NEWLINE:
1134 		ctype = (tp->t_flags >> 8) & 03;
1135 		if (ctype == 1) { /* tty 37 */
1136 			if (*colp > 0) {
1137 				c = (((unsigned)*colp) >> 4) + 3;
1138 				if ((unsigned)c > 6)
1139 					c = 6;
1140 			}
1141 		} else if (ctype == 2) /* vt05 */
1142 			c = mstohz(100);
1143 		*colp = 0;
1144 		break;
1145 
1146 	case TAB:
1147 		ctype = (tp->t_flags >> 10) & 03;
1148 		if (ctype == 1) { /* tty 37 */
1149 			c = 1 - (*colp | ~07);
1150 			if (c < 5)
1151 				c = 0;
1152 		}
1153 		*colp |= 07;
1154 		(*colp)++;
1155 		break;
1156 
1157 	case VTAB:
1158 		if (tp->t_flags&VTDELAY) /* tty 37 */
1159 			c = 0177;
1160 		break;
1161 
1162 	case RETURN:
1163 		ctype = (tp->t_flags >> 12) & 03;
1164 		if (ctype == 1) /* tn 300 */
1165 			c = mstohz(83);
1166 		else if (ctype == 2) /* ti 700 */
1167 			c = mstohz(166);
1168 		else if (ctype == 3) { /* concept 100 */
1169 			int i;
1170 
1171 			if ((i = *colp) >= 0)
1172 				for (; i < 9; i++)
1173 					(void) putc(0177, &tp->t_outq);
1174 		}
1175 		*colp = 0;
1176 	}
1177 	if (c && (tp->t_lflag&FLUSHO) == 0)
1178 		(void) putc(c|TTY_QUOTE, &tp->t_outq);
1179 	return (-1);
1180 }
1181 #undef mstohz
1182 
1183 /*
1184  * Called from device's read routine after it has
1185  * calculated the tty-structure given as argument.
1186  */
1187 ttread(tp, uio, flag)
1188 	register struct tty *tp;
1189 	struct uio *uio;
1190 {
1191 	register struct clist *qp;
1192 	register int c;
1193 	register long lflag;
1194 	register u_char *cc = tp->t_cc;
1195 	int s, first, error = 0;
1196 
1197 loop:
1198 	lflag = tp->t_lflag;
1199 	s = spltty();
1200 	/*
1201 	 * take pending input first
1202 	 */
1203 	if (lflag&PENDIN)
1204 		ttypend(tp);
1205 	splx(s);
1206 
1207 	/*
1208 	 * Hang process if it's in the background.
1209 	 */
1210 	if (isbackground(u.u_procp, tp)) {
1211 		if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) ||
1212 		   (u.u_procp->p_sigmask & sigmask(SIGTTIN)) ||
1213 		    u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0)
1214 			return (EIO);
1215 		pgsignal(u.u_procp->p_pgrp, SIGTTIN);
1216 		if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0))
1217 			return (error);
1218 		goto loop;
1219 	}
1220 
1221 	/*
1222 	 * If canonical, use the canonical queue,
1223 	 * else use the raw queue.
1224 	 *
1225 	 * XXX - should get rid of canonical queue.
1226 	 * (actually, should get rid of clists...)
1227 	 */
1228 	qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq;
1229 
1230 	/*
1231 	 * If there is no input, sleep on rawq
1232 	 * awaiting hardware receipt and notification.
1233 	 * If we have data, we don't need to check for carrier.
1234 	 */
1235 	s = spltty();
1236 	if (qp->c_cc <= 0) {
1237 		int carrier;
1238 
1239 		carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL);
1240 		if (!carrier && tp->t_state&TS_ISOPEN) {
1241 			splx(s);
1242 			return (0);	/* EOF */
1243 		}
1244 		if (flag & IO_NDELAY) {
1245 			splx(s);
1246 			return (EWOULDBLOCK);
1247 		}
1248 		error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
1249 		    carrier ? ttyin : ttopen, 0);
1250 		splx(s);
1251 		if (error)
1252 			return (error);
1253 		goto loop;
1254 	}
1255 	splx(s);
1256 
1257 	/*
1258 	 * Input present, check for input mapping and processing.
1259 	 */
1260 	first = 1;
1261 	while ((c = getc(qp)) >= 0) {
1262 		/*
1263 		 * delayed suspend (^Y)
1264 		 */
1265 		if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) {
1266 			pgsignal(tp->t_pgrp, SIGTSTP);
1267 			if (first) {
1268 				if (error = tsleep((caddr_t)&lbolt,
1269 				    TTIPRI | PCATCH, ttybg, 0))
1270 					break;
1271 				goto loop;
1272 			}
1273 			break;
1274 		}
1275 		/*
1276 		 * Interpret EOF only in canonical mode.
1277 		 */
1278 		if (CCEQ(cc[VEOF], c) && lflag&ICANON)
1279 			break;
1280 		/*
1281 		 * Give user character.
1282 		 */
1283  		error = ureadc(c, uio);
1284 		if (error)
1285 			break;
1286  		if (uio->uio_resid == 0)
1287 			break;
1288 		/*
1289 		 * In canonical mode check for a "break character"
1290 		 * marking the end of a "line of input".
1291 		 */
1292 		if (lflag&ICANON && ttbreakc(c))
1293 			break;
1294 		first = 0;
1295 	}
1296 	/*
1297 	 * Look to unblock output now that (presumably)
1298 	 * the input queue has gone down.
1299 	 */
1300 	if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
1301 		if (cc[VSTART] != _POSIX_VDISABLE
1302 		   && putc(cc[VSTART], &tp->t_outq) == 0) {
1303 			tp->t_state &= ~TS_TBLOCK;
1304 			ttstart(tp);
1305 		}
1306 	}
1307 	return (error);
1308 }
1309 
1310 /*
1311  * Check the output queue on tp for space for a kernel message
1312  * (from uprintf/tprintf).  Allow some space over the normal
1313  * hiwater mark so we don't lose messages due to normal flow
1314  * control, but don't let the tty run amok.
1315  * Sleeps here are not interruptible, but we return prematurely
1316  * if new signals come in.
1317  */
1318 ttycheckoutq(tp, wait)
1319 	register struct tty *tp;
1320 	int wait;
1321 {
1322 	int hiwat, s, oldsig;
1323 
1324 	hiwat = tp->t_hiwat;
1325 	s = spltty();
1326 	oldsig = u.u_procp->p_sig;
1327 	if (tp->t_outq.c_cc > hiwat + 200)
1328 		while (tp->t_outq.c_cc > hiwat) {
1329 			ttstart(tp);
1330 			if (wait == 0 || u.u_procp->p_sig != oldsig) {
1331 				splx(s);
1332 				return (0);
1333 			}
1334 			timeout(wakeup, (caddr_t)&tp->t_outq, hz);
1335 			tp->t_state |= TS_ASLEEP;
1336 			sleep((caddr_t)&tp->t_outq, PZERO - 1);
1337 		}
1338 	splx(s);
1339 	return (1);
1340 }
1341 
1342 /*
1343  * Called from the device's write routine after it has
1344  * calculated the tty-structure given as argument.
1345  */
1346 ttwrite(tp, uio, flag)
1347 	register struct tty *tp;
1348 	register struct uio *uio;
1349 {
1350 	register char *cp;
1351 	register int cc = 0, ce;
1352 	int i, hiwat, cnt, error, s;
1353 	char obuf[OBUFSIZ];
1354 
1355 	hiwat = tp->t_hiwat;
1356 	cnt = uio->uio_resid;
1357 	error = 0;
1358 loop:
1359 	s = spltty();
1360 	if ((tp->t_state&TS_CARR_ON) == 0 && (tp->t_cflag&CLOCAL) == 0) {
1361 		if (tp->t_state&TS_ISOPEN) {
1362 			splx(s);
1363 			return (EIO);
1364 		} else if (flag & IO_NDELAY) {
1365 			splx(s);
1366 			error = EWOULDBLOCK;
1367 			goto out;
1368 		} else {
1369 			/*
1370 			 * sleep awaiting carrier
1371 			 */
1372 			error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
1373 			    ttopen, 0);
1374 			splx(s);
1375 			if (error)
1376 				goto out;
1377 			goto loop;
1378 		}
1379 	}
1380 	splx(s);
1381 	/*
1382 	 * Hang the process if it's in the background.
1383 	 */
1384 	if (isbackground(u.u_procp, tp) &&
1385 	    (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
1386 	    (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1387 	    (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0 &&
1388 	     u.u_procp->p_pgrp->pg_jobc) {
1389 		pgsignal(u.u_procp->p_pgrp, SIGTTOU);
1390 		if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0))
1391 			goto out;
1392 		goto loop;
1393 	}
1394 	/*
1395 	 * Process the user's data in at most OBUFSIZ
1396 	 * chunks.  Perform any output translation.
1397 	 * Keep track of high water mark, sleep on overflow
1398 	 * awaiting device aid in acquiring new space.
1399 	 */
1400 	while (uio->uio_resid > 0 || cc > 0) {
1401 		if (tp->t_lflag&FLUSHO) {
1402 			uio->uio_resid = 0;
1403 			return (0);
1404 		}
1405 		if (tp->t_outq.c_cc > hiwat)
1406 			goto ovhiwat;
1407 		/*
1408 		 * Grab a hunk of data from the user,
1409 		 * unless we have some leftover from last time.
1410 		 */
1411 		if (cc == 0) {
1412 			cc = min(uio->uio_resid, OBUFSIZ);
1413 			cp = obuf;
1414 			error = uiomove(cp, cc, uio);
1415 			if (error) {
1416 				cc = 0;
1417 				break;
1418 			}
1419 		}
1420 		/*
1421 		 * If nothing fancy need be done, grab those characters we
1422 		 * can handle without any of ttyoutput's processing and
1423 		 * just transfer them to the output q.  For those chars
1424 		 * which require special processing (as indicated by the
1425 		 * bits in partab), call ttyoutput.  After processing
1426 		 * a hunk of data, look for FLUSHO so ^O's will take effect
1427 		 * immediately.
1428 		 */
1429 		while (cc > 0) {
1430 			if ((tp->t_oflag&OPOST) == 0)
1431 				ce = cc;
1432 			else {
1433 				ce = cc - scanc((unsigned)cc, (u_char *)cp,
1434 				   (u_char *)partab, 077);
1435 				/*
1436 				 * If ce is zero, then we're processing
1437 				 * a special character through ttyoutput.
1438 				 */
1439 				if (ce == 0) {
1440 					tp->t_rocount = 0;
1441 					if (ttyoutput(*cp, tp) >= 0) {
1442 					    /* no c-lists, wait a bit */
1443 					    ttstart(tp);
1444 					    if (error = tsleep((caddr_t)&lbolt,
1445 						TTOPRI | PCATCH, ttybuf, 0))
1446 						    break;
1447 					    goto loop;
1448 					}
1449 					cp++, cc--;
1450 					if ((tp->t_lflag&FLUSHO) ||
1451 					    tp->t_outq.c_cc > hiwat)
1452 						goto ovhiwat;
1453 					continue;
1454 				}
1455 			}
1456 			/*
1457 			 * A bunch of normal characters have been found,
1458 			 * transfer them en masse to the output queue and
1459 			 * continue processing at the top of the loop.
1460 			 * If there are any further characters in this
1461 			 * <= OBUFSIZ chunk, the first should be a character
1462 			 * requiring special handling by ttyoutput.
1463 			 */
1464 			tp->t_rocount = 0;
1465 			i = b_to_q(cp, ce, &tp->t_outq);
1466 			ce -= i;
1467 			tp->t_col += ce;
1468 			cp += ce, cc -= ce, tk_nout += ce;
1469 			tp->t_outcc += ce;
1470 			if (i > 0) {
1471 				/* out of c-lists, wait a bit */
1472 				ttstart(tp);
1473 				if (error = tsleep((caddr_t)&lbolt,
1474 				    TTOPRI | PCATCH, ttybuf, 0))
1475 					break;
1476 				goto loop;
1477 			}
1478 			if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat)
1479 				break;
1480 		}
1481 		ttstart(tp);
1482 	}
1483 out:
1484 	/*
1485 	 * If cc is nonzero, we leave the uio structure inconsistent,
1486 	 * as the offset and iov pointers have moved forward,
1487 	 * but it doesn't matter (the call will either return short
1488 	 * or restart with a new uio).
1489 	 */
1490 	uio->uio_resid += cc;
1491 	return (error);
1492 
1493 ovhiwat:
1494 	ttstart(tp);
1495 	s = spltty();
1496 	/*
1497 	 * This can only occur if FLUSHO is set in t_lflag,
1498 	 * or if ttstart/oproc is synchronous (or very fast).
1499 	 */
1500 	if (tp->t_outq.c_cc <= hiwat) {
1501 		splx(s);
1502 		goto loop;
1503 	}
1504 	if (flag & IO_NDELAY) {
1505 		splx(s);
1506 		uio->uio_resid += cc;
1507 		if (uio->uio_resid == cnt)
1508 			return (EWOULDBLOCK);
1509 		return (0);
1510 	}
1511 	tp->t_state |= TS_ASLEEP;
1512 	error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1513 	splx(s);
1514 	if (error)
1515 		goto out;
1516 	goto loop;
1517 }
1518 
1519 /*
1520  * Rubout one character from the rawq of tp
1521  * as cleanly as possible.
1522  */
1523 ttyrub(c, tp)
1524 	register c;
1525 	register struct tty *tp;
1526 {
1527 	register char *cp;
1528 	register int savecol;
1529 	int s;
1530 	char *nextc();
1531 
1532 	if ((tp->t_lflag&ECHO) == 0)
1533 		return;
1534 	tp->t_lflag &= ~FLUSHO;
1535 	if (tp->t_lflag&ECHOE) {
1536 		if (tp->t_rocount == 0) {
1537 			/*
1538 			 * Screwed by ttwrite; retype
1539 			 */
1540 			ttyretype(tp);
1541 			return;
1542 		}
1543 		if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE))
1544 			ttyrubo(tp, 2);
1545 		else switch (partab[c&=0377]&077) {
1546 
1547 		case ORDINARY:
1548 			ttyrubo(tp, 1);
1549 			break;
1550 
1551 		case VTAB:
1552 		case BACKSPACE:
1553 		case CONTROL:
1554 		case RETURN:
1555 			if (tp->t_lflag&ECHOCTL)
1556 				ttyrubo(tp, 2);
1557 			break;
1558 
1559 		case TAB: {
1560 			int c;
1561 
1562 			if (tp->t_rocount < tp->t_rawq.c_cc) {
1563 				ttyretype(tp);
1564 				return;
1565 			}
1566 			s = spltty();
1567 			savecol = tp->t_col;
1568 			tp->t_state |= TS_CNTTB;
1569 			tp->t_lflag |= FLUSHO;
1570 			tp->t_col = tp->t_rocol;
1571 			cp = tp->t_rawq.c_cf;
1572 			if (cp)
1573 				c = *cp;	/* XXX FIX NEXTC */
1574 			for (; cp; cp = nextc(&tp->t_rawq, cp, &c))
1575 				ttyecho(c, tp);
1576 			tp->t_lflag &= ~FLUSHO;
1577 			tp->t_state &= ~TS_CNTTB;
1578 			splx(s);
1579 			/*
1580 			 * savecol will now be length of the tab
1581 			 */
1582 			savecol -= tp->t_col;
1583 			tp->t_col += savecol;
1584 			if (savecol > 8)
1585 				savecol = 8;		/* overflow screw */
1586 			while (--savecol >= 0)
1587 				(void) ttyoutput('\b', tp);
1588 			break;
1589 		}
1590 
1591 		default:
1592 			/* XXX */
1593 			printf("ttyrub: would panic c = %d, val = %d\n",
1594 				c, partab[c&=0377]&077);
1595 			/*panic("ttyrub");*/
1596 		}
1597 	} else if (tp->t_lflag&ECHOPRT) {
1598 		if ((tp->t_state&TS_ERASE) == 0) {
1599 			(void) ttyoutput('\\', tp);
1600 			tp->t_state |= TS_ERASE;
1601 		}
1602 		ttyecho(c, tp);
1603 	} else
1604 		ttyecho(tp->t_cc[VERASE], tp);
1605 	tp->t_rocount--;
1606 }
1607 
1608 /*
1609  * Crt back over cnt chars perhaps
1610  * erasing them.
1611  */
1612 ttyrubo(tp, cnt)
1613 	register struct tty *tp;
1614 	int cnt;
1615 {
1616 
1617 	while (--cnt >= 0)
1618 		ttyoutstr("\b \b", tp);
1619 }
1620 
1621 /*
1622  * Reprint the rawq line.
1623  * We assume c_cc has already been checked.
1624  */
1625 ttyretype(tp)
1626 	register struct tty *tp;
1627 {
1628 	register char *cp;
1629 	char *nextc();
1630 	int s, c;
1631 
1632 	if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1633 		ttyecho(tp->t_cc[VREPRINT], tp);
1634 	(void) ttyoutput('\n', tp);
1635 	s = spltty();
1636 	/*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE
1637 	  BIT OF FIRST CHAR ****/
1638 	for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) {
1639 		ttyecho(c, tp);
1640 	}
1641 	for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) {
1642 		ttyecho(c, tp);
1643 	}
1644 	tp->t_state &= ~TS_ERASE;
1645 	splx(s);
1646 	tp->t_rocount = tp->t_rawq.c_cc;
1647 	tp->t_rocol = 0;
1648 }
1649 
1650 /*
1651  * Echo a typed character to the terminal.
1652  */
1653 ttyecho(c, tp)
1654 	register c;
1655 	register struct tty *tp;
1656 {
1657 	if ((tp->t_state&TS_CNTTB) == 0)
1658 		tp->t_lflag &= ~FLUSHO;
1659 	if ((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 || c == '\n'))
1660 		return;
1661 	if (tp->t_lflag&ECHOCTL) {
1662 		if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
1663 		    c == 0177) {
1664 			(void) ttyoutput('^', tp);
1665 			c &= TTY_CHARMASK;
1666 			if (c == 0177)
1667 				c = '?';
1668 			else
1669 				c += 'A' - 1;
1670 		}
1671 	}
1672 	(void) ttyoutput(c, tp);
1673 }
1674 
1675 /*
1676  * send string cp to tp
1677  */
1678 ttyoutstr(cp, tp)
1679 	register char *cp;
1680 	register struct tty *tp;
1681 {
1682 	register char c;
1683 
1684 	while (c = *cp++)
1685 		(void) ttyoutput(c, tp);
1686 }
1687 
1688 ttwakeup(tp)
1689 	struct tty *tp;
1690 {
1691 
1692 	if (tp->t_rsel) {
1693 		selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
1694 		tp->t_state &= ~TS_RCOLL;
1695 		tp->t_rsel = 0;
1696 	}
1697 	if (tp->t_state & TS_ASYNC)
1698 		pgsignal(tp->t_pgrp, SIGIO);
1699 	wakeup((caddr_t)&tp->t_rawq);
1700 }
1701 
1702 /*
1703  * set tty hi and low water marks
1704  *
1705  * Try to arrange the dynamics so there's about one second
1706  * from hi to low water.
1707  *
1708  */
1709 ttsetwater(tp)
1710 	struct tty *tp;
1711 {
1712 	register cps = tp->t_ospeed / 10;
1713 	register x;
1714 
1715 #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x))
1716 	tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT);
1717 	x += cps;
1718 	x = clamp(x, TTMAXHIWAT, TTMINHIWAT);
1719 	tp->t_hiwat = roundup(x, CBSIZE);
1720 #undef clamp
1721 }
1722 
1723 ttspeedtab(speed, table)
1724 	struct speedtab table[];
1725 {
1726 	register int i;
1727 
1728 	for (i = 0; table[i].sp_speed != -1; i++)
1729 		if (table[i].sp_speed == speed)
1730 			return(table[i].sp_code);
1731 	return(-1);
1732 }
1733 
1734 int ttyhostname = 0;
1735 /*
1736  * (^T)
1737  * Report on state of foreground process group.
1738  */
1739 ttyinfo(tp)
1740 	struct tty *tp;
1741 {
1742 	register struct proc *p, *pick = NULL;
1743 	register char *cp = hostname;
1744 	int x, s;
1745 	struct timeval utime, stime;
1746 #define	pgtok(a)	(((a)*NBPG)/1024)
1747 
1748 	if (ttycheckoutq(tp,0) == 0)
1749 		return;
1750 	/*
1751 	 * hostname
1752 	 */
1753 	if (ttyhostname) {
1754 		if (*cp == '\0')
1755 			ttyprintf(tp, "amnesia");
1756 		else
1757 			while (*cp && *cp != '.')
1758 				tputchar(*cp++, tp);
1759 		tputchar(' ');
1760 	}
1761 	/*
1762 	 * load average
1763 	 */
1764 	x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT;
1765 	ttyprintf(tp, "load: %d.", x/100);
1766 	ttyoutint(x%100, 10, 2, tp);
1767 	if (tp->t_session == NULL)
1768 		ttyprintf(tp, " not a controlling terminal\n");
1769 	else if (tp->t_pgrp == NULL)
1770 		ttyprintf(tp, " no foreground process group\n");
1771 	else if ((p = tp->t_pgrp->pg_mem) == NULL)
1772 		ttyprintf(tp, " empty foreground process group\n");
1773 	else {
1774 		/* pick interesting process */
1775 		for (; p != NULL; p = p->p_pgrpnxt) {
1776 			if (proc_compare(pick, p))
1777 				pick = p;
1778 		}
1779 		ttyprintf(tp, "  cmd: %s %d [%s] ",
1780 			pick->p_comm, pick->p_pid,
1781 			pick->p_wmesg ? pick->p_wmesg : "running");
1782 		/*
1783 		 * cpu time
1784 		 */
1785 		if (u.u_procp == pick)
1786 			s = splclock();
1787 		utime = pick->p_utime;
1788 		stime = pick->p_stime;
1789 		if (u.u_procp == pick)
1790 			splx(s);
1791 		/* user time */
1792 		x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */
1793 		ttyoutint(utime.tv_sec, 10, 1, tp);
1794 		tputchar('.', tp);
1795 		ttyoutint(x, 10, 2, tp);
1796 		tputchar('u', tp);
1797 		tputchar(' ', tp);
1798 		/* system time */
1799 		x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */
1800 		ttyoutint(stime.tv_sec, 10, 1, tp);
1801 		tputchar('.', tp);
1802 		ttyoutint(x, 10, 2, tp);
1803 		tputchar('s', tp);
1804 		tputchar(' ', tp);
1805 		/*
1806 		 * pctcpu
1807 		 */
1808 		x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT;
1809 		ttyoutint(x/100, 10, 1, tp);
1810 #ifdef notdef	/* do we really want this ??? */
1811 		tputchar('.', tp);
1812 		ttyoutint(x%100, 10, 2, tp);
1813 #endif
1814 		ttyprintf(tp, "%% %dk\n", pgtok(pick->p_ssize + pick->p_dsize));
1815 	}
1816 	tp->t_rocount = 0;	/* so pending input will be retyped if BS */
1817 }
1818 
1819 ttyoutint(n, base, min, tp)
1820 	register int n, base, min;
1821 	register struct tty *tp;
1822 {
1823 	char info[16];
1824 	register char *p = info;
1825 
1826 	while (--min >= 0 || n) {
1827 		*p++ = "0123456789abcdef"[n%base];
1828 		n /= base;
1829 	}
1830 	while (p > info)
1831 		ttyoutput(*--p, tp);
1832 }
1833 
1834 /*
1835  * Returns 1 if p2 is "better" than p1
1836  *
1837  * The algorithm for picking the "interesting" process is thus:
1838  *
1839  *	1) (Only foreground processes are eligable - implied)
1840  *	2) Runnable processes are favored over anything
1841  *	   else.  The runner with the highest cpu
1842  *	   utilization is picked (p_cpu).  Ties are
1843  *	   broken by picking the highest pid.
1844  *	3  Next, the sleeper with the shortest sleep
1845  *	   time is favored.  With ties, we pick out
1846  *	   just "short-term" sleepers (SSINTR == 0).
1847  *	   Further ties are broken by picking the highest
1848  *	   pid.
1849  *
1850  */
1851 #define isrun(p)	(((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
1852 proc_compare(p1, p2)
1853 	register struct proc *p1, *p2;
1854 {
1855 
1856 	if (p1 == NULL)
1857 		return (1);
1858 	/*
1859 	 * see if at least one of them is runnable
1860 	 */
1861 	switch (isrun(p1)<<1 | isrun(p2)) {
1862 	case 0x01:
1863 		return (1);
1864 	case 0x10:
1865 		return (0);
1866 	case 0x11:
1867 		/*
1868 		 * tie - favor one with highest recent cpu utilization
1869 		 */
1870 		if (p2->p_cpu > p1->p_cpu)
1871 			return (1);
1872 		if (p1->p_cpu > p2->p_cpu)
1873 			return (0);
1874 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
1875 	}
1876 	/*
1877 	 * pick the one with the smallest sleep time
1878 	 */
1879 	if (p2->p_slptime > p1->p_slptime)
1880 		return (0);
1881 	if (p1->p_slptime > p2->p_slptime)
1882 		return (1);
1883 	/*
1884 	 * favor one sleeping in a non-interruptible sleep
1885 	 */
1886 	if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0)
1887 		return (1);
1888 	if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
1889 		return (0);
1890 	return(p2->p_pid > p1->p_pid);		/* tie - return highest pid */
1891 }
1892 #define TOTTY	0x2	/* XXX should be in header */
1893 /*VARARGS2*/
1894 ttyprintf(tp, fmt, x1)
1895 	struct tty *tp;
1896 	char *fmt;
1897 	unsigned x1;
1898 {
1899 	prf(fmt, &x1, TOTTY, (caddr_t)tp);
1900 }
1901 
1902 /*
1903  * Output char to tty; console putchar style.
1904  */
1905 tputchar(c, tp)
1906 	int c;
1907 	struct tty *tp;
1908 {
1909 	register s = spltty();
1910 
1911 	if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN))
1912 	    == (TS_CARR_ON | TS_ISOPEN)) {
1913 		if (c == '\n')
1914 			(void) ttyoutput('\r', tp);
1915 		(void) ttyoutput(c, tp);
1916 		ttstart(tp);
1917 		splx(s);
1918 		return (0);
1919 	}
1920 	splx(s);
1921 	return (-1);
1922 }
1923