[project @ 2001-01-16 14:06:14 by simonmar]
[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.4 2001/01/16 14:06:14 simonmar 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    IOFileObject* fo = (IOFileObject*)ptr;
32    struct termios tios;
33    int fd, rc;
34
35    fd = fo->fd;
36
37 #ifndef mingw32_TARGET_OS
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(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 #ifndef mingw32_TARGET_OS
74    while ( (rc = tcgetattr(fd,&tios)) == -1) {
75         if (errno != EINTR) {
76             cvtErrno();
77             stdErrno();
78             return -1;
79         }
80    }
81    return (tios.c_cflag & ECHO ? 1 : 0);
82 #else
83    return 0;
84 #endif
85 }
86
87 StgInt
88 isTerminalDevice(ptr)
89 StgForeignPtr ptr;
90 {
91    IOFileObject* fo = (IOFileObject*)ptr;
92    struct termios tios;
93    int fd, rc;
94
95    fd = fo -> fd;
96
97 #ifndef mingw32_TARGET_OS
98    while ( (rc = tcgetattr(fd,&tios)) == -1) {
99         if (errno == ENOTTY) return 0;
100         if (errno != EINTR) {
101             cvtErrno();
102             stdErrno();
103             return -1;
104         }
105    }
106    return 1;
107 #else
108    return 0;
109 #endif
110 }