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