xref: /csrg-svn/sys/kern/tty.c (revision 49907)
149594Sbostic /*-
249594Sbostic  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
349594Sbostic  * Copyright (c) 1991 The Regents of the University of California.
449594Sbostic  * All rights reserved.
523387Smckusick  *
649594Sbostic  * %sccs.include.redist.c%
749594Sbostic  *
8*49907Sbostic  *	@(#)tty.c	7.44 (Berkeley) 05/28/91
923387Smckusick  */
1039Sbill 
1117095Sbloom #include "param.h"
1217095Sbloom #include "systm.h"
1317095Sbloom #include "ioctl.h"
1439407Smarc #define TTYDEFCHARS
1517095Sbloom #include "tty.h"
1635811Smarc #undef TTYDEFCHARS
1717095Sbloom #include "proc.h"
1817095Sbloom #include "file.h"
1917095Sbloom #include "conf.h"
2029946Skarels #include "dkstat.h"
2117095Sbloom #include "uio.h"
2217095Sbloom #include "kernel.h"
2337728Smckusick #include "vnode.h"
2435811Smarc #include "syslog.h"
2539Sbill 
2648439Skarels #include "vm/vm.h"
2737525Smckusick 
28*49907Sbostic static int proc_compare __P((struct proc *p1, struct proc *p2));
29*49907Sbostic 
3040712Skarels /* symbolic sleep message strings */
3140712Skarels char ttyin[] = "ttyin";
3240712Skarels char ttyout[] = "ttyout";
3341370Smarc char ttopen[] = "ttyopn";
3441370Smarc char ttclos[] = "ttycls";
3540712Skarels char ttybg[] = "ttybg";
3640712Skarels char ttybuf[] = "ttybuf";
3740712Skarels 
387436Skre /*
397436Skre  * Table giving parity for characters and indicating
4035811Smarc  * character classes to tty driver. The 8th bit
4135811Smarc  * indicates parity, the 7th bit indicates the character
4235811Smarc  * is an alphameric or underscore (for ALTWERASE), and the
4335811Smarc  * low 6 bits indicate delay type.  If the low 6 bits are 0
4449380Skarels  * then the character needs no special processing on output;
4549380Skarels  * classes other than 0 might be translated or (not currently)
4649380Skarels  * require delays.
477436Skre  */
4849380Skarels #define	PARITY(c)	(partab[c] & 0x80)
4949380Skarels #define	ISALPHA(c)	(partab[(c)&TTY_CHARMASK] & 0x40)
5049380Skarels #define	CCLASSMASK	0x3f
5149380Skarels #define	CCLASS(c)	(partab[c] & CCLASSMASK)
5239Sbill 
5349380Skarels #define	E	0x00	/* even parity */
5449380Skarels #define	O	0x80	/* odd parity */
5549380Skarels #define	ALPHA	0x40	/* alpha or underscore */
5649380Skarels 
5749380Skarels #define	NO	ORDINARY
5849380Skarels #define	NA	ORDINARY|ALPHA
5949380Skarels #define	CC	CONTROL
6049380Skarels #define	BS	BACKSPACE
6149380Skarels #define	NL	NEWLINE
6249380Skarels #define	TB	TAB
6349380Skarels #define	VT	VTAB
6449380Skarels #define	CR	RETURN
6549380Skarels 
667436Skre char partab[] = {
6749380Skarels 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC,	/* nul - bel */
6849380Skarels 	O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
6949380Skarels 	O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
7049380Skarels 	E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
7149380Skarels 	O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
7249380Skarels 	E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
7349380Skarels 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
7449380Skarels 	O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
7549380Skarels 	O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
7649380Skarels 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
7749380Skarels 	E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
7849380Skarels 	O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
7949380Skarels 	E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
8049380Skarels 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
8149380Skarels 	O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
8249380Skarels 	E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
837436Skre 	/*
8449380Skarels 	 * "meta" chars; should be settable per charset.
8549380Skarels 	 * For now, treat all as normal characters.
867436Skre 	 */
8749380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
8849380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
8949380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9049380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9149380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9249380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9349380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9449380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9549380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9649380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9749380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9849380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
9949380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
10049380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
10149380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
10249380Skarels 	NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
1037436Skre };
10449380Skarels #undef	NO
10549380Skarels #undef	NA
10649380Skarels #undef	CC
10749380Skarels #undef	BS
10849380Skarels #undef	NL
10949380Skarels #undef	TB
11049380Skarels #undef	VT
11149380Skarels #undef	CR
1127436Skre 
11335811Smarc extern struct tty *constty;		/* temporary virtual console */
11435811Smarc 
115146Sbill /*
11635811Smarc  * Is 'c' a line delimiter ("break" character)?
11739Sbill  */
11840712Skarels #define ttbreakc(c) ((c) == '\n' || ((c) == cc[VEOF] || \
11940712Skarels 	(c) == cc[VEOL] || (c) == cc[VEOL2]) && (c) != _POSIX_VDISABLE)
12039Sbill 
12139Sbill ttychars(tp)
1229578Ssam 	struct tty *tp;
12339Sbill {
12447545Skarels 
12535811Smarc 	bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
12639Sbill }
12739Sbill 
12839Sbill /*
12949380Skarels  * Flush tty after output has drained.
13039Sbill  */
13112752Ssam ttywflush(tp)
13237584Smarc 	struct tty *tp;
13339Sbill {
13440712Skarels 	int error;
13539Sbill 
13640712Skarels 	if ((error = ttywait(tp)) == 0)
13740712Skarels 		ttyflush(tp, FREAD);
13840712Skarels 	return (error);
13912752Ssam }
14012752Ssam 
14135811Smarc /*
14235811Smarc  * Wait for output to drain.
14335811Smarc  */
14412752Ssam ttywait(tp)
14512752Ssam 	register struct tty *tp;
14612752Ssam {
14740712Skarels 	int error = 0, s = spltty();
14812752Ssam 
14913809Ssam 	while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
15037584Smarc 	    (tp->t_state&TS_CARR_ON || tp->t_cflag&CLOCAL) &&
15137584Smarc 	    tp->t_oproc) {
152903Sbill 		(*tp->t_oproc)(tp);
1535408Swnj 		tp->t_state |= TS_ASLEEP;
15443377Smarc 		if (error = ttysleep(tp, (caddr_t)&tp->t_outq,
15543377Smarc 		    TTOPRI | PCATCH, ttyout, 0))
15640712Skarels 			break;
157903Sbill 	}
1589859Ssam 	splx(s);
15940712Skarels 	return (error);
16039Sbill }
16139Sbill 
16249380Skarels #define	flushq(qq) { \
16349380Skarels 	register struct clist *q = qq; \
16449380Skarels 	if (q->c_cc) \
16549380Skarels 		ndflush(q, q->c_cc); \
16649380Skarels }
16749380Skarels 
16839Sbill /*
16949380Skarels  * Flush TTY read and/or write queues,
17049380Skarels  * notifying anyone waiting.
17139Sbill  */
17212752Ssam ttyflush(tp, rw)
1737625Ssam 	register struct tty *tp;
17439Sbill {
175903Sbill 	register s;
176903Sbill 
17717545Skarels 	s = spltty();
178903Sbill 	if (rw & FREAD) {
17949380Skarels 		flushq(&tp->t_canq);
18049380Skarels 		flushq(&tp->t_rawq);
18149380Skarels 		tp->t_rocount = 0;
18249380Skarels 		tp->t_rocol = 0;
18349380Skarels 		tp->t_state &= ~TS_LOCAL;
18437584Smarc 		ttwakeup(tp);
185903Sbill 	}
186903Sbill 	if (rw & FWRITE) {
1875408Swnj 		tp->t_state &= ~TS_TTSTOP;
1885426Swnj 		(*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
18949380Skarels 		flushq(&tp->t_outq);
19049380Skarels 		wakeup((caddr_t)&tp->t_outq);
19149380Skarels 		if (tp->t_wsel) {
19249380Skarels 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
19349380Skarels 			tp->t_wsel = 0;
19449380Skarels 			tp->t_state &= ~TS_WCOLL;
19549380Skarels 		}
196903Sbill 	}
197903Sbill 	splx(s);
19839Sbill }
19939Sbill 
200903Sbill /*
201903Sbill  * Send stop character on input overflow.
202903Sbill  */
203903Sbill ttyblock(tp)
2047625Ssam 	register struct tty *tp;
20539Sbill {
206903Sbill 	register x;
2079578Ssam 
208903Sbill 	x = tp->t_rawq.c_cc + tp->t_canq.c_cc;
209903Sbill 	if (tp->t_rawq.c_cc > TTYHOG) {
21012752Ssam 		ttyflush(tp, FREAD|FWRITE);
2115408Swnj 		tp->t_state &= ~TS_TBLOCK;
212903Sbill 	}
21315118Skarels 	/*
21415118Skarels 	 * Block further input iff:
21515118Skarels 	 * Current input > threshold AND input is available to user program
21615118Skarels 	 */
21742350Smckusick 	if (x >= TTYHOG/2 && (tp->t_state & TS_TBLOCK) == 0 &&
21840712Skarels 	    ((tp->t_lflag&ICANON) == 0) || (tp->t_canq.c_cc > 0) &&
21935811Smarc 	    tp->t_cc[VSTOP] != _POSIX_VDISABLE) {
22042350Smckusick 		if (putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
22115118Skarels 			tp->t_state |= TS_TBLOCK;
22215118Skarels 			ttstart(tp);
22315118Skarels 		}
224903Sbill 	}
22539Sbill }
22639Sbill 
22747545Skarels ttstart(tp)
22837584Smarc 	struct tty *tp;
229121Sbill {
230121Sbill 
23147545Skarels 	if (tp->t_oproc)		/* kludge for pty */
23247545Skarels 		(*tp->t_oproc)(tp);
23347545Skarels }
23447545Skarels 
23547545Skarels ttrstrt(tp)				/* XXX */
23647545Skarels 	struct tty *tp;
23747545Skarels {
23847545Skarels 
23940712Skarels #ifdef DIAGNOSTIC
2409578Ssam 	if (tp == 0)
2419578Ssam 		panic("ttrstrt");
24240712Skarels #endif
2435408Swnj 	tp->t_state &= ~TS_TIMEOUT;
244903Sbill 	ttstart(tp);
245121Sbill }
246121Sbill 
24739Sbill 
24839Sbill /*
24949380Skarels  * Common code for ioctls on tty devices.
25049380Skarels  * Called after line-discipline-specific ioctl
25149380Skarels  * has been called to do discipline-specific functions
25249380Skarels  * and/or reject any of these ioctl commands.
25339Sbill  */
2541780Sbill /*ARGSUSED*/
2557625Ssam ttioctl(tp, com, data, flag)
2567625Ssam 	register struct tty *tp;
2577625Ssam 	caddr_t data;
25839Sbill {
25947545Skarels 	register struct proc *p = curproc;		/* XXX */
26039Sbill 	extern int nldisp;
26137554Smckusick 	int s, error;
26239Sbill 
263903Sbill 	/*
264903Sbill 	 * If the ioctl involves modification,
26517545Skarels 	 * hang if in the background.
266903Sbill 	 */
2677625Ssam 	switch (com) {
26839Sbill 
26935811Smarc 	case TIOCSETD:
270903Sbill 	case TIOCFLUSH:
27135811Smarc 	/*case TIOCSPGRP:*/
2729325Ssam 	case TIOCSTI:
27317598Sbloom 	case TIOCSWINSZ:
27435811Smarc 	case TIOCSETA:
27535811Smarc 	case TIOCSETAW:
27635811Smarc 	case TIOCSETAF:
27740030Smarc #ifdef COMPAT_43
27840030Smarc 	case TIOCSETP:
27940030Smarc 	case TIOCSETN:
28040030Smarc 	case TIOCSETC:
28140030Smarc 	case TIOCSLTC:
28240030Smarc 	case TIOCLBIS:
28340030Smarc 	case TIOCLBIC:
28440030Smarc 	case TIOCLSET:
28540030Smarc 	case OTIOCSETD:
28640030Smarc #endif
28747545Skarels 		while (isbackground(curproc, tp) &&
28847545Skarels 		   p->p_pgrp->pg_jobc && (p->p_flag&SPPWAIT) == 0 &&
28947545Skarels 		   (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
29047545Skarels 		   (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
29147545Skarels 			pgsignal(p->p_pgrp, SIGTTOU, 1);
29243377Smarc 			if (error = ttysleep(tp, (caddr_t)&lbolt,
29343377Smarc 			    TTOPRI | PCATCH, ttybg, 0))
29440712Skarels 				return (error);
295903Sbill 		}
296903Sbill 		break;
297903Sbill 	}
298903Sbill 
2999578Ssam 	/*
3009578Ssam 	 * Process the ioctl.
3019578Ssam 	 */
3027625Ssam 	switch (com) {
303903Sbill 
3048556Sroot 	/* get discipline number */
30539Sbill 	case TIOCGETD:
3067625Ssam 		*(int *)data = tp->t_line;
30739Sbill 		break;
30839Sbill 
3098556Sroot 	/* set line discipline */
3107625Ssam 	case TIOCSETD: {
3117625Ssam 		register int t = *(int *)data;
31235811Smarc 		dev_t dev = tp->t_dev;
3137625Ssam 
31435811Smarc 		if ((unsigned)t >= nldisp)
31510851Ssam 			return (ENXIO);
31625584Skarels 		if (t != tp->t_line) {
31725584Skarels 			s = spltty();
31849752Smarc 			(*linesw[tp->t_line].l_close)(tp, flag);
31925584Skarels 			error = (*linesw[t].l_open)(dev, tp);
32025584Skarels 			if (error) {
32135811Smarc 				(void)(*linesw[tp->t_line].l_open)(dev, tp);
32225584Skarels 				splx(s);
32325584Skarels 				return (error);
32425584Skarels 			}
32525584Skarels 			tp->t_line = t;
32610851Ssam 			splx(s);
32710851Ssam 		}
32839Sbill 		break;
3297625Ssam 	}
33039Sbill 
3318556Sroot 	/* prevent more opens on channel */
3325614Swnj 	case TIOCEXCL:
3335614Swnj 		tp->t_state |= TS_XCLUDE;
3345614Swnj 		break;
3355614Swnj 
3365614Swnj 	case TIOCNXCL:
3375614Swnj 		tp->t_state &= ~TS_XCLUDE;
3385614Swnj 		break;
3395614Swnj 
34039Sbill 	case TIOCHPCL:
34135811Smarc 		tp->t_cflag |= HUPCL;
34239Sbill 		break;
34339Sbill 
3443942Sbugs 	case TIOCFLUSH: {
3457625Ssam 		register int flags = *(int *)data;
3467625Ssam 
3477625Ssam 		if (flags == 0)
3483942Sbugs 			flags = FREAD|FWRITE;
3497625Ssam 		else
3507625Ssam 			flags &= FREAD|FWRITE;
35112752Ssam 		ttyflush(tp, flags);
35239Sbill 		break;
3533944Sbugs 	}
35439Sbill 
35537584Smarc 	case FIOASYNC:
35637584Smarc 		if (*(int *)data)
35737584Smarc 			tp->t_state |= TS_ASYNC;
35837584Smarc 		else
35937584Smarc 			tp->t_state &= ~TS_ASYNC;
36037584Smarc 		break;
36137584Smarc 
36237584Smarc 	case FIONBIO:
36337584Smarc 		break;	/* XXX remove */
36437584Smarc 
3658556Sroot 	/* return number of characters immediately available */
3667625Ssam 	case FIONREAD:
3677625Ssam 		*(off_t *)data = ttnread(tp);
368174Sbill 		break;
369174Sbill 
37013077Ssam 	case TIOCOUTQ:
37113077Ssam 		*(int *)data = tp->t_outq.c_cc;
37213077Ssam 		break;
37313077Ssam 
3748589Sroot 	case TIOCSTOP:
37517545Skarels 		s = spltty();
3769578Ssam 		if ((tp->t_state&TS_TTSTOP) == 0) {
3775573Swnj 			tp->t_state |= TS_TTSTOP;
3785573Swnj 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
3795573Swnj 		}
3807625Ssam 		splx(s);
3815573Swnj 		break;
3825573Swnj 
3838589Sroot 	case TIOCSTART:
38417545Skarels 		s = spltty();
38535811Smarc 		if ((tp->t_state&TS_TTSTOP) || (tp->t_lflag&FLUSHO)) {
3865573Swnj 			tp->t_state &= ~TS_TTSTOP;
38735811Smarc 			tp->t_lflag &= ~FLUSHO;
3885573Swnj 			ttstart(tp);
3895573Swnj 		}
3907625Ssam 		splx(s);
3915573Swnj 		break;
3925573Swnj 
3939325Ssam 	/*
3949325Ssam 	 * Simulate typing of a character at the terminal.
3959325Ssam 	 */
3969325Ssam 	case TIOCSTI:
39747545Skarels 		if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
39817183Smckusick 			return (EPERM);
39947545Skarels 		if (p->p_ucred->cr_uid && !isctty(p, tp))
4009325Ssam 			return (EACCES);
4019578Ssam 		(*linesw[tp->t_line].l_rint)(*(char *)data, tp);
4029325Ssam 		break;
4039325Ssam 
40435811Smarc 	case TIOCGETA: {
40535811Smarc 		struct termios *t = (struct termios *)data;
40612752Ssam 
40735811Smarc 		bcopy(&tp->t_termios, t, sizeof(struct termios));
40835811Smarc 		break;
40935811Smarc 	}
41035811Smarc 
41135811Smarc 	case TIOCSETA:
41235811Smarc 	case TIOCSETAW:
41337584Smarc 	case TIOCSETAF: {
41435811Smarc 		register struct termios *t = (struct termios *)data;
41540712Skarels 
41617545Skarels 		s = spltty();
41739407Smarc 		if (com == TIOCSETAW || com == TIOCSETAF) {
41840712Skarels 			if (error = ttywait(tp)) {
41940712Skarels 				splx(s);
42040712Skarels 				return (error);
42140712Skarels 			}
42245007Smarc 			if (com == TIOCSETAF)
42339407Smarc 				ttyflush(tp, FREAD);
42439407Smarc 		}
42540712Skarels 		if ((t->c_cflag&CIGNORE) == 0) {
42635811Smarc 			/*
42735811Smarc 			 * set device hardware
42835811Smarc 			 */
42937584Smarc 			if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
43037584Smarc 				splx(s);
43135811Smarc 				return (error);
43237584Smarc 			} else {
43340712Skarels 				if ((tp->t_state&TS_CARR_ON) == 0 &&
43437584Smarc 				    (tp->t_cflag&CLOCAL) &&
43540712Skarels 				    (t->c_cflag&CLOCAL) == 0) {
43637584Smarc 					tp->t_state &= ~TS_ISOPEN;
43737584Smarc 					tp->t_state |= TS_WOPEN;
43837584Smarc 					ttwakeup(tp);
43937584Smarc 				}
44035811Smarc 				tp->t_cflag = t->c_cflag;
44135811Smarc 				tp->t_ispeed = t->c_ispeed;
44235811Smarc 				tp->t_ospeed = t->c_ospeed;
44334492Skarels 			}
44435811Smarc 			ttsetwater(tp);
44512752Ssam 		}
44639407Smarc 		if (com != TIOCSETAF) {
44735811Smarc 			if ((t->c_lflag&ICANON) != (tp->t_lflag&ICANON))
44835811Smarc 				if (t->c_lflag&ICANON) {
44935811Smarc 					tp->t_lflag |= PENDIN;
45035811Smarc 					ttwakeup(tp);
45135811Smarc 				}
45235811Smarc 				else {
45335811Smarc 					struct clist tq;
45435811Smarc 
45535811Smarc 					catq(&tp->t_rawq, &tp->t_canq);
45635811Smarc 					tq = tp->t_rawq;
45735811Smarc 					tp->t_rawq = tp->t_canq;
45835811Smarc 					tp->t_canq = tq;
45935811Smarc 				}
46012752Ssam 		}
46135811Smarc 		tp->t_iflag = t->c_iflag;
46235811Smarc 		tp->t_oflag = t->c_oflag;
46342882Smarc 		/*
46442882Smarc 		 * Make the EXTPROC bit read only.
46542882Smarc 		 */
46642882Smarc 		if (tp->t_lflag&EXTPROC)
46742882Smarc 			t->c_lflag |= EXTPROC;
46842882Smarc 		else
46942882Smarc 			t->c_lflag &= ~EXTPROC;
47035811Smarc 		tp->t_lflag = t->c_lflag;
47135811Smarc 		bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
47212752Ssam 		splx(s);
47312752Ssam 		break;
47412752Ssam 	}
47512752Ssam 
47612752Ssam 	/*
47739555Smarc 	 * Set controlling terminal.
47839555Smarc 	 * Session ctty vnode pointer set in vnode layer.
47934492Skarels 	 */
48047545Skarels 	case TIOCSCTTY:
48139555Smarc 		if (!SESS_LEADER(p) ||
48239555Smarc 		   (p->p_session->s_ttyvp || tp->t_session) &&
48339555Smarc 		   (tp->t_session != p->p_session))
48439407Smarc 			return (EPERM);
48535811Smarc 		tp->t_session = p->p_session;
48639555Smarc 		tp->t_pgrp = p->p_pgrp;
48739555Smarc 		p->p_session->s_ttyp = tp;
48839555Smarc 		p->p_flag |= SCTTY;
48934492Skarels 		break;
49039555Smarc 
49134492Skarels 	/*
49235811Smarc 	 * Set terminal process group.
49317545Skarels 	 */
49418650Sbloom 	case TIOCSPGRP: {
49535811Smarc 		register struct pgrp *pgrp = pgfind(*(int *)data);
49617545Skarels 
49739555Smarc 		if (!isctty(p, tp))
49839555Smarc 			return (ENOTTY);
49940030Smarc 		else if (pgrp == NULL || pgrp->pg_session != p->p_session)
50039555Smarc 			return (EPERM);
50139555Smarc 		tp->t_pgrp = pgrp;
50212752Ssam 		break;
50318650Sbloom 	}
50412752Ssam 
50512752Ssam 	case TIOCGPGRP:
50647545Skarels 		if (!isctty(p, tp))
50739555Smarc 			return (ENOTTY);
50845007Smarc 		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
50912752Ssam 		break;
51012752Ssam 
51117598Sbloom 	case TIOCSWINSZ:
51218650Sbloom 		if (bcmp((caddr_t)&tp->t_winsize, data,
51318650Sbloom 		    sizeof (struct winsize))) {
51417598Sbloom 			tp->t_winsize = *(struct winsize *)data;
51542882Smarc 			pgsignal(tp->t_pgrp, SIGWINCH, 1);
51617598Sbloom 		}
51717598Sbloom 		break;
51817598Sbloom 
51917598Sbloom 	case TIOCGWINSZ:
52017598Sbloom 		*(struct winsize *)data = tp->t_winsize;
52117598Sbloom 		break;
52217598Sbloom 
52330534Skarels 	case TIOCCONS:
52430534Skarels 		if (*(int *)data) {
52542141Smckusick 			if (constty && constty != tp &&
52642141Smckusick 			    (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) ==
52742141Smckusick 			    (TS_CARR_ON|TS_ISOPEN))
52830534Skarels 				return (EBUSY);
52930534Skarels #ifndef	UCONSOLE
53047545Skarels 			if (error = suser(p->p_ucred, &p->p_acflag))
53137554Smckusick 				return (error);
53230534Skarels #endif
53330534Skarels 			constty = tp;
53430534Skarels 		} else if (tp == constty)
53533404Skarels 			constty = NULL;
53630534Skarels 		break;
53730534Skarels 
53848439Skarels 	case TIOCDRAIN:
53948439Skarels 		if (error = ttywait(tp))
54048439Skarels 			return (error);
54148439Skarels 		break;
54248439Skarels 
54347545Skarels 	default:
54435811Smarc #ifdef COMPAT_43
54547545Skarels 		return (ttcompat(tp, com, data, flag));
54647545Skarels #else
54747545Skarels 		return (-1);
54835811Smarc #endif
54939Sbill 	}
5508556Sroot 	return (0);
55139Sbill }
5524484Swnj 
5534484Swnj ttnread(tp)
5544484Swnj 	struct tty *tp;
5554484Swnj {
5564484Swnj 	int nread = 0;
5574484Swnj 
55835811Smarc 	if (tp->t_lflag & PENDIN)
5594484Swnj 		ttypend(tp);
5604484Swnj 	nread = tp->t_canq.c_cc;
56135811Smarc 	if ((tp->t_lflag & ICANON) == 0)
5624484Swnj 		nread += tp->t_rawq.c_cc;
5634484Swnj 	return (nread);
5644484Swnj }
5654484Swnj 
5665408Swnj ttselect(dev, rw)
5674484Swnj 	dev_t dev;
5685408Swnj 	int rw;
5694484Swnj {
5704484Swnj 	register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
5714484Swnj 	int nread;
57217545Skarels 	int s = spltty();
5734484Swnj 
5745408Swnj 	switch (rw) {
5754484Swnj 
5764484Swnj 	case FREAD:
5774484Swnj 		nread = ttnread(tp);
57837584Smarc 		if (nread > 0 ||
57940712Skarels 		   ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0))
5805408Swnj 			goto win;
5814938Swnj 		if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
5825408Swnj 			tp->t_state |= TS_RCOLL;
5834484Swnj 		else
58447545Skarels 			tp->t_rsel = curproc;
5855408Swnj 		break;
5864484Swnj 
5875408Swnj 	case FWRITE:
58835811Smarc 		if (tp->t_outq.c_cc <= tp->t_lowat)
5895408Swnj 			goto win;
5905408Swnj 		if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
5915408Swnj 			tp->t_state |= TS_WCOLL;
5925408Swnj 		else
59347545Skarels 			tp->t_wsel = curproc;
5945408Swnj 		break;
5954484Swnj 	}
5965408Swnj 	splx(s);
5975408Swnj 	return (0);
5985408Swnj win:
5995408Swnj 	splx(s);
6005408Swnj 	return (1);
6014484Swnj }
6027436Skre 
6037502Sroot /*
60449380Skarels  * Initial open of tty, or (re)entry to standard tty line discipline.
6057502Sroot  */
6067502Sroot ttyopen(dev, tp)
6077625Ssam 	dev_t dev;
6087625Ssam 	register struct tty *tp;
6097502Sroot {
6107502Sroot 
6117502Sroot 	tp->t_dev = dev;
61235811Smarc 
6137502Sroot 	tp->t_state &= ~TS_WOPEN;
61417545Skarels 	if ((tp->t_state & TS_ISOPEN) == 0) {
61517545Skarels 		tp->t_state |= TS_ISOPEN;
61617598Sbloom 		bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize));
61717545Skarels 	}
6188556Sroot 	return (0);
6197502Sroot }
6207502Sroot 
6217502Sroot /*
62225391Skarels  * "close" a line discipline
62325391Skarels  */
62449752Smarc ttylclose(tp, flag)
62549752Smarc 	struct tty *tp;
62649752Smarc 	int flag;
62725391Skarels {
62825391Skarels 
62949752Smarc 	if (flag&IO_NDELAY)
63049752Smarc 		ttyflush(tp, FREAD|FWRITE);
63149752Smarc 	else
63249752Smarc 		ttywflush(tp);
63325391Skarels }
63425391Skarels 
63525391Skarels /*
63649380Skarels  * Handle close() on a tty line: flush and set to initial state,
63749380Skarels  * bumping generation number so that pending read/write calls
63849380Skarels  * can detect recycling of the tty.
6397502Sroot  */
6407502Sroot ttyclose(tp)
6417625Ssam 	register struct tty *tp;
6427502Sroot {
64330534Skarels 	if (constty == tp)
64430534Skarels 		constty = NULL;
64525391Skarels 	ttyflush(tp, FREAD|FWRITE);
64639555Smarc 	tp->t_session = NULL;
64739555Smarc 	tp->t_pgrp = NULL;
6487502Sroot 	tp->t_state = 0;
64943377Smarc 	tp->t_gen++;
65040712Skarels 	return (0);
6517502Sroot }
6527502Sroot 
6537502Sroot /*
65425391Skarels  * Handle modem control transition on a tty.
65525391Skarels  * Flag indicates new state of carrier.
65625391Skarels  * Returns 0 if the line should be turned off, otherwise 1.
65725391Skarels  */
65825391Skarels ttymodem(tp, flag)
65925391Skarels 	register struct tty *tp;
66025391Skarels {
66125391Skarels 
66242193Smarc 	if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag&MDMBUF)) {
66325391Skarels 		/*
66425391Skarels 		 * MDMBUF: do flow control according to carrier flag
66525391Skarels 		 */
66625391Skarels 		if (flag) {
66725391Skarels 			tp->t_state &= ~TS_TTSTOP;
66825391Skarels 			ttstart(tp);
66925391Skarels 		} else if ((tp->t_state&TS_TTSTOP) == 0) {
67025391Skarels 			tp->t_state |= TS_TTSTOP;
67125391Skarels 			(*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
67225391Skarels 		}
67325391Skarels 	} else if (flag == 0) {
67425391Skarels 		/*
67525391Skarels 		 * Lost carrier.
67625391Skarels 		 */
67725391Skarels 		tp->t_state &= ~TS_CARR_ON;
67842193Smarc 		if (tp->t_state&TS_ISOPEN && (tp->t_cflag&CLOCAL) == 0) {
67942193Smarc 			if (tp->t_session && tp->t_session->s_leader)
68042193Smarc 				psignal(tp->t_session->s_leader, SIGHUP);
68142193Smarc 			ttyflush(tp, FREAD|FWRITE);
68242193Smarc 			return (0);
68325391Skarels 		}
68425391Skarels 	} else {
68525391Skarels 		/*
68625391Skarels 		 * Carrier now on.
68725391Skarels 		 */
68825391Skarels 		tp->t_state |= TS_CARR_ON;
68937584Smarc 		ttwakeup(tp);
69025391Skarels 	}
69125391Skarels 	return (1);
69225391Skarels }
69325391Skarels 
69425391Skarels /*
69525404Skarels  * Default modem control routine (for other line disciplines).
69625404Skarels  * Return argument flag, to turn off device on carrier drop.
69725404Skarels  */
69825415Skarels nullmodem(tp, flag)
69925415Skarels 	register struct tty *tp;
70025404Skarels 	int flag;
70125404Skarels {
70225404Skarels 
70325404Skarels 	if (flag)
70425404Skarels 		tp->t_state |= TS_CARR_ON;
70539407Smarc 	else {
70625404Skarels 		tp->t_state &= ~TS_CARR_ON;
70742193Smarc 		if ((tp->t_cflag&CLOCAL) == 0) {
70842193Smarc 			if (tp->t_session && tp->t_session->s_leader)
70942193Smarc 				psignal(tp->t_session->s_leader, SIGHUP);
71042193Smarc 			return (0);
71142193Smarc 		}
71239407Smarc 	}
71342193Smarc 	return (1);
71425404Skarels }
71525404Skarels 
71625404Skarels /*
7177502Sroot  * reinput pending characters after state switch
71817545Skarels  * call at spltty().
7197502Sroot  */
7207502Sroot ttypend(tp)
7217625Ssam 	register struct tty *tp;
7227502Sroot {
7237502Sroot 	struct clist tq;
7247502Sroot 	register c;
7257502Sroot 
72635811Smarc 	tp->t_lflag &= ~PENDIN;
7279578Ssam 	tp->t_state |= TS_TYPEN;
7287502Sroot 	tq = tp->t_rawq;
7297502Sroot 	tp->t_rawq.c_cc = 0;
7307502Sroot 	tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
7317502Sroot 	while ((c = getc(&tq)) >= 0)
7327502Sroot 		ttyinput(c, tp);
7339578Ssam 	tp->t_state &= ~TS_TYPEN;
7347502Sroot }
7357502Sroot 
7367502Sroot /*
73749380Skarels  * Process input of a single character received on a tty.
7387502Sroot  */
7397502Sroot ttyinput(c, tp)
7407625Ssam 	register c;
7417625Ssam 	register struct tty *tp;
7427502Sroot {
74335811Smarc 	register int iflag = tp->t_iflag;
74435811Smarc 	register int lflag = tp->t_lflag;
74535811Smarc 	register u_char *cc = tp->t_cc;
74635811Smarc 	int i, err;
7477502Sroot 
7489578Ssam 	/*
7499578Ssam 	 * If input is pending take it first.
7509578Ssam 	 */
75135811Smarc 	if (lflag&PENDIN)
7527502Sroot 		ttypend(tp);
75335811Smarc 	/*
75435811Smarc 	 * Gather stats.
75535811Smarc 	 */
7567502Sroot 	tk_nin++;
75735811Smarc 	if (lflag&ICANON) {
75835811Smarc 		tk_cancc++;
75935811Smarc 		tp->t_cancc++;
76035811Smarc 	} else {
76135811Smarc 		tk_rawcc++;
76235811Smarc 		tp->t_rawcc++;
76335811Smarc 	}
7649578Ssam 	/*
76535811Smarc 	 * Handle exceptional conditions (break, parity, framing).
7669578Ssam 	 */
76735811Smarc 	if (err = (c&TTY_ERRORMASK)) {
76835811Smarc 		c &= ~TTY_ERRORMASK;
76935811Smarc 		if (err&TTY_FE && !c) {		/* break */
77035811Smarc 			if (iflag&IGNBRK)
77135811Smarc 				goto endcase;
77235811Smarc 			else if (iflag&BRKINT && lflag&ISIG &&
77335811Smarc 				(cc[VINTR] != _POSIX_VDISABLE))
77435811Smarc 				c = cc[VINTR];
77547545Skarels 			else if (iflag&PARMRK)
77647545Skarels 				goto parmrk;
77735811Smarc 		} else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) {
77835811Smarc 			if (iflag&IGNPAR)
77935811Smarc 				goto endcase;
78035811Smarc 			else if (iflag&PARMRK) {
78135811Smarc parmrk:
78235811Smarc 				putc(0377|TTY_QUOTE, &tp->t_rawq);
78335811Smarc 				putc(0|TTY_QUOTE, &tp->t_rawq);
78435811Smarc 				putc(c|TTY_QUOTE, &tp->t_rawq);
78535811Smarc 				goto endcase;
78635811Smarc 			} else
78735811Smarc 				c = 0;
7887502Sroot 		}
7899578Ssam 	}
7909578Ssam 	/*
79135811Smarc 	 * In tandem mode, check high water mark.
7929578Ssam 	 */
79335811Smarc 	if (iflag&IXOFF)
79435811Smarc 		ttyblock(tp);
79535811Smarc 	if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
79649380Skarels 		c &= ~0x80;
79744419Smarc 	if ((tp->t_lflag&EXTPROC) == 0) {
79844419Smarc 		/*
79944419Smarc 		 * Check for literal nexting very first
80044419Smarc 		 */
80144419Smarc 		if (tp->t_state&TS_LNCH) {
80244419Smarc 			c |= TTY_QUOTE;
80344419Smarc 			tp->t_state &= ~TS_LNCH;
80444419Smarc 		}
80544419Smarc 		/*
80644419Smarc 		 * Scan for special characters.  This code
80744419Smarc 		 * is really just a big case statement with
80844419Smarc 		 * non-constant cases.  The bottom of the
80944419Smarc 		 * case statement is labeled ``endcase'', so goto
81044419Smarc 		 * it after a case match, or similar.
81144419Smarc 		 */
81244419Smarc 
81344419Smarc 		/*
81444419Smarc 		 * Control chars which aren't controlled
81544419Smarc 		 * by ICANON, ISIG, or IXON.
81644419Smarc 		 */
81744419Smarc 		if (lflag&IEXTEN) {
81844419Smarc 			if (CCEQ(cc[VLNEXT], c)) {
81944419Smarc 				if (lflag&ECHO) {
82044419Smarc 					if (lflag&ECHOE)
82144419Smarc 						ttyoutstr("^\b", tp);
82244419Smarc 					else
82344419Smarc 						ttyecho(c, tp);
82444419Smarc 				}
82544419Smarc 				tp->t_state |= TS_LNCH;
82644419Smarc 				goto endcase;
82744419Smarc 			}
82844419Smarc 			if (CCEQ(cc[VDISCARD], c)) {
82944419Smarc 				if (lflag&FLUSHO)
83044419Smarc 					tp->t_lflag &= ~FLUSHO;
83144419Smarc 				else {
83244419Smarc 					ttyflush(tp, FWRITE);
83335811Smarc 					ttyecho(c, tp);
83444419Smarc 					if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
83544419Smarc 						ttyretype(tp);
83644419Smarc 					tp->t_lflag |= FLUSHO;
83744419Smarc 				}
83844419Smarc 				goto startoutput;
83935811Smarc 			}
8409578Ssam 		}
84144419Smarc 		/*
84244419Smarc 		 * Signals.
84344419Smarc 		 */
84444419Smarc 		if (lflag&ISIG) {
84544419Smarc 			if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
84644419Smarc 				if ((lflag&NOFLSH) == 0)
84744419Smarc 					ttyflush(tp, FREAD|FWRITE);
8487502Sroot 				ttyecho(c, tp);
84944419Smarc 				pgsignal(tp->t_pgrp,
85044419Smarc 				    CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
85144419Smarc 				goto endcase;
8527502Sroot 			}
85344419Smarc 			if (CCEQ(cc[VSUSP], c)) {
85444419Smarc 				if ((lflag&NOFLSH) == 0)
85544419Smarc 					ttyflush(tp, FREAD);
85644419Smarc 				ttyecho(c, tp);
85744419Smarc 				pgsignal(tp->t_pgrp, SIGTSTP, 1);
85844419Smarc 				goto endcase;
85944419Smarc 			}
8609578Ssam 		}
86144419Smarc 		/*
86244419Smarc 		 * Handle start/stop characters.
86344419Smarc 		 */
86444419Smarc 		if (iflag&IXON) {
86544419Smarc 			if (CCEQ(cc[VSTOP], c)) {
86644419Smarc 				if ((tp->t_state&TS_TTSTOP) == 0) {
86744419Smarc 					tp->t_state |= TS_TTSTOP;
86844419Smarc 					(*cdevsw[major(tp->t_dev)].d_stop)(tp,
86944419Smarc 					   0);
87044419Smarc 					return;
87144419Smarc 				}
87244419Smarc 				if (!CCEQ(cc[VSTART], c))
87344419Smarc 					return;
87444419Smarc 				/*
87544419Smarc 				 * if VSTART == VSTOP then toggle
87644419Smarc 				 */
87744419Smarc 				goto endcase;
87835811Smarc 			}
87944419Smarc 			if (CCEQ(cc[VSTART], c))
88044419Smarc 				goto restartoutput;
8819578Ssam 		}
88244419Smarc 		/*
88344419Smarc 		 * IGNCR, ICRNL, & INLCR
88444419Smarc 		 */
88544419Smarc 		if (c == '\r') {
88644419Smarc 			if (iflag&IGNCR)
88744419Smarc 				goto endcase;
88844419Smarc 			else if (iflag&ICRNL)
88944419Smarc 				c = '\n';
89044419Smarc 		} else if (c == '\n' && iflag&INLCR)
89144419Smarc 			c = '\r';
8929578Ssam 	}
89347545Skarels 	if ((tp->t_lflag&EXTPROC) == 0 && lflag&ICANON) {
89444419Smarc 		/*
89544419Smarc 		 * From here on down canonical mode character
89644419Smarc 		 * processing takes place.
89744419Smarc 		 */
89844419Smarc 		/*
89944419Smarc 		 * erase (^H / ^?)
90044419Smarc 		 */
90144419Smarc 		if (CCEQ(cc[VERASE], c)) {
90244419Smarc 			if (tp->t_rawq.c_cc)
9039578Ssam 				ttyrub(unputc(&tp->t_rawq), tp);
90444419Smarc 			goto endcase;
9059578Ssam 		}
90644419Smarc 		/*
90744419Smarc 		 * kill (^U)
90844419Smarc 		 */
90944419Smarc 		if (CCEQ(cc[VKILL], c)) {
91044419Smarc 			if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount &&
91144419Smarc 			    (lflag&ECHOPRT) == 0) {
91244419Smarc 				while (tp->t_rawq.c_cc)
91344419Smarc 					ttyrub(unputc(&tp->t_rawq), tp);
91444419Smarc 			} else {
91544419Smarc 				ttyecho(c, tp);
91644419Smarc 				if (lflag&ECHOK || lflag&ECHOKE)
91744419Smarc 					ttyecho('\n', tp);
91844419Smarc 				while (getc(&tp->t_rawq) > 0)
91944419Smarc 					;
92044419Smarc 				tp->t_rocount = 0;
92144419Smarc 			}
92244419Smarc 			tp->t_state &= ~TS_LOCAL;
92344419Smarc 			goto endcase;
92444419Smarc 		}
92544419Smarc 		/*
92644419Smarc 		 * word erase (^W)
92744419Smarc 		 */
92844419Smarc 		if (CCEQ(cc[VWERASE], c)) {
92944419Smarc 			int ctype;
93047545Skarels 			int alt = lflag&ALTWERASE;
93135811Smarc 
93244419Smarc 			/*
93344419Smarc 			 * erase whitespace
93444419Smarc 			 */
93544419Smarc 			while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
93644419Smarc 				ttyrub(c, tp);
93744419Smarc 			if (c == -1)
93844419Smarc 				goto endcase;
93944419Smarc 			/*
94047545Skarels 			 * erase last char of word and remember the
94147545Skarels 			 * next chars type (for ALTWERASE)
94244419Smarc 			 */
94335811Smarc 			ttyrub(c, tp);
94444419Smarc 			c = unputc(&tp->t_rawq);
94547545Skarels 			if (c == -1)
94644419Smarc 				goto endcase;
94749380Skarels 			ctype = ISALPHA(c);
94844419Smarc 			/*
94947545Skarels 			 * erase rest of word
95044419Smarc 			 */
95144419Smarc 			do {
95244419Smarc 				ttyrub(c, tp);
95344419Smarc 				c = unputc(&tp->t_rawq);
95444419Smarc 				if (c == -1)
95544419Smarc 					goto endcase;
95647545Skarels 			} while (c != ' ' && c != '\t' &&
95749380Skarels 				(alt == 0 || ISALPHA(c) == ctype));
95844419Smarc 			(void) putc(c, &tp->t_rawq);
95934492Skarels 			goto endcase;
96044419Smarc 		}
96135811Smarc 		/*
96244419Smarc 		 * reprint line (^R)
96335811Smarc 		 */
96444419Smarc 		if (CCEQ(cc[VREPRINT], c)) {
96544419Smarc 			ttyretype(tp);
96634492Skarels 			goto endcase;
96734492Skarels 		}
96835811Smarc 		/*
96944419Smarc 		 * ^T - kernel info and generate SIGINFO
97035811Smarc 		 */
97144419Smarc 		if (CCEQ(cc[VSTATUS], c)) {
97244419Smarc 			pgsignal(tp->t_pgrp, SIGINFO, 1);
97344419Smarc 			if ((lflag&NOKERNINFO) == 0)
97444419Smarc 				ttyinfo(tp);
97544419Smarc 			goto endcase;
97644419Smarc 		}
9779578Ssam 	}
9789578Ssam 	/*
9799578Ssam 	 * Check for input buffer overflow
9809578Ssam 	 */
98147545Skarels 	if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
98235811Smarc 		if (iflag&IMAXBEL) {
98335811Smarc 			if (tp->t_outq.c_cc < tp->t_hiwat)
98435811Smarc 				(void) ttyoutput(CTRL('g'), tp);
98535811Smarc 		} else
98635811Smarc 			ttyflush(tp, FREAD | FWRITE);
9879578Ssam 		goto endcase;
98810391Ssam 	}
9899578Ssam 	/*
9909578Ssam 	 * Put data char in q for user and
9919578Ssam 	 * wakeup on seeing a line delimiter.
9929578Ssam 	 */
9939578Ssam 	if (putc(c, &tp->t_rawq) >= 0) {
99447545Skarels 		if ((lflag&ICANON) == 0) {
99547545Skarels 			ttwakeup(tp);
99647545Skarels 			ttyecho(c, tp);
99747545Skarels 			goto endcase;
99847545Skarels 		}
99935811Smarc 		if (ttbreakc(c)) {
10009578Ssam 			tp->t_rocount = 0;
10019578Ssam 			catq(&tp->t_rawq, &tp->t_canq);
10027502Sroot 			ttwakeup(tp);
10039578Ssam 		} else if (tp->t_rocount++ == 0)
10049578Ssam 			tp->t_rocol = tp->t_col;
10059578Ssam 		if (tp->t_state&TS_ERASE) {
100635811Smarc 			/*
100735811Smarc 			 * end of prterase \.../
100835811Smarc 			 */
10099578Ssam 			tp->t_state &= ~TS_ERASE;
10109578Ssam 			(void) ttyoutput('/', tp);
10119578Ssam 		}
10129578Ssam 		i = tp->t_col;
10137502Sroot 		ttyecho(c, tp);
101435811Smarc 		if (CCEQ(cc[VEOF], c) && lflag&ECHO) {
101535811Smarc 			/*
101635811Smarc 			 * Place the cursor over the '^' of the ^D.
101735811Smarc 			 */
10189578Ssam 			i = MIN(2, tp->t_col - i);
10199578Ssam 			while (i > 0) {
10209578Ssam 				(void) ttyoutput('\b', tp);
10219578Ssam 				i--;
10229578Ssam 			}
10239578Ssam 		}
10247502Sroot 	}
10259578Ssam endcase:
10269578Ssam 	/*
102735811Smarc 	 * IXANY means allow any character to restart output.
10289578Ssam 	 */
102940712Skarels 	if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 &&
103040712Skarels 	    cc[VSTART] != cc[VSTOP])
10317502Sroot 		return;
10329578Ssam restartoutput:
10337502Sroot 	tp->t_state &= ~TS_TTSTOP;
103435811Smarc 	tp->t_lflag &= ~FLUSHO;
10359578Ssam startoutput:
10367502Sroot 	ttstart(tp);
10377502Sroot }
10387502Sroot 
10397502Sroot /*
104049380Skarels  * Output a single character on a tty, doing output processing
104149380Skarels  * as needed (expanding tabs, newline processing, etc.).
104249380Skarels  * Returns < 0 if putc succeeds, otherwise returns char to resend.
10437502Sroot  * Must be recursive.
10447502Sroot  */
10457502Sroot ttyoutput(c, tp)
10467502Sroot 	register c;
10477502Sroot 	register struct tty *tp;
10487502Sroot {
104949380Skarels 	register int col;
105035811Smarc 	register long oflag = tp->t_oflag;
105135811Smarc 
105240712Skarels 	if ((oflag&OPOST) == 0) {
105335811Smarc 		if (tp->t_lflag&FLUSHO)
10547502Sroot 			return (-1);
10557502Sroot 		if (putc(c, &tp->t_outq))
10567625Ssam 			return (c);
10577502Sroot 		tk_nout++;
105835811Smarc 		tp->t_outcc++;
10597502Sroot 		return (-1);
10607502Sroot 	}
106135811Smarc 	c &= TTY_CHARMASK;
10627502Sroot 	/*
106349380Skarels 	 * Do tab expansion if OXTABS is set.
106442882Smarc 	 * Special case if we have external processing, we don't
106542882Smarc 	 * do the tab expansion because we'll probably get it
106642882Smarc 	 * wrong.  If tab expansion needs to be done, let it
106742882Smarc 	 * happen externally.
10687502Sroot 	 */
106947545Skarels 	if (c == '\t' && oflag&OXTABS && (tp->t_lflag&EXTPROC) == 0) {
10707502Sroot 		register int s;
10717502Sroot 
10727502Sroot 		c = 8 - (tp->t_col&7);
107335811Smarc 		if ((tp->t_lflag&FLUSHO) == 0) {
107417545Skarels 			s = spltty();		/* don't interrupt tabs */
10757502Sroot 			c -= b_to_q("        ", c, &tp->t_outq);
10767502Sroot 			tk_nout += c;
107735811Smarc 			tp->t_outcc += c;
10787502Sroot 			splx(s);
10797502Sroot 		}
10807502Sroot 		tp->t_col += c;
10817502Sroot 		return (c ? -1 : '\t');
10827502Sroot 	}
108335811Smarc 	if (c == CEOT && oflag&ONOEOT)
108447545Skarels 		return (-1);
10857502Sroot 	tk_nout++;
108635811Smarc 	tp->t_outcc++;
10877502Sroot 	/*
108849380Skarels 	 * Newline translation: if ONLCR is set,
108949380Skarels 	 * translate newline into "\r\n".
10907502Sroot 	 */
109135811Smarc 	if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0)
10927502Sroot 		return (c);
109335811Smarc 	if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
109435811Smarc 		return (c);
109547545Skarels 
109649380Skarels 	col = tp->t_col;
109749380Skarels 	switch (CCLASS(c)) {
10987502Sroot 
10997502Sroot 	case ORDINARY:
110049380Skarels 		col++;
11017502Sroot 
11027502Sroot 	case CONTROL:
11037502Sroot 		break;
11047502Sroot 
11057502Sroot 	case BACKSPACE:
110649380Skarels 		if (col > 0)
110749380Skarels 			col--;
11087502Sroot 		break;
11097502Sroot 
11107502Sroot 	case NEWLINE:
111149380Skarels 		col = 0;
11127502Sroot 		break;
11137502Sroot 
11147502Sroot 	case TAB:
111549380Skarels 		col = (col + 8) &~ 0x7;
11167502Sroot 		break;
11177502Sroot 
11187502Sroot 	case RETURN:
111949380Skarels 		col = 0;
11207502Sroot 	}
112149380Skarels 	tp->t_col = col;
11227502Sroot 	return (-1);
11237502Sroot }
11247502Sroot 
11257502Sroot /*
112649380Skarels  * Process a read call on a tty device.
11277502Sroot  */
112837584Smarc ttread(tp, uio, flag)
11297625Ssam 	register struct tty *tp;
11307722Swnj 	struct uio *uio;
11317502Sroot {
11327502Sroot 	register struct clist *qp;
113335811Smarc 	register int c;
113441383Smarc 	register long lflag;
113535811Smarc 	register u_char *cc = tp->t_cc;
113647545Skarels 	register struct proc *p = curproc;
11379859Ssam 	int s, first, error = 0;
11387502Sroot 
11397502Sroot loop:
114041383Smarc 	lflag = tp->t_lflag;
114137584Smarc 	s = spltty();
11429578Ssam 	/*
114337584Smarc 	 * take pending input first
11449578Ssam 	 */
114535811Smarc 	if (lflag&PENDIN)
11467502Sroot 		ttypend(tp);
11479859Ssam 	splx(s);
114840712Skarels 
11499578Ssam 	/*
11509578Ssam 	 * Hang process if it's in the background.
11519578Ssam 	 */
115247545Skarels 	if (isbackground(p, tp)) {
115347545Skarels 		if ((p->p_sigignore & sigmask(SIGTTIN)) ||
115447545Skarels 		   (p->p_sigmask & sigmask(SIGTTIN)) ||
115547545Skarels 		    p->p_flag&SPPWAIT || p->p_pgrp->pg_jobc == 0)
11568520Sroot 			return (EIO);
115747545Skarels 		pgsignal(p->p_pgrp, SIGTTIN, 1);
115843377Smarc 		if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH,
115943377Smarc 		    ttybg, 0))
116040712Skarels 			return (error);
116123165Sbloom 		goto loop;
11627502Sroot 	}
116340712Skarels 
11649578Ssam 	/*
116535811Smarc 	 * If canonical, use the canonical queue,
116635811Smarc 	 * else use the raw queue.
116737584Smarc 	 *
116847545Skarels 	 * (should get rid of clists...)
11699578Ssam 	 */
117035811Smarc 	qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq;
117140712Skarels 
11729578Ssam 	/*
117340712Skarels 	 * If there is no input, sleep on rawq
117440712Skarels 	 * awaiting hardware receipt and notification.
117540712Skarels 	 * If we have data, we don't need to check for carrier.
11769578Ssam 	 */
117717545Skarels 	s = spltty();
11789578Ssam 	if (qp->c_cc <= 0) {
117940712Skarels 		int carrier;
118040712Skarels 
118140712Skarels 		carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL);
118240712Skarels 		if (!carrier && tp->t_state&TS_ISOPEN) {
11839859Ssam 			splx(s);
118440712Skarels 			return (0);	/* EOF */
11857502Sroot 		}
118637728Smckusick 		if (flag & IO_NDELAY) {
118737584Smarc 			splx(s);
118837584Smarc 			return (EWOULDBLOCK);
118937584Smarc 		}
119043377Smarc 		error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
119140712Skarels 		    carrier ? ttyin : ttopen, 0);
11929859Ssam 		splx(s);
119343377Smarc 		if (error)
119440712Skarels 			return (error);
11959578Ssam 		goto loop;
11969578Ssam 	}
11979859Ssam 	splx(s);
119840712Skarels 
11999578Ssam 	/*
120035811Smarc 	 * Input present, check for input mapping and processing.
12019578Ssam 	 */
12029578Ssam 	first = 1;
12039578Ssam 	while ((c = getc(qp)) >= 0) {
12049578Ssam 		/*
120535811Smarc 		 * delayed suspend (^Y)
12069578Ssam 		 */
120735811Smarc 		if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) {
120842882Smarc 			pgsignal(tp->t_pgrp, SIGTSTP, 1);
12099578Ssam 			if (first) {
121043377Smarc 				if (error = ttysleep(tp, (caddr_t)&lbolt,
121143377Smarc 				    TTIPRI | PCATCH, ttybg, 0))
121240712Skarels 					break;
12139578Ssam 				goto loop;
12149578Ssam 			}
12159578Ssam 			break;
12167502Sroot 		}
12179578Ssam 		/*
121835811Smarc 		 * Interpret EOF only in canonical mode.
12199578Ssam 		 */
122035811Smarc 		if (CCEQ(cc[VEOF], c) && lflag&ICANON)
12219578Ssam 			break;
12229578Ssam 		/*
12239578Ssam 		 * Give user character.
12249578Ssam 		 */
122540712Skarels  		error = ureadc(c, uio);
12269578Ssam 		if (error)
12279578Ssam 			break;
122814938Smckusick  		if (uio->uio_resid == 0)
12299578Ssam 			break;
12309578Ssam 		/*
123135811Smarc 		 * In canonical mode check for a "break character"
12329578Ssam 		 * marking the end of a "line of input".
12339578Ssam 		 */
123440712Skarels 		if (lflag&ICANON && ttbreakc(c))
12359578Ssam 			break;
12369578Ssam 		first = 0;
12377502Sroot 	}
12389578Ssam 	/*
12399578Ssam 	 * Look to unblock output now that (presumably)
12409578Ssam 	 * the input queue has gone down.
12419578Ssam 	 */
124235811Smarc 	if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
124347545Skarels 		if (cc[VSTART] != _POSIX_VDISABLE &&
124447545Skarels 		    putc(cc[VSTART], &tp->t_outq) == 0) {
12457502Sroot 			tp->t_state &= ~TS_TBLOCK;
12467502Sroot 			ttstart(tp);
12477502Sroot 		}
124835811Smarc 	}
12498520Sroot 	return (error);
12507502Sroot }
12517502Sroot 
12527502Sroot /*
125325391Skarels  * Check the output queue on tp for space for a kernel message
125425391Skarels  * (from uprintf/tprintf).  Allow some space over the normal
125525391Skarels  * hiwater mark so we don't lose messages due to normal flow
125625391Skarels  * control, but don't let the tty run amok.
125730695Skarels  * Sleeps here are not interruptible, but we return prematurely
125830695Skarels  * if new signals come in.
125925391Skarels  */
126025391Skarels ttycheckoutq(tp, wait)
126125391Skarels 	register struct tty *tp;
126225391Skarels 	int wait;
126325391Skarels {
126430695Skarels 	int hiwat, s, oldsig;
126548439Skarels 	extern int wakeup();
126625391Skarels 
126735811Smarc 	hiwat = tp->t_hiwat;
126825391Skarels 	s = spltty();
126947545Skarels 	oldsig = curproc->p_sig;
127025391Skarels 	if (tp->t_outq.c_cc > hiwat + 200)
127129946Skarels 		while (tp->t_outq.c_cc > hiwat) {
127229946Skarels 			ttstart(tp);
127347545Skarels 			if (wait == 0 || curproc->p_sig != oldsig) {
127429946Skarels 				splx(s);
127529946Skarels 				return (0);
127629946Skarels 			}
127730695Skarels 			timeout(wakeup, (caddr_t)&tp->t_outq, hz);
127829946Skarels 			tp->t_state |= TS_ASLEEP;
127930695Skarels 			sleep((caddr_t)&tp->t_outq, PZERO - 1);
128025391Skarels 		}
128125391Skarels 	splx(s);
128225391Skarels 	return (1);
128325391Skarels }
128425391Skarels 
128525391Skarels /*
128649380Skarels  * Process a write call on a tty device.
12877502Sroot  */
128837584Smarc ttwrite(tp, uio, flag)
12897625Ssam 	register struct tty *tp;
12909578Ssam 	register struct uio *uio;
12917502Sroot {
12927502Sroot 	register char *cp;
129340712Skarels 	register int cc = 0, ce;
129447545Skarels 	register struct proc *p = curproc;
12959578Ssam 	int i, hiwat, cnt, error, s;
12967502Sroot 	char obuf[OBUFSIZ];
12977502Sroot 
129835811Smarc 	hiwat = tp->t_hiwat;
12999578Ssam 	cnt = uio->uio_resid;
13009578Ssam 	error = 0;
13017502Sroot loop:
130237584Smarc 	s = spltty();
130340712Skarels 	if ((tp->t_state&TS_CARR_ON) == 0 && (tp->t_cflag&CLOCAL) == 0) {
130437584Smarc 		if (tp->t_state&TS_ISOPEN) {
130537584Smarc 			splx(s);
130637584Smarc 			return (EIO);
130737728Smckusick 		} else if (flag & IO_NDELAY) {
130837584Smarc 			splx(s);
130940712Skarels 			error = EWOULDBLOCK;
131040712Skarels 			goto out;
131137584Smarc 		} else {
131237584Smarc 			/*
131337584Smarc 			 * sleep awaiting carrier
131437584Smarc 			 */
131543377Smarc 			error = ttysleep(tp, (caddr_t)&tp->t_rawq,
131643377Smarc 					TTIPRI | PCATCH,ttopen, 0);
131737584Smarc 			splx(s);
131843377Smarc 			if (error)
131940712Skarels 				goto out;
132037584Smarc 			goto loop;
132137584Smarc 		}
132237584Smarc 	}
132337584Smarc 	splx(s);
13249578Ssam 	/*
13259578Ssam 	 * Hang the process if it's in the background.
13269578Ssam 	 */
132747545Skarels 	if (isbackground(p, tp) &&
132847545Skarels 	    tp->t_lflag&TOSTOP && (p->p_flag&SPPWAIT) == 0 &&
132947545Skarels 	    (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
133047545Skarels 	    (p->p_sigmask & sigmask(SIGTTOU)) == 0 &&
133147545Skarels 	     p->p_pgrp->pg_jobc) {
133247545Skarels 		pgsignal(p->p_pgrp, SIGTTOU, 1);
133343377Smarc 		if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH,
133443377Smarc 		    ttybg, 0))
133540712Skarels 			goto out;
133621776Sbloom 		goto loop;
13377502Sroot 	}
13389578Ssam 	/*
13399578Ssam 	 * Process the user's data in at most OBUFSIZ
134040712Skarels 	 * chunks.  Perform any output translation.
134140712Skarels 	 * Keep track of high water mark, sleep on overflow
134240712Skarels 	 * awaiting device aid in acquiring new space.
13439578Ssam 	 */
134440712Skarels 	while (uio->uio_resid > 0 || cc > 0) {
134540712Skarels 		if (tp->t_lflag&FLUSHO) {
134640712Skarels 			uio->uio_resid = 0;
134740712Skarels 			return (0);
134840712Skarels 		}
134940712Skarels 		if (tp->t_outq.c_cc > hiwat)
135032067Skarels 			goto ovhiwat;
13519578Ssam 		/*
135240712Skarels 		 * Grab a hunk of data from the user,
135340712Skarels 		 * unless we have some leftover from last time.
13549578Ssam 		 */
13557822Sroot 		if (cc == 0) {
135640712Skarels 			cc = min(uio->uio_resid, OBUFSIZ);
135740712Skarels 			cp = obuf;
135840712Skarels 			error = uiomove(cp, cc, uio);
135940712Skarels 			if (error) {
136040712Skarels 				cc = 0;
136140712Skarels 				break;
136240712Skarels 			}
13637822Sroot 		}
13649578Ssam 		/*
13659578Ssam 		 * If nothing fancy need be done, grab those characters we
13669578Ssam 		 * can handle without any of ttyoutput's processing and
13679578Ssam 		 * just transfer them to the output q.  For those chars
13689578Ssam 		 * which require special processing (as indicated by the
13699578Ssam 		 * bits in partab), call ttyoutput.  After processing
13709578Ssam 		 * a hunk of data, look for FLUSHO so ^O's will take effect
13719578Ssam 		 * immediately.
13729578Ssam 		 */
13739578Ssam 		while (cc > 0) {
137440712Skarels 			if ((tp->t_oflag&OPOST) == 0)
13757502Sroot 				ce = cc;
13767502Sroot 			else {
137734492Skarels 				ce = cc - scanc((unsigned)cc, (u_char *)cp,
137849380Skarels 				   (u_char *)partab, CCLASSMASK);
13799578Ssam 				/*
13809578Ssam 				 * If ce is zero, then we're processing
13819578Ssam 				 * a special character through ttyoutput.
13829578Ssam 				 */
13839578Ssam 				if (ce == 0) {
13847502Sroot 					tp->t_rocount = 0;
13857502Sroot 					if (ttyoutput(*cp, tp) >= 0) {
138621776Sbloom 					    /* no c-lists, wait a bit */
138721776Sbloom 					    ttstart(tp);
138843377Smarc 					    if (error = ttysleep(tp,
138943377Smarc 						(caddr_t)&lbolt,
139043377Smarc 						 TTOPRI | PCATCH, ttybuf, 0))
139140712Skarels 						    break;
139221776Sbloom 					    goto loop;
13937502Sroot 					}
13949578Ssam 					cp++, cc--;
139535811Smarc 					if ((tp->t_lflag&FLUSHO) ||
13969578Ssam 					    tp->t_outq.c_cc > hiwat)
13977502Sroot 						goto ovhiwat;
13989578Ssam 					continue;
13997502Sroot 				}
14007502Sroot 			}
14019578Ssam 			/*
14029578Ssam 			 * A bunch of normal characters have been found,
14039578Ssam 			 * transfer them en masse to the output queue and
14049578Ssam 			 * continue processing at the top of the loop.
14059578Ssam 			 * If there are any further characters in this
14069578Ssam 			 * <= OBUFSIZ chunk, the first should be a character
14079578Ssam 			 * requiring special handling by ttyoutput.
14089578Ssam 			 */
14097502Sroot 			tp->t_rocount = 0;
14109578Ssam 			i = b_to_q(cp, ce, &tp->t_outq);
14119578Ssam 			ce -= i;
14129578Ssam 			tp->t_col += ce;
14139578Ssam 			cp += ce, cc -= ce, tk_nout += ce;
141435811Smarc 			tp->t_outcc += ce;
14159578Ssam 			if (i > 0) {
14169578Ssam 				/* out of c-lists, wait a bit */
14177502Sroot 				ttstart(tp);
141843377Smarc 				if (error = ttysleep(tp, (caddr_t)&lbolt,
141943377Smarc 					    TTOPRI | PCATCH, ttybuf, 0))
142040712Skarels 					break;
142121776Sbloom 				goto loop;
14227502Sroot 			}
142335811Smarc 			if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat)
142440712Skarels 				break;
14257502Sroot 		}
142635811Smarc 		ttstart(tp);
14277502Sroot 	}
142840712Skarels out:
142940712Skarels 	/*
143040712Skarels 	 * If cc is nonzero, we leave the uio structure inconsistent,
143140712Skarels 	 * as the offset and iov pointers have moved forward,
143240712Skarels 	 * but it doesn't matter (the call will either return short
143340712Skarels 	 * or restart with a new uio).
143440712Skarels 	 */
143540712Skarels 	uio->uio_resid += cc;
14368520Sroot 	return (error);
143740712Skarels 
14387502Sroot ovhiwat:
143932067Skarels 	ttstart(tp);
144032067Skarels 	s = spltty();
14419578Ssam 	/*
144235811Smarc 	 * This can only occur if FLUSHO is set in t_lflag,
144332067Skarels 	 * or if ttstart/oproc is synchronous (or very fast).
14449578Ssam 	 */
14457502Sroot 	if (tp->t_outq.c_cc <= hiwat) {
14469578Ssam 		splx(s);
14477502Sroot 		goto loop;
14487502Sroot 	}
144937728Smckusick 	if (flag & IO_NDELAY) {
145017545Skarels 		splx(s);
145140712Skarels 		uio->uio_resid += cc;
14527822Sroot 		if (uio->uio_resid == cnt)
14538520Sroot 			return (EWOULDBLOCK);
14548520Sroot 		return (0);
14557502Sroot 	}
14567502Sroot 	tp->t_state |= TS_ASLEEP;
145743377Smarc 	error = ttysleep(tp, (caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
14589578Ssam 	splx(s);
145943377Smarc 	if (error)
146040712Skarels 		goto out;
14617502Sroot 	goto loop;
14627502Sroot }
14637502Sroot 
14647502Sroot /*
14657502Sroot  * Rubout one character from the rawq of tp
14667502Sroot  * as cleanly as possible.
14677502Sroot  */
14687502Sroot ttyrub(c, tp)
14697625Ssam 	register c;
14707625Ssam 	register struct tty *tp;
14717502Sroot {
14727502Sroot 	register char *cp;
14737502Sroot 	register int savecol;
14747502Sroot 	int s;
14757502Sroot 	char *nextc();
14767502Sroot 
147742882Smarc 	if ((tp->t_lflag&ECHO) == 0 || (tp->t_lflag&EXTPROC))
14787502Sroot 		return;
147935811Smarc 	tp->t_lflag &= ~FLUSHO;
148035811Smarc 	if (tp->t_lflag&ECHOE) {
14817502Sroot 		if (tp->t_rocount == 0) {
14827502Sroot 			/*
14837502Sroot 			 * Screwed by ttwrite; retype
14847502Sroot 			 */
14857502Sroot 			ttyretype(tp);
14867502Sroot 			return;
14877502Sroot 		}
148835811Smarc 		if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE))
14897502Sroot 			ttyrubo(tp, 2);
149049380Skarels 		else switch (CCLASS(c &= TTY_CHARMASK)) {
14917502Sroot 
14927502Sroot 		case ORDINARY:
149335811Smarc 			ttyrubo(tp, 1);
14947502Sroot 			break;
14957502Sroot 
14967502Sroot 		case VTAB:
14977502Sroot 		case BACKSPACE:
14987502Sroot 		case CONTROL:
14997502Sroot 		case RETURN:
150047545Skarels 		case NEWLINE:
150135811Smarc 			if (tp->t_lflag&ECHOCTL)
15027502Sroot 				ttyrubo(tp, 2);
15037502Sroot 			break;
15047502Sroot 
150535811Smarc 		case TAB: {
150635811Smarc 			int c;
150735811Smarc 
15087502Sroot 			if (tp->t_rocount < tp->t_rawq.c_cc) {
15097502Sroot 				ttyretype(tp);
15107502Sroot 				return;
15117502Sroot 			}
151217545Skarels 			s = spltty();
15137502Sroot 			savecol = tp->t_col;
15149578Ssam 			tp->t_state |= TS_CNTTB;
151535811Smarc 			tp->t_lflag |= FLUSHO;
15167502Sroot 			tp->t_col = tp->t_rocol;
15179578Ssam 			cp = tp->t_rawq.c_cf;
151839407Smarc 			if (cp)
151939407Smarc 				c = *cp;	/* XXX FIX NEXTC */
152035811Smarc 			for (; cp; cp = nextc(&tp->t_rawq, cp, &c))
152135811Smarc 				ttyecho(c, tp);
152235811Smarc 			tp->t_lflag &= ~FLUSHO;
15239578Ssam 			tp->t_state &= ~TS_CNTTB;
15247502Sroot 			splx(s);
15257502Sroot 			/*
15267502Sroot 			 * savecol will now be length of the tab
15277502Sroot 			 */
15287502Sroot 			savecol -= tp->t_col;
15297502Sroot 			tp->t_col += savecol;
15307502Sroot 			if (savecol > 8)
15317502Sroot 				savecol = 8;		/* overflow screw */
15327502Sroot 			while (--savecol >= 0)
15337502Sroot 				(void) ttyoutput('\b', tp);
15347502Sroot 			break;
153535811Smarc 		}
15367502Sroot 
15377502Sroot 		default:
153837584Smarc 			/* XXX */
153935811Smarc 			printf("ttyrub: would panic c = %d, val = %d\n",
154049380Skarels 				c, CCLASS(c));
154135811Smarc 			/*panic("ttyrub");*/
15427502Sroot 		}
154335811Smarc 	} else if (tp->t_lflag&ECHOPRT) {
15449578Ssam 		if ((tp->t_state&TS_ERASE) == 0) {
15457502Sroot 			(void) ttyoutput('\\', tp);
15469578Ssam 			tp->t_state |= TS_ERASE;
15477502Sroot 		}
15487502Sroot 		ttyecho(c, tp);
15497502Sroot 	} else
155035811Smarc 		ttyecho(tp->t_cc[VERASE], tp);
15517502Sroot 	tp->t_rocount--;
15527502Sroot }
15537502Sroot 
15547502Sroot /*
15557502Sroot  * Crt back over cnt chars perhaps
15567502Sroot  * erasing them.
15577502Sroot  */
15587502Sroot ttyrubo(tp, cnt)
15597625Ssam 	register struct tty *tp;
15607625Ssam 	int cnt;
15617502Sroot {
15627502Sroot 
15637502Sroot 	while (--cnt >= 0)
156440712Skarels 		ttyoutstr("\b \b", tp);
15657502Sroot }
15667502Sroot 
15677502Sroot /*
15687502Sroot  * Reprint the rawq line.
15697502Sroot  * We assume c_cc has already been checked.
15707502Sroot  */
15717502Sroot ttyretype(tp)
15727625Ssam 	register struct tty *tp;
15737502Sroot {
15747502Sroot 	register char *cp;
15757502Sroot 	char *nextc();
157635811Smarc 	int s, c;
15777502Sroot 
157835811Smarc 	if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
157935811Smarc 		ttyecho(tp->t_cc[VREPRINT], tp);
15807502Sroot 	(void) ttyoutput('\n', tp);
158117545Skarels 	s = spltty();
158235811Smarc 	/*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE
158335811Smarc 	  BIT OF FIRST CHAR ****/
158435811Smarc 	for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) {
158535811Smarc 		ttyecho(c, tp);
158635811Smarc 	}
158735811Smarc 	for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) {
158835811Smarc 		ttyecho(c, tp);
158935811Smarc 	}
15909578Ssam 	tp->t_state &= ~TS_ERASE;
15917502Sroot 	splx(s);
15927502Sroot 	tp->t_rocount = tp->t_rawq.c_cc;
15937502Sroot 	tp->t_rocol = 0;
15947502Sroot }
15957502Sroot 
15967502Sroot /*
159735811Smarc  * Echo a typed character to the terminal.
15987502Sroot  */
15997502Sroot ttyecho(c, tp)
16007625Ssam 	register c;
16017625Ssam 	register struct tty *tp;
16027502Sroot {
16039578Ssam 	if ((tp->t_state&TS_CNTTB) == 0)
160435811Smarc 		tp->t_lflag &= ~FLUSHO;
160547545Skarels 	if (((tp->t_lflag&ECHO) == 0 &&
160647545Skarels 	    ((tp->t_lflag&ECHONL) == 0 || c == '\n')) || (tp->t_lflag&EXTPROC))
16077502Sroot 		return;
160835811Smarc 	if (tp->t_lflag&ECHOCTL) {
160940712Skarels 		if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' ||
161040712Skarels 		    c == 0177) {
16117502Sroot 			(void) ttyoutput('^', tp);
161235811Smarc 			c &= TTY_CHARMASK;
16137502Sroot 			if (c == 0177)
16147502Sroot 				c = '?';
16157502Sroot 			else
16167502Sroot 				c += 'A' - 1;
16177502Sroot 		}
16187502Sroot 	}
161935811Smarc 	(void) ttyoutput(c, tp);
16207502Sroot }
16217502Sroot 
16227502Sroot /*
16237502Sroot  * send string cp to tp
16247502Sroot  */
162540712Skarels ttyoutstr(cp, tp)
16267625Ssam 	register char *cp;
16277625Ssam 	register struct tty *tp;
16287502Sroot {
16297502Sroot 	register char c;
16307502Sroot 
16317502Sroot 	while (c = *cp++)
16327502Sroot 		(void) ttyoutput(c, tp);
16337502Sroot }
16347502Sroot 
163549380Skarels /*
163649380Skarels  * Wake up any readers on a tty.
163749380Skarels  */
16387502Sroot ttwakeup(tp)
163947545Skarels 	register struct tty *tp;
16407502Sroot {
16417502Sroot 
16427502Sroot 	if (tp->t_rsel) {
16437502Sroot 		selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
16447502Sroot 		tp->t_state &= ~TS_RCOLL;
16457502Sroot 		tp->t_rsel = 0;
16467502Sroot 	}
164712752Ssam 	if (tp->t_state & TS_ASYNC)
164842882Smarc 		pgsignal(tp->t_pgrp, SIGIO, 1);
16497502Sroot 	wakeup((caddr_t)&tp->t_rawq);
16507502Sroot }
165135811Smarc 
165235811Smarc /*
165348439Skarels  * Look up a code for a specified speed in a conversion table;
165448439Skarels  * used by drivers to map software speed values to hardware parameters.
165548439Skarels  */
165648439Skarels ttspeedtab(speed, table)
165748439Skarels 	register struct speedtab *table;
165848439Skarels {
165948439Skarels 
166048439Skarels 	for ( ; table->sp_speed != -1; table++)
166148439Skarels 		if (table->sp_speed == speed)
166248439Skarels 			return (table->sp_code);
166348439Skarels 	return (-1);
166448439Skarels }
166548439Skarels 
166648439Skarels /*
166735811Smarc  * set tty hi and low water marks
166835811Smarc  *
166935811Smarc  * Try to arrange the dynamics so there's about one second
167035811Smarc  * from hi to low water.
167135811Smarc  *
167235811Smarc  */
167335811Smarc ttsetwater(tp)
167435811Smarc 	struct tty *tp;
167535811Smarc {
167635811Smarc 	register cps = tp->t_ospeed / 10;
167735811Smarc 	register x;
167835811Smarc 
167935811Smarc #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x))
168035811Smarc 	tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT);
168135811Smarc 	x += cps;
168235811Smarc 	x = clamp(x, TTMAXHIWAT, TTMINHIWAT);
168335811Smarc 	tp->t_hiwat = roundup(x, CBSIZE);
168435811Smarc #undef clamp
168535811Smarc }
168635811Smarc 
168739407Smarc /*
168839407Smarc  * Report on state of foreground process group.
168939407Smarc  */
169039407Smarc ttyinfo(tp)
1691*49907Sbostic 	register struct tty *tp;
169239407Smarc {
1693*49907Sbostic 	register struct proc *p, *pick;
169441177Smarc 	struct timeval utime, stime;
1695*49907Sbostic 	int tmp;
169639407Smarc 
169739407Smarc 	if (ttycheckoutq(tp,0) == 0)
169839407Smarc 		return;
1699*49907Sbostic 
1700*49907Sbostic 	/* Print load average. */
1701*49907Sbostic 	tmp = (averunnable[0] * 100 + FSCALE / 2) >> FSHIFT;
1702*49907Sbostic 	ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1703*49907Sbostic 
170439555Smarc 	if (tp->t_session == NULL)
1705*49907Sbostic 		ttyprintf(tp, "not a controlling terminal\n");
170641177Smarc 	else if (tp->t_pgrp == NULL)
1707*49907Sbostic 		ttyprintf(tp, "no foreground process group\n");
170841177Smarc 	else if ((p = tp->t_pgrp->pg_mem) == NULL)
1709*49907Sbostic 		ttyprintf(tp, "empty foreground process group\n");
171039407Smarc 	else {
1711*49907Sbostic 		/* Pick interesting process. */
1712*49907Sbostic 		for (pick = NULL; p != NULL; p = p->p_pgrpnxt)
171341177Smarc 			if (proc_compare(pick, p))
171441177Smarc 				pick = p;
1715*49907Sbostic 
1716*49907Sbostic 		ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1717*49907Sbostic 		    pick->p_stat == SRUN ? "running" :
1718*49907Sbostic 		    pick->p_wmesg ? pick->p_wmesg : "iowait");
1719*49907Sbostic 
1720*49907Sbostic 		/*
1721*49907Sbostic 		 * Lock out clock if process is running; get user/system
1722*49907Sbostic 		 * cpu time.
172341177Smarc 		 */
172447545Skarels 		if (curproc == pick)
1725*49907Sbostic 			tmp = splclock();
172641177Smarc 		utime = pick->p_utime;
172741177Smarc 		stime = pick->p_stime;
172847545Skarels 		if (curproc == pick)
1729*49907Sbostic 			splx(tmp);
173039407Smarc 
1731*49907Sbostic 		/* Print user time. */
1732*49907Sbostic 		ttyprintf(tp, "%d.%02du ",
1733*49907Sbostic 		    utime.tv_sec, (utime.tv_usec + 5000) / 10000);
173441177Smarc 
1735*49907Sbostic 		/* Print system time. */
1736*49907Sbostic 		ttyprintf(tp, "%d.%02ds ",
1737*49907Sbostic 		    stime.tv_sec, (stime.tv_usec + 5000) / 10000);
1738*49907Sbostic 
1739*49907Sbostic #define	pgtok(a)	(((a) * NBPG) / 1024)
1740*49907Sbostic 		/* Print percentage cpu, resident set size. */
1741*49907Sbostic 		tmp = pick->p_pctcpu * 10000 + FSCALE / 2 >> FSHIFT;
1742*49907Sbostic 		ttyprintf(tp, "%d%% %dk\n",
1743*49907Sbostic 		   tmp / 100, pgtok(pick->p_vmspace->vm_rssize));
174441177Smarc 	}
1745*49907Sbostic 	tp->t_rocount = 0;	/* so pending input will be retyped if BS */
174641177Smarc }
174741177Smarc 
174841177Smarc /*
174941177Smarc  * Returns 1 if p2 is "better" than p1
175041177Smarc  *
175141177Smarc  * The algorithm for picking the "interesting" process is thus:
175241177Smarc  *
175341177Smarc  *	1) (Only foreground processes are eligable - implied)
175441177Smarc  *	2) Runnable processes are favored over anything
175541177Smarc  *	   else.  The runner with the highest cpu
175641177Smarc  *	   utilization is picked (p_cpu).  Ties are
175741177Smarc  *	   broken by picking the highest pid.
175841177Smarc  *	3  Next, the sleeper with the shortest sleep
175941177Smarc  *	   time is favored.  With ties, we pick out
176041177Smarc  *	   just "short-term" sleepers (SSINTR == 0).
176141177Smarc  *	   Further ties are broken by picking the highest
176241177Smarc  *	   pid.
176341177Smarc  *
176441177Smarc  */
176541177Smarc #define isrun(p)	(((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
176645723Smckusick #define TESTAB(a, b)    ((a)<<1 | (b))
176745723Smckusick #define ONLYA   2
176845723Smckusick #define ONLYB   1
176945723Smckusick #define BOTH    3
177045723Smckusick 
1771*49907Sbostic static int
177241177Smarc proc_compare(p1, p2)
177341177Smarc 	register struct proc *p1, *p2;
177441177Smarc {
177541177Smarc 
177641177Smarc 	if (p1 == NULL)
177741177Smarc 		return (1);
177841177Smarc 	/*
177941177Smarc 	 * see if at least one of them is runnable
178041177Smarc 	 */
178145723Smckusick 	switch (TESTAB(isrun(p1), isrun(p2))) {
178245723Smckusick 	case ONLYA:
178345723Smckusick 		return (0);
178445723Smckusick 	case ONLYB:
178541177Smarc 		return (1);
178645723Smckusick 	case BOTH:
178741177Smarc 		/*
178841177Smarc 		 * tie - favor one with highest recent cpu utilization
178941177Smarc 		 */
179041177Smarc 		if (p2->p_cpu > p1->p_cpu)
179141177Smarc 			return (1);
179241177Smarc 		if (p1->p_cpu > p2->p_cpu)
179341177Smarc 			return (0);
179441177Smarc 		return (p2->p_pid > p1->p_pid);	/* tie - return highest pid */
179541177Smarc 	}
179645723Smckusick 	/*
179745723Smckusick  	 * weed out zombies
179845723Smckusick 	 */
179945723Smckusick 	switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
180045723Smckusick 	case ONLYA:
180145723Smckusick 		return (1);
180245723Smckusick 	case ONLYB:
180345723Smckusick 		return (0);
180445723Smckusick 	case BOTH:
180545723Smckusick 		return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
180645723Smckusick 	}
180741177Smarc 	/*
180841177Smarc 	 * pick the one with the smallest sleep time
180941177Smarc 	 */
181041177Smarc 	if (p2->p_slptime > p1->p_slptime)
181141177Smarc 		return (0);
181241177Smarc 	if (p1->p_slptime > p2->p_slptime)
181341177Smarc 		return (1);
181441177Smarc 	/*
181541177Smarc 	 * favor one sleeping in a non-interruptible sleep
181641177Smarc 	 */
181741177Smarc 	if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0)
181841177Smarc 		return (1);
181941177Smarc 	if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0)
182041177Smarc 		return (0);
182147545Skarels 	return (p2->p_pid > p1->p_pid);		/* tie - return highest pid */
182241177Smarc }
182345723Smckusick 
182439555Smarc /*
182539555Smarc  * Output char to tty; console putchar style.
182639555Smarc  */
182739555Smarc tputchar(c, tp)
182839555Smarc 	int c;
182939555Smarc 	struct tty *tp;
183039555Smarc {
183139555Smarc 	register s = spltty();
183239555Smarc 
183347545Skarels 	if ((tp->t_state & (TS_CARR_ON|TS_ISOPEN)) == (TS_CARR_ON|TS_ISOPEN)) {
183439555Smarc 		if (c == '\n')
183539555Smarc 			(void) ttyoutput('\r', tp);
183639555Smarc 		(void) ttyoutput(c, tp);
183739555Smarc 		ttstart(tp);
183839555Smarc 		splx(s);
183939555Smarc 		return (0);
184039555Smarc 	}
184139555Smarc 	splx(s);
184239555Smarc 	return (-1);
184339555Smarc }
184443377Smarc 
184544419Smarc /*
184649380Skarels  * Sleep on chan, returning ERESTART if tty changed
184749380Skarels  * while we napped and returning any errors (e.g. EINTR/ETIMEDOUT)
184849380Skarels  * reported by tsleep.  If the tty is revoked, restarting a pending
184949380Skarels  * call will redo validation done at the start of the call.
185044419Smarc  */
185143377Smarc ttysleep(tp, chan, pri, wmesg, timo)
185243377Smarc 	struct tty *tp;
185343377Smarc 	caddr_t chan;
185443377Smarc 	int pri;
185543377Smarc 	char *wmesg;
185643377Smarc 	int timo;
185743377Smarc {
185843377Smarc 	int error;
185943377Smarc 	short gen = tp->t_gen;
186043377Smarc 
186143377Smarc 	if (error = tsleep(chan, pri, wmesg, timo))
186243377Smarc 		return (error);
186343377Smarc 	if (tp->t_gen != gen)
186443377Smarc 		return (ERESTART);
186543377Smarc 	return (0);
186643377Smarc }
1867