Added a bulk of tests from Mainsoft repository.
[mono.git] / mono / metadata / console-io.c
1 /*
2  * console-io.c: ConsoleDriver internal calls
3  *
4  * Author:
5  *      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6  *
7  * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
8  */
9
10 #include <config.h>
11 #include <glib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <signal.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19 /* On solaris, curses.h must come before both termios.h and term.h */
20 #ifdef HAVE_CURSES_H
21 #include <curses.h>
22 #endif
23 #ifdef HAVE_TERMIOS_H
24 #include <termios.h>
25 #endif
26 #ifdef HAVE_TERM_H
27 #include <term.h>
28 #endif
29 /* Needed for FIONREAD under solaris */
30 #ifdef HAVE_SYS_FILIO_H
31 #include <sys/filio.h>
32 #endif
33 #ifndef PLATFORM_WIN32
34 #ifndef TIOCGWINSZ
35 #include <sys/ioctl.h>
36 #endif
37 #endif
38
39 #include <mono/metadata/console-io.h>
40 #include <mono/metadata/exception.h>
41
42 static gboolean setup_finished;
43 static gboolean atexit_called;
44 static gchar *teardown_str;
45
46 #ifdef PLATFORM_WIN32
47 MonoBoolean
48 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
49 {
50         MONO_ARCH_SAVE_REGS;
51
52         return (GetFileType (handle) == FILE_TYPE_CHAR);
53 }
54
55 MonoBoolean
56 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
57 {
58         return FALSE;
59 }
60
61 MonoBoolean
62 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
63 {
64         return FALSE;
65 }
66
67 gint32
68 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
69 {
70         return FALSE;
71 }
72
73 MonoBoolean
74 ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown)
75 {
76         return FALSE;
77 }
78
79 #else
80 static struct termios initial_attr;
81
82 MonoBoolean
83 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
84 {
85         MONO_ARCH_SAVE_REGS;
86
87         return isatty (GPOINTER_TO_INT (handle));
88 }
89
90 static MonoBoolean
91 set_property (gint property, gboolean value)
92 {
93         struct termios attr;
94         gboolean callset = FALSE;
95         gboolean check;
96         
97         MONO_ARCH_SAVE_REGS;
98
99         if (tcgetattr (STDIN_FILENO, &attr) == -1)
100                 return FALSE;
101
102         check = (attr.c_lflag & property) != 0;
103         if ((value || check) && !(value && check)) {
104                 callset = TRUE;
105                 if (value)
106                         attr.c_lflag |= property;
107                 else
108                         attr.c_lflag &= ~property;
109         }
110
111         if (!callset)
112                 return TRUE;
113
114         if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
115                 return FALSE;
116
117         return TRUE;
118 }
119
120 MonoBoolean
121 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
122 {
123         return set_property (ECHO, want_echo);
124 }
125
126 MonoBoolean
127 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
128 {
129         return set_property (IGNBRK, !want_break);
130 }
131
132 gint32
133 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
134 {
135         fd_set rfds;
136         struct timeval tv;
137         struct timeval *tvptr;
138         div_t divvy;
139         int ret, nbytes;
140
141         MONO_ARCH_SAVE_REGS;
142
143         do {
144                 FD_ZERO (&rfds);
145                 FD_SET (STDIN_FILENO, &rfds);
146                 if (timeout >= 0) {
147                         divvy = div (timeout, 1000);
148                         tv.tv_sec = divvy.quot;
149                         tv.tv_usec = divvy.rem;
150                         tvptr = &tv;
151                 } else {
152                         tvptr = NULL;
153                 }
154                 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
155         } while (ret == -1 && errno == EINTR);
156
157         if (ret > 0) {
158                 nbytes = 0;
159                 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
160                 if (ret >= 0)
161                         ret = nbytes;
162         }
163
164         return (ret > 0) ? ret : 0;
165 }
166
167 static void
168 tty_teardown (void)
169 {
170         MONO_ARCH_SAVE_REGS;
171
172         if (!setup_finished)
173                 return;
174
175         if (teardown_str != NULL) {
176                 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
177                 g_free (teardown_str);
178                 teardown_str = NULL;
179         }
180
181         tcflush (STDIN_FILENO, TCIFLUSH);
182         tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
183         setup_finished = FALSE;
184 }
185
186 MonoBoolean
187 ves_icall_System_ConsoleDriver_TtySetup (MonoString *teardown)
188 {
189         struct termios attr;
190         
191         MONO_ARCH_SAVE_REGS;
192
193         if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
194                 return FALSE;
195
196         attr = initial_attr;
197         attr.c_lflag &= ~ICANON;
198         attr.c_cc [VMIN] = 1;
199         attr.c_cc [VTIME] = 0;
200         if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
201                 return FALSE;
202
203         setup_finished = TRUE;
204         if (!atexit_called) {
205                 if (teardown != NULL)
206                         teardown_str = mono_string_to_utf8 (teardown);
207
208                 atexit (tty_teardown);
209         }
210
211         return TRUE;
212 }
213 #endif
214