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