Sat Oct 11 19:46:19 CEST 2008 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / console-io.c
1
2 /*
3  * console-io.c: ConsoleDriver internal calls
4  *
5  * Author:
6  *      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7  *
8  * Copyright (C) 2005 Novell, Inc. (http://www.novell.com)
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <signal.h>
17 #ifdef HAVE_SYS_TIME_H
18 #include <sys/time.h>
19 #endif
20 #include <sys/types.h>
21 #ifdef HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/object-internals.h>
26 #include <mono/metadata/class-internals.h>
27 #include <mono/metadata/domain-internals.h>
28 #include <mono/metadata/metadata.h>
29 #include <mono/metadata/threadpool.h>
30 /* On solaris, curses.h must come before both termios.h and term.h */
31 #ifdef HAVE_CURSES_H
32 #include <curses.h>
33 #endif
34 #ifdef HAVE_TERMIOS_H
35 #include <termios.h>
36 #endif
37 #ifdef HAVE_TERM_H
38 #include <term.h>
39 #endif
40 /* Needed for FIONREAD under solaris */
41 #ifdef HAVE_SYS_FILIO_H
42 #include <sys/filio.h>
43 #endif
44 #ifndef PLATFORM_WIN32
45 #ifndef TIOCGWINSZ
46 #include <sys/ioctl.h>
47 #endif
48 #endif
49
50 #include <mono/metadata/console-io.h>
51 #include <mono/metadata/exception.h>
52
53 static gboolean setup_finished;
54 static gboolean atexit_called;
55
56 /* The string used to return the terminal to its previous state */
57 static gchar *teardown_str;
58
59 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
60 static gchar *keypad_xmit_str;
61
62 #ifdef HAVE_TERMIOS_H
63 /* This is the last state used by Mono, used after a CONT signal is received */
64 static struct termios mono_attr;
65 #endif
66
67 #ifdef PLATFORM_WIN32
68 MonoBoolean
69 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
70 {
71         MONO_ARCH_SAVE_REGS;
72
73         return (GetFileType (handle) == FILE_TYPE_CHAR);
74 }
75
76 MonoBoolean
77 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
78 {
79         return FALSE;
80 }
81
82 MonoBoolean
83 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
84 {
85         return FALSE;
86 }
87
88 gint32
89 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
90 {
91         return FALSE;
92 }
93
94 MonoBoolean
95 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
96 {
97         return FALSE;
98 }
99
100 #else
101 static struct termios initial_attr;
102
103 MonoBoolean
104 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
105 {
106         MONO_ARCH_SAVE_REGS;
107
108         return isatty (GPOINTER_TO_INT (handle));
109 }
110
111 static MonoBoolean
112 set_property (gint property, gboolean value)
113 {
114         struct termios attr;
115         gboolean callset = FALSE;
116         gboolean check;
117         
118         MONO_ARCH_SAVE_REGS;
119
120         if (tcgetattr (STDIN_FILENO, &attr) == -1)
121                 return FALSE;
122
123         check = (attr.c_lflag & property) != 0;
124         if ((value || check) && !(value && check)) {
125                 callset = TRUE;
126                 if (value)
127                         attr.c_lflag |= property;
128                 else
129                         attr.c_lflag &= ~property;
130         }
131
132         if (!callset)
133                 return TRUE;
134
135         if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
136                 return FALSE;
137
138         mono_attr = attr;
139         return TRUE;
140 }
141
142 MonoBoolean
143 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
144 {
145         
146         return set_property (ECHO, want_echo);
147 }
148
149 MonoBoolean
150 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
151 {
152         return set_property (IGNBRK, !want_break);
153 }
154
155 gint32
156 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
157 {
158         fd_set rfds;
159         struct timeval tv;
160         struct timeval *tvptr;
161         div_t divvy;
162         int ret, nbytes;
163
164         MONO_ARCH_SAVE_REGS;
165
166         do {
167                 FD_ZERO (&rfds);
168                 FD_SET (STDIN_FILENO, &rfds);
169                 if (timeout >= 0) {
170                         divvy = div (timeout, 1000);
171                         tv.tv_sec = divvy.quot;
172                         tv.tv_usec = divvy.rem;
173                         tvptr = &tv;
174                 } else {
175                         tvptr = NULL;
176                 }
177                 ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
178         } while (ret == -1 && errno == EINTR);
179
180         if (ret > 0) {
181                 nbytes = 0;
182                 ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
183                 if (ret >= 0)
184                         ret = nbytes;
185         }
186
187         return (ret > 0) ? ret : 0;
188 }
189
190 static gint32 cols_and_lines;
191
192 #ifdef TIOCGWINSZ
193 static int
194 terminal_get_dimensions (void)
195 {
196         struct winsize ws;
197
198         if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0)
199                 return (ws.ws_col << 16) | ws.ws_row;
200
201         return -1;
202 }
203 #else
204 static int
205 terminal_get_dimensions (void)
206 {
207         return -1;
208 }
209 #endif
210
211 static void
212 tty_teardown (void)
213 {
214         MONO_ARCH_SAVE_REGS;
215
216         if (!setup_finished)
217                 return;
218
219         if (teardown_str != NULL) {
220                 write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
221                 g_free (teardown_str);
222                 teardown_str = NULL;
223         }
224
225         tcflush (STDIN_FILENO, TCIFLUSH);
226         tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
227         set_property (ECHO, TRUE);
228         setup_finished = FALSE;
229 }
230
231 static void
232 do_console_cancel_event (void)
233 {
234         static MonoClassField *cancel_handler_field;
235         MonoDomain *domain = mono_domain_get ();
236         MonoClass *klass;
237         MonoDelegate *load_value;
238         MonoMethod *method;
239         MonoMethodMessage *msg;
240         MonoMethod *im;
241
242         if (!domain->domain)
243                 return;
244
245         klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
246         if (klass == NULL)
247                 return;
248
249         if (cancel_handler_field == NULL) {
250                 cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
251                 g_assert (cancel_handler_field);
252         }
253
254         mono_field_static_get_value (mono_class_vtable (domain, klass), cancel_handler_field, &load_value);
255         if (load_value == NULL)
256                 return;
257
258         klass = load_value->object.vtable->klass;
259         method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
260         g_assert (method != NULL);
261         im = mono_get_delegate_invoke (method->klass);
262         msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
263         mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
264 }
265
266 static gboolean in_sigint;
267 static void
268 sigint_handler (int signo)
269 {
270         MONO_ARCH_SAVE_REGS;
271
272         if (in_sigint)
273                 return;
274
275         in_sigint = TRUE;
276         do_console_cancel_event ();
277         in_sigint = FALSE;
278 }
279
280 static struct sigaction save_sigcont, save_sigint, save_sigwinch;
281
282 static void
283 sigcont_handler (int signo, void *the_siginfo, void *data)
284 {
285         // Ignore error, there is not much we can do in the sigcont handler.
286         tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);
287
288         if (keypad_xmit_str != NULL)
289                 write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));
290
291         // Call previous handler
292         if (save_sigcont.sa_sigaction != NULL &&
293             save_sigcont.sa_sigaction != (void *)SIG_DFL &&
294             save_sigcont.sa_sigaction != (void *)SIG_IGN)
295                 (*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
296 }
297
298 static void
299 sigwinch_handler (int signo, void *the_siginfo, void *data)
300 {
301         int dims = terminal_get_dimensions ();
302         if (dims != -1)
303                 cols_and_lines = dims;
304         
305         // Call previous handler
306         if (save_sigwinch.sa_sigaction != NULL &&
307             save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
308             save_sigwinch.sa_sigaction != (void *)SIG_IGN)
309                 (*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
310 }
311
312 void
313 console_set_signal_handlers ()
314 {
315         struct sigaction sigcont, sigint, sigwinch;
316
317         memset (&sigcont, 0, sizeof (struct sigaction));
318         memset (&sigint, 0, sizeof (struct sigaction));
319         memset (&sigwinch, 0, sizeof (struct sigaction));
320         
321         // Continuing
322         sigcont.sa_handler = (void *) sigcont_handler;
323         sigcont.sa_flags = 0;
324         sigemptyset (&sigcont.sa_mask);
325         sigaction (SIGCONT, &sigcont, &save_sigcont);
326         
327         // Interrupt handler
328         sigint.sa_handler = sigint_handler;
329         sigint.sa_flags = 0;
330         sigemptyset (&sigint.sa_mask);
331         sigaction (SIGINT, &sigint, &save_sigint);
332
333         // Window size changed
334         sigwinch.sa_handler = (void *) sigwinch_handler;
335         sigwinch.sa_flags = 0;
336         sigemptyset (&sigwinch.sa_mask);
337         sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
338 }
339
340 //
341 // Currently unused, should we ever call the restore handler?
342 // Perhaps before calling into Process.Start?
343 //
344 void
345 console_restore_signal_handlers ()
346 {
347         sigaction (SIGCONT, &save_sigcont, NULL);
348         sigaction (SIGINT, &save_sigint, NULL);
349         sigaction (SIGWINCH, &save_sigwinch, NULL);
350 }
351
352 MonoBoolean
353 ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, char *verase, char *vsusp, char*intr, int **size)
354 {
355         int dims;
356
357         MONO_ARCH_SAVE_REGS;
358
359         dims = terminal_get_dimensions ();
360         if (dims == -1){
361                 int cols = 0, rows = 0;
362                                       
363                 char *str = getenv ("COLUMNS");
364                 if (str != NULL)
365                         cols = atoi (str);
366                 str = getenv ("LINES");
367                 if (str != NULL)
368                         rows = atoi (str);
369
370                 if (cols != 0 && rows != 0)
371                         cols_and_lines = (cols << 16) | rows;
372                 else
373                         cols_and_lines = -1;
374         } else {
375                 cols_and_lines = dims;
376         }
377         
378         *size = &cols_and_lines;
379         
380         *verase = '\0';
381         *vsusp = '\0';
382         *intr = '\0';
383         if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
384                 return FALSE;
385
386         mono_attr = initial_attr;
387         mono_attr.c_lflag &= ~(ICANON);
388         mono_attr.c_iflag &= ~(IXON|IXOFF);
389         mono_attr.c_cc [VMIN] = 1;
390         mono_attr.c_cc [VTIME] = 0;
391         if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
392                 return FALSE;
393
394         *verase = initial_attr.c_cc [VERASE];
395         *vsusp = initial_attr.c_cc [VSUSP];
396         *intr = initial_attr.c_cc [VINTR];
397         /* If initialized from another appdomain... */
398         if (setup_finished)
399                 return TRUE;
400
401         keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;
402         
403         console_set_signal_handlers ();
404         setup_finished = TRUE;
405         if (!atexit_called) {
406                 if (teardown != NULL)
407                         teardown_str = mono_string_to_utf8 (teardown);
408
409                 atexit (tty_teardown);
410         }
411
412         return TRUE;
413 }
414
415 #endif /* !PLATFORM_WIN32 */