tty.c revision 1.12.6.1 1 1.12.6.1 jdc /* $NetBSD: tty.c,v 1.12.6.1 2000/01/09 20:43:23 jdc 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.12.6.1 jdc __RCSID("$NetBSD: tty.c,v 1.12.6.1 2000/01/09 20:43:23 jdc 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.11 mrg #include <sys/fcntl.h>
47 1.11 mrg #include <sys/ioctl.h>
48 1.1 mycroft
49 1.6 cgd #include <stdlib.h>
50 1.1 mycroft #include <termios.h>
51 1.1 mycroft #include <unistd.h>
52 1.1 mycroft
53 1.6 cgd #include "curses.h"
54 1.6 cgd
55 1.2 cgd /*
56 1.2 cgd * In general, curses should leave tty hardware settings alone (speed, parity,
57 1.2 cgd * word size). This is most easily done in BSD by using TCSASOFT on all
58 1.2 cgd * tcsetattr calls. On other systems, it would be better to get and restore
59 1.2 cgd * those attributes at each change, or at least when stopped and restarted.
60 1.2 cgd * See also the comments in getterm().
61 1.2 cgd */
62 1.2 cgd #ifdef TCSASOFT
63 1.11 mrg int __tcaction = 1; /* Ignore hardware settings. */
64 1.2 cgd #else
65 1.11 mrg int __tcaction = 0;
66 1.2 cgd #endif
67 1.2 cgd
68 1.2 cgd struct termios __orig_termios, __baset;
69 1.11 mrg int __endwin;
70 1.2 cgd static struct termios cbreakt, rawt, *curt;
71 1.1 mycroft static int useraw;
72 1.11 mrg static int ovmin = 1;
73 1.11 mrg static int ovtime = 0;
74 1.1 mycroft
75 1.2 cgd #ifndef OXTABS
76 1.2 cgd #ifdef XTABS /* SMI uses XTABS. */
77 1.2 cgd #define OXTABS XTABS
78 1.2 cgd #else
79 1.2 cgd #define OXTABS 0
80 1.2 cgd #endif
81 1.2 cgd #endif
82 1.2 cgd
83 1.1 mycroft /*
84 1.1 mycroft * gettmode --
85 1.1 mycroft * Do terminal type initialization.
86 1.1 mycroft */
87 1.1 mycroft int
88 1.1 mycroft gettmode()
89 1.1 mycroft {
90 1.2 cgd useraw = 0;
91 1.11 mrg
92 1.2 cgd if (tcgetattr(STDIN_FILENO, &__orig_termios))
93 1.2 cgd return (ERR);
94 1.2 cgd
95 1.2 cgd __baset = __orig_termios;
96 1.2 cgd __baset.c_oflag &= ~OXTABS;
97 1.1 mycroft
98 1.11 mrg GT = 0; /* historical. was used before we wired OXTABS
99 1.11 mrg * off */
100 1.2 cgd NONL = (__baset.c_oflag & ONLCR) == 0;
101 1.1 mycroft
102 1.2 cgd /*
103 1.2 cgd * XXX
104 1.2 cgd * System V and SMI systems overload VMIN and VTIME, such that
105 1.2 cgd * VMIN is the same as the VEOF element, and VTIME is the same
106 1.2 cgd * as the VEOL element. This means that, if VEOF was ^D, the
107 1.2 cgd * default VMIN is 4. Majorly stupid.
108 1.2 cgd */
109 1.2 cgd cbreakt = __baset;
110 1.2 cgd cbreakt.c_lflag &= ~ICANON;
111 1.2 cgd cbreakt.c_cc[VMIN] = 1;
112 1.2 cgd cbreakt.c_cc[VTIME] = 0;
113 1.2 cgd
114 1.2 cgd rawt = cbreakt;
115 1.11 mrg rawt.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | INLCR | IGNCR | ICRNL | IXON);
116 1.2 cgd rawt.c_oflag &= ~OPOST;
117 1.11 mrg rawt.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
118 1.2 cgd
119 1.2 cgd /*
120 1.2 cgd * In general, curses should leave hardware-related settings alone.
121 1.2 cgd * This includes parity and word size. Older versions set the tty
122 1.2 cgd * to 8 bits, no parity in raw(), but this is considered to be an
123 1.2 cgd * artifact of the old tty interface. If it's desired to change
124 1.2 cgd * parity and word size, the TCSASOFT bit has to be removed from the
125 1.2 cgd * calls that switch to/from "raw" mode.
126 1.2 cgd */
127 1.2 cgd if (!__tcaction) {
128 1.2 cgd rawt.c_iflag &= ~ISTRIP;
129 1.11 mrg rawt.c_cflag &= ~(CSIZE | PARENB);
130 1.2 cgd rawt.c_cflag |= CS8;
131 1.2 cgd }
132 1.1 mycroft
133 1.2 cgd curt = &__baset;
134 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
135 1.2 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
136 1.1 mycroft }
137 1.1 mycroft
138 1.1 mycroft int
139 1.1 mycroft raw()
140 1.1 mycroft {
141 1.9 phil /* Check if we need to restart ... */
142 1.9 phil if (__endwin) {
143 1.9 phil __endwin = 0;
144 1.9 phil __restartwin();
145 1.9 phil }
146 1.11 mrg
147 1.1 mycroft useraw = __pfast = __rawmode = 1;
148 1.2 cgd curt = &rawt;
149 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
150 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
151 1.1 mycroft }
152 1.1 mycroft
153 1.1 mycroft int
154 1.1 mycroft noraw()
155 1.1 mycroft {
156 1.9 phil /* Check if we need to restart ... */
157 1.9 phil if (__endwin) {
158 1.9 phil __endwin = 0;
159 1.9 phil __restartwin();
160 1.9 phil }
161 1.11 mrg
162 1.1 mycroft useraw = __pfast = __rawmode = 0;
163 1.2 cgd curt = &__baset;
164 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
165 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
166 1.1 mycroft }
167 1.1 mycroft
168 1.1 mycroft int
169 1.1 mycroft cbreak()
170 1.1 mycroft {
171 1.9 phil /* Check if we need to restart ... */
172 1.9 phil if (__endwin) {
173 1.9 phil __endwin = 0;
174 1.9 phil __restartwin();
175 1.9 phil }
176 1.11 mrg
177 1.1 mycroft __rawmode = 1;
178 1.2 cgd curt = useraw ? &rawt : &cbreakt;
179 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
180 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
181 1.1 mycroft }
182 1.1 mycroft
183 1.1 mycroft int
184 1.1 mycroft nocbreak()
185 1.1 mycroft {
186 1.9 phil /* Check if we need to restart ... */
187 1.9 phil if (__endwin) {
188 1.9 phil __endwin = 0;
189 1.9 phil __restartwin();
190 1.9 phil }
191 1.1 mycroft
192 1.1 mycroft __rawmode = 0;
193 1.2 cgd curt = useraw ? &rawt : &__baset;
194 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
195 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
196 1.1 mycroft }
197 1.11 mrg
198 1.11 mrg int
199 1.11 mrg __delay()
200 1.11 mrg {
201 1.11 mrg /* Check if we need to restart ... */
202 1.11 mrg if (__endwin) {
203 1.11 mrg __endwin = 0;
204 1.11 mrg __restartwin();
205 1.11 mrg }
206 1.11 mrg
207 1.11 mrg rawt.c_cc[VMIN] = 1;
208 1.11 mrg rawt.c_cc[VTIME] = 0;
209 1.11 mrg cbreakt.c_cc[VMIN] = 1;
210 1.11 mrg cbreakt.c_cc[VTIME] = 0;
211 1.11 mrg __baset.c_cc[VMIN] = 1;
212 1.11 mrg __baset.c_cc[VTIME] = 0;
213 1.11 mrg
214 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
215 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
216 1.11 mrg }
217 1.11 mrg
218 1.11 mrg int
219 1.11 mrg __nodelay()
220 1.11 mrg {
221 1.11 mrg /* Check if we need to restart ... */
222 1.11 mrg if (__endwin) {
223 1.11 mrg __endwin = 0;
224 1.11 mrg __restartwin();
225 1.11 mrg }
226 1.11 mrg
227 1.11 mrg rawt.c_cc[VMIN] = 0;
228 1.11 mrg rawt.c_cc[VTIME] = 0;
229 1.11 mrg cbreakt.c_cc[VMIN] = 0;
230 1.11 mrg cbreakt.c_cc[VTIME] = 0;
231 1.11 mrg __baset.c_cc[VMIN] = 0;
232 1.11 mrg __baset.c_cc[VTIME] = 0;
233 1.11 mrg
234 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
235 1.11 mrg TCSASOFT : TCSANOW, curt) ? ERR : OK);
236 1.11 mrg }
237 1.11 mrg
238 1.11 mrg void
239 1.11 mrg __save_termios()
240 1.11 mrg {
241 1.11 mrg /* Check if we need to restart ... */
242 1.11 mrg if (__endwin) {
243 1.11 mrg __endwin = 0;
244 1.11 mrg __restartwin();
245 1.11 mrg }
246 1.11 mrg
247 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
248 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
249 1.11 mrg }
250 1.11 mrg
251 1.11 mrg void
252 1.11 mrg __restore_termios()
253 1.11 mrg {
254 1.11 mrg /* Check if we need to restart ... */
255 1.11 mrg if (__endwin) {
256 1.11 mrg __endwin = 0;
257 1.11 mrg __restartwin();
258 1.11 mrg }
259 1.11 mrg
260 1.11 mrg rawt.c_cc[VMIN] = ovmin;
261 1.11 mrg rawt.c_cc[VTIME] = ovtime;
262 1.11 mrg cbreakt.c_cc[VMIN] = ovmin;
263 1.11 mrg cbreakt.c_cc[VTIME] = ovtime;
264 1.11 mrg __baset.c_cc[VMIN] = ovmin;
265 1.11 mrg __baset.c_cc[VTIME] = ovtime;
266 1.11 mrg }
267 1.11 mrg
268 1.11 mrg int
269 1.11 mrg __timeout(delay)
270 1.11 mrg int delay;
271 1.11 mrg {
272 1.11 mrg /* Check if we need to restart ... */
273 1.11 mrg if (__endwin) {
274 1.11 mrg __endwin = 0;
275 1.11 mrg __restartwin();
276 1.11 mrg }
277 1.11 mrg
278 1.11 mrg ovmin = cbreakt.c_cc[VMIN];
279 1.11 mrg ovtime = cbreakt.c_cc[VTIME];
280 1.11 mrg rawt.c_cc[VMIN] = 0;
281 1.11 mrg rawt.c_cc[VTIME] = delay;
282 1.11 mrg cbreakt.c_cc[VMIN] = 0;
283 1.11 mrg cbreakt.c_cc[VTIME] = delay;
284 1.11 mrg __baset.c_cc[VMIN] = 0;
285 1.11 mrg __baset.c_cc[VTIME] = delay;
286 1.11 mrg
287 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
288 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
289 1.11 mrg }
290 1.11 mrg
291 1.11 mrg int
292 1.11 mrg __notimeout()
293 1.11 mrg {
294 1.11 mrg /* Check if we need to restart ... */
295 1.11 mrg if (__endwin) {
296 1.11 mrg __endwin = 0;
297 1.11 mrg __restartwin();
298 1.11 mrg }
299 1.11 mrg
300 1.11 mrg rawt.c_cc[VMIN] = 1;
301 1.11 mrg rawt.c_cc[VTIME] = 0;
302 1.11 mrg cbreakt.c_cc[VMIN] = 1;
303 1.11 mrg cbreakt.c_cc[VTIME] = 0;
304 1.11 mrg __baset.c_cc[VMIN] = 1;
305 1.11 mrg __baset.c_cc[VTIME] = 0;
306 1.11 mrg
307 1.11 mrg return (tcsetattr(STDIN_FILENO, __tcaction ?
308 1.11 mrg TCSASOFT | TCSANOW : TCSANOW, curt) ? ERR : OK);
309 1.11 mrg }
310 1.11 mrg
311 1.1 mycroft int
312 1.1 mycroft echo()
313 1.1 mycroft {
314 1.9 phil /* Check if we need to restart ... */
315 1.9 phil if (__endwin) {
316 1.9 phil __endwin = 0;
317 1.9 phil __restartwin();
318 1.9 phil }
319 1.9 phil
320 1.1 mycroft rawt.c_lflag |= ECHO;
321 1.2 cgd cbreakt.c_lflag |= ECHO;
322 1.2 cgd __baset.c_lflag |= ECHO;
323 1.11 mrg
324 1.1 mycroft __echoit = 1;
325 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
326 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
327 1.1 mycroft }
328 1.1 mycroft
329 1.1 mycroft int
330 1.1 mycroft noecho()
331 1.1 mycroft {
332 1.9 phil /* Check if we need to restart ... */
333 1.9 phil if (__endwin) {
334 1.9 phil __endwin = 0;
335 1.9 phil __restartwin();
336 1.9 phil }
337 1.9 phil
338 1.1 mycroft rawt.c_lflag &= ~ECHO;
339 1.2 cgd cbreakt.c_lflag &= ~ECHO;
340 1.2 cgd __baset.c_lflag &= ~ECHO;
341 1.12 simonb
342 1.1 mycroft __echoit = 0;
343 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
344 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
345 1.1 mycroft }
346 1.1 mycroft
347 1.1 mycroft int
348 1.1 mycroft nl()
349 1.1 mycroft {
350 1.9 phil /* Check if we need to restart ... */
351 1.9 phil if (__endwin) {
352 1.9 phil __endwin = 0;
353 1.9 phil __restartwin();
354 1.9 phil }
355 1.9 phil
356 1.1 mycroft rawt.c_iflag |= ICRNL;
357 1.1 mycroft rawt.c_oflag |= ONLCR;
358 1.2 cgd cbreakt.c_iflag |= ICRNL;
359 1.2 cgd cbreakt.c_oflag |= ONLCR;
360 1.2 cgd __baset.c_iflag |= ICRNL;
361 1.2 cgd __baset.c_oflag |= ONLCR;
362 1.1 mycroft
363 1.1 mycroft __pfast = __rawmode;
364 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
365 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
366 1.1 mycroft }
367 1.1 mycroft
368 1.1 mycroft int
369 1.1 mycroft nonl()
370 1.1 mycroft {
371 1.9 phil /* Check if we need to restart ... */
372 1.9 phil if (__endwin) {
373 1.9 phil __endwin = 0;
374 1.9 phil __restartwin();
375 1.9 phil }
376 1.9 phil
377 1.1 mycroft rawt.c_iflag &= ~ICRNL;
378 1.1 mycroft rawt.c_oflag &= ~ONLCR;
379 1.2 cgd cbreakt.c_iflag &= ~ICRNL;
380 1.2 cgd cbreakt.c_oflag &= ~ONLCR;
381 1.2 cgd __baset.c_iflag &= ~ICRNL;
382 1.2 cgd __baset.c_oflag &= ~ONLCR;
383 1.1 mycroft
384 1.1 mycroft __pfast = 1;
385 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
386 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, curt) ? ERR : OK);
387 1.2 cgd }
388 1.2 cgd
389 1.2 cgd void
390 1.2 cgd __startwin()
391 1.2 cgd {
392 1.6 cgd static char *stdbuf;
393 1.6 cgd static size_t len;
394 1.6 cgd
395 1.11 mrg (void) fflush(stdout);
396 1.6 cgd
397 1.6 cgd /*
398 1.6 cgd * Some C libraries default to a 1K buffer when talking to a tty.
399 1.6 cgd * With a larger screen, especially across a network, we'd like
400 1.6 cgd * to get it to all flush in a single write. Make it twice as big
401 1.6 cgd * as just the characters (so that we have room for cursor motions
402 1.11 mrg * and attribute information) but no more than 8K.
403 1.6 cgd */
404 1.6 cgd if (stdbuf == NULL) {
405 1.6 cgd if ((len = LINES * COLS * 2) > 8192)
406 1.6 cgd len = 8192;
407 1.6 cgd if ((stdbuf = malloc(len)) == NULL)
408 1.6 cgd len = 0;
409 1.6 cgd }
410 1.11 mrg (void) setvbuf(stdout, stdbuf, _IOFBF, len);
411 1.2 cgd
412 1.2 cgd tputs(TI, 0, __cputchar);
413 1.2 cgd tputs(VS, 0, __cputchar);
414 1.11 mrg tputs(KS, 0, __cputchar);
415 1.1 mycroft }
416 1.1 mycroft
417 1.1 mycroft int
418 1.1 mycroft endwin()
419 1.1 mycroft {
420 1.8 phil __endwin = 1;
421 1.8 phil return __stopwin();
422 1.11 mrg }
423 1.11 mrg
424 1.12.6.1 jdc bool
425 1.11 mrg isendwin()
426 1.11 mrg {
427 1.12.6.1 jdc return (__endwin ? TRUE : FALSE);
428 1.11 mrg }
429 1.11 mrg
430 1.11 mrg int
431 1.11 mrg flushinp()
432 1.11 mrg {
433 1.11 mrg int what = FREAD;
434 1.11 mrg (void) ioctl(STDIN_FILENO, TIOCFLUSH, &what);
435 1.11 mrg return (OK);
436 1.1 mycroft }
437 1.1 mycroft
438 1.1 mycroft /*
439 1.1 mycroft * The following routines, savetty and resetty are completely useless and
440 1.1 mycroft * are left in only as stubs. If people actually use them they will almost
441 1.1 mycroft * certainly screw up the state of the world.
442 1.1 mycroft */
443 1.1 mycroft static struct termios savedtty;
444 1.1 mycroft int
445 1.1 mycroft savetty()
446 1.1 mycroft {
447 1.6 cgd return (tcgetattr(STDIN_FILENO, &savedtty) ? ERR : OK);
448 1.1 mycroft }
449 1.1 mycroft
450 1.1 mycroft int
451 1.1 mycroft resetty()
452 1.1 mycroft {
453 1.2 cgd return (tcsetattr(STDIN_FILENO, __tcaction ?
454 1.6 cgd TCSASOFT | TCSADRAIN : TCSADRAIN, &savedtty) ? ERR : OK);
455 1.1 mycroft }
456