155989Sbostic /*- 267082Sbostic * Copyright (c) 1992, 1993, 1994 362546Sbostic * The Regents of the University of California. All rights reserved. 455989Sbostic * 555989Sbostic * %sccs.include.redist.c% 655989Sbostic */ 755989Sbostic 855989Sbostic #ifndef lint 9*67174Sbostic static char sccsid[] = "@(#)tty.c 8.4 (Berkeley) 05/18/94"; 1055989Sbostic #endif /* not lint */ 1155989Sbostic 1255989Sbostic #include <termios.h> 1355989Sbostic #include <unistd.h> 1455989Sbostic 1567082Sbostic #include "curses.h" 1667082Sbostic 1760627Sbostic /* 1860627Sbostic * In general, curses should leave tty hardware settings alone (speed, parity, 1960627Sbostic * word size). This is most easily done in BSD by using TCSASOFT on all 2060627Sbostic * tcsetattr calls. On other systems, it would be better to get and restore 2160627Sbostic * those attributes at each change, or at least when stopped and restarted. 2260627Sbostic * See also the comments in getterm(). 2360627Sbostic */ 2460627Sbostic #ifdef TCSASOFT 2562545Sbostic int __tcaction = 1; /* Ignore hardware settings. */ 2660627Sbostic #else 2762545Sbostic int __tcaction = 0; 2860627Sbostic #endif 2960627Sbostic 3061229Smarc struct termios __orig_termios, __baset; 3161229Smarc static struct termios cbreakt, rawt, *curt; 3255989Sbostic static int useraw; 3355989Sbostic 3460616Sbostic #ifndef OXTABS 3560616Sbostic #ifdef XTABS /* SMI uses XTABS. */ 3660616Sbostic #define OXTABS XTABS 3760616Sbostic #else 3860616Sbostic #define OXTABS 0 3960616Sbostic #endif 4060616Sbostic #endif 4160616Sbostic 4255989Sbostic /* 4355989Sbostic * gettmode -- 4455989Sbostic * Do terminal type initialization. 4555989Sbostic */ 4655989Sbostic int 4755989Sbostic gettmode() 4855989Sbostic { 4957286Sbostic useraw = 0; 5057286Sbostic 5157716Sbostic if (tcgetattr(STDIN_FILENO, &__orig_termios)) 5257472Sbostic return (ERR); 5355989Sbostic 5461229Smarc __baset = __orig_termios; 5561229Smarc __baset.c_oflag &= ~OXTABS; 5655989Sbostic 5761229Smarc GT = 0; /* historical. was used before we wired OXTABS off */ 5861229Smarc NONL = (__baset.c_oflag & ONLCR) == 0; 5960619Sbostic 6060619Sbostic /* 6160619Sbostic * XXX 6260619Sbostic * System V and SMI systems overload VMIN and VTIME, such that 6360619Sbostic * VMIN is the same as the VEOF element, and VTIME is the same 6460619Sbostic * as the VEOL element. This means that, if VEOF was ^D, the 6560619Sbostic * default VMIN is 4. Majorly stupid. 6660619Sbostic */ 6761229Smarc cbreakt = __baset; 6860627Sbostic cbreakt.c_lflag &= ~ICANON; 6960627Sbostic cbreakt.c_cc[VMIN] = 1; 7060627Sbostic cbreakt.c_cc[VTIME] = 0; 7155989Sbostic 7260627Sbostic rawt = cbreakt; 7360627Sbostic rawt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|INLCR|IGNCR|ICRNL|IXON); 7460627Sbostic rawt.c_oflag &= ~OPOST; 7560627Sbostic rawt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); 7662545Sbostic 7760627Sbostic /* 7860627Sbostic * In general, curses should leave hardware-related settings alone. 7960627Sbostic * This includes parity and word size. Older versions set the tty 8060627Sbostic * to 8 bits, no parity in raw(), but this is considered to be an 8160627Sbostic * artifact of the old tty interface. If it's desired to change 8262545Sbostic * parity and word size, the TCSASOFT bit has to be removed from the 8362545Sbostic * calls that switch to/from "raw" mode. 8460627Sbostic */ 8562545Sbostic if (!__tcaction) { 8662545Sbostic rawt.c_iflag &= ~ISTRIP; 8762545Sbostic rawt.c_cflag &= ~(CSIZE|PARENB); 8862545Sbostic rawt.c_cflag |= CS8; 8962545Sbostic } 9060627Sbostic 9161229Smarc curt = &__baset; 9262545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 9362545Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 9455989Sbostic } 9555989Sbostic 9655989Sbostic int 9755989Sbostic raw() 9855989Sbostic { 9955989Sbostic useraw = __pfast = __rawmode = 1; 10060627Sbostic curt = &rawt; 10162545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 102*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 10355989Sbostic } 10455989Sbostic 10555989Sbostic int 10655989Sbostic noraw() 10755989Sbostic { 10855989Sbostic useraw = __pfast = __rawmode = 0; 10961229Smarc curt = &__baset; 11062545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 111*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 11255989Sbostic } 11355989Sbostic 11455989Sbostic int 11555989Sbostic cbreak() 11655989Sbostic { 11755989Sbostic 11855989Sbostic __rawmode = 1; 11960627Sbostic curt = useraw ? &rawt : &cbreakt; 12062545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 121*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 12255989Sbostic } 12355989Sbostic 12455989Sbostic int 12555989Sbostic nocbreak() 12655989Sbostic { 12755989Sbostic 12855989Sbostic __rawmode = 0; 12961229Smarc curt = useraw ? &rawt : &__baset; 13062545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 131*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 13255989Sbostic } 13355989Sbostic 13455989Sbostic int 13555989Sbostic echo() 13655989Sbostic { 13755989Sbostic rawt.c_lflag |= ECHO; 13860627Sbostic cbreakt.c_lflag |= ECHO; 13961229Smarc __baset.c_lflag |= ECHO; 14055989Sbostic 14155989Sbostic __echoit = 1; 14262545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 143*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 14455989Sbostic } 14555989Sbostic 14655989Sbostic int 14755989Sbostic noecho() 14855989Sbostic { 14955989Sbostic rawt.c_lflag &= ~ECHO; 15060627Sbostic cbreakt.c_lflag &= ~ECHO; 15161229Smarc __baset.c_lflag &= ~ECHO; 15255989Sbostic 15355989Sbostic __echoit = 0; 15462545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 155*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 15655989Sbostic } 15755989Sbostic 15855989Sbostic int 15955989Sbostic nl() 16055989Sbostic { 16155989Sbostic rawt.c_iflag |= ICRNL; 16255989Sbostic rawt.c_oflag |= ONLCR; 16360627Sbostic cbreakt.c_iflag |= ICRNL; 16460627Sbostic cbreakt.c_oflag |= ONLCR; 16561229Smarc __baset.c_iflag |= ICRNL; 16661229Smarc __baset.c_oflag |= ONLCR; 16755989Sbostic 16855989Sbostic __pfast = __rawmode; 16962545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 170*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 17155989Sbostic } 17255989Sbostic 17355989Sbostic int 17455989Sbostic nonl() 17555989Sbostic { 17655989Sbostic rawt.c_iflag &= ~ICRNL; 17755989Sbostic rawt.c_oflag &= ~ONLCR; 17860627Sbostic cbreakt.c_iflag &= ~ICRNL; 17960627Sbostic cbreakt.c_oflag &= ~ONLCR; 18061229Smarc __baset.c_iflag &= ~ICRNL; 18161229Smarc __baset.c_oflag &= ~ONLCR; 18255989Sbostic 18355989Sbostic __pfast = 1; 18462545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 185*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK); 18655989Sbostic } 18755989Sbostic 18859862Sbostic void 18959862Sbostic __startwin() 19059862Sbostic { 19159862Sbostic (void)fflush(stdout); 19259862Sbostic (void)setvbuf(stdout, NULL, _IOFBF, 0); 19359862Sbostic 19459862Sbostic tputs(TI, 0, __cputchar); 19559862Sbostic tputs(VS, 0, __cputchar); 19659862Sbostic } 19759862Sbostic 19855989Sbostic int 19955989Sbostic endwin() 20055989Sbostic { 20165314Sbostic __restore_stophandler(); 20265314Sbostic 20357716Sbostic if (curscr != NULL) { 20457716Sbostic if (curscr->flags & __WSTANDOUT) { 20557716Sbostic tputs(SE, 0, __cputchar); 20657716Sbostic curscr->flags &= ~__WSTANDOUT; 20757716Sbostic } 20860071Sbostic __mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0); 20955989Sbostic } 21055989Sbostic 21155989Sbostic (void)tputs(VE, 0, __cputchar); 21255989Sbostic (void)tputs(TE, 0, __cputchar); 21355989Sbostic (void)fflush(stdout); 21459862Sbostic (void)setvbuf(stdout, NULL, _IOLBF, 0); 21555989Sbostic 21662545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 217*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, &__orig_termios) ? ERR : OK); 21855989Sbostic } 21955989Sbostic 22055989Sbostic /* 22155989Sbostic * The following routines, savetty and resetty are completely useless and 22255989Sbostic * are left in only as stubs. If people actually use them they will almost 22355989Sbostic * certainly screw up the state of the world. 22455989Sbostic */ 22555989Sbostic static struct termios savedtty; 22655989Sbostic int 22755989Sbostic savetty() 22855989Sbostic { 229*67174Sbostic return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK); 23055989Sbostic } 23155989Sbostic 23255989Sbostic int 23355989Sbostic resetty() 23455989Sbostic { 23562545Sbostic return (tcsetattr(STDIN_FILENO, __tcaction ? 236*67174Sbostic TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK); 23755989Sbostic } 238