123387Smckusick /* 240712Skarels * Copyright (c) 1982, 1986, 1990 Regents of the University of California. 323387Smckusick * All rights reserved. The Berkeley software License Agreement 423387Smckusick * specifies the terms and conditions for redistribution. 523387Smckusick * 6*42141Smckusick * @(#)tty.c 7.25 (Berkeley) 05/16/90 723387Smckusick */ 839Sbill 917095Sbloom #include "param.h" 1017095Sbloom #include "systm.h" 1117095Sbloom #include "user.h" 1217095Sbloom #include "ioctl.h" 1339407Smarc #define TTYDEFCHARS 1417095Sbloom #include "tty.h" 1535811Smarc #undef TTYDEFCHARS 1617095Sbloom #include "proc.h" 1717095Sbloom #include "file.h" 1817095Sbloom #include "conf.h" 1929946Skarels #include "dkstat.h" 2017095Sbloom #include "uio.h" 2117095Sbloom #include "kernel.h" 2237728Smckusick #include "vnode.h" 2335811Smarc #include "syslog.h" 2439Sbill 2537525Smckusick #include "machine/reg.h" 2637525Smckusick 2740712Skarels /* symbolic sleep message strings */ 2840712Skarels char ttyin[] = "ttyin"; 2940712Skarels char ttyout[] = "ttyout"; 3041370Smarc char ttopen[] = "ttyopn"; 3141370Smarc char ttclos[] = "ttycls"; 3240712Skarels char ttybg[] = "ttybg"; 3340712Skarels char ttybuf[] = "ttybuf"; 3440712Skarels 357436Skre /* 367436Skre * Table giving parity for characters and indicating 3735811Smarc * character classes to tty driver. The 8th bit 3835811Smarc * indicates parity, the 7th bit indicates the character 3935811Smarc * is an alphameric or underscore (for ALTWERASE), and the 4035811Smarc * low 6 bits indicate delay type. If the low 6 bits are 0 4135811Smarc * then the character needs no special processing on output. 427436Skre */ 4339Sbill 447436Skre char partab[] = { 4535811Smarc 0001,0201,0201,0001,0201,0001,0001,0201, /* nul - bel */ 4635811Smarc 0202,0004,0003,0201,0005,0206,0201,0001, /* bs - si */ 4735811Smarc 0201,0001,0001,0201,0001,0201,0201,0001, /* dle - etb */ 4835811Smarc 0001,0201,0201,0001,0201,0001,0001,0201, /* can - us */ 4935811Smarc 0200,0000,0000,0200,0000,0200,0200,0000, /* sp - ' */ 5035811Smarc 0000,0200,0200,0000,0200,0000,0000,0200, /* ( - / */ 5135811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* 0 - 7 */ 5235811Smarc 0300,0100,0000,0200,0000,0200,0200,0000, /* 8 - ? */ 5335811Smarc 0200,0100,0100,0300,0100,0300,0300,0100, /* @ - G */ 5435811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* H - O */ 5535811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* P - W */ 5635811Smarc 0300,0100,0100,0200,0000,0200,0200,0300, /* X - _ */ 5735811Smarc 0000,0300,0300,0100,0300,0100,0100,0300, /* ` - g */ 5835811Smarc 0300,0100,0100,0300,0100,0300,0300,0100, /* h - o */ 5935811Smarc 0300,0100,0100,0300,0100,0300,0300,0100, /* p - w */ 6035811Smarc 0100,0300,0300,0000,0200,0000,0000,0201, /* x - del */ 617436Skre /* 6235811Smarc * meta chars 637436Skre */ 6435811Smarc 0001,0201,0201,0001,0201,0001,0001,0201, /* nul - bel */ 6535811Smarc 0202,0004,0003,0201,0005,0206,0201,0001, /* bs - si */ 6635811Smarc 0201,0001,0001,0201,0001,0201,0201,0001, /* dle - etb */ 6735811Smarc 0001,0201,0201,0001,0201,0001,0001,0201, /* can - us */ 6835811Smarc 0200,0000,0000,0200,0000,0200,0200,0000, /* sp - ' */ 6935811Smarc 0000,0200,0200,0000,0200,0000,0000,0200, /* ( - / */ 7035811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* 0 - 7 */ 7135811Smarc 0300,0100,0000,0200,0000,0200,0200,0000, /* 8 - ? */ 7235811Smarc 0200,0100,0100,0300,0100,0300,0300,0100, /* @ - G */ 7335811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* H - O */ 7435811Smarc 0100,0300,0300,0100,0300,0100,0100,0300, /* P - W */ 7535811Smarc 0300,0100,0100,0200,0000,0200,0200,0300, /* X - _ */ 7635811Smarc 0000,0300,0300,0100,0300,0100,0100,0300, /* ` - g */ 7735811Smarc 0300,0100,0100,0300,0100,0300,0300,0100, /* h - o */ 7835811Smarc 0300,0100,0100,0300,0100,0300,0300,0100, /* p - w */ 7935811Smarc 0100,0300,0300,0000,0200,0000,0000,0201, /* x - del */ 807436Skre }; 817436Skre 8235811Smarc extern struct tty *constty; /* temporary virtual console */ 8335811Smarc extern char partab[], maptab[]; 8435811Smarc 85146Sbill /* 8635811Smarc * Is 'c' a line delimiter ("break" character)? 8739Sbill */ 8840712Skarels #define ttbreakc(c) ((c) == '\n' || ((c) == cc[VEOF] || \ 8940712Skarels (c) == cc[VEOL] || (c) == cc[VEOL2]) && (c) != _POSIX_VDISABLE) 9039Sbill 9139Sbill ttychars(tp) 929578Ssam struct tty *tp; 9339Sbill { 9435811Smarc bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars)); 9539Sbill } 9639Sbill 9739Sbill /* 98903Sbill * Wait for output to drain, then flush input waiting. 9939Sbill */ 10012752Ssam ttywflush(tp) 10137584Smarc struct tty *tp; 10239Sbill { 10340712Skarels int error; 10439Sbill 10540712Skarels if ((error = ttywait(tp)) == 0) 10640712Skarels ttyflush(tp, FREAD); 10740712Skarels return (error); 10812752Ssam } 10912752Ssam 11035811Smarc /* 11135811Smarc * Wait for output to drain. 11235811Smarc */ 11312752Ssam ttywait(tp) 11412752Ssam register struct tty *tp; 11512752Ssam { 11640712Skarels int error = 0, s = spltty(); 11712752Ssam 11813809Ssam while ((tp->t_outq.c_cc || tp->t_state&TS_BUSY) && 11937584Smarc (tp->t_state&TS_CARR_ON || tp->t_cflag&CLOCAL) && 12037584Smarc tp->t_oproc) { 121903Sbill (*tp->t_oproc)(tp); 1225408Swnj tp->t_state |= TS_ASLEEP; 12340712Skarels if (error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, 12440712Skarels ttyout, 0)) 12540712Skarels break; 126903Sbill } 1279859Ssam splx(s); 12840712Skarels return (error); 12939Sbill } 13039Sbill 13139Sbill /* 1329578Ssam * Flush all TTY queues 13339Sbill */ 13412752Ssam ttyflush(tp, rw) 1357625Ssam register struct tty *tp; 13639Sbill { 137903Sbill register s; 138903Sbill 13917545Skarels s = spltty(); 140903Sbill if (rw & FREAD) { 141903Sbill while (getc(&tp->t_canq) >= 0) 142903Sbill ; 14337584Smarc ttwakeup(tp); 144903Sbill } 145903Sbill if (rw & FWRITE) { 14637584Smarc wakeup((caddr_t)&tp->t_outq); /* XXX? what about selwakeup? */ 1475408Swnj tp->t_state &= ~TS_TTSTOP; 1485426Swnj (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw); 149903Sbill while (getc(&tp->t_outq) >= 0) 150903Sbill ; 151903Sbill } 152903Sbill if (rw & FREAD) { 153903Sbill while (getc(&tp->t_rawq) >= 0) 154903Sbill ; 1559578Ssam tp->t_rocount = 0; 156903Sbill tp->t_rocol = 0; 1579578Ssam tp->t_state &= ~TS_LOCAL; 158903Sbill } 159903Sbill splx(s); 16039Sbill } 16139Sbill 162903Sbill /* 163903Sbill * Send stop character on input overflow. 164903Sbill */ 165903Sbill ttyblock(tp) 1667625Ssam register struct tty *tp; 16739Sbill { 168903Sbill register x; 1699578Ssam 170903Sbill x = tp->t_rawq.c_cc + tp->t_canq.c_cc; 171903Sbill if (tp->t_rawq.c_cc > TTYHOG) { 17212752Ssam ttyflush(tp, FREAD|FWRITE); 1735408Swnj tp->t_state &= ~TS_TBLOCK; 174903Sbill } 17515118Skarels /* 17615118Skarels * Block further input iff: 17715118Skarels * Current input > threshold AND input is available to user program 17815118Skarels */ 17916055Skarels if (x >= TTYHOG/2 && 18040712Skarels ((tp->t_lflag&ICANON) == 0) || (tp->t_canq.c_cc > 0) && 18135811Smarc tp->t_cc[VSTOP] != _POSIX_VDISABLE) { 18235811Smarc if (putc(tp->t_cc[VSTOP], &tp->t_outq)==0) { 18315118Skarels tp->t_state |= TS_TBLOCK; 18415118Skarels ttstart(tp); 18515118Skarels } 186903Sbill } 18739Sbill } 18839Sbill 18939Sbill /* 190903Sbill * Restart typewriter output following a delay 191903Sbill * timeout. 192903Sbill * The name of the routine is passed to the timeout 193903Sbill * subroutine and it is called during a clock interrupt. 194121Sbill */ 195903Sbill ttrstrt(tp) 19637584Smarc struct tty *tp; 197121Sbill { 198121Sbill 19940712Skarels #ifdef DIAGNOSTIC 2009578Ssam if (tp == 0) 2019578Ssam panic("ttrstrt"); 20240712Skarels #endif 2035408Swnj tp->t_state &= ~TS_TIMEOUT; 204903Sbill ttstart(tp); 205121Sbill } 206121Sbill 207121Sbill /* 208903Sbill * Start output on the typewriter. It is used from the top half 209903Sbill * after some characters have been put on the output queue, 210903Sbill * from the interrupt routine to transmit the next 211903Sbill * character, and after a timeout has finished. 21239Sbill */ 213903Sbill ttstart(tp) 21437584Smarc struct tty *tp; 21539Sbill { 21639Sbill 21732067Skarels if (tp->t_oproc) /* kludge for pty */ 218903Sbill (*tp->t_oproc)(tp); 21939Sbill } 22039Sbill 22139Sbill /* 222903Sbill * Common code for tty ioctls. 22339Sbill */ 2241780Sbill /*ARGSUSED*/ 2257625Ssam ttioctl(tp, com, data, flag) 2267625Ssam register struct tty *tp; 2277625Ssam caddr_t data; 22839Sbill { 22939Sbill extern int nldisp; 23037554Smckusick int s, error; 23139Sbill 232903Sbill /* 233903Sbill * If the ioctl involves modification, 23417545Skarels * hang if in the background. 235903Sbill */ 2367625Ssam switch (com) { 23739Sbill 23835811Smarc case TIOCSETD: 239903Sbill case TIOCFLUSH: 24035811Smarc /*case TIOCSPGRP:*/ 2419325Ssam case TIOCSTI: 24217598Sbloom case TIOCSWINSZ: 24335811Smarc case TIOCSETA: 24435811Smarc case TIOCSETAW: 24535811Smarc case TIOCSETAF: 24639407Smarc /**** these get removed **** 24735811Smarc case TIOCSETAS: 24835811Smarc case TIOCSETAWS: 24935811Smarc case TIOCSETAFS: 25039407Smarc /***************************/ 25140030Smarc #ifdef COMPAT_43 25240030Smarc case TIOCSETP: 25340030Smarc case TIOCSETN: 25440030Smarc case TIOCSETC: 25540030Smarc case TIOCSLTC: 25640030Smarc case TIOCLBIS: 25740030Smarc case TIOCLBIC: 25840030Smarc case TIOCLSET: 25940030Smarc case OTIOCSETD: 26040030Smarc #endif 26139555Smarc while (isbackground(u.u_procp, tp) && 26235811Smarc u.u_procp->p_pgrp->pg_jobc && 263903Sbill (u.u_procp->p_flag&SVFORK) == 0 && 26440712Skarels (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 && 26540712Skarels (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0) { 26635811Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU); 26740712Skarels if (error = tsleep((caddr_t)&lbolt, TTOPRI | PCATCH, 26840712Skarels ttybg, 0)) 26940712Skarels return (error); 270903Sbill } 271903Sbill break; 272903Sbill } 273903Sbill 2749578Ssam /* 2759578Ssam * Process the ioctl. 2769578Ssam */ 2777625Ssam switch (com) { 278903Sbill 2798556Sroot /* get discipline number */ 28039Sbill case TIOCGETD: 2817625Ssam *(int *)data = tp->t_line; 28239Sbill break; 28339Sbill 2848556Sroot /* set line discipline */ 2857625Ssam case TIOCSETD: { 2867625Ssam register int t = *(int *)data; 28735811Smarc dev_t dev = tp->t_dev; 2887625Ssam 28935811Smarc if ((unsigned)t >= nldisp) 29010851Ssam return (ENXIO); 29125584Skarels if (t != tp->t_line) { 29225584Skarels s = spltty(); 29325584Skarels (*linesw[tp->t_line].l_close)(tp); 29425584Skarels error = (*linesw[t].l_open)(dev, tp); 29525584Skarels if (error) { 29635811Smarc (void)(*linesw[tp->t_line].l_open)(dev, tp); 29725584Skarels splx(s); 29825584Skarels return (error); 29925584Skarels } 30025584Skarels tp->t_line = t; 30110851Ssam splx(s); 30210851Ssam } 30339Sbill break; 3047625Ssam } 30539Sbill 3068556Sroot /* prevent more opens on channel */ 3075614Swnj case TIOCEXCL: 3085614Swnj tp->t_state |= TS_XCLUDE; 3095614Swnj break; 3105614Swnj 3115614Swnj case TIOCNXCL: 3125614Swnj tp->t_state &= ~TS_XCLUDE; 3135614Swnj break; 3145614Swnj 31539Sbill case TIOCHPCL: 31635811Smarc tp->t_cflag |= HUPCL; 31739Sbill break; 31839Sbill 3193942Sbugs case TIOCFLUSH: { 3207625Ssam register int flags = *(int *)data; 3217625Ssam 3227625Ssam if (flags == 0) 3233942Sbugs flags = FREAD|FWRITE; 3247625Ssam else 3257625Ssam flags &= FREAD|FWRITE; 32612752Ssam ttyflush(tp, flags); 32739Sbill break; 3283944Sbugs } 32939Sbill 33037584Smarc case FIOASYNC: 33137584Smarc if (*(int *)data) 33237584Smarc tp->t_state |= TS_ASYNC; 33337584Smarc else 33437584Smarc tp->t_state &= ~TS_ASYNC; 33537584Smarc break; 33637584Smarc 33737584Smarc case FIONBIO: 33837584Smarc break; /* XXX remove */ 33937584Smarc 3408556Sroot /* return number of characters immediately available */ 3417625Ssam case FIONREAD: 3427625Ssam *(off_t *)data = ttnread(tp); 343174Sbill break; 344174Sbill 34513077Ssam case TIOCOUTQ: 34613077Ssam *(int *)data = tp->t_outq.c_cc; 34713077Ssam break; 34813077Ssam 3498589Sroot case TIOCSTOP: 35017545Skarels s = spltty(); 3519578Ssam if ((tp->t_state&TS_TTSTOP) == 0) { 3525573Swnj tp->t_state |= TS_TTSTOP; 3535573Swnj (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 3545573Swnj } 3557625Ssam splx(s); 3565573Swnj break; 3575573Swnj 3588589Sroot case TIOCSTART: 35917545Skarels s = spltty(); 36035811Smarc if ((tp->t_state&TS_TTSTOP) || (tp->t_lflag&FLUSHO)) { 3615573Swnj tp->t_state &= ~TS_TTSTOP; 36235811Smarc tp->t_lflag &= ~FLUSHO; 3635573Swnj ttstart(tp); 3645573Swnj } 3657625Ssam splx(s); 3665573Swnj break; 3675573Swnj 3689325Ssam /* 3699325Ssam * Simulate typing of a character at the terminal. 3709325Ssam */ 3719325Ssam case TIOCSTI: 37217183Smckusick if (u.u_uid && (flag & FREAD) == 0) 37317183Smckusick return (EPERM); 37439555Smarc if (u.u_uid && !isctty(u.u_procp, tp)) 3759325Ssam return (EACCES); 3769578Ssam (*linesw[tp->t_line].l_rint)(*(char *)data, tp); 3779325Ssam break; 3789325Ssam 37935811Smarc case TIOCGETA: { 38035811Smarc struct termios *t = (struct termios *)data; 38112752Ssam 38235811Smarc bcopy(&tp->t_termios, t, sizeof(struct termios)); 38335811Smarc break; 38435811Smarc } 38535811Smarc 38639407Smarc /*** THIS ALL GETS REMOVED ***/ 38739407Smarc case JUNK_TIOCSETAS: 38839407Smarc case JUNK_TIOCSETAWS: 38939407Smarc case JUNK_TIOCSETAFS: 39039407Smarc ((struct termios *)data)->c_cflag |= CIGNORE; 39139407Smarc switch(com) { 39239407Smarc case JUNK_TIOCSETAS: 39339407Smarc com = TIOCSETA; 39439407Smarc break; 39539407Smarc case JUNK_TIOCSETAWS: 39639407Smarc com = TIOCSETAW; 39739407Smarc break; 39839407Smarc case JUNK_TIOCSETAFS: 39939407Smarc com = TIOCSETAF; 40039407Smarc break; 40139407Smarc } 40239407Smarc /*******************************/ 40339407Smarc /*FALLTHROGH*/ 40435811Smarc case TIOCSETA: 40535811Smarc case TIOCSETAW: 40637584Smarc case TIOCSETAF: { 40735811Smarc register struct termios *t = (struct termios *)data; 40840712Skarels 40917545Skarels s = spltty(); 41039407Smarc if (com == TIOCSETAW || com == TIOCSETAF) { 41140712Skarels if (error = ttywait(tp)) { 41240712Skarels splx(s); 41340712Skarels return (error); 41440712Skarels } 41539407Smarc if (com == TIOCSETAF); 41639407Smarc ttyflush(tp, FREAD); 41739407Smarc } 41840712Skarels if ((t->c_cflag&CIGNORE) == 0) { 41935811Smarc /* 42035811Smarc * set device hardware 42135811Smarc */ 42237584Smarc if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 42337584Smarc splx(s); 42435811Smarc return (error); 42537584Smarc } else { 42640712Skarels if ((tp->t_state&TS_CARR_ON) == 0 && 42737584Smarc (tp->t_cflag&CLOCAL) && 42840712Skarels (t->c_cflag&CLOCAL) == 0) { 42937584Smarc tp->t_state &= ~TS_ISOPEN; 43037584Smarc tp->t_state |= TS_WOPEN; 43137584Smarc ttwakeup(tp); 43237584Smarc } 43335811Smarc tp->t_cflag = t->c_cflag; 43435811Smarc tp->t_ispeed = t->c_ispeed; 43535811Smarc tp->t_ospeed = t->c_ospeed; 43634492Skarels } 43735811Smarc ttsetwater(tp); 43812752Ssam } 43939407Smarc if (com != TIOCSETAF) { 44035811Smarc if ((t->c_lflag&ICANON) != (tp->t_lflag&ICANON)) 44135811Smarc if (t->c_lflag&ICANON) { 44235811Smarc tp->t_lflag |= PENDIN; 44335811Smarc ttwakeup(tp); 44435811Smarc } 44535811Smarc else { 44635811Smarc struct clist tq; 44735811Smarc 44835811Smarc catq(&tp->t_rawq, &tp->t_canq); 44935811Smarc tq = tp->t_rawq; 45035811Smarc tp->t_rawq = tp->t_canq; 45135811Smarc tp->t_canq = tq; 45235811Smarc } 45312752Ssam } 45435811Smarc tp->t_iflag = t->c_iflag; 45535811Smarc tp->t_oflag = t->c_oflag; 45635811Smarc tp->t_lflag = t->c_lflag; 45735811Smarc bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 45812752Ssam splx(s); 45912752Ssam break; 46012752Ssam } 46112752Ssam 46212752Ssam /* 46339555Smarc * Set controlling terminal. 46439555Smarc * Session ctty vnode pointer set in vnode layer. 46534492Skarels */ 46635811Smarc case TIOCSCTTY: { 46735811Smarc register struct proc *p = u.u_procp; 46834492Skarels 46939555Smarc if (!SESS_LEADER(p) || 47039555Smarc (p->p_session->s_ttyvp || tp->t_session) && 47139555Smarc (tp->t_session != p->p_session)) 47239407Smarc return (EPERM); 47335811Smarc tp->t_session = p->p_session; 47439555Smarc tp->t_pgrp = p->p_pgrp; 47539555Smarc p->p_session->s_ttyp = tp; 47639555Smarc p->p_flag |= SCTTY; 47734492Skarels break; 47835811Smarc } 47939555Smarc 48034492Skarels /* 48135811Smarc * Set terminal process group. 48217545Skarels */ 48318650Sbloom case TIOCSPGRP: { 48435811Smarc register struct proc *p = u.u_procp; 48535811Smarc register struct pgrp *pgrp = pgfind(*(int *)data); 48617545Skarels 48739555Smarc if (!isctty(p, tp)) 48839555Smarc return (ENOTTY); 48940030Smarc else if (pgrp == NULL || pgrp->pg_session != p->p_session) 49039555Smarc return (EPERM); 49139555Smarc tp->t_pgrp = pgrp; 49212752Ssam break; 49318650Sbloom } 49412752Ssam 49512752Ssam case TIOCGPGRP: 49639555Smarc if (!isctty(u.u_procp, tp)) 49739555Smarc return (ENOTTY); 49839555Smarc *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 49912752Ssam break; 50012752Ssam 50117598Sbloom case TIOCSWINSZ: 50218650Sbloom if (bcmp((caddr_t)&tp->t_winsize, data, 50318650Sbloom sizeof (struct winsize))) { 50417598Sbloom tp->t_winsize = *(struct winsize *)data; 50539555Smarc pgsignal(tp->t_pgrp, SIGWINCH); 50617598Sbloom } 50717598Sbloom break; 50817598Sbloom 50917598Sbloom case TIOCGWINSZ: 51017598Sbloom *(struct winsize *)data = tp->t_winsize; 51117598Sbloom break; 51217598Sbloom 51330534Skarels case TIOCCONS: 51430534Skarels if (*(int *)data) { 515*42141Smckusick if (constty && constty != tp && 516*42141Smckusick (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) == 517*42141Smckusick (TS_CARR_ON|TS_ISOPEN)) 51830534Skarels return (EBUSY); 51930534Skarels #ifndef UCONSOLE 52037554Smckusick if (error = suser(u.u_cred, &u.u_acflag)) 52137554Smckusick return (error); 52230534Skarels #endif 52330534Skarels constty = tp; 52430534Skarels } else if (tp == constty) 52533404Skarels constty = NULL; 52630534Skarels break; 52730534Skarels 52835811Smarc #ifdef COMPAT_43 52935811Smarc case TIOCGETP: 53035811Smarc case TIOCSETP: 53135811Smarc case TIOCSETN: 53235811Smarc case TIOCGETC: 53335811Smarc case TIOCSETC: 53435811Smarc case TIOCSLTC: 53535811Smarc case TIOCGLTC: 53635811Smarc case TIOCLBIS: 53735811Smarc case TIOCLBIC: 53835811Smarc case TIOCLSET: 53935811Smarc case TIOCLGET: 54039407Smarc case OTIOCGETD: 54139407Smarc case OTIOCSETD: 54235811Smarc return(ttcompat(tp, com, data, flag)); 54335811Smarc #endif 54435811Smarc 54539Sbill default: 5468556Sroot return (-1); 54739Sbill } 5488556Sroot return (0); 54939Sbill } 5504484Swnj 5514484Swnj ttnread(tp) 5524484Swnj struct tty *tp; 5534484Swnj { 5544484Swnj int nread = 0; 5554484Swnj 55635811Smarc if (tp->t_lflag & PENDIN) 5574484Swnj ttypend(tp); 5584484Swnj nread = tp->t_canq.c_cc; 55935811Smarc if ((tp->t_lflag & ICANON) == 0) 5604484Swnj nread += tp->t_rawq.c_cc; 5614484Swnj return (nread); 5624484Swnj } 5634484Swnj 5645408Swnj ttselect(dev, rw) 5654484Swnj dev_t dev; 5665408Swnj int rw; 5674484Swnj { 5684484Swnj register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)]; 5694484Swnj int nread; 57017545Skarels int s = spltty(); 5714484Swnj 5725408Swnj switch (rw) { 5734484Swnj 5744484Swnj case FREAD: 5754484Swnj nread = ttnread(tp); 57637584Smarc if (nread > 0 || 57740712Skarels ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0)) 5785408Swnj goto win; 5794938Swnj if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait) 5805408Swnj tp->t_state |= TS_RCOLL; 5814484Swnj else 5824484Swnj tp->t_rsel = u.u_procp; 5835408Swnj break; 5844484Swnj 5855408Swnj case FWRITE: 58635811Smarc if (tp->t_outq.c_cc <= tp->t_lowat) 5875408Swnj goto win; 5885408Swnj if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait) 5895408Swnj tp->t_state |= TS_WCOLL; 5905408Swnj else 5915408Swnj tp->t_wsel = u.u_procp; 5925408Swnj break; 5934484Swnj } 5945408Swnj splx(s); 5955408Swnj return (0); 5965408Swnj win: 5975408Swnj splx(s); 5985408Swnj return (1); 5994484Swnj } 6007436Skre 6017502Sroot /* 60225391Skarels * Initial open of tty, or (re)entry to line discipline. 6037502Sroot */ 6047502Sroot ttyopen(dev, tp) 6057625Ssam dev_t dev; 6067625Ssam register struct tty *tp; 6077502Sroot { 6087502Sroot 6097502Sroot tp->t_dev = dev; 61035811Smarc 6117502Sroot tp->t_state &= ~TS_WOPEN; 61217545Skarels if ((tp->t_state & TS_ISOPEN) == 0) { 61317545Skarels tp->t_state |= TS_ISOPEN; 61417598Sbloom bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize)); 61517545Skarels } 6168556Sroot return (0); 6177502Sroot } 6187502Sroot 6197502Sroot /* 62025391Skarels * "close" a line discipline 62125391Skarels */ 62225391Skarels ttylclose(tp) 62325391Skarels register struct tty *tp; 62425391Skarels { 62525391Skarels 62625391Skarels ttywflush(tp); 62725391Skarels } 62825391Skarels 62925391Skarels /* 6307502Sroot * clean tp on last close 6317502Sroot */ 6327502Sroot ttyclose(tp) 6337625Ssam register struct tty *tp; 6347502Sroot { 63530534Skarels if (constty == tp) 63630534Skarels constty = NULL; 63725391Skarels ttyflush(tp, FREAD|FWRITE); 63839555Smarc tp->t_session = NULL; 63939555Smarc tp->t_pgrp = NULL; 6407502Sroot tp->t_state = 0; 64140712Skarels return (0); 6427502Sroot } 6437502Sroot 6447502Sroot /* 64525391Skarels * Handle modem control transition on a tty. 64625391Skarels * Flag indicates new state of carrier. 64725391Skarels * Returns 0 if the line should be turned off, otherwise 1. 64825391Skarels */ 64925391Skarels ttymodem(tp, flag) 65025391Skarels register struct tty *tp; 65125391Skarels { 65225391Skarels 65335811Smarc if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag & MDMBUF)) { 65425391Skarels /* 65525391Skarels * MDMBUF: do flow control according to carrier flag 65625391Skarels */ 65725391Skarels if (flag) { 65825391Skarels tp->t_state &= ~TS_TTSTOP; 65925391Skarels ttstart(tp); 66025391Skarels } else if ((tp->t_state&TS_TTSTOP) == 0) { 66125391Skarels tp->t_state |= TS_TTSTOP; 66225391Skarels (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 66325391Skarels } 66425391Skarels } else if (flag == 0) { 66525391Skarels /* 66625391Skarels * Lost carrier. 66725391Skarels */ 66825391Skarels tp->t_state &= ~TS_CARR_ON; 66925391Skarels if (tp->t_state & TS_ISOPEN) { 67035811Smarc if ((tp->t_lflag & NOHANG) == 0) { 67139555Smarc pgsignal(tp->t_pgrp, SIGHUP); 67239555Smarc pgsignal(tp->t_pgrp, SIGCONT); 67325391Skarels ttyflush(tp, FREAD|FWRITE); 67425391Skarels return (0); 67525391Skarels } 67625391Skarels } 67725391Skarels } else { 67825391Skarels /* 67925391Skarels * Carrier now on. 68025391Skarels */ 68125391Skarels tp->t_state |= TS_CARR_ON; 68237584Smarc ttwakeup(tp); 68325391Skarels } 68425391Skarels return (1); 68525391Skarels } 68625391Skarels 68725391Skarels /* 68825404Skarels * Default modem control routine (for other line disciplines). 68925404Skarels * Return argument flag, to turn off device on carrier drop. 69025404Skarels */ 69125415Skarels nullmodem(tp, flag) 69225415Skarels register struct tty *tp; 69325404Skarels int flag; 69425404Skarels { 69525404Skarels 69625404Skarels if (flag) 69725404Skarels tp->t_state |= TS_CARR_ON; 69839407Smarc else { 69925404Skarels tp->t_state &= ~TS_CARR_ON; 70039407Smarc if ((tp->t_lflag & NOHANG) == 0) 70139555Smarc pgsignal(tp->t_pgrp, SIGHUP); 70239407Smarc } 70325404Skarels return (flag); 70425404Skarels } 70525404Skarels 70625404Skarels /* 7077502Sroot * reinput pending characters after state switch 70817545Skarels * call at spltty(). 7097502Sroot */ 7107502Sroot ttypend(tp) 7117625Ssam register struct tty *tp; 7127502Sroot { 7137502Sroot struct clist tq; 7147502Sroot register c; 7157502Sroot 71635811Smarc tp->t_lflag &= ~PENDIN; 7179578Ssam tp->t_state |= TS_TYPEN; 7187502Sroot tq = tp->t_rawq; 7197502Sroot tp->t_rawq.c_cc = 0; 7207502Sroot tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 7217502Sroot while ((c = getc(&tq)) >= 0) 7227502Sroot ttyinput(c, tp); 7239578Ssam tp->t_state &= ~TS_TYPEN; 7247502Sroot } 7257502Sroot 7267502Sroot /* 72735811Smarc * 7289578Ssam * Place a character on raw TTY input queue, 7299578Ssam * putting in delimiters and waking up top 7309578Ssam * half as needed. Also echo if required. 7319578Ssam * The arguments are the character and the 7329578Ssam * appropriate tty structure. 7337502Sroot */ 7347502Sroot ttyinput(c, tp) 7357625Ssam register c; 7367625Ssam register struct tty *tp; 7377502Sroot { 73835811Smarc register int iflag = tp->t_iflag; 73935811Smarc register int lflag = tp->t_lflag; 74035811Smarc register u_char *cc = tp->t_cc; 74135811Smarc int i, err; 7427502Sroot 7439578Ssam /* 7449578Ssam * If input is pending take it first. 7459578Ssam */ 74635811Smarc if (lflag&PENDIN) 7477502Sroot ttypend(tp); 74835811Smarc /* 74935811Smarc * Gather stats. 75035811Smarc */ 7517502Sroot tk_nin++; 75235811Smarc if (lflag&ICANON) { 75335811Smarc tk_cancc++; 75435811Smarc tp->t_cancc++; 75535811Smarc } else { 75635811Smarc tk_rawcc++; 75735811Smarc tp->t_rawcc++; 75835811Smarc } 7599578Ssam /* 76035811Smarc * Handle exceptional conditions (break, parity, framing). 7619578Ssam */ 76235811Smarc if (err = (c&TTY_ERRORMASK)) { 76335811Smarc c &= ~TTY_ERRORMASK; 76435811Smarc if (err&TTY_FE && !c) { /* break */ 76535811Smarc if (iflag&IGNBRK) 76635811Smarc goto endcase; 76735811Smarc else if (iflag&BRKINT && lflag&ISIG && 76835811Smarc (cc[VINTR] != _POSIX_VDISABLE)) 76935811Smarc c = cc[VINTR]; 77035811Smarc else { 77135811Smarc c = 0; 77235811Smarc if (iflag&PARMRK) 77335811Smarc goto parmrk; 77435811Smarc } 77535811Smarc } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) { 77635811Smarc if (iflag&IGNPAR) 77735811Smarc goto endcase; 77835811Smarc else if (iflag&PARMRK) { 77935811Smarc parmrk: 78035811Smarc putc(0377|TTY_QUOTE, &tp->t_rawq); 78135811Smarc putc(0|TTY_QUOTE, &tp->t_rawq); 78235811Smarc putc(c|TTY_QUOTE, &tp->t_rawq); 78335811Smarc goto endcase; 78435811Smarc } else 78535811Smarc c = 0; 7867502Sroot } 7879578Ssam } 7889578Ssam /* 78935811Smarc * In tandem mode, check high water mark. 7909578Ssam */ 79135811Smarc if (iflag&IXOFF) 79235811Smarc ttyblock(tp); 79335811Smarc if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP)) 7949578Ssam c &= 0177; 7959578Ssam /* 7969578Ssam * Check for literal nexting very first 7979578Ssam */ 7989578Ssam if (tp->t_state&TS_LNCH) { 79935811Smarc c |= TTY_QUOTE; 8009578Ssam tp->t_state &= ~TS_LNCH; 8019578Ssam } 8029578Ssam /* 8039578Ssam * Scan for special characters. This code 8049578Ssam * is really just a big case statement with 8059578Ssam * non-constant cases. The bottom of the 8069578Ssam * case statement is labeled ``endcase'', so goto 8079578Ssam * it after a case match, or similar. 8089578Ssam */ 80935811Smarc /* 81035811Smarc * Control chars which aren't controlled 81135811Smarc * by ICANON, ISIG, or IXON. 81235811Smarc */ 81339407Smarc if (lflag&IEXTEN) { 81440712Skarels if (CCEQ(cc[VLNEXT], c)) { 81535811Smarc if (lflag&ECHO) { 81635811Smarc if (lflag&ECHOE) 81740712Skarels ttyoutstr("^\b", tp); 81835811Smarc else 81935811Smarc ttyecho(c, tp); 82035811Smarc } 8219578Ssam tp->t_state |= TS_LNCH; 8229578Ssam goto endcase; 8239578Ssam } 82440712Skarels if (CCEQ(cc[VFLUSHO], c)) { 82535811Smarc if (lflag&FLUSHO) 82635811Smarc tp->t_lflag &= ~FLUSHO; 8277502Sroot else { 82812752Ssam ttyflush(tp, FWRITE); 8297502Sroot ttyecho(c, tp); 8309578Ssam if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 8317502Sroot ttyretype(tp); 83235811Smarc tp->t_lflag |= FLUSHO; 8337502Sroot } 8349578Ssam goto startoutput; 8359578Ssam } 83635811Smarc } 83735811Smarc /* 83835811Smarc * Signals. 83935811Smarc */ 84035811Smarc if (lflag&ISIG) { 84135811Smarc if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 84235811Smarc if ((lflag&NOFLSH) == 0) 84335811Smarc ttyflush(tp, FREAD|FWRITE); 84435811Smarc ttyecho(c, tp); 84539555Smarc pgsignal(tp->t_pgrp, 84640712Skarels CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT); 84735811Smarc goto endcase; 84835811Smarc } 84940712Skarels if (CCEQ(cc[VSUSP], c)) { 85035811Smarc if ((lflag&NOFLSH) == 0) 85112752Ssam ttyflush(tp, FREAD); 8529578Ssam ttyecho(c, tp); 85339555Smarc pgsignal(tp->t_pgrp, SIGTSTP); 8549578Ssam goto endcase; 8559578Ssam } 8569578Ssam } 8579578Ssam /* 8589578Ssam * Handle start/stop characters. 8599578Ssam */ 86035811Smarc if (iflag&IXON) { 86140712Skarels if (CCEQ(cc[VSTOP], c)) { 86235811Smarc if ((tp->t_state&TS_TTSTOP) == 0) { 86335811Smarc tp->t_state |= TS_TTSTOP; 86435811Smarc (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 86535811Smarc return; 86635811Smarc } 86735811Smarc if (!CCEQ(cc[VSTART], c)) 86835811Smarc return; 86935811Smarc /* 87035811Smarc * if VSTART == VSTOP then toggle 87135811Smarc */ 87235811Smarc goto endcase; 8739578Ssam } 87435811Smarc if (CCEQ(cc[VSTART], c)) 87535811Smarc goto restartoutput; 8769578Ssam } 8779578Ssam /* 87835811Smarc * IGNCR, ICRNL, & INLCR 8799578Ssam */ 88035811Smarc if (c == '\r') { 88135811Smarc if (iflag&IGNCR) 88235811Smarc goto endcase; 88335811Smarc else if (iflag&ICRNL) 88435811Smarc c = '\n'; 8859578Ssam } 88635811Smarc else if (c == '\n' && iflag&INLCR) 88735811Smarc c = '\r'; 8889578Ssam /* 88935811Smarc * Non canonical mode; don't process line editing 8909578Ssam * characters; check high water mark for wakeup. 89135811Smarc * 8929578Ssam */ 89340712Skarels if ((lflag&ICANON) == 0) { 8949578Ssam if (tp->t_rawq.c_cc > TTYHOG) { 89535811Smarc if (iflag&IMAXBEL) { 89635811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 89735811Smarc (void) ttyoutput(CTRL('g'), tp); 89835811Smarc } else 89935811Smarc ttyflush(tp, FREAD | FWRITE); 90035811Smarc } else { 90135811Smarc if (putc(c, &tp->t_rawq) >= 0) { 90235811Smarc ttwakeup(tp); 90335811Smarc ttyecho(c, tp); 90435811Smarc } 9057502Sroot } 9069578Ssam goto endcase; 9079578Ssam } 9089578Ssam /* 90935811Smarc * From here on down canonical mode character 9109578Ssam * processing takes place. 9119578Ssam */ 91235811Smarc /* 91335811Smarc * erase (^H / ^?) 91435811Smarc */ 91539407Smarc if (CCEQ(cc[VERASE], c)) { 9169578Ssam if (tp->t_rawq.c_cc) 9179578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9189578Ssam goto endcase; 9199578Ssam } 92035811Smarc /* 92135811Smarc * kill (^U) 92235811Smarc */ 92335811Smarc if (CCEQ(cc[VKILL], c)) { 92437584Smarc if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount && 92540712Skarels (lflag&ECHOPRT) == 0) { 9269578Ssam while (tp->t_rawq.c_cc) 9279578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9289578Ssam } else { 9299578Ssam ttyecho(c, tp); 93035811Smarc if (lflag&ECHOK || lflag&ECHOKE) 93135811Smarc ttyecho('\n', tp); 9329578Ssam while (getc(&tp->t_rawq) > 0) 9339578Ssam ; 9349578Ssam tp->t_rocount = 0; 9359578Ssam } 9369578Ssam tp->t_state &= ~TS_LOCAL; 9379578Ssam goto endcase; 9389578Ssam } 9399578Ssam /* 94035811Smarc * word erase (^W) 9419578Ssam */ 94235811Smarc if (CCEQ(cc[VWERASE], c)) { 94335811Smarc int ctype; 94435811Smarc 94535811Smarc #define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0) 94635811Smarc /* 94735811Smarc * erase whitespace 94835811Smarc */ 94935811Smarc while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 95035811Smarc ttyrub(c, tp); 95135811Smarc if (c == -1) 95234492Skarels goto endcase; 95335811Smarc /* 95435811Smarc * special case last char of token 95535811Smarc */ 95635811Smarc ttyrub(c, tp); 95735811Smarc c = unputc(&tp->t_rawq); 95835811Smarc if (c == -1 || c == ' ' || c == '\t') { 95935811Smarc if (c != -1) 96035811Smarc (void) putc(c, &tp->t_rawq); 96134492Skarels goto endcase; 96234492Skarels } 96335811Smarc /* 96435811Smarc * erase rest of token 96535811Smarc */ 96635811Smarc ctype = CTYPE(c); 96735811Smarc do { 96835811Smarc ttyrub(c, tp); 96935811Smarc c = unputc(&tp->t_rawq); 97035811Smarc if (c == -1) 97135811Smarc goto endcase; 97235811Smarc } while (c != ' ' && c != '\t' && CTYPE(c) == ctype); 97335811Smarc (void) putc(c, &tp->t_rawq); 97435811Smarc goto endcase; 97535811Smarc #undef CTYPE 9769578Ssam } 9779578Ssam /* 97835811Smarc * reprint line (^R) 97935811Smarc */ 98035811Smarc if (CCEQ(cc[VREPRINT], c)) { 98135811Smarc ttyretype(tp); 98235811Smarc goto endcase; 98335811Smarc } 984*42141Smckusick if (CCEQ(cc[VINFO], c)) { 985*42141Smckusick pgsignal(tp->t_pgrp, SIGINFO); 986*42141Smckusick if ((lflag&NOKERNINFO) == 0) 987*42141Smckusick ttyinfo(tp); 988*42141Smckusick goto endcase; 989*42141Smckusick } 99035811Smarc /* 9919578Ssam * Check for input buffer overflow 9929578Ssam */ 99310391Ssam if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) { 99435811Smarc if (iflag&IMAXBEL) { 99535811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 99635811Smarc (void) ttyoutput(CTRL('g'), tp); 99735811Smarc } else 99835811Smarc ttyflush(tp, FREAD | FWRITE); 9999578Ssam goto endcase; 100010391Ssam } 10019578Ssam /* 10029578Ssam * Put data char in q for user and 10039578Ssam * wakeup on seeing a line delimiter. 10049578Ssam */ 10059578Ssam if (putc(c, &tp->t_rawq) >= 0) { 100635811Smarc if (ttbreakc(c)) { 10079578Ssam tp->t_rocount = 0; 10089578Ssam catq(&tp->t_rawq, &tp->t_canq); 10097502Sroot ttwakeup(tp); 10109578Ssam } else if (tp->t_rocount++ == 0) 10119578Ssam tp->t_rocol = tp->t_col; 10129578Ssam if (tp->t_state&TS_ERASE) { 101335811Smarc /* 101435811Smarc * end of prterase \.../ 101535811Smarc */ 10169578Ssam tp->t_state &= ~TS_ERASE; 10179578Ssam (void) ttyoutput('/', tp); 10189578Ssam } 10199578Ssam i = tp->t_col; 10207502Sroot ttyecho(c, tp); 102135811Smarc if (CCEQ(cc[VEOF], c) && lflag&ECHO) { 102235811Smarc /* 102335811Smarc * Place the cursor over the '^' of the ^D. 102435811Smarc */ 10259578Ssam i = MIN(2, tp->t_col - i); 10269578Ssam while (i > 0) { 10279578Ssam (void) ttyoutput('\b', tp); 10289578Ssam i--; 10299578Ssam } 10309578Ssam } 10317502Sroot } 10329578Ssam endcase: 10339578Ssam /* 103435811Smarc * IXANY means allow any character to restart output. 10359578Ssam */ 103640712Skarels if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 && 103740712Skarels cc[VSTART] != cc[VSTOP]) 10387502Sroot return; 10399578Ssam restartoutput: 10407502Sroot tp->t_state &= ~TS_TTSTOP; 104135811Smarc tp->t_lflag &= ~FLUSHO; 10429578Ssam startoutput: 10437502Sroot ttstart(tp); 10447502Sroot } 10457502Sroot 10467502Sroot /* 10479578Ssam * Put character on TTY output queue, adding delays, 10487502Sroot * expanding tabs, and handling the CR/NL bit. 10499578Ssam * This is called both from the top half for output, 10509578Ssam * and from interrupt level for echoing. 10517502Sroot * The arguments are the character and the tty structure. 10527502Sroot * Returns < 0 if putc succeeds, otherwise returns char to resend 10537502Sroot * Must be recursive. 10547502Sroot */ 10557502Sroot ttyoutput(c, tp) 10567502Sroot register c; 10577502Sroot register struct tty *tp; 10587502Sroot { 10597502Sroot register char *colp; 10607502Sroot register ctype; 106135811Smarc register long oflag = tp->t_oflag; 106235811Smarc 106340712Skarels if ((oflag&OPOST) == 0) { 106435811Smarc if (tp->t_lflag&FLUSHO) 10657502Sroot return (-1); 10667502Sroot if (putc(c, &tp->t_outq)) 10677625Ssam return (c); 10687502Sroot tk_nout++; 106935811Smarc tp->t_outcc++; 10707502Sroot return (-1); 10717502Sroot } 107235811Smarc c &= TTY_CHARMASK; 10737502Sroot /* 10747502Sroot * Turn tabs to spaces as required 10757502Sroot */ 107635811Smarc if (c == '\t' && oflag&OXTABS ) { 10777502Sroot register int s; 10787502Sroot 10797502Sroot c = 8 - (tp->t_col&7); 108035811Smarc if ((tp->t_lflag&FLUSHO) == 0) { 108117545Skarels s = spltty(); /* don't interrupt tabs */ 10827502Sroot c -= b_to_q(" ", c, &tp->t_outq); 10837502Sroot tk_nout += c; 108435811Smarc tp->t_outcc += c; 10857502Sroot splx(s); 10867502Sroot } 10877502Sroot tp->t_col += c; 10887502Sroot return (c ? -1 : '\t'); 10897502Sroot } 109035811Smarc if (c == CEOT && oflag&ONOEOT) 109135811Smarc return(-1); 10927502Sroot tk_nout++; 109335811Smarc tp->t_outcc++; 10947502Sroot /* 10957502Sroot * turn <nl> to <cr><lf> if desired. 10967502Sroot */ 109735811Smarc if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0) 10987502Sroot return (c); 109935811Smarc if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq)) 110035811Smarc return (c); 11017502Sroot /* 11027502Sroot * Calculate delays. 11037502Sroot * The numbers here represent clock ticks 11047502Sroot * and are not necessarily optimal for all terminals. 11059578Ssam * 11069578Ssam * SHOULD JUST ALLOW USER TO SPECIFY DELAYS 110735811Smarc * 110835811Smarc * (actually, should THROW AWAY terminals which need delays) 11097502Sroot */ 11107502Sroot colp = &tp->t_col; 11117502Sroot ctype = partab[c]; 11127502Sroot c = 0; 11137502Sroot switch (ctype&077) { 11147502Sroot 11157502Sroot case ORDINARY: 11167502Sroot (*colp)++; 11177502Sroot 11187502Sroot case CONTROL: 11197502Sroot break; 11207502Sroot 11217502Sroot case BACKSPACE: 11227502Sroot if (*colp) 11237502Sroot (*colp)--; 11247502Sroot break; 11257502Sroot 112613821Ssam /* 112713821Ssam * This macro is close enough to the correct thing; 112813821Ssam * it should be replaced by real user settable delays 112913821Ssam * in any event... 113013821Ssam */ 113113821Ssam #define mstohz(ms) (((ms) * hz) >> 10) 11327502Sroot case NEWLINE: 11337502Sroot ctype = (tp->t_flags >> 8) & 03; 11347625Ssam if (ctype == 1) { /* tty 37 */ 113526357Skarels if (*colp > 0) { 113626357Skarels c = (((unsigned)*colp) >> 4) + 3; 113726357Skarels if ((unsigned)c > 6) 113826357Skarels c = 6; 113926357Skarels } 11409578Ssam } else if (ctype == 2) /* vt05 */ 114113821Ssam c = mstohz(100); 11427502Sroot *colp = 0; 11437502Sroot break; 11447502Sroot 11457502Sroot case TAB: 11467502Sroot ctype = (tp->t_flags >> 10) & 03; 11477625Ssam if (ctype == 1) { /* tty 37 */ 11487502Sroot c = 1 - (*colp | ~07); 11497625Ssam if (c < 5) 11507502Sroot c = 0; 11517502Sroot } 11527502Sroot *colp |= 07; 11537502Sroot (*colp)++; 11547502Sroot break; 11557502Sroot 11567502Sroot case VTAB: 11579578Ssam if (tp->t_flags&VTDELAY) /* tty 37 */ 11587502Sroot c = 0177; 11597502Sroot break; 11607502Sroot 11617502Sroot case RETURN: 11627502Sroot ctype = (tp->t_flags >> 12) & 03; 11639578Ssam if (ctype == 1) /* tn 300 */ 116413821Ssam c = mstohz(83); 11659578Ssam else if (ctype == 2) /* ti 700 */ 116613821Ssam c = mstohz(166); 11679578Ssam else if (ctype == 3) { /* concept 100 */ 11687502Sroot int i; 11699578Ssam 11707502Sroot if ((i = *colp) >= 0) 11719578Ssam for (; i < 9; i++) 11727502Sroot (void) putc(0177, &tp->t_outq); 11737502Sroot } 11747502Sroot *colp = 0; 11757502Sroot } 117635811Smarc if (c && (tp->t_lflag&FLUSHO) == 0) 117735811Smarc (void) putc(c|TTY_QUOTE, &tp->t_outq); 11787502Sroot return (-1); 11797502Sroot } 118013821Ssam #undef mstohz 11817502Sroot 11827502Sroot /* 11837502Sroot * Called from device's read routine after it has 11847502Sroot * calculated the tty-structure given as argument. 11857502Sroot */ 118637584Smarc ttread(tp, uio, flag) 11877625Ssam register struct tty *tp; 11887722Swnj struct uio *uio; 11897502Sroot { 11907502Sroot register struct clist *qp; 119135811Smarc register int c; 119241383Smarc register long lflag; 119335811Smarc register u_char *cc = tp->t_cc; 11949859Ssam int s, first, error = 0; 11957502Sroot 11967502Sroot loop: 119741383Smarc lflag = tp->t_lflag; 119837584Smarc s = spltty(); 11999578Ssam /* 120037584Smarc * take pending input first 12019578Ssam */ 120235811Smarc if (lflag&PENDIN) 12037502Sroot ttypend(tp); 12049859Ssam splx(s); 120540712Skarels 12069578Ssam /* 12079578Ssam * Hang process if it's in the background. 12089578Ssam */ 120939555Smarc if (isbackground(u.u_procp, tp)) { 121024392Skarels if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) || 121124392Skarels (u.u_procp->p_sigmask & sigmask(SIGTTIN)) || 121235811Smarc u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0) 12138520Sroot return (EIO); 121435811Smarc pgsignal(u.u_procp->p_pgrp, SIGTTIN); 121540712Skarels if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0)) 121640712Skarels return (error); 121723165Sbloom goto loop; 12187502Sroot } 121940712Skarels 12209578Ssam /* 122135811Smarc * If canonical, use the canonical queue, 122235811Smarc * else use the raw queue. 122337584Smarc * 122437584Smarc * XXX - should get rid of canonical queue. 122537584Smarc * (actually, should get rid of clists...) 12269578Ssam */ 122735811Smarc qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq; 122840712Skarels 12299578Ssam /* 123040712Skarels * If there is no input, sleep on rawq 123140712Skarels * awaiting hardware receipt and notification. 123240712Skarels * If we have data, we don't need to check for carrier. 12339578Ssam */ 123417545Skarels s = spltty(); 12359578Ssam if (qp->c_cc <= 0) { 123640712Skarels int carrier; 123740712Skarels 123840712Skarels carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL); 123940712Skarels if (!carrier && tp->t_state&TS_ISOPEN) { 12409859Ssam splx(s); 124140712Skarels return (0); /* EOF */ 12427502Sroot } 124337728Smckusick if (flag & IO_NDELAY) { 124437584Smarc splx(s); 124537584Smarc return (EWOULDBLOCK); 124637584Smarc } 124740712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 124840712Skarels carrier ? ttyin : ttopen, 0); 12499859Ssam splx(s); 125040712Skarels if (error) 125140712Skarels return (error); 12529578Ssam goto loop; 12539578Ssam } 12549859Ssam splx(s); 125540712Skarels 12569578Ssam /* 125735811Smarc * Input present, check for input mapping and processing. 12589578Ssam */ 12599578Ssam first = 1; 12609578Ssam while ((c = getc(qp)) >= 0) { 12619578Ssam /* 126235811Smarc * delayed suspend (^Y) 12639578Ssam */ 126435811Smarc if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) { 126539555Smarc pgsignal(tp->t_pgrp, SIGTSTP); 12669578Ssam if (first) { 126740712Skarels if (error = tsleep((caddr_t)&lbolt, 126840712Skarels TTIPRI | PCATCH, ttybg, 0)) 126940712Skarels break; 12709578Ssam goto loop; 12719578Ssam } 12729578Ssam break; 12737502Sroot } 12749578Ssam /* 127535811Smarc * Interpret EOF only in canonical mode. 12769578Ssam */ 127735811Smarc if (CCEQ(cc[VEOF], c) && lflag&ICANON) 12789578Ssam break; 12799578Ssam /* 12809578Ssam * Give user character. 12819578Ssam */ 128240712Skarels error = ureadc(c, uio); 12839578Ssam if (error) 12849578Ssam break; 128514938Smckusick if (uio->uio_resid == 0) 12869578Ssam break; 12879578Ssam /* 128835811Smarc * In canonical mode check for a "break character" 12899578Ssam * marking the end of a "line of input". 12909578Ssam */ 129140712Skarels if (lflag&ICANON && ttbreakc(c)) 12929578Ssam break; 12939578Ssam first = 0; 12947502Sroot } 12959578Ssam /* 12969578Ssam * Look to unblock output now that (presumably) 12979578Ssam * the input queue has gone down. 12989578Ssam */ 129935811Smarc if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) { 130035811Smarc if (cc[VSTART] != _POSIX_VDISABLE 130135811Smarc && putc(cc[VSTART], &tp->t_outq) == 0) { 13027502Sroot tp->t_state &= ~TS_TBLOCK; 13037502Sroot ttstart(tp); 13047502Sroot } 130535811Smarc } 13068520Sroot return (error); 13077502Sroot } 13087502Sroot 13097502Sroot /* 131025391Skarels * Check the output queue on tp for space for a kernel message 131125391Skarels * (from uprintf/tprintf). Allow some space over the normal 131225391Skarels * hiwater mark so we don't lose messages due to normal flow 131325391Skarels * control, but don't let the tty run amok. 131430695Skarels * Sleeps here are not interruptible, but we return prematurely 131530695Skarels * if new signals come in. 131625391Skarels */ 131725391Skarels ttycheckoutq(tp, wait) 131825391Skarels register struct tty *tp; 131925391Skarels int wait; 132025391Skarels { 132130695Skarels int hiwat, s, oldsig; 132225391Skarels 132335811Smarc hiwat = tp->t_hiwat; 132425391Skarels s = spltty(); 132530695Skarels oldsig = u.u_procp->p_sig; 132625391Skarels if (tp->t_outq.c_cc > hiwat + 200) 132729946Skarels while (tp->t_outq.c_cc > hiwat) { 132829946Skarels ttstart(tp); 132930695Skarels if (wait == 0 || u.u_procp->p_sig != oldsig) { 133029946Skarels splx(s); 133129946Skarels return (0); 133229946Skarels } 133330695Skarels timeout(wakeup, (caddr_t)&tp->t_outq, hz); 133429946Skarels tp->t_state |= TS_ASLEEP; 133530695Skarels sleep((caddr_t)&tp->t_outq, PZERO - 1); 133625391Skarels } 133725391Skarels splx(s); 133825391Skarels return (1); 133925391Skarels } 134025391Skarels 134125391Skarels /* 13427502Sroot * Called from the device's write routine after it has 13437502Sroot * calculated the tty-structure given as argument. 13447502Sroot */ 134537584Smarc ttwrite(tp, uio, flag) 13467625Ssam register struct tty *tp; 13479578Ssam register struct uio *uio; 13487502Sroot { 13497502Sroot register char *cp; 135040712Skarels register int cc = 0, ce; 13519578Ssam int i, hiwat, cnt, error, s; 13527502Sroot char obuf[OBUFSIZ]; 13537502Sroot 135435811Smarc hiwat = tp->t_hiwat; 13559578Ssam cnt = uio->uio_resid; 13569578Ssam error = 0; 13577502Sroot loop: 135837584Smarc s = spltty(); 135940712Skarels if ((tp->t_state&TS_CARR_ON) == 0 && (tp->t_cflag&CLOCAL) == 0) { 136037584Smarc if (tp->t_state&TS_ISOPEN) { 136137584Smarc splx(s); 136237584Smarc return (EIO); 136337728Smckusick } else if (flag & IO_NDELAY) { 136437584Smarc splx(s); 136540712Skarels error = EWOULDBLOCK; 136640712Skarels goto out; 136737584Smarc } else { 136837584Smarc /* 136937584Smarc * sleep awaiting carrier 137037584Smarc */ 137140712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 137240712Skarels ttopen, 0); 137337584Smarc splx(s); 137440712Skarels if (error) 137540712Skarels goto out; 137637584Smarc goto loop; 137737584Smarc } 137837584Smarc } 137937584Smarc splx(s); 13809578Ssam /* 13819578Ssam * Hang the process if it's in the background. 13829578Ssam */ 138339555Smarc if (isbackground(u.u_procp, tp) && 138435811Smarc (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 && 138540712Skarels (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 && 138640712Skarels (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0 && 138735811Smarc u.u_procp->p_pgrp->pg_jobc) { 138835811Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU); 138940712Skarels if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0)) 139040712Skarels goto out; 139121776Sbloom goto loop; 13927502Sroot } 13939578Ssam /* 13949578Ssam * Process the user's data in at most OBUFSIZ 139540712Skarels * chunks. Perform any output translation. 139640712Skarels * Keep track of high water mark, sleep on overflow 139740712Skarels * awaiting device aid in acquiring new space. 13989578Ssam */ 139940712Skarels while (uio->uio_resid > 0 || cc > 0) { 140040712Skarels if (tp->t_lflag&FLUSHO) { 140140712Skarels uio->uio_resid = 0; 140240712Skarels return (0); 140340712Skarels } 140440712Skarels if (tp->t_outq.c_cc > hiwat) 140532067Skarels goto ovhiwat; 14069578Ssam /* 140740712Skarels * Grab a hunk of data from the user, 140840712Skarels * unless we have some leftover from last time. 14099578Ssam */ 14107822Sroot if (cc == 0) { 141140712Skarels cc = min(uio->uio_resid, OBUFSIZ); 141240712Skarels cp = obuf; 141340712Skarels error = uiomove(cp, cc, uio); 141440712Skarels if (error) { 141540712Skarels cc = 0; 141640712Skarels break; 141740712Skarels } 14187822Sroot } 14199578Ssam /* 14209578Ssam * If nothing fancy need be done, grab those characters we 14219578Ssam * can handle without any of ttyoutput's processing and 14229578Ssam * just transfer them to the output q. For those chars 14239578Ssam * which require special processing (as indicated by the 14249578Ssam * bits in partab), call ttyoutput. After processing 14259578Ssam * a hunk of data, look for FLUSHO so ^O's will take effect 14269578Ssam * immediately. 14279578Ssam */ 14289578Ssam while (cc > 0) { 142940712Skarels if ((tp->t_oflag&OPOST) == 0) 14307502Sroot ce = cc; 14317502Sroot else { 143234492Skarels ce = cc - scanc((unsigned)cc, (u_char *)cp, 143334492Skarels (u_char *)partab, 077); 14349578Ssam /* 14359578Ssam * If ce is zero, then we're processing 14369578Ssam * a special character through ttyoutput. 14379578Ssam */ 14389578Ssam if (ce == 0) { 14397502Sroot tp->t_rocount = 0; 14407502Sroot if (ttyoutput(*cp, tp) >= 0) { 144121776Sbloom /* no c-lists, wait a bit */ 144221776Sbloom ttstart(tp); 144340712Skarels if (error = tsleep((caddr_t)&lbolt, 144440712Skarels TTOPRI | PCATCH, ttybuf, 0)) 144540712Skarels break; 144621776Sbloom goto loop; 14477502Sroot } 14489578Ssam cp++, cc--; 144935811Smarc if ((tp->t_lflag&FLUSHO) || 14509578Ssam tp->t_outq.c_cc > hiwat) 14517502Sroot goto ovhiwat; 14529578Ssam continue; 14537502Sroot } 14547502Sroot } 14559578Ssam /* 14569578Ssam * A bunch of normal characters have been found, 14579578Ssam * transfer them en masse to the output queue and 14589578Ssam * continue processing at the top of the loop. 14599578Ssam * If there are any further characters in this 14609578Ssam * <= OBUFSIZ chunk, the first should be a character 14619578Ssam * requiring special handling by ttyoutput. 14629578Ssam */ 14637502Sroot tp->t_rocount = 0; 14649578Ssam i = b_to_q(cp, ce, &tp->t_outq); 14659578Ssam ce -= i; 14669578Ssam tp->t_col += ce; 14679578Ssam cp += ce, cc -= ce, tk_nout += ce; 146835811Smarc tp->t_outcc += ce; 14699578Ssam if (i > 0) { 14709578Ssam /* out of c-lists, wait a bit */ 14717502Sroot ttstart(tp); 147240712Skarels if (error = tsleep((caddr_t)&lbolt, 147340712Skarels TTOPRI | PCATCH, ttybuf, 0)) 147440712Skarels break; 147521776Sbloom goto loop; 14767502Sroot } 147735811Smarc if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat) 147840712Skarels break; 14797502Sroot } 148035811Smarc ttstart(tp); 14817502Sroot } 148240712Skarels out: 148340712Skarels /* 148440712Skarels * If cc is nonzero, we leave the uio structure inconsistent, 148540712Skarels * as the offset and iov pointers have moved forward, 148640712Skarels * but it doesn't matter (the call will either return short 148740712Skarels * or restart with a new uio). 148840712Skarels */ 148940712Skarels uio->uio_resid += cc; 14908520Sroot return (error); 149140712Skarels 14927502Sroot ovhiwat: 149332067Skarels ttstart(tp); 149432067Skarels s = spltty(); 14959578Ssam /* 149635811Smarc * This can only occur if FLUSHO is set in t_lflag, 149732067Skarels * or if ttstart/oproc is synchronous (or very fast). 14989578Ssam */ 14997502Sroot if (tp->t_outq.c_cc <= hiwat) { 15009578Ssam splx(s); 15017502Sroot goto loop; 15027502Sroot } 150337728Smckusick if (flag & IO_NDELAY) { 150417545Skarels splx(s); 150540712Skarels uio->uio_resid += cc; 15067822Sroot if (uio->uio_resid == cnt) 15078520Sroot return (EWOULDBLOCK); 15088520Sroot return (0); 15097502Sroot } 15107502Sroot tp->t_state |= TS_ASLEEP; 151140712Skarels error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 15129578Ssam splx(s); 151340712Skarels if (error) 151440712Skarels goto out; 15157502Sroot goto loop; 15167502Sroot } 15177502Sroot 15187502Sroot /* 15197502Sroot * Rubout one character from the rawq of tp 15207502Sroot * as cleanly as possible. 15217502Sroot */ 15227502Sroot ttyrub(c, tp) 15237625Ssam register c; 15247625Ssam register struct tty *tp; 15257502Sroot { 15267502Sroot register char *cp; 15277502Sroot register int savecol; 15287502Sroot int s; 15297502Sroot char *nextc(); 15307502Sroot 153135811Smarc if ((tp->t_lflag&ECHO) == 0) 15327502Sroot return; 153335811Smarc tp->t_lflag &= ~FLUSHO; 153435811Smarc if (tp->t_lflag&ECHOE) { 15357502Sroot if (tp->t_rocount == 0) { 15367502Sroot /* 15377502Sroot * Screwed by ttwrite; retype 15387502Sroot */ 15397502Sroot ttyretype(tp); 15407502Sroot return; 15417502Sroot } 154235811Smarc if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE)) 15437502Sroot ttyrubo(tp, 2); 154435811Smarc else switch (partab[c&=0377]&077) { 15457502Sroot 15467502Sroot case ORDINARY: 154735811Smarc ttyrubo(tp, 1); 15487502Sroot break; 15497502Sroot 15507502Sroot case VTAB: 15517502Sroot case BACKSPACE: 15527502Sroot case CONTROL: 15537502Sroot case RETURN: 155435811Smarc if (tp->t_lflag&ECHOCTL) 15557502Sroot ttyrubo(tp, 2); 15567502Sroot break; 15577502Sroot 155835811Smarc case TAB: { 155935811Smarc int c; 156035811Smarc 15617502Sroot if (tp->t_rocount < tp->t_rawq.c_cc) { 15627502Sroot ttyretype(tp); 15637502Sroot return; 15647502Sroot } 156517545Skarels s = spltty(); 15667502Sroot savecol = tp->t_col; 15679578Ssam tp->t_state |= TS_CNTTB; 156835811Smarc tp->t_lflag |= FLUSHO; 15697502Sroot tp->t_col = tp->t_rocol; 15709578Ssam cp = tp->t_rawq.c_cf; 157139407Smarc if (cp) 157239407Smarc c = *cp; /* XXX FIX NEXTC */ 157335811Smarc for (; cp; cp = nextc(&tp->t_rawq, cp, &c)) 157435811Smarc ttyecho(c, tp); 157535811Smarc tp->t_lflag &= ~FLUSHO; 15769578Ssam tp->t_state &= ~TS_CNTTB; 15777502Sroot splx(s); 15787502Sroot /* 15797502Sroot * savecol will now be length of the tab 15807502Sroot */ 15817502Sroot savecol -= tp->t_col; 15827502Sroot tp->t_col += savecol; 15837502Sroot if (savecol > 8) 15847502Sroot savecol = 8; /* overflow screw */ 15857502Sroot while (--savecol >= 0) 15867502Sroot (void) ttyoutput('\b', tp); 15877502Sroot break; 158835811Smarc } 15897502Sroot 15907502Sroot default: 159137584Smarc /* XXX */ 159235811Smarc printf("ttyrub: would panic c = %d, val = %d\n", 159335811Smarc c, partab[c&=0377]&077); 159435811Smarc /*panic("ttyrub");*/ 15957502Sroot } 159635811Smarc } else if (tp->t_lflag&ECHOPRT) { 15979578Ssam if ((tp->t_state&TS_ERASE) == 0) { 15987502Sroot (void) ttyoutput('\\', tp); 15999578Ssam tp->t_state |= TS_ERASE; 16007502Sroot } 16017502Sroot ttyecho(c, tp); 16027502Sroot } else 160335811Smarc ttyecho(tp->t_cc[VERASE], tp); 16047502Sroot tp->t_rocount--; 16057502Sroot } 16067502Sroot 16077502Sroot /* 16087502Sroot * Crt back over cnt chars perhaps 16097502Sroot * erasing them. 16107502Sroot */ 16117502Sroot ttyrubo(tp, cnt) 16127625Ssam register struct tty *tp; 16137625Ssam int cnt; 16147502Sroot { 16157502Sroot 16167502Sroot while (--cnt >= 0) 161740712Skarels ttyoutstr("\b \b", tp); 16187502Sroot } 16197502Sroot 16207502Sroot /* 16217502Sroot * Reprint the rawq line. 16227502Sroot * We assume c_cc has already been checked. 16237502Sroot */ 16247502Sroot ttyretype(tp) 16257625Ssam register struct tty *tp; 16267502Sroot { 16277502Sroot register char *cp; 16287502Sroot char *nextc(); 162935811Smarc int s, c; 16307502Sroot 163135811Smarc if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 163235811Smarc ttyecho(tp->t_cc[VREPRINT], tp); 16337502Sroot (void) ttyoutput('\n', tp); 163417545Skarels s = spltty(); 163535811Smarc /*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE 163635811Smarc BIT OF FIRST CHAR ****/ 163735811Smarc for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) { 163835811Smarc ttyecho(c, tp); 163935811Smarc } 164035811Smarc for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) { 164135811Smarc ttyecho(c, tp); 164235811Smarc } 16439578Ssam tp->t_state &= ~TS_ERASE; 16447502Sroot splx(s); 16457502Sroot tp->t_rocount = tp->t_rawq.c_cc; 16467502Sroot tp->t_rocol = 0; 16477502Sroot } 16487502Sroot 16497502Sroot /* 165035811Smarc * Echo a typed character to the terminal. 16517502Sroot */ 16527502Sroot ttyecho(c, tp) 16537625Ssam register c; 16547625Ssam register struct tty *tp; 16557502Sroot { 16569578Ssam if ((tp->t_state&TS_CNTTB) == 0) 165735811Smarc tp->t_lflag &= ~FLUSHO; 165840712Skarels if ((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 || c == '\n')) 16597502Sroot return; 166035811Smarc if (tp->t_lflag&ECHOCTL) { 166140712Skarels if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' || 166240712Skarels c == 0177) { 16637502Sroot (void) ttyoutput('^', tp); 166435811Smarc c &= TTY_CHARMASK; 16657502Sroot if (c == 0177) 16667502Sroot c = '?'; 16677502Sroot else 16687502Sroot c += 'A' - 1; 16697502Sroot } 16707502Sroot } 167135811Smarc (void) ttyoutput(c, tp); 16727502Sroot } 16737502Sroot 16747502Sroot /* 16757502Sroot * send string cp to tp 16767502Sroot */ 167740712Skarels ttyoutstr(cp, tp) 16787625Ssam register char *cp; 16797625Ssam register struct tty *tp; 16807502Sroot { 16817502Sroot register char c; 16827502Sroot 16837502Sroot while (c = *cp++) 16847502Sroot (void) ttyoutput(c, tp); 16857502Sroot } 16867502Sroot 16877502Sroot ttwakeup(tp) 16887502Sroot struct tty *tp; 16897502Sroot { 16907502Sroot 16917502Sroot if (tp->t_rsel) { 16927502Sroot selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL); 16937502Sroot tp->t_state &= ~TS_RCOLL; 16947502Sroot tp->t_rsel = 0; 16957502Sroot } 169612752Ssam if (tp->t_state & TS_ASYNC) 169739555Smarc pgsignal(tp->t_pgrp, SIGIO); 16987502Sroot wakeup((caddr_t)&tp->t_rawq); 16997502Sroot } 170035811Smarc 170135811Smarc /* 170235811Smarc * set tty hi and low water marks 170335811Smarc * 170435811Smarc * Try to arrange the dynamics so there's about one second 170535811Smarc * from hi to low water. 170635811Smarc * 170735811Smarc */ 170835811Smarc ttsetwater(tp) 170935811Smarc struct tty *tp; 171035811Smarc { 171135811Smarc register cps = tp->t_ospeed / 10; 171235811Smarc register x; 171335811Smarc 171435811Smarc #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x)) 171535811Smarc tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT); 171635811Smarc x += cps; 171735811Smarc x = clamp(x, TTMAXHIWAT, TTMINHIWAT); 171835811Smarc tp->t_hiwat = roundup(x, CBSIZE); 171935811Smarc #undef clamp 172035811Smarc } 172135811Smarc 172235811Smarc ttspeedtab(speed, table) 172335811Smarc struct speedtab table[]; 172435811Smarc { 172535811Smarc register int i; 172635811Smarc 172735811Smarc for (i = 0; table[i].sp_speed != -1; i++) 172835811Smarc if (table[i].sp_speed == speed) 172935811Smarc return(table[i].sp_code); 173035811Smarc return(-1); 173135811Smarc } 173239407Smarc 173341177Smarc int ttyhostname = 0; 173439407Smarc /* 173539407Smarc * (^T) 173639407Smarc * Report on state of foreground process group. 173739407Smarc */ 173839407Smarc ttyinfo(tp) 173939407Smarc struct tty *tp; 174039407Smarc { 174141177Smarc register struct proc *p, *pick = NULL; 174241177Smarc register char *cp = hostname; 174341177Smarc int x, s; 174441177Smarc struct timeval utime, stime; 174541177Smarc #define pgtok(a) ((a)/(1024/NBPG)) 174639407Smarc 174739407Smarc if (ttycheckoutq(tp,0) == 0) 174839407Smarc return; 174941177Smarc /* 175041177Smarc * hostname 175141177Smarc */ 175241177Smarc if (ttyhostname) { 175341177Smarc if (*cp == '\0') 175441177Smarc ttyprintf(tp, "amnesia"); 175541177Smarc else 175641177Smarc while (*cp && *cp != '.') 175741177Smarc tputchar(*cp++, tp); 175841177Smarc tputchar(' '); 175941177Smarc } 176041177Smarc /* 176141177Smarc * load average 176241177Smarc */ 176341177Smarc x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT; 176441177Smarc ttyprintf(tp, "load: %d.", x/100); 176541177Smarc ttyoutint(x%100, 10, 2, tp); 176639555Smarc if (tp->t_session == NULL) 176741177Smarc ttyprintf(tp, " not a controlling terminal\n"); 176841177Smarc else if (tp->t_pgrp == NULL) 176941177Smarc ttyprintf(tp, " no foreground process group\n"); 177041177Smarc else if ((p = tp->t_pgrp->pg_mem) == NULL) 177141177Smarc ttyprintf(tp, " empty foreground process group\n"); 177239407Smarc else { 177341177Smarc /* pick interesting process */ 177439407Smarc for (; p != NULL; p = p->p_pgrpnxt) { 177541177Smarc if (proc_compare(pick, p)) 177641177Smarc pick = p; 177739407Smarc } 177841177Smarc ttyprintf(tp, " cmd: %s %d [%s] ", 177941177Smarc pick->p_comm, pick->p_pid, 178041177Smarc pick->p_wmesg ? pick->p_wmesg : "running"); 178141177Smarc /* 178241177Smarc * cpu time 178341177Smarc */ 178441177Smarc if (u.u_procp == pick) 178541177Smarc s = splclock(); 178641177Smarc utime = pick->p_utime; 178741177Smarc stime = pick->p_stime; 178841177Smarc if (u.u_procp == pick) 178941177Smarc splx(s); 179041177Smarc /* user time */ 179141177Smarc x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */ 179241177Smarc ttyoutint(utime.tv_sec, 10, 1, tp); 179341177Smarc tputchar('.', tp); 179441177Smarc ttyoutint(x, 10, 2, tp); 179541177Smarc tputchar('u', tp); 179641177Smarc tputchar(' ', tp); 179741177Smarc /* system time */ 179841177Smarc x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */ 179941177Smarc ttyoutint(stime.tv_sec, 10, 1, tp); 180041177Smarc tputchar('.', tp); 180141177Smarc ttyoutint(x, 10, 2, tp); 180241177Smarc tputchar('s', tp); 180341177Smarc tputchar(' ', tp); 180441177Smarc /* 180541177Smarc * pctcpu 180641177Smarc */ 180741177Smarc x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT; 180841177Smarc ttyoutint(x/100, 10, 1, tp); 180941177Smarc #ifdef notdef /* do we really want this ??? */ 181041177Smarc tputchar('.', tp); 181141177Smarc ttyoutint(x%100, 10, 2, tp); 181241177Smarc #endif 181341177Smarc ttyprintf(tp, "%% %dk\n", pgtok(pick->p_ssize + pick->p_dsize)); 181439407Smarc } 181541177Smarc tp->t_rocount = 0; /* so pending input will be retyped if BS */ 181639407Smarc } 181739407Smarc 181841177Smarc ttyoutint(n, base, min, tp) 181941177Smarc register int n, base, min; 182041177Smarc register struct tty *tp; 182141177Smarc { 182241177Smarc char info[16]; 182341177Smarc register char *p = info; 182441177Smarc 182541177Smarc while (--min >= 0 || n) { 182641177Smarc *p++ = "0123456789abcdef"[n%base]; 182741177Smarc n /= base; 182841177Smarc } 182941177Smarc while (p > info) 183041177Smarc ttyoutput(*--p, tp); 183141177Smarc } 183241177Smarc 183341177Smarc /* 183441177Smarc * Returns 1 if p2 is "better" than p1 183541177Smarc * 183641177Smarc * The algorithm for picking the "interesting" process is thus: 183741177Smarc * 183841177Smarc * 1) (Only foreground processes are eligable - implied) 183941177Smarc * 2) Runnable processes are favored over anything 184041177Smarc * else. The runner with the highest cpu 184141177Smarc * utilization is picked (p_cpu). Ties are 184241177Smarc * broken by picking the highest pid. 184341177Smarc * 3 Next, the sleeper with the shortest sleep 184441177Smarc * time is favored. With ties, we pick out 184541177Smarc * just "short-term" sleepers (SSINTR == 0). 184641177Smarc * Further ties are broken by picking the highest 184741177Smarc * pid. 184841177Smarc * 184941177Smarc */ 185041177Smarc #define isrun(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 185141177Smarc proc_compare(p1, p2) 185241177Smarc register struct proc *p1, *p2; 185341177Smarc { 185441177Smarc 185541177Smarc if (p1 == NULL) 185641177Smarc return (1); 185741177Smarc /* 185841177Smarc * see if at least one of them is runnable 185941177Smarc */ 186041177Smarc switch (isrun(p1)<<1 | isrun(p2)) { 186141177Smarc case 0x01: 186241177Smarc return (1); 186341177Smarc case 0x10: 186441177Smarc return (0); 186541177Smarc case 0x11: 186641177Smarc /* 186741177Smarc * tie - favor one with highest recent cpu utilization 186841177Smarc */ 186941177Smarc if (p2->p_cpu > p1->p_cpu) 187041177Smarc return (1); 187141177Smarc if (p1->p_cpu > p2->p_cpu) 187241177Smarc return (0); 187341177Smarc return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 187441177Smarc } 187541177Smarc /* 187641177Smarc * pick the one with the smallest sleep time 187741177Smarc */ 187841177Smarc if (p2->p_slptime > p1->p_slptime) 187941177Smarc return (0); 188041177Smarc if (p1->p_slptime > p2->p_slptime) 188141177Smarc return (1); 188241177Smarc /* 188341177Smarc * favor one sleeping in a non-interruptible sleep 188441177Smarc */ 188541177Smarc if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0) 188641177Smarc return (1); 188741177Smarc if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0) 188841177Smarc return (0); 188941177Smarc return(p2->p_pid > p1->p_pid); /* tie - return highest pid */ 189041177Smarc } 189139407Smarc #define TOTTY 0x2 /* XXX should be in header */ 189239407Smarc /*VARARGS2*/ 189339407Smarc ttyprintf(tp, fmt, x1) 189439555Smarc struct tty *tp; 189539407Smarc char *fmt; 189639407Smarc unsigned x1; 189739407Smarc { 189839555Smarc prf(fmt, &x1, TOTTY, (caddr_t)tp); 189939407Smarc } 190039555Smarc 190139555Smarc /* 190239555Smarc * Output char to tty; console putchar style. 190339555Smarc */ 190439555Smarc tputchar(c, tp) 190539555Smarc int c; 190639555Smarc struct tty *tp; 190739555Smarc { 190839555Smarc register s = spltty(); 190939555Smarc 191039555Smarc if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN)) 191139555Smarc == (TS_CARR_ON | TS_ISOPEN)) { 191239555Smarc if (c == '\n') 191339555Smarc (void) ttyoutput('\r', tp); 191439555Smarc (void) ttyoutput(c, tp); 191539555Smarc ttstart(tp); 191639555Smarc splx(s); 191739555Smarc return (0); 191839555Smarc } 191939555Smarc splx(s); 192039555Smarc return (-1); 192139555Smarc } 1922