[project @ 2001-02-19 16:07:48 by rrt]
[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.5 2001/02/19 16:07:48 rrt 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(StgForeignPtr ptr, StgInt on)
30 {
31 #ifndef mingw32_TARGET_OS
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 #endif
60   return 0;
61 }
62
63 StgInt
64 getTerminalEcho(StgForeignPtr ptr)
65 {
66 #ifndef mingw32_TARGET_OS
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 #else
82    return 0;
83 #endif
84 }
85
86 StgInt
87 isTerminalDevice(StgForeignPtr ptr)
88 {
89 #ifndef mingw32_TARGET_OS
90    IOFileObject* fo = (IOFileObject*)ptr;
91    struct termios tios;
92    int fd, rc;
93
94    fd = fo -> fd;
95
96    while ( (rc = tcgetattr(fd,&tios)) == -1) {
97         if (errno == ENOTTY) return 0;
98         if (errno != EINTR) {
99             cvtErrno();
100             stdErrno();
101             return -1;
102         }
103    }
104    return 1;
105 #else
106    return 0;
107 #endif
108 }