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*42193Smarc * @(#)tty.c 7.26 (Berkeley) 05/17/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) { 51542141Smckusick if (constty && constty != tp && 51642141Smckusick (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) == 51742141Smckusick (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 653*42193Smarc 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; 669*42193Smarc if (tp->t_state&TS_ISOPEN && (tp->t_cflag&CLOCAL) == 0) { 670*42193Smarc if (tp->t_session && tp->t_session->s_leader) 671*42193Smarc psignal(tp->t_session->s_leader, SIGHUP); 672*42193Smarc ttyflush(tp, FREAD|FWRITE); 673*42193Smarc return (0); 67425391Skarels } 67525391Skarels } else { 67625391Skarels /* 67725391Skarels * Carrier now on. 67825391Skarels */ 67925391Skarels tp->t_state |= TS_CARR_ON; 68037584Smarc ttwakeup(tp); 68125391Skarels } 68225391Skarels return (1); 68325391Skarels } 68425391Skarels 68525391Skarels /* 68625404Skarels * Default modem control routine (for other line disciplines). 68725404Skarels * Return argument flag, to turn off device on carrier drop. 68825404Skarels */ 68925415Skarels nullmodem(tp, flag) 69025415Skarels register struct tty *tp; 69125404Skarels int flag; 69225404Skarels { 69325404Skarels 69425404Skarels if (flag) 69525404Skarels tp->t_state |= TS_CARR_ON; 69639407Smarc else { 69725404Skarels tp->t_state &= ~TS_CARR_ON; 698*42193Smarc if ((tp->t_cflag&CLOCAL) == 0) { 699*42193Smarc if (tp->t_session && tp->t_session->s_leader) 700*42193Smarc psignal(tp->t_session->s_leader, SIGHUP); 701*42193Smarc return (0); 702*42193Smarc } 70339407Smarc } 704*42193Smarc return (1); 70525404Skarels } 70625404Skarels 70725404Skarels /* 7087502Sroot * reinput pending characters after state switch 70917545Skarels * call at spltty(). 7107502Sroot */ 7117502Sroot ttypend(tp) 7127625Ssam register struct tty *tp; 7137502Sroot { 7147502Sroot struct clist tq; 7157502Sroot register c; 7167502Sroot 71735811Smarc tp->t_lflag &= ~PENDIN; 7189578Ssam tp->t_state |= TS_TYPEN; 7197502Sroot tq = tp->t_rawq; 7207502Sroot tp->t_rawq.c_cc = 0; 7217502Sroot tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 7227502Sroot while ((c = getc(&tq)) >= 0) 7237502Sroot ttyinput(c, tp); 7249578Ssam tp->t_state &= ~TS_TYPEN; 7257502Sroot } 7267502Sroot 7277502Sroot /* 72835811Smarc * 7299578Ssam * Place a character on raw TTY input queue, 7309578Ssam * putting in delimiters and waking up top 7319578Ssam * half as needed. Also echo if required. 7329578Ssam * The arguments are the character and the 7339578Ssam * appropriate tty structure. 7347502Sroot */ 7357502Sroot ttyinput(c, tp) 7367625Ssam register c; 7377625Ssam register struct tty *tp; 7387502Sroot { 73935811Smarc register int iflag = tp->t_iflag; 74035811Smarc register int lflag = tp->t_lflag; 74135811Smarc register u_char *cc = tp->t_cc; 74235811Smarc int i, err; 7437502Sroot 7449578Ssam /* 7459578Ssam * If input is pending take it first. 7469578Ssam */ 74735811Smarc if (lflag&PENDIN) 7487502Sroot ttypend(tp); 74935811Smarc /* 75035811Smarc * Gather stats. 75135811Smarc */ 7527502Sroot tk_nin++; 75335811Smarc if (lflag&ICANON) { 75435811Smarc tk_cancc++; 75535811Smarc tp->t_cancc++; 75635811Smarc } else { 75735811Smarc tk_rawcc++; 75835811Smarc tp->t_rawcc++; 75935811Smarc } 7609578Ssam /* 76135811Smarc * Handle exceptional conditions (break, parity, framing). 7629578Ssam */ 76335811Smarc if (err = (c&TTY_ERRORMASK)) { 76435811Smarc c &= ~TTY_ERRORMASK; 76535811Smarc if (err&TTY_FE && !c) { /* break */ 76635811Smarc if (iflag&IGNBRK) 76735811Smarc goto endcase; 76835811Smarc else if (iflag&BRKINT && lflag&ISIG && 76935811Smarc (cc[VINTR] != _POSIX_VDISABLE)) 77035811Smarc c = cc[VINTR]; 77135811Smarc else { 77235811Smarc c = 0; 77335811Smarc if (iflag&PARMRK) 77435811Smarc goto parmrk; 77535811Smarc } 77635811Smarc } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) { 77735811Smarc if (iflag&IGNPAR) 77835811Smarc goto endcase; 77935811Smarc else if (iflag&PARMRK) { 78035811Smarc parmrk: 78135811Smarc putc(0377|TTY_QUOTE, &tp->t_rawq); 78235811Smarc putc(0|TTY_QUOTE, &tp->t_rawq); 78335811Smarc putc(c|TTY_QUOTE, &tp->t_rawq); 78435811Smarc goto endcase; 78535811Smarc } else 78635811Smarc c = 0; 7877502Sroot } 7889578Ssam } 7899578Ssam /* 79035811Smarc * In tandem mode, check high water mark. 7919578Ssam */ 79235811Smarc if (iflag&IXOFF) 79335811Smarc ttyblock(tp); 79435811Smarc if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP)) 7959578Ssam c &= 0177; 7969578Ssam /* 7979578Ssam * Check for literal nexting very first 7989578Ssam */ 7999578Ssam if (tp->t_state&TS_LNCH) { 80035811Smarc c |= TTY_QUOTE; 8019578Ssam tp->t_state &= ~TS_LNCH; 8029578Ssam } 8039578Ssam /* 8049578Ssam * Scan for special characters. This code 8059578Ssam * is really just a big case statement with 8069578Ssam * non-constant cases. The bottom of the 8079578Ssam * case statement is labeled ``endcase'', so goto 8089578Ssam * it after a case match, or similar. 8099578Ssam */ 81035811Smarc /* 81135811Smarc * Control chars which aren't controlled 81235811Smarc * by ICANON, ISIG, or IXON. 81335811Smarc */ 81439407Smarc if (lflag&IEXTEN) { 81540712Skarels if (CCEQ(cc[VLNEXT], c)) { 81635811Smarc if (lflag&ECHO) { 81735811Smarc if (lflag&ECHOE) 81840712Skarels ttyoutstr("^\b", tp); 81935811Smarc else 82035811Smarc ttyecho(c, tp); 82135811Smarc } 8229578Ssam tp->t_state |= TS_LNCH; 8239578Ssam goto endcase; 8249578Ssam } 82540712Skarels if (CCEQ(cc[VFLUSHO], c)) { 82635811Smarc if (lflag&FLUSHO) 82735811Smarc tp->t_lflag &= ~FLUSHO; 8287502Sroot else { 82912752Ssam ttyflush(tp, FWRITE); 8307502Sroot ttyecho(c, tp); 8319578Ssam if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 8327502Sroot ttyretype(tp); 83335811Smarc tp->t_lflag |= FLUSHO; 8347502Sroot } 8359578Ssam goto startoutput; 8369578Ssam } 83735811Smarc } 83835811Smarc /* 83935811Smarc * Signals. 84035811Smarc */ 84135811Smarc if (lflag&ISIG) { 84235811Smarc if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 84335811Smarc if ((lflag&NOFLSH) == 0) 84435811Smarc ttyflush(tp, FREAD|FWRITE); 84535811Smarc ttyecho(c, tp); 84639555Smarc pgsignal(tp->t_pgrp, 84740712Skarels CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT); 84835811Smarc goto endcase; 84935811Smarc } 85040712Skarels if (CCEQ(cc[VSUSP], c)) { 85135811Smarc if ((lflag&NOFLSH) == 0) 85212752Ssam ttyflush(tp, FREAD); 8539578Ssam ttyecho(c, tp); 85439555Smarc pgsignal(tp->t_pgrp, SIGTSTP); 8559578Ssam goto endcase; 8569578Ssam } 8579578Ssam } 8589578Ssam /* 8599578Ssam * Handle start/stop characters. 8609578Ssam */ 86135811Smarc if (iflag&IXON) { 86240712Skarels if (CCEQ(cc[VSTOP], c)) { 86335811Smarc if ((tp->t_state&TS_TTSTOP) == 0) { 86435811Smarc tp->t_state |= TS_TTSTOP; 86535811Smarc (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 86635811Smarc return; 86735811Smarc } 86835811Smarc if (!CCEQ(cc[VSTART], c)) 86935811Smarc return; 87035811Smarc /* 87135811Smarc * if VSTART == VSTOP then toggle 87235811Smarc */ 87335811Smarc goto endcase; 8749578Ssam } 87535811Smarc if (CCEQ(cc[VSTART], c)) 87635811Smarc goto restartoutput; 8779578Ssam } 8789578Ssam /* 87935811Smarc * IGNCR, ICRNL, & INLCR 8809578Ssam */ 88135811Smarc if (c == '\r') { 88235811Smarc if (iflag&IGNCR) 88335811Smarc goto endcase; 88435811Smarc else if (iflag&ICRNL) 88535811Smarc c = '\n'; 8869578Ssam } 88735811Smarc else if (c == '\n' && iflag&INLCR) 88835811Smarc c = '\r'; 8899578Ssam /* 89035811Smarc * Non canonical mode; don't process line editing 8919578Ssam * characters; check high water mark for wakeup. 89235811Smarc * 8939578Ssam */ 89440712Skarels if ((lflag&ICANON) == 0) { 8959578Ssam if (tp->t_rawq.c_cc > TTYHOG) { 89635811Smarc if (iflag&IMAXBEL) { 89735811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 89835811Smarc (void) ttyoutput(CTRL('g'), tp); 89935811Smarc } else 90035811Smarc ttyflush(tp, FREAD | FWRITE); 90135811Smarc } else { 90235811Smarc if (putc(c, &tp->t_rawq) >= 0) { 90335811Smarc ttwakeup(tp); 90435811Smarc ttyecho(c, tp); 90535811Smarc } 9067502Sroot } 9079578Ssam goto endcase; 9089578Ssam } 9099578Ssam /* 91035811Smarc * From here on down canonical mode character 9119578Ssam * processing takes place. 9129578Ssam */ 91335811Smarc /* 91435811Smarc * erase (^H / ^?) 91535811Smarc */ 91639407Smarc if (CCEQ(cc[VERASE], c)) { 9179578Ssam if (tp->t_rawq.c_cc) 9189578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9199578Ssam goto endcase; 9209578Ssam } 92135811Smarc /* 92235811Smarc * kill (^U) 92335811Smarc */ 92435811Smarc if (CCEQ(cc[VKILL], c)) { 92537584Smarc if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount && 92640712Skarels (lflag&ECHOPRT) == 0) { 9279578Ssam while (tp->t_rawq.c_cc) 9289578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9299578Ssam } else { 9309578Ssam ttyecho(c, tp); 93135811Smarc if (lflag&ECHOK || lflag&ECHOKE) 93235811Smarc ttyecho('\n', tp); 9339578Ssam while (getc(&tp->t_rawq) > 0) 9349578Ssam ; 9359578Ssam tp->t_rocount = 0; 9369578Ssam } 9379578Ssam tp->t_state &= ~TS_LOCAL; 9389578Ssam goto endcase; 9399578Ssam } 9409578Ssam /* 94135811Smarc * word erase (^W) 9429578Ssam */ 94335811Smarc if (CCEQ(cc[VWERASE], c)) { 94435811Smarc int ctype; 94535811Smarc 94635811Smarc #define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0) 94735811Smarc /* 94835811Smarc * erase whitespace 94935811Smarc */ 95035811Smarc while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 95135811Smarc ttyrub(c, tp); 95235811Smarc if (c == -1) 95334492Skarels goto endcase; 95435811Smarc /* 95535811Smarc * special case last char of token 95635811Smarc */ 95735811Smarc ttyrub(c, tp); 95835811Smarc c = unputc(&tp->t_rawq); 95935811Smarc if (c == -1 || c == ' ' || c == '\t') { 96035811Smarc if (c != -1) 96135811Smarc (void) putc(c, &tp->t_rawq); 96234492Skarels goto endcase; 96334492Skarels } 96435811Smarc /* 96535811Smarc * erase rest of token 96635811Smarc */ 96735811Smarc ctype = CTYPE(c); 96835811Smarc do { 96935811Smarc ttyrub(c, tp); 97035811Smarc c = unputc(&tp->t_rawq); 97135811Smarc if (c == -1) 97235811Smarc goto endcase; 97335811Smarc } while (c != ' ' && c != '\t' && CTYPE(c) == ctype); 97435811Smarc (void) putc(c, &tp->t_rawq); 97535811Smarc goto endcase; 97635811Smarc #undef CTYPE 9779578Ssam } 9789578Ssam /* 97935811Smarc * reprint line (^R) 98035811Smarc */ 98135811Smarc if (CCEQ(cc[VREPRINT], c)) { 98235811Smarc ttyretype(tp); 98335811Smarc goto endcase; 98435811Smarc } 98542141Smckusick if (CCEQ(cc[VINFO], c)) { 98642141Smckusick pgsignal(tp->t_pgrp, SIGINFO); 98742141Smckusick if ((lflag&NOKERNINFO) == 0) 98842141Smckusick ttyinfo(tp); 98942141Smckusick goto endcase; 99042141Smckusick } 99135811Smarc /* 9929578Ssam * Check for input buffer overflow 9939578Ssam */ 99410391Ssam if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) { 99535811Smarc if (iflag&IMAXBEL) { 99635811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 99735811Smarc (void) ttyoutput(CTRL('g'), tp); 99835811Smarc } else 99935811Smarc ttyflush(tp, FREAD | FWRITE); 10009578Ssam goto endcase; 100110391Ssam } 10029578Ssam /* 10039578Ssam * Put data char in q for user and 10049578Ssam * wakeup on seeing a line delimiter. 10059578Ssam */ 10069578Ssam if (putc(c, &tp->t_rawq) >= 0) { 100735811Smarc if (ttbreakc(c)) { 10089578Ssam tp->t_rocount = 0; 10099578Ssam catq(&tp->t_rawq, &tp->t_canq); 10107502Sroot ttwakeup(tp); 10119578Ssam } else if (tp->t_rocount++ == 0) 10129578Ssam tp->t_rocol = tp->t_col; 10139578Ssam if (tp->t_state&TS_ERASE) { 101435811Smarc /* 101535811Smarc * end of prterase \.../ 101635811Smarc */ 10179578Ssam tp->t_state &= ~TS_ERASE; 10189578Ssam (void) ttyoutput('/', tp); 10199578Ssam } 10209578Ssam i = tp->t_col; 10217502Sroot ttyecho(c, tp); 102235811Smarc if (CCEQ(cc[VEOF], c) && lflag&ECHO) { 102335811Smarc /* 102435811Smarc * Place the cursor over the '^' of the ^D. 102535811Smarc */ 10269578Ssam i = MIN(2, tp->t_col - i); 10279578Ssam while (i > 0) { 10289578Ssam (void) ttyoutput('\b', tp); 10299578Ssam i--; 10309578Ssam } 10319578Ssam } 10327502Sroot } 10339578Ssam endcase: 10349578Ssam /* 103535811Smarc * IXANY means allow any character to restart output. 10369578Ssam */ 103740712Skarels if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 && 103840712Skarels cc[VSTART] != cc[VSTOP]) 10397502Sroot return; 10409578Ssam restartoutput: 10417502Sroot tp->t_state &= ~TS_TTSTOP; 104235811Smarc tp->t_lflag &= ~FLUSHO; 10439578Ssam startoutput: 10447502Sroot ttstart(tp); 10457502Sroot } 10467502Sroot 10477502Sroot /* 10489578Ssam * Put character on TTY output queue, adding delays, 10497502Sroot * expanding tabs, and handling the CR/NL bit. 10509578Ssam * This is called both from the top half for output, 10519578Ssam * and from interrupt level for echoing. 10527502Sroot * The arguments are the character and the tty structure. 10537502Sroot * Returns < 0 if putc succeeds, otherwise returns char to resend 10547502Sroot * Must be recursive. 10557502Sroot */ 10567502Sroot ttyoutput(c, tp) 10577502Sroot register c; 10587502Sroot register struct tty *tp; 10597502Sroot { 10607502Sroot register char *colp; 10617502Sroot register ctype; 106235811Smarc register long oflag = tp->t_oflag; 106335811Smarc 106440712Skarels if ((oflag&OPOST) == 0) { 106535811Smarc if (tp->t_lflag&FLUSHO) 10667502Sroot return (-1); 10677502Sroot if (putc(c, &tp->t_outq)) 10687625Ssam return (c); 10697502Sroot tk_nout++; 107035811Smarc tp->t_outcc++; 10717502Sroot return (-1); 10727502Sroot } 107335811Smarc c &= TTY_CHARMASK; 10747502Sroot /* 10757502Sroot * Turn tabs to spaces as required 10767502Sroot */ 107735811Smarc if (c == '\t' && oflag&OXTABS ) { 10787502Sroot register int s; 10797502Sroot 10807502Sroot c = 8 - (tp->t_col&7); 108135811Smarc if ((tp->t_lflag&FLUSHO) == 0) { 108217545Skarels s = spltty(); /* don't interrupt tabs */ 10837502Sroot c -= b_to_q(" ", c, &tp->t_outq); 10847502Sroot tk_nout += c; 108535811Smarc tp->t_outcc += c; 10867502Sroot splx(s); 10877502Sroot } 10887502Sroot tp->t_col += c; 10897502Sroot return (c ? -1 : '\t'); 10907502Sroot } 109135811Smarc if (c == CEOT && oflag&ONOEOT) 109235811Smarc return(-1); 10937502Sroot tk_nout++; 109435811Smarc tp->t_outcc++; 10957502Sroot /* 10967502Sroot * turn <nl> to <cr><lf> if desired. 10977502Sroot */ 109835811Smarc if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0) 10997502Sroot return (c); 110035811Smarc if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq)) 110135811Smarc return (c); 11027502Sroot /* 11037502Sroot * Calculate delays. 11047502Sroot * The numbers here represent clock ticks 11057502Sroot * and are not necessarily optimal for all terminals. 11069578Ssam * 11079578Ssam * SHOULD JUST ALLOW USER TO SPECIFY DELAYS 110835811Smarc * 110935811Smarc * (actually, should THROW AWAY terminals which need delays) 11107502Sroot */ 11117502Sroot colp = &tp->t_col; 11127502Sroot ctype = partab[c]; 11137502Sroot c = 0; 11147502Sroot switch (ctype&077) { 11157502Sroot 11167502Sroot case ORDINARY: 11177502Sroot (*colp)++; 11187502Sroot 11197502Sroot case CONTROL: 11207502Sroot break; 11217502Sroot 11227502Sroot case BACKSPACE: 11237502Sroot if (*colp) 11247502Sroot (*colp)--; 11257502Sroot break; 11267502Sroot 112713821Ssam /* 112813821Ssam * This macro is close enough to the correct thing; 112913821Ssam * it should be replaced by real user settable delays 113013821Ssam * in any event... 113113821Ssam */ 113213821Ssam #define mstohz(ms) (((ms) * hz) >> 10) 11337502Sroot case NEWLINE: 11347502Sroot ctype = (tp->t_flags >> 8) & 03; 11357625Ssam if (ctype == 1) { /* tty 37 */ 113626357Skarels if (*colp > 0) { 113726357Skarels c = (((unsigned)*colp) >> 4) + 3; 113826357Skarels if ((unsigned)c > 6) 113926357Skarels c = 6; 114026357Skarels } 11419578Ssam } else if (ctype == 2) /* vt05 */ 114213821Ssam c = mstohz(100); 11437502Sroot *colp = 0; 11447502Sroot break; 11457502Sroot 11467502Sroot case TAB: 11477502Sroot ctype = (tp->t_flags >> 10) & 03; 11487625Ssam if (ctype == 1) { /* tty 37 */ 11497502Sroot c = 1 - (*colp | ~07); 11507625Ssam if (c < 5) 11517502Sroot c = 0; 11527502Sroot } 11537502Sroot *colp |= 07; 11547502Sroot (*colp)++; 11557502Sroot break; 11567502Sroot 11577502Sroot case VTAB: 11589578Ssam if (tp->t_flags&VTDELAY) /* tty 37 */ 11597502Sroot c = 0177; 11607502Sroot break; 11617502Sroot 11627502Sroot case RETURN: 11637502Sroot ctype = (tp->t_flags >> 12) & 03; 11649578Ssam if (ctype == 1) /* tn 300 */ 116513821Ssam c = mstohz(83); 11669578Ssam else if (ctype == 2) /* ti 700 */ 116713821Ssam c = mstohz(166); 11689578Ssam else if (ctype == 3) { /* concept 100 */ 11697502Sroot int i; 11709578Ssam 11717502Sroot if ((i = *colp) >= 0) 11729578Ssam for (; i < 9; i++) 11737502Sroot (void) putc(0177, &tp->t_outq); 11747502Sroot } 11757502Sroot *colp = 0; 11767502Sroot } 117735811Smarc if (c && (tp->t_lflag&FLUSHO) == 0) 117835811Smarc (void) putc(c|TTY_QUOTE, &tp->t_outq); 11797502Sroot return (-1); 11807502Sroot } 118113821Ssam #undef mstohz 11827502Sroot 11837502Sroot /* 11847502Sroot * Called from device's read routine after it has 11857502Sroot * calculated the tty-structure given as argument. 11867502Sroot */ 118737584Smarc ttread(tp, uio, flag) 11887625Ssam register struct tty *tp; 11897722Swnj struct uio *uio; 11907502Sroot { 11917502Sroot register struct clist *qp; 119235811Smarc register int c; 119341383Smarc register long lflag; 119435811Smarc register u_char *cc = tp->t_cc; 11959859Ssam int s, first, error = 0; 11967502Sroot 11977502Sroot loop: 119841383Smarc lflag = tp->t_lflag; 119937584Smarc s = spltty(); 12009578Ssam /* 120137584Smarc * take pending input first 12029578Ssam */ 120335811Smarc if (lflag&PENDIN) 12047502Sroot ttypend(tp); 12059859Ssam splx(s); 120640712Skarels 12079578Ssam /* 12089578Ssam * Hang process if it's in the background. 12099578Ssam */ 121039555Smarc if (isbackground(u.u_procp, tp)) { 121124392Skarels if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) || 121224392Skarels (u.u_procp->p_sigmask & sigmask(SIGTTIN)) || 121335811Smarc u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0) 12148520Sroot return (EIO); 121535811Smarc pgsignal(u.u_procp->p_pgrp, SIGTTIN); 121640712Skarels if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0)) 121740712Skarels return (error); 121823165Sbloom goto loop; 12197502Sroot } 122040712Skarels 12219578Ssam /* 122235811Smarc * If canonical, use the canonical queue, 122335811Smarc * else use the raw queue. 122437584Smarc * 122537584Smarc * XXX - should get rid of canonical queue. 122637584Smarc * (actually, should get rid of clists...) 12279578Ssam */ 122835811Smarc qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq; 122940712Skarels 12309578Ssam /* 123140712Skarels * If there is no input, sleep on rawq 123240712Skarels * awaiting hardware receipt and notification. 123340712Skarels * If we have data, we don't need to check for carrier. 12349578Ssam */ 123517545Skarels s = spltty(); 12369578Ssam if (qp->c_cc <= 0) { 123740712Skarels int carrier; 123840712Skarels 123940712Skarels carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL); 124040712Skarels if (!carrier && tp->t_state&TS_ISOPEN) { 12419859Ssam splx(s); 124240712Skarels return (0); /* EOF */ 12437502Sroot } 124437728Smckusick if (flag & IO_NDELAY) { 124537584Smarc splx(s); 124637584Smarc return (EWOULDBLOCK); 124737584Smarc } 124840712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 124940712Skarels carrier ? ttyin : ttopen, 0); 12509859Ssam splx(s); 125140712Skarels if (error) 125240712Skarels return (error); 12539578Ssam goto loop; 12549578Ssam } 12559859Ssam splx(s); 125640712Skarels 12579578Ssam /* 125835811Smarc * Input present, check for input mapping and processing. 12599578Ssam */ 12609578Ssam first = 1; 12619578Ssam while ((c = getc(qp)) >= 0) { 12629578Ssam /* 126335811Smarc * delayed suspend (^Y) 12649578Ssam */ 126535811Smarc if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) { 126639555Smarc pgsignal(tp->t_pgrp, SIGTSTP); 12679578Ssam if (first) { 126840712Skarels if (error = tsleep((caddr_t)&lbolt, 126940712Skarels TTIPRI | PCATCH, ttybg, 0)) 127040712Skarels break; 12719578Ssam goto loop; 12729578Ssam } 12739578Ssam break; 12747502Sroot } 12759578Ssam /* 127635811Smarc * Interpret EOF only in canonical mode. 12779578Ssam */ 127835811Smarc if (CCEQ(cc[VEOF], c) && lflag&ICANON) 12799578Ssam break; 12809578Ssam /* 12819578Ssam * Give user character. 12829578Ssam */ 128340712Skarels error = ureadc(c, uio); 12849578Ssam if (error) 12859578Ssam break; 128614938Smckusick if (uio->uio_resid == 0) 12879578Ssam break; 12889578Ssam /* 128935811Smarc * In canonical mode check for a "break character" 12909578Ssam * marking the end of a "line of input". 12919578Ssam */ 129240712Skarels if (lflag&ICANON && ttbreakc(c)) 12939578Ssam break; 12949578Ssam first = 0; 12957502Sroot } 12969578Ssam /* 12979578Ssam * Look to unblock output now that (presumably) 12989578Ssam * the input queue has gone down. 12999578Ssam */ 130035811Smarc if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) { 130135811Smarc if (cc[VSTART] != _POSIX_VDISABLE 130235811Smarc && putc(cc[VSTART], &tp->t_outq) == 0) { 13037502Sroot tp->t_state &= ~TS_TBLOCK; 13047502Sroot ttstart(tp); 13057502Sroot } 130635811Smarc } 13078520Sroot return (error); 13087502Sroot } 13097502Sroot 13107502Sroot /* 131125391Skarels * Check the output queue on tp for space for a kernel message 131225391Skarels * (from uprintf/tprintf). Allow some space over the normal 131325391Skarels * hiwater mark so we don't lose messages due to normal flow 131425391Skarels * control, but don't let the tty run amok. 131530695Skarels * Sleeps here are not interruptible, but we return prematurely 131630695Skarels * if new signals come in. 131725391Skarels */ 131825391Skarels ttycheckoutq(tp, wait) 131925391Skarels register struct tty *tp; 132025391Skarels int wait; 132125391Skarels { 132230695Skarels int hiwat, s, oldsig; 132325391Skarels 132435811Smarc hiwat = tp->t_hiwat; 132525391Skarels s = spltty(); 132630695Skarels oldsig = u.u_procp->p_sig; 132725391Skarels if (tp->t_outq.c_cc > hiwat + 200) 132829946Skarels while (tp->t_outq.c_cc > hiwat) { 132929946Skarels ttstart(tp); 133030695Skarels if (wait == 0 || u.u_procp->p_sig != oldsig) { 133129946Skarels splx(s); 133229946Skarels return (0); 133329946Skarels } 133430695Skarels timeout(wakeup, (caddr_t)&tp->t_outq, hz); 133529946Skarels tp->t_state |= TS_ASLEEP; 133630695Skarels sleep((caddr_t)&tp->t_outq, PZERO - 1); 133725391Skarels } 133825391Skarels splx(s); 133925391Skarels return (1); 134025391Skarels } 134125391Skarels 134225391Skarels /* 13437502Sroot * Called from the device's write routine after it has 13447502Sroot * calculated the tty-structure given as argument. 13457502Sroot */ 134637584Smarc ttwrite(tp, uio, flag) 13477625Ssam register struct tty *tp; 13489578Ssam register struct uio *uio; 13497502Sroot { 13507502Sroot register char *cp; 135140712Skarels register int cc = 0, ce; 13529578Ssam int i, hiwat, cnt, error, s; 13537502Sroot char obuf[OBUFSIZ]; 13547502Sroot 135535811Smarc hiwat = tp->t_hiwat; 13569578Ssam cnt = uio->uio_resid; 13579578Ssam error = 0; 13587502Sroot loop: 135937584Smarc s = spltty(); 136040712Skarels if ((tp->t_state&TS_CARR_ON) == 0 && (tp->t_cflag&CLOCAL) == 0) { 136137584Smarc if (tp->t_state&TS_ISOPEN) { 136237584Smarc splx(s); 136337584Smarc return (EIO); 136437728Smckusick } else if (flag & IO_NDELAY) { 136537584Smarc splx(s); 136640712Skarels error = EWOULDBLOCK; 136740712Skarels goto out; 136837584Smarc } else { 136937584Smarc /* 137037584Smarc * sleep awaiting carrier 137137584Smarc */ 137240712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 137340712Skarels ttopen, 0); 137437584Smarc splx(s); 137540712Skarels if (error) 137640712Skarels goto out; 137737584Smarc goto loop; 137837584Smarc } 137937584Smarc } 138037584Smarc splx(s); 13819578Ssam /* 13829578Ssam * Hang the process if it's in the background. 13839578Ssam */ 138439555Smarc if (isbackground(u.u_procp, tp) && 138535811Smarc (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 && 138640712Skarels (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 && 138740712Skarels (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0 && 138835811Smarc u.u_procp->p_pgrp->pg_jobc) { 138935811Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU); 139040712Skarels if (error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, ttybg, 0)) 139140712Skarels goto out; 139221776Sbloom goto loop; 13937502Sroot } 13949578Ssam /* 13959578Ssam * Process the user's data in at most OBUFSIZ 139640712Skarels * chunks. Perform any output translation. 139740712Skarels * Keep track of high water mark, sleep on overflow 139840712Skarels * awaiting device aid in acquiring new space. 13999578Ssam */ 140040712Skarels while (uio->uio_resid > 0 || cc > 0) { 140140712Skarels if (tp->t_lflag&FLUSHO) { 140240712Skarels uio->uio_resid = 0; 140340712Skarels return (0); 140440712Skarels } 140540712Skarels if (tp->t_outq.c_cc > hiwat) 140632067Skarels goto ovhiwat; 14079578Ssam /* 140840712Skarels * Grab a hunk of data from the user, 140940712Skarels * unless we have some leftover from last time. 14109578Ssam */ 14117822Sroot if (cc == 0) { 141240712Skarels cc = min(uio->uio_resid, OBUFSIZ); 141340712Skarels cp = obuf; 141440712Skarels error = uiomove(cp, cc, uio); 141540712Skarels if (error) { 141640712Skarels cc = 0; 141740712Skarels break; 141840712Skarels } 14197822Sroot } 14209578Ssam /* 14219578Ssam * If nothing fancy need be done, grab those characters we 14229578Ssam * can handle without any of ttyoutput's processing and 14239578Ssam * just transfer them to the output q. For those chars 14249578Ssam * which require special processing (as indicated by the 14259578Ssam * bits in partab), call ttyoutput. After processing 14269578Ssam * a hunk of data, look for FLUSHO so ^O's will take effect 14279578Ssam * immediately. 14289578Ssam */ 14299578Ssam while (cc > 0) { 143040712Skarels if ((tp->t_oflag&OPOST) == 0) 14317502Sroot ce = cc; 14327502Sroot else { 143334492Skarels ce = cc - scanc((unsigned)cc, (u_char *)cp, 143434492Skarels (u_char *)partab, 077); 14359578Ssam /* 14369578Ssam * If ce is zero, then we're processing 14379578Ssam * a special character through ttyoutput. 14389578Ssam */ 14399578Ssam if (ce == 0) { 14407502Sroot tp->t_rocount = 0; 14417502Sroot if (ttyoutput(*cp, tp) >= 0) { 144221776Sbloom /* no c-lists, wait a bit */ 144321776Sbloom ttstart(tp); 144440712Skarels if (error = tsleep((caddr_t)&lbolt, 144540712Skarels TTOPRI | PCATCH, ttybuf, 0)) 144640712Skarels break; 144721776Sbloom goto loop; 14487502Sroot } 14499578Ssam cp++, cc--; 145035811Smarc if ((tp->t_lflag&FLUSHO) || 14519578Ssam tp->t_outq.c_cc > hiwat) 14527502Sroot goto ovhiwat; 14539578Ssam continue; 14547502Sroot } 14557502Sroot } 14569578Ssam /* 14579578Ssam * A bunch of normal characters have been found, 14589578Ssam * transfer them en masse to the output queue and 14599578Ssam * continue processing at the top of the loop. 14609578Ssam * If there are any further characters in this 14619578Ssam * <= OBUFSIZ chunk, the first should be a character 14629578Ssam * requiring special handling by ttyoutput. 14639578Ssam */ 14647502Sroot tp->t_rocount = 0; 14659578Ssam i = b_to_q(cp, ce, &tp->t_outq); 14669578Ssam ce -= i; 14679578Ssam tp->t_col += ce; 14689578Ssam cp += ce, cc -= ce, tk_nout += ce; 146935811Smarc tp->t_outcc += ce; 14709578Ssam if (i > 0) { 14719578Ssam /* out of c-lists, wait a bit */ 14727502Sroot ttstart(tp); 147340712Skarels if (error = tsleep((caddr_t)&lbolt, 147440712Skarels TTOPRI | PCATCH, ttybuf, 0)) 147540712Skarels break; 147621776Sbloom goto loop; 14777502Sroot } 147835811Smarc if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat) 147940712Skarels break; 14807502Sroot } 148135811Smarc ttstart(tp); 14827502Sroot } 148340712Skarels out: 148440712Skarels /* 148540712Skarels * If cc is nonzero, we leave the uio structure inconsistent, 148640712Skarels * as the offset and iov pointers have moved forward, 148740712Skarels * but it doesn't matter (the call will either return short 148840712Skarels * or restart with a new uio). 148940712Skarels */ 149040712Skarels uio->uio_resid += cc; 14918520Sroot return (error); 149240712Skarels 14937502Sroot ovhiwat: 149432067Skarels ttstart(tp); 149532067Skarels s = spltty(); 14969578Ssam /* 149735811Smarc * This can only occur if FLUSHO is set in t_lflag, 149832067Skarels * or if ttstart/oproc is synchronous (or very fast). 14999578Ssam */ 15007502Sroot if (tp->t_outq.c_cc <= hiwat) { 15019578Ssam splx(s); 15027502Sroot goto loop; 15037502Sroot } 150437728Smckusick if (flag & IO_NDELAY) { 150517545Skarels splx(s); 150640712Skarels uio->uio_resid += cc; 15077822Sroot if (uio->uio_resid == cnt) 15088520Sroot return (EWOULDBLOCK); 15098520Sroot return (0); 15107502Sroot } 15117502Sroot tp->t_state |= TS_ASLEEP; 151240712Skarels error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 15139578Ssam splx(s); 151440712Skarels if (error) 151540712Skarels goto out; 15167502Sroot goto loop; 15177502Sroot } 15187502Sroot 15197502Sroot /* 15207502Sroot * Rubout one character from the rawq of tp 15217502Sroot * as cleanly as possible. 15227502Sroot */ 15237502Sroot ttyrub(c, tp) 15247625Ssam register c; 15257625Ssam register struct tty *tp; 15267502Sroot { 15277502Sroot register char *cp; 15287502Sroot register int savecol; 15297502Sroot int s; 15307502Sroot char *nextc(); 15317502Sroot 153235811Smarc if ((tp->t_lflag&ECHO) == 0) 15337502Sroot return; 153435811Smarc tp->t_lflag &= ~FLUSHO; 153535811Smarc if (tp->t_lflag&ECHOE) { 15367502Sroot if (tp->t_rocount == 0) { 15377502Sroot /* 15387502Sroot * Screwed by ttwrite; retype 15397502Sroot */ 15407502Sroot ttyretype(tp); 15417502Sroot return; 15427502Sroot } 154335811Smarc if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE)) 15447502Sroot ttyrubo(tp, 2); 154535811Smarc else switch (partab[c&=0377]&077) { 15467502Sroot 15477502Sroot case ORDINARY: 154835811Smarc ttyrubo(tp, 1); 15497502Sroot break; 15507502Sroot 15517502Sroot case VTAB: 15527502Sroot case BACKSPACE: 15537502Sroot case CONTROL: 15547502Sroot case RETURN: 155535811Smarc if (tp->t_lflag&ECHOCTL) 15567502Sroot ttyrubo(tp, 2); 15577502Sroot break; 15587502Sroot 155935811Smarc case TAB: { 156035811Smarc int c; 156135811Smarc 15627502Sroot if (tp->t_rocount < tp->t_rawq.c_cc) { 15637502Sroot ttyretype(tp); 15647502Sroot return; 15657502Sroot } 156617545Skarels s = spltty(); 15677502Sroot savecol = tp->t_col; 15689578Ssam tp->t_state |= TS_CNTTB; 156935811Smarc tp->t_lflag |= FLUSHO; 15707502Sroot tp->t_col = tp->t_rocol; 15719578Ssam cp = tp->t_rawq.c_cf; 157239407Smarc if (cp) 157339407Smarc c = *cp; /* XXX FIX NEXTC */ 157435811Smarc for (; cp; cp = nextc(&tp->t_rawq, cp, &c)) 157535811Smarc ttyecho(c, tp); 157635811Smarc tp->t_lflag &= ~FLUSHO; 15779578Ssam tp->t_state &= ~TS_CNTTB; 15787502Sroot splx(s); 15797502Sroot /* 15807502Sroot * savecol will now be length of the tab 15817502Sroot */ 15827502Sroot savecol -= tp->t_col; 15837502Sroot tp->t_col += savecol; 15847502Sroot if (savecol > 8) 15857502Sroot savecol = 8; /* overflow screw */ 15867502Sroot while (--savecol >= 0) 15877502Sroot (void) ttyoutput('\b', tp); 15887502Sroot break; 158935811Smarc } 15907502Sroot 15917502Sroot default: 159237584Smarc /* XXX */ 159335811Smarc printf("ttyrub: would panic c = %d, val = %d\n", 159435811Smarc c, partab[c&=0377]&077); 159535811Smarc /*panic("ttyrub");*/ 15967502Sroot } 159735811Smarc } else if (tp->t_lflag&ECHOPRT) { 15989578Ssam if ((tp->t_state&TS_ERASE) == 0) { 15997502Sroot (void) ttyoutput('\\', tp); 16009578Ssam tp->t_state |= TS_ERASE; 16017502Sroot } 16027502Sroot ttyecho(c, tp); 16037502Sroot } else 160435811Smarc ttyecho(tp->t_cc[VERASE], tp); 16057502Sroot tp->t_rocount--; 16067502Sroot } 16077502Sroot 16087502Sroot /* 16097502Sroot * Crt back over cnt chars perhaps 16107502Sroot * erasing them. 16117502Sroot */ 16127502Sroot ttyrubo(tp, cnt) 16137625Ssam register struct tty *tp; 16147625Ssam int cnt; 16157502Sroot { 16167502Sroot 16177502Sroot while (--cnt >= 0) 161840712Skarels ttyoutstr("\b \b", tp); 16197502Sroot } 16207502Sroot 16217502Sroot /* 16227502Sroot * Reprint the rawq line. 16237502Sroot * We assume c_cc has already been checked. 16247502Sroot */ 16257502Sroot ttyretype(tp) 16267625Ssam register struct tty *tp; 16277502Sroot { 16287502Sroot register char *cp; 16297502Sroot char *nextc(); 163035811Smarc int s, c; 16317502Sroot 163235811Smarc if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 163335811Smarc ttyecho(tp->t_cc[VREPRINT], tp); 16347502Sroot (void) ttyoutput('\n', tp); 163517545Skarels s = spltty(); 163635811Smarc /*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE 163735811Smarc BIT OF FIRST CHAR ****/ 163835811Smarc for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) { 163935811Smarc ttyecho(c, tp); 164035811Smarc } 164135811Smarc for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) { 164235811Smarc ttyecho(c, tp); 164335811Smarc } 16449578Ssam tp->t_state &= ~TS_ERASE; 16457502Sroot splx(s); 16467502Sroot tp->t_rocount = tp->t_rawq.c_cc; 16477502Sroot tp->t_rocol = 0; 16487502Sroot } 16497502Sroot 16507502Sroot /* 165135811Smarc * Echo a typed character to the terminal. 16527502Sroot */ 16537502Sroot ttyecho(c, tp) 16547625Ssam register c; 16557625Ssam register struct tty *tp; 16567502Sroot { 16579578Ssam if ((tp->t_state&TS_CNTTB) == 0) 165835811Smarc tp->t_lflag &= ~FLUSHO; 165940712Skarels if ((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 || c == '\n')) 16607502Sroot return; 166135811Smarc if (tp->t_lflag&ECHOCTL) { 166240712Skarels if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' || 166340712Skarels c == 0177) { 16647502Sroot (void) ttyoutput('^', tp); 166535811Smarc c &= TTY_CHARMASK; 16667502Sroot if (c == 0177) 16677502Sroot c = '?'; 16687502Sroot else 16697502Sroot c += 'A' - 1; 16707502Sroot } 16717502Sroot } 167235811Smarc (void) ttyoutput(c, tp); 16737502Sroot } 16747502Sroot 16757502Sroot /* 16767502Sroot * send string cp to tp 16777502Sroot */ 167840712Skarels ttyoutstr(cp, tp) 16797625Ssam register char *cp; 16807625Ssam register struct tty *tp; 16817502Sroot { 16827502Sroot register char c; 16837502Sroot 16847502Sroot while (c = *cp++) 16857502Sroot (void) ttyoutput(c, tp); 16867502Sroot } 16877502Sroot 16887502Sroot ttwakeup(tp) 16897502Sroot struct tty *tp; 16907502Sroot { 16917502Sroot 16927502Sroot if (tp->t_rsel) { 16937502Sroot selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL); 16947502Sroot tp->t_state &= ~TS_RCOLL; 16957502Sroot tp->t_rsel = 0; 16967502Sroot } 169712752Ssam if (tp->t_state & TS_ASYNC) 169839555Smarc pgsignal(tp->t_pgrp, SIGIO); 16997502Sroot wakeup((caddr_t)&tp->t_rawq); 17007502Sroot } 170135811Smarc 170235811Smarc /* 170335811Smarc * set tty hi and low water marks 170435811Smarc * 170535811Smarc * Try to arrange the dynamics so there's about one second 170635811Smarc * from hi to low water. 170735811Smarc * 170835811Smarc */ 170935811Smarc ttsetwater(tp) 171035811Smarc struct tty *tp; 171135811Smarc { 171235811Smarc register cps = tp->t_ospeed / 10; 171335811Smarc register x; 171435811Smarc 171535811Smarc #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x)) 171635811Smarc tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT); 171735811Smarc x += cps; 171835811Smarc x = clamp(x, TTMAXHIWAT, TTMINHIWAT); 171935811Smarc tp->t_hiwat = roundup(x, CBSIZE); 172035811Smarc #undef clamp 172135811Smarc } 172235811Smarc 172335811Smarc ttspeedtab(speed, table) 172435811Smarc struct speedtab table[]; 172535811Smarc { 172635811Smarc register int i; 172735811Smarc 172835811Smarc for (i = 0; table[i].sp_speed != -1; i++) 172935811Smarc if (table[i].sp_speed == speed) 173035811Smarc return(table[i].sp_code); 173135811Smarc return(-1); 173235811Smarc } 173339407Smarc 173441177Smarc int ttyhostname = 0; 173539407Smarc /* 173639407Smarc * (^T) 173739407Smarc * Report on state of foreground process group. 173839407Smarc */ 173939407Smarc ttyinfo(tp) 174039407Smarc struct tty *tp; 174139407Smarc { 174241177Smarc register struct proc *p, *pick = NULL; 174341177Smarc register char *cp = hostname; 174441177Smarc int x, s; 174541177Smarc struct timeval utime, stime; 174641177Smarc #define pgtok(a) ((a)/(1024/NBPG)) 174739407Smarc 174839407Smarc if (ttycheckoutq(tp,0) == 0) 174939407Smarc return; 175041177Smarc /* 175141177Smarc * hostname 175241177Smarc */ 175341177Smarc if (ttyhostname) { 175441177Smarc if (*cp == '\0') 175541177Smarc ttyprintf(tp, "amnesia"); 175641177Smarc else 175741177Smarc while (*cp && *cp != '.') 175841177Smarc tputchar(*cp++, tp); 175941177Smarc tputchar(' '); 176041177Smarc } 176141177Smarc /* 176241177Smarc * load average 176341177Smarc */ 176441177Smarc x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT; 176541177Smarc ttyprintf(tp, "load: %d.", x/100); 176641177Smarc ttyoutint(x%100, 10, 2, tp); 176739555Smarc if (tp->t_session == NULL) 176841177Smarc ttyprintf(tp, " not a controlling terminal\n"); 176941177Smarc else if (tp->t_pgrp == NULL) 177041177Smarc ttyprintf(tp, " no foreground process group\n"); 177141177Smarc else if ((p = tp->t_pgrp->pg_mem) == NULL) 177241177Smarc ttyprintf(tp, " empty foreground process group\n"); 177339407Smarc else { 177441177Smarc /* pick interesting process */ 177539407Smarc for (; p != NULL; p = p->p_pgrpnxt) { 177641177Smarc if (proc_compare(pick, p)) 177741177Smarc pick = p; 177839407Smarc } 177941177Smarc ttyprintf(tp, " cmd: %s %d [%s] ", 178041177Smarc pick->p_comm, pick->p_pid, 178141177Smarc pick->p_wmesg ? pick->p_wmesg : "running"); 178241177Smarc /* 178341177Smarc * cpu time 178441177Smarc */ 178541177Smarc if (u.u_procp == pick) 178641177Smarc s = splclock(); 178741177Smarc utime = pick->p_utime; 178841177Smarc stime = pick->p_stime; 178941177Smarc if (u.u_procp == pick) 179041177Smarc splx(s); 179141177Smarc /* user time */ 179241177Smarc x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */ 179341177Smarc ttyoutint(utime.tv_sec, 10, 1, tp); 179441177Smarc tputchar('.', tp); 179541177Smarc ttyoutint(x, 10, 2, tp); 179641177Smarc tputchar('u', tp); 179741177Smarc tputchar(' ', tp); 179841177Smarc /* system time */ 179941177Smarc x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */ 180041177Smarc ttyoutint(stime.tv_sec, 10, 1, tp); 180141177Smarc tputchar('.', tp); 180241177Smarc ttyoutint(x, 10, 2, tp); 180341177Smarc tputchar('s', tp); 180441177Smarc tputchar(' ', tp); 180541177Smarc /* 180641177Smarc * pctcpu 180741177Smarc */ 180841177Smarc x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT; 180941177Smarc ttyoutint(x/100, 10, 1, tp); 181041177Smarc #ifdef notdef /* do we really want this ??? */ 181141177Smarc tputchar('.', tp); 181241177Smarc ttyoutint(x%100, 10, 2, tp); 181341177Smarc #endif 181441177Smarc ttyprintf(tp, "%% %dk\n", pgtok(pick->p_ssize + pick->p_dsize)); 181539407Smarc } 181641177Smarc tp->t_rocount = 0; /* so pending input will be retyped if BS */ 181739407Smarc } 181839407Smarc 181941177Smarc ttyoutint(n, base, min, tp) 182041177Smarc register int n, base, min; 182141177Smarc register struct tty *tp; 182241177Smarc { 182341177Smarc char info[16]; 182441177Smarc register char *p = info; 182541177Smarc 182641177Smarc while (--min >= 0 || n) { 182741177Smarc *p++ = "0123456789abcdef"[n%base]; 182841177Smarc n /= base; 182941177Smarc } 183041177Smarc while (p > info) 183141177Smarc ttyoutput(*--p, tp); 183241177Smarc } 183341177Smarc 183441177Smarc /* 183541177Smarc * Returns 1 if p2 is "better" than p1 183641177Smarc * 183741177Smarc * The algorithm for picking the "interesting" process is thus: 183841177Smarc * 183941177Smarc * 1) (Only foreground processes are eligable - implied) 184041177Smarc * 2) Runnable processes are favored over anything 184141177Smarc * else. The runner with the highest cpu 184241177Smarc * utilization is picked (p_cpu). Ties are 184341177Smarc * broken by picking the highest pid. 184441177Smarc * 3 Next, the sleeper with the shortest sleep 184541177Smarc * time is favored. With ties, we pick out 184641177Smarc * just "short-term" sleepers (SSINTR == 0). 184741177Smarc * Further ties are broken by picking the highest 184841177Smarc * pid. 184941177Smarc * 185041177Smarc */ 185141177Smarc #define isrun(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 185241177Smarc proc_compare(p1, p2) 185341177Smarc register struct proc *p1, *p2; 185441177Smarc { 185541177Smarc 185641177Smarc if (p1 == NULL) 185741177Smarc return (1); 185841177Smarc /* 185941177Smarc * see if at least one of them is runnable 186041177Smarc */ 186141177Smarc switch (isrun(p1)<<1 | isrun(p2)) { 186241177Smarc case 0x01: 186341177Smarc return (1); 186441177Smarc case 0x10: 186541177Smarc return (0); 186641177Smarc case 0x11: 186741177Smarc /* 186841177Smarc * tie - favor one with highest recent cpu utilization 186941177Smarc */ 187041177Smarc if (p2->p_cpu > p1->p_cpu) 187141177Smarc return (1); 187241177Smarc if (p1->p_cpu > p2->p_cpu) 187341177Smarc return (0); 187441177Smarc return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 187541177Smarc } 187641177Smarc /* 187741177Smarc * pick the one with the smallest sleep time 187841177Smarc */ 187941177Smarc if (p2->p_slptime > p1->p_slptime) 188041177Smarc return (0); 188141177Smarc if (p1->p_slptime > p2->p_slptime) 188241177Smarc return (1); 188341177Smarc /* 188441177Smarc * favor one sleeping in a non-interruptible sleep 188541177Smarc */ 188641177Smarc if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0) 188741177Smarc return (1); 188841177Smarc if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0) 188941177Smarc return (0); 189041177Smarc return(p2->p_pid > p1->p_pid); /* tie - return highest pid */ 189141177Smarc } 189239407Smarc #define TOTTY 0x2 /* XXX should be in header */ 189339407Smarc /*VARARGS2*/ 189439407Smarc ttyprintf(tp, fmt, x1) 189539555Smarc struct tty *tp; 189639407Smarc char *fmt; 189739407Smarc unsigned x1; 189839407Smarc { 189939555Smarc prf(fmt, &x1, TOTTY, (caddr_t)tp); 190039407Smarc } 190139555Smarc 190239555Smarc /* 190339555Smarc * Output char to tty; console putchar style. 190439555Smarc */ 190539555Smarc tputchar(c, tp) 190639555Smarc int c; 190739555Smarc struct tty *tp; 190839555Smarc { 190939555Smarc register s = spltty(); 191039555Smarc 191139555Smarc if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN)) 191239555Smarc == (TS_CARR_ON | TS_ISOPEN)) { 191339555Smarc if (c == '\n') 191439555Smarc (void) ttyoutput('\r', tp); 191539555Smarc (void) ttyoutput(c, tp); 191639555Smarc ttstart(tp); 191739555Smarc splx(s); 191839555Smarc return (0); 191939555Smarc } 192039555Smarc splx(s); 192139555Smarc return (-1); 192239555Smarc } 1923