xref: /csrg-svn/lib/libcurses/tty.c (revision 60616)
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*60616Sbostic static char sccsid[] = "@(#)tty.c	5.14 (Berkeley) 05/30/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 
25*60616Sbostic #ifndef	OXTABS
26*60616Sbostic #ifdef	XTABS			/* SMI uses XTABS. */
27*60616Sbostic #define	OXTABS	XTABS
28*60616Sbostic #else
29*60616Sbostic #define	OXTABS	0
30*60616Sbostic #endif
31*60616Sbostic #endif
32*60616Sbostic 
3355989Sbostic /*
3455989Sbostic  * gettmode --
3555989Sbostic  *	Do terminal type initialization.
3655989Sbostic  */
3755989Sbostic int
3855989Sbostic gettmode()
3955989Sbostic {
4057286Sbostic 	useraw = 0;
4157286Sbostic 
4257716Sbostic 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
4357472Sbostic 		return (ERR);
4455989Sbostic 
4557716Sbostic 	GT = (__orig_termios.c_oflag & OXTABS) == 0;
4657716Sbostic 	NONL = (__orig_termios.c_oflag & ONLCR) == 0;
4755989Sbostic 
4857716Sbostic 	norawt = __orig_termios;
4955989Sbostic 	norawt.c_oflag &= ~OXTABS;
5055989Sbostic 	rawt = norawt;
5155989Sbostic 	cfmakeraw(&rawt);
5255989Sbostic 
5357472Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
5455989Sbostic }
5555989Sbostic 
5655989Sbostic int
5755989Sbostic raw()
5855989Sbostic {
5955989Sbostic 	useraw = __pfast = __rawmode = 1;
6055989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
6155989Sbostic }
6255989Sbostic 
6355989Sbostic int
6455989Sbostic noraw()
6555989Sbostic {
6655989Sbostic 	useraw = __pfast = __rawmode = 0;
6755989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
6855989Sbostic }
6955989Sbostic 
7055989Sbostic int
7155989Sbostic cbreak()
7255989Sbostic {
7355989Sbostic 	rawt.c_lflag &= ~ICANON;
7455989Sbostic 	norawt.c_lflag &= ~ICANON;
7555989Sbostic 
7655989Sbostic 	__rawmode = 1;
7755989Sbostic 	if (useraw)
7855989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
7955989Sbostic 	else
8055989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
8155989Sbostic }
8255989Sbostic 
8355989Sbostic int
8455989Sbostic nocbreak()
8555989Sbostic {
8655989Sbostic 	rawt.c_lflag |= ICANON;
8755989Sbostic 	norawt.c_lflag |= ICANON;
8855989Sbostic 
8955989Sbostic 	__rawmode = 0;
9055989Sbostic 	if (useraw)
9155989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
9255989Sbostic 	else
9355989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
9455989Sbostic }
9555989Sbostic 
9655989Sbostic int
9755989Sbostic echo()
9855989Sbostic {
9955989Sbostic 	rawt.c_lflag |= ECHO;
10055989Sbostic 	norawt.c_lflag |= ECHO;
10155989Sbostic 
10255989Sbostic 	__echoit = 1;
10355989Sbostic 	if (useraw)
10455989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
10555989Sbostic 	else
10655989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
10755989Sbostic }
10855989Sbostic 
10955989Sbostic int
11055989Sbostic noecho()
11155989Sbostic {
11255989Sbostic 	rawt.c_lflag &= ~ECHO;
11355989Sbostic 	norawt.c_lflag &= ~ECHO;
11455989Sbostic 
11555989Sbostic 	__echoit = 0;
11655989Sbostic 	if (useraw)
11755989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
11855989Sbostic 	else
11955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
12055989Sbostic }
12155989Sbostic 
12255989Sbostic int
12355989Sbostic nl()
12455989Sbostic {
12555989Sbostic 	rawt.c_iflag |= ICRNL;
12655989Sbostic 	rawt.c_oflag |= ONLCR;
12756115Selan 	norawt.c_iflag |= ICRNL;
12856115Selan 	norawt.c_oflag |= ONLCR;
12955989Sbostic 
13055989Sbostic 	__pfast = __rawmode;
13155989Sbostic 	if (useraw)
13255989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
13355989Sbostic 	else
13455989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
13555989Sbostic }
13655989Sbostic 
13755989Sbostic int
13855989Sbostic nonl()
13955989Sbostic {
14055989Sbostic 	rawt.c_iflag &= ~ICRNL;
14155989Sbostic 	rawt.c_oflag &= ~ONLCR;
14256115Selan 	norawt.c_iflag &= ~ICRNL;
14356115Selan 	norawt.c_oflag &= ~ONLCR;
14455989Sbostic 
14555989Sbostic 	__pfast = 1;
14655989Sbostic 	if (useraw)
14755989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
14855989Sbostic 	else
14955989Sbostic 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
15055989Sbostic }
15155989Sbostic 
15259862Sbostic void
15359862Sbostic __startwin()
15459862Sbostic {
15559862Sbostic 	(void)fflush(stdout);
15659862Sbostic 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
15759862Sbostic 
15859862Sbostic 	tputs(TI, 0, __cputchar);
15959862Sbostic 	tputs(VS, 0, __cputchar);
16059862Sbostic }
16159862Sbostic 
16255989Sbostic int
16355989Sbostic endwin()
16455989Sbostic {
16557716Sbostic 	if (curscr != NULL) {
16657716Sbostic 		if (curscr->flags & __WSTANDOUT) {
16757716Sbostic 			tputs(SE, 0, __cputchar);
16857716Sbostic 			curscr->flags &= ~__WSTANDOUT;
16957716Sbostic 		}
17060071Sbostic 		__mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0, 0);
17155989Sbostic 	}
17255989Sbostic 
17355989Sbostic 	(void)tputs(VE, 0, __cputchar);
17455989Sbostic 	(void)tputs(TE, 0, __cputchar);
17555989Sbostic 	(void)fflush(stdout);
17659862Sbostic 	(void)setvbuf(stdout, NULL, _IOLBF, 0);
17755989Sbostic 
17857716Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &__orig_termios));
17955989Sbostic }
18055989Sbostic 
18155989Sbostic /*
18255989Sbostic  * The following routines, savetty and resetty are completely useless and
18355989Sbostic  * are left in only as stubs.  If people actually use them they will almost
18455989Sbostic  * certainly screw up the state of the world.
18555989Sbostic  */
18655989Sbostic static struct termios savedtty;
18755989Sbostic int
18855989Sbostic savetty()
18955989Sbostic {
19055989Sbostic 	return (tcgetattr(STDIN_FILENO, &savedtty));
19155989Sbostic }
19255989Sbostic 
19355989Sbostic int
19455989Sbostic resetty()
19555989Sbostic {
19655989Sbostic 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
19755989Sbostic }
198