mpcc ports hang with TS_BUSY; bug report 4.3BSD-tahoe/sys/23
[unix-history] / usr / src / sys / kern / tty.c
CommitLineData
da7c5cc6 1/*
0880b18e 2 * Copyright (c) 1982, 1986 Regents of the University of California.
da7c5cc6
KM
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 *
3f36033f 6 * @(#)tty.c 7.12 (Berkeley) %G%
da7c5cc6 7 */
961945a8
SL
8
9#include "../machine/reg.h"
89dc6dfb 10
94368568
JB
11#include "param.h"
12#include "systm.h"
13#include "dir.h"
14#include "user.h"
15#include "ioctl.h"
16#include "tty.h"
2c6a4e11
MT
17#define TTYDEFCHARS
18#include "ttydefaults.h"
19#undef TTYDEFCHARS
20#include "termios.h"
94368568 21#include "proc.h"
94368568
JB
22#include "file.h"
23#include "conf.h"
fb1db32c 24#include "dkstat.h"
94368568
JB
25#include "uio.h"
26#include "kernel.h"
2c6a4e11 27#include "syslog.h"
89dc6dfb 28
8bba2b8f
RE
29/*
30 * Table giving parity for characters and indicating
31 * character classes to tty driver. In particular,
32 * if the low 6 bits are 0, then the character needs
33 * no special processing on output.
34 */
35
36char partab[] = {
37 0001,0201,0201,0001,0201,0001,0001,0201,
38 0202,0004,0003,0201,0005,0206,0201,0001,
39 0201,0001,0001,0201,0001,0201,0201,0001,
40 0001,0201,0201,0001,0201,0001,0001,0201,
41 0200,0000,0000,0200,0000,0200,0200,0000,
42 0000,0200,0200,0000,0200,0000,0000,0200,
43 0000,0200,0200,0000,0200,0000,0000,0200,
44 0200,0000,0000,0200,0000,0200,0200,0000,
45 0200,0000,0000,0200,0000,0200,0200,0000,
46 0000,0200,0200,0000,0200,0000,0000,0200,
47 0000,0200,0200,0000,0200,0000,0000,0200,
48 0200,0000,0000,0200,0000,0200,0200,0000,
49 0000,0200,0200,0000,0200,0000,0000,0200,
50 0200,0000,0000,0200,0000,0200,0200,0000,
51 0200,0000,0000,0200,0000,0200,0200,0000,
52 0000,0200,0200,0000,0200,0000,0000,0201,
53
54 /*
55 * 7 bit ascii ends with the last character above,
56 * but we contine through all 256 codes for the sake
57 * of the tty output routines which use special vax
58 * instructions which need a 256 character trt table.
59 */
60
61 0007,0007,0007,0007,0007,0007,0007,0007,
62 0007,0007,0007,0007,0007,0007,0007,0007,
63 0007,0007,0007,0007,0007,0007,0007,0007,
64 0007,0007,0007,0007,0007,0007,0007,0007,
65 0007,0007,0007,0007,0007,0007,0007,0007,
66 0007,0007,0007,0007,0007,0007,0007,0007,
67 0007,0007,0007,0007,0007,0007,0007,0007,
68 0007,0007,0007,0007,0007,0007,0007,0007,
69 0007,0007,0007,0007,0007,0007,0007,0007,
70 0007,0007,0007,0007,0007,0007,0007,0007,
71 0007,0007,0007,0007,0007,0007,0007,0007,
72 0007,0007,0007,0007,0007,0007,0007,0007,
73 0007,0007,0007,0007,0007,0007,0007,0007,
74 0007,0007,0007,0007,0007,0007,0007,0007,
75 0007,0007,0007,0007,0007,0007,0007,0007,
76 0007,0007,0007,0007,0007,0007,0007,0007
77};
89dc6dfb 78
89dc6dfb
BJ
79/*
80 * Input mapping table-- if an entry is non-zero, when the
81 * corresponding character is typed preceded by "\" the escape
82 * sequence is replaced by the table value. Mostly used for
83 * upper-case only terminals.
84 */
89dc6dfb
BJ
85char maptab[] ={
86 000,000,000,000,000,000,000,000,
87 000,000,000,000,000,000,000,000,
88 000,000,000,000,000,000,000,000,
89 000,000,000,000,000,000,000,000,
90 000,'|',000,000,000,000,000,'`',
91 '{','}',000,000,000,000,000,000,
92 000,000,000,000,000,000,000,000,
93 000,000,000,000,000,000,000,000,
94 000,000,000,000,000,000,000,000,
95 000,000,000,000,000,000,000,000,
96 000,000,000,000,000,000,000,000,
97 000,000,000,000,000,000,'~',000,
98 000,'A','B','C','D','E','F','G',
99 'H','I','J','K','L','M','N','O',
100 'P','Q','R','S','T','U','V','W',
101 'X','Y','Z',000,000,000,000,000,
102};
103
0d65848d 104short tthiwat[16] =
6f21eede 105 { 100,100,100,100,100,100,100,200,200,400,400,400,650,650,1300,2000 };
0d65848d
BJ
106short ttlowat[16] =
107 { 30, 30, 30, 30, 30, 30, 30, 50, 50,120,120,120,125,125,125,125 };
108
adf4bc89 109extern struct tty *constty; /* temporary virtual console */
2c6a4e11
MT
110extern char partab[], maptab[];
111
112/*
113 * Debugging aids
114 */
115#define dprintf if (tp->t_trace & TTRACE_IO)printf
116
117/*
118 * Is 'c' a line delimiter ("break" character)?
119 */
120#define ttbreakc(c) (c == '\n' || CCEQ(cc[VEOF], c) || \
121 CCEQ(cc[VEOL], c) || CCEQ(cc[VEOL2], c))
adf4bc89 122
89dc6dfb 123ttychars(tp)
becb115c 124 struct tty *tp;
89dc6dfb 125{
2c6a4e11 126 bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars));
89dc6dfb
BJ
127}
128
129/*
2c6a4e11 130 *
50e2732b 131 * Wait for output to drain, then flush input waiting.
89dc6dfb 132 */
88a7a62a 133ttywflush(tp)
941944c9 134 register struct tty *tp;
89dc6dfb
BJ
135{
136
88a7a62a
SL
137 ttywait(tp);
138 ttyflush(tp, FREAD);
139}
140
2c6a4e11
MT
141/*
142 * Wait for output to drain.
143 */
88a7a62a
SL
144ttywait(tp)
145 register struct tty *tp;
146{
4f3a716e 147 register int s = spltty();
88a7a62a 148
5de287d9 149 while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) &&
9670d122 150 tp->t_state&TS_CARR_ON && tp->t_oproc) {
50e2732b 151 (*tp->t_oproc)(tp);
941944c9 152 tp->t_state |= TS_ASLEEP;
50e2732b
BJ
153 sleep((caddr_t)&tp->t_outq, TTOPRI);
154 }
875a1e2c 155 splx(s);
89dc6dfb
BJ
156}
157
158/*
becb115c 159 * Flush all TTY queues
89dc6dfb 160 */
88a7a62a 161ttyflush(tp, rw)
4b72e2f9 162 register struct tty *tp;
89dc6dfb 163{
50e2732b
BJ
164 register s;
165
4f3a716e 166 s = spltty();
50e2732b
BJ
167 if (rw & FREAD) {
168 while (getc(&tp->t_canq) >= 0)
169 ;
170 wakeup((caddr_t)&tp->t_rawq);
171 }
172 if (rw & FWRITE) {
173 wakeup((caddr_t)&tp->t_outq);
941944c9 174 tp->t_state &= ~TS_TTSTOP;
39697bd8 175 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
50e2732b
BJ
176 while (getc(&tp->t_outq) >= 0)
177 ;
178 }
179 if (rw & FREAD) {
180 while (getc(&tp->t_rawq) >= 0)
181 ;
becb115c 182 tp->t_rocount = 0;
50e2732b 183 tp->t_rocol = 0;
becb115c 184 tp->t_state &= ~TS_LOCAL;
50e2732b
BJ
185 }
186 splx(s);
89dc6dfb
BJ
187}
188
50e2732b
BJ
189/*
190 * Send stop character on input overflow.
191 */
192ttyblock(tp)
4b72e2f9 193 register struct tty *tp;
89dc6dfb 194{
50e2732b 195 register x;
becb115c 196
50e2732b
BJ
197 x = tp->t_rawq.c_cc + tp->t_canq.c_cc;
198 if (tp->t_rawq.c_cc > TTYHOG) {
88a7a62a 199 ttyflush(tp, FREAD|FWRITE);
941944c9 200 tp->t_state &= ~TS_TBLOCK;
50e2732b 201 }
600d5712
MK
202 /*
203 * Block further input iff:
204 * Current input > threshold AND input is available to user program
205 */
28b2d854 206 if (x >= TTYHOG/2 &&
2c6a4e11
MT
207 (!(tp->t_lflag&ICANON)) || (tp->t_canq.c_cc > 0) &&
208 tp->t_cc[VSTOP] != POSIX_V_DISABLE) {
209 if (putc(tp->t_cc[VSTOP], &tp->t_outq)==0) {
600d5712
MK
210 tp->t_state |= TS_TBLOCK;
211 ttstart(tp);
212 }
50e2732b 213 }
89dc6dfb
BJ
214}
215
101ba270 216/*
50e2732b
BJ
217 * Restart typewriter output following a delay
218 * timeout.
219 * The name of the routine is passed to the timeout
220 * subroutine and it is called during a clock interrupt.
101ba270 221 */
50e2732b 222ttrstrt(tp)
4b72e2f9 223 register struct tty *tp;
101ba270
BJ
224{
225
becb115c
SL
226 if (tp == 0)
227 panic("ttrstrt");
941944c9 228 tp->t_state &= ~TS_TIMEOUT;
50e2732b 229 ttstart(tp);
101ba270
BJ
230}
231
89dc6dfb 232/*
50e2732b
BJ
233 * Start output on the typewriter. It is used from the top half
234 * after some characters have been put on the output queue,
235 * from the interrupt routine to transmit the next
236 * character, and after a timeout has finished.
89dc6dfb 237 */
50e2732b 238ttstart(tp)
4b72e2f9 239 register struct tty *tp;
89dc6dfb 240{
8062c8a7 241
b482b2c8 242 if (tp->t_oproc) /* kludge for pty */
50e2732b 243 (*tp->t_oproc)(tp);
89dc6dfb
BJ
244}
245
246/*
50e2732b 247 * Common code for tty ioctls.
89dc6dfb 248 */
49c84d3f 249/*ARGSUSED*/
4b72e2f9
SL
250ttioctl(tp, com, data, flag)
251 register struct tty *tp;
252 caddr_t data;
89dc6dfb 253{
89dc6dfb 254 extern int nldisp;
2c6a4e11 255 int soft;
fc6972ae 256 int s;
89dc6dfb 257
adf4bc89 258
50e2732b
BJ
259 /*
260 * If the ioctl involves modification,
4f3a716e 261 * hang if in the background.
50e2732b 262 */
4b72e2f9 263 switch (com) {
50e2732b 264
2c6a4e11 265 case TIOCSETD:
50e2732b 266 case TIOCFLUSH:
50e2732b 267 case TIOCSPGRP:
8203fe05 268 case TIOCSTI:
f9eea644 269 case TIOCSWINSZ:
2c6a4e11
MT
270 case TIOCSETA:
271 case TIOCSETAW:
272 case TIOCSETAF:
273 case TIOCSETAS:
274 case TIOCSETAWS:
275 case TIOCSETAFS:
276 while (tp->t_line == POSXDISC &&
50e2732b
BJ
277 u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
278 (u.u_procp->p_flag&SVFORK) == 0 &&
52c041cd
MK
279 !(u.u_procp->p_sigignore & sigmask(SIGTTOU)) &&
280 !(u.u_procp->p_sigmask & sigmask(SIGTTOU))) {
50e2732b
BJ
281 gsignal(u.u_procp->p_pgrp, SIGTTOU);
282 sleep((caddr_t)&lbolt, TTOPRI);
283 }
284 break;
285 }
286
becb115c
SL
287 /*
288 * Process the ioctl.
289 */
4b72e2f9 290 switch (com) {
89dc6dfb 291
fc6972ae 292 /* get discipline number */
89dc6dfb 293 case TIOCGETD:
4b72e2f9 294 *(int *)data = tp->t_line;
89dc6dfb
BJ
295 break;
296
fc6972ae 297 /* set line discipline */
4b72e2f9
SL
298 case TIOCSETD: {
299 register int t = *(int *)data;
2c6a4e11 300 dev_t dev = tp->t_dev;
becb115c 301 int error = 0;
4b72e2f9 302
9b905352 303 if ((unsigned) t >= nldisp)
4a003df3 304 return (ENXIO);
2eaeef4d
MK
305 if (t != tp->t_line) {
306 s = spltty();
307 (*linesw[tp->t_line].l_close)(tp);
308 error = (*linesw[t].l_open)(dev, tp);
309 if (error) {
2c6a4e11 310 (void)(*linesw[tp->t_line].l_open)(dev, tp);
2eaeef4d
MK
311 splx(s);
312 return (error);
313 }
314 tp->t_line = t;
4a003df3 315 splx(s);
4a003df3 316 }
2c6a4e11
MT
317 if (tp->t_trace & TTRACE_STATE)
318 ttytrace(com, tp);
89dc6dfb 319 break;
4b72e2f9 320 }
89dc6dfb 321
fc6972ae 322 /* prevent more opens on channel */
152c2598
BJ
323 case TIOCEXCL:
324 tp->t_state |= TS_XCLUDE;
325 break;
326
327 case TIOCNXCL:
328 tp->t_state &= ~TS_XCLUDE;
329 break;
330
89dc6dfb 331 case TIOCHPCL:
2c6a4e11 332 tp->t_cflag |= HUPCL;
89dc6dfb
BJ
333 break;
334
7d973a4d 335 case TIOCFLUSH: {
4b72e2f9
SL
336 register int flags = *(int *)data;
337
338 if (flags == 0)
7d973a4d 339 flags = FREAD|FWRITE;
4b72e2f9
SL
340 else
341 flags &= FREAD|FWRITE;
88a7a62a 342 ttyflush(tp, flags);
89dc6dfb 343 break;
4568c08e 344 }
89dc6dfb 345
fc6972ae 346 /* return number of characters immediately available */
4b72e2f9
SL
347 case FIONREAD:
348 *(off_t *)data = ttnread(tp);
8062c8a7 349 break;
8062c8a7 350
b968e450
SL
351 case TIOCOUTQ:
352 *(int *)data = tp->t_outq.c_cc;
353 break;
354
a8d3bf7f 355 case TIOCSTOP:
4f3a716e 356 s = spltty();
becb115c 357 if ((tp->t_state&TS_TTSTOP) == 0) {
bfcf09ee
BJ
358 tp->t_state |= TS_TTSTOP;
359 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
360 }
4b72e2f9 361 splx(s);
bfcf09ee
BJ
362 break;
363
a8d3bf7f 364 case TIOCSTART:
4f3a716e 365 s = spltty();
2c6a4e11 366 if ((tp->t_state&TS_TTSTOP) || (tp->t_lflag&FLUSHO)) {
bfcf09ee 367 tp->t_state &= ~TS_TTSTOP;
2c6a4e11 368 tp->t_lflag &= ~FLUSHO;
bfcf09ee
BJ
369 ttstart(tp);
370 }
4b72e2f9 371 splx(s);
bfcf09ee
BJ
372 break;
373
8203fe05
SL
374 /*
375 * Simulate typing of a character at the terminal.
376 */
377 case TIOCSTI:
477d62ec
KM
378 if (u.u_uid && (flag & FREAD) == 0)
379 return (EPERM);
8203fe05
SL
380 if (u.u_uid && u.u_ttyp != tp)
381 return (EACCES);
becb115c 382 (*linesw[tp->t_line].l_rint)(*(char *)data, tp);
8203fe05
SL
383 break;
384
2c6a4e11
MT
385 case TIOCGETA: {
386 struct termios *t = (struct termios *)data;
387 bcopy(&tp->t_termio, t, sizeof(struct termios));
388 if (tp->t_trace & TTRACE_STATE)
389 ttytrace(com, tp);
390 break;
391 }
392
393 case TIOCSETAS:
394 case TIOCSETAWS:
395 case TIOCSETAFS:
396 soft = 1;
397 goto set;
398 case TIOCSETA:
399 case TIOCSETAW:
400 case TIOCSETAF:
401 soft = 0;
402set:
403 {
404 register struct termios *t = (struct termios *)data;
405
4f3a716e 406 s = spltty();
2c6a4e11
MT
407 if (com == TIOCSETAF || com == TIOCSETAFS)
408 ttywflush(tp);
409 else {
410 if (com == TIOCSETAW || com == TIOCSETAWS)
411 ttywait(tp);
412 if ((t->c_lflag&ICANON) != (tp->t_lflag&ICANON))
413 if (t->c_lflag&ICANON) {
414 tp->t_lflag |= PENDIN;
415 ttwakeup(tp);
416 }
417 else {
418 struct clist tq;
419
420 catq(&tp->t_rawq, &tp->t_canq);
421 tq = tp->t_rawq;
422 tp->t_rawq = tp->t_canq;
423 tp->t_canq = tq;
424 }
88a7a62a 425 }
2c6a4e11
MT
426 tp->t_iflag = t->c_iflag;
427 tp->t_oflag = t->c_oflag;
428 tp->t_lflag = t->c_lflag;
429 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
430 if (!soft) {
431 tp->t_cflag = t->c_cflag;
432 tp->t_ispeed = t->c_ispeed;
433 tp->t_ospeed = t->c_ospeed;
88a7a62a
SL
434 }
435 splx(s);
2c6a4e11
MT
436 if (tp->t_trace & TTRACE_STATE)
437 ttytrace(com, tp);
88a7a62a
SL
438 break;
439 }
440
2c6a4e11
MT
441 case TIOCTRACE:
442 tp->t_trace = *(int *)data;
88a7a62a 443 break;
88a7a62a
SL
444
445 case FIONBIO:
446 if (*(int *)data)
447 tp->t_state |= TS_NBIO;
448 else
449 tp->t_state &= ~TS_NBIO;
450 break;
451
452 case FIOASYNC:
453 if (*(int *)data)
454 tp->t_state |= TS_ASYNC;
455 else
456 tp->t_state &= ~TS_ASYNC;
457 break;
458
88a7a62a 459
4f3a716e 460 /*
cd69487e 461 * Allow SPGRP only if tty is open for reading.
f9eea644
JB
462 * Quick check: if we can find a process in the new pgrp,
463 * this user must own that process.
464 * SHOULD VERIFY THAT PGRP IS IN USE AND IS THIS USER'S.
4f3a716e 465 */
245aeefa 466 case TIOCSPGRP: {
4f3a716e
MK
467 struct proc *p;
468 int pgrp = *(int *)data;
469
470 if (u.u_uid && (flag & FREAD) == 0)
471 return (EPERM);
f9eea644
JB
472 p = pfind(pgrp);
473 if (p && p->p_pgrp == pgrp &&
474 p->p_uid != u.u_uid && u.u_uid && !inferior(p))
475 return (EPERM);
4f3a716e 476 tp->t_pgrp = pgrp;
88a7a62a 477 break;
245aeefa 478 }
88a7a62a
SL
479
480 case TIOCGPGRP:
481 *(int *)data = tp->t_pgrp;
482 break;
483
f9eea644 484 case TIOCSWINSZ:
245aeefa
JB
485 if (bcmp((caddr_t)&tp->t_winsize, data,
486 sizeof (struct winsize))) {
f9eea644
JB
487 tp->t_winsize = *(struct winsize *)data;
488 gsignal(tp->t_pgrp, SIGWINCH);
489 }
490 break;
491
492 case TIOCGWINSZ:
493 *(struct winsize *)data = tp->t_winsize;
494 break;
495
adf4bc89
MK
496 case TIOCCONS:
497 if (*(int *)data) {
498 if (constty != NULL)
499 return (EBUSY);
500#ifndef UCONSOLE
501 if (!suser())
502 return (EPERM);
503#endif
504 constty = tp;
505 } else if (tp == constty)
1eebc1a7 506 constty = NULL;
adf4bc89
MK
507 break;
508
2c6a4e11
MT
509 /* allow old ioctls for now */
510 case TIOCGETP:
511 case TIOCSETP:
512 case TIOCSETN:
513 case TIOCGETC:
514 case TIOCSETC:
515 case TIOCSLTC:
516 case TIOCGLTC:
517 case TIOCLBIS:
518 case TIOCLBIC:
519 case TIOCLSET:
520 case TIOCLGET:
521 return(ottioctl(tp, com, data, flag));
522
89dc6dfb 523 default:
fc6972ae 524 return (-1);
89dc6dfb 525 }
fc6972ae 526 return (0);
89dc6dfb 527}
e1d74936 528
2c6a4e11
MT
529/*
530 * DEBUG - to be removed
531 */
532ttytrace(ioc, tp)
533 struct tty *tp;
534{
535 register u_char *cc = tp->t_cc;
536 char comm[MAXCOMLEN+1];
537 static int seq = 0;
538
539 bcopy(u.u_comm, comm, MAXCOMLEN+1);
540 comm[MAXCOMLEN] = '\0';
541
542 /* trace changes to line disciplines */
543 if (ioc == TIOCSETD) {
544 log(LOG_LOCAL4|LOG_DEBUG, "%s:%x:%x:%x:%x\n",
545 comm, ioc, u.u_procp->p_pid, tp->t_dev, tp->t_line);
546 return;
547 }
548
549 /*
550 * format for the trace record is:
551 *
552 * u_comm:ioctl:pid:dev_t:ldisc:iflag:oflag:lflag:cflag:ispeed:
553 * ospeed:cc's...:seq
554 *
555 * u_comm is a string and all other values are hex. "cc's..."
556 * stands for control chars 0 through NCC-1. seq is a sequence #
557 * to force syslogd to log every entry (rather than hold them to
558 * print "last message repeated...".
559 */
560 log(LOG_LOCAL4|LOG_DEBUG, "%s:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x:%x\n",
561 comm, ioc, u.u_procp->p_pid, tp->t_dev, tp->t_line,
562 tp->t_iflag, tp->t_oflag, tp->t_lflag, tp->t_cflag,
563 tp->t_ispeed, tp->t_ospeed, cc[0], cc[1], cc[2], cc[3],
564 cc[4], cc[5], cc[6], cc[7], cc[8], cc[9], cc[10], cc[11],
565 cc[12], cc[13], cc[14], cc[15], cc[16], cc[17], cc[18],
566 cc[19], seq++);
567}
568
e1d74936
BJ
569ttnread(tp)
570 struct tty *tp;
571{
572 int nread = 0;
573
2c6a4e11 574 if (tp->t_lflag & PENDIN)
e1d74936
BJ
575 ttypend(tp);
576 nread = tp->t_canq.c_cc;
2c6a4e11 577 if (tp->t_lflag & ICANON == 0)
e1d74936
BJ
578 nread += tp->t_rawq.c_cc;
579 return (nread);
580}
581
941944c9 582ttselect(dev, rw)
e1d74936 583 dev_t dev;
941944c9 584 int rw;
e1d74936
BJ
585{
586 register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)];
587 int nread;
4f3a716e 588 int s = spltty();
e1d74936 589
941944c9 590 switch (rw) {
e1d74936
BJ
591
592 case FREAD:
593 nread = ttnread(tp);
fb1db32c 594 if (nread > 0 || (tp->t_state & TS_CARR_ON) == 0)
941944c9 595 goto win;
89b8a44c 596 if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait)
941944c9 597 tp->t_state |= TS_RCOLL;
e1d74936
BJ
598 else
599 tp->t_rsel = u.u_procp;
941944c9 600 break;
e1d74936 601
941944c9
BJ
602 case FWRITE:
603 if (tp->t_outq.c_cc <= TTLOWAT(tp))
604 goto win;
941944c9
BJ
605 if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait)
606 tp->t_state |= TS_WCOLL;
607 else
608 tp->t_wsel = u.u_procp;
609 break;
e1d74936 610 }
941944c9
BJ
611 splx(s);
612 return (0);
613win:
614 splx(s);
615 return (1);
e1d74936 616}
8bba2b8f 617
4147b3f6 618/*
605d7712 619 * Initial open of tty, or (re)entry to line discipline.
becb115c 620 * Establish a process group for distribution of
4147b3f6 621 * quits and interrupts from the tty.
4147b3f6
BJ
622 */
623ttyopen(dev, tp)
4b72e2f9
SL
624 dev_t dev;
625 register struct tty *tp;
4147b3f6
BJ
626{
627 register struct proc *pp;
628
629 pp = u.u_procp;
630 tp->t_dev = dev;
2c6a4e11 631 if (pp->p_pgrp == 0) { /* XXX - this has got to change */
4147b3f6
BJ
632 u.u_ttyp = tp;
633 u.u_ttyd = dev;
634 if (tp->t_pgrp == 0)
635 tp->t_pgrp = pp->p_pid;
636 pp->p_pgrp = tp->t_pgrp;
637 }
638 tp->t_state &= ~TS_WOPEN;
4f3a716e
MK
639 if ((tp->t_state & TS_ISOPEN) == 0) {
640 tp->t_state |= TS_ISOPEN;
f9eea644 641 bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize));
2c6a4e11
MT
642 /*
643 * CHANGE: used to do a ttywflush() if it was the
644 * old (old) line discipline.
645 */
4f3a716e 646 }
fc6972ae 647 return (0);
4147b3f6
BJ
648}
649
605d7712
MK
650/*
651 * "close" a line discipline
652 */
653ttylclose(tp)
654 register struct tty *tp;
655{
656
657 ttywflush(tp);
605d7712
MK
658}
659
4147b3f6
BJ
660/*
661 * clean tp on last close
662 */
663ttyclose(tp)
4b72e2f9 664 register struct tty *tp;
4147b3f6
BJ
665{
666
adf4bc89
MK
667 if (constty == tp)
668 constty = NULL;
605d7712 669 ttyflush(tp, FREAD|FWRITE);
4147b3f6 670 tp->t_pgrp = 0;
4147b3f6
BJ
671 tp->t_state = 0;
672}
673
605d7712
MK
674/*
675 * Handle modem control transition on a tty.
676 * Flag indicates new state of carrier.
677 * Returns 0 if the line should be turned off, otherwise 1.
678 */
679ttymodem(tp, flag)
680 register struct tty *tp;
681{
682
2c6a4e11 683 if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag & MDMBUF)) {
605d7712
MK
684 /*
685 * MDMBUF: do flow control according to carrier flag
686 */
687 if (flag) {
688 tp->t_state &= ~TS_TTSTOP;
689 ttstart(tp);
690 } else if ((tp->t_state&TS_TTSTOP) == 0) {
691 tp->t_state |= TS_TTSTOP;
692 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
693 }
694 } else if (flag == 0) {
695 /*
696 * Lost carrier.
697 */
698 tp->t_state &= ~TS_CARR_ON;
699 if (tp->t_state & TS_ISOPEN) {
2c6a4e11 700 if ((tp->t_lflag & NOHANG) == 0) {
605d7712
MK
701 gsignal(tp->t_pgrp, SIGHUP);
702 gsignal(tp->t_pgrp, SIGCONT);
703 ttyflush(tp, FREAD|FWRITE);
704 return (0);
705 }
706 }
707 } else {
708 /*
709 * Carrier now on.
710 */
711 tp->t_state |= TS_CARR_ON;
712 wakeup((caddr_t)&tp->t_rawq);
713 }
714 return (1);
715}
716
e3f2038d
MK
717/*
718 * Default modem control routine (for other line disciplines).
719 * Return argument flag, to turn off device on carrier drop.
720 */
2f54b5e8
MK
721nullmodem(tp, flag)
722 register struct tty *tp;
e3f2038d
MK
723 int flag;
724{
725
726 if (flag)
727 tp->t_state |= TS_CARR_ON;
728 else
729 tp->t_state &= ~TS_CARR_ON;
730 return (flag);
731}
732
4147b3f6
BJ
733/*
734 * reinput pending characters after state switch
4f3a716e 735 * call at spltty().
4147b3f6
BJ
736 */
737ttypend(tp)
4b72e2f9 738 register struct tty *tp;
4147b3f6
BJ
739{
740 struct clist tq;
741 register c;
742
2c6a4e11 743 tp->t_lflag &= ~PENDIN;
becb115c 744 tp->t_state |= TS_TYPEN;
4147b3f6
BJ
745 tq = tp->t_rawq;
746 tp->t_rawq.c_cc = 0;
747 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
748 while ((c = getc(&tq)) >= 0)
749 ttyinput(c, tp);
becb115c 750 tp->t_state &= ~TS_TYPEN;
4147b3f6
BJ
751}
752
753/*
2c6a4e11 754 *
becb115c
SL
755 * Place a character on raw TTY input queue,
756 * putting in delimiters and waking up top
757 * half as needed. Also echo if required.
758 * The arguments are the character and the
759 * appropriate tty structure.
4147b3f6
BJ
760 */
761ttyinput(c, tp)
4b72e2f9
SL
762 register c;
763 register struct tty *tp;
4147b3f6 764{
2c6a4e11
MT
765 register int iflag = tp->t_iflag;
766 register int lflag = tp->t_lflag;
767 register u_char *cc = tp->t_cc;
768 int i, err;
4147b3f6 769
becb115c
SL
770 /*
771 * If input is pending take it first.
772 */
2c6a4e11 773 if (lflag&PENDIN)
4147b3f6 774 ttypend(tp);
2c6a4e11 775
4147b3f6 776 tk_nin++;
becb115c
SL
777
778 /*
2c6a4e11 779 * Handle exceptional conditions (break, parity, framing).
becb115c 780 */
2c6a4e11
MT
781 if (err = (c&TTY_ERRORMASK)) {
782 c &= TTY_CHARMASK;
783 if (err&TTY_FE && !c) { /* break */
784 if (iflag&IGNBRK)
785 goto endcase;
786 else if (iflag&BRKINT && lflag&ISIG &&
787 (cc[VINTR] != POSIX_V_DISABLE))
788 c = cc[VINTR];
789 else
790 c = 0;
791 } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) {
792 if (iflag&IGNPAR)
793 goto endcase;
794 else if (iflag&PARMRK) {
795 ttyinput(0377, tp);
796 ttyinput(0, tp);
797 } else
798 c = 0;
4147b3f6 799 }
becb115c
SL
800 }
801
2c6a4e11
MT
802 dprintf("<%o>\n", c);
803
804 /*
805 * In tandem mode, check high water mark.
806 */
807 if (iflag&IXOFF)
808 ttyblock(tp);
809
becb115c
SL
810 /*
811 * Ignore any high bit added during
812 * previous ttyinput processing.
813 */
2c6a4e11 814 if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP))
becb115c
SL
815 c &= 0177;
816 /*
817 * Check for literal nexting very first
818 */
819 if (tp->t_state&TS_LNCH) {
820 c |= 0200;
821 tp->t_state &= ~TS_LNCH;
822 }
823
824 /*
825 * Scan for special characters. This code
826 * is really just a big case statement with
827 * non-constant cases. The bottom of the
828 * case statement is labeled ``endcase'', so goto
829 * it after a case match, or similar.
830 */
2c6a4e11
MT
831
832 /*
833 * Extensions to POSIX input modes which aren't controlled
834 * by ICANON, ISIG, or IXON.
835 */
836 if (iflag&IEXTEN) {
837 if (CCEQ(cc[VLNEXT],c) && (iflag&ISTRIP)) {
838 if (lflag&ECHO)
839 ttyout("^\b", tp); /*XXX - presumes too much */
becb115c
SL
840 tp->t_state |= TS_LNCH;
841 goto endcase;
842 }
2c6a4e11
MT
843 if (CCEQ(cc[VFLUSHO],c)) {
844 if (lflag&FLUSHO)
845 tp->t_lflag &= ~FLUSHO;
4147b3f6 846 else {
88a7a62a 847 ttyflush(tp, FWRITE);
4147b3f6 848 ttyecho(c, tp);
becb115c 849 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
4147b3f6 850 ttyretype(tp);
2c6a4e11 851 tp->t_lflag |= FLUSHO;
4147b3f6 852 }
becb115c
SL
853 goto startoutput;
854 }
2c6a4e11
MT
855 }
856
857 /*
858 * Signals.
859 */
860 if (lflag&ISIG) {
861 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
862 if ((lflag&NOFLSH) == 0)
863 ttyflush(tp, FREAD|FWRITE);
864 ttyecho(c, tp);
865 gsignal(tp->t_pgrp, CCEQ(cc[VINTR],c) ?
866 SIGINT : SIGQUIT);
867 goto endcase;
868 }
869 if (CCEQ(cc[VSUSP],c)) {
870 if ((lflag&NOFLSH) == 0)
88a7a62a 871 ttyflush(tp, FREAD);
4147b3f6 872 ttyecho(c, tp);
becb115c
SL
873 gsignal(tp->t_pgrp, SIGTSTP);
874 goto endcase;
875 }
876 }
877
878 /*
879 * Handle start/stop characters.
880 */
2c6a4e11
MT
881 if (iflag&IXON) {
882 if (CCEQ(cc[VSTOP],c)) {
883 if ((tp->t_state&TS_TTSTOP) == 0) {
884 tp->t_state |= TS_TTSTOP;
885 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
886 return;
887 }
888 if (!CCEQ(cc[VSTART], c))
889 return;
890 /*
891 * if VSTART == VSTOP we toggle
892 */
893 goto endcase;
becb115c 894 }
2c6a4e11
MT
895 if (CCEQ(cc[VSTART], c))
896 goto restartoutput;
becb115c 897 }
becb115c
SL
898
899 /*
2c6a4e11 900 * IGNCR, ICRNL, & INLCR
becb115c 901 */
2c6a4e11
MT
902 if (c == '\r') {
903 if (iflag&IGNCR)
904 goto endcase;
905 else if (iflag&ICRNL)
906 c = '\n';
becb115c 907 }
2c6a4e11
MT
908 else if (c == '\n' && iflag&INLCR)
909 c = '\r';
becb115c 910
2c6a4e11
MT
911#ifdef notdef
912 /*
913 * We'd like to completely remove it.
914 */
58786a81
JB
915 if (tp->t_flags & LCASE && c <= 0177) {
916 if (tp->t_state&TS_BKSL) {
917 ttyrub(unputc(&tp->t_rawq), tp);
918 if (maptab[c])
919 c = maptab[c];
920 c |= 0200;
921 tp->t_state &= ~(TS_BKSL|TS_QUOT);
922 } else if (c >= 'A' && c <= 'Z')
923 c += 'a' - 'A';
924 else if (c == '\\')
925 tp->t_state |= TS_BKSL;
926 }
2c6a4e11 927#endif /*notdef*/
58786a81 928
becb115c 929 /*
2c6a4e11 930 * Non canonical mode, don't process line editing
becb115c 931 * characters; check high water mark for wakeup.
2c6a4e11 932 *
becb115c 933 */
2c6a4e11 934 if (!(lflag&ICANON)) {
becb115c 935 if (tp->t_rawq.c_cc > TTYHOG) {
2c6a4e11
MT
936 if (iflag&IMAXBEL) {
937 if (tp->t_outq.c_cc < TTHIWAT(tp))
938 (void) ttyoutput(CTRL('g'), tp);
939 } else
940 ttyflush(tp, FREAD | FWRITE);
4147b3f6
BJ
941 } else if (putc(c, &tp->t_rawq) >= 0) {
942 ttwakeup(tp);
943 ttyecho(c, tp);
944 }
becb115c
SL
945 goto endcase;
946 }
947
948 /*
2c6a4e11 949 * From here on down canonical mode character
becb115c
SL
950 * processing takes place.
951 */
2c6a4e11
MT
952
953 /*
954 * Oldstyle quoting of erase, kill, and eof chars.
955 *
956 * Historically is '\' , but can be changed (read: disabled)
957 * with the VQUOTE subscript.
958 */
becb115c 959 if ((tp->t_state&TS_QUOT) &&
2c6a4e11 960 (CCEQ(cc[VERASE], c) || CCEQ(cc[VKILL], c) || CCEQ(cc[VEOF], c))) {
becb115c
SL
961 ttyrub(unputc(&tp->t_rawq), tp);
962 c |= 0200;
963 }
2c6a4e11
MT
964
965 /*
966 * erase (^H / ^?)
967 */
968 if (CCEQ(cc[VERASE], c)) {
becb115c
SL
969 if (tp->t_rawq.c_cc)
970 ttyrub(unputc(&tp->t_rawq), tp);
971 goto endcase;
972 }
2c6a4e11
MT
973 /*
974 * kill (^X / ^U)
975 */
976 if (CCEQ(cc[VKILL], c)) {
977 if (lflag&ECHOKE &&
becb115c
SL
978 tp->t_rawq.c_cc == tp->t_rocount) {
979 while (tp->t_rawq.c_cc)
980 ttyrub(unputc(&tp->t_rawq), tp);
981 } else {
982 ttyecho(c, tp);
2c6a4e11
MT
983 if (lflag&ECHOK || lflag&ECHOKE)
984 ttyecho('\n', tp);
becb115c
SL
985 while (getc(&tp->t_rawq) > 0)
986 ;
987 tp->t_rocount = 0;
988 }
989 tp->t_state &= ~TS_LOCAL;
990 goto endcase;
991 }
992
993 /*
2c6a4e11 994 * word erase (^W)
becb115c 995 */
2c6a4e11
MT
996 if (CCEQ(cc[VWERASE], c)) {
997 if (tp->t_rawq.c_cc == 0)
998 goto endcase;
999 do {
1000 c = unputc(&tp->t_rawq);
1001 if (c != ' ' && c != '\t')
1002 goto erasenb;
1003 ttyrub(c, tp);
1004 } while (tp->t_rawq.c_cc);
1005 goto endcase;
1006erasenb:
1007 do {
1008 ttyrub(c, tp);
becb115c
SL
1009 if (tp->t_rawq.c_cc == 0)
1010 goto endcase;
2c6a4e11
MT
1011 c = unputc(&tp->t_rawq);
1012 } while (c != ' ' && c != '\t');
1013 (void) putc(c, &tp->t_rawq);
1014 goto endcase;
1015 }
1016 /*
1017 * reprint line (^R)
1018 */
1019 if (CCEQ(cc[VREPRINT], c)) {
1020 ttyretype(tp);
1021 goto endcase;
becb115c
SL
1022 }
1023
1024 /*
1025 * Check for input buffer overflow
1026 */
7c9f96c0 1027 if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) {
2c6a4e11
MT
1028 if (iflag&IMAXBEL) {
1029 if (tp->t_outq.c_cc < TTHIWAT(tp))
1030 (void) ttyoutput(CTRL('g'), tp);
1031 } else
1032 ttyflush(tp, FREAD | FWRITE);
becb115c 1033 goto endcase;
7c9f96c0 1034 }
becb115c
SL
1035
1036 /*
1037 * Put data char in q for user and
1038 * wakeup on seeing a line delimiter.
1039 */
1040 if (putc(c, &tp->t_rawq) >= 0) {
2c6a4e11 1041 if (ttbreakc(c)) {
becb115c
SL
1042 tp->t_rocount = 0;
1043 catq(&tp->t_rawq, &tp->t_canq);
4147b3f6 1044 ttwakeup(tp);
becb115c
SL
1045 } else if (tp->t_rocount++ == 0)
1046 tp->t_rocol = tp->t_col;
1047 tp->t_state &= ~TS_QUOT;
2c6a4e11
MT
1048 if (CCEQ(cc[VQUOTE], c) && (iflag&ISTRIP))
1049 tp->t_state |= TS_QUOT; /* '\' escape */
becb115c 1050 if (tp->t_state&TS_ERASE) {
2c6a4e11
MT
1051 /*
1052 * end of prterase \.../
1053 */
becb115c
SL
1054 tp->t_state &= ~TS_ERASE;
1055 (void) ttyoutput('/', tp);
1056 }
1057 i = tp->t_col;
4147b3f6 1058 ttyecho(c, tp);
2c6a4e11
MT
1059 if (CCEQ(cc[VEOF], c) && lflag&ECHO) {
1060 /*
1061 * Place the cursor over the '^' of the ^D.
1062 */
becb115c
SL
1063 i = MIN(2, tp->t_col - i);
1064 while (i > 0) {
1065 (void) ttyoutput('\b', tp);
1066 i--;
1067 }
1068 }
4147b3f6 1069 }
becb115c
SL
1070endcase:
1071 /*
2c6a4e11 1072 * IXANY means allow any character to restart output.
becb115c 1073 */
2c6a4e11
MT
1074 if (tp->t_state&TS_TTSTOP && (iflag&IXANY == 0)
1075 && cc[VSTART] != cc[VSTOP])
4147b3f6 1076 return;
2c6a4e11 1077
becb115c 1078restartoutput:
4147b3f6 1079 tp->t_state &= ~TS_TTSTOP;
2c6a4e11 1080 tp->t_lflag &= ~FLUSHO;
becb115c 1081startoutput:
4147b3f6
BJ
1082 ttstart(tp);
1083}
1084
1085/*
becb115c 1086 * Put character on TTY output queue, adding delays,
4147b3f6 1087 * expanding tabs, and handling the CR/NL bit.
becb115c
SL
1088 * This is called both from the top half for output,
1089 * and from interrupt level for echoing.
4147b3f6
BJ
1090 * The arguments are the character and the tty structure.
1091 * Returns < 0 if putc succeeds, otherwise returns char to resend
1092 * Must be recursive.
1093 */
1094ttyoutput(c, tp)
1095 register c;
1096 register struct tty *tp;
1097{
1098 register char *colp;
1099 register ctype;
1100
2c6a4e11
MT
1101 if (!(tp->t_oflag&OPOST)) {
1102 if (tp->t_lflag&FLUSHO)
4147b3f6
BJ
1103 return (-1);
1104 if (putc(c, &tp->t_outq))
4b72e2f9 1105 return (c);
4147b3f6
BJ
1106 tk_nout++;
1107 return (-1);
1108 }
2c6a4e11 1109 c &= 0377;
4147b3f6
BJ
1110 /*
1111 * Turn tabs to spaces as required
1112 */
2c6a4e11 1113 if (c == '\t' && tp->t_oflag&OXTABS ) {
4147b3f6
BJ
1114 register int s;
1115
1116 c = 8 - (tp->t_col&7);
2c6a4e11 1117 if ((tp->t_lflag&FLUSHO) == 0) {
4f3a716e 1118 s = spltty(); /* don't interrupt tabs */
4147b3f6
BJ
1119 c -= b_to_q(" ", c, &tp->t_outq);
1120 tk_nout += c;
1121 splx(s);
1122 }
1123 tp->t_col += c;
1124 return (c ? -1 : '\t');
1125 }
1126 tk_nout++;
2c6a4e11 1127#ifdef notdef
4147b3f6
BJ
1128 /*
1129 * for upper-case-only terminals,
1130 * generate escapes.
1131 */
1132 if (tp->t_flags&LCASE) {
1133 colp = "({)}!|^~'`";
4b72e2f9
SL
1134 while (*colp++)
1135 if (c == *colp++) {
4147b3f6
BJ
1136 if (ttyoutput('\\', tp) >= 0)
1137 return (c);
1138 c = colp[-2];
1139 break;
1140 }
becb115c 1141 if ('A' <= c && c <= 'Z') {
4147b3f6
BJ
1142 if (ttyoutput('\\', tp) >= 0)
1143 return (c);
becb115c 1144 } else if ('a' <= c && c <= 'z')
4147b3f6
BJ
1145 c += 'A' - 'a';
1146 }
2c6a4e11 1147#endif
becb115c 1148
4147b3f6
BJ
1149 /*
1150 * turn <nl> to <cr><lf> if desired.
1151 */
2c6a4e11 1152 if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0)
4147b3f6 1153 return (c);
2c6a4e11 1154#ifdef notdef
becb115c 1155 if (c == '~' && tp->t_flags&TILDE)
4147b3f6 1156 c = '`';
2c6a4e11
MT
1157#endif
1158 if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq))
4147b3f6
BJ
1159 return (c);
1160 /*
1161 * Calculate delays.
1162 * The numbers here represent clock ticks
1163 * and are not necessarily optimal for all terminals.
1164 * The delays are indicated by characters above 0200.
1165 * In raw mode there are no delays and the
1166 * transmission path is 8 bits wide.
becb115c
SL
1167 *
1168 * SHOULD JUST ALLOW USER TO SPECIFY DELAYS
4147b3f6
BJ
1169 */
1170 colp = &tp->t_col;
1171 ctype = partab[c];
1172 c = 0;
1173 switch (ctype&077) {
1174
1175 case ORDINARY:
1176 (*colp)++;
1177
1178 case CONTROL:
1179 break;
1180
1181 case BACKSPACE:
1182 if (*colp)
1183 (*colp)--;
1184 break;
1185
3686c829
SL
1186 /*
1187 * This macro is close enough to the correct thing;
1188 * it should be replaced by real user settable delays
1189 * in any event...
1190 */
1191#define mstohz(ms) (((ms) * hz) >> 10)
4147b3f6
BJ
1192 case NEWLINE:
1193 ctype = (tp->t_flags >> 8) & 03;
4b72e2f9 1194 if (ctype == 1) { /* tty 37 */
8011f5df
MK
1195 if (*colp > 0) {
1196 c = (((unsigned)*colp) >> 4) + 3;
1197 if ((unsigned)c > 6)
1198 c = 6;
1199 }
becb115c 1200 } else if (ctype == 2) /* vt05 */
3686c829 1201 c = mstohz(100);
4147b3f6
BJ
1202 *colp = 0;
1203 break;
1204
1205 case TAB:
1206 ctype = (tp->t_flags >> 10) & 03;
4b72e2f9 1207 if (ctype == 1) { /* tty 37 */
4147b3f6 1208 c = 1 - (*colp | ~07);
4b72e2f9 1209 if (c < 5)
4147b3f6
BJ
1210 c = 0;
1211 }
1212 *colp |= 07;
1213 (*colp)++;
1214 break;
1215
1216 case VTAB:
becb115c 1217 if (tp->t_flags&VTDELAY) /* tty 37 */
4147b3f6
BJ
1218 c = 0177;
1219 break;
1220
1221 case RETURN:
1222 ctype = (tp->t_flags >> 12) & 03;
becb115c 1223 if (ctype == 1) /* tn 300 */
3686c829 1224 c = mstohz(83);
becb115c 1225 else if (ctype == 2) /* ti 700 */
3686c829 1226 c = mstohz(166);
becb115c 1227 else if (ctype == 3) { /* concept 100 */
4147b3f6 1228 int i;
becb115c 1229
4147b3f6 1230 if ((i = *colp) >= 0)
becb115c 1231 for (; i < 9; i++)
4147b3f6
BJ
1232 (void) putc(0177, &tp->t_outq);
1233 }
1234 *colp = 0;
1235 }
2c6a4e11 1236 if (c && (tp->t_lflag&FLUSHO) == 0)
4147b3f6
BJ
1237 (void) putc(c|0200, &tp->t_outq);
1238 return (-1);
1239}
3686c829 1240#undef mstohz
4147b3f6
BJ
1241
1242/*
1243 * Called from device's read routine after it has
1244 * calculated the tty-structure given as argument.
1245 */
d6d7360b 1246ttread(tp, uio)
4b72e2f9 1247 register struct tty *tp;
d6d7360b 1248 struct uio *uio;
4147b3f6
BJ
1249{
1250 register struct clist *qp;
2c6a4e11
MT
1251 register int c, t_flags;
1252 register long lflag = tp->t_lflag;
1253 register long iflag = tp->t_iflag;
1254 register u_char *cc = tp->t_cc;
875a1e2c 1255 int s, first, error = 0;
4147b3f6 1256
2c6a4e11 1257
4147b3f6 1258loop:
becb115c
SL
1259 /*
1260 * Take any pending input first.
1261 */
4f3a716e 1262 s = spltty();
2c6a4e11 1263 if (tp->t_lflag&PENDIN)
4147b3f6 1264 ttypend(tp);
875a1e2c 1265 splx(s);
becb115c 1266
58786a81
JB
1267 if ((tp->t_state&TS_CARR_ON)==0)
1268 return (EIO);
1269
becb115c
SL
1270 /*
1271 * Hang process if it's in the background.
1272 */
58786a81 1273 if (tp == u.u_ttyp && u.u_procp->p_pgrp != tp->t_pgrp) {
52c041cd
MK
1274 if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) ||
1275 (u.u_procp->p_sigmask & sigmask(SIGTTIN)) ||
4147b3f6 1276 u.u_procp->p_flag&SVFORK)
840510a3 1277 return (EIO);
4147b3f6
BJ
1278 gsignal(u.u_procp->p_pgrp, SIGTTIN);
1279 sleep((caddr_t)&lbolt, TTIPRI);
58786a81 1280 goto loop;
4147b3f6 1281 }
becb115c
SL
1282 t_flags = tp->t_flags;
1283
1284 /*
2c6a4e11
MT
1285 * If canonical, use the canonical queue,
1286 * else use the raw queue.
becb115c 1287 */
2c6a4e11 1288 qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq;
becb115c
SL
1289
1290 /*
1291 * No input, sleep on rawq awaiting hardware
1292 * receipt and notification.
1293 */
4f3a716e 1294 s = spltty();
becb115c
SL
1295 if (qp->c_cc <= 0) {
1296 if ((tp->t_state&TS_CARR_ON) == 0 ||
1297 (tp->t_state&TS_NBIO)) {
875a1e2c 1298 splx(s);
becb115c 1299 return (EWOULDBLOCK);
4147b3f6 1300 }
becb115c 1301 sleep((caddr_t)&tp->t_rawq, TTIPRI);
875a1e2c 1302 splx(s);
becb115c
SL
1303 goto loop;
1304 }
875a1e2c 1305 splx(s);
becb115c
SL
1306
1307 /*
2c6a4e11 1308 * Input present, check for input mapping and processing.
becb115c
SL
1309 */
1310 first = 1;
1311 while ((c = getc(qp)) >= 0) {
becb115c 1312 /*
2c6a4e11 1313 * delayed suspend (^Y)
becb115c 1314 */
2c6a4e11 1315 if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) {
becb115c
SL
1316 gsignal(tp->t_pgrp, SIGTSTP);
1317 if (first) {
1318 sleep((caddr_t)&lbolt, TTIPRI);
1319 goto loop;
1320 }
1321 break;
4147b3f6 1322 }
becb115c 1323 /*
2c6a4e11 1324 * Interpret EOF only in canonical mode.
becb115c 1325 */
2c6a4e11 1326 if (CCEQ(cc[VEOF], c) && lflag&ICANON)
becb115c
SL
1327 break;
1328 /*
1329 * Give user character.
1330 */
2c6a4e11 1331 error = ureadc(iflag&ISTRIP ? c & 0177 : c , uio);
becb115c
SL
1332 if (error)
1333 break;
9ec65acb 1334 if (uio->uio_resid == 0)
becb115c
SL
1335 break;
1336 /*
2c6a4e11 1337 * In canonical mode check for a "break character"
becb115c
SL
1338 * marking the end of a "line of input".
1339 */
2c6a4e11 1340 if (lflag&ICANON && ttbreakc(c)) {
becb115c 1341 break;
2c6a4e11 1342 }
becb115c 1343 first = 0;
4147b3f6 1344 }
becb115c 1345
875a1e2c 1346checktandem:
becb115c
SL
1347 /*
1348 * Look to unblock output now that (presumably)
1349 * the input queue has gone down.
1350 */
2c6a4e11
MT
1351 if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) {
1352 if (cc[VSTART] != POSIX_V_DISABLE
1353 && putc(cc[VSTART], &tp->t_outq) == 0) {
4147b3f6
BJ
1354 tp->t_state &= ~TS_TBLOCK;
1355 ttstart(tp);
1356 }
2c6a4e11 1357 }
840510a3 1358 return (error);
4147b3f6
BJ
1359}
1360
605d7712
MK
1361/*
1362 * Check the output queue on tp for space for a kernel message
1363 * (from uprintf/tprintf). Allow some space over the normal
1364 * hiwater mark so we don't lose messages due to normal flow
1365 * control, but don't let the tty run amok.
ca9b16e5
MK
1366 * Sleeps here are not interruptible, but we return prematurely
1367 * if new signals come in.
605d7712
MK
1368 */
1369ttycheckoutq(tp, wait)
1370 register struct tty *tp;
1371 int wait;
1372{
ca9b16e5 1373 int hiwat, s, oldsig;
605d7712
MK
1374
1375 hiwat = TTHIWAT(tp);
1376 s = spltty();
ca9b16e5 1377 oldsig = u.u_procp->p_sig;
605d7712 1378 if (tp->t_outq.c_cc > hiwat + 200)
fb1db32c
MK
1379 while (tp->t_outq.c_cc > hiwat) {
1380 ttstart(tp);
ca9b16e5 1381 if (wait == 0 || u.u_procp->p_sig != oldsig) {
fb1db32c
MK
1382 splx(s);
1383 return (0);
1384 }
ca9b16e5 1385 timeout(wakeup, (caddr_t)&tp->t_outq, hz);
fb1db32c 1386 tp->t_state |= TS_ASLEEP;
ca9b16e5 1387 sleep((caddr_t)&tp->t_outq, PZERO - 1);
605d7712 1388 }
605d7712
MK
1389 splx(s);
1390 return (1);
1391}
1392
4147b3f6
BJ
1393/*
1394 * Called from the device's write routine after it has
1395 * calculated the tty-structure given as argument.
1396 */
913ea2d1 1397ttwrite(tp, uio)
4b72e2f9 1398 register struct tty *tp;
becb115c 1399 register struct uio *uio;
4147b3f6 1400{
4147b3f6 1401 register char *cp;
becb115c
SL
1402 register int cc, ce, c;
1403 int i, hiwat, cnt, error, s;
4147b3f6 1404 char obuf[OBUFSIZ];
4147b3f6 1405
becb115c
SL
1406 hiwat = TTHIWAT(tp);
1407 cnt = uio->uio_resid;
1408 error = 0;
4147b3f6 1409loop:
93d2ecff
JB
1410 if ((tp->t_state&TS_CARR_ON) == 0)
1411 return (EIO);
becb115c
SL
1412 /*
1413 * Hang the process if it's in the background.
1414 */
93d2ecff 1415 if (u.u_procp->p_pgrp != tp->t_pgrp && tp == u.u_ttyp &&
2c6a4e11 1416 (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 &&
52c041cd
MK
1417 !(u.u_procp->p_sigignore & sigmask(SIGTTOU)) &&
1418 !(u.u_procp->p_sigmask & sigmask(SIGTTOU))) {
4147b3f6
BJ
1419 gsignal(u.u_procp->p_pgrp, SIGTTOU);
1420 sleep((caddr_t)&lbolt, TTIPRI);
93d2ecff 1421 goto loop;
4147b3f6 1422 }
becb115c
SL
1423
1424 /*
1425 * Process the user's data in at most OBUFSIZ
1426 * chunks. Perform lower case simulation and
1427 * similar hacks. Keep track of high water
1428 * mark, sleep on overflow awaiting device aid
1429 * in acquiring new space.
1430 */
913ea2d1 1431 while (uio->uio_resid > 0) {
b482b2c8
MK
1432 if (tp->t_outq.c_cc > hiwat) {
1433 cc = 0;
1434 goto ovhiwat;
1435 }
becb115c
SL
1436 /*
1437 * Grab a hunk of data from the user.
1438 */
913ea2d1
BJ
1439 cc = uio->uio_iov->iov_len;
1440 if (cc == 0) {
1441 uio->uio_iovcnt--;
1442 uio->uio_iov++;
93d2ecff 1443 if (uio->uio_iovcnt <= 0)
913ea2d1
BJ
1444 panic("ttwrite");
1445 continue;
1446 }
1447 if (cc > OBUFSIZ)
1448 cc = OBUFSIZ;
4147b3f6 1449 cp = obuf;
88a7a62a 1450 error = uiomove(cp, cc, UIO_WRITE, uio);
840510a3 1451 if (error)
4147b3f6 1452 break;
2c6a4e11 1453 if (tp->t_lflag&FLUSHO)
4147b3f6 1454 continue;
2c6a4e11 1455#ifdef notdef
becb115c
SL
1456 /*
1457 * If we're mapping lower case or kludging tildes,
1458 * then we've got to look at each character, so
1459 * just feed the stuff to ttyoutput...
1460 */
1461 if (tp->t_flags & (LCASE|TILDE)) {
1462 while (cc > 0) {
4147b3f6
BJ
1463 c = *cp++;
1464 tp->t_rocount = 0;
4b72e2f9 1465 while ((c = ttyoutput(c, tp)) >= 0) {
4147b3f6
BJ
1466 /* out of clists, wait a bit */
1467 ttstart(tp);
1468 sleep((caddr_t)&lbolt, TTOPRI);
1469 tp->t_rocount = 0;
93d2ecff
JB
1470 if (cc != 0) {
1471 uio->uio_iov->iov_base -= cc;
1472 uio->uio_iov->iov_len += cc;
1473 uio->uio_resid += cc;
1474 uio->uio_offset -= cc;
1475 }
1476 goto loop;
4147b3f6
BJ
1477 }
1478 --cc;
1479 if (tp->t_outq.c_cc > hiwat)
1480 goto ovhiwat;
1481 }
1482 continue;
1483 }
2c6a4e11 1484#endif
becb115c
SL
1485 /*
1486 * If nothing fancy need be done, grab those characters we
1487 * can handle without any of ttyoutput's processing and
1488 * just transfer them to the output q. For those chars
1489 * which require special processing (as indicated by the
1490 * bits in partab), call ttyoutput. After processing
1491 * a hunk of data, look for FLUSHO so ^O's will take effect
1492 * immediately.
1493 */
1494 while (cc > 0) {
2c6a4e11 1495 if (!(tp->t_oflag&OPOST))
4147b3f6
BJ
1496 ce = cc;
1497 else {
3f36033f
MK
1498 ce = cc - scanc((unsigned)cc, (u_char *)cp,
1499 (u_char *)partab, 077);
becb115c
SL
1500 /*
1501 * If ce is zero, then we're processing
1502 * a special character through ttyoutput.
1503 */
1504 if (ce == 0) {
4147b3f6
BJ
1505 tp->t_rocount = 0;
1506 if (ttyoutput(*cp, tp) >= 0) {
93d2ecff
JB
1507 /* no c-lists, wait a bit */
1508 ttstart(tp);
1509 sleep((caddr_t)&lbolt, TTOPRI);
1510 if (cc != 0) {
1511 uio->uio_iov->iov_base -= cc;
1512 uio->uio_iov->iov_len += cc;
1513 uio->uio_resid += cc;
1514 uio->uio_offset -= cc;
1515 }
1516 goto loop;
4147b3f6 1517 }
becb115c 1518 cp++, cc--;
2c6a4e11 1519 if (tp->t_lflag&FLUSHO ||
becb115c 1520 tp->t_outq.c_cc > hiwat)
4147b3f6 1521 goto ovhiwat;
becb115c 1522 continue;
4147b3f6
BJ
1523 }
1524 }
becb115c
SL
1525 /*
1526 * A bunch of normal characters have been found,
1527 * transfer them en masse to the output queue and
1528 * continue processing at the top of the loop.
1529 * If there are any further characters in this
1530 * <= OBUFSIZ chunk, the first should be a character
1531 * requiring special handling by ttyoutput.
1532 */
4147b3f6 1533 tp->t_rocount = 0;
becb115c
SL
1534 i = b_to_q(cp, ce, &tp->t_outq);
1535 ce -= i;
1536 tp->t_col += ce;
1537 cp += ce, cc -= ce, tk_nout += ce;
1538 if (i > 0) {
1539 /* out of c-lists, wait a bit */
4147b3f6
BJ
1540 ttstart(tp);
1541 sleep((caddr_t)&lbolt, TTOPRI);
93d2ecff
JB
1542 uio->uio_iov->iov_base -= cc;
1543 uio->uio_iov->iov_len += cc;
1544 uio->uio_resid += cc;
1545 uio->uio_offset -= cc;
1546 goto loop;
4147b3f6 1547 }
2c6a4e11 1548 if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat)
4147b3f6
BJ
1549 goto ovhiwat;
1550 }
2c6a4e11 1551 ttstart(tp);
4147b3f6 1552 }
840510a3 1553 return (error);
4147b3f6
BJ
1554
1555ovhiwat:
becb115c
SL
1556 if (cc != 0) {
1557 uio->uio_iov->iov_base -= cc;
1558 uio->uio_iov->iov_len += cc;
1559 uio->uio_resid += cc;
1560 uio->uio_offset -= cc;
1561 }
b482b2c8
MK
1562 ttstart(tp);
1563 s = spltty();
becb115c 1564 /*
2c6a4e11 1565 * This can only occur if FLUSHO is set in t_lflag,
b482b2c8 1566 * or if ttstart/oproc is synchronous (or very fast).
becb115c 1567 */
4147b3f6 1568 if (tp->t_outq.c_cc <= hiwat) {
becb115c 1569 splx(s);
4147b3f6
BJ
1570 goto loop;
1571 }
becb115c 1572 if (tp->t_state&TS_NBIO) {
4f3a716e 1573 splx(s);
913ea2d1 1574 if (uio->uio_resid == cnt)
840510a3
BJ
1575 return (EWOULDBLOCK);
1576 return (0);
4147b3f6
BJ
1577 }
1578 tp->t_state |= TS_ASLEEP;
1579 sleep((caddr_t)&tp->t_outq, TTOPRI);
becb115c 1580 splx(s);
4147b3f6
BJ
1581 goto loop;
1582}
1583
1584/*
1585 * Rubout one character from the rawq of tp
1586 * as cleanly as possible.
1587 */
1588ttyrub(c, tp)
4b72e2f9
SL
1589 register c;
1590 register struct tty *tp;
4147b3f6
BJ
1591{
1592 register char *cp;
1593 register int savecol;
1594 int s;
1595 char *nextc();
1596
2c6a4e11 1597 if ((tp->t_lflag&ECHO) == 0)
4147b3f6 1598 return;
2c6a4e11 1599 tp->t_lflag &= ~FLUSHO;
4147b3f6 1600 c &= 0377;
2c6a4e11 1601 if (tp->t_lflag&ECHOE) {
4147b3f6
BJ
1602 if (tp->t_rocount == 0) {
1603 /*
1604 * Screwed by ttwrite; retype
1605 */
1606 ttyretype(tp);
1607 return;
1608 }
2c6a4e11 1609 /* if tab or newline was escaped - XXX - not 8bit */
becb115c 1610 if (c == ('\t'|0200) || c == ('\n'|0200))
4147b3f6 1611 ttyrubo(tp, 2);
becb115c 1612 else switch (partab[c&=0177]&0177) {
4147b3f6
BJ
1613
1614 case ORDINARY:
2c6a4e11 1615#ifdef notdef
4147b3f6
BJ
1616 if (tp->t_flags&LCASE && c >= 'A' && c <= 'Z')
1617 ttyrubo(tp, 2);
1618 else
2c6a4e11 1619#endif
4147b3f6
BJ
1620 ttyrubo(tp, 1);
1621 break;
1622
1623 case VTAB:
1624 case BACKSPACE:
1625 case CONTROL:
1626 case RETURN:
2c6a4e11 1627 if (tp->t_lflag&ECHOCTL)
4147b3f6
BJ
1628 ttyrubo(tp, 2);
1629 break;
1630
1631 case TAB:
1632 if (tp->t_rocount < tp->t_rawq.c_cc) {
1633 ttyretype(tp);
1634 return;
1635 }
4f3a716e 1636 s = spltty();
4147b3f6 1637 savecol = tp->t_col;
becb115c 1638 tp->t_state |= TS_CNTTB;
2c6a4e11 1639 tp->t_lflag |= FLUSHO;
4147b3f6 1640 tp->t_col = tp->t_rocol;
becb115c
SL
1641 cp = tp->t_rawq.c_cf;
1642 for (; cp; cp = nextc(&tp->t_rawq, cp))
4147b3f6 1643 ttyecho(*cp, tp);
2c6a4e11 1644 tp->t_lflag &= ~FLUSHO;
becb115c 1645 tp->t_state &= ~TS_CNTTB;
4147b3f6
BJ
1646 splx(s);
1647 /*
1648 * savecol will now be length of the tab
1649 */
1650 savecol -= tp->t_col;
1651 tp->t_col += savecol;
1652 if (savecol > 8)
1653 savecol = 8; /* overflow screw */
1654 while (--savecol >= 0)
1655 (void) ttyoutput('\b', tp);
1656 break;
1657
1658 default:
1659 panic("ttyrub");
1660 }
2c6a4e11 1661 } else if (tp->t_lflag&ECHOPRT) {
becb115c 1662 if ((tp->t_state&TS_ERASE) == 0) {
4147b3f6 1663 (void) ttyoutput('\\', tp);
becb115c 1664 tp->t_state |= TS_ERASE;
4147b3f6
BJ
1665 }
1666 ttyecho(c, tp);
1667 } else
2c6a4e11 1668 ttyecho(tp->t_cc[VERASE], tp);
4147b3f6
BJ
1669 tp->t_rocount--;
1670}
1671
1672/*
1673 * Crt back over cnt chars perhaps
1674 * erasing them.
1675 */
1676ttyrubo(tp, cnt)
4b72e2f9
SL
1677 register struct tty *tp;
1678 int cnt;
4147b3f6 1679{
2c6a4e11 1680 register char *rubostring = tp->t_lflag&ECHOE ? "\b \b" : "\b";
4147b3f6
BJ
1681
1682 while (--cnt >= 0)
becb115c 1683 ttyout(rubostring, tp);
4147b3f6
BJ
1684}
1685
1686/*
1687 * Reprint the rawq line.
1688 * We assume c_cc has already been checked.
1689 */
1690ttyretype(tp)
4b72e2f9 1691 register struct tty *tp;
4147b3f6
BJ
1692{
1693 register char *cp;
1694 char *nextc();
1695 int s;
1696
2c6a4e11
MT
1697 if (tp->t_cc[VREPRINT] != POSIX_V_DISABLE)
1698 ttyecho(tp->t_cc[VREPRINT], tp);
4147b3f6 1699 (void) ttyoutput('\n', tp);
4f3a716e 1700 s = spltty();
4147b3f6
BJ
1701 for (cp = tp->t_canq.c_cf; cp; cp = nextc(&tp->t_canq, cp))
1702 ttyecho(*cp, tp);
1703 for (cp = tp->t_rawq.c_cf; cp; cp = nextc(&tp->t_rawq, cp))
1704 ttyecho(*cp, tp);
becb115c 1705 tp->t_state &= ~TS_ERASE;
4147b3f6
BJ
1706 splx(s);
1707 tp->t_rocount = tp->t_rawq.c_cc;
1708 tp->t_rocol = 0;
1709}
1710
1711/*
2c6a4e11 1712 * Echo a typed character to the terminal.
4147b3f6
BJ
1713 */
1714ttyecho(c, tp)
4b72e2f9
SL
1715 register c;
1716 register struct tty *tp;
4147b3f6
BJ
1717{
1718
4147b3f6 1719 c &= 0377;
2c6a4e11
MT
1720 if ((tp->t_state&TS_CNTTB) == 0)
1721 tp->t_lflag &= ~FLUSHO;
1722 if ((tp->t_lflag&ECHO) == 0 && !(tp->t_lflag&ECHONL && c == '\n'))
4147b3f6 1723 return;
2c6a4e11 1724 if (tp->t_lflag&ECHOCTL) {
4147b3f6
BJ
1725 if ((c&0177) <= 037 && c!='\t' && c!='\n' || (c&0177)==0177) {
1726 (void) ttyoutput('^', tp);
1727 c &= 0177;
1728 if (c == 0177)
1729 c = '?';
2c6a4e11 1730#ifdef notdef
4147b3f6
BJ
1731 else if (tp->t_flags&LCASE)
1732 c += 'a' - 1;
2c6a4e11 1733#endif
4147b3f6
BJ
1734 else
1735 c += 'A' - 1;
1736 }
1737 }
becb115c 1738 (void) ttyoutput(c&0177, tp);
4147b3f6
BJ
1739}
1740
4147b3f6
BJ
1741/*
1742 * send string cp to tp
1743 */
1744ttyout(cp, tp)
4b72e2f9
SL
1745 register char *cp;
1746 register struct tty *tp;
4147b3f6
BJ
1747{
1748 register char c;
1749
1750 while (c = *cp++)
1751 (void) ttyoutput(c, tp);
1752}
1753
1754ttwakeup(tp)
1755 struct tty *tp;
1756{
1757
1758 if (tp->t_rsel) {
1759 selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL);
1760 tp->t_state &= ~TS_RCOLL;
1761 tp->t_rsel = 0;
1762 }
88a7a62a
SL
1763 if (tp->t_state & TS_ASYNC)
1764 gsignal(tp->t_pgrp, SIGIO);
4147b3f6
BJ
1765 wakeup((caddr_t)&tp->t_rawq);
1766}