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*43377Smarc * @(#)tty.c 7.33 (Berkeley) 06/21/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; 123*43377Smarc if (error = ttysleep(tp, (caddr_t)&tp->t_outq, 124*43377Smarc TTOPRI | PCATCH, 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 */ 17942350Smckusick if (x >= TTYHOG/2 && (tp->t_state & TS_TBLOCK) == 0 && 18040712Skarels ((tp->t_lflag&ICANON) == 0) || (tp->t_canq.c_cc > 0) && 18135811Smarc tp->t_cc[VSTOP] != _POSIX_VDISABLE) { 18242350Smckusick 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) { 26642882Smarc pgsignal(u.u_procp->p_pgrp, SIGTTOU, 1); 267*43377Smarc if (error = ttysleep(tp, (caddr_t)&lbolt, 268*43377Smarc TTOPRI | PCATCH, 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; 45642882Smarc /* 45742882Smarc * Make the EXTPROC bit read only. 45842882Smarc */ 45942882Smarc if (tp->t_lflag&EXTPROC) 46042882Smarc t->c_lflag |= EXTPROC; 46142882Smarc else 46242882Smarc t->c_lflag &= ~EXTPROC; 46335811Smarc tp->t_lflag = t->c_lflag; 46435811Smarc bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 46512752Ssam splx(s); 46612752Ssam break; 46712752Ssam } 46812752Ssam 46912752Ssam /* 47039555Smarc * Set controlling terminal. 47139555Smarc * Session ctty vnode pointer set in vnode layer. 47234492Skarels */ 47335811Smarc case TIOCSCTTY: { 47435811Smarc register struct proc *p = u.u_procp; 47534492Skarels 47639555Smarc if (!SESS_LEADER(p) || 47739555Smarc (p->p_session->s_ttyvp || tp->t_session) && 47839555Smarc (tp->t_session != p->p_session)) 47939407Smarc return (EPERM); 48035811Smarc tp->t_session = p->p_session; 48139555Smarc tp->t_pgrp = p->p_pgrp; 48239555Smarc p->p_session->s_ttyp = tp; 48339555Smarc p->p_flag |= SCTTY; 48434492Skarels break; 48535811Smarc } 48639555Smarc 48734492Skarels /* 48835811Smarc * Set terminal process group. 48917545Skarels */ 49018650Sbloom case TIOCSPGRP: { 49135811Smarc register struct proc *p = u.u_procp; 49235811Smarc register struct pgrp *pgrp = pgfind(*(int *)data); 49317545Skarels 49439555Smarc if (!isctty(p, tp)) 49539555Smarc return (ENOTTY); 49640030Smarc else if (pgrp == NULL || pgrp->pg_session != p->p_session) 49739555Smarc return (EPERM); 49839555Smarc tp->t_pgrp = pgrp; 49912752Ssam break; 50018650Sbloom } 50112752Ssam 50212752Ssam case TIOCGPGRP: 50339555Smarc if (!isctty(u.u_procp, tp)) 50439555Smarc return (ENOTTY); 50539555Smarc *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; 50612752Ssam break; 50712752Ssam 50817598Sbloom case TIOCSWINSZ: 50918650Sbloom if (bcmp((caddr_t)&tp->t_winsize, data, 51018650Sbloom sizeof (struct winsize))) { 51117598Sbloom tp->t_winsize = *(struct winsize *)data; 51242882Smarc pgsignal(tp->t_pgrp, SIGWINCH, 1); 51317598Sbloom } 51417598Sbloom break; 51517598Sbloom 51617598Sbloom case TIOCGWINSZ: 51717598Sbloom *(struct winsize *)data = tp->t_winsize; 51817598Sbloom break; 51917598Sbloom 52030534Skarels case TIOCCONS: 52130534Skarels if (*(int *)data) { 52242141Smckusick if (constty && constty != tp && 52342141Smckusick (constty->t_state & (TS_CARR_ON|TS_ISOPEN)) == 52442141Smckusick (TS_CARR_ON|TS_ISOPEN)) 52530534Skarels return (EBUSY); 52630534Skarels #ifndef UCONSOLE 52737554Smckusick if (error = suser(u.u_cred, &u.u_acflag)) 52837554Smckusick return (error); 52930534Skarels #endif 53030534Skarels constty = tp; 53130534Skarels } else if (tp == constty) 53233404Skarels constty = NULL; 53330534Skarels break; 53430534Skarels 53535811Smarc #ifdef COMPAT_43 53635811Smarc case TIOCGETP: 53735811Smarc case TIOCSETP: 53835811Smarc case TIOCSETN: 53935811Smarc case TIOCGETC: 54035811Smarc case TIOCSETC: 54135811Smarc case TIOCSLTC: 54235811Smarc case TIOCGLTC: 54335811Smarc case TIOCLBIS: 54435811Smarc case TIOCLBIC: 54535811Smarc case TIOCLSET: 54635811Smarc case TIOCLGET: 54739407Smarc case OTIOCGETD: 54839407Smarc case OTIOCSETD: 549*43377Smarc case OTIOCCONS: 55035811Smarc return(ttcompat(tp, com, data, flag)); 55135811Smarc #endif 55235811Smarc 55339Sbill default: 5548556Sroot return (-1); 55539Sbill } 5568556Sroot return (0); 55739Sbill } 5584484Swnj 5594484Swnj ttnread(tp) 5604484Swnj struct tty *tp; 5614484Swnj { 5624484Swnj int nread = 0; 5634484Swnj 56435811Smarc if (tp->t_lflag & PENDIN) 5654484Swnj ttypend(tp); 5664484Swnj nread = tp->t_canq.c_cc; 56735811Smarc if ((tp->t_lflag & ICANON) == 0) 5684484Swnj nread += tp->t_rawq.c_cc; 5694484Swnj return (nread); 5704484Swnj } 5714484Swnj 5725408Swnj ttselect(dev, rw) 5734484Swnj dev_t dev; 5745408Swnj int rw; 5754484Swnj { 5764484Swnj register struct tty *tp = &cdevsw[major(dev)].d_ttys[minor(dev)]; 5774484Swnj int nread; 57817545Skarels int s = spltty(); 5794484Swnj 5805408Swnj switch (rw) { 5814484Swnj 5824484Swnj case FREAD: 5834484Swnj nread = ttnread(tp); 58437584Smarc if (nread > 0 || 58540712Skarels ((tp->t_cflag&CLOCAL) == 0 && (tp->t_state&TS_CARR_ON) == 0)) 5865408Swnj goto win; 5874938Swnj if (tp->t_rsel && tp->t_rsel->p_wchan == (caddr_t)&selwait) 5885408Swnj tp->t_state |= TS_RCOLL; 5894484Swnj else 5904484Swnj tp->t_rsel = u.u_procp; 5915408Swnj break; 5924484Swnj 5935408Swnj case FWRITE: 59435811Smarc if (tp->t_outq.c_cc <= tp->t_lowat) 5955408Swnj goto win; 5965408Swnj if (tp->t_wsel && tp->t_wsel->p_wchan == (caddr_t)&selwait) 5975408Swnj tp->t_state |= TS_WCOLL; 5985408Swnj else 5995408Swnj tp->t_wsel = u.u_procp; 6005408Swnj break; 6014484Swnj } 6025408Swnj splx(s); 6035408Swnj return (0); 6045408Swnj win: 6055408Swnj splx(s); 6065408Swnj return (1); 6074484Swnj } 6087436Skre 6097502Sroot /* 61025391Skarels * Initial open of tty, or (re)entry to line discipline. 6117502Sroot */ 6127502Sroot ttyopen(dev, tp) 6137625Ssam dev_t dev; 6147625Ssam register struct tty *tp; 6157502Sroot { 6167502Sroot 6177502Sroot tp->t_dev = dev; 61835811Smarc 6197502Sroot tp->t_state &= ~TS_WOPEN; 62017545Skarels if ((tp->t_state & TS_ISOPEN) == 0) { 62117545Skarels tp->t_state |= TS_ISOPEN; 62217598Sbloom bzero((caddr_t)&tp->t_winsize, sizeof(tp->t_winsize)); 62317545Skarels } 6248556Sroot return (0); 6257502Sroot } 6267502Sroot 6277502Sroot /* 62825391Skarels * "close" a line discipline 62925391Skarels */ 63025391Skarels ttylclose(tp) 63125391Skarels register struct tty *tp; 63225391Skarels { 63325391Skarels 63425391Skarels ttywflush(tp); 63525391Skarels } 63625391Skarels 63725391Skarels /* 6387502Sroot * clean tp on last close 6397502Sroot */ 6407502Sroot ttyclose(tp) 6417625Ssam register struct tty *tp; 6427502Sroot { 64330534Skarels if (constty == tp) 64430534Skarels constty = NULL; 64525391Skarels ttyflush(tp, FREAD|FWRITE); 64639555Smarc tp->t_session = NULL; 64739555Smarc tp->t_pgrp = NULL; 6487502Sroot tp->t_state = 0; 649*43377Smarc tp->t_gen++; 65040712Skarels return (0); 6517502Sroot } 6527502Sroot 6537502Sroot /* 65425391Skarels * Handle modem control transition on a tty. 65525391Skarels * Flag indicates new state of carrier. 65625391Skarels * Returns 0 if the line should be turned off, otherwise 1. 65725391Skarels */ 65825391Skarels ttymodem(tp, flag) 65925391Skarels register struct tty *tp; 66025391Skarels { 66125391Skarels 66242193Smarc if ((tp->t_state&TS_WOPEN) == 0 && (tp->t_lflag&MDMBUF)) { 66325391Skarels /* 66425391Skarels * MDMBUF: do flow control according to carrier flag 66525391Skarels */ 66625391Skarels if (flag) { 66725391Skarels tp->t_state &= ~TS_TTSTOP; 66825391Skarels ttstart(tp); 66925391Skarels } else if ((tp->t_state&TS_TTSTOP) == 0) { 67025391Skarels tp->t_state |= TS_TTSTOP; 67125391Skarels (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 67225391Skarels } 67325391Skarels } else if (flag == 0) { 67425391Skarels /* 67525391Skarels * Lost carrier. 67625391Skarels */ 67725391Skarels tp->t_state &= ~TS_CARR_ON; 67842193Smarc if (tp->t_state&TS_ISOPEN && (tp->t_cflag&CLOCAL) == 0) { 67942193Smarc if (tp->t_session && tp->t_session->s_leader) 68042193Smarc psignal(tp->t_session->s_leader, SIGHUP); 68142193Smarc ttyflush(tp, FREAD|FWRITE); 68242193Smarc return (0); 68325391Skarels } 68425391Skarels } else { 68525391Skarels /* 68625391Skarels * Carrier now on. 68725391Skarels */ 68825391Skarels tp->t_state |= TS_CARR_ON; 68937584Smarc ttwakeup(tp); 69025391Skarels } 69125391Skarels return (1); 69225391Skarels } 69325391Skarels 69425391Skarels /* 69525404Skarels * Default modem control routine (for other line disciplines). 69625404Skarels * Return argument flag, to turn off device on carrier drop. 69725404Skarels */ 69825415Skarels nullmodem(tp, flag) 69925415Skarels register struct tty *tp; 70025404Skarels int flag; 70125404Skarels { 70225404Skarels 70325404Skarels if (flag) 70425404Skarels tp->t_state |= TS_CARR_ON; 70539407Smarc else { 70625404Skarels tp->t_state &= ~TS_CARR_ON; 70742193Smarc if ((tp->t_cflag&CLOCAL) == 0) { 70842193Smarc if (tp->t_session && tp->t_session->s_leader) 70942193Smarc psignal(tp->t_session->s_leader, SIGHUP); 71042193Smarc return (0); 71142193Smarc } 71239407Smarc } 71342193Smarc return (1); 71425404Skarels } 71525404Skarels 71625404Skarels /* 7177502Sroot * reinput pending characters after state switch 71817545Skarels * call at spltty(). 7197502Sroot */ 7207502Sroot ttypend(tp) 7217625Ssam register struct tty *tp; 7227502Sroot { 7237502Sroot struct clist tq; 7247502Sroot register c; 7257502Sroot 72635811Smarc tp->t_lflag &= ~PENDIN; 7279578Ssam tp->t_state |= TS_TYPEN; 7287502Sroot tq = tp->t_rawq; 7297502Sroot tp->t_rawq.c_cc = 0; 7307502Sroot tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 7317502Sroot while ((c = getc(&tq)) >= 0) 7327502Sroot ttyinput(c, tp); 7339578Ssam tp->t_state &= ~TS_TYPEN; 7347502Sroot } 7357502Sroot 7367502Sroot /* 73735811Smarc * 7389578Ssam * Place a character on raw TTY input queue, 7399578Ssam * putting in delimiters and waking up top 7409578Ssam * half as needed. Also echo if required. 7419578Ssam * The arguments are the character and the 7429578Ssam * appropriate tty structure. 7437502Sroot */ 7447502Sroot ttyinput(c, tp) 7457625Ssam register c; 7467625Ssam register struct tty *tp; 7477502Sroot { 74835811Smarc register int iflag = tp->t_iflag; 74935811Smarc register int lflag = tp->t_lflag; 75035811Smarc register u_char *cc = tp->t_cc; 75135811Smarc int i, err; 7527502Sroot 7539578Ssam /* 7549578Ssam * If input is pending take it first. 7559578Ssam */ 75635811Smarc if (lflag&PENDIN) 7577502Sroot ttypend(tp); 75835811Smarc /* 75935811Smarc * Gather stats. 76035811Smarc */ 7617502Sroot tk_nin++; 76235811Smarc if (lflag&ICANON) { 76335811Smarc tk_cancc++; 76435811Smarc tp->t_cancc++; 76535811Smarc } else { 76635811Smarc tk_rawcc++; 76735811Smarc tp->t_rawcc++; 76835811Smarc } 7699578Ssam /* 77035811Smarc * Handle exceptional conditions (break, parity, framing). 7719578Ssam */ 77235811Smarc if (err = (c&TTY_ERRORMASK)) { 77335811Smarc c &= ~TTY_ERRORMASK; 77435811Smarc if (err&TTY_FE && !c) { /* break */ 77535811Smarc if (iflag&IGNBRK) 77635811Smarc goto endcase; 77735811Smarc else if (iflag&BRKINT && lflag&ISIG && 77835811Smarc (cc[VINTR] != _POSIX_VDISABLE)) 77935811Smarc c = cc[VINTR]; 78035811Smarc else { 78135811Smarc c = 0; 78235811Smarc if (iflag&PARMRK) 78335811Smarc goto parmrk; 78435811Smarc } 78535811Smarc } else if ((err&TTY_PE && iflag&INPCK) || err&TTY_FE) { 78635811Smarc if (iflag&IGNPAR) 78735811Smarc goto endcase; 78835811Smarc else if (iflag&PARMRK) { 78935811Smarc parmrk: 79035811Smarc putc(0377|TTY_QUOTE, &tp->t_rawq); 79135811Smarc putc(0|TTY_QUOTE, &tp->t_rawq); 79235811Smarc putc(c|TTY_QUOTE, &tp->t_rawq); 79335811Smarc goto endcase; 79435811Smarc } else 79535811Smarc c = 0; 7967502Sroot } 7979578Ssam } 7989578Ssam /* 79935811Smarc * In tandem mode, check high water mark. 8009578Ssam */ 80135811Smarc if (iflag&IXOFF) 80235811Smarc ttyblock(tp); 80335811Smarc if ((tp->t_state&TS_TYPEN) == 0 && (iflag&ISTRIP)) 8049578Ssam c &= 0177; 80542882Smarc if ((tp->t_lflag&EXTPROC) == 0) { 8069578Ssam /* 8079578Ssam * Check for literal nexting very first 8089578Ssam */ 8099578Ssam if (tp->t_state&TS_LNCH) { 81035811Smarc c |= TTY_QUOTE; 8119578Ssam tp->t_state &= ~TS_LNCH; 8129578Ssam } 8139578Ssam /* 8149578Ssam * Scan for special characters. This code 8159578Ssam * is really just a big case statement with 8169578Ssam * non-constant cases. The bottom of the 8179578Ssam * case statement is labeled ``endcase'', so goto 8189578Ssam * it after a case match, or similar. 8199578Ssam */ 82035811Smarc /* 82135811Smarc * Control chars which aren't controlled 82235811Smarc * by ICANON, ISIG, or IXON. 82335811Smarc */ 82439407Smarc if (lflag&IEXTEN) { 82540712Skarels if (CCEQ(cc[VLNEXT], c)) { 82635811Smarc if (lflag&ECHO) { 82735811Smarc if (lflag&ECHOE) 82840712Skarels ttyoutstr("^\b", tp); 82935811Smarc else 83035811Smarc ttyecho(c, tp); 83135811Smarc } 8329578Ssam tp->t_state |= TS_LNCH; 8339578Ssam goto endcase; 8349578Ssam } 83543093Smarc if (CCEQ(cc[VDISCARD], c)) { 83635811Smarc if (lflag&FLUSHO) 83735811Smarc tp->t_lflag &= ~FLUSHO; 8387502Sroot else { 83912752Ssam ttyflush(tp, FWRITE); 8407502Sroot ttyecho(c, tp); 8419578Ssam if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 8427502Sroot ttyretype(tp); 84335811Smarc tp->t_lflag |= FLUSHO; 8447502Sroot } 8459578Ssam goto startoutput; 8469578Ssam } 84735811Smarc } 84835811Smarc /* 84935811Smarc * Signals. 85035811Smarc */ 85135811Smarc if (lflag&ISIG) { 85235811Smarc if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 85335811Smarc if ((lflag&NOFLSH) == 0) 85435811Smarc ttyflush(tp, FREAD|FWRITE); 85535811Smarc ttyecho(c, tp); 85639555Smarc pgsignal(tp->t_pgrp, 85742882Smarc CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 85835811Smarc goto endcase; 85935811Smarc } 86040712Skarels if (CCEQ(cc[VSUSP], c)) { 86135811Smarc if ((lflag&NOFLSH) == 0) 86212752Ssam ttyflush(tp, FREAD); 8639578Ssam ttyecho(c, tp); 86442882Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 8659578Ssam goto endcase; 8669578Ssam } 8679578Ssam } 8689578Ssam /* 8699578Ssam * Handle start/stop characters. 8709578Ssam */ 87135811Smarc if (iflag&IXON) { 87240712Skarels if (CCEQ(cc[VSTOP], c)) { 87335811Smarc if ((tp->t_state&TS_TTSTOP) == 0) { 87435811Smarc tp->t_state |= TS_TTSTOP; 87535811Smarc (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 87635811Smarc return; 87735811Smarc } 87835811Smarc if (!CCEQ(cc[VSTART], c)) 87935811Smarc return; 88035811Smarc /* 88135811Smarc * if VSTART == VSTOP then toggle 88235811Smarc */ 88335811Smarc goto endcase; 8849578Ssam } 88535811Smarc if (CCEQ(cc[VSTART], c)) 88635811Smarc goto restartoutput; 8879578Ssam } 8889578Ssam /* 88935811Smarc * IGNCR, ICRNL, & INLCR 8909578Ssam */ 89135811Smarc if (c == '\r') { 89235811Smarc if (iflag&IGNCR) 89335811Smarc goto endcase; 89435811Smarc else if (iflag&ICRNL) 89535811Smarc c = '\n'; 8969578Ssam } 89735811Smarc else if (c == '\n' && iflag&INLCR) 89835811Smarc c = '\r'; 89942882Smarc } 9009578Ssam /* 90135811Smarc * Non canonical mode; don't process line editing 9029578Ssam * characters; check high water mark for wakeup. 90335811Smarc * 9049578Ssam */ 90540712Skarels if ((lflag&ICANON) == 0) { 9069578Ssam if (tp->t_rawq.c_cc > TTYHOG) { 90735811Smarc if (iflag&IMAXBEL) { 90835811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 90935811Smarc (void) ttyoutput(CTRL('g'), tp); 91035811Smarc } else 91135811Smarc ttyflush(tp, FREAD | FWRITE); 91235811Smarc } else { 91335811Smarc if (putc(c, &tp->t_rawq) >= 0) { 91435811Smarc ttwakeup(tp); 91535811Smarc ttyecho(c, tp); 91635811Smarc } 9177502Sroot } 9189578Ssam goto endcase; 9199578Ssam } 92042882Smarc if ((tp->t_lflag&EXTPROC) == 0) { 9219578Ssam /* 92235811Smarc * From here on down canonical mode character 9239578Ssam * processing takes place. 9249578Ssam */ 92535811Smarc /* 92635811Smarc * erase (^H / ^?) 92735811Smarc */ 928*43377Smarc if ((c == cc[VERASE] || c == cc[VERASE2]) && c != _POSIX_VDISABLE && 929*43377Smarc cc[VERASE] != _POSIX_VDISABLE) { 9309578Ssam if (tp->t_rawq.c_cc) 9319578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9329578Ssam goto endcase; 9339578Ssam } 93435811Smarc /* 93535811Smarc * kill (^U) 93635811Smarc */ 93735811Smarc if (CCEQ(cc[VKILL], c)) { 93837584Smarc if (lflag&ECHOKE && tp->t_rawq.c_cc == tp->t_rocount && 93940712Skarels (lflag&ECHOPRT) == 0) { 9409578Ssam while (tp->t_rawq.c_cc) 9419578Ssam ttyrub(unputc(&tp->t_rawq), tp); 9429578Ssam } else { 9439578Ssam ttyecho(c, tp); 94435811Smarc if (lflag&ECHOK || lflag&ECHOKE) 94535811Smarc ttyecho('\n', tp); 9469578Ssam while (getc(&tp->t_rawq) > 0) 9479578Ssam ; 9489578Ssam tp->t_rocount = 0; 9499578Ssam } 9509578Ssam tp->t_state &= ~TS_LOCAL; 9519578Ssam goto endcase; 9529578Ssam } 9539578Ssam /* 95435811Smarc * word erase (^W) 9559578Ssam */ 95635811Smarc if (CCEQ(cc[VWERASE], c)) { 95735811Smarc int ctype; 95835811Smarc 95935811Smarc #define CTYPE(c) ((lflag&ALTWERASE) ? (partab[(c)&TTY_CHARMASK]&0100) : 0) 96035811Smarc /* 96135811Smarc * erase whitespace 96235811Smarc */ 96335811Smarc while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 96435811Smarc ttyrub(c, tp); 96535811Smarc if (c == -1) 96634492Skarels goto endcase; 96735811Smarc /* 96835811Smarc * special case last char of token 96935811Smarc */ 97035811Smarc ttyrub(c, tp); 97135811Smarc c = unputc(&tp->t_rawq); 97235811Smarc if (c == -1 || c == ' ' || c == '\t') { 97335811Smarc if (c != -1) 97435811Smarc (void) putc(c, &tp->t_rawq); 97534492Skarels goto endcase; 97634492Skarels } 97735811Smarc /* 97835811Smarc * erase rest of token 97935811Smarc */ 98035811Smarc ctype = CTYPE(c); 98135811Smarc do { 98235811Smarc ttyrub(c, tp); 98335811Smarc c = unputc(&tp->t_rawq); 98435811Smarc if (c == -1) 98535811Smarc goto endcase; 98635811Smarc } while (c != ' ' && c != '\t' && CTYPE(c) == ctype); 98735811Smarc (void) putc(c, &tp->t_rawq); 98835811Smarc goto endcase; 98935811Smarc #undef CTYPE 9909578Ssam } 9919578Ssam /* 99235811Smarc * reprint line (^R) 99335811Smarc */ 99435811Smarc if (CCEQ(cc[VREPRINT], c)) { 99535811Smarc ttyretype(tp); 99635811Smarc goto endcase; 99735811Smarc } 998*43377Smarc /* 999*43377Smarc * ^T - kernel info and generate SIGINFO 1000*43377Smarc */ 100143322Smarc if (CCEQ(cc[VSTATUS], c)) { 100242882Smarc pgsignal(tp->t_pgrp, SIGINFO, 1); 100342141Smckusick if ((lflag&NOKERNINFO) == 0) 100442141Smckusick ttyinfo(tp); 100542141Smckusick goto endcase; 100642141Smckusick } 100742882Smarc } 100835811Smarc /* 10099578Ssam * Check for input buffer overflow 10109578Ssam */ 101110391Ssam if (tp->t_rawq.c_cc+tp->t_canq.c_cc >= TTYHOG) { 101235811Smarc if (iflag&IMAXBEL) { 101335811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 101435811Smarc (void) ttyoutput(CTRL('g'), tp); 101535811Smarc } else 101635811Smarc ttyflush(tp, FREAD | FWRITE); 10179578Ssam goto endcase; 101810391Ssam } 10199578Ssam /* 10209578Ssam * Put data char in q for user and 10219578Ssam * wakeup on seeing a line delimiter. 10229578Ssam */ 10239578Ssam if (putc(c, &tp->t_rawq) >= 0) { 102435811Smarc if (ttbreakc(c)) { 10259578Ssam tp->t_rocount = 0; 10269578Ssam catq(&tp->t_rawq, &tp->t_canq); 10277502Sroot ttwakeup(tp); 10289578Ssam } else if (tp->t_rocount++ == 0) 10299578Ssam tp->t_rocol = tp->t_col; 10309578Ssam if (tp->t_state&TS_ERASE) { 103135811Smarc /* 103235811Smarc * end of prterase \.../ 103335811Smarc */ 10349578Ssam tp->t_state &= ~TS_ERASE; 10359578Ssam (void) ttyoutput('/', tp); 10369578Ssam } 10379578Ssam i = tp->t_col; 10387502Sroot ttyecho(c, tp); 103935811Smarc if (CCEQ(cc[VEOF], c) && lflag&ECHO) { 104035811Smarc /* 104135811Smarc * Place the cursor over the '^' of the ^D. 104235811Smarc */ 10439578Ssam i = MIN(2, tp->t_col - i); 10449578Ssam while (i > 0) { 10459578Ssam (void) ttyoutput('\b', tp); 10469578Ssam i--; 10479578Ssam } 10489578Ssam } 10497502Sroot } 10509578Ssam endcase: 10519578Ssam /* 105235811Smarc * IXANY means allow any character to restart output. 10539578Ssam */ 105440712Skarels if ((tp->t_state&TS_TTSTOP) && (iflag&IXANY) == 0 && 105540712Skarels cc[VSTART] != cc[VSTOP]) 10567502Sroot return; 10579578Ssam restartoutput: 10587502Sroot tp->t_state &= ~TS_TTSTOP; 105935811Smarc tp->t_lflag &= ~FLUSHO; 10609578Ssam startoutput: 10617502Sroot ttstart(tp); 10627502Sroot } 10637502Sroot 10647502Sroot /* 10659578Ssam * Put character on TTY output queue, adding delays, 10667502Sroot * expanding tabs, and handling the CR/NL bit. 10679578Ssam * This is called both from the top half for output, 10689578Ssam * and from interrupt level for echoing. 10697502Sroot * The arguments are the character and the tty structure. 10707502Sroot * Returns < 0 if putc succeeds, otherwise returns char to resend 10717502Sroot * Must be recursive. 10727502Sroot */ 10737502Sroot ttyoutput(c, tp) 10747502Sroot register c; 10757502Sroot register struct tty *tp; 10767502Sroot { 10777502Sroot register char *colp; 10787502Sroot register ctype; 107935811Smarc register long oflag = tp->t_oflag; 108035811Smarc 108140712Skarels if ((oflag&OPOST) == 0) { 108235811Smarc if (tp->t_lflag&FLUSHO) 10837502Sroot return (-1); 10847502Sroot if (putc(c, &tp->t_outq)) 10857625Ssam return (c); 10867502Sroot tk_nout++; 108735811Smarc tp->t_outcc++; 10887502Sroot return (-1); 10897502Sroot } 109035811Smarc c &= TTY_CHARMASK; 10917502Sroot /* 10927502Sroot * Turn tabs to spaces as required 109342882Smarc * 109442882Smarc * Special case if we have external processing, we don't 109542882Smarc * do the tab expansion because we'll probably get it 109642882Smarc * wrong. If tab expansion needs to be done, let it 109742882Smarc * happen externally. 10987502Sroot */ 109942882Smarc if ((tp->t_lflag&EXTPROC) == 0 && 110042882Smarc c == '\t' && oflag&OXTABS ) { 11017502Sroot register int s; 11027502Sroot 11037502Sroot c = 8 - (tp->t_col&7); 110435811Smarc if ((tp->t_lflag&FLUSHO) == 0) { 110517545Skarels s = spltty(); /* don't interrupt tabs */ 11067502Sroot c -= b_to_q(" ", c, &tp->t_outq); 11077502Sroot tk_nout += c; 110835811Smarc tp->t_outcc += c; 11097502Sroot splx(s); 11107502Sroot } 11117502Sroot tp->t_col += c; 11127502Sroot return (c ? -1 : '\t'); 11137502Sroot } 111435811Smarc if (c == CEOT && oflag&ONOEOT) 111535811Smarc return(-1); 11167502Sroot tk_nout++; 111735811Smarc tp->t_outcc++; 11187502Sroot /* 11197502Sroot * turn <nl> to <cr><lf> if desired. 11207502Sroot */ 112135811Smarc if (c == '\n' && (tp->t_oflag&ONLCR) && ttyoutput('\r', tp) >= 0) 11227502Sroot return (c); 112335811Smarc if ((tp->t_lflag&FLUSHO) == 0 && putc(c, &tp->t_outq)) 112435811Smarc return (c); 11257502Sroot /* 11267502Sroot * Calculate delays. 11277502Sroot * The numbers here represent clock ticks 11287502Sroot * and are not necessarily optimal for all terminals. 11299578Ssam * 11309578Ssam * SHOULD JUST ALLOW USER TO SPECIFY DELAYS 113135811Smarc * 113235811Smarc * (actually, should THROW AWAY terminals which need delays) 11337502Sroot */ 11347502Sroot colp = &tp->t_col; 11357502Sroot ctype = partab[c]; 11367502Sroot c = 0; 11377502Sroot switch (ctype&077) { 11387502Sroot 11397502Sroot case ORDINARY: 11407502Sroot (*colp)++; 11417502Sroot 11427502Sroot case CONTROL: 11437502Sroot break; 11447502Sroot 11457502Sroot case BACKSPACE: 11467502Sroot if (*colp) 11477502Sroot (*colp)--; 11487502Sroot break; 11497502Sroot 115013821Ssam /* 115113821Ssam * This macro is close enough to the correct thing; 115213821Ssam * it should be replaced by real user settable delays 115313821Ssam * in any event... 115413821Ssam */ 115513821Ssam #define mstohz(ms) (((ms) * hz) >> 10) 11567502Sroot case NEWLINE: 11577502Sroot ctype = (tp->t_flags >> 8) & 03; 11587625Ssam if (ctype == 1) { /* tty 37 */ 115926357Skarels if (*colp > 0) { 116026357Skarels c = (((unsigned)*colp) >> 4) + 3; 116126357Skarels if ((unsigned)c > 6) 116226357Skarels c = 6; 116326357Skarels } 11649578Ssam } else if (ctype == 2) /* vt05 */ 116513821Ssam c = mstohz(100); 11667502Sroot *colp = 0; 11677502Sroot break; 11687502Sroot 11697502Sroot case TAB: 11707502Sroot ctype = (tp->t_flags >> 10) & 03; 11717625Ssam if (ctype == 1) { /* tty 37 */ 11727502Sroot c = 1 - (*colp | ~07); 11737625Ssam if (c < 5) 11747502Sroot c = 0; 11757502Sroot } 11767502Sroot *colp |= 07; 11777502Sroot (*colp)++; 11787502Sroot break; 11797502Sroot 11807502Sroot case VTAB: 11819578Ssam if (tp->t_flags&VTDELAY) /* tty 37 */ 11827502Sroot c = 0177; 11837502Sroot break; 11847502Sroot 11857502Sroot case RETURN: 11867502Sroot ctype = (tp->t_flags >> 12) & 03; 11879578Ssam if (ctype == 1) /* tn 300 */ 118813821Ssam c = mstohz(83); 11899578Ssam else if (ctype == 2) /* ti 700 */ 119013821Ssam c = mstohz(166); 11919578Ssam else if (ctype == 3) { /* concept 100 */ 11927502Sroot int i; 11939578Ssam 11947502Sroot if ((i = *colp) >= 0) 11959578Ssam for (; i < 9; i++) 11967502Sroot (void) putc(0177, &tp->t_outq); 11977502Sroot } 11987502Sroot *colp = 0; 11997502Sroot } 120035811Smarc if (c && (tp->t_lflag&FLUSHO) == 0) 120135811Smarc (void) putc(c|TTY_QUOTE, &tp->t_outq); 12027502Sroot return (-1); 12037502Sroot } 120413821Ssam #undef mstohz 12057502Sroot 12067502Sroot /* 12077502Sroot * Called from device's read routine after it has 12087502Sroot * calculated the tty-structure given as argument. 12097502Sroot */ 121037584Smarc ttread(tp, uio, flag) 12117625Ssam register struct tty *tp; 12127722Swnj struct uio *uio; 12137502Sroot { 12147502Sroot register struct clist *qp; 121535811Smarc register int c; 121641383Smarc register long lflag; 121735811Smarc register u_char *cc = tp->t_cc; 12189859Ssam int s, first, error = 0; 12197502Sroot 12207502Sroot loop: 122141383Smarc lflag = tp->t_lflag; 122237584Smarc s = spltty(); 12239578Ssam /* 122437584Smarc * take pending input first 12259578Ssam */ 122635811Smarc if (lflag&PENDIN) 12277502Sroot ttypend(tp); 12289859Ssam splx(s); 122940712Skarels 12309578Ssam /* 12319578Ssam * Hang process if it's in the background. 12329578Ssam */ 123339555Smarc if (isbackground(u.u_procp, tp)) { 123424392Skarels if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) || 123524392Skarels (u.u_procp->p_sigmask & sigmask(SIGTTIN)) || 123635811Smarc u.u_procp->p_flag&SVFORK || u.u_procp->p_pgrp->pg_jobc == 0) 12378520Sroot return (EIO); 123842882Smarc pgsignal(u.u_procp->p_pgrp, SIGTTIN, 1); 1239*43377Smarc if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 1240*43377Smarc ttybg, 0)) 124140712Skarels return (error); 124223165Sbloom goto loop; 12437502Sroot } 124440712Skarels 12459578Ssam /* 124635811Smarc * If canonical, use the canonical queue, 124735811Smarc * else use the raw queue. 124837584Smarc * 124937584Smarc * XXX - should get rid of canonical queue. 125037584Smarc * (actually, should get rid of clists...) 12519578Ssam */ 125235811Smarc qp = lflag&ICANON ? &tp->t_canq : &tp->t_rawq; 125340712Skarels 12549578Ssam /* 125540712Skarels * If there is no input, sleep on rawq 125640712Skarels * awaiting hardware receipt and notification. 125740712Skarels * If we have data, we don't need to check for carrier. 12589578Ssam */ 125917545Skarels s = spltty(); 12609578Ssam if (qp->c_cc <= 0) { 126140712Skarels int carrier; 126240712Skarels 126340712Skarels carrier = (tp->t_state&TS_CARR_ON) || (tp->t_cflag&CLOCAL); 126440712Skarels if (!carrier && tp->t_state&TS_ISOPEN) { 12659859Ssam splx(s); 126640712Skarels return (0); /* EOF */ 12677502Sroot } 126837728Smckusick if (flag & IO_NDELAY) { 126937584Smarc splx(s); 127037584Smarc return (EWOULDBLOCK); 127137584Smarc } 1272*43377Smarc error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 127340712Skarels carrier ? ttyin : ttopen, 0); 12749859Ssam splx(s); 1275*43377Smarc if (error) 127640712Skarels return (error); 12779578Ssam goto loop; 12789578Ssam } 12799859Ssam splx(s); 128040712Skarels 12819578Ssam /* 128235811Smarc * Input present, check for input mapping and processing. 12839578Ssam */ 12849578Ssam first = 1; 12859578Ssam while ((c = getc(qp)) >= 0) { 12869578Ssam /* 128735811Smarc * delayed suspend (^Y) 12889578Ssam */ 128935811Smarc if (CCEQ(cc[VDSUSP], c) && lflag&ISIG) { 129042882Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 12919578Ssam if (first) { 1292*43377Smarc if (error = ttysleep(tp, (caddr_t)&lbolt, 1293*43377Smarc TTIPRI | PCATCH, ttybg, 0)) 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 */ 1396*43377Smarc error = ttysleep(tp, (caddr_t)&tp->t_rawq, 1397*43377Smarc TTIPRI | PCATCH,ttopen, 0); 139837584Smarc splx(s); 1399*43377Smarc if (error) 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); 1414*43377Smarc if (error = ttysleep(tp, (caddr_t)&lbolt, TTIPRI | PCATCH, 1415*43377Smarc ttybg, 0)) 141640712Skarels goto out; 141721776Sbloom goto loop; 14187502Sroot } 14199578Ssam /* 14209578Ssam * Process the user's data in at most OBUFSIZ 142140712Skarels * chunks. Perform any output translation. 142240712Skarels * Keep track of high water mark, sleep on overflow 142340712Skarels * awaiting device aid in acquiring new space. 14249578Ssam */ 142540712Skarels while (uio->uio_resid > 0 || cc > 0) { 142640712Skarels if (tp->t_lflag&FLUSHO) { 142740712Skarels uio->uio_resid = 0; 142840712Skarels return (0); 142940712Skarels } 143040712Skarels if (tp->t_outq.c_cc > hiwat) 143132067Skarels goto ovhiwat; 14329578Ssam /* 143340712Skarels * Grab a hunk of data from the user, 143440712Skarels * unless we have some leftover from last time. 14359578Ssam */ 14367822Sroot if (cc == 0) { 143740712Skarels cc = min(uio->uio_resid, OBUFSIZ); 143840712Skarels cp = obuf; 143940712Skarels error = uiomove(cp, cc, uio); 144040712Skarels if (error) { 144140712Skarels cc = 0; 144240712Skarels break; 144340712Skarels } 14447822Sroot } 14459578Ssam /* 14469578Ssam * If nothing fancy need be done, grab those characters we 14479578Ssam * can handle without any of ttyoutput's processing and 14489578Ssam * just transfer them to the output q. For those chars 14499578Ssam * which require special processing (as indicated by the 14509578Ssam * bits in partab), call ttyoutput. After processing 14519578Ssam * a hunk of data, look for FLUSHO so ^O's will take effect 14529578Ssam * immediately. 14539578Ssam */ 14549578Ssam while (cc > 0) { 145540712Skarels if ((tp->t_oflag&OPOST) == 0) 14567502Sroot ce = cc; 14577502Sroot else { 145834492Skarels ce = cc - scanc((unsigned)cc, (u_char *)cp, 145934492Skarels (u_char *)partab, 077); 14609578Ssam /* 14619578Ssam * If ce is zero, then we're processing 14629578Ssam * a special character through ttyoutput. 14639578Ssam */ 14649578Ssam if (ce == 0) { 14657502Sroot tp->t_rocount = 0; 14667502Sroot if (ttyoutput(*cp, tp) >= 0) { 146721776Sbloom /* no c-lists, wait a bit */ 146821776Sbloom ttstart(tp); 1469*43377Smarc if (error = ttysleep(tp, 1470*43377Smarc (caddr_t)&lbolt, 1471*43377Smarc TTOPRI | PCATCH, ttybuf, 0)) 147240712Skarels break; 147321776Sbloom goto loop; 14747502Sroot } 14759578Ssam cp++, cc--; 147635811Smarc if ((tp->t_lflag&FLUSHO) || 14779578Ssam tp->t_outq.c_cc > hiwat) 14787502Sroot goto ovhiwat; 14799578Ssam continue; 14807502Sroot } 14817502Sroot } 14829578Ssam /* 14839578Ssam * A bunch of normal characters have been found, 14849578Ssam * transfer them en masse to the output queue and 14859578Ssam * continue processing at the top of the loop. 14869578Ssam * If there are any further characters in this 14879578Ssam * <= OBUFSIZ chunk, the first should be a character 14889578Ssam * requiring special handling by ttyoutput. 14899578Ssam */ 14907502Sroot tp->t_rocount = 0; 14919578Ssam i = b_to_q(cp, ce, &tp->t_outq); 14929578Ssam ce -= i; 14939578Ssam tp->t_col += ce; 14949578Ssam cp += ce, cc -= ce, tk_nout += ce; 149535811Smarc tp->t_outcc += ce; 14969578Ssam if (i > 0) { 14979578Ssam /* out of c-lists, wait a bit */ 14987502Sroot ttstart(tp); 1499*43377Smarc if (error = ttysleep(tp, (caddr_t)&lbolt, 1500*43377Smarc TTOPRI | PCATCH, ttybuf, 0)) 150140712Skarels break; 150221776Sbloom goto loop; 15037502Sroot } 150435811Smarc if (tp->t_lflag&FLUSHO || tp->t_outq.c_cc > hiwat) 150540712Skarels break; 15067502Sroot } 150735811Smarc ttstart(tp); 15087502Sroot } 150940712Skarels out: 151040712Skarels /* 151140712Skarels * If cc is nonzero, we leave the uio structure inconsistent, 151240712Skarels * as the offset and iov pointers have moved forward, 151340712Skarels * but it doesn't matter (the call will either return short 151440712Skarels * or restart with a new uio). 151540712Skarels */ 151640712Skarels uio->uio_resid += cc; 15178520Sroot return (error); 151840712Skarels 15197502Sroot ovhiwat: 152032067Skarels ttstart(tp); 152132067Skarels s = spltty(); 15229578Ssam /* 152335811Smarc * This can only occur if FLUSHO is set in t_lflag, 152432067Skarels * or if ttstart/oproc is synchronous (or very fast). 15259578Ssam */ 15267502Sroot if (tp->t_outq.c_cc <= hiwat) { 15279578Ssam splx(s); 15287502Sroot goto loop; 15297502Sroot } 153037728Smckusick if (flag & IO_NDELAY) { 153117545Skarels splx(s); 153240712Skarels uio->uio_resid += cc; 15337822Sroot if (uio->uio_resid == cnt) 15348520Sroot return (EWOULDBLOCK); 15358520Sroot return (0); 15367502Sroot } 15377502Sroot tp->t_state |= TS_ASLEEP; 1538*43377Smarc error = ttysleep(tp, (caddr_t)&tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 15399578Ssam splx(s); 1540*43377Smarc if (error) 154140712Skarels goto out; 15427502Sroot goto loop; 15437502Sroot } 15447502Sroot 15457502Sroot /* 15467502Sroot * Rubout one character from the rawq of tp 15477502Sroot * as cleanly as possible. 15487502Sroot */ 15497502Sroot ttyrub(c, tp) 15507625Ssam register c; 15517625Ssam register struct tty *tp; 15527502Sroot { 15537502Sroot register char *cp; 15547502Sroot register int savecol; 15557502Sroot int s; 15567502Sroot char *nextc(); 15577502Sroot 155842882Smarc if ((tp->t_lflag&ECHO) == 0 || (tp->t_lflag&EXTPROC)) 15597502Sroot return; 156035811Smarc tp->t_lflag &= ~FLUSHO; 156135811Smarc if (tp->t_lflag&ECHOE) { 15627502Sroot if (tp->t_rocount == 0) { 15637502Sroot /* 15647502Sroot * Screwed by ttwrite; retype 15657502Sroot */ 15667502Sroot ttyretype(tp); 15677502Sroot return; 15687502Sroot } 156935811Smarc if (c == ('\t'|TTY_QUOTE) || c == ('\n'|TTY_QUOTE)) 15707502Sroot ttyrubo(tp, 2); 157135811Smarc else switch (partab[c&=0377]&077) { 15727502Sroot 15737502Sroot case ORDINARY: 157435811Smarc ttyrubo(tp, 1); 15757502Sroot break; 15767502Sroot 15777502Sroot case VTAB: 15787502Sroot case BACKSPACE: 15797502Sroot case CONTROL: 15807502Sroot case RETURN: 158135811Smarc if (tp->t_lflag&ECHOCTL) 15827502Sroot ttyrubo(tp, 2); 15837502Sroot break; 15847502Sroot 158535811Smarc case TAB: { 158635811Smarc int c; 158735811Smarc 15887502Sroot if (tp->t_rocount < tp->t_rawq.c_cc) { 15897502Sroot ttyretype(tp); 15907502Sroot return; 15917502Sroot } 159217545Skarels s = spltty(); 15937502Sroot savecol = tp->t_col; 15949578Ssam tp->t_state |= TS_CNTTB; 159535811Smarc tp->t_lflag |= FLUSHO; 15967502Sroot tp->t_col = tp->t_rocol; 15979578Ssam cp = tp->t_rawq.c_cf; 159839407Smarc if (cp) 159939407Smarc c = *cp; /* XXX FIX NEXTC */ 160035811Smarc for (; cp; cp = nextc(&tp->t_rawq, cp, &c)) 160135811Smarc ttyecho(c, tp); 160235811Smarc tp->t_lflag &= ~FLUSHO; 16039578Ssam tp->t_state &= ~TS_CNTTB; 16047502Sroot splx(s); 16057502Sroot /* 16067502Sroot * savecol will now be length of the tab 16077502Sroot */ 16087502Sroot savecol -= tp->t_col; 16097502Sroot tp->t_col += savecol; 16107502Sroot if (savecol > 8) 16117502Sroot savecol = 8; /* overflow screw */ 16127502Sroot while (--savecol >= 0) 16137502Sroot (void) ttyoutput('\b', tp); 16147502Sroot break; 161535811Smarc } 16167502Sroot 16177502Sroot default: 161837584Smarc /* XXX */ 161935811Smarc printf("ttyrub: would panic c = %d, val = %d\n", 162035811Smarc c, partab[c&=0377]&077); 162135811Smarc /*panic("ttyrub");*/ 16227502Sroot } 162335811Smarc } else if (tp->t_lflag&ECHOPRT) { 16249578Ssam if ((tp->t_state&TS_ERASE) == 0) { 16257502Sroot (void) ttyoutput('\\', tp); 16269578Ssam tp->t_state |= TS_ERASE; 16277502Sroot } 16287502Sroot ttyecho(c, tp); 16297502Sroot } else 163035811Smarc ttyecho(tp->t_cc[VERASE], tp); 16317502Sroot tp->t_rocount--; 16327502Sroot } 16337502Sroot 16347502Sroot /* 16357502Sroot * Crt back over cnt chars perhaps 16367502Sroot * erasing them. 16377502Sroot */ 16387502Sroot ttyrubo(tp, cnt) 16397625Ssam register struct tty *tp; 16407625Ssam int cnt; 16417502Sroot { 16427502Sroot 16437502Sroot while (--cnt >= 0) 164440712Skarels ttyoutstr("\b \b", tp); 16457502Sroot } 16467502Sroot 16477502Sroot /* 16487502Sroot * Reprint the rawq line. 16497502Sroot * We assume c_cc has already been checked. 16507502Sroot */ 16517502Sroot ttyretype(tp) 16527625Ssam register struct tty *tp; 16537502Sroot { 16547502Sroot register char *cp; 16557502Sroot char *nextc(); 165635811Smarc int s, c; 16577502Sroot 165835811Smarc if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 165935811Smarc ttyecho(tp->t_cc[VREPRINT], tp); 16607502Sroot (void) ttyoutput('\n', tp); 166117545Skarels s = spltty(); 166235811Smarc /*** XXX *** FIX *** NEXTC IS BROKEN - DOESN'T CHECK QUOTE 166335811Smarc BIT OF FIRST CHAR ****/ 166435811Smarc for (cp = tp->t_canq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_canq, cp, &c)) { 166535811Smarc ttyecho(c, tp); 166635811Smarc } 166735811Smarc for (cp = tp->t_rawq.c_cf, c=(cp?*cp:0); cp; cp = nextc(&tp->t_rawq, cp, &c)) { 166835811Smarc ttyecho(c, tp); 166935811Smarc } 16709578Ssam tp->t_state &= ~TS_ERASE; 16717502Sroot splx(s); 16727502Sroot tp->t_rocount = tp->t_rawq.c_cc; 16737502Sroot tp->t_rocol = 0; 16747502Sroot } 16757502Sroot 16767502Sroot /* 167735811Smarc * Echo a typed character to the terminal. 16787502Sroot */ 16797502Sroot ttyecho(c, tp) 16807625Ssam register c; 16817625Ssam register struct tty *tp; 16827502Sroot { 16839578Ssam if ((tp->t_state&TS_CNTTB) == 0) 168435811Smarc tp->t_lflag &= ~FLUSHO; 168542882Smarc if (((tp->t_lflag&ECHO) == 0 && ((tp->t_lflag&ECHONL) == 0 || 168642882Smarc c == '\n')) || (tp->t_lflag&EXTPROC)) 16877502Sroot return; 168835811Smarc if (tp->t_lflag&ECHOCTL) { 168940712Skarels if ((c&TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' || 169040712Skarels c == 0177) { 16917502Sroot (void) ttyoutput('^', tp); 169235811Smarc c &= TTY_CHARMASK; 16937502Sroot if (c == 0177) 16947502Sroot c = '?'; 16957502Sroot else 16967502Sroot c += 'A' - 1; 16977502Sroot } 16987502Sroot } 169935811Smarc (void) ttyoutput(c, tp); 17007502Sroot } 17017502Sroot 17027502Sroot /* 17037502Sroot * send string cp to tp 17047502Sroot */ 170540712Skarels ttyoutstr(cp, tp) 17067625Ssam register char *cp; 17077625Ssam register struct tty *tp; 17087502Sroot { 17097502Sroot register char c; 17107502Sroot 17117502Sroot while (c = *cp++) 17127502Sroot (void) ttyoutput(c, tp); 17137502Sroot } 17147502Sroot 17157502Sroot ttwakeup(tp) 17167502Sroot struct tty *tp; 17177502Sroot { 17187502Sroot 17197502Sroot if (tp->t_rsel) { 17207502Sroot selwakeup(tp->t_rsel, tp->t_state&TS_RCOLL); 17217502Sroot tp->t_state &= ~TS_RCOLL; 17227502Sroot tp->t_rsel = 0; 17237502Sroot } 172412752Ssam if (tp->t_state & TS_ASYNC) 172542882Smarc pgsignal(tp->t_pgrp, SIGIO, 1); 17267502Sroot wakeup((caddr_t)&tp->t_rawq); 17277502Sroot } 172835811Smarc 172935811Smarc /* 173035811Smarc * set tty hi and low water marks 173135811Smarc * 173235811Smarc * Try to arrange the dynamics so there's about one second 173335811Smarc * from hi to low water. 173435811Smarc * 173535811Smarc */ 173635811Smarc ttsetwater(tp) 173735811Smarc struct tty *tp; 173835811Smarc { 173935811Smarc register cps = tp->t_ospeed / 10; 174035811Smarc register x; 174135811Smarc 174235811Smarc #define clamp(x, h, l) ((x)>h ? h : ((x)<l) ? l : (x)) 174335811Smarc tp->t_lowat = x = clamp(cps/2, TTMAXLOWAT, TTMINLOWAT); 174435811Smarc x += cps; 174535811Smarc x = clamp(x, TTMAXHIWAT, TTMINHIWAT); 174635811Smarc tp->t_hiwat = roundup(x, CBSIZE); 174735811Smarc #undef clamp 174835811Smarc } 174935811Smarc 175035811Smarc ttspeedtab(speed, table) 175135811Smarc struct speedtab table[]; 175235811Smarc { 175335811Smarc register int i; 175435811Smarc 175535811Smarc for (i = 0; table[i].sp_speed != -1; i++) 175635811Smarc if (table[i].sp_speed == speed) 175735811Smarc return(table[i].sp_code); 175835811Smarc return(-1); 175935811Smarc } 176039407Smarc 176141177Smarc int ttyhostname = 0; 176239407Smarc /* 176339407Smarc * (^T) 176439407Smarc * Report on state of foreground process group. 176539407Smarc */ 176639407Smarc ttyinfo(tp) 176739407Smarc struct tty *tp; 176839407Smarc { 176941177Smarc register struct proc *p, *pick = NULL; 177041177Smarc register char *cp = hostname; 177141177Smarc int x, s; 177241177Smarc struct timeval utime, stime; 177342350Smckusick #define pgtok(a) (((a)*NBPG)/1024) 177439407Smarc 177539407Smarc if (ttycheckoutq(tp,0) == 0) 177639407Smarc return; 177741177Smarc /* 177841177Smarc * hostname 177941177Smarc */ 178041177Smarc if (ttyhostname) { 178141177Smarc if (*cp == '\0') 178241177Smarc ttyprintf(tp, "amnesia"); 178341177Smarc else 178441177Smarc while (*cp && *cp != '.') 178541177Smarc tputchar(*cp++, tp); 178641177Smarc tputchar(' '); 178741177Smarc } 178841177Smarc /* 178941177Smarc * load average 179041177Smarc */ 179141177Smarc x = (averunnable[0] * 100 + FSCALE/2) >> FSHIFT; 179241177Smarc ttyprintf(tp, "load: %d.", x/100); 179341177Smarc ttyoutint(x%100, 10, 2, tp); 179439555Smarc if (tp->t_session == NULL) 179541177Smarc ttyprintf(tp, " not a controlling terminal\n"); 179641177Smarc else if (tp->t_pgrp == NULL) 179741177Smarc ttyprintf(tp, " no foreground process group\n"); 179841177Smarc else if ((p = tp->t_pgrp->pg_mem) == NULL) 179941177Smarc ttyprintf(tp, " empty foreground process group\n"); 180039407Smarc else { 180141177Smarc /* pick interesting process */ 180239407Smarc for (; p != NULL; p = p->p_pgrpnxt) { 180341177Smarc if (proc_compare(pick, p)) 180441177Smarc pick = p; 180539407Smarc } 180641177Smarc ttyprintf(tp, " cmd: %s %d [%s] ", 180741177Smarc pick->p_comm, pick->p_pid, 180841177Smarc pick->p_wmesg ? pick->p_wmesg : "running"); 180941177Smarc /* 181041177Smarc * cpu time 181141177Smarc */ 181241177Smarc if (u.u_procp == pick) 181341177Smarc s = splclock(); 181441177Smarc utime = pick->p_utime; 181541177Smarc stime = pick->p_stime; 181641177Smarc if (u.u_procp == pick) 181741177Smarc splx(s); 181841177Smarc /* user time */ 181941177Smarc x = (utime.tv_usec + 5000) / 10000; /* scale to 100's */ 182041177Smarc ttyoutint(utime.tv_sec, 10, 1, tp); 182141177Smarc tputchar('.', tp); 182241177Smarc ttyoutint(x, 10, 2, tp); 182341177Smarc tputchar('u', tp); 182441177Smarc tputchar(' ', tp); 182541177Smarc /* system time */ 182641177Smarc x = (stime.tv_usec + 5000) / 10000; /* scale to 100's */ 182741177Smarc ttyoutint(stime.tv_sec, 10, 1, tp); 182841177Smarc tputchar('.', tp); 182941177Smarc ttyoutint(x, 10, 2, tp); 183041177Smarc tputchar('s', tp); 183141177Smarc tputchar(' ', tp); 183241177Smarc /* 183341177Smarc * pctcpu 183441177Smarc */ 183541177Smarc x = pick->p_pctcpu * 10000 + FSCALE/2 >> FSHIFT; 183641177Smarc ttyoutint(x/100, 10, 1, tp); 183741177Smarc #ifdef notdef /* do we really want this ??? */ 183841177Smarc tputchar('.', tp); 183941177Smarc ttyoutint(x%100, 10, 2, tp); 184041177Smarc #endif 184141177Smarc ttyprintf(tp, "%% %dk\n", pgtok(pick->p_ssize + pick->p_dsize)); 184239407Smarc } 184341177Smarc tp->t_rocount = 0; /* so pending input will be retyped if BS */ 184439407Smarc } 184539407Smarc 184641177Smarc ttyoutint(n, base, min, tp) 184741177Smarc register int n, base, min; 184841177Smarc register struct tty *tp; 184941177Smarc { 185041177Smarc char info[16]; 185141177Smarc register char *p = info; 185241177Smarc 185341177Smarc while (--min >= 0 || n) { 185441177Smarc *p++ = "0123456789abcdef"[n%base]; 185541177Smarc n /= base; 185641177Smarc } 185741177Smarc while (p > info) 185841177Smarc ttyoutput(*--p, tp); 185941177Smarc } 186041177Smarc 186141177Smarc /* 186241177Smarc * Returns 1 if p2 is "better" than p1 186341177Smarc * 186441177Smarc * The algorithm for picking the "interesting" process is thus: 186541177Smarc * 186641177Smarc * 1) (Only foreground processes are eligable - implied) 186741177Smarc * 2) Runnable processes are favored over anything 186841177Smarc * else. The runner with the highest cpu 186941177Smarc * utilization is picked (p_cpu). Ties are 187041177Smarc * broken by picking the highest pid. 187141177Smarc * 3 Next, the sleeper with the shortest sleep 187241177Smarc * time is favored. With ties, we pick out 187341177Smarc * just "short-term" sleepers (SSINTR == 0). 187441177Smarc * Further ties are broken by picking the highest 187541177Smarc * pid. 187641177Smarc * 187741177Smarc */ 187841177Smarc #define isrun(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 187941177Smarc proc_compare(p1, p2) 188041177Smarc register struct proc *p1, *p2; 188141177Smarc { 188241177Smarc 188341177Smarc if (p1 == NULL) 188441177Smarc return (1); 188541177Smarc /* 188641177Smarc * see if at least one of them is runnable 188741177Smarc */ 188841177Smarc switch (isrun(p1)<<1 | isrun(p2)) { 188941177Smarc case 0x01: 189041177Smarc return (1); 189141177Smarc case 0x10: 189241177Smarc return (0); 189341177Smarc case 0x11: 189441177Smarc /* 189541177Smarc * tie - favor one with highest recent cpu utilization 189641177Smarc */ 189741177Smarc if (p2->p_cpu > p1->p_cpu) 189841177Smarc return (1); 189941177Smarc if (p1->p_cpu > p2->p_cpu) 190041177Smarc return (0); 190141177Smarc return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 190241177Smarc } 190341177Smarc /* 190441177Smarc * pick the one with the smallest sleep time 190541177Smarc */ 190641177Smarc if (p2->p_slptime > p1->p_slptime) 190741177Smarc return (0); 190841177Smarc if (p1->p_slptime > p2->p_slptime) 190941177Smarc return (1); 191041177Smarc /* 191141177Smarc * favor one sleeping in a non-interruptible sleep 191241177Smarc */ 191341177Smarc if (p1->p_flag&SSINTR && (p2->p_flag&SSINTR) == 0) 191441177Smarc return (1); 191541177Smarc if (p2->p_flag&SSINTR && (p1->p_flag&SSINTR) == 0) 191641177Smarc return (0); 191741177Smarc return(p2->p_pid > p1->p_pid); /* tie - return highest pid */ 191841177Smarc } 191939407Smarc #define TOTTY 0x2 /* XXX should be in header */ 192039407Smarc /*VARARGS2*/ 192139407Smarc ttyprintf(tp, fmt, x1) 192239555Smarc struct tty *tp; 192339407Smarc char *fmt; 192439407Smarc unsigned x1; 192539407Smarc { 192639555Smarc prf(fmt, &x1, TOTTY, (caddr_t)tp); 192739407Smarc } 192839555Smarc 192939555Smarc /* 193039555Smarc * Output char to tty; console putchar style. 193139555Smarc */ 193239555Smarc tputchar(c, tp) 193339555Smarc int c; 193439555Smarc struct tty *tp; 193539555Smarc { 193639555Smarc register s = spltty(); 193739555Smarc 193839555Smarc if ((tp->t_state & (TS_CARR_ON | TS_ISOPEN)) 193939555Smarc == (TS_CARR_ON | TS_ISOPEN)) { 194039555Smarc if (c == '\n') 194139555Smarc (void) ttyoutput('\r', tp); 194239555Smarc (void) ttyoutput(c, tp); 194339555Smarc ttstart(tp); 194439555Smarc splx(s); 194539555Smarc return (0); 194639555Smarc } 194739555Smarc splx(s); 194839555Smarc return (-1); 194939555Smarc } 1950*43377Smarc 1951*43377Smarc ttysleep(tp, chan, pri, wmesg, timo) 1952*43377Smarc struct tty *tp; 1953*43377Smarc caddr_t chan; 1954*43377Smarc int pri; 1955*43377Smarc char *wmesg; 1956*43377Smarc int timo; 1957*43377Smarc { 1958*43377Smarc int error; 1959*43377Smarc short gen = tp->t_gen; 1960*43377Smarc 1961*43377Smarc if (error = tsleep(chan, pri, wmesg, timo)) 1962*43377Smarc return (error); 1963*43377Smarc if (tp->t_gen != gen) 1964*43377Smarc return (ERESTART); 1965*43377Smarc return (0); 1966*43377Smarc } 1967