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