rts: delint/detab/dewhitespace TTY.c
[ghc.git] / rts / posix / TTY.c
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 1998-2009
4 *
5 * TTY-related functionality
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #include "PosixSource.h"
10 #include "Rts.h"
11
12 #include "RtsUtils.h" // __hscore_get/set prototypes
13 #include "TTY.h"
14
15 #ifdef HAVE_TERMIOS_H
16 #include <termios.h>
17 #endif
18 #ifdef HAVE_SIGNAL_H
19 #include <signal.h>
20 #endif
21
22 // Here we save the terminal settings on the standard file
23 // descriptors, if we need to change them (eg. to support NoBuffering
24 // input).
25 static void *saved_termios[3] = {NULL,NULL,NULL};
26
27 void*
28 __hscore_get_saved_termios(int fd)
29 {
30 return (0 <= fd &&
31 fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
32 saved_termios[fd] : NULL;
33 }
34
35 void
36 __hscore_set_saved_termios(int fd, void* ts)
37 {
38 if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
39 saved_termios[fd] = ts;
40 }
41 }
42
43 void
44 resetTerminalSettings (void)
45 {
46 #if HAVE_TERMIOS_H
47 // Reset the terminal settings on the standard file descriptors,
48 // if we changed them. See System.Posix.Internals.tcSetAttr for
49 // more details, including the reason we termporarily disable
50 // SIGTTOU here.
51 {
52 int fd;
53 sigset_t sigset, old_sigset;
54 sigemptyset(&sigset);
55 sigaddset(&sigset, SIGTTOU);
56 sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
57 for (fd = 0; fd <= 2; fd++) {
58 struct termios* ts =
59 (struct termios*)__hscore_get_saved_termios(fd);
60 if (ts != NULL) {
61 tcsetattr(fd,TCSANOW,ts);
62 }
63 }
64 sigprocmask(SIG_SETMASK, &old_sigset, NULL);
65 }
66 #endif
67 }