xref: /csrg-svn/lib/libcurses/tty.c (revision 59862)
155989Sbostic /*-
255989Sbostic  * Copyright (c) 1992 The Regents of the University of California.
355989Sbostic  * All rights reserved.
455989Sbostic  *
555989Sbostic  * %sccs.include.redist.c%
655989Sbostic  */
755989Sbostic 
855989Sbostic #ifndef lint
9*59862Sbostic static char sccsid[] = "@(#)tty.c	5.10 (Berkeley) 05/10/93";
1055989Sbostic #endif /* not lint */
1155989Sbostic 
1255989Sbostic /*
1355989Sbostic  * Terminal initialization routines.
1455989Sbostic  */
1555989Sbostic #include <sys/ioctl.h>
1655989Sbostic 
1755989Sbostic #include <curses.h>
1855989Sbostic #include <termios.h>
1955989Sbostic #include <unistd.h>
2055989Sbostic 
2157716Sbostic struct termios __orig_termios;
2255989Sbostic static struct termios norawt, rawt;
2355989Sbostic static int useraw;
2455989Sbostic 
2555989Sbostic /*
2655989Sbostic  * gettmode --
2755989Sbostic  *	Do terminal type initialization.
2855989Sbostic  */
2955989Sbostic int
3055989Sbostic gettmode()
3155989Sbostic {
3257286Sbostic 	useraw = 0;
3357286Sbostic 
3457716Sbostic 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
3557472Sbostic 		return (ERR);
3655989Sbostic 
3757716Sbostic 	GT = (__orig_termios.c_oflag & OXTABS) == 0;
3857716Sbostic 	NONL = (__orig_termios.c_oflag & ONLCR) == 0;
3955989Sbostic 
4057716Sbostic 	norawt = __orig_termios;
4155989Sbostic 	norawt.c_oflag &= ~OXTABS;
4255989Sbostic 	rawt = norawt;
4355989Sbostic 	cfmakeraw(&rawt);
4455989Sbostic 
4557472Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
4655989Sbostic }
4755989Sbostic 
4855989Sbostic int
4955989Sbostic raw()
5055989Sbostic {
5155989Sbostic 	useraw = __pfast = __rawmode = 1;
5255989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
5355989Sbostic }
5455989Sbostic 
5555989Sbostic int
5655989Sbostic noraw()
5755989Sbostic {
5855989Sbostic 	useraw = __pfast = __rawmode = 0;
5955989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
6055989Sbostic }
6155989Sbostic 
6255989Sbostic int
6355989Sbostic cbreak()
6455989Sbostic {
6555989Sbostic 	rawt.c_lflag &= ~ICANON;
6655989Sbostic 	norawt.c_lflag &= ~ICANON;
6755989Sbostic 
6855989Sbostic 	__rawmode = 1;
6955989Sbostic 	if (useraw)
7055989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
7155989Sbostic 	else
7255989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
7355989Sbostic }
7455989Sbostic 
7555989Sbostic int
7655989Sbostic nocbreak()
7755989Sbostic {
7855989Sbostic 	rawt.c_lflag |= ICANON;
7955989Sbostic 	norawt.c_lflag |= ICANON;
8055989Sbostic 
8155989Sbostic 	__rawmode = 0;
8255989Sbostic 	if (useraw)
8355989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
8455989Sbostic 	else
8555989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
8655989Sbostic }
8755989Sbostic 
8855989Sbostic int
8955989Sbostic echo()
9055989Sbostic {
9155989Sbostic 	rawt.c_lflag |= ECHO;
9255989Sbostic 	norawt.c_lflag |= ECHO;
9355989Sbostic 
9455989Sbostic 	__echoit = 1;
9555989Sbostic 	if (useraw)
9655989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
9755989Sbostic 	else
9855989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
9955989Sbostic }
10055989Sbostic 
10155989Sbostic int
10255989Sbostic noecho()
10355989Sbostic {
10455989Sbostic 	rawt.c_lflag &= ~ECHO;
10555989Sbostic 	norawt.c_lflag &= ~ECHO;
10655989Sbostic 
10755989Sbostic 	__echoit = 0;
10855989Sbostic 	if (useraw)
10955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
11055989Sbostic 	else
11155989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
11255989Sbostic }
11355989Sbostic 
11455989Sbostic int
11555989Sbostic nl()
11655989Sbostic {
11755989Sbostic 	rawt.c_iflag |= ICRNL;
11855989Sbostic 	rawt.c_oflag |= ONLCR;
11956115Selan 	norawt.c_iflag |= ICRNL;
12056115Selan 	norawt.c_oflag |= ONLCR;
12155989Sbostic 
12255989Sbostic 	__pfast = __rawmode;
12355989Sbostic 	if (useraw)
12455989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
12555989Sbostic 	else
12655989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
12755989Sbostic }
12855989Sbostic 
12955989Sbostic int
13055989Sbostic nonl()
13155989Sbostic {
13255989Sbostic 	rawt.c_iflag &= ~ICRNL;
13355989Sbostic 	rawt.c_oflag &= ~ONLCR;
13456115Selan 	norawt.c_iflag &= ~ICRNL;
13556115Selan 	norawt.c_oflag &= ~ONLCR;
13655989Sbostic 
13755989Sbostic 	__pfast = 1;
13855989Sbostic 	if (useraw)
13955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
14055989Sbostic 	else
14155989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
14255989Sbostic }
14355989Sbostic 
144*59862Sbostic void
145*59862Sbostic __startwin()
146*59862Sbostic {
147*59862Sbostic 	(void)fflush(stdout);
148*59862Sbostic 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
149*59862Sbostic 
150*59862Sbostic 	tputs(TI, 0, __cputchar);
151*59862Sbostic 	tputs(VS, 0, __cputchar);
152*59862Sbostic }
153*59862Sbostic 
15455989Sbostic int
15555989Sbostic endwin()
15655989Sbostic {
15757716Sbostic 	if (curscr != NULL) {
15857716Sbostic 		if (curscr->flags & __WSTANDOUT) {
15957716Sbostic 			tputs(SE, 0, __cputchar);
16057716Sbostic 			curscr->flags &= ~__WSTANDOUT;
16157716Sbostic 		}
16257716Sbostic 		mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0);
16355989Sbostic 	}
16455989Sbostic 
16555989Sbostic 	(void)tputs(VE, 0, __cputchar);
16655989Sbostic 	(void)tputs(TE, 0, __cputchar);
16755989Sbostic 	(void)fflush(stdout);
168*59862Sbostic 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
16955989Sbostic 
17057716Sbostic 	__echoit = __orig_termios.c_lflag & ECHO;
17157716Sbostic 	__rawmode = __orig_termios.c_lflag & ICANON;
17257716Sbostic 	__pfast = __orig_termios.c_iflag & ICRNL ? __rawmode : 1;
17357716Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &__orig_termios));
17455989Sbostic }
17555989Sbostic 
17655989Sbostic /*
17755989Sbostic  * The following routines, savetty and resetty are completely useless and
17855989Sbostic  * are left in only as stubs.  If people actually use them they will almost
17955989Sbostic  * certainly screw up the state of the world.
18055989Sbostic  */
18155989Sbostic static struct termios savedtty;
18255989Sbostic int
18355989Sbostic savetty()
18455989Sbostic {
18555989Sbostic 	return (tcgetattr(STDIN_FILENO, &savedtty));
18655989Sbostic }
18755989Sbostic 
18855989Sbostic int
18955989Sbostic resetty()
19055989Sbostic {
19155989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
19255989Sbostic }
193