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*43093Smarc * @(#)tty.c 7.30 (Berkeley) 06/14/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; 12343092Smarc if ((error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, 12443092Smarc ttyout, 0)) || 12543092Smarc (error = ttclosed(tp))) 12640712Skarels break; 127903Sbill } 1289859Ssam splx(s); 12940712Skarels return (error); 13039Sbill } 13139Sbill 13239Sbill /* 1339578Ssam * Flush all TTY queues 13439Sbill */ 13512752Ssam ttyflush(tp, rw) 1367625Ssam register struct tty *tp; 13739Sbill { 138903Sbill register s; 139903Sbill 14017545Skarels s = spltty(); 141903Sbill if (rw & FREAD) { 142903Sbill while (getc(&tp->t_canq) >= 0) 143903Sbill ; 14437584Smarc ttwakeup(tp); 145903Sbill } 146903Sbill if (rw & FWRITE) { 14737584Smarc wakeup((caddr_t)&tp->t_outq); /* XXX? what about selwakeup? */ 1485408Swnj tp->t_state &= ~TS_TTSTOP; 1495426Swnj (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw); 150903Sbill while (getc(&tp->t_outq) >= 0) 151903Sbill ; 152903Sbill } 153903Sbill if (rw & FREAD) { 154903Sbill while (getc(&tp->t_rawq) >= 0) 155903Sbill ; 1569578Ssam tp->t_rocount = 0; 157903Sbill tp->t_rocol = 0; 1589578Ssam tp->t_state &= ~TS_LOCAL; 159903Sbill } 160903Sbill splx(s); 16139Sbill } 16239Sbill 163903Sbill /* 164903Sbill * Send stop character on input overflow. 165903Sbill */ 166903Sbill ttyblock(tp) 1677625Ssam register struct tty *tp; 16839Sbill { 169903Sbill register x; 1709578Ssam 171903Sbill x = tp->t_rawq.c_cc + tp->t_canq.c_cc; 172903Sbill if (tp->t_rawq.c_cc > TTYHOG) { 17312752Ssam ttyflush(tp, FREAD|FWRITE); 1745408Swnj tp->t_state &= ~TS_TBLOCK; 175903Sbill } 17615118Skarels /* 17715118Skarels * Block further input iff: 17815118Skarels * Current input > threshold AND input is available to user program 17915118Skarels */ 18042350Smckusick if (x >= TTYHOG/2 && (tp->t_state & TS_TBLOCK) == 0 && 18140712Skarels ((tp->t_lflag&ICANON) == 0) || (tp->t_canq.c_cc > 0) && 18235811Smarc tp->t_cc[VSTOP] != _POSIX_VDISABLE) { 18342350Smckusick if (putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) { 18415118Skarels tp->t_state |= TS_TBLOCK; 18515118Skarels ttstart(tp); 18615118Skarels } 187903Sbill } 18839Sbill } 18939Sbill 19039Sbill /* 191903Sbill * Restart typewriter output following a delay 192903Sbill * timeout. 193903Sbill * The name of the routine is passed to the timeout 194903Sbill * subroutine and it is called during a clock interrupt. 195121Sbill */ 196903Sbill ttrstrt(tp) 19737584Smarc struct tty *tp; 198121Sbill { 199121Sbill 20040712Skarels #ifdef DIAGNOSTIC 2019578Ssam if (tp == 0) 2029578Ssam panic("ttrstrt"); 20340712Skarels #endif 2045408Swnj tp->t_state &= ~TS_TIMEOUT; 205903Sbill ttstart(tp); 206121Sbill } 207121Sbill 208121Sbill /* 209903Sbill * Start output on the typewriter. It is used from the top half 210903Sbill * after some characters have been put on the output queue, 211903Sbill * from the interrupt routine to transmit the next 212903Sbill * character, and after a timeout has finished. 21339Sbill */ 214903Sbill ttstart(tp) 21537584Smarc struct tty *tp; 21639Sbill { 21739Sbill 21832067Skarels if (tp->t_oproc) /* kludge for pty */ 219903Sbill (*tp->t_oproc)(tp); 22039Sbill } 22139Sbill 22239Sbill /* 223903Sbill * Common code for tty ioctls. 22439Sbill */ 2251780Sbill /*ARGSUSED*/ 2267625Ssam ttioctl(tp, com, data, flag) 2277625Ssam register struct tty *tp; 2287625Ssam caddr_t data; 22939Sbill { 23039Sbill extern int nldisp; 23137554Smckusick int s, error; 23239Sbill 233903Sbill /* 234903Sbill * If the ioctl involves modification, 23517545Skarels * hang if in the background. 236903Sbill */ 2377625Ssam switch (com) { 23839Sbill 23935811Smarc case TIOCSETD: 240903Sbill case TIOCFLUSH: 24135811Smarc /*case TIOCSPGRP:*/ 2429325Ssam case TIOCSTI: 24317598Sbloom case TIOCSWINSZ: 24435811Smarc case TIOCSETA: 24535811Smarc case TIOCSETAW: 24635811Smarc case TIOCSETAF: 24739407Smarc /**** these get removed **** 24835811Smarc case TIOCSETAS: 24935811Smarc case TIOCSETAWS: 25035811Smarc case TIOCSETAFS: 25139407Smarc /***************************/ 25240030Smarc #ifdef COMPAT_43 25340030Smarc case TIOCSETP: 25440030Smarc case TIOCSETN: 25540030Smarc case TIOCSETC: 25640030Smarc case TIOCSLTC: 25740030Smarc case TIOCLBIS: 25840030Smarc case TIOCLBIC: 25940030Smarc case TIOCLSET: 26040030Smarc case OTIOCSETD: 26140030Smarc #endif 26239555Smarc while (isbackground(u.u_procp, tp) && 26335811Smarc u.u_procp->p_pgrp->pg_jobc && 264903Sbill (u.u_procp->p_flag&SVFORK) == 0 && 26540712Skarels (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 && 26640712Skarels (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0) { 26742882Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU, 1); 26843092Smarc if ((error = tsleep((caddr_t)&lbolt, TTOPRI | PCATCH, 26943092Smarc ttybg, 0)) || 27043092Smarc (error = ttclosed(tp))) 27140712Skarels return (error); 272903Sbill } 273903Sbill break; 274903Sbill } 275903Sbill 2769578Ssam /* 2779578Ssam * Process the ioctl. 2789578Ssam */ 2797625Ssam switch (com) { 280903Sbill 2818556Sroot /* get discipline number */ 28239Sbill case TIOCGETD: 2837625Ssam *(int *)data = tp->t_line; 28439Sbill break; 28539Sbill 2868556Sroot /* set line discipline */ 2877625Ssam case TIOCSETD: { 2887625Ssam register int t = *(int *)data; 28935811Smarc dev_t dev = tp->t_dev; 2907625Ssam 29135811Smarc if ((unsigned)t >= nldisp) 29210851Ssam return (ENXIO); 29325584Skarels if (t != tp->t_line) { 29425584Skarels s = spltty(); 29525584Skarels (*linesw[tp->t_line].l_close)(tp); 29625584Skarels error = (*linesw[t].l_open)(dev, tp); 29725584Skarels if (error) { 29835811Smarc (void)(*linesw[tp->t_line].l_open)(dev, tp); 29925584Skarels splx(s); 30025584Skarels return (error); 30125584Skarels } 30225584Skarels tp->t_line = t; 30310851Ssam splx(s); 30410851Ssam } 30539Sbill break; 3067625Ssam } 30739Sbill 3088556Sroot /* prevent more opens on channel */ 3095614Swnj case TIOCEXCL: 3105614Swnj tp->t_state |= TS_XCLUDE; 3115614Swnj break; 3125614Swnj 3135614Swnj case TIOCNXCL: 3145614Swnj tp->t_state &= ~TS_XCLUDE; 3155614Swnj break; 3165614Swnj 31739Sbill case TIOCHPCL: 31835811Smarc tp->t_cflag |= HUPCL; 31939Sbill break; 32039Sbill 3213942Sbugs case TIOCFLUSH: { 3227625Ssam register int flags = *(int *)data; 3237625Ssam 3247625Ssam if (flags == 0) 3253942Sbugs flags = FREAD|FWRITE; 3267625Ssam else 3277625Ssam flags &= FREAD|FWRITE; 32812752Ssam ttyflush(tp, flags); 32939Sbill break; 3303944Sbugs } 33139Sbill 33237584Smarc case FIOASYNC: 33337584Smarc if (*(int *)data) 33437584Smarc tp->t_state |= TS_ASYNC; 33537584Smarc else 33637584Smarc tp->t_state &= ~TS_ASYNC; 33737584Smarc break; 33837584Smarc 33937584Smarc case FIONBIO: 34037584Smarc break; /* XXX remove */ 34137584Smarc 3428556Sroot /* return number of characters immediately available */ 3437625Ssam case FIONREAD: 3447625Ssam *(off_t *)data = ttnread(tp); 345174Sbill break; 346174Sbill 34713077Ssam case TIOCOUTQ: 34813077Ssam *(int *)data = tp->t_outq.c_cc; 34913077Ssam break; 35013077Ssam 3518589Sroot case TIOCSTOP: 35217545Skarels s = spltty(); 3539578Ssam if ((tp->t_state&TS_TTSTOP) == 0) { 3545573Swnj tp->t_state |= TS_TTSTOP; 3555573Swnj (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 3565573Swnj } 3577625Ssam splx(s); 3585573Swnj break; 3595573Swnj 3608589Sroot case TIOCSTART: 36117545Skarels s = spltty(); 36235811Smarc if ((tp->t_state&TS_TTSTOP) || (tp->t_lflag&FLUSHO)) { 3635573Swnj tp->t_state &= ~TS_TTSTOP; 36435811Smarc tp->t_lflag &= ~FLUSHO; 3655573Swnj ttstart(tp); 3665573Swnj } 3677625Ssam splx(s); 3685573Swnj break; 3695573Swnj 3709325Ssam /* 3719325Ssam * Simulate typing of a character at the terminal. 3729325Ssam */ 3739325Ssam case TIOCSTI: 37417183Smckusick if (u.u_uid && (flag & FREAD) == 0) 37517183Smckusick return (EPERM); 37639555Smarc if (u.u_uid && !isctty(u.u_procp, tp)) 3779325Ssam return (EACCES); 3789578Ssam (*linesw[tp->t_line].l_rint)(*(char *)data, tp); 3799325Ssam break; 3809325Ssam 38135811Smarc case TIOCGETA: { 38235811Smarc struct termios *t = (struct termios *)data; 38312752Ssam 38435811Smarc bcopy(&tp->t_termios, t, sizeof(struct termios)); 38535811Smarc break; 38635811Smarc } 38735811Smarc 38839407Smarc /*** THIS ALL GETS REMOVED ***/ 38939407Smarc case JUNK_TIOCSETAS: 39039407Smarc case JUNK_TIOCSETAWS: 39139407Smarc case JUNK_TIOCSETAFS: 39239407Smarc ((struct termios *)data)->c_cflag |= CIGNORE; 39339407Smarc switch(com) { 39439407Smarc case JUNK_TIOCSETAS: 39539407Smarc com = TIOCSETA; 39639407Smarc break; 39739407Smarc case JUNK_TIOCSETAWS: 39839407Smarc com = TIOCSETAW; 39939407Smarc break; 40039407Smarc case JUNK_TIOCSETAFS: 40139407Smarc com = TIOCSETAF; 40239407Smarc break; 40339407Smarc } 40439407Smarc /*******************************/ 40539407Smarc /*FALLTHROGH*/ 40635811Smarc case TIOCSETA: 40735811Smarc case TIOCSETAW: 40837584Smarc case TIOCSETAF: { 40935811Smarc register struct termios *t = (struct termios *)data; 41040712Skarels 41117545Skarels s = spltty(); 41239407Smarc if (com == TIOCSETAW || com == TIOCSETAF) { 41340712Skarels if (error = ttywait(tp)) { 41440712Skarels splx(s); 41540712Skarels return (error); 41640712Skarels } 41739407Smarc if (com == TIOCSETAF); 41839407Smarc ttyflush(tp, FREAD); 41939407Smarc } 42040712Skarels if ((t->c_cflag&CIGNORE) == 0) { 42135811Smarc /* 42235811Smarc * set device hardware 42335811Smarc */ 42437584Smarc if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 42537584Smarc splx(s); 42635811Smarc return (error); 42737584Smarc } else { 42840712Skarels if ((tp->t_state&TS_CARR_ON) == 0 && 42937584Smarc (tp->t_cflag&CLOCAL) && 43040712Skarels (t->c_cflag&CLOCAL) == 0) { 43137584Smarc tp->t_state &= ~TS_ISOPEN; 43237584Smarc tp->t_state |= TS_WOPEN; 43337584Smarc ttwakeup(tp); 43437584Smarc } 43535811Smarc tp->t_cflag = t->c_cflag; 43635811Smarc tp->t_ispeed = t->c_ispeed; 43735811Smarc tp->t_ospeed = t->c_ospeed; 43834492Skarels } 43935811Smarc ttsetwater(tp); 44012752Ssam } 44139407Smarc if (com != TIOCSETAF) { 44235811Smarc if ((t->c_lflag&ICANON) != (tp->t_lflag&ICANON)) 44335811Smarc if (t->c_lflag&ICANON) { 44435811Smarc tp->t_lflag |= PENDIN; 44535811Smarc ttwakeup(tp); 44635811Smarc } 44735811Smarc else { 44835811Smarc struct clist tq; 44935811Smarc 45035811Smarc catq(&tp->t_rawq, &tp->t_canq); 45135811Smarc tq = tp->t_rawq; 45235811Smarc tp->t_rawq = tp->t_canq; 45335811Smarc tp->t_canq = tq; 45435811Smarc } 45512752Ssam } 45635811Smarc tp->t_iflag = t->c_iflag; 45735811Smarc tp->t_oflag = t->c_oflag; 45842882Smarc /* 45942882Smarc * Make the EXTPROC bit read only. 46042882Smarc */ 46142882Smarc if (tp->t_lflag&EXTPROC) 46242882Smarc t->c_lflag |= EXTPROC; 46342882Smarc else 46442882Smarc t->c_lflag &= ~EXTPROC; 46535811Smarc tp->t_lflag = t->c_lflag; 46635811Smarc bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 46712752Ssam splx(s); 46812752Ssam break; 46912752Ssam } 47012752Ssam 47112752Ssam /* 47239555Smarc * Set controlling terminal. 47339555Smarc * Session ctty vnode pointer set in vnode layer. 47434492Skarels */ 47535811Smarc case TIOCSCTTY: { 47635811Smarc register struct proc *p = u.u_procp; 47734492Skarels 47839555Smarc if (!SESS_LEADER(p) || 47939555Smarc (p->p_session->s_ttyvp || tp->t_session) && 48039555Smarc (tp->t_session != p->p_session)) 48139407Smarc return (EPERM); 48235811Smarc tp->t_session = p->p_session; 48339555Smarc tp->t_pgrp = p->p_pgrp; 48439555Smarc p->p_session->s_ttyp = tp; 48539555Smarc p->p_flag |= SCTTY; 48634492Skarels break; 48735811Smarc } 48839555Smarc 48934492Skarels /* 49035811Smarc * Set terminal process group. 49117545Skarels */ 49218650Sbloom case TIOCSPGRP: { 49335811Smarc register struct proc *p = u.u_procp; 49435811Smarc register struct pgrp *pgrp = pgfind(*(int *)data); 49517545Skarels 49639555Smarc if (!isctty(p, tp)) 49739555Smarc return (ENOTTY); 49840030Smarc else if (pgrp == NULL || pgrp->pg_session != p->p_session) 49939555Smarc return (EPERM); 50039555Smarc tp->t_pgrp = pgrp; 50112752Ssam break; 50218650Sbloom } 50312752Ssam 50412752Ssam case TIOCGPGRP: 50539555Smarc if (!isctty(u.u_procp, tp)) 50639555Smarc return (ENOTTY); 50739555Smarc *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 50812752Ssam break; 50912752Ssam 51017598Sbloom case TIOCSWINSZ: 51118650Sbloom if (bcmp((caddr_t)&tp->t_winsize, data, 51218650Sbloom sizeof (struct winsize))) { 51317598Sbloom tp->t_winsize = *(struct winsize *)data; 51442882Smarc pgsignal(tp->t_pgrp, SIGWINCH, 1); 51517598Sbloom } 51617598Sbloom break; 51717598Sbloom 51817598Sbloom case TIOCGWINSZ: 51917598Sbloom *(struct winsize *)data = tp->t_winsize; 52017598Sbloom break; 52117598Sbloom 52230534Skarels case TIOCCONS: 52330534Skarels if (*(int *)data) { 52442141Smckusick if (constty && constty != tp && 52542141Smckusick (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) == 52642141Smckusick (TS_CARR_ON|TS_ISOPEN)) 52730534Skarels return (EBUSY); 52830534Skarels #ifndef UCONSOLE 52937554Smckusick if (error = suser(u.u_cred, &u.u_acflag)) 53037554Smckusick return (error); 53130534Skarels #endif 53230534Skarels constty = tp; 53330534Skarels } else if (tp == constty) 53433404Skarels constty = NULL; 53530534Skarels break; 53630534Skarels 53735811Smarc #ifdef COMPAT_43 53835811Smarc case TIOCGETP: 53935811Smarc case TIOCSETP: 54035811Smarc case TIOCSETN: 54135811Smarc case TIOCGETC: 54235811Smarc case TIOCSETC: 54335811Smarc case TIOCSLTC: 54435811Smarc case TIOCGLTC: 54535811Smarc case TIOCLBIS: 54635811Smarc case TIOCLBIC: 54735811Smarc case TIOCLSET: 54835811Smarc case TIOCLGET: 54939407Smarc case OTIOCGETD: 55039407Smarc case OTIOCSETD: 55135811Smarc return(ttcompat(tp, com, data, flag)); 55235811Smarc #endif 55335811Smarc 55439Sbill default: 5558556Sroot return (-1); 55639Sbill } 5578556Sroot return (0); 55839Sbill } 5594484Swnj 5604484Swnj ttnread(tp) 5614484Swnj struct tty *tp; 5624484Swnj { 5634484Swnj int nread = 0; 5644484Swnj 56535811Smarc if (tp->t_lflag & PENDIN) 5664484Swnj ttypend(tp); 5674484Swnj nread = tp->t_canq.c_cc; 56835811Smarc if ((tp->t_lflag & ICANON) == 0) 5694484Swnj nread += tp->t_rawq.c_cc; 5704484Swnj return (nread); 5714484Swnj } 5724484Swnj 5735408Swnj ttselect(dev, rw) 5744484Swnj dev_t dev; 5755408Swnj int rw; 5764484Swnj { 5774484Swnj register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)]; 5784484Swnj int nread; 57917545Skarels int s = spltty(); 5804484Swnj 5815408Swnj switch (rw) { 5824484Swnj 5834484Swnj case FREAD: 5844484Swnj nread = ttnread(tp); 58537584Smarc if (nread > 0 || 58640712Skarels ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0)) 5875408Swnj goto win; 5884938Swnj if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait) 5895408Swnj tp->t_state |= TS_RCOLL; 5904484Swnj else 5914484Swnj tp->t_rsel = u.u_procp; 5925408Swnj break; 5934484Swnj 5945408Swnj case FWRITE: 59535811Smarc if (tp->t_outq.c_cc <= tp->t_lowat) 5965408Swnj goto win; 5975408Swnj if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait) 5985408Swnj tp->t_state |= TS_WCOLL; 5995408Swnj else 6005408Swnj tp->t_wsel = u.u_procp; 6015408Swnj break; 6024484Swnj } 6035408Swnj splx(s); 6045408Swnj return (0); 6055408Swnj win: 6065408Swnj splx(s); 6075408Swnj return (1); 6084484Swnj } 6097436Skre 6107502Sroot /* 61125391Skarels * Initial open of tty, or (re)entry to line discipline. 6127502Sroot */ 6137502Sroot ttyopen(dev, tp) 6147625Ssam dev_t dev; 6157625Ssam register struct tty *tp; 6167502Sroot { 6177502Sroot 6187502Sroot tp->t_dev = dev; 61935811Smarc 62043092Smarc if (ttclosed(tp)) /* XXX is this still needed? (drivers do it) */ 62143092Smarc return (ERESTART); 6227502Sroot tp->t_state &= ~TS_WOPEN; 62317545Skarels if ((tp->t_state & TS_ISOPEN) == 0) { 62417545Skarels tp->t_state |= TS_ISOPEN; 62517598Sbloom bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize)); 62617545Skarels } 6278556Sroot return (0); 6287502Sroot } 6297502Sroot 6307502Sroot /* 63125391Skarels * "close" a line discipline 63225391Skarels */ 63325391Skarels ttylclose(tp) 63425391Skarels register struct tty *tp; 63525391Skarels { 63625391Skarels 63725391Skarels ttywflush(tp); 63825391Skarels } 63925391Skarels 64025391Skarels /* 6417502Sroot * clean tp on last close 6427502Sroot */ 6437502Sroot ttyclose(tp) 6447625Ssam register struct tty *tp; 6457502Sroot { 64630534Skarels if (constty == tp) 64730534Skarels constty = NULL; 64825391Skarels ttyflush(tp, FREAD|FWRITE); 64939555Smarc tp->t_session = NULL; 65039555Smarc tp->t_pgrp = NULL; 6517502Sroot tp->t_state = 0; 65240712Skarels return (0); 6537502Sroot } 6547502Sroot 6557502Sroot /* 65625391Skarels * Handle modem control transition on a tty. 65725391Skarels * Flag indicates new state of carrier. 65825391Skarels * Returns 0 if the line should be turned off, otherwise 1. 65925391Skarels */ 66025391Skarels ttymodem(tp, flag) 66125391Skarels register struct tty *tp; 66225391Skarels { 66325391Skarels 66442193Smarc if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag&MDMBUF)) { 66525391Skarels /* 66625391Skarels * MDMBUF: do flow control according to carrier flag 66725391Skarels */ 66825391Skarels if (flag) { 66925391Skarels tp->t_state &= ~TS_TTSTOP; 67025391Skarels ttstart(tp); 67125391Skarels } else if ((tp->t_state&TS_TTSTOP) == 0) { 67225391Skarels tp->t_state |= TS_TTSTOP; 67325391Skarels (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 67425391Skarels } 67525391Skarels } else if (flag == 0) { 67625391Skarels /* 67725391Skarels * Lost carrier. 67825391Skarels */ 67925391Skarels tp->t_state &= ~TS_CARR_ON; 68042193Smarc if (tp->t_state&TS_ISOPEN && (tp->t_cflag&CLOCAL) == 0) { 68142193Smarc if (tp->t_session && tp->t_session->s_leader) 68242193Smarc psignal(tp->t_session->s_leader, SIGHUP); 68342193Smarc ttyflush(tp, FREAD|FWRITE); 68442193Smarc return (0); 68525391Skarels } 68625391Skarels } else { 68725391Skarels /* 68825391Skarels * Carrier now on. 68925391Skarels */ 69025391Skarels tp->t_state |= TS_CARR_ON; 69137584Smarc ttwakeup(tp); 69225391Skarels } 69325391Skarels return (1); 69425391Skarels } 69525391Skarels 69625391Skarels /* 69725404Skarels * Default modem control routine (for other line disciplines). 69825404Skarels * Return argument flag, to turn off device on carrier drop. 69925404Skarels */ 70025415Skarels nullmodem(tp, flag) 70125415Skarels register struct tty *tp; 70225404Skarels int flag; 70325404Skarels { 70425404Skarels 70525404Skarels if (flag) 70625404Skarels tp->t_state |= TS_CARR_ON; 70739407Smarc else { 70825404Skarels tp->t_state &= ~TS_CARR_ON; 70942193Smarc if ((tp->t_cflag&CLOCAL) == 0) { 71042193Smarc if (tp->t_session && tp->t_session->s_leader) 71142193Smarc psignal(tp->t_session->s_leader, SIGHUP); 71242193Smarc return (0); 71342193Smarc } 71439407Smarc } 71542193Smarc return (1); 71625404Skarels } 71725404Skarels 71825404Skarels /* 7197502Sroot * reinput pending characters after state switch 72017545Skarels * call at spltty(). 7217502Sroot */ 7227502Sroot ttypend(tp) 7237625Ssam register struct tty *tp; 7247502Sroot { 7257502Sroot struct clist tq; 7267502Sroot register c; 7277502Sroot 72835811Smarc tp->t_lflag &= ~PENDIN; 7299578Ssam tp->t_state |= TS_TYPEN; 7307502Sroot tq = tp->t_rawq; 7317502Sroot tp->t_rawq.c_cc = 0; 7327502Sroot tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 7337502Sroot while ((c = getc(&tq)) >= 0) 7347502Sroot ttyinput(c, tp); 7359578Ssam tp->t_state &= ~TS_TYPEN; 7367502Sroot } 7377502Sroot 7387502Sroot /* 73935811Smarc * 7409578Ssam * Place a character on raw TTY input queue, 7419578Ssam * putting in delimiters and waking up top 7429578Ssam * half as needed. Also echo if required. 7439578Ssam * The arguments are the character and the 7449578Ssam * appropriate tty structure. 7457502Sroot */ 7467502Sroot ttyinput(c, tp) 7477625Ssam register c; 7487625Ssam register struct tty *tp; 7497502Sroot { 75035811Smarc register int iflag = tp->t_iflag; 75135811Smarc register int lflag = tp->t_lflag; 75235811Smarc register u_char *cc = tp->t_cc; 75335811Smarc int i, err; 7547502Sroot 7559578Ssam /* 7569578Ssam * If input is pending take it first. 7579578Ssam */ 75835811Smarc if (lflag&PENDIN) 7597502Sroot ttypend(tp); 76035811Smarc /* 76135811Smarc * Gather stats. 76235811Smarc */ 7637502Sroot tk_nin++; 76435811Smarc if (lflag&ICANON) { 76535811Smarc tk_cancc++; 76635811Smarc tp->t_cancc++; 76735811Smarc } else { 76835811Smarc tk_rawcc++; 76935811Smarc tp->t_rawcc++; 77035811Smarc } 7719578Ssam /* 77235811Smarc * Handle exceptional conditions (break, parity, framing). 7739578Ssam */ 77435811Smarc if (err = (c&TTY_ERRORMASK)) { 77535811Smarc c &= ~TTY_ERRORMASK; 77635811Smarc if (err&TTY_FE && !c) { /* break */ 77735811Smarc if (iflag&IGNBRK) 77835811Smarc goto endcase; 77935811Smarc else if (iflag&BRKINT && lflag&ISIG && 78035811Smarc (cc[VINTR] != _POSIX_VDISABLE)) 78135811Smarc c = cc[VINTR]; 78235811Smarc else { 78335811Smarc c = 0; 78435811Smarc if (iflag&PARMRK) 78535811Smarc goto parmrk; 78635811Smarc } 78735811Smarc } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) { 78835811Smarc if (iflag&IGNPAR) 78935811Smarc goto endcase; 79035811Smarc else if (iflag&PARMRK) { 79135811Smarc parmrk: 79235811Smarc putc(0377|TTY_QUOTE, &tp->t_rawq); 79335811Smarc putc(0|TTY_QUOTE, &tp->t_rawq); 79435811Smarc putc(c|TTY_QUOTE, &tp->t_rawq); 79535811Smarc goto endcase; 79635811Smarc } else 79735811Smarc c = 0; 7987502Sroot } 7999578Ssam } 8009578Ssam /* 80135811Smarc * In tandem mode, check high water mark. 8029578Ssam */ 80335811Smarc if (iflag&IXOFF) 80435811Smarc ttyblock(tp); 80535811Smarc if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP)) 8069578Ssam c &= 0177; 80742882Smarc if ((tp->t_lflag&EXTPROC) == 0) { 8089578Ssam /* 8099578Ssam * Check for literal nexting very first 8109578Ssam */ 8119578Ssam if (tp->t_state&TS_LNCH) { 81235811Smarc c |= TTY_QUOTE; 8139578Ssam tp->t_state &= ~TS_LNCH; 8149578Ssam } 8159578Ssam /* 8169578Ssam * Scan for special characters. This code 8179578Ssam * is really just a big case statement with 8189578Ssam * non-constant cases. The bottom of the 8199578Ssam * case statement is labeled ``endcase'', so goto 8209578Ssam * it after a case match, or similar. 8219578Ssam */ 82235811Smarc /* 82335811Smarc * Control chars which aren't controlled 82435811Smarc * by ICANON, ISIG, or IXON. 82535811Smarc */ 82639407Smarc if (lflag&IEXTEN) { 82740712Skarels if (CCEQ(cc[VLNEXT], c)) { 82835811Smarc if (lflag&ECHO) { 82935811Smarc if (lflag&ECHOE) 83040712Skarels ttyoutstr("^\b", tp); 83135811Smarc else 83235811Smarc ttyecho(c, tp); 83335811Smarc } 8349578Ssam tp->t_state |= TS_LNCH; 8359578Ssam goto endcase; 8369578Ssam } 837*43093Smarc if (CCEQ(cc[VDISCARD], c)) { 83835811Smarc if (lflag&FLUSHO) 83935811Smarc tp->t_lflag &= ~FLUSHO; 8407502Sroot else { 84112752Ssam ttyflush(tp, FWRITE); 8427502Sroot ttyecho(c, tp); 8439578Ssam if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 8447502Sroot ttyretype(tp); 84535811Smarc tp->t_lflag |= FLUSHO; 8467502Sroot } 8479578Ssam goto startoutput; 8489578Ssam } 84935811Smarc } 85035811Smarc /* 85135811Smarc * Signals. 85235811Smarc */ 85335811Smarc if (lflag&ISIG) { 85435811Smarc if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 85535811Smarc if ((lflag&NOFLSH) == 0) 85635811Smarc ttyflush(tp, FREAD|FWRITE); 85735811Smarc ttyecho(c, tp); 85839555Smarc pgsignal(tp->t_pgrp, 85942882Smarc CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 86035811Smarc goto endcase; 86135811Smarc } 86240712Skarels if (CCEQ(cc[VSUSP], c)) { 86335811Smarc if ((lflag&NOFLSH) == 0) 86412752Ssam ttyflush(tp, FREAD); 8659578Ssam ttyecho(c, tp); 86642882Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 8679578Ssam goto endcase; 8689578Ssam } 8699578Ssam } 8709578Ssam /* 8719578Ssam * Handle start/stop characters. 8729578Ssam */ 87335811Smarc if (iflag&IXON) { 87440712Skarels if (CCEQ(cc[VSTOP], c)) { 87535811Smarc if ((tp->t_state&TS_TTSTOP) == 0) { 87635811Smarc tp->t_state |= TS_TTSTOP; 87735811Smarc (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 87835811Smarc return; 87935811Smarc } 88035811Smarc if (!CCEQ(cc[VSTART], c)) 88135811Smarc return; 88235811Smarc /* 88335811Smarc * if VSTART == VSTOP then toggle 88435811Smarc */ 88535811Smarc goto endcase; 8869578Ssam } 88735811Smarc if (CCEQ(cc[VSTART], c)) 88835811Smarc goto restartoutput; 8899578Ssam } 8909578Ssam /* 89135811Smarc * IGNCR, ICRNL, & INLCR 8929578Ssam */ 89335811Smarc if (c == '\r') { 89435811Smarc if (iflag&IGNCR) 89535811Smarc goto endcase; 89635811Smarc else if (iflag&ICRNL) 89735811Smarc c = '\n'; 8989578Ssam } 89935811Smarc else if (c == '\n' && iflag&INLCR) 90035811Smarc c = '\r'; 90142882Smarc } 9029578Ssam /* 90335811Smarc * Non canonical mode; don't process line editing 9049578Ssam * characters; check high water mark for wakeup. 90535811Smarc * 9069578Ssam */ 90740712Skarels if ((lflag&ICANON) == 0) { 9089578Ssam if (tp->t_rawq.c_cc > TTYHOG) { 90935811Smarc if (iflag&IMAXBEL) { 91035811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 91135811Smarc (void) ttyoutput(CTRL('g'), tp); 91235811Smarc } else 91335811Smarc ttyflush(tp, FREAD | FWRITE); 91435811Smarc } else { 91535811Smarc if (putc(c, &tp->t_rawq) >= 0) { 91635811Smarc ttwakeup(tp); 91735811Smarc ttyecho(c, tp); 91835811Smarc } 9197502Sroot } 9209578Ssam goto endcase; 9219578Ssam } 92242882Smarc if ((tp->t_lflag&EXTPROC) == 0) { 9239578Ssam /* 92435811Smarc * From here on down canonical mode character 9259578Ssam * processing takes place. 9269578Ssam */ 92735811Smarc /* 92835811Smarc * erase (^H / ^?) 92935811Smarc */ 93039407Smarc if (CCEQ(cc[VERASE], c)) { 9319578Ssam if (tp->t_rawq.c_cc) 9329578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9339578Ssam goto endcase; 9349578Ssam } 93535811Smarc /* 93635811Smarc * kill (^U) 93735811Smarc */ 93835811Smarc if (CCEQ(cc[VKILL], c)) { 93937584Smarc if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount && 94040712Skarels (lflag&ECHOPRT) == 0) { 9419578Ssam while (tp->t_rawq.c_cc) 9429578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9439578Ssam } else { 9449578Ssam ttyecho(c, tp); 94535811Smarc if (lflag&ECHOK || lflag&ECHOKE) 94635811Smarc ttyecho('\n', tp); 9479578Ssam while (getc(&tp->t_rawq) > 0) 9489578Ssam ; 9499578Ssam tp->t_rocount = 0; 9509578Ssam } 9519578Ssam tp->t_state &= ~TS_LOCAL; 9529578Ssam goto endcase; 9539578Ssam } 9549578Ssam /* 95535811Smarc * word erase (^W) 9569578Ssam */ 95735811Smarc if (CCEQ(cc[VWERASE], c)) { 95835811Smarc int ctype; 95935811Smarc 96035811Smarc #define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0) 96135811Smarc /* 96235811Smarc * erase whitespace 96335811Smarc */ 96435811Smarc while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 96535811Smarc ttyrub(c, tp); 96635811Smarc if (c == -1) 96734492Skarels goto endcase; 96835811Smarc /* 96935811Smarc * special case last char of token 97035811Smarc */ 97135811Smarc ttyrub(c, tp); 97235811Smarc c = unputc(&tp->t_rawq); 97335811Smarc if (c == -1 || c == ' ' || c == '\t') { 97435811Smarc if (c != -1) 97535811Smarc (void) putc(c, &tp->t_rawq); 97634492Skarels goto endcase; 97734492Skarels } 97835811Smarc /* 97935811Smarc * erase rest of token 98035811Smarc */ 98135811Smarc ctype = CTYPE(c); 98235811Smarc do { 98335811Smarc ttyrub(c, tp); 98435811Smarc c = unputc(&tp->t_rawq); 98535811Smarc if (c == -1) 98635811Smarc goto endcase; 98735811Smarc } while (c != ' ' && c != '\t' && CTYPE(c) == ctype); 98835811Smarc (void) putc(c, &tp->t_rawq); 98935811Smarc goto endcase; 99035811Smarc #undef CTYPE 9919578Ssam } 9929578Ssam /* 99335811Smarc * reprint line (^R) 99435811Smarc */ 99535811Smarc if (CCEQ(cc[VREPRINT], c)) { 99635811Smarc ttyretype(tp); 99735811Smarc goto endcase; 99835811Smarc } 99942141Smckusick if (CCEQ(cc[VINFO], c)) { 100042882Smarc pgsignal(tp->t_pgrp, SIGINFO, 1); 100142141Smckusick if ((lflag&NOKERNINFO) == 0) 100242141Smckusick ttyinfo(tp); 100342141Smckusick goto endcase; 100442141Smckusick } 100542882Smarc } 100635811Smarc /* 10079578Ssam * Check for input buffer overflow 10089578Ssam */ 100910391Ssam if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) { 101035811Smarc if (iflag&IMAXBEL) { 101135811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 101235811Smarc (void) ttyoutput(CTRL('g'), tp); 101335811Smarc } else 101435811Smarc ttyflush(tp, FREAD | FWRITE); 10159578Ssam goto endcase; 101610391Ssam } 10179578Ssam /* 10189578Ssam * Put data char in q for user and 10199578Ssam * wakeup on seeing a line delimiter. 10209578Ssam */ 10219578Ssam if (putc(c, &tp->t_rawq) >= 0) { 102235811Smarc if (ttbreakc(c)) { 10239578Ssam tp->t_rocount = 0; 10249578Ssam catq(&tp->t_rawq, &tp->t_canq); 10257502Sroot ttwakeup(tp); 10269578Ssam } else if (tp->t_rocount++ == 0) 10279578Ssam tp->t_rocol = tp->t_col; 10289578Ssam if (tp->t_state&TS_ERASE) { 102935811Smarc /* 103035811Smarc * end of prterase \.../ 103135811Smarc */ 10329578Ssam tp->t_state &= ~TS_ERASE; 10339578Ssam (void) ttyoutput('/', tp); 10349578Ssam } 10359578Ssam i = tp->t_col; 10367502Sroot ttyecho(c, tp); 103735811Smarc if (CCEQ(cc[VEOF], c) && lflag&ECHO) { 103835811Smarc /* 103935811Smarc * Place the cursor over the '^' of the ^D. 104035811Smarc */ 10419578Ssam i = MIN(2, tp->t_col - i); 10429578Ssam while (i > 0) { 10439578Ssam (void) ttyoutput('\b', tp); 10449578Ssam i--; 10459578Ssam } 10469578Ssam } 10477502Sroot } 10489578Ssam endcase: 10499578Ssam /* 105035811Smarc * IXANY means allow any character to restart output. 10519578Ssam */ 105240712Skarels if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 && 105340712Skarels cc[VSTART] != cc[VSTOP]) 10547502Sroot return; 10559578Ssam restartoutput: 10567502Sroot tp->t_state &= ~TS_TTSTOP; 105735811Smarc tp->t_lflag &= ~FLUSHO; 10589578Ssam startoutput: 10597502Sroot ttstart(tp); 10607502Sroot } 10617502Sroot 10627502Sroot /* 10639578Ssam * Put character on TTY output queue, adding delays, 10647502Sroot * expanding tabs, and handling the CR/NL bit. 10659578Ssam * This is called both from the top half for output, 10669578Ssam * and from interrupt level for echoing. 10677502Sroot * The arguments are the character and the tty structure. 10687502Sroot * Returns < 0 if putc succeeds, otherwise returns char to resend 10697502Sroot * Must be recursive. 10707502Sroot */ 10717502Sroot ttyoutput(c, tp) 10727502Sroot register c; 10737502Sroot register struct tty *tp; 10747502Sroot { 10757502Sroot register char *colp; 10767502Sroot register ctype; 107735811Smarc register long oflag = tp->t_oflag; 107835811Smarc 107940712Skarels if ((oflag&OPOST) == 0) { 108035811Smarc if (tp->t_lflag&FLUSHO) 10817502Sroot return (-1); 10827502Sroot if (putc(c, &tp->t_outq)) 10837625Ssam return (c); 10847502Sroot tk_nout++; 108535811Smarc tp->t_outcc++; 10867502Sroot return (-1); 10877502Sroot } 108835811Smarc c &= TTY_CHARMASK; 10897502Sroot /* 10907502Sroot * Turn tabs to spaces as required 109142882Smarc * 109242882Smarc * Special case if we have external processing, we don't 109342882Smarc * do the tab expansion because we'll probably get it 109442882Smarc * wrong. If tab expansion needs to be done, let it 109542882Smarc * happen externally. 10967502Sroot */ 109742882Smarc if ((tp->t_lflag&EXTPROC) == 0 && 109842882Smarc c == '\t' && oflag&OXTABS ) { 10997502Sroot register int s; 11007502Sroot 11017502Sroot c = 8 - (tp->t_col&7); 110235811Smarc if ((tp->t_lflag&FLUSHO) == 0) { 110317545Skarels s = spltty(); /* don't interrupt tabs */ 11047502Sroot c -= b_to_q(" ", c, &tp->t_outq); 11057502Sroot tk_nout += c; 110635811Smarc tp->t_outcc += c; 11077502Sroot splx(s); 11087502Sroot } 11097502Sroot tp->t_col += c; 11107502Sroot return (c ? -1 : '\t'); 11117502Sroot } 111235811Smarc if (c == CEOT && oflag&ONOEOT) 111335811Smarc return(-1); 11147502Sroot tk_nout++; 111535811Smarc tp->t_outcc++; 11167502Sroot /* 11177502Sroot * turn <nl> to <cr><lf> if desired. 11187502Sroot */ 111935811Smarc if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0) 11207502Sroot return (c); 112135811Smarc if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq)) 112235811Smarc return (c); 11237502Sroot /* 11247502Sroot * Calculate delays. 11257502Sroot * The numbers here represent clock ticks 11267502Sroot * and are not necessarily optimal for all terminals. 11279578Ssam * 11289578Ssam * SHOULD JUST ALLOW USER TO SPECIFY DELAYS 112935811Smarc * 113035811Smarc * (actually, should THROW AWAY terminals which need delays) 11317502Sroot */ 11327502Sroot colp = &tp->t_col; 11337502Sroot ctype = partab[c]; 11347502Sroot c = 0; 11357502Sroot switch (ctype&077) { 11367502Sroot 11377502Sroot case ORDINARY: 11387502Sroot (*colp)++; 11397502Sroot 11407502Sroot case CONTROL: 11417502Sroot break; 11427502Sroot 11437502Sroot case BACKSPACE: 11447502Sroot if (*colp) 11457502Sroot (*colp)--; 11467502Sroot break; 11477502Sroot 114813821Ssam /* 114913821Ssam * This macro is close enough to the correct thing; 115013821Ssam * it should be replaced by real user settable delays 115113821Ssam * in any event... 115213821Ssam */ 115313821Ssam #define mstohz(ms) (((ms) * hz) >> 10) 11547502Sroot case NEWLINE: 11557502Sroot ctype = (tp->t_flags >> 8) & 03; 11567625Ssam if (ctype == 1) { /* tty 37 */ 115726357Skarels if (*colp > 0) { 115826357Skarels c = (((unsigned)*colp) >> 4) + 3; 115926357Skarels if ((unsigned)c > 6) 116026357Skarels c = 6; 116126357Skarels } 11629578Ssam } else if (ctype == 2) /* vt05 */ 116313821Ssam c = mstohz(100); 11647502Sroot *colp = 0; 11657502Sroot break; 11667502Sroot 11677502Sroot case TAB: 11687502Sroot ctype = (tp->t_flags >> 10) & 03; 11697625Ssam if (ctype == 1) { /* tty 37 */ 11707502Sroot c = 1 - (*colp | ~07); 11717625Ssam if (c < 5) 11727502Sroot c = 0; 11737502Sroot } 11747502Sroot *colp |= 07; 11757502Sroot (*colp)++; 11767502Sroot break; 11777502Sroot 11787502Sroot case VTAB: 11799578Ssam if (tp->t_flags&VTDELAY) /* tty 37 */ 11807502Sroot c = 0177; 11817502Sroot break; 11827502Sroot 11837502Sroot case RETURN: 11847502Sroot ctype = (tp->t_flags >> 12) & 03; 11859578Ssam if (ctype == 1) /* tn 300 */ 118613821Ssam c = mstohz(83); 11879578Ssam else if (ctype == 2) /* ti 700 */ 118813821Ssam c = mstohz(166); 11899578Ssam else if (ctype == 3) { /* concept 100 */ 11907502Sroot int i; 11919578Ssam 11927502Sroot if ((i = *colp) >= 0) 11939578Ssam for (; i < 9; i++) 11947502Sroot (void) putc(0177, &tp->t_outq); 11957502Sroot } 11967502Sroot *colp = 0; 11977502Sroot } 119835811Smarc if (c && (tp->t_lflag&FLUSHO) == 0) 119935811Smarc (void) putc(c|TTY_QUOTE, &tp->t_outq); 12007502Sroot return (-1); 12017502Sroot } 120213821Ssam #undef mstohz 12037502Sroot 12047502Sroot /* 12057502Sroot * Called from device's read routine after it has 12067502Sroot * calculated the tty-structure given as argument. 12077502Sroot */ 120837584Smarc ttread(tp, uio, flag) 12097625Ssam register struct tty *tp; 12107722Swnj struct uio *uio; 12117502Sroot { 12127502Sroot register struct clist *qp; 121335811Smarc register int c; 121441383Smarc register long lflag; 121535811Smarc register u_char *cc = tp->t_cc; 12169859Ssam int s, first, error = 0; 12177502Sroot 12187502Sroot loop: 121941383Smarc lflag = tp->t_lflag; 122037584Smarc s = spltty(); 12219578Ssam /* 122237584Smarc * take pending input first 12239578Ssam */ 122435811Smarc if (lflag&PENDIN) 12257502Sroot ttypend(tp); 12269859Ssam splx(s); 122740712Skarels 12289578Ssam /* 12299578Ssam * Hang process if it's in the background. 12309578Ssam */ 123139555Smarc if (isbackground(u.u_procp, tp)) { 123224392Skarels if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) || 123324392Skarels (u.u_procp->p_sigmask & sigmask(SIGTTIN)) || 123435811Smarc u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0) 12358520Sroot return (EIO); 123642882Smarc pgsignal(u.u_procp->p_pgrp, SIGTTIN, 1); 123743092Smarc if ((error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, 123843092Smarc ttybg, 0)) || 123943092Smarc (error = ttclosed(tp))) 124040712Skarels return (error); 124123165Sbloom goto loop; 12427502Sroot } 124340712Skarels 12449578Ssam /* 124535811Smarc * If canonical, use the canonical queue, 124635811Smarc * else use the raw queue. 124737584Smarc * 124837584Smarc * XXX - should get rid of canonical queue. 124937584Smarc * (actually, should get rid of clists...) 12509578Ssam */ 125135811Smarc qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq; 125240712Skarels 12539578Ssam /* 125440712Skarels * If there is no input, sleep on rawq 125540712Skarels * awaiting hardware receipt and notification. 125640712Skarels * If we have data, we don't need to check for carrier. 12579578Ssam */ 125817545Skarels s = spltty(); 12599578Ssam if (qp->c_cc <= 0) { 126040712Skarels int carrier; 126140712Skarels 126240712Skarels carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL); 126340712Skarels if (!carrier && tp->t_state&TS_ISOPEN) { 12649859Ssam splx(s); 126540712Skarels return (0); /* EOF */ 12667502Sroot } 126737728Smckusick if (flag & IO_NDELAY) { 126837584Smarc splx(s); 126937584Smarc return (EWOULDBLOCK); 127037584Smarc } 127140712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 127240712Skarels carrier ? ttyin : ttopen, 0); 12739859Ssam splx(s); 127443092Smarc if (error || (error = ttclosed(tp))) 127540712Skarels return (error); 12769578Ssam goto loop; 12779578Ssam } 12789859Ssam splx(s); 127940712Skarels 12809578Ssam /* 128135811Smarc * Input present, check for input mapping and processing. 12829578Ssam */ 12839578Ssam first = 1; 12849578Ssam while ((c = getc(qp)) >= 0) { 12859578Ssam /* 128635811Smarc * delayed suspend (^Y) 12879578Ssam */ 128835811Smarc if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) { 128942882Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 12909578Ssam if (first) { 129143092Smarc if ((error = tsleep((caddr_t)&lbolt, 129243092Smarc TTIPRI | PCATCH, ttybg, 0)) || 129343092Smarc (error = ttclosed(tp))) 129440712Skarels break; 12959578Ssam goto loop; 12969578Ssam } 12979578Ssam break; 12987502Sroot } 12999578Ssam /* 130035811Smarc * Interpret EOF only in canonical mode. 13019578Ssam */ 130235811Smarc if (CCEQ(cc[VEOF], c) && lflag&ICANON) 13039578Ssam break; 13049578Ssam /* 13059578Ssam * Give user character. 13069578Ssam */ 130740712Skarels error = ureadc(c, uio); 13089578Ssam if (error) 13099578Ssam break; 131014938Smckusick if (uio->uio_resid == 0) 13119578Ssam break; 13129578Ssam /* 131335811Smarc * In canonical mode check for a "break character" 13149578Ssam * marking the end of a "line of input". 13159578Ssam */ 131640712Skarels if (lflag&ICANON && ttbreakc(c)) 13179578Ssam break; 13189578Ssam first = 0; 13197502Sroot } 13209578Ssam /* 13219578Ssam * Look to unblock output now that (presumably) 13229578Ssam * the input queue has gone down. 13239578Ssam */ 132435811Smarc if (tp->t_state&TS_TBLOCK && tp->t_rawq.c_cc < TTYHOG/5) { 132535811Smarc if (cc[VSTART] != _POSIX_VDISABLE 132635811Smarc && putc(cc[VSTART], &tp->t_outq) == 0) { 13277502Sroot tp->t_state &= ~TS_TBLOCK; 13287502Sroot ttstart(tp); 13297502Sroot } 133035811Smarc } 13318520Sroot return (error); 13327502Sroot } 13337502Sroot 13347502Sroot /* 133525391Skarels * Check the output queue on tp for space for a kernel message 133625391Skarels * (from uprintf/tprintf). Allow some space over the normal 133725391Skarels * hiwater mark so we don't lose messages due to normal flow 133825391Skarels * control, but don't let the tty run amok. 133930695Skarels * Sleeps here are not interruptible, but we return prematurely 134030695Skarels * if new signals come in. 134125391Skarels */ 134225391Skarels ttycheckoutq(tp, wait) 134325391Skarels register struct tty *tp; 134425391Skarels int wait; 134525391Skarels { 134630695Skarels int hiwat, s, oldsig; 134725391Skarels 134835811Smarc hiwat = tp->t_hiwat; 134925391Skarels s = spltty(); 135030695Skarels oldsig = u.u_procp->p_sig; 135125391Skarels if (tp->t_outq.c_cc > hiwat + 200) 135229946Skarels while (tp->t_outq.c_cc > hiwat) { 135329946Skarels ttstart(tp); 135430695Skarels if (wait == 0 || u.u_procp->p_sig != oldsig) { 135529946Skarels splx(s); 135629946Skarels return (0); 135729946Skarels } 135830695Skarels timeout(wakeup, (caddr_t)&tp->t_outq, hz); 135929946Skarels tp->t_state |= TS_ASLEEP; 136030695Skarels sleep((caddr_t)&tp->t_outq, PZERO - 1); 136125391Skarels } 136225391Skarels splx(s); 136325391Skarels return (1); 136425391Skarels } 136525391Skarels 136625391Skarels /* 13677502Sroot * Called from the device's write routine after it has 13687502Sroot * calculated the tty-structure given as argument. 13697502Sroot */ 137037584Smarc ttwrite(tp, uio, flag) 13717625Ssam register struct tty *tp; 13729578Ssam register struct uio *uio; 13737502Sroot { 13747502Sroot register char *cp; 137540712Skarels register int cc = 0, ce; 13769578Ssam int i, hiwat, cnt, error, s; 13777502Sroot char obuf[OBUFSIZ]; 13787502Sroot 137935811Smarc hiwat = tp->t_hiwat; 13809578Ssam cnt = uio->uio_resid; 13819578Ssam error = 0; 13827502Sroot loop: 138337584Smarc s = spltty(); 138440712Skarels if ((tp->t_state&TS_CARR_ON) == 0 && (tp->t_cflag&CLOCAL) == 0) { 138537584Smarc if (tp->t_state&TS_ISOPEN) { 138637584Smarc splx(s); 138737584Smarc return (EIO); 138837728Smckusick } else if (flag & IO_NDELAY) { 138937584Smarc splx(s); 139040712Skarels error = EWOULDBLOCK; 139140712Skarels goto out; 139237584Smarc } else { 139337584Smarc /* 139437584Smarc * sleep awaiting carrier 139537584Smarc */ 139640712Skarels error = tsleep((caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 139740712Skarels ttopen, 0); 139837584Smarc splx(s); 139943092Smarc if (error || (error = ttclosed(tp))) 140040712Skarels goto out; 140137584Smarc goto loop; 140237584Smarc } 140337584Smarc } 140437584Smarc splx(s); 14059578Ssam /* 14069578Ssam * Hang the process if it's in the background. 14079578Ssam */ 140839555Smarc if (isbackground(u.u_procp, tp) && 140935811Smarc (tp->t_lflag&TOSTOP) && (u.u_procp->p_flag&SVFORK)==0 && 141040712Skarels (u.u_procp->p_sigignore & sigmask(SIGTTOU)) == 0 && 141140712Skarels (u.u_procp->p_sigmask & sigmask(SIGTTOU)) == 0 && 141235811Smarc u.u_procp->p_pgrp->pg_jobc) { 141342882Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU, 1); 141443092Smarc if ((error = tsleep((caddr_t)&lbolt, TTIPRI | PCATCH, 141543092Smarc ttybg, 0)) || 141643092Smarc (error = ttclosed(tp))) 141740712Skarels goto out; 141821776Sbloom goto loop; 14197502Sroot } 14209578Ssam /* 14219578Ssam * Process the user's data in at most OBUFSIZ 142240712Skarels * chunks. Perform any output translation. 142340712Skarels * Keep track of high water mark, sleep on overflow 142440712Skarels * awaiting device aid in acquiring new space. 14259578Ssam */ 142640712Skarels while (uio->uio_resid > 0 || cc > 0) { 142740712Skarels if (tp->t_lflag&FLUSHO) { 142840712Skarels uio->uio_resid = 0; 142940712Skarels return (0); 143040712Skarels } 143140712Skarels if (tp->t_outq.c_cc > hiwat) 143232067Skarels goto ovhiwat; 14339578Ssam /* 143440712Skarels * Grab a hunk of data from the user, 143540712Skarels * unless we have some leftover from last time. 14369578Ssam */ 14377822Sroot if (cc == 0) { 143840712Skarels cc = min(uio->uio_resid, OBUFSIZ); 143940712Skarels cp = obuf; 144040712Skarels error = uiomove(cp, cc, uio); 144140712Skarels if (error) { 144240712Skarels cc = 0; 144340712Skarels break; 144440712Skarels } 14457822Sroot } 14469578Ssam /* 14479578Ssam * If nothing fancy need be done, grab those characters we 14489578Ssam * can handle without any of ttyoutput's processing and 14499578Ssam * just transfer them to the output q. For those chars 14509578Ssam * which require special processing (as indicated by the 14519578Ssam * bits in partab), call ttyoutput. After processing 14529578Ssam * a hunk of data, look for FLUSHO so ^O's will take effect 14539578Ssam * immediately. 14549578Ssam */ 14559578Ssam while (cc > 0) { 145640712Skarels if ((tp->t_oflag&OPOST) == 0) 14577502Sroot ce = cc; 14587502Sroot else { 145934492Skarels ce = cc - scanc((unsigned)cc, (u_char *)cp, 146034492Skarels (u_char *)partab, 077); 14619578Ssam /* 14629578Ssam * If ce is zero, then we're processing 14639578Ssam * a special character through ttyoutput. 14649578Ssam */ 14659578Ssam if (ce == 0) { 14667502Sroot tp->t_rocount = 0; 14677502Sroot if (ttyoutput(*cp, tp) >= 0) { 146821776Sbloom /* no c-lists, wait a bit */ 146921776Sbloom ttstart(tp); 147043092Smarc if ((error = tsleep((caddr_t)&lbolt, 147143092Smarc TTOPRI | PCATCH, ttybuf, 0)) || 147243092Smarc (error = ttclosed(tp))) 147340712Skarels break; 147421776Sbloom goto loop; 14757502Sroot } 14769578Ssam cp++, cc--; 147735811Smarc if ((tp->t_lflag&FLUSHO) || 14789578Ssam tp->t_outq.c_cc > hiwat) 14797502Sroot goto ovhiwat; 14809578Ssam continue; 14817502Sroot } 14827502Sroot } 14839578Ssam /* 14849578Ssam * A bunch of normal characters have been found, 14859578Ssam * transfer them en masse to the output queue and 14869578Ssam * continue processing at the top of the loop. 14879578Ssam * If there are any further characters in this 14889578Ssam * <= OBUFSIZ chunk, the first should be a character 14899578Ssam * requiring special handling by ttyoutput. 14909578Ssam */ 14917502Sroot tp->t_rocount = 0; 14929578Ssam i = b_to_q(cp, ce, &tp->t_outq); 14939578Ssam ce -= i; 14949578Ssam tp->t_col += ce; 14959578Ssam cp += ce, cc -= ce, tk_nout += ce; 149635811Smarc tp->t_outcc += ce; 14979578Ssam if (i > 0) { 14989578Ssam /* out of c-lists, wait a bit */ 14997502Sroot ttstart(tp); 150043092Smarc if ((error = tsleep((caddr_t)&lbolt, 150143092Smarc TTOPRI | PCATCH, ttybuf, 0)) || 150243092Smarc (error = ttclosed(tp))) 150340712Skarels break; 150421776Sbloom goto loop; 15057502Sroot } 150635811Smarc if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat) 150740712Skarels break; 15087502Sroot } 150935811Smarc ttstart(tp); 15107502Sroot } 151140712Skarels out: 151240712Skarels /* 151340712Skarels * If cc is nonzero, we leave the uio structure inconsistent, 151440712Skarels * as the offset and iov pointers have moved forward, 151540712Skarels * but it doesn't matter (the call will either return short 151640712Skarels * or restart with a new uio). 151740712Skarels */ 151840712Skarels uio->uio_resid += cc; 15198520Sroot return (error); 152040712Skarels 15217502Sroot ovhiwat: 152232067Skarels ttstart(tp); 152332067Skarels s = spltty(); 15249578Ssam /* 152535811Smarc * This can only occur if FLUSHO is set in t_lflag, 152632067Skarels * or if ttstart/oproc is synchronous (or very fast). 15279578Ssam */ 15287502Sroot if (tp->t_outq.c_cc <= hiwat) { 15299578Ssam splx(s); 15307502Sroot goto loop; 15317502Sroot } 153237728Smckusick if (flag & IO_NDELAY) { 153317545Skarels splx(s); 153440712Skarels uio->uio_resid += cc; 15357822Sroot if (uio->uio_resid == cnt) 15368520Sroot return (EWOULDBLOCK); 15378520Sroot return (0); 15387502Sroot } 15397502Sroot tp->t_state |= TS_ASLEEP; 154040712Skarels error = tsleep((caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 15419578Ssam splx(s); 154243092Smarc if (error || (error = ttclosed(tp))) 154340712Skarels goto out; 15447502Sroot goto loop; 15457502Sroot } 15467502Sroot 15477502Sroot /* 15487502Sroot * Rubout one character from the rawq of tp 15497502Sroot * as cleanly as possible. 15507502Sroot */ 15517502Sroot ttyrub(c, tp) 15527625Ssam register c; 15537625Ssam register struct tty *tp; 15547502Sroot { 15557502Sroot register char *cp; 15567502Sroot register int savecol; 15577502Sroot int s; 15587502Sroot char *nextc(); 15597502Sroot 156042882Smarc if ((tp->t_lflag&ECHO) == 0 || (tp->t_lflag&EXTPROC)) 15617502Sroot return; 156235811Smarc tp->t_lflag &= ~FLUSHO; 156335811Smarc if (tp->t_lflag&ECHOE) { 15647502Sroot if (tp->t_rocount == 0) { 15657502Sroot /* 15667502Sroot * Screwed by ttwrite; retype 15677502Sroot */ 15687502Sroot ttyretype(tp); 15697502Sroot return; 15707502Sroot } 157135811Smarc if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE)) 15727502Sroot ttyrubo(tp, 2); 157335811Smarc else switch (partab[c&=0377]&077) { 15747502Sroot 15757502Sroot case ORDINARY: 157635811Smarc ttyrubo(tp, 1); 15777502Sroot break; 15787502Sroot 15797502Sroot case VTAB: 15807502Sroot case BACKSPACE: 15817502Sroot case CONTROL: 15827502Sroot case RETURN: 158335811Smarc if (tp->t_lflag&ECHOCTL) 15847502Sroot ttyrubo(tp, 2); 15857502Sroot break; 15867502Sroot 158735811Smarc case TAB: { 158835811Smarc int c; 158935811Smarc 15907502Sroot if (tp->t_rocount < tp->t_rawq.c_cc) { 15917502Sroot ttyretype(tp); 15927502Sroot return; 15937502Sroot } 159417545Skarels s = spltty(); 15957502Sroot savecol = tp->t_col; 15969578Ssam tp->t_state |= TS_CNTTB; 159735811Smarc tp->t_lflag |= FLUSHO; 15987502Sroot tp->t_col = tp->t_rocol; 15999578Ssam cp = tp->t_rawq.c_cf; 160039407Smarc if (cp) 160139407Smarc c = *cp; /* XXX FIX NEXTC */ 160235811Smarc for (; cp; cp = nextc(&tp->t_rawq, cp, &c)) 160335811Smarc ttyecho(c, tp); 160435811Smarc tp->t_lflag &= ~FLUSHO; 16059578Ssam tp->t_state &= ~TS_CNTTB; 16067502Sroot splx(s); 16077502Sroot /* 16087502Sroot * savecol will now be length of the tab 16097502Sroot */ 16107502Sroot savecol -= tp->t_col; 16117502Sroot tp->t_col += savecol; 16127502Sroot if (savecol > 8) 16137502Sroot savecol = 8; /* overflow screw */ 16147502Sroot while (--savecol >= 0) 16157502Sroot (void) ttyoutput('\b', tp); 16167502Sroot break; 161735811Smarc } 16187502Sroot 16197502Sroot default: 162037584Smarc /* XXX */ 162135811Smarc printf("ttyrub: would panic c = %d, val = %d\n", 162235811Smarc c, partab[c&=0377]&077); 162335811Smarc /*panic("ttyrub");*/ 16247502Sroot } 162535811Smarc } else if (tp->t_lflag&ECHOPRT) { 16269578Ssam if ((tp->t_state&TS_ERASE) == 0) { 16277502Sroot (void) ttyoutput('\\', tp); 16289578Ssam tp->t_state |= TS_ERASE; 16297502Sroot } 16307502Sroot ttyecho(c, tp); 16317502Sroot } else 163235811Smarc ttyecho(tp->t_cc[VERASE], tp); 16337502Sroot tp->t_rocount--; 16347502Sroot } 16357502Sroot 16367502Sroot /* 16377502Sroot * Crt back over cnt chars perhaps 16387502Sroot * erasing them. 16397502Sroot */ 16407502Sroot ttyrubo(tp, cnt) 16417625Ssam register struct tty *tp; 16427625Ssam int cnt; 16437502Sroot { 16447502Sroot 16457502Sroot while (--cnt >= 0) 164640712Skarels ttyoutstr("\b \b", tp); 16477502Sroot } 16487502Sroot 16497502Sroot /* 16507502Sroot * Reprint the rawq line. 16517502Sroot * We assume c_cc has already been checked. 16527502Sroot */ 16537502Sroot ttyretype(tp) 16547625Ssam register struct tty *tp; 16557502Sroot { 16567502Sroot register char *cp; 16577502Sroot char *nextc(); 165835811Smarc int s, c; 16597502Sroot 166035811Smarc if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 166135811Smarc ttyecho(tp->t_cc[VREPRINT], tp); 16627502Sroot (void) ttyoutput('\n', tp); 166317545Skarels s = spltty(); 166435811Smarc /*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE 166535811Smarc BIT OF FIRST CHAR ****/ 166635811Smarc for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) { 166735811Smarc ttyecho(c, tp); 166835811Smarc } 166935811Smarc for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) { 167035811Smarc ttyecho(c, tp); 167135811Smarc } 16729578Ssam tp->t_state &= ~TS_ERASE; 16737502Sroot splx(s); 16747502Sroot tp->t_rocount = tp->t_rawq.c_cc; 16757502Sroot tp->t_rocol = 0; 16767502Sroot } 16777502Sroot 16787502Sroot /* 167935811Smarc * Echo a typed character to the terminal. 16807502Sroot */ 16817502Sroot ttyecho(c, tp) 16827625Ssam register c; 16837625Ssam register struct tty *tp; 16847502Sroot { 16859578Ssam if ((tp->t_state&TS_CNTTB) == 0) 168635811Smarc tp->t_lflag &= ~FLUSHO; 168742882Smarc if (((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 || 168842882Smarc c == '\n')) || (tp->t_lflag&EXTPROC)) 16897502Sroot return; 169035811Smarc if (tp->t_lflag&ECHOCTL) { 169140712Skarels if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' || 169240712Skarels c == 0177) { 16937502Sroot (void) ttyoutput('^', tp); 169435811Smarc c &= TTY_CHARMASK; 16957502Sroot if (c == 0177) 16967502Sroot c = '?'; 16977502Sroot else 16987502Sroot c += 'A' - 1; 16997502Sroot } 17007502Sroot } 170135811Smarc (void) ttyoutput(c, tp); 17027502Sroot } 17037502Sroot 17047502Sroot /* 17057502Sroot * send string cp to tp 17067502Sroot */ 170740712Skarels ttyoutstr(cp, tp) 17087625Ssam register char *cp; 17097625Ssam register struct tty *tp; 17107502Sroot { 17117502Sroot register char c; 17127502Sroot 17137502Sroot while (c = *cp++) 17147502Sroot (void) ttyoutput(c, tp); 17157502Sroot } 17167502Sroot 17177502Sroot ttwakeup(tp) 17187502Sroot struct tty *tp; 17197502Sroot { 17207502Sroot 17217502Sroot if (tp->t_rsel) { 17227502Sroot selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL); 17237502Sroot tp->t_state &= ~TS_RCOLL; 17247502Sroot tp->t_rsel = 0; 17257502Sroot } 172612752Ssam if (tp->t_state & TS_ASYNC) 172742882Smarc pgsignal(tp->t_pgrp, SIGIO, 1); 17287502Sroot wakeup((caddr_t)&tp->t_rawq); 17297502Sroot } 173035811Smarc 173135811Smarc /* 173235811Smarc * set tty hi and low water marks 173335811Smarc * 173435811Smarc * Try to arrange the dynamics so there's about one second 173535811Smarc * from hi to low water. 173635811Smarc * 173735811Smarc */ 173835811Smarc ttsetwater(tp) 173935811Smarc struct tty *tp; 174035811Smarc { 174135811Smarc register cps = tp->t_ospeed / 10; 174235811Smarc register x; 174335811Smarc 174435811Smarc #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x)) 174535811Smarc tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT); 174635811Smarc x += cps; 174735811Smarc x = clamp(x, TTMAXHIWAT, TTMINHIWAT); 174835811Smarc tp->t_hiwat = roundup(x, CBSIZE); 174935811Smarc #undef clamp 175035811Smarc } 175135811Smarc 175235811Smarc ttspeedtab(speed, table) 175335811Smarc struct speedtab table[]; 175435811Smarc { 175535811Smarc register int i; 175635811Smarc 175735811Smarc for (i = 0; table[i].sp_speed != -1; i++) 175835811Smarc if (table[i].sp_speed == speed) 175935811Smarc return(table[i].sp_code); 176035811Smarc return(-1); 176135811Smarc } 176239407Smarc 176341177Smarc int ttyhostname = 0; 176439407Smarc /* 176539407Smarc * (^T) 176639407Smarc * Report on state of foreground process group. 176739407Smarc */ 176839407Smarc ttyinfo(tp) 176939407Smarc struct tty *tp; 177039407Smarc { 177141177Smarc register struct proc *p, *pick = NULL; 177241177Smarc register char *cp = hostname; 177341177Smarc int x, s; 177441177Smarc struct timeval utime, stime; 177542350Smckusick #define pgtok(a) (((a)*NBPG)/1024) 177639407Smarc 177739407Smarc if (ttycheckoutq(tp,0) == 0) 177839407Smarc return; 177941177Smarc /* 178041177Smarc * hostname 178141177Smarc */ 178241177Smarc if (ttyhostname) { 178341177Smarc if (*cp == '\0') 178441177Smarc ttyprintf(tp, "amnesia"); 178541177Smarc else 178641177Smarc while (*cp && *cp != '.') 178741177Smarc tputchar(*cp++, tp); 178841177Smarc tputchar(' '); 178941177Smarc } 179041177Smarc /* 179141177Smarc * load average 179241177Smarc */ 179341177Smarc x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT; 179441177Smarc ttyprintf(tp, "load: %d.", x/100); 179541177Smarc ttyoutint(x%100, 10, 2, tp); 179639555Smarc if (tp->t_session == NULL) 179741177Smarc ttyprintf(tp, " not a controlling terminal\n"); 179841177Smarc else if (tp->t_pgrp == NULL) 179941177Smarc ttyprintf(tp, " no foreground process group\n"); 180041177Smarc else if ((p = tp->t_pgrp->pg_mem) == NULL) 180141177Smarc ttyprintf(tp, " empty foreground process group\n"); 180239407Smarc else { 180341177Smarc /* pick interesting process */ 180439407Smarc for (; p != NULL; p = p->p_pgrpnxt) { 180541177Smarc if (proc_compare(pick, p)) 180641177Smarc pick = p; 180739407Smarc } 180841177Smarc ttyprintf(tp, " cmd: %s %d [%s] ", 180941177Smarc pick->p_comm, pick->p_pid, 181041177Smarc pick->p_wmesg ? pick->p_wmesg : "running"); 181141177Smarc /* 181241177Smarc * cpu time 181341177Smarc */ 181441177Smarc if (u.u_procp == pick) 181541177Smarc s = splclock(); 181641177Smarc utime = pick->p_utime; 181741177Smarc stime = pick->p_stime; 181841177Smarc if (u.u_procp == pick) 181941177Smarc splx(s); 182041177Smarc /* user time */ 182141177Smarc x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */ 182241177Smarc ttyoutint(utime.tv_sec, 10, 1, tp); 182341177Smarc tputchar('.', tp); 182441177Smarc ttyoutint(x, 10, 2, tp); 182541177Smarc tputchar('u', tp); 182641177Smarc tputchar(' ', tp); 182741177Smarc /* system time */ 182841177Smarc x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */ 182941177Smarc ttyoutint(stime.tv_sec, 10, 1, tp); 183041177Smarc tputchar('.', tp); 183141177Smarc ttyoutint(x, 10, 2, tp); 183241177Smarc tputchar('s', tp); 183341177Smarc tputchar(' ', tp); 183441177Smarc /* 183541177Smarc * pctcpu 183641177Smarc */ 183741177Smarc x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT; 183841177Smarc ttyoutint(x/100, 10, 1, tp); 183941177Smarc #ifdef notdef /* do we really want this ??? */ 184041177Smarc tputchar('.', tp); 184141177Smarc ttyoutint(x%100, 10, 2, tp); 184241177Smarc #endif 184341177Smarc ttyprintf(tp, "%% %dk\n", pgtok(pick->p_ssize + pick->p_dsize)); 184439407Smarc } 184541177Smarc tp->t_rocount = 0; /* so pending input will be retyped if BS */ 184639407Smarc } 184739407Smarc 184841177Smarc ttyoutint(n, base, min, tp) 184941177Smarc register int n, base, min; 185041177Smarc register struct tty *tp; 185141177Smarc { 185241177Smarc char info[16]; 185341177Smarc register char *p = info; 185441177Smarc 185541177Smarc while (--min >= 0 || n) { 185641177Smarc *p++ = "0123456789abcdef"[n%base]; 185741177Smarc n /= base; 185841177Smarc } 185941177Smarc while (p > info) 186041177Smarc ttyoutput(*--p, tp); 186141177Smarc } 186241177Smarc 186341177Smarc /* 186441177Smarc * Returns 1 if p2 is "better" than p1 186541177Smarc * 186641177Smarc * The algorithm for picking the "interesting" process is thus: 186741177Smarc * 186841177Smarc * 1) (Only foreground processes are eligable - implied) 186941177Smarc * 2) Runnable processes are favored over anything 187041177Smarc * else. The runner with the highest cpu 187141177Smarc * utilization is picked (p_cpu). Ties are 187241177Smarc * broken by picking the highest pid. 187341177Smarc * 3 Next, the sleeper with the shortest sleep 187441177Smarc * time is favored. With ties, we pick out 187541177Smarc * just "short-term" sleepers (SSINTR == 0). 187641177Smarc * Further ties are broken by picking the highest 187741177Smarc * pid. 187841177Smarc * 187941177Smarc */ 188041177Smarc #define isrun(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 188141177Smarc proc_compare(p1, p2) 188241177Smarc register struct proc *p1, *p2; 188341177Smarc { 188441177Smarc 188541177Smarc if (p1 == NULL) 188641177Smarc return (1); 188741177Smarc /* 188841177Smarc * see if at least one of them is runnable 188941177Smarc */ 189041177Smarc switch (isrun(p1)<<1 | isrun(p2)) { 189141177Smarc case 0x01: 189241177Smarc return (1); 189341177Smarc case 0x10: 189441177Smarc return (0); 189541177Smarc case 0x11: 189641177Smarc /* 189741177Smarc * tie - favor one with highest recent cpu utilization 189841177Smarc */ 189941177Smarc if (p2->p_cpu > p1->p_cpu) 190041177Smarc return (1); 190141177Smarc if (p1->p_cpu > p2->p_cpu) 190241177Smarc return (0); 190341177Smarc return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 190441177Smarc } 190541177Smarc /* 190641177Smarc * pick the one with the smallest sleep time 190741177Smarc */ 190841177Smarc if (p2->p_slptime > p1->p_slptime) 190941177Smarc return (0); 191041177Smarc if (p1->p_slptime > p2->p_slptime) 191141177Smarc return (1); 191241177Smarc /* 191341177Smarc * favor one sleeping in a non-interruptible sleep 191441177Smarc */ 191541177Smarc if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0) 191641177Smarc return (1); 191741177Smarc if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0) 191841177Smarc return (0); 191941177Smarc return(p2->p_pid > p1->p_pid); /* tie - return highest pid */ 192041177Smarc } 192139407Smarc #define TOTTY 0x2 /* XXX should be in header */ 192239407Smarc /*VARARGS2*/ 192339407Smarc ttyprintf(tp, fmt, x1) 192439555Smarc struct tty *tp; 192539407Smarc char *fmt; 192639407Smarc unsigned x1; 192739407Smarc { 192839555Smarc prf(fmt, &x1, TOTTY, (caddr_t)tp); 192939407Smarc } 193039555Smarc 193139555Smarc /* 193239555Smarc * Output char to tty; console putchar style. 193339555Smarc */ 193439555Smarc tputchar(c, tp) 193539555Smarc int c; 193639555Smarc struct tty *tp; 193739555Smarc { 193839555Smarc register s = spltty(); 193939555Smarc 194039555Smarc if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN)) 194139555Smarc == (TS_CARR_ON | TS_ISOPEN)) { 194239555Smarc if (c == '\n') 194339555Smarc (void) ttyoutput('\r', tp); 194439555Smarc (void) ttyoutput(c, tp); 194539555Smarc ttstart(tp); 194639555Smarc splx(s); 194739555Smarc return (0); 194839555Smarc } 194939555Smarc splx(s); 195039555Smarc return (-1); 195139555Smarc } 1952