tty.c revision 1.13 1 1.13 blymn /* $NetBSD: tty.c,v 1.13 2000/04/11 13:57:10 blymn Exp $ */
2 1.7 mikel
3 1.1 mycroft /*-
4 1.6 cgd * Copyright (c) 1992, 1993, 1994
5 1.2 cgd * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * Redistribution and use in source and binary forms, with or without
8 1.1 mycroft * modification, are permitted provided that the following conditions
9 1.1 mycroft * are met:
10 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
11 1.1 mycroft * notice, this list of conditions and the following disclaimer.
12 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
14 1.1 mycroft * documentation and/or other materials provided with the distribution.
15 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
16 1.1 mycroft * must display the following acknowledgement:
17 1.1 mycroft * This product includes software developed by the University of
18 1.1 mycroft * California, Berkeley and its contributors.
19 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
20 1.1 mycroft * may be used to endorse or promote products derived from this software
21 1.1 mycroft * without specific prior written permission.
22 1.1 mycroft *
23 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 mycroft * SUCH DAMAGE.
34 1.1 mycroft */
35 1.1 mycroft
36 1.7 mikel #include <sys/cdefs.h>
37 1.1 mycroft #ifndef lint
38 1.7 mikel #if 0
39 1.10 perry static char sccsid[] = "@(#)tty.c 8.6 (Berkeley) 1/10/95";
40 1.7 mikel #else
41 1.13 blymn __RCSID("$NetBSD: tty.c,v 1.13 2000/04/11 13:57:10 blymn Exp $");
42 1.7 mikel #endif
43 1.11 mrg #endif /* not lint */
44 1.11 mrg
45 1.11 mrg #include <sys/types.h>
46 1.1 mycroft
47 1.6 cgd #include <stdlib.h>
48 1.1 mycroft #include <termios.h>
49 1.1 mycroft #include <unistd.h>
50 1.1 mycroft
51 1.6 cgd #include "curses.h"
52 1.6 cgd
53 1.2 cgd /*
54 1.2 cgd * In general, curses should leave tty hardware settings alone (speed, parity,
55 1.2 cgd * word size). This is most easily done in BSD by using TCSASOFT on all
56 1.2 cgd * tcsetattr calls. On other systems, it would be better to get and restore
57 1.2 cgd * those attributes at each change, or at least when stopped and restarted.
58 1.2 cgd * See also the comments in getterm().
59 1.2 cgd */
60 1.2 cgd #ifdef TCSASOFT
61 1.11 mrg int __tcaction = 1; /* Ignore hardware settings. */
62 1.2 cgd #else
63 1.11 mrg int __tcaction = 0;
64 1.2 cgd #endif
65 1.2 cgd
66 1.2 cgd struct termios __orig_termios, __baset;
67 1.11 mrg int __endwin;
68 1.2 cgd static struct termios cbreakt, rawt, *curt;
69 1.1 mycroft static int useraw;
70 1.11 mrg static int ovmin = 1;
71 1.11 mrg static int ovtime = 0;
72 1.1 mycroft
73 1.2 cgd #ifndef OXTABS
74 1.2 cgd #ifdef XTABS /* SMI uses XTABS. */
75 1.2 cgd #define OXTABS XTABS
76 1.2 cgd #else
77 1.2 cgd #define OXTABS 0
78 1.2 cgd #endif
79 1.2 cgd #endif
80 1.2 cgd
81 1.1 mycroft /*
82 1.1 mycroft * gettmode --
83 1.1 mycroft * Do terminal type initialization.
84 1.1 mycroft */
85 1.1 mycroft int
86 1.1 mycroft gettmode()
87 1.1 mycroft {
88 1.2 cgd useraw = 0;
89 1.11 mrg
90 1.2 cgd if (tcgetattr(STDIN_FILENO, &__orig_termios))
91 1.2 cgd return (ERR);
92 1.2 cgd
93 1.2 cgd __baset = __orig_termios;
94 1.2 cgd __baset.c_oflag &= ~OXTABS;
95 1.1 mycroft
96 1.11 mrg GT = 0; /* historical. was used before we wired OXTABS
97 1.11 mrg * off */
98 1.2 cgd NONL = (__baset.c_oflag & ONLCR) == 0;
99 1.1 mycroft
100 1.2 cgd /*
101 1.2 cgd * XXX
102 1.2 cgd * System V and SMI systems overload VMIN and VTIME, such that
103 1.2 cgd * VMIN is the same as the VEOF element, and VTIME is the same
104 1.2 cgd * as the VEOL element. This means that, if VEOF was ^D, the
105 1.2 cgd * default VMIN is 4. Majorly stupid.
106 1.2 cgd */
107 1.2 cgd cbreakt = __baset;
108 1.2 cgd cbreakt.c_lflag &= ~ICANON;
109 1.2 cgd cbreakt.c_cc[VMIN] = 1;
110 1.2 cgd cbreakt.c_cc[VTIME] = 0;
111 1.2 cgd
112 1.2 cgd rawt = cbreakt;
113 1.11 mrg rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON);
114 1.2 cgd rawt.c_oflag &= ~OPOST;
115 1.11 mrg rawt.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
116 1.2 cgd
117 1.2 cgd /*
118 1.2 cgd * In general, curses should leave hardware-related settings alone.
119 1.2 cgd * This includes parity and word size. Older versions set the tty
120 1.2 cgd * to 8 bits, no parity in raw(), but this is considered to be an
121 1.2 cgd * artifact of the old tty interface. If it's desired to change
122 1.2 cgd * parity and word size, the TCSASOFT bit has to be removed from the
123 1.2 cgd * calls that switch to/from "raw" mode.
124 1.2 cgd */
125 1.2 cgd if (!__tcaction) {
126 1.2 cgd rawt.c_iflag &= ~ISTRIP;
127 1.11 mrg rawt.c_cflag &= ~(CSIZE | PARENB);
128 1.2 cgd rawt.c_cflag |= CS8;
129 1.2 cgd }
130 1.1 mycroft
131 1.2 cgd curt = &__baset;
132 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
133 1.2 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
134 1.1 mycroft }
135 1.1 mycroft
136 1.1 mycroft int
137 1.1 mycroft raw()
138 1.1 mycroft {
139 1.9 phil /* Check if we need to restart ... */
140 1.9 phil if (__endwin) {
141 1.9 phil __endwin = 0;
142 1.9 phil __restartwin();
143 1.9 phil }
144 1.11 mrg
145 1.1 mycroft useraw = __pfast = __rawmode = 1;
146 1.2 cgd curt = &rawt;
147 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
148 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
149 1.1 mycroft }
150 1.1 mycroft
151 1.1 mycroft int
152 1.1 mycroft noraw()
153 1.1 mycroft {
154 1.9 phil /* Check if we need to restart ... */
155 1.9 phil if (__endwin) {
156 1.9 phil __endwin = 0;
157 1.9 phil __restartwin();
158 1.9 phil }
159 1.11 mrg
160 1.1 mycroft useraw = __pfast = __rawmode = 0;
161 1.2 cgd curt = &__baset;
162 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
163 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
164 1.1 mycroft }
165 1.1 mycroft
166 1.1 mycroft int
167 1.1 mycroft cbreak()
168 1.1 mycroft {
169 1.9 phil /* Check if we need to restart ... */
170 1.9 phil if (__endwin) {
171 1.9 phil __endwin = 0;
172 1.9 phil __restartwin();
173 1.9 phil }
174 1.11 mrg
175 1.1 mycroft __rawmode = 1;
176 1.2 cgd curt = useraw ? &rawt : &cbreakt;
177 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
178 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
179 1.1 mycroft }
180 1.1 mycroft
181 1.1 mycroft int
182 1.1 mycroft nocbreak()
183 1.1 mycroft {
184 1.9 phil /* Check if we need to restart ... */
185 1.9 phil if (__endwin) {
186 1.9 phil __endwin = 0;
187 1.9 phil __restartwin();
188 1.9 phil }
189 1.1 mycroft
190 1.1 mycroft __rawmode = 0;
191 1.2 cgd curt = useraw ? &rawt : &__baset;
192 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
193 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
194 1.1 mycroft }
195 1.11 mrg
196 1.11 mrg int
197 1.11 mrg __delay()
198 1.11 mrg {
199 1.11 mrg /* Check if we need to restart ... */
200 1.11 mrg if (__endwin) {
201 1.11 mrg __endwin = 0;
202 1.11 mrg __restartwin();
203 1.11 mrg }
204 1.11 mrg
205 1.11 mrg rawt.c_cc[VMIN] = 1;
206 1.11 mrg rawt.c_cc[VTIME] = 0;
207 1.11 mrg cbreakt.c_cc[VMIN] = 1;
208 1.11 mrg cbreakt.c_cc[VTIME] = 0;
209 1.11 mrg __baset.c_cc[VMIN] = 1;
210 1.11 mrg __baset.c_cc[VTIME] = 0;
211 1.11 mrg
212 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
213 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
214 1.11 mrg }
215 1.11 mrg
216 1.11 mrg int
217 1.11 mrg __nodelay()
218 1.11 mrg {
219 1.11 mrg /* Check if we need to restart ... */
220 1.11 mrg if (__endwin) {
221 1.11 mrg __endwin = 0;
222 1.11 mrg __restartwin();
223 1.11 mrg }
224 1.11 mrg
225 1.11 mrg rawt.c_cc[VMIN] = 0;
226 1.11 mrg rawt.c_cc[VTIME] = 0;
227 1.11 mrg cbreakt.c_cc[VMIN] = 0;
228 1.11 mrg cbreakt.c_cc[VTIME] = 0;
229 1.11 mrg __baset.c_cc[VMIN] = 0;
230 1.11 mrg __baset.c_cc[VTIME] = 0;
231 1.11 mrg
232 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
233 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
234 1.11 mrg }
235 1.11 mrg
236 1.11 mrg void
237 1.11 mrg __save_termios()
238 1.11 mrg {
239 1.11 mrg /* Check if we need to restart ... */
240 1.11 mrg if (__endwin) {
241 1.11 mrg __endwin = 0;
242 1.11 mrg __restartwin();
243 1.11 mrg }
244 1.11 mrg
245 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
246 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
247 1.11 mrg }
248 1.11 mrg
249 1.11 mrg void
250 1.11 mrg __restore_termios()
251 1.11 mrg {
252 1.11 mrg /* Check if we need to restart ... */
253 1.11 mrg if (__endwin) {
254 1.11 mrg __endwin = 0;
255 1.11 mrg __restartwin();
256 1.11 mrg }
257 1.11 mrg
258 1.11 mrg rawt.c_cc[VMIN] = ovmin;
259 1.11 mrg rawt.c_cc[VTIME] = ovtime;
260 1.11 mrg cbreakt.c_cc[VMIN] = ovmin;
261 1.11 mrg cbreakt.c_cc[VTIME] = ovtime;
262 1.11 mrg __baset.c_cc[VMIN] = ovmin;
263 1.11 mrg __baset.c_cc[VTIME] = ovtime;
264 1.11 mrg }
265 1.11 mrg
266 1.11 mrg int
267 1.11 mrg __timeout(delay)
268 1.11 mrg int delay;
269 1.11 mrg {
270 1.11 mrg /* Check if we need to restart ... */
271 1.11 mrg if (__endwin) {
272 1.11 mrg __endwin = 0;
273 1.11 mrg __restartwin();
274 1.11 mrg }
275 1.11 mrg
276 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
277 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
278 1.11 mrg rawt.c_cc[VMIN] = 0;
279 1.11 mrg rawt.c_cc[VTIME] = delay;
280 1.11 mrg cbreakt.c_cc[VMIN] = 0;
281 1.11 mrg cbreakt.c_cc[VTIME] = delay;
282 1.11 mrg __baset.c_cc[VMIN] = 0;
283 1.11 mrg __baset.c_cc[VTIME] = delay;
284 1.11 mrg
285 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
286 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
287 1.11 mrg }
288 1.11 mrg
289 1.11 mrg int
290 1.11 mrg __notimeout()
291 1.11 mrg {
292 1.11 mrg /* Check if we need to restart ... */
293 1.11 mrg if (__endwin) {
294 1.11 mrg __endwin = 0;
295 1.11 mrg __restartwin();
296 1.11 mrg }
297 1.11 mrg
298 1.11 mrg rawt.c_cc[VMIN] = 1;
299 1.11 mrg rawt.c_cc[VTIME] = 0;
300 1.11 mrg cbreakt.c_cc[VMIN] = 1;
301 1.11 mrg cbreakt.c_cc[VTIME] = 0;
302 1.11 mrg __baset.c_cc[VMIN] = 1;
303 1.11 mrg __baset.c_cc[VTIME] = 0;
304 1.11 mrg
305 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
306 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
307 1.11 mrg }
308 1.11 mrg
309 1.1 mycroft int
310 1.1 mycroft echo()
311 1.1 mycroft {
312 1.9 phil /* Check if we need to restart ... */
313 1.9 phil if (__endwin) {
314 1.9 phil __endwin = 0;
315 1.9 phil __restartwin();
316 1.9 phil }
317 1.9 phil
318 1.1 mycroft rawt.c_lflag |= ECHO;
319 1.2 cgd cbreakt.c_lflag |= ECHO;
320 1.2 cgd __baset.c_lflag |= ECHO;
321 1.11 mrg
322 1.1 mycroft __echoit = 1;
323 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
324 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
325 1.1 mycroft }
326 1.1 mycroft
327 1.1 mycroft int
328 1.1 mycroft noecho()
329 1.1 mycroft {
330 1.9 phil /* Check if we need to restart ... */
331 1.9 phil if (__endwin) {
332 1.9 phil __endwin = 0;
333 1.9 phil __restartwin();
334 1.9 phil }
335 1.9 phil
336 1.1 mycroft rawt.c_lflag &= ~ECHO;
337 1.2 cgd cbreakt.c_lflag &= ~ECHO;
338 1.2 cgd __baset.c_lflag &= ~ECHO;
339 1.12 simonb
340 1.1 mycroft __echoit = 0;
341 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
342 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
343 1.1 mycroft }
344 1.1 mycroft
345 1.1 mycroft int
346 1.1 mycroft nl()
347 1.1 mycroft {
348 1.9 phil /* Check if we need to restart ... */
349 1.9 phil if (__endwin) {
350 1.9 phil __endwin = 0;
351 1.9 phil __restartwin();
352 1.9 phil }
353 1.9 phil
354 1.1 mycroft rawt.c_iflag |= ICRNL;
355 1.1 mycroft rawt.c_oflag |= ONLCR;
356 1.2 cgd cbreakt.c_iflag |= ICRNL;
357 1.2 cgd cbreakt.c_oflag |= ONLCR;
358 1.2 cgd __baset.c_iflag |= ICRNL;
359 1.2 cgd __baset.c_oflag |= ONLCR;
360 1.1 mycroft
361 1.1 mycroft __pfast = __rawmode;
362 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
363 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
364 1.1 mycroft }
365 1.1 mycroft
366 1.1 mycroft int
367 1.1 mycroft nonl()
368 1.1 mycroft {
369 1.9 phil /* Check if we need to restart ... */
370 1.9 phil if (__endwin) {
371 1.9 phil __endwin = 0;
372 1.9 phil __restartwin();
373 1.9 phil }
374 1.9 phil
375 1.1 mycroft rawt.c_iflag &= ~ICRNL;
376 1.1 mycroft rawt.c_oflag &= ~ONLCR;
377 1.2 cgd cbreakt.c_iflag &= ~ICRNL;
378 1.2 cgd cbreakt.c_oflag &= ~ONLCR;
379 1.2 cgd __baset.c_iflag &= ~ICRNL;
380 1.2 cgd __baset.c_oflag &= ~ONLCR;
381 1.1 mycroft
382 1.1 mycroft __pfast = 1;
383 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
384 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
385 1.2 cgd }
386 1.2 cgd
387 1.2 cgd void
388 1.2 cgd __startwin()
389 1.2 cgd {
390 1.6 cgd static char *stdbuf;
391 1.6 cgd static size_t len;
392 1.6 cgd
393 1.11 mrg (void) fflush(stdout);
394 1.6 cgd
395 1.6 cgd /*
396 1.6 cgd * Some C libraries default to a 1K buffer when talking to a tty.
397 1.6 cgd * With a larger screen, especially across a network, we'd like
398 1.6 cgd * to get it to all flush in a single write. Make it twice as big
399 1.6 cgd * as just the characters (so that we have room for cursor motions
400 1.11 mrg * and attribute information) but no more than 8K.
401 1.6 cgd */
402 1.6 cgd if (stdbuf == NULL) {
403 1.6 cgd if ((len = LINES * COLS * 2) > 8192)
404 1.6 cgd len = 8192;
405 1.6 cgd if ((stdbuf = malloc(len)) == NULL)
406 1.6 cgd len = 0;
407 1.6 cgd }
408 1.11 mrg (void) setvbuf(stdout, stdbuf, _IOFBF, len);
409 1.2 cgd
410 1.2 cgd tputs(TI, 0, __cputchar);
411 1.2 cgd tputs(VS, 0, __cputchar);
412 1.11 mrg tputs(KS, 0, __cputchar);
413 1.1 mycroft }
414 1.1 mycroft
415 1.1 mycroft int
416 1.1 mycroft endwin()
417 1.1 mycroft {
418 1.8 phil __endwin = 1;
419 1.8 phil return __stopwin();
420 1.11 mrg }
421 1.11 mrg
422 1.13 blymn bool
423 1.11 mrg isendwin()
424 1.11 mrg {
425 1.13 blymn return (__endwin ? TRUE : FALSE);
426 1.11 mrg }
427 1.11 mrg
428 1.11 mrg int
429 1.11 mrg flushinp()
430 1.11 mrg {
431 1.13 blymn (void) fpurge(stdin);
432 1.11 mrg return (OK);
433 1.1 mycroft }
434 1.1 mycroft
435 1.1 mycroft /*
436 1.1 mycroft * The following routines, savetty and resetty are completely useless and
437 1.1 mycroft * are left in only as stubs. If people actually use them they will almost
438 1.1 mycroft * certainly screw up the state of the world.
439 1.1 mycroft */
440 1.1 mycroft static struct termios savedtty;
441 1.1 mycroft int
442 1.1 mycroft savetty()
443 1.1 mycroft {
444 1.6 cgd return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
445 1.1 mycroft }
446 1.1 mycroft
447 1.1 mycroft int
448 1.1 mycroft resetty()
449 1.1 mycroft {
450 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
451 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
452 1.1 mycroft }
453