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