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