[project @ 1998-11-26 09:17:22 by sof]
[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(ptr, on)
29 StgForeignObj ptr;
30 StgInt on;
31 {
32    IOFileObject* fo = (IOFileObject*)ptr;
33    struct termios tios;
34    int fd, rc;
35
36    fd = fo->fd;
37
38    while ( (rc = tcgetattr(fd,&tios)) == -1) {
39         if (errno != EINTR) {
40             cvtErrno();
41             stdErrno();
42             return -1;
43         }
44    }
45
46    if (on) {
47      tios.c_lflag |= ECHO;
48    } else {
49      tios.c_lflag &= ~ECHO;
50    }
51
52    while ( (rc = tcsetattr(fd,TCSANOW,&tios)) == -1) {
53         if (errno != EINTR) {
54             cvtErrno();
55             stdErrno();
56             return -1;
57         }
58    }
59   return 0;
60 }
61
62 StgInt
63 getTerminalEcho(ptr)
64 StgForeignObj ptr;
65 {
66    IOFileObject* fo = (IOFileObject*)ptr;
67    struct termios tios;
68    int fd, rc;
69
70    fd = fo->fd;
71
72    while ( (rc = tcgetattr(fd,&tios)) == -1) {
73         if (errno != EINTR) {
74             cvtErrno();
75             stdErrno();
76             return -1;
77         }
78    }
79    return (tios.c_cflag & ECHO ? 1 : 0);
80 }
81
82 StgInt
83 isTerminalDevice(ptr)
84 StgForeignObj ptr;
85 {
86    IOFileObject* fo = (IOFileObject*)ptr;
87    struct termios tios;
88    int fd, rc;
89
90    fd = fo -> fd;
91
92    while ( (rc = tcgetattr(fd,&tios)) == -1) {
93         if (errno == ENOTTY) return 0;
94         if (errno != EINTR) {
95             cvtErrno();
96             stdErrno();
97             return -1;
98         }
99    }
100    return 1;
101 }
102
103 \end{code}