149594Sbostic /*- 263178Sbostic * Copyright (c) 1982, 1986, 1990, 1991, 1993 363178Sbostic * The Regents of the University of California. All rights reserved. 449594Sbostic * All rights reserved. 523387Smckusick * 649594Sbostic * %sccs.include.redist.c% 749594Sbostic * 8*64530Sbostic * @(#)tty.c 8.3 (Berkeley) 09/21/93 923387Smckusick */ 1039Sbill 1156517Sbostic #include <sys/param.h> 1256517Sbostic #include <sys/systm.h> 1356517Sbostic #include <sys/ioctl.h> 1456517Sbostic #include <sys/proc.h> 1564416Sbostic #define TTYDEFCHARS 1656517Sbostic #include <sys/tty.h> 1764416Sbostic #undef TTYDEFCHARS 1856517Sbostic #include <sys/file.h> 1956517Sbostic #include <sys/conf.h> 2056517Sbostic #include <sys/dkstat.h> 2156517Sbostic #include <sys/uio.h> 2256517Sbostic #include <sys/kernel.h> 2356517Sbostic #include <sys/vnode.h> 2456517Sbostic #include <sys/syslog.h> 2539Sbill 2656517Sbostic #include <vm/vm.h> 2737525Smckusick 2864416Sbostic static int proc_compare __P((struct proc *p1, struct proc *p2)); 2964416Sbostic static int ttnread __P((struct tty *)); 3064416Sbostic static void ttyblock __P((struct tty *tp)); 3164416Sbostic static void ttyecho __P((int, struct tty *tp)); 3264416Sbostic static void ttyrubo __P((struct tty *, int)); 3349907Sbostic 3464416Sbostic /* Symbolic sleep message strings. */ 3564416Sbostic char ttclos[] = "ttycls"; 3664416Sbostic char ttopen[] = "ttyopn"; 3764416Sbostic char ttybg[] = "ttybg"; 3864416Sbostic char ttybuf[] = "ttybuf"; 3964416Sbostic char ttyin[] = "ttyin"; 4064416Sbostic char ttyout[] = "ttyout"; 4140712Skarels 427436Skre /* 4364416Sbostic * Table with character classes and parity. The 8th bit indicates parity, 4464416Sbostic * the 7th bit indicates the character is an alphameric or underscore (for 4564416Sbostic * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits 4664416Sbostic * are 0 then the character needs no special processing on output; classes 4764416Sbostic * other than 0 might be translated or (not currently) require delays. 487436Skre */ 4964416Sbostic #define E 0x00 /* Even parity. */ 5064416Sbostic #define O 0x80 /* Odd parity. */ 5164416Sbostic #define PARITY(c) (char_type[c] & O) 5264416Sbostic 5364416Sbostic #define ALPHA 0x40 /* Alpha or underscore. */ 5464416Sbostic #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA) 5564416Sbostic 5649380Skarels #define CCLASSMASK 0x3f 5764416Sbostic #define CCLASS(c) (char_type[c] & CCLASSMASK) 5839Sbill 5964416Sbostic #define BS BACKSPACE 6049380Skarels #define CC CONTROL 6164416Sbostic #define CR RETURN 6264416Sbostic #define NA ORDINARY | ALPHA 6349380Skarels #define NL NEWLINE 6464416Sbostic #define NO ORDINARY 6549380Skarels #define TB TAB 6649380Skarels #define VT VTAB 6749380Skarels 6864416Sbostic char const char_type[] = { 6949380Skarels E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */ 7049380Skarels O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */ 7149380Skarels O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */ 7249380Skarels E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */ 7349380Skarels O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */ 7449380Skarels E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */ 7549380Skarels E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */ 7649380Skarels O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */ 7749380Skarels O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */ 7849380Skarels E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */ 7949380Skarels E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */ 8049380Skarels O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */ 8149380Skarels E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */ 8249380Skarels O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */ 8349380Skarels O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */ 8449380Skarels E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */ 857436Skre /* 8664416Sbostic * Meta chars; should be settable per character set; 8764416Sbostic * for now, treat them all as normal characters. 887436Skre */ 8949380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9049380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9149380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9249380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9349380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9449380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9549380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9649380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9749380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9849380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 9949380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 10049380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 10149380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 10249380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 10349380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 10449380Skarels NA, NA, NA, NA, NA, NA, NA, NA, 1057436Skre }; 10664416Sbostic #undef BS 10764416Sbostic #undef CC 10864416Sbostic #undef CR 10949380Skarels #undef NA 11049380Skarels #undef NL 11164416Sbostic #undef NO 11249380Skarels #undef TB 11349380Skarels #undef VT 1147436Skre 11564416Sbostic /* Macros to clear/set/test flags. */ 11664416Sbostic #define SET(t, f) (t) |= (f) 11764416Sbostic #define CLR(t, f) (t) &= ~(f) 11864416Sbostic #define ISSET(t, f) ((t) & (f)) 11935811Smarc 120146Sbill /* 12164416Sbostic * Initial open of tty, or (re)entry to standard tty line discipline. 12239Sbill */ 12364416Sbostic int 12464416Sbostic ttyopen(device, tp) 12564416Sbostic dev_t device; 12612752Ssam register struct tty *tp; 12712752Ssam { 12852485Storek int s; 12947545Skarels 13052485Storek s = spltty(); 13164416Sbostic tp->t_dev = device; 13264416Sbostic if (!ISSET(tp->t_state, TS_ISOPEN)) { 13364416Sbostic SET(tp->t_state, TS_ISOPEN); 13464416Sbostic bzero(&tp->t_winsize, sizeof(tp->t_winsize)); 135903Sbill } 13664416Sbostic CLR(tp->t_state, TS_WOPEN); 1375408Swnj splx(s); 1385408Swnj return (0); 1394484Swnj } 1407436Skre 1417502Sroot /* 14249380Skarels * Handle close() on a tty line: flush and set to initial state, 14349380Skarels * bumping generation number so that pending read/write calls 14449380Skarels * can detect recycling of the tty. 1457502Sroot */ 14664416Sbostic int 1477502Sroot ttyclose(tp) 1487625Ssam register struct tty *tp; 1497502Sroot { 15064416Sbostic extern struct tty *constty; /* Temporary virtual console. */ 15164416Sbostic 15230534Skarels if (constty == tp) 15330534Skarels constty = NULL; 15464416Sbostic 15564416Sbostic ttyflush(tp, FREAD | FWRITE); 15664416Sbostic 15764416Sbostic tp->t_gen++; 15864416Sbostic tp->t_pgrp = NULL; 15939555Smarc tp->t_session = NULL; 1607502Sroot tp->t_state = 0; 16140712Skarels return (0); 1627502Sroot } 1637502Sroot 16464416Sbostic #define FLUSHQ(q) { \ 16564416Sbostic if ((q)->c_cc) \ 16664416Sbostic ndflush(q, (q)->c_cc); \ 16725391Skarels } 16825391Skarels 16964416Sbostic /* Is 'c' a line delimiter ("break" character)? */ 17064416Sbostic #define TTBREAKC(c) \ 17164416Sbostic ((c) == '\n' || ((c) == cc[VEOF] || \ 17264416Sbostic (c) == cc[VEOL] || (c) == cc[VEOL2]) && (c) != _POSIX_VDISABLE) 17325404Skarels 1747502Sroot 1757502Sroot /* 17649380Skarels * Process input of a single character received on a tty. 1777502Sroot */ 17864416Sbostic int 1797502Sroot ttyinput(c, tp) 18064416Sbostic register int c; 1817625Ssam register struct tty *tp; 1827502Sroot { 18364416Sbostic register int iflag, lflag; 18464416Sbostic register u_char *cc; 18535811Smarc int i, err; 1867502Sroot 1879578Ssam /* 1889578Ssam * If input is pending take it first. 1899578Ssam */ 19064416Sbostic lflag = tp->t_lflag; 19164416Sbostic if (ISSET(lflag, PENDIN)) 1927502Sroot ttypend(tp); 19335811Smarc /* 19435811Smarc * Gather stats. 19535811Smarc */ 19664416Sbostic if (ISSET(lflag, ICANON)) { 19764416Sbostic ++tk_cancc; 19864416Sbostic ++tp->t_cancc; 19935811Smarc } else { 20064416Sbostic ++tk_rawcc; 20164416Sbostic ++tp->t_rawcc; 20235811Smarc } 20364416Sbostic ++tk_nin; 20464416Sbostic 20564416Sbostic /* Handle exceptional conditions (break, parity, framing). */ 20664416Sbostic cc = tp->t_cc; 20764416Sbostic iflag = tp->t_iflag; 20864416Sbostic if (err = (c & TTY_ERRORMASK)) { 20935811Smarc c &= ~TTY_ERRORMASK; 21064416Sbostic if (err & TTY_FE && !c) { /* Break. */ 21164416Sbostic if (ISSET(iflag, IGNBRK)) 21235811Smarc goto endcase; 21364416Sbostic else if (ISSET(iflag, BRKINT) && 21464416Sbostic ISSET(lflag, ISIG) && 21564416Sbostic (cc[VINTR] != _POSIX_VDISABLE)) 21635811Smarc c = cc[VINTR]; 21764416Sbostic else if (ISSET(iflag, PARMRK)) 21847545Skarels goto parmrk; 21964416Sbostic } else if (err & TTY_PE && 22064416Sbostic ISSET(iflag, INPCK) || err & TTY_FE) { 22164416Sbostic if (ISSET(iflag, IGNPAR)) 22235811Smarc goto endcase; 22364416Sbostic else if (ISSET(iflag, PARMRK)) { 22464416Sbostic parmrk: putc(0377 | TTY_QUOTE, &tp->t_rawq); 22564416Sbostic putc(0 | TTY_QUOTE, &tp->t_rawq); 22664416Sbostic putc(c | TTY_QUOTE, &tp->t_rawq); 22735811Smarc goto endcase; 22835811Smarc } else 22935811Smarc c = 0; 2307502Sroot } 2319578Ssam } 2329578Ssam /* 23335811Smarc * In tandem mode, check high water mark. 2349578Ssam */ 23564416Sbostic if (ISSET(iflag, IXOFF)) 23635811Smarc ttyblock(tp); 23764416Sbostic if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP)) 23849380Skarels c &= ~0x80; 23964416Sbostic if (!ISSET(lflag, EXTPROC)) { 24044419Smarc /* 24144419Smarc * Check for literal nexting very first 24244419Smarc */ 24364416Sbostic if (ISSET(tp->t_state, TS_LNCH)) { 24444419Smarc c |= TTY_QUOTE; 24564416Sbostic CLR(tp->t_state, TS_LNCH); 24644419Smarc } 24744419Smarc /* 24844419Smarc * Scan for special characters. This code 24944419Smarc * is really just a big case statement with 25044419Smarc * non-constant cases. The bottom of the 25144419Smarc * case statement is labeled ``endcase'', so goto 25244419Smarc * it after a case match, or similar. 25344419Smarc */ 25444419Smarc 25544419Smarc /* 25644419Smarc * Control chars which aren't controlled 25744419Smarc * by ICANON, ISIG, or IXON. 25844419Smarc */ 25964416Sbostic if (ISSET(lflag, IEXTEN)) { 26044419Smarc if (CCEQ(cc[VLNEXT], c)) { 26164416Sbostic if (ISSET(lflag, ECHO)) { 26264416Sbostic if (ISSET(lflag, ECHOE)) { 26364416Sbostic (void)ttyoutput('^', tp); 26464416Sbostic (void)ttyoutput('\b', tp); 26564416Sbostic } else 26644419Smarc ttyecho(c, tp); 26744419Smarc } 26864416Sbostic SET(tp->t_state, TS_LNCH); 26944419Smarc goto endcase; 27044419Smarc } 27144419Smarc if (CCEQ(cc[VDISCARD], c)) { 27264416Sbostic if (ISSET(lflag, FLUSHO)) 27364416Sbostic CLR(tp->t_lflag, FLUSHO); 27444419Smarc else { 27544419Smarc ttyflush(tp, FWRITE); 27635811Smarc ttyecho(c, tp); 27744419Smarc if (tp->t_rawq.c_cc + tp->t_canq.c_cc) 27844419Smarc ttyretype(tp); 27964416Sbostic SET(tp->t_lflag, FLUSHO); 28044419Smarc } 28144419Smarc goto startoutput; 28235811Smarc } 2839578Ssam } 28444419Smarc /* 28544419Smarc * Signals. 28644419Smarc */ 28764416Sbostic if (ISSET(lflag, ISIG)) { 28844419Smarc if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) { 28964416Sbostic if (!ISSET(lflag, NOFLSH)) 29064416Sbostic ttyflush(tp, FREAD | FWRITE); 2917502Sroot ttyecho(c, tp); 29244419Smarc pgsignal(tp->t_pgrp, 29344419Smarc CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1); 29444419Smarc goto endcase; 2957502Sroot } 29644419Smarc if (CCEQ(cc[VSUSP], c)) { 29764416Sbostic if (!ISSET(lflag, NOFLSH)) 29844419Smarc ttyflush(tp, FREAD); 29944419Smarc ttyecho(c, tp); 30044419Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 30144419Smarc goto endcase; 30244419Smarc } 3039578Ssam } 30444419Smarc /* 30544419Smarc * Handle start/stop characters. 30644419Smarc */ 30764416Sbostic if (ISSET(iflag, IXON)) { 30844419Smarc if (CCEQ(cc[VSTOP], c)) { 30964416Sbostic if (!ISSET(tp->t_state, TS_TTSTOP)) { 31064416Sbostic SET(tp->t_state, TS_TTSTOP); 31152485Storek #ifdef sun4c /* XXX */ 31252485Storek (*tp->t_stop)(tp, 0); 31352485Storek #else 31444419Smarc (*cdevsw[major(tp->t_dev)].d_stop)(tp, 31544419Smarc 0); 31652485Storek #endif 31764416Sbostic return (0); 31844419Smarc } 31944419Smarc if (!CCEQ(cc[VSTART], c)) 32064416Sbostic return (0); 32164416Sbostic /* 32264416Sbostic * if VSTART == VSTOP then toggle 32344419Smarc */ 32444419Smarc goto endcase; 32535811Smarc } 32644419Smarc if (CCEQ(cc[VSTART], c)) 32744419Smarc goto restartoutput; 3289578Ssam } 32944419Smarc /* 33044419Smarc * IGNCR, ICRNL, & INLCR 33144419Smarc */ 33244419Smarc if (c == '\r') { 33364416Sbostic if (ISSET(iflag, IGNCR)) 33444419Smarc goto endcase; 33564416Sbostic else if (ISSET(iflag, ICRNL)) 33644419Smarc c = '\n'; 33764416Sbostic } else if (c == '\n' && ISSET(iflag, INLCR)) 33844419Smarc c = '\r'; 3399578Ssam } 34064416Sbostic if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) { 34144419Smarc /* 34244419Smarc * From here on down canonical mode character 34344419Smarc * processing takes place. 34444419Smarc */ 34544419Smarc /* 34644419Smarc * erase (^H / ^?) 34744419Smarc */ 34844419Smarc if (CCEQ(cc[VERASE], c)) { 34944419Smarc if (tp->t_rawq.c_cc) 3509578Ssam ttyrub(unputc(&tp->t_rawq), tp); 35144419Smarc goto endcase; 3529578Ssam } 35344419Smarc /* 35444419Smarc * kill (^U) 35544419Smarc */ 35644419Smarc if (CCEQ(cc[VKILL], c)) { 35764416Sbostic if (ISSET(lflag, ECHOKE) && 35864416Sbostic tp->t_rawq.c_cc == tp->t_rocount && 35964416Sbostic !ISSET(lflag, ECHOPRT)) 36044419Smarc while (tp->t_rawq.c_cc) 36144419Smarc ttyrub(unputc(&tp->t_rawq), tp); 36264416Sbostic else { 36344419Smarc ttyecho(c, tp); 36464416Sbostic if (ISSET(lflag, ECHOK) || 36564416Sbostic ISSET(lflag, ECHOKE)) 36644419Smarc ttyecho('\n', tp); 36764416Sbostic FLUSHQ(&tp->t_rawq); 36844419Smarc tp->t_rocount = 0; 36944419Smarc } 37064416Sbostic CLR(tp->t_state, TS_LOCAL); 37144419Smarc goto endcase; 37244419Smarc } 37344419Smarc /* 37444419Smarc * word erase (^W) 37544419Smarc */ 37664416Sbostic if (CCEQ(cc[VWERASE], c)) { 37764416Sbostic int alt = ISSET(lflag, ALTWERASE); 37844419Smarc int ctype; 37935811Smarc 38064416Sbostic /* 38164416Sbostic * erase whitespace 38244419Smarc */ 38344419Smarc while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t') 38444419Smarc ttyrub(c, tp); 38544419Smarc if (c == -1) 38644419Smarc goto endcase; 38744419Smarc /* 38847545Skarels * erase last char of word and remember the 38947545Skarels * next chars type (for ALTWERASE) 39044419Smarc */ 39135811Smarc ttyrub(c, tp); 39244419Smarc c = unputc(&tp->t_rawq); 39347545Skarels if (c == -1) 39444419Smarc goto endcase; 39551003Sbostic if (c == ' ' || c == '\t') { 39651003Sbostic putc(c, &tp->t_rawq); 39751003Sbostic goto endcase; 39851003Sbostic } 39949380Skarels ctype = ISALPHA(c); 40044419Smarc /* 40147545Skarels * erase rest of word 40244419Smarc */ 40344419Smarc do { 40444419Smarc ttyrub(c, tp); 40544419Smarc c = unputc(&tp->t_rawq); 40644419Smarc if (c == -1) 40744419Smarc goto endcase; 40864416Sbostic } while (c != ' ' && c != '\t' && 40964416Sbostic (alt == 0 || ISALPHA(c) == ctype)); 41064416Sbostic (void)putc(c, &tp->t_rawq); 41134492Skarels goto endcase; 41244419Smarc } 41335811Smarc /* 41444419Smarc * reprint line (^R) 41535811Smarc */ 41644419Smarc if (CCEQ(cc[VREPRINT], c)) { 41744419Smarc ttyretype(tp); 41834492Skarels goto endcase; 41934492Skarels } 42035811Smarc /* 42144419Smarc * ^T - kernel info and generate SIGINFO 42235811Smarc */ 42344419Smarc if (CCEQ(cc[VSTATUS], c)) { 42464416Sbostic if (ISSET(lflag, ISIG)) 42551068Smarc pgsignal(tp->t_pgrp, SIGINFO, 1); 42664416Sbostic if (!ISSET(lflag, NOKERNINFO)) 42744419Smarc ttyinfo(tp); 42844419Smarc goto endcase; 42944419Smarc } 4309578Ssam } 4319578Ssam /* 4329578Ssam * Check for input buffer overflow 4339578Ssam */ 43447545Skarels if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) { 43564416Sbostic if (ISSET(iflag, IMAXBEL)) { 43635811Smarc if (tp->t_outq.c_cc < tp->t_hiwat) 43764416Sbostic (void)ttyoutput(CTRL('g'), tp); 43835811Smarc } else 43935811Smarc ttyflush(tp, FREAD | FWRITE); 4409578Ssam goto endcase; 44110391Ssam } 4429578Ssam /* 4439578Ssam * Put data char in q for user and 4449578Ssam * wakeup on seeing a line delimiter. 4459578Ssam */ 4469578Ssam if (putc(c, &tp->t_rawq) >= 0) { 44764416Sbostic if (!ISSET(lflag, ICANON)) { 44847545Skarels ttwakeup(tp); 44947545Skarels ttyecho(c, tp); 45047545Skarels goto endcase; 45147545Skarels } 45264416Sbostic if (TTBREAKC(c)) { 4539578Ssam tp->t_rocount = 0; 4549578Ssam catq(&tp->t_rawq, &tp->t_canq); 4557502Sroot ttwakeup(tp); 4569578Ssam } else if (tp->t_rocount++ == 0) 457*64530Sbostic tp->t_rocol = tp->t_column; 45864416Sbostic if (ISSET(tp->t_state, TS_ERASE)) { 45935811Smarc /* 46035811Smarc * end of prterase \.../ 46135811Smarc */ 46264416Sbostic CLR(tp->t_state, TS_ERASE); 46364416Sbostic (void)ttyoutput('/', tp); 4649578Ssam } 465*64530Sbostic i = tp->t_column; 4667502Sroot ttyecho(c, tp); 46764416Sbostic if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) { 46835811Smarc /* 46935811Smarc * Place the cursor over the '^' of the ^D. 47035811Smarc */ 471*64530Sbostic i = min(2, tp->t_column - i); 4729578Ssam while (i > 0) { 47364416Sbostic (void)ttyoutput('\b', tp); 4749578Ssam i--; 4759578Ssam } 4769578Ssam } 4777502Sroot } 4789578Ssam endcase: 4799578Ssam /* 48035811Smarc * IXANY means allow any character to restart output. 4819578Ssam */ 48264416Sbostic if (ISSET(tp->t_state, TS_TTSTOP) && 48364416Sbostic !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP]) 48464416Sbostic return (0); 4859578Ssam restartoutput: 48664416Sbostic CLR(tp->t_lflag, FLUSHO); 48764416Sbostic CLR(tp->t_state, TS_TTSTOP); 4889578Ssam startoutput: 48964416Sbostic return (ttstart(tp)); 4907502Sroot } 4917502Sroot 4927502Sroot /* 49349380Skarels * Output a single character on a tty, doing output processing 49449380Skarels * as needed (expanding tabs, newline processing, etc.). 49564416Sbostic * Returns < 0 if succeeds, otherwise returns char to resend. 4967502Sroot * Must be recursive. 4977502Sroot */ 49864416Sbostic int 4997502Sroot ttyoutput(c, tp) 50064416Sbostic register int c; 5017502Sroot register struct tty *tp; 5027502Sroot { 50364416Sbostic register long oflag; 50464416Sbostic register int col, s; 50564416Sbostic 50664416Sbostic oflag = tp->t_oflag; 50764416Sbostic if (!ISSET(oflag, OPOST)) { 50864416Sbostic if (ISSET(tp->t_lflag, FLUSHO)) 5097502Sroot return (-1); 5107502Sroot if (putc(c, &tp->t_outq)) 5117625Ssam return (c); 5127502Sroot tk_nout++; 51335811Smarc tp->t_outcc++; 5147502Sroot return (-1); 5157502Sroot } 5167502Sroot /* 51764416Sbostic * Do tab expansion if OXTABS is set. Special case if we external 51864416Sbostic * processing, we don't do the tab expansion because we'll probably 51964416Sbostic * get it wrong. If tab expansion needs to be done, let it happen 52064416Sbostic * externally. 5217502Sroot */ 52264416Sbostic c &= TTY_CHARMASK; 52364416Sbostic if (c == '\t' && 52464416Sbostic ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) { 525*64530Sbostic c = 8 - (tp->t_column & 7); 52664416Sbostic if (!ISSET(tp->t_lflag, FLUSHO)) { 52764416Sbostic s = spltty(); /* Don't interrupt tabs. */ 5287502Sroot c -= b_to_q(" ", c, &tp->t_outq); 5297502Sroot tk_nout += c; 53035811Smarc tp->t_outcc += c; 5317502Sroot splx(s); 5327502Sroot } 533*64530Sbostic tp->t_column += c; 5347502Sroot return (c ? -1 : '\t'); 5357502Sroot } 53664416Sbostic if (c == CEOT && ISSET(oflag, ONOEOT)) 53747545Skarels return (-1); 5387502Sroot tk_nout++; 53935811Smarc tp->t_outcc++; 5407502Sroot /* 54149380Skarels * Newline translation: if ONLCR is set, 54249380Skarels * translate newline into "\r\n". 5437502Sroot */ 54464416Sbostic if (c == '\n' && 54564416Sbostic ISSET(tp->t_oflag, ONLCR) && ttyoutput('\r', tp) >= 0) 5467502Sroot return (c); 54764416Sbostic if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq)) 54835811Smarc return (c); 54947545Skarels 550*64530Sbostic col = tp->t_column; 55149380Skarels switch (CCLASS(c)) { 5527502Sroot case BACKSPACE: 55349380Skarels if (col > 0) 55464416Sbostic --col; 5557502Sroot break; 55664416Sbostic case CONTROL: 55764416Sbostic break; 5587502Sroot case NEWLINE: 55964416Sbostic case RETURN: 56049380Skarels col = 0; 5617502Sroot break; 56264416Sbostic case ORDINARY: 56364416Sbostic ++col; 56464416Sbostic break; 5657502Sroot case TAB: 56664416Sbostic col = (col + 8) & ~7; 5677502Sroot break; 5687502Sroot } 569*64530Sbostic tp->t_column = col; 5707502Sroot return (-1); 5717502Sroot } 5727502Sroot 5737502Sroot /* 57464416Sbostic * Common code for ioctls on all tty devices. Called after line-discipline 57564416Sbostic * specific ioctl has been called to do discipline-specific functions and/or 57664416Sbostic * reject any of these ioctl commands. 57764416Sbostic */ 57864416Sbostic /* ARGSUSED */ 57964416Sbostic int 58064416Sbostic ttioctl(tp, com, data, flag) 58164416Sbostic register struct tty *tp; 58264416Sbostic int com, flag; 58364416Sbostic void *data; 58464416Sbostic { 58564416Sbostic extern struct tty *constty; /* Temporary virtual console. */ 58664416Sbostic extern int nldisp; 58764416Sbostic register struct proc *p; 58864416Sbostic int s, error; 58964416Sbostic 59064416Sbostic p = curproc; /* XXX */ 59164416Sbostic 59264416Sbostic /* If the ioctl involves modification, hang if in the background. */ 59364416Sbostic switch (com) { 59464416Sbostic case TIOCFLUSH: 59564416Sbostic case TIOCSETA: 59664416Sbostic case TIOCSETD: 59764416Sbostic case TIOCSETAF: 59864416Sbostic case TIOCSETAW: 59964416Sbostic #ifdef notdef 60064416Sbostic case TIOCSPGRP: 60164416Sbostic #endif 60264416Sbostic case TIOCSTI: 60364416Sbostic case TIOCSWINSZ: 60464416Sbostic #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 60564416Sbostic case TIOCLBIC: 60664416Sbostic case TIOCLBIS: 60764416Sbostic case TIOCLSET: 60864416Sbostic case TIOCSETC: 60964416Sbostic case OTIOCSETD: 61064416Sbostic case TIOCSETN: 61164416Sbostic case TIOCSETP: 61264416Sbostic case TIOCSLTC: 61364416Sbostic #endif 61464416Sbostic while (isbackground(curproc, tp) && 61564416Sbostic p->p_pgrp->pg_jobc && (p->p_flag & SPPWAIT) == 0 && 61664416Sbostic (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 61764416Sbostic (p->p_sigmask & sigmask(SIGTTOU)) == 0) { 61864416Sbostic pgsignal(p->p_pgrp, SIGTTOU, 1); 61964416Sbostic if (error = ttysleep(tp, 62064416Sbostic &lbolt, TTOPRI | PCATCH, ttybg, 0)) 62164416Sbostic return (error); 62264416Sbostic } 62364416Sbostic break; 62464416Sbostic } 62564416Sbostic 62664416Sbostic switch (com) { /* Process the ioctl. */ 62764416Sbostic case FIOASYNC: /* set/clear async i/o */ 62864416Sbostic s = spltty(); 62964416Sbostic if (*(int *)data) 63064416Sbostic SET(tp->t_state, TS_ASYNC); 63164416Sbostic else 63264416Sbostic CLR(tp->t_state, TS_ASYNC); 63364416Sbostic splx(s); 63464416Sbostic break; 63564416Sbostic case FIONBIO: /* set/clear non-blocking i/o */ 63664416Sbostic break; /* XXX: delete. */ 63764416Sbostic case FIONREAD: /* get # bytes to read */ 63864416Sbostic *(int *)data = ttnread(tp); 63964416Sbostic break; 64064416Sbostic case TIOCEXCL: /* set exclusive use of tty */ 64164416Sbostic s = spltty(); 64264416Sbostic SET(tp->t_state, TS_XCLUDE); 64364416Sbostic splx(s); 64464416Sbostic break; 64564416Sbostic case TIOCFLUSH: { /* flush buffers */ 64664416Sbostic register int flags = *(int *)data; 64764416Sbostic 64864416Sbostic if (flags == 0) 64964416Sbostic flags = FREAD | FWRITE; 65064416Sbostic else 65164416Sbostic flags &= FREAD | FWRITE; 65264416Sbostic ttyflush(tp, flags); 65364416Sbostic break; 65464416Sbostic } 65564416Sbostic case TIOCCONS: /* become virtual console */ 65664416Sbostic if (*(int *)data) { 65764416Sbostic if (constty && constty != tp && 65864416Sbostic ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) == 65964416Sbostic (TS_CARR_ON | TS_ISOPEN)) 66064416Sbostic return (EBUSY); 66164416Sbostic #ifndef UCONSOLE 66264416Sbostic if (error = suser(p->p_ucred, &p->p_acflag)) 66364416Sbostic return (error); 66464416Sbostic #endif 66564416Sbostic constty = tp; 66664416Sbostic } else if (tp == constty) 66764416Sbostic constty = NULL; 66864416Sbostic break; 66964416Sbostic case TIOCDRAIN: /* wait till output drained */ 67064416Sbostic if (error = ttywait(tp)) 67164416Sbostic return (error); 67264416Sbostic break; 67364416Sbostic case TIOCGETA: { /* get termios struct */ 67464416Sbostic struct termios *t = (struct termios *)data; 67564416Sbostic 67664416Sbostic bcopy(&tp->t_termios, t, sizeof(struct termios)); 67764416Sbostic break; 67864416Sbostic } 67964416Sbostic case TIOCGETD: /* get line discipline */ 68064416Sbostic *(int *)data = tp->t_line; 68164416Sbostic break; 68264416Sbostic case TIOCGWINSZ: /* get window size */ 68364416Sbostic *(struct winsize *)data = tp->t_winsize; 68464416Sbostic break; 68564416Sbostic case TIOCGPGRP: /* get pgrp of tty */ 68664416Sbostic if (!isctty(p, tp)) 68764416Sbostic return (ENOTTY); 68864416Sbostic *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID; 68964416Sbostic break; 69064416Sbostic #ifdef TIOCHPCL 69164416Sbostic case TIOCHPCL: /* hang up on last close */ 69264416Sbostic s = spltty(); 69364416Sbostic tp->t_cflag |= HUPCL; 69464416Sbostic splx(s); 69564416Sbostic break; 69664416Sbostic #endif 69764416Sbostic case TIOCNXCL: /* reset exclusive use of tty */ 69864416Sbostic s = spltty(); 69964416Sbostic CLR(tp->t_state, TS_XCLUDE); 70064416Sbostic splx(s); 70164416Sbostic break; 70264416Sbostic case TIOCOUTQ: /* output queue size */ 70364416Sbostic *(int *)data = tp->t_outq.c_cc; 70464416Sbostic break; 70564416Sbostic case TIOCSETA: /* set termios struct */ 70664416Sbostic case TIOCSETAW: /* drain output, set */ 70764416Sbostic case TIOCSETAF: { /* drn out, fls in, set */ 70864416Sbostic register struct termios *t = (struct termios *)data; 70964416Sbostic 71064416Sbostic s = spltty(); 71164416Sbostic if (com == TIOCSETAW || com == TIOCSETAF) { 71264416Sbostic if (error = ttywait(tp)) { 71364416Sbostic splx(s); 71464416Sbostic return (error); 71564416Sbostic } 71664416Sbostic if (com == TIOCSETAF) 71764416Sbostic ttyflush(tp, FREAD); 71864416Sbostic } 71964416Sbostic if (!ISSET(t->c_cflag, CIGNORE)) { 72064416Sbostic /* 72164416Sbostic * Set device hardware. 72264416Sbostic */ 72364416Sbostic if (tp->t_param && (error = (*tp->t_param)(tp, t))) { 72464416Sbostic splx(s); 72564416Sbostic return (error); 72664416Sbostic } else { 72764416Sbostic if (!ISSET(tp->t_state, TS_CARR_ON) && 72864416Sbostic ISSET(tp->t_cflag, CLOCAL) && 72964416Sbostic !ISSET(t->c_cflag, CLOCAL)) { 73064416Sbostic CLR(tp->t_state, TS_ISOPEN); 73164416Sbostic SET(tp->t_state, TS_WOPEN); 73264416Sbostic ttwakeup(tp); 73364416Sbostic } 73464416Sbostic tp->t_cflag = t->c_cflag; 73564416Sbostic tp->t_ispeed = t->c_ispeed; 73664416Sbostic tp->t_ospeed = t->c_ospeed; 73764416Sbostic } 73864416Sbostic ttsetwater(tp); 73964416Sbostic } 74064416Sbostic if (com != TIOCSETAF) { 74164416Sbostic if (ISSET(t->c_lflag, ICANON) != 74264416Sbostic ISSET(tp->t_lflag, ICANON)) 74364416Sbostic if (ISSET(t->c_lflag, ICANON)) { 74464416Sbostic tp->t_lflag |= PENDIN; 74564416Sbostic ttwakeup(tp); 74664416Sbostic } else { 74764416Sbostic struct clist tq; 74864416Sbostic 74964416Sbostic catq(&tp->t_rawq, &tp->t_canq); 75064416Sbostic tq = tp->t_rawq; 75164416Sbostic tp->t_rawq = tp->t_canq; 75264416Sbostic tp->t_canq = tq; 75364416Sbostic } 75464416Sbostic } 75564416Sbostic tp->t_iflag = t->c_iflag; 75664416Sbostic tp->t_oflag = t->c_oflag; 75764416Sbostic /* 75864416Sbostic * Make the EXTPROC bit read only. 75964416Sbostic */ 76064416Sbostic if (ISSET(tp->t_lflag, EXTPROC)) 76164416Sbostic SET(t->c_lflag, EXTPROC); 76264416Sbostic else 76364416Sbostic CLR(t->c_lflag, EXTPROC); 76464416Sbostic tp->t_lflag = t->c_lflag; 76564416Sbostic bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc)); 76664416Sbostic splx(s); 76764416Sbostic break; 76864416Sbostic } 76964416Sbostic case TIOCSETD: { /* set line discipline */ 77064416Sbostic register int t = *(int *)data; 77164416Sbostic dev_t device = tp->t_dev; 77264416Sbostic 77364416Sbostic if ((u_int)t >= nldisp) 77464416Sbostic return (ENXIO); 77564416Sbostic if (t != tp->t_line) { 77664416Sbostic s = spltty(); 77764416Sbostic (*linesw[tp->t_line].l_close)(tp, flag); 77864416Sbostic error = (*linesw[t].l_open)(device, tp); 77964416Sbostic if (error) { 78064416Sbostic (void)(*linesw[tp->t_line].l_open)(device, tp); 78164416Sbostic splx(s); 78264416Sbostic return (error); 78364416Sbostic } 78464416Sbostic tp->t_line = t; 78564416Sbostic splx(s); 78664416Sbostic } 78764416Sbostic break; 78864416Sbostic } 78964416Sbostic case TIOCSTART: /* start output, like ^Q */ 79064416Sbostic s = spltty(); 79164416Sbostic if (ISSET(tp->t_state, TS_TTSTOP) || 79264416Sbostic ISSET(tp->t_lflag, FLUSHO)) { 79364416Sbostic CLR(tp->t_lflag, FLUSHO); 79464416Sbostic CLR(tp->t_state, TS_TTSTOP); 79564416Sbostic ttstart(tp); 79664416Sbostic } 79764416Sbostic splx(s); 79864416Sbostic break; 79964416Sbostic case TIOCSTI: /* simulate terminal input */ 80064416Sbostic if (p->p_ucred->cr_uid && (flag & FREAD) == 0) 80164416Sbostic return (EPERM); 80264416Sbostic if (p->p_ucred->cr_uid && !isctty(p, tp)) 80364416Sbostic return (EACCES); 80464416Sbostic (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp); 80564416Sbostic break; 80664416Sbostic case TIOCSTOP: /* stop output, like ^S */ 80764416Sbostic s = spltty(); 80864416Sbostic if (!ISSET(tp->t_state, TS_TTSTOP)) { 80964416Sbostic SET(tp->t_state, TS_TTSTOP); 81064416Sbostic #ifdef sun4c /* XXX */ 81164416Sbostic (*tp->t_stop)(tp, 0); 81264416Sbostic #else 81364416Sbostic (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 81464416Sbostic #endif 81564416Sbostic } 81664416Sbostic splx(s); 81764416Sbostic break; 81864416Sbostic case TIOCSCTTY: /* become controlling tty */ 81964416Sbostic /* Session ctty vnode pointer set in vnode layer. */ 82064416Sbostic if (!SESS_LEADER(p) || 82164416Sbostic (p->p_session->s_ttyvp || tp->t_session) && 82264416Sbostic (tp->t_session != p->p_session)) 82364416Sbostic return (EPERM); 82464416Sbostic tp->t_session = p->p_session; 82564416Sbostic tp->t_pgrp = p->p_pgrp; 82664416Sbostic p->p_session->s_ttyp = tp; 82764416Sbostic p->p_flag |= SCTTY; 82864416Sbostic break; 82964416Sbostic case TIOCSPGRP: { /* set pgrp of tty */ 83064416Sbostic register struct pgrp *pgrp = pgfind(*(int *)data); 83164416Sbostic 83264416Sbostic if (!isctty(p, tp)) 83364416Sbostic return (ENOTTY); 83464416Sbostic else if (pgrp == NULL || pgrp->pg_session != p->p_session) 83564416Sbostic return (EPERM); 83664416Sbostic tp->t_pgrp = pgrp; 83764416Sbostic break; 83864416Sbostic } 83964416Sbostic case TIOCSWINSZ: /* set window size */ 84064416Sbostic if (bcmp((caddr_t)&tp->t_winsize, data, 84164416Sbostic sizeof (struct winsize))) { 84264416Sbostic tp->t_winsize = *(struct winsize *)data; 84364416Sbostic pgsignal(tp->t_pgrp, SIGWINCH, 1); 84464416Sbostic } 84564416Sbostic break; 84664416Sbostic default: 84764416Sbostic #if defined(COMPAT_43) || defined(COMPAT_SUNOS) 84864416Sbostic return (ttcompat(tp, com, data, flag)); 84964416Sbostic #else 85064416Sbostic return (-1); 85164416Sbostic #endif 85264416Sbostic } 85364416Sbostic return (0); 85464416Sbostic } 85564416Sbostic 85664416Sbostic int 85764416Sbostic ttselect(device, rw, p) 85864416Sbostic dev_t device; 85964416Sbostic int rw; 86064416Sbostic struct proc *p; 86164416Sbostic { 86264416Sbostic register struct tty *tp; 86364416Sbostic int nread, s; 86464416Sbostic 86564416Sbostic tp = &cdevsw[major(device)].d_ttys[minor(device)]; 86664416Sbostic 86764416Sbostic s = spltty(); 86864416Sbostic switch (rw) { 86964416Sbostic case FREAD: 87064416Sbostic nread = ttnread(tp); 87164416Sbostic if (nread > 0 || !ISSET(tp->t_cflag, CLOCAL) && 87264416Sbostic !ISSET(tp->t_state, TS_CARR_ON)) 87364416Sbostic goto win; 87464416Sbostic selrecord(p, &tp->t_rsel); 87564416Sbostic break; 87664416Sbostic case FWRITE: 87764416Sbostic if (tp->t_outq.c_cc <= tp->t_lowat) { 87864416Sbostic win: splx(s); 87964416Sbostic return (1); 88064416Sbostic } 88164416Sbostic selrecord(p, &tp->t_wsel); 88264416Sbostic break; 88364416Sbostic } 88464416Sbostic splx(s); 88564416Sbostic return (0); 88664416Sbostic } 88764416Sbostic 88864416Sbostic static int 88964416Sbostic ttnread(tp) 89064416Sbostic struct tty *tp; 89164416Sbostic { 89264416Sbostic int nread; 89364416Sbostic 89464416Sbostic if (ISSET(tp->t_lflag, PENDIN)) 89564416Sbostic ttypend(tp); 89664416Sbostic nread = tp->t_canq.c_cc; 89764416Sbostic if (!ISSET(tp->t_lflag, ICANON)) 89864416Sbostic nread += tp->t_rawq.c_cc; 89964416Sbostic return (nread); 90064416Sbostic } 90164416Sbostic 90264416Sbostic /* 90364416Sbostic * Wait for output to drain. 90464416Sbostic */ 90564416Sbostic int 90664416Sbostic ttywait(tp) 90764416Sbostic register struct tty *tp; 90864416Sbostic { 90964416Sbostic int error, s; 91064416Sbostic 91164416Sbostic error = 0; 91264416Sbostic s = spltty(); 91364416Sbostic while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) && 91464416Sbostic (ISSET(tp->t_state, TS_CARR_ON) || ISSET(tp->t_cflag, CLOCAL)) 91564416Sbostic && tp->t_oproc) { 91664416Sbostic (*tp->t_oproc)(tp); 91764416Sbostic SET(tp->t_state, TS_ASLEEP); 91864416Sbostic if (error = ttysleep(tp, 91964416Sbostic &tp->t_outq, TTOPRI | PCATCH, ttyout, 0)) 92064416Sbostic break; 92164416Sbostic } 92264416Sbostic splx(s); 92364416Sbostic return (error); 92464416Sbostic } 92564416Sbostic 92664416Sbostic /* 92764416Sbostic * Flush if successfully wait. 92864416Sbostic */ 92964416Sbostic int 93064416Sbostic ttywflush(tp) 93164416Sbostic struct tty *tp; 93264416Sbostic { 93364416Sbostic int error; 93464416Sbostic 93564416Sbostic if ((error = ttywait(tp)) == 0) 93664416Sbostic ttyflush(tp, FREAD); 93764416Sbostic return (error); 93864416Sbostic } 93964416Sbostic 94064416Sbostic /* 94164416Sbostic * Flush tty read and/or write queues, notifying anyone waiting. 94264416Sbostic */ 94364416Sbostic void 94464416Sbostic ttyflush(tp, rw) 94564416Sbostic register struct tty *tp; 94664416Sbostic int rw; 94764416Sbostic { 94864416Sbostic register int s; 94964416Sbostic 95064416Sbostic s = spltty(); 95164416Sbostic if (rw & FREAD) { 95264416Sbostic FLUSHQ(&tp->t_canq); 95364416Sbostic FLUSHQ(&tp->t_rawq); 95464416Sbostic tp->t_rocount = 0; 95564416Sbostic tp->t_rocol = 0; 95664416Sbostic CLR(tp->t_state, TS_LOCAL); 95764416Sbostic ttwakeup(tp); 95864416Sbostic } 95964416Sbostic if (rw & FWRITE) { 96064416Sbostic CLR(tp->t_state, TS_TTSTOP); 96164416Sbostic #ifdef sun4c /* XXX */ 96264416Sbostic (*tp->t_stop)(tp, rw); 96364416Sbostic #else 96464416Sbostic (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw); 96564416Sbostic #endif 96664416Sbostic FLUSHQ(&tp->t_outq); 96764416Sbostic wakeup((caddr_t)&tp->t_outq); 96864416Sbostic selwakeup(&tp->t_wsel); 96964416Sbostic } 97064416Sbostic splx(s); 97164416Sbostic } 97264416Sbostic 97364416Sbostic /* 97464416Sbostic * Copy in the default termios characters. 97564416Sbostic */ 97664416Sbostic void 97764416Sbostic ttychars(tp) 97864416Sbostic struct tty *tp; 97964416Sbostic { 98064416Sbostic 98164416Sbostic bcopy(ttydefchars, tp->t_cc, sizeof(ttydefchars)); 98264416Sbostic } 98364416Sbostic 98464416Sbostic /* 98564416Sbostic * Send stop character on input overflow. 98664416Sbostic */ 98764416Sbostic static void 98864416Sbostic ttyblock(tp) 98964416Sbostic register struct tty *tp; 99064416Sbostic { 99164416Sbostic register int total; 99264416Sbostic 99364416Sbostic total = tp->t_rawq.c_cc + tp->t_canq.c_cc; 99464416Sbostic if (tp->t_rawq.c_cc > TTYHOG) { 99564416Sbostic ttyflush(tp, FREAD | FWRITE); 99664416Sbostic CLR(tp->t_state, TS_TBLOCK); 99764416Sbostic } 99864416Sbostic /* 99964416Sbostic * Block further input iff: current input > threshold 100064416Sbostic * AND input is available to user program. 100164416Sbostic */ 100264416Sbostic if (total >= TTYHOG / 2 && 100364416Sbostic !ISSET(tp->t_state, TS_TBLOCK) && 100464416Sbostic !ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0 && 100564416Sbostic tp->t_cc[VSTOP] != _POSIX_VDISABLE) { 100664416Sbostic if (putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) { 100764416Sbostic SET(tp->t_state, TS_TBLOCK); 100864416Sbostic ttstart(tp); 100964416Sbostic } 101064416Sbostic } 101164416Sbostic } 101264416Sbostic 101364416Sbostic void 101464416Sbostic ttrstrt(tp_arg) 101564416Sbostic void *tp_arg; 101664416Sbostic { 101764416Sbostic struct tty *tp; 101864416Sbostic int s; 101964416Sbostic 102064416Sbostic #ifdef DIAGNOSTIC 102164416Sbostic if (tp_arg == NULL) 102264416Sbostic panic("ttrstrt"); 102364416Sbostic #endif 102464416Sbostic tp = tp_arg; 102564416Sbostic s = spltty(); 102664416Sbostic 102764416Sbostic CLR(tp->t_state, TS_TIMEOUT); 102864416Sbostic ttstart(tp); 102964416Sbostic 103064416Sbostic splx(s); 103164416Sbostic } 103264416Sbostic 103364416Sbostic int 103464416Sbostic ttstart(tp) 103564416Sbostic struct tty *tp; 103664416Sbostic { 103764416Sbostic 103864416Sbostic if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */ 103964416Sbostic (*tp->t_oproc)(tp); 104064416Sbostic return (0); 104164416Sbostic } 104264416Sbostic 104364416Sbostic /* 104464416Sbostic * "close" a line discipline 104564416Sbostic */ 104664416Sbostic int 104764416Sbostic ttylclose(tp, flag) 104864416Sbostic struct tty *tp; 104964416Sbostic int flag; 105064416Sbostic { 105164416Sbostic 105264416Sbostic if (flag & IO_NDELAY) 105364416Sbostic ttyflush(tp, FREAD | FWRITE); 105464416Sbostic else 105564416Sbostic ttywflush(tp); 105664416Sbostic return (0); 105764416Sbostic } 105864416Sbostic 105964416Sbostic /* 106064416Sbostic * Handle modem control transition on a tty. 106164416Sbostic * Flag indicates new state of carrier. 106264416Sbostic * Returns 0 if the line should be turned off, otherwise 1. 106364416Sbostic */ 106464416Sbostic int 106564416Sbostic ttymodem(tp, flag) 106664416Sbostic register struct tty *tp; 106764416Sbostic int flag; 106864416Sbostic { 106964416Sbostic 107064416Sbostic if (!ISSET(tp->t_state, TS_WOPEN) && ISSET(tp->t_cflag, MDMBUF)) { 107164416Sbostic /* 107264416Sbostic * MDMBUF: do flow control according to carrier flag 107364416Sbostic */ 107464416Sbostic if (flag) { 107564416Sbostic CLR(tp->t_state, TS_TTSTOP); 107664416Sbostic ttstart(tp); 107764416Sbostic } else if (!ISSET(tp->t_state, TS_TTSTOP)) { 107864416Sbostic SET(tp->t_state, TS_TTSTOP); 107964416Sbostic #ifdef sun4c /* XXX */ 108064416Sbostic (*tp->t_stop)(tp, 0); 108164416Sbostic #else 108264416Sbostic (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0); 108364416Sbostic #endif 108464416Sbostic } 108564416Sbostic } else if (flag == 0) { 108664416Sbostic /* 108764416Sbostic * Lost carrier. 108864416Sbostic */ 108964416Sbostic CLR(tp->t_state, TS_CARR_ON); 109064416Sbostic if (ISSET(tp->t_state, TS_ISOPEN) && 109164416Sbostic !ISSET(tp->t_cflag, CLOCAL)) { 109264416Sbostic if (tp->t_session && tp->t_session->s_leader) 109364416Sbostic psignal(tp->t_session->s_leader, SIGHUP); 109464416Sbostic ttyflush(tp, FREAD | FWRITE); 109564416Sbostic return (0); 109664416Sbostic } 109764416Sbostic } else { 109864416Sbostic /* 109964416Sbostic * Carrier now on. 110064416Sbostic */ 110164416Sbostic SET(tp->t_state, TS_CARR_ON); 110264416Sbostic ttwakeup(tp); 110364416Sbostic } 110464416Sbostic return (1); 110564416Sbostic } 110664416Sbostic 110764416Sbostic /* 110864416Sbostic * Default modem control routine (for other line disciplines). 110964416Sbostic * Return argument flag, to turn off device on carrier drop. 111064416Sbostic */ 111164416Sbostic int 111264416Sbostic nullmodem(tp, flag) 111364416Sbostic register struct tty *tp; 111464416Sbostic int flag; 111564416Sbostic { 111664416Sbostic 111764416Sbostic if (flag) 111864416Sbostic SET(tp->t_state, TS_CARR_ON); 111964416Sbostic else { 112064416Sbostic CLR(tp->t_state, TS_CARR_ON); 112164416Sbostic if (!ISSET(tp->t_cflag, CLOCAL)) { 112264416Sbostic if (tp->t_session && tp->t_session->s_leader) 112364416Sbostic psignal(tp->t_session->s_leader, SIGHUP); 112464416Sbostic return (0); 112564416Sbostic } 112664416Sbostic } 112764416Sbostic return (1); 112864416Sbostic } 112964416Sbostic 113064416Sbostic /* 113164416Sbostic * Reinput pending characters after state switch 113264416Sbostic * call at spltty(). 113364416Sbostic */ 113464416Sbostic void 113564416Sbostic ttypend(tp) 113664416Sbostic register struct tty *tp; 113764416Sbostic { 113864416Sbostic struct clist tq; 113964416Sbostic register c; 114064416Sbostic 114164416Sbostic CLR(tp->t_lflag, PENDIN); 114264416Sbostic SET(tp->t_state, TS_TYPEN); 114364416Sbostic tq = tp->t_rawq; 114464416Sbostic tp->t_rawq.c_cc = 0; 114564416Sbostic tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0; 114664416Sbostic while ((c = getc(&tq)) >= 0) 114764416Sbostic ttyinput(c, tp); 114864416Sbostic CLR(tp->t_state, TS_TYPEN); 114964416Sbostic } 115064416Sbostic 115164416Sbostic /* 115249380Skarels * Process a read call on a tty device. 11537502Sroot */ 115464416Sbostic int 115537584Smarc ttread(tp, uio, flag) 11567625Ssam register struct tty *tp; 11577722Swnj struct uio *uio; 115852485Storek int flag; 11597502Sroot { 11607502Sroot register struct clist *qp; 116135811Smarc register int c; 116241383Smarc register long lflag; 116335811Smarc register u_char *cc = tp->t_cc; 116447545Skarels register struct proc *p = curproc; 11659859Ssam int s, first, error = 0; 11667502Sroot 116764416Sbostic loop: lflag = tp->t_lflag; 116837584Smarc s = spltty(); 11699578Ssam /* 117064416Sbostic * take pending input first 11719578Ssam */ 117264416Sbostic if (ISSET(lflag, PENDIN)) 11737502Sroot ttypend(tp); 11749859Ssam splx(s); 117540712Skarels 11769578Ssam /* 11779578Ssam * Hang process if it's in the background. 11789578Ssam */ 117947545Skarels if (isbackground(p, tp)) { 118047545Skarels if ((p->p_sigignore & sigmask(SIGTTIN)) || 118147545Skarels (p->p_sigmask & sigmask(SIGTTIN)) || 118264416Sbostic p->p_flag & SPPWAIT || p->p_pgrp->pg_jobc == 0) 11838520Sroot return (EIO); 118447545Skarels pgsignal(p->p_pgrp, SIGTTIN, 1); 118564416Sbostic if (error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0)) 118640712Skarels return (error); 118723165Sbloom goto loop; 11887502Sroot } 118940712Skarels 11909578Ssam /* 119135811Smarc * If canonical, use the canonical queue, 119235811Smarc * else use the raw queue. 119337584Smarc * 119447545Skarels * (should get rid of clists...) 11959578Ssam */ 119664416Sbostic qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq; 119740712Skarels 11989578Ssam /* 119940712Skarels * If there is no input, sleep on rawq 120040712Skarels * awaiting hardware receipt and notification. 120140712Skarels * If we have data, we don't need to check for carrier. 12029578Ssam */ 120317545Skarels s = spltty(); 12049578Ssam if (qp->c_cc <= 0) { 120540712Skarels int carrier; 120640712Skarels 120764416Sbostic carrier = ISSET(tp->t_state, TS_CARR_ON) || 120864416Sbostic ISSET(tp->t_cflag, CLOCAL); 120964416Sbostic if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) { 12109859Ssam splx(s); 121140712Skarels return (0); /* EOF */ 12127502Sroot } 121337728Smckusick if (flag & IO_NDELAY) { 121437584Smarc splx(s); 121537584Smarc return (EWOULDBLOCK); 121637584Smarc } 121764416Sbostic error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH, 121840712Skarels carrier ? ttyin : ttopen, 0); 12199859Ssam splx(s); 122043377Smarc if (error) 122140712Skarels return (error); 12229578Ssam goto loop; 12239578Ssam } 12249859Ssam splx(s); 122540712Skarels 12269578Ssam /* 122735811Smarc * Input present, check for input mapping and processing. 12289578Ssam */ 12299578Ssam first = 1; 12309578Ssam while ((c = getc(qp)) >= 0) { 12319578Ssam /* 123235811Smarc * delayed suspend (^Y) 12339578Ssam */ 123464416Sbostic if (CCEQ(cc[VDSUSP], c) && ISSET(lflag, ISIG)) { 123542882Smarc pgsignal(tp->t_pgrp, SIGTSTP, 1); 12369578Ssam if (first) { 123764416Sbostic if (error = ttysleep(tp, 123864416Sbostic &lbolt, TTIPRI | PCATCH, ttybg, 0)) 123940712Skarels break; 12409578Ssam goto loop; 12419578Ssam } 12429578Ssam break; 12437502Sroot } 12449578Ssam /* 124535811Smarc * Interpret EOF only in canonical mode. 12469578Ssam */ 124764416Sbostic if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON)) 12489578Ssam break; 12499578Ssam /* 12509578Ssam * Give user character. 12519578Ssam */ 125240712Skarels error = ureadc(c, uio); 12539578Ssam if (error) 12549578Ssam break; 125514938Smckusick if (uio->uio_resid == 0) 12569578Ssam break; 12579578Ssam /* 125835811Smarc * In canonical mode check for a "break character" 12599578Ssam * marking the end of a "line of input". 12609578Ssam */ 126164416Sbostic if (ISSET(lflag, ICANON) && TTBREAKC(c)) 12629578Ssam break; 12639578Ssam first = 0; 12647502Sroot } 12659578Ssam /* 12669578Ssam * Look to unblock output now that (presumably) 12679578Ssam * the input queue has gone down. 12689578Ssam */ 126952485Storek s = spltty(); 127064416Sbostic if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) { 127147545Skarels if (cc[VSTART] != _POSIX_VDISABLE && 127247545Skarels putc(cc[VSTART], &tp->t_outq) == 0) { 127364416Sbostic CLR(tp->t_state, TS_TBLOCK); 12747502Sroot ttstart(tp); 12757502Sroot } 127635811Smarc } 127752485Storek splx(s); 12788520Sroot return (error); 12797502Sroot } 12807502Sroot 12817502Sroot /* 128264416Sbostic * Check the output queue on tp for space for a kernel message (from uprintf 128364416Sbostic * or tprintf). Allow some space over the normal hiwater mark so we don't 128464416Sbostic * lose messages due to normal flow control, but don't let the tty run amok. 128564416Sbostic * Sleeps here are not interruptible, but we return prematurely if new signals 128664416Sbostic * arrive. 128725391Skarels */ 128864416Sbostic int 128925391Skarels ttycheckoutq(tp, wait) 129025391Skarels register struct tty *tp; 129125391Skarels int wait; 129225391Skarels { 129330695Skarels int hiwat, s, oldsig; 129425391Skarels 129535811Smarc hiwat = tp->t_hiwat; 129625391Skarels s = spltty(); 129752485Storek oldsig = wait ? curproc->p_sig : 0; 129825391Skarels if (tp->t_outq.c_cc > hiwat + 200) 129929946Skarels while (tp->t_outq.c_cc > hiwat) { 130029946Skarels ttstart(tp); 130147545Skarels if (wait == 0 || curproc->p_sig != oldsig) { 130229946Skarels splx(s); 130329946Skarels return (0); 130429946Skarels } 130554782Storek timeout((void (*)__P((void *)))wakeup, 130654782Storek (void *)&tp->t_outq, hz); 130764416Sbostic SET(tp->t_state, TS_ASLEEP); 130830695Skarels sleep((caddr_t)&tp->t_outq, PZERO - 1); 130925391Skarels } 131025391Skarels splx(s); 131125391Skarels return (1); 131225391Skarels } 131325391Skarels 131425391Skarels /* 131549380Skarels * Process a write call on a tty device. 13167502Sroot */ 131764416Sbostic int 131837584Smarc ttwrite(tp, uio, flag) 13197625Ssam register struct tty *tp; 13209578Ssam register struct uio *uio; 132152485Storek int flag; 13227502Sroot { 13237502Sroot register char *cp; 132464416Sbostic register int cc, ce; 132564416Sbostic register struct proc *p; 13269578Ssam int i, hiwat, cnt, error, s; 13277502Sroot char obuf[OBUFSIZ]; 13287502Sroot 132935811Smarc hiwat = tp->t_hiwat; 13309578Ssam cnt = uio->uio_resid; 13319578Ssam error = 0; 13327502Sroot loop: 133337584Smarc s = spltty(); 133464416Sbostic if (!ISSET(tp->t_state, TS_CARR_ON) && 133564416Sbostic !ISSET(tp->t_cflag, CLOCAL)) { 133664416Sbostic if (ISSET(tp->t_state, TS_ISOPEN)) { 133737584Smarc splx(s); 133837584Smarc return (EIO); 133937728Smckusick } else if (flag & IO_NDELAY) { 134037584Smarc splx(s); 134140712Skarels error = EWOULDBLOCK; 134240712Skarels goto out; 134337584Smarc } else { 134464416Sbostic /* Sleep awaiting carrier. */ 134564416Sbostic error = ttysleep(tp, 134664416Sbostic &tp->t_rawq, TTIPRI | PCATCH,ttopen, 0); 134737584Smarc splx(s); 134843377Smarc if (error) 134940712Skarels goto out; 135037584Smarc goto loop; 135137584Smarc } 135237584Smarc } 135337584Smarc splx(s); 13549578Ssam /* 13559578Ssam * Hang the process if it's in the background. 13569578Ssam */ 135764416Sbostic p = curproc; 135864416Sbostic if (isbackground(p, tp) && 135964416Sbostic ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & SPPWAIT) == 0 && 136047545Skarels (p->p_sigignore & sigmask(SIGTTOU)) == 0 && 136147545Skarels (p->p_sigmask & sigmask(SIGTTOU)) == 0 && 136247545Skarels p->p_pgrp->pg_jobc) { 136347545Skarels pgsignal(p->p_pgrp, SIGTTOU, 1); 136464416Sbostic if (error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0)) 136540712Skarels goto out; 136621776Sbloom goto loop; 13677502Sroot } 13689578Ssam /* 136964416Sbostic * Process the user's data in at most OBUFSIZ chunks. Perform any 137064416Sbostic * output translation. Keep track of high water mark, sleep on 137164416Sbostic * overflow awaiting device aid in acquiring new space. 13729578Ssam */ 137364416Sbostic for (cc = 0; uio->uio_resid > 0 || cc > 0;) { 137464416Sbostic if (ISSET(tp->t_lflag, FLUSHO)) { 137540712Skarels uio->uio_resid = 0; 137640712Skarels return (0); 137740712Skarels } 137840712Skarels if (tp->t_outq.c_cc > hiwat) 137932067Skarels goto ovhiwat; 13809578Ssam /* 138164416Sbostic * Grab a hunk of data from the user, unless we have some 138264416Sbostic * leftover from last time. 13839578Ssam */ 13847822Sroot if (cc == 0) { 138540712Skarels cc = min(uio->uio_resid, OBUFSIZ); 138640712Skarels cp = obuf; 138740712Skarels error = uiomove(cp, cc, uio); 138840712Skarels if (error) { 138940712Skarels cc = 0; 139040712Skarels break; 139140712Skarels } 13927822Sroot } 13939578Ssam /* 13949578Ssam * If nothing fancy need be done, grab those characters we 13959578Ssam * can handle without any of ttyoutput's processing and 13969578Ssam * just transfer them to the output q. For those chars 13979578Ssam * which require special processing (as indicated by the 139864416Sbostic * bits in char_type), call ttyoutput. After processing 13999578Ssam * a hunk of data, look for FLUSHO so ^O's will take effect 14009578Ssam * immediately. 14019578Ssam */ 14029578Ssam while (cc > 0) { 140364416Sbostic if (!ISSET(tp->t_oflag, OPOST)) 14047502Sroot ce = cc; 14057502Sroot else { 140664416Sbostic ce = cc - scanc((u_int)cc, (u_char *)cp, 140764416Sbostic (u_char *)char_type, CCLASSMASK); 14089578Ssam /* 14099578Ssam * If ce is zero, then we're processing 14109578Ssam * a special character through ttyoutput. 14119578Ssam */ 14129578Ssam if (ce == 0) { 14137502Sroot tp->t_rocount = 0; 14147502Sroot if (ttyoutput(*cp, tp) >= 0) { 141564416Sbostic /* No Clists, wait a bit. */ 141664416Sbostic ttstart(tp); 141764416Sbostic if (error = ttysleep(tp, &lbolt, 141864416Sbostic TTOPRI | PCATCH, ttybuf, 0)) 141964416Sbostic break; 142064416Sbostic goto loop; 14217502Sroot } 142264416Sbostic cp++; 142364416Sbostic cc--; 142464416Sbostic if (ISSET(tp->t_lflag, FLUSHO) || 14259578Ssam tp->t_outq.c_cc > hiwat) 14267502Sroot goto ovhiwat; 14279578Ssam continue; 14287502Sroot } 14297502Sroot } 14309578Ssam /* 143164416Sbostic * A bunch of normal characters have been found. 143264416Sbostic * Transfer them en masse to the output queue and 14339578Ssam * continue processing at the top of the loop. 14349578Ssam * If there are any further characters in this 14359578Ssam * <= OBUFSIZ chunk, the first should be a character 14369578Ssam * requiring special handling by ttyoutput. 14379578Ssam */ 14387502Sroot tp->t_rocount = 0; 14399578Ssam i = b_to_q(cp, ce, &tp->t_outq); 14409578Ssam ce -= i; 1441*64530Sbostic tp->t_column += ce; 14429578Ssam cp += ce, cc -= ce, tk_nout += ce; 144335811Smarc tp->t_outcc += ce; 14449578Ssam if (i > 0) { 144564416Sbostic /* No Clists, wait a bit. */ 14467502Sroot ttstart(tp); 144764416Sbostic if (error = ttysleep(tp, 144864416Sbostic &lbolt, TTOPRI | PCATCH, ttybuf, 0)) 144940712Skarels break; 145021776Sbloom goto loop; 14517502Sroot } 145264416Sbostic if (ISSET(tp->t_lflag, FLUSHO) || 145364416Sbostic tp->t_outq.c_cc > hiwat) 145440712Skarels break; 14557502Sroot } 145635811Smarc ttstart(tp); 14577502Sroot } 145840712Skarels out: 145940712Skarels /* 146064416Sbostic * If cc is nonzero, we leave the uio structure inconsistent, as the 146164416Sbostic * offset and iov pointers have moved forward, but it doesn't matter 146264416Sbostic * (the call will either return short or restart with a new uio). 146340712Skarels */ 146440712Skarels uio->uio_resid += cc; 14658520Sroot return (error); 146640712Skarels 14677502Sroot ovhiwat: 146832067Skarels ttstart(tp); 146932067Skarels s = spltty(); 14709578Ssam /* 147135811Smarc * This can only occur if FLUSHO is set in t_lflag, 147232067Skarels * or if ttstart/oproc is synchronous (or very fast). 14739578Ssam */ 14747502Sroot if (tp->t_outq.c_cc <= hiwat) { 14759578Ssam splx(s); 14767502Sroot goto loop; 14777502Sroot } 147837728Smckusick if (flag & IO_NDELAY) { 147917545Skarels splx(s); 148040712Skarels uio->uio_resid += cc; 148164416Sbostic return (uio->uio_resid == cnt ? EWOULDBLOCK : 0); 14827502Sroot } 148364416Sbostic SET(tp->t_state, TS_ASLEEP); 148464416Sbostic error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0); 14859578Ssam splx(s); 148643377Smarc if (error) 148740712Skarels goto out; 14887502Sroot goto loop; 14897502Sroot } 14907502Sroot 14917502Sroot /* 14927502Sroot * Rubout one character from the rawq of tp 14937502Sroot * as cleanly as possible. 14947502Sroot */ 149564416Sbostic void 14967502Sroot ttyrub(c, tp) 149764416Sbostic register int c; 14987625Ssam register struct tty *tp; 14997502Sroot { 15007502Sroot register char *cp; 15017502Sroot register int savecol; 150264416Sbostic int tabc, s; 15037502Sroot 150464416Sbostic if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC)) 15057502Sroot return; 150664416Sbostic CLR(tp->t_lflag, FLUSHO); 150764416Sbostic if (ISSET(tp->t_lflag, ECHOE)) { 15087502Sroot if (tp->t_rocount == 0) { 15097502Sroot /* 15107502Sroot * Screwed by ttwrite; retype 15117502Sroot */ 15127502Sroot ttyretype(tp); 15137502Sroot return; 15147502Sroot } 151564416Sbostic if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE)) 15167502Sroot ttyrubo(tp, 2); 151749380Skarels else switch (CCLASS(c &= TTY_CHARMASK)) { 15187502Sroot case ORDINARY: 151935811Smarc ttyrubo(tp, 1); 15207502Sroot break; 15217502Sroot case BACKSPACE: 15227502Sroot case CONTROL: 152364416Sbostic case NEWLINE: 15247502Sroot case RETURN: 152564416Sbostic case VTAB: 152664416Sbostic if (ISSET(tp->t_lflag, ECHOCTL)) 15277502Sroot ttyrubo(tp, 2); 15287502Sroot break; 152964416Sbostic case TAB: 15307502Sroot if (tp->t_rocount < tp->t_rawq.c_cc) { 15317502Sroot ttyretype(tp); 15327502Sroot return; 15337502Sroot } 153417545Skarels s = spltty(); 1535*64530Sbostic savecol = tp->t_column; 153664416Sbostic SET(tp->t_state, TS_CNTTB); 153764416Sbostic SET(tp->t_lflag, FLUSHO); 1538*64530Sbostic tp->t_column = tp->t_rocol; 15399578Ssam cp = tp->t_rawq.c_cf; 154039407Smarc if (cp) 154164416Sbostic tabc = *cp; /* XXX FIX NEXTC */ 154264416Sbostic for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc)) 154364416Sbostic ttyecho(tabc, tp); 154464416Sbostic CLR(tp->t_lflag, FLUSHO); 154564416Sbostic CLR(tp->t_state, TS_CNTTB); 15467502Sroot splx(s); 154764416Sbostic 154864416Sbostic /* savecol will now be length of the tab. */ 1549*64530Sbostic savecol -= tp->t_column; 1550*64530Sbostic tp->t_column += savecol; 15517502Sroot if (savecol > 8) 15527502Sroot savecol = 8; /* overflow screw */ 15537502Sroot while (--savecol >= 0) 155464416Sbostic (void)ttyoutput('\b', tp); 15557502Sroot break; 155664416Sbostic default: /* XXX */ 155764416Sbostic #define PANICSTR "ttyrub: would panic c = %d, val = %d\n" 155864416Sbostic (void)printf(PANICSTR, c, CCLASS(c)); 155964416Sbostic #ifdef notdef 156064416Sbostic panic(PANICSTR, c, CCLASS(c)); 156164416Sbostic #endif 156235811Smarc } 156364416Sbostic } else if (ISSET(tp->t_lflag, ECHOPRT)) { 156464416Sbostic if (!ISSET(tp->t_state, TS_ERASE)) { 156564416Sbostic SET(tp->t_state, TS_ERASE); 156664416Sbostic (void)ttyoutput('\\', tp); 15677502Sroot } 15687502Sroot ttyecho(c, tp); 15697502Sroot } else 157035811Smarc ttyecho(tp->t_cc[VERASE], tp); 157164416Sbostic --tp->t_rocount; 15727502Sroot } 15737502Sroot 15747502Sroot /* 157564416Sbostic * Back over cnt characters, erasing them. 15767502Sroot */ 157764416Sbostic static void 15787502Sroot ttyrubo(tp, cnt) 15797625Ssam register struct tty *tp; 15807625Ssam int cnt; 15817502Sroot { 15827502Sroot 158364416Sbostic while (cnt-- > 0) { 158464416Sbostic (void)ttyoutput('\b', tp); 158564416Sbostic (void)ttyoutput(' ', tp); 158664416Sbostic (void)ttyoutput('\b', tp); 158764416Sbostic } 15887502Sroot } 15897502Sroot 15907502Sroot /* 159164416Sbostic * ttyretype -- 159264416Sbostic * Reprint the rawq line. Note, it is assumed that c_cc has already 159364416Sbostic * been checked. 15947502Sroot */ 159564416Sbostic void 15967502Sroot ttyretype(tp) 15977625Ssam register struct tty *tp; 15987502Sroot { 15997502Sroot register char *cp; 160035811Smarc int s, c; 16017502Sroot 160264416Sbostic /* Echo the reprint character. */ 160335811Smarc if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE) 160435811Smarc ttyecho(tp->t_cc[VREPRINT], tp); 160564416Sbostic 160664416Sbostic (void)ttyoutput('\n', tp); 160764416Sbostic 160864416Sbostic /* 160964416Sbostic * XXX 161064416Sbostic * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE 161164416Sbostic * BIT OF FIRST CHAR. 161264416Sbostic */ 161317545Skarels s = spltty(); 161464416Sbostic for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0); 161564416Sbostic cp != NULL; cp = nextc(&tp->t_canq, cp, &c)) 161635811Smarc ttyecho(c, tp); 161764416Sbostic for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0); 161864416Sbostic cp != NULL; cp = nextc(&tp->t_rawq, cp, &c)) 161935811Smarc ttyecho(c, tp); 162064416Sbostic CLR(tp->t_state, TS_ERASE); 16217502Sroot splx(s); 162264416Sbostic 16237502Sroot tp->t_rocount = tp->t_rawq.c_cc; 16247502Sroot tp->t_rocol = 0; 16257502Sroot } 16267502Sroot 16277502Sroot /* 162835811Smarc * Echo a typed character to the terminal. 16297502Sroot */ 163064416Sbostic static void 16317502Sroot ttyecho(c, tp) 163264416Sbostic register int c; 16337625Ssam register struct tty *tp; 16347502Sroot { 163564416Sbostic 163664416Sbostic if (!ISSET(tp->t_state, TS_CNTTB)) 163764416Sbostic CLR(tp->t_lflag, FLUSHO); 163864416Sbostic if ((!ISSET(tp->t_lflag, ECHO) && 163964416Sbostic (!ISSET(tp->t_lflag, ECHONL) || c == '\n')) || 164064416Sbostic ISSET(tp->t_lflag, EXTPROC)) 16417502Sroot return; 164264416Sbostic if (ISSET(tp->t_lflag, ECHOCTL) && 164364416Sbostic ((c & TTY_CHARMASK) <= 037 && c != '\t' && c != '\n' || 164464416Sbostic (c & TTY_CHARMASK) == 0177)) { 164564416Sbostic (void)ttyoutput('^', tp); 164664416Sbostic c &= TTY_CHARMASK; 164764416Sbostic if (c == 0177) 164864416Sbostic c = '?'; 164964416Sbostic else 165064416Sbostic c += 'A' - 1; 16517502Sroot } 165264416Sbostic (void)ttyoutput(c, tp); 16537502Sroot } 16547502Sroot 16557502Sroot /* 165649380Skarels * Wake up any readers on a tty. 165749380Skarels */ 165864416Sbostic void 16597502Sroot ttwakeup(tp) 166047545Skarels register struct tty *tp; 16617502Sroot { 16627502Sroot 166352522Smckusick selwakeup(&tp->t_rsel); 166464416Sbostic if (ISSET(tp->t_state, TS_ASYNC)) 166564416Sbostic pgsignal(tp->t_pgrp, SIGIO, 1); 16667502Sroot wakeup((caddr_t)&tp->t_rawq); 16677502Sroot } 166835811Smarc 166935811Smarc /* 167048439Skarels * Look up a code for a specified speed in a conversion table; 167148439Skarels * used by drivers to map software speed values to hardware parameters. 167248439Skarels */ 167364416Sbostic int 167448439Skarels ttspeedtab(speed, table) 167552485Storek int speed; 167648439Skarels register struct speedtab *table; 167748439Skarels { 167848439Skarels 167948439Skarels for ( ; table->sp_speed != -1; table++) 168048439Skarels if (table->sp_speed == speed) 168148439Skarels return (table->sp_code); 168248439Skarels return (-1); 168348439Skarels } 168448439Skarels 168548439Skarels /* 168664416Sbostic * Set tty hi and low water marks. 168735811Smarc * 168835811Smarc * Try to arrange the dynamics so there's about one second 168935811Smarc * from hi to low water. 169064416Sbostic * 169135811Smarc */ 169264416Sbostic void 169335811Smarc ttsetwater(tp) 169435811Smarc struct tty *tp; 169535811Smarc { 169664416Sbostic register int cps, x; 169735811Smarc 169864416Sbostic #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x)) 169964416Sbostic 170064416Sbostic cps = tp->t_ospeed / 10; 170164416Sbostic tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT); 170235811Smarc x += cps; 170364416Sbostic x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT); 170435811Smarc tp->t_hiwat = roundup(x, CBSIZE); 170564416Sbostic #undef CLAMP 170635811Smarc } 170735811Smarc 170839407Smarc /* 170939407Smarc * Report on state of foreground process group. 171039407Smarc */ 171164416Sbostic void 171239407Smarc ttyinfo(tp) 171349907Sbostic register struct tty *tp; 171439407Smarc { 171549907Sbostic register struct proc *p, *pick; 171641177Smarc struct timeval utime, stime; 171749907Sbostic int tmp; 171839407Smarc 171964416Sbostic if (ttycheckoutq(tp,0) == 0) 172039407Smarc return; 172149907Sbostic 172249907Sbostic /* Print load average. */ 172352666Smckusick tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT; 172449907Sbostic ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100); 172549907Sbostic 172639555Smarc if (tp->t_session == NULL) 172749907Sbostic ttyprintf(tp, "not a controlling terminal\n"); 172841177Smarc else if (tp->t_pgrp == NULL) 172949907Sbostic ttyprintf(tp, "no foreground process group\n"); 173041177Smarc else if ((p = tp->t_pgrp->pg_mem) == NULL) 173149907Sbostic ttyprintf(tp, "empty foreground process group\n"); 173239407Smarc else { 173349907Sbostic /* Pick interesting process. */ 173449907Sbostic for (pick = NULL; p != NULL; p = p->p_pgrpnxt) 173541177Smarc if (proc_compare(pick, p)) 173641177Smarc pick = p; 173749907Sbostic 173849907Sbostic ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid, 173949907Sbostic pick->p_stat == SRUN ? "running" : 174049907Sbostic pick->p_wmesg ? pick->p_wmesg : "iowait"); 174149907Sbostic 174254782Storek calcru(pick, &utime, &stime, NULL); 174339407Smarc 174449907Sbostic /* Print user time. */ 174549907Sbostic ttyprintf(tp, "%d.%02du ", 174649907Sbostic utime.tv_sec, (utime.tv_usec + 5000) / 10000); 174741177Smarc 174849907Sbostic /* Print system time. */ 174949907Sbostic ttyprintf(tp, "%d.%02ds ", 175049907Sbostic stime.tv_sec, (stime.tv_usec + 5000) / 10000); 175149907Sbostic 175249907Sbostic #define pgtok(a) (((a) * NBPG) / 1024) 175349907Sbostic /* Print percentage cpu, resident set size. */ 175449907Sbostic tmp = pick->p_pctcpu * 10000 + FSCALE / 2 >> FSHIFT; 175549907Sbostic ttyprintf(tp, "%d%% %dk\n", 175652485Storek tmp / 100, 175752485Storek pick->p_stat == SIDL || pick->p_stat == SZOMB ? 0 : 175852485Storek pgtok(pick->p_vmspace->vm_rssize)); 175941177Smarc } 176049907Sbostic tp->t_rocount = 0; /* so pending input will be retyped if BS */ 176141177Smarc } 176241177Smarc 176341177Smarc /* 176441177Smarc * Returns 1 if p2 is "better" than p1 176541177Smarc * 176641177Smarc * The algorithm for picking the "interesting" process is thus: 176741177Smarc * 176841177Smarc * 1) (Only foreground processes are eligable - implied) 176941177Smarc * 2) Runnable processes are favored over anything 177041177Smarc * else. The runner with the highest cpu 177141177Smarc * utilization is picked (p_cpu). Ties are 177241177Smarc * broken by picking the highest pid. 177341177Smarc * 3 Next, the sleeper with the shortest sleep 177441177Smarc * time is favored. With ties, we pick out 177541177Smarc * just "short-term" sleepers (SSINTR == 0). 177641177Smarc * Further ties are broken by picking the highest 177741177Smarc * pid. 177841177Smarc * 177941177Smarc */ 178041177Smarc #define isrun(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL)) 178145723Smckusick #define TESTAB(a, b) ((a)<<1 | (b)) 178245723Smckusick #define ONLYA 2 178345723Smckusick #define ONLYB 1 178445723Smckusick #define BOTH 3 178549907Sbostic static int 178641177Smarc proc_compare(p1, p2) 178741177Smarc register struct proc *p1, *p2; 178841177Smarc { 178941177Smarc 179041177Smarc if (p1 == NULL) 179141177Smarc return (1); 179241177Smarc /* 179341177Smarc * see if at least one of them is runnable 179441177Smarc */ 179545723Smckusick switch (TESTAB(isrun(p1), isrun(p2))) { 179645723Smckusick case ONLYA: 179745723Smckusick return (0); 179845723Smckusick case ONLYB: 179941177Smarc return (1); 180045723Smckusick case BOTH: 180141177Smarc /* 180241177Smarc * tie - favor one with highest recent cpu utilization 180341177Smarc */ 180441177Smarc if (p2->p_cpu > p1->p_cpu) 180541177Smarc return (1); 180641177Smarc if (p1->p_cpu > p2->p_cpu) 180741177Smarc return (0); 180841177Smarc return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 180941177Smarc } 181045723Smckusick /* 181145723Smckusick * weed out zombies 181245723Smckusick */ 181345723Smckusick switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) { 181445723Smckusick case ONLYA: 181545723Smckusick return (1); 181645723Smckusick case ONLYB: 181745723Smckusick return (0); 181845723Smckusick case BOTH: 181945723Smckusick return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 182045723Smckusick } 182164416Sbostic /* 182241177Smarc * pick the one with the smallest sleep time 182341177Smarc */ 182441177Smarc if (p2->p_slptime > p1->p_slptime) 182541177Smarc return (0); 182641177Smarc if (p1->p_slptime > p2->p_slptime) 182741177Smarc return (1); 182841177Smarc /* 182941177Smarc * favor one sleeping in a non-interruptible sleep 183041177Smarc */ 183164416Sbostic if (p1->p_flag & SSINTR && (p2->p_flag & SSINTR) == 0) 183241177Smarc return (1); 183364416Sbostic if (p2->p_flag & SSINTR && (p1->p_flag & SSINTR) == 0) 183441177Smarc return (0); 183547545Skarels return (p2->p_pid > p1->p_pid); /* tie - return highest pid */ 183641177Smarc } 183745723Smckusick 183839555Smarc /* 183939555Smarc * Output char to tty; console putchar style. 184039555Smarc */ 184164416Sbostic int 184239555Smarc tputchar(c, tp) 184339555Smarc int c; 184439555Smarc struct tty *tp; 184539555Smarc { 184664416Sbostic register int s; 184739555Smarc 184864416Sbostic s = spltty(); 184964416Sbostic if (ISSET(tp->t_state, 185064416Sbostic TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) { 185139555Smarc splx(s); 185264416Sbostic return (-1); 185339555Smarc } 185464416Sbostic if (c == '\n') 185564416Sbostic (void)ttyoutput('\r', tp); 185664416Sbostic (void)ttyoutput(c, tp); 185764416Sbostic ttstart(tp); 185839555Smarc splx(s); 185964416Sbostic return (0); 186039555Smarc } 186143377Smarc 186244419Smarc /* 186364416Sbostic * Sleep on chan, returning ERESTART if tty changed while we napped and 186464416Sbostic * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If 186564416Sbostic * the tty is revoked, restarting a pending call will redo validation done 186664416Sbostic * at the start of the call. 186744419Smarc */ 186864416Sbostic int 186943377Smarc ttysleep(tp, chan, pri, wmesg, timo) 187043377Smarc struct tty *tp; 187164416Sbostic void *chan; 187264416Sbostic int pri, timo; 187343377Smarc char *wmesg; 187443377Smarc { 187543377Smarc int error; 187664416Sbostic short gen; 187743377Smarc 187864416Sbostic gen = tp->t_gen; 187943377Smarc if (error = tsleep(chan, pri, wmesg, timo)) 188043377Smarc return (error); 188164416Sbostic return (tp->t_gen == gen ? 0 : ERESTART); 188243377Smarc } 1883