155989Sbostic /*- 262546Sbostic * Copyright (c) 1992, 1993 362546Sbostic * The Regents of the University of California. All rights reserved. 455989Sbostic * 555989Sbostic * %sccs.include.redist.c% 655989Sbostic */ 755989Sbostic 855989Sbostic #ifndef lint 9*65314Sbostic static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 01/02/94"; 1055989Sbostic #endif /* not lint */ 1155989Sbostic 1255989Sbostic #include <sys/ioctl.h> 1355989Sbostic 1455989Sbostic #include <curses.h> 1555989Sbostic #include <termios.h> 1655989Sbostic #include <unistd.h> 1755989Sbostic 1860627Sbostic /* 1960627Sbostic * In general, curses should leave tty hardware settings alone (speed, parity, 2060627Sbostic * word size). This is most easily done in BSD by using TCSASOFT on all 2160627Sbostic * tcsetattr calls. On other systems, it would be better to get and restore 2260627Sbostic * those attributes at each change, or at least when stopped and restarted. 2360627Sbostic * See also the comments in getterm(). 2460627Sbostic */ 2560627Sbostic #ifdef TCSASOFT 2662545Sbostic int __tcaction = 1; /* Ignore hardware settings. */ 2760627Sbostic #else 2862545Sbostic int __tcaction = 0; 2960627Sbostic #endif 3060627Sbostic 3161229Smarc struct termios __orig_termios, __baset; 3261229Smarc static struct termios cbreakt, rawt, *curt; 3355989Sbostic static int useraw; 3455989Sbostic 3560616Sbostic #ifndef OXTABS 3660616Sbostic #ifdef XTABS /* SMI uses XTABS. */ 3760616Sbostic #define OXTABS XTABS 3860616Sbostic #else 3960616Sbostic #define OXTABS 0 4060616Sbostic #endif 4160616Sbostic #endif 4260616Sbostic 4355989Sbostic /* 4455989Sbostic * gettmode -- 4555989Sbostic * Do terminal type initialization. 4655989Sbostic */ 4755989Sbostic int 4855989Sbostic gettmode() 4955989Sbostic { 5057286Sbostic useraw = 0; 5157286Sbostic 5257716Sbostic if (tcgetattr(STDIN_FILENO, &__orig_termios)) 5357472Sbostic return (ERR); 5455989Sbostic 5561229Smarc __baset = __orig_termios; 5661229Smarc __baset.c_oflag &= ~OXTABS; 5755989Sbostic 5861229Smarc GT = 0; /* historical. was used before we wired OXTABS off */ 5961229Smarc NONL = (__baset.c_oflag & ONLCR) == 0; 6060619Sbostic 6160619Sbostic /* 6260619Sbostic * XXX 6360619Sbostic * System V and SMI systems overload VMIN and VTIME, such that 6460619Sbostic * VMIN is the same as the VEOF element, and VTIME is the same 6560619Sbostic * as the VEOL element. This means that, if VEOF was ^D, the 6660619Sbostic * default VMIN is 4. Majorly stupid. 6760619Sbostic */ 6861229Smarc cbreakt = __baset; 6960627Sbostic cbreakt.c_lflag &= ~ICANON; 7060627Sbostic cbreakt.c_cc[VMIN] = 1; 7160627Sbostic cbreakt.c_cc[VTIME] = 0; 7255989Sbostic 7360627Sbostic rawt = cbreakt; 7460627Sbostic rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON); 7560627Sbostic rawt.c_oflag &= ~OPOST; 7660627Sbostic rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 7762545Sbostic 7860627Sbostic /* 7960627Sbostic * In general, curses should leave hardware-related settings alone. 8060627Sbostic * This includes parity and word size. Older versions set the tty 8160627Sbostic * to 8 bits, no parity in raw(), but this is considered to be an 8260627Sbostic * artifact of the old tty interface. If it's desired to change 8362545Sbostic * parity and word size, the TCSASOFT bit has to be removed from the 8462545Sbostic * calls that switch to/from "raw" mode. 8560627Sbostic */ 8662545Sbostic if (!__tcaction) { 8762545Sbostic rawt.c_iflag &= ~ISTRIP; 8862545Sbostic rawt.c_cflag &= ~(CSIZE|PARENB); 8962545Sbostic rawt.c_cflag |= CS8; 9062545Sbostic } 9160627Sbostic 9261229Smarc curt = &__baset; 9362545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 9462545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 9555989Sbostic } 9655989Sbostic 9755989Sbostic int 9855989Sbostic raw() 9955989Sbostic { 10055989Sbostic useraw = __pfast = __rawmode = 1; 10160627Sbostic curt = &rawt; 10262545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 10362545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 10455989Sbostic } 10555989Sbostic 10655989Sbostic int 10755989Sbostic noraw() 10855989Sbostic { 10955989Sbostic useraw = __pfast = __rawmode = 0; 11061229Smarc curt = &__baset; 11162545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 11262545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 11355989Sbostic } 11455989Sbostic 11555989Sbostic int 11655989Sbostic cbreak() 11755989Sbostic { 11855989Sbostic 11955989Sbostic __rawmode = 1; 12060627Sbostic curt = useraw ? &rawt : &cbreakt; 12162545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 12262545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 12355989Sbostic } 12455989Sbostic 12555989Sbostic int 12655989Sbostic nocbreak() 12755989Sbostic { 12855989Sbostic 12955989Sbostic __rawmode = 0; 13061229Smarc curt = useraw ? &rawt : &__baset; 13162545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 13262545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 13355989Sbostic } 13455989Sbostic 13555989Sbostic int 13655989Sbostic echo() 13755989Sbostic { 13855989Sbostic rawt.c_lflag |= ECHO; 13960627Sbostic cbreakt.c_lflag |= ECHO; 14061229Smarc __baset.c_lflag |= ECHO; 14155989Sbostic 14255989Sbostic __echoit = 1; 14362545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 14462545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 14555989Sbostic } 14655989Sbostic 14755989Sbostic int 14855989Sbostic noecho() 14955989Sbostic { 15055989Sbostic rawt.c_lflag &= ~ECHO; 15160627Sbostic cbreakt.c_lflag &= ~ECHO; 15261229Smarc __baset.c_lflag &= ~ECHO; 15355989Sbostic 15455989Sbostic __echoit = 0; 15562545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 15662545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 15755989Sbostic } 15855989Sbostic 15955989Sbostic int 16055989Sbostic nl() 16155989Sbostic { 16255989Sbostic rawt.c_iflag |= ICRNL; 16355989Sbostic rawt.c_oflag |= ONLCR; 16460627Sbostic cbreakt.c_iflag |= ICRNL; 16560627Sbostic cbreakt.c_oflag |= ONLCR; 16661229Smarc __baset.c_iflag |= ICRNL; 16761229Smarc __baset.c_oflag |= ONLCR; 16855989Sbostic 16955989Sbostic __pfast = __rawmode; 17062545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 17162545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 17255989Sbostic } 17355989Sbostic 17455989Sbostic int 17555989Sbostic nonl() 17655989Sbostic { 17755989Sbostic rawt.c_iflag &= ~ICRNL; 17855989Sbostic rawt.c_oflag &= ~ONLCR; 17960627Sbostic cbreakt.c_iflag &= ~ICRNL; 18060627Sbostic cbreakt.c_oflag &= ~ONLCR; 18161229Smarc __baset.c_iflag &= ~ICRNL; 18261229Smarc __baset.c_oflag &= ~ONLCR; 18355989Sbostic 18455989Sbostic __pfast = 1; 18562545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 18662545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt)); 18755989Sbostic } 18855989Sbostic 18959862Sbostic void 19059862Sbostic __startwin() 19159862Sbostic { 19259862Sbostic (void)fflush(stdout); 19359862Sbostic (void)setvbuf(stdout, NULL, _IOFBF, 0); 19459862Sbostic 19559862Sbostic tputs(TI, 0, __cputchar); 19659862Sbostic tputs(VS, 0, __cputchar); 19759862Sbostic } 19859862Sbostic 19955989Sbostic int 20055989Sbostic endwin() 20155989Sbostic { 202*65314Sbostic __restore_stophandler(); 203*65314Sbostic 20457716Sbostic if (curscr != NULL) { 20557716Sbostic if (curscr->flags & __WSTANDOUT) { 20657716Sbostic tputs(SE, 0, __cputchar); 20757716Sbostic curscr->flags &= ~__WSTANDOUT; 20857716Sbostic } 20960071Sbostic __mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0); 21055989Sbostic } 21155989Sbostic 21255989Sbostic (void)tputs(VE, 0, __cputchar); 21355989Sbostic (void)tputs(TE, 0, __cputchar); 21455989Sbostic (void)fflush(stdout); 21559862Sbostic (void)setvbuf(stdout, NULL, _IOLBF, 0); 21655989Sbostic 21762545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 21862545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios)); 21955989Sbostic } 22055989Sbostic 22155989Sbostic /* 22255989Sbostic * The following routines, savetty and resetty are completely useless and 22355989Sbostic * are left in only as stubs. If people actually use them they will almost 22455989Sbostic * certainly screw up the state of the world. 22555989Sbostic */ 22655989Sbostic static struct termios savedtty; 22755989Sbostic int 22855989Sbostic savetty() 22955989Sbostic { 23055989Sbostic return (tcgetattr(STDIN_FILENO, &savedtty)); 23155989Sbostic } 23255989Sbostic 23355989Sbostic int 23455989Sbostic resetty() 23555989Sbostic { 23662545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 23762545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty)); 23855989Sbostic } 239