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