ce4b65910121b52bffd7bcfaee5d496c4ebce70f
[ghc-hetmet.git] / ghc / lib / std / cbits / echoAux.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
3 %
4 \subsection[echoAux.lc]{Support functions for changing echoing}
5
6 \begin{code}
7
8 #include "rtsdefs.h"
9 #include "stgio.h"
10
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14
15 #ifdef HAVE_SYS_STAT_H
16 #include <sys/stat.h>
17 #endif
18
19 #ifdef HAVE_TERMIOS_H
20 #include <termios.h>
21 #endif
22
23 #ifdef HAVE_FCNTL_H
24 #include <fcntl.h>
25 #endif
26
27 StgInt
28 setTerminalEcho(fp, on)
29 StgForeignObj fp;
30 StgInt on;
31 {
32    struct termios tios;
33    int fd, rc;
34
35    while ( (fd = fileno((FILE*)fp)) < 0) {
36         if (errno != EINTR) {
37             cvtErrno();
38             stdErrno();
39             return -1;
40         }
41    }
42    while ( (rc = tcgetattr(fd,&tios)) == -1) {
43         if (errno != EINTR) {
44             cvtErrno();
45             stdErrno();
46             return -1;
47         }
48    }
49
50    if (on) {
51      tios.c_lflag |= ECHO;
52    } else {
53      tios.c_lflag &= ~ECHO;
54    }
55
56    while ( (rc = tcsetattr(fd,TCSANOW,&tios)) == -1) {
57         if (errno != EINTR) {
58             cvtErrno();
59             stdErrno();
60             return -1;
61         }
62    }
63   return 0;
64 }
65
66 StgInt
67 getTerminalEcho(fp)
68 StgForeignObj fp;
69 {
70    struct termios tios;
71    int fd, rc;
72
73    while ( (fd = fileno((FILE*)fp)) < 0) {
74         if (errno != EINTR) {
75             cvtErrno();
76             stdErrno();
77             return -1;
78         }
79    }
80    while ( (rc = tcgetattr(fd,&tios)) == -1) {
81         if (errno != EINTR) {
82             cvtErrno();
83             stdErrno();
84             return -1;
85         }
86    }
87    return (tios.c_cflag & ECHO ? 1 : 0);
88 }
89
90 StgInt
91 isTerminalDevice(fp)
92 StgForeignObj fp;
93 {
94    struct termios tios;
95    int fd, rc;
96
97    while ( (fd = fileno((FILE*)fp)) < 0) {
98         if (errno != EINTR) {
99             cvtErrno();
100             stdErrno();
101             return -1;
102         }
103    }
104    while ( (rc = tcgetattr(fd,&tios)) == -1) {
105         if (errno == ENOTTY) return 0;
106         if (errno != EINTR) {
107             cvtErrno();
108             stdErrno();
109             return -1;
110         }
111    }
112    return 1;
113 }
114
115 \end{code}