RTS tidyup sweep, first phase
[ghc-hetmet.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 && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
31     saved_termios[fd] : NULL;
32 }
33
34 void
35 __hscore_set_saved_termios(int fd, void* ts)
36 {
37   if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
38     saved_termios[fd] = ts;
39   }
40 }
41
42 void
43 resetTerminalSettings (void)
44 {
45 #if HAVE_TERMIOS_H
46     // Reset the terminal settings on the standard file descriptors,
47     // if we changed them.  See System.Posix.Internals.tcSetAttr for
48     // more details, including the reason we termporarily disable
49     // SIGTTOU here.
50     { 
51         int fd;
52         sigset_t sigset, old_sigset;
53         sigemptyset(&sigset);
54         sigaddset(&sigset, SIGTTOU);
55         sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
56         for (fd = 0; fd <= 2; fd++) {
57             struct termios* ts = (struct termios*)__hscore_get_saved_termios(fd);
58             if (ts != NULL) {
59                 tcsetattr(fd,TCSANOW,ts);
60             }
61         }
62         sigprocmask(SIG_SETMASK, &old_sigset, NULL);
63     }
64 #endif
65 }