summaryrefslogtreecommitdiff
path: root/rts/posix/TTY.c
blob: 0c7eaf0ea47a3b6d90d4315dec3ca4c12204f4f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* -----------------------------------------------------------------------------
 *
 * (c) The GHC Team, 1998-2009
 *
 * TTY-related functionality
 *
 * ---------------------------------------------------------------------------*/

#include "rts/PosixSource.h"
#include "Rts.h"

#include "RtsUtils.h" // __hscore_get/set prototypes
#include "TTY.h"

#if defined(HAVE_TERMIOS_H)
#include <termios.h>
#endif
#if defined(HAVE_SIGNAL_H)
#include <signal.h>
#endif

// Here we save the terminal settings on the standard file
// descriptors, if we need to change them (eg. to support NoBuffering
// input).
static void *saved_termios[3] = {NULL,NULL,NULL};

void*
__hscore_get_saved_termios(int fd)
{
  return (0 <= fd &&
            fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) ?
  saved_termios[fd] : NULL;
}

void
__hscore_set_saved_termios(int fd, void* ts)
{
  if (0 <= fd && fd < (int)(sizeof(saved_termios) / sizeof(*saved_termios))) {
    saved_termios[fd] = ts;
  }
}

void
resetTerminalSettings (void)
{
#if HAVE_TERMIOS_H
    // Reset the terminal settings on the standard file descriptors,
    // if we changed them.  See System.Posix.Internals.tcSetAttr for
    // more details, including the reason we temporarily disable
    // SIGTTOU here.
    {
        int fd;
        sigset_t sigset, old_sigset;
        sigemptyset(&sigset);
        sigaddset(&sigset, SIGTTOU);
        sigprocmask(SIG_BLOCK, &sigset, &old_sigset);
        for (fd = 0; fd <= 2; fd++) {
            struct termios* ts =
                (struct termios*)__hscore_get_saved_termios(fd);
            if (ts != NULL) {
                tcsetattr(fd,TCSANOW,ts);
            }
        }
        sigprocmask(SIG_SETMASK, &old_sigset, NULL);
    }
#endif
}