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