xref: /original-bsd/lib/libcurses/tty.c (revision 67aa38ab)
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.9 (Berkeley) 01/24/93";
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 __orig_termios;
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 	useraw = 0;
33 
34 	if (tcgetattr(STDIN_FILENO, &__orig_termios))
35 		return (ERR);
36 
37 	GT = (__orig_termios.c_oflag & OXTABS) == 0;
38 	NONL = (__orig_termios.c_oflag & ONLCR) == 0;
39 
40 	norawt = __orig_termios;
41 	norawt.c_oflag &= ~OXTABS;
42 	rawt = norawt;
43 	cfmakeraw(&rawt);
44 
45 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt) ? ERR : OK);
46 }
47 
48 int
49 raw()
50 {
51 	useraw = __pfast = __rawmode = 1;
52 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
53 }
54 
55 int
56 noraw()
57 {
58 	useraw = __pfast = __rawmode = 0;
59 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
60 }
61 
62 int
63 cbreak()
64 {
65 	rawt.c_lflag &= ~ICANON;
66 	norawt.c_lflag &= ~ICANON;
67 
68 	__rawmode = 1;
69 	if (useraw)
70 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
71 	else
72 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
73 }
74 
75 int
76 nocbreak()
77 {
78 	rawt.c_lflag |= ICANON;
79 	norawt.c_lflag |= ICANON;
80 
81 	__rawmode = 0;
82 	if (useraw)
83 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
84 	else
85 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
86 }
87 
88 int
89 echo()
90 {
91 	rawt.c_lflag |= ECHO;
92 	norawt.c_lflag |= ECHO;
93 
94 	__echoit = 1;
95 	if (useraw)
96 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
97 	else
98 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
99 }
100 
101 int
102 noecho()
103 {
104 	rawt.c_lflag &= ~ECHO;
105 	norawt.c_lflag &= ~ECHO;
106 
107 	__echoit = 0;
108 	if (useraw)
109 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
110 	else
111 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
112 }
113 
114 int
115 nl()
116 {
117 	rawt.c_iflag |= ICRNL;
118 	rawt.c_oflag |= ONLCR;
119 	norawt.c_iflag |= ICRNL;
120 	norawt.c_oflag |= ONLCR;
121 
122 	__pfast = __rawmode;
123 	if (useraw)
124 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
125 	else
126 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
127 }
128 
129 int
130 nonl()
131 {
132 	rawt.c_iflag &= ~ICRNL;
133 	rawt.c_oflag &= ~ONLCR;
134 	norawt.c_iflag &= ~ICRNL;
135 	norawt.c_oflag &= ~ONLCR;
136 
137 	__pfast = 1;
138 	if (useraw)
139 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &rawt));
140 	else
141 		return (tcsetattr(STDIN_FILENO, TCSADRAIN, &norawt));
142 }
143 
144 int
145 endwin()
146 {
147 	if (curscr != NULL) {
148 		if (curscr->flags & __WSTANDOUT) {
149 			tputs(SE, 0, __cputchar);
150 			curscr->flags &= ~__WSTANDOUT;
151 		}
152 		mvcur(curscr->cury, curscr->cury, curscr->maxy - 1, 0);
153 	}
154 
155 	(void)tputs(VE, 0, __cputchar);
156 	(void)tputs(TE, 0, __cputchar);
157 	(void)fflush(stdout);
158 
159 	__echoit = __orig_termios.c_lflag & ECHO;
160 	__rawmode = __orig_termios.c_lflag & ICANON;
161 	__pfast = __orig_termios.c_iflag & ICRNL ? __rawmode : 1;
162 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &__orig_termios));
163 }
164 
165 void
166 __startwin()
167 {
168 	tputs(TI, 0, __cputchar);
169 	tputs(VS, 0, __cputchar);
170 }
171 
172 /*
173  * The following routines, savetty and resetty are completely useless and
174  * are left in only as stubs.  If people actually use them they will almost
175  * certainly screw up the state of the world.
176  */
177 static struct termios savedtty;
178 int
179 savetty()
180 {
181 	return (tcgetattr(STDIN_FILENO, &savedtty));
182 }
183 
184 int
185 resetty()
186 {
187 	return (tcsetattr(STDIN_FILENO, TCSADRAIN, &savedtty));
188 }
189