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