xref: /original-bsd/lib/libcurses/tty.c (revision e0851aa6)
1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)tty.c	5.3 (Berkeley) 09/14/92";
10 #endif /* not lint */
11 
12 /*
13  * Terminal initialization routines.
14  */
15 #include <sys/ioctl.h>
16 
17 #include <curses.h>
18 #include <termios.h>
19 #include <unistd.h>
20 
21 struct termios newtermio, origtermio;
22 static struct termios norawt, rawt;
23 static int useraw;
24 
25 /*
26  * gettmode --
27  *	Do terminal type initialization.
28  */
29 int
30 gettmode()
31 {
32 	if (tcgetattr(STDIN_FILENO, &origtermio))
33 		return (OK);
34 
35 	GT = (origtermio.c_oflag & OXTABS) == 0;
36 	NONL = (origtermio.c_oflag & ONLCR) == 0;
37 
38 	norawt = origtermio;
39 	norawt.c_oflag &= ~OXTABS;
40 	rawt = norawt;
41 	cfmakeraw(&rawt);
42 
43 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
44 }
45 
46 int
47 raw()
48 {
49 	useraw = __pfast = __rawmode = 1;
50 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
51 }
52 
53 int
54 noraw()
55 {
56 	useraw = __pfast = __rawmode = 0;
57 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
58 }
59 
60 int
61 cbreak()
62 {
63 	rawt.c_lflag &= ~ICANON;
64 	norawt.c_lflag &= ~ICANON;
65 
66 	__rawmode = 1;
67 	if (useraw)
68 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
69 	else
70 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
71 }
72 
73 int
74 nocbreak()
75 {
76 	rawt.c_lflag |= ICANON;
77 	norawt.c_lflag |= ICANON;
78 
79 	__rawmode = 0;
80 	if (useraw)
81 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
82 	else
83 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
84 }
85 
86 int
87 echo()
88 {
89 	rawt.c_lflag |= ECHO;
90 	norawt.c_lflag |= ECHO;
91 
92 	__echoit = 1;
93 	if (useraw)
94 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
95 	else
96 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
97 }
98 
99 int
100 noecho()
101 {
102 	rawt.c_lflag &= ~ECHO;
103 	norawt.c_lflag &= ~ECHO;
104 
105 	__echoit = 0;
106 	if (useraw)
107 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
108 	else
109 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
110 }
111 
112 int
113 nl()
114 {
115 	rawt.c_iflag |= ICRNL;
116 	rawt.c_oflag |= ONLCR;
117 	norawt.c_iflag |= ICRNL;
118 	norawt.c_oflag |= ONLCR;
119 
120 	__pfast = __rawmode;
121 	if (useraw)
122 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
123 	else
124 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
125 }
126 
127 int
128 nonl()
129 {
130 	rawt.c_iflag &= ~ICRNL;
131 	rawt.c_oflag &= ~ONLCR;
132 	norawt.c_iflag &= ~ICRNL;
133 	norawt.c_oflag &= ~ONLCR;
134 
135 	__pfast = 1;
136 	if (useraw)
137 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
138 	else
139 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
140 }
141 
142 int
143 endwin()
144 {
145 	if (curscr) {
146 		if (curscr->flags & __WSTANDOUT) {
147 			tputs(SE, 0, __cputchar);
148 			curscr->flags &= ~__WSTANDOUT;
149 		}
150 		__endwin = 1;
151 	}
152 
153 	(void)tputs(VE, 0, __cputchar);
154 	(void)tputs(TE, 0, __cputchar);
155 	(void)fflush(stdout);
156 
157 	__echoit = origtermio.c_lflag & ECHO;
158 	__rawmode = origtermio.c_lflag & ICANON;
159 	__pfast = origtermio.c_iflag & ICRNL ? __rawmode : 1;
160 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtermio));
161 }
162 
163 /*
164  * The following routines, savetty and resetty are completely useless and
165  * are left in only as stubs.  If people actually use them they will almost
166  * certainly screw up the state of the world.
167  */
168 static struct termios savedtty;
169 int
170 savetty()
171 {
172 	return (tcgetattr(STDIN_FILENO, &savedtty));
173 }
174 
175 int
176 resetty()
177 {
178 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
179 }
180