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