3 * ConsoleDriver internal calls for Unix systems.
6 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 * Copyright (C) 2005-2009 Novell, Inc. (http://www.novell.com)
9 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
18 #ifdef HAVE_SYS_SELECT_H
19 # include <sys/select.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
24 #include <sys/types.h>
28 #include <mono/metadata/appdomain.h>
29 #include <mono/metadata/object-internals.h>
30 #include <mono/metadata/class-internals.h>
31 #include <mono/metadata/domain-internals.h>
32 #include <mono/metadata/gc-internals.h>
33 #include <mono/metadata/metadata.h>
34 #include <mono/metadata/threadpool.h>
35 #include <mono/utils/mono-signal-handler.h>
36 #include <mono/utils/mono-proclib.h>
37 #include <mono/utils/w32api.h>
38 #include <mono/utils/mono-errno.h>
40 /* On solaris, curses.h must come before both termios.h and term.h */
51 /* Needed for FIONREAD under solaris */
52 #ifdef HAVE_SYS_FILIO_H
53 # include <sys/filio.h>
55 #ifdef HAVE_SYS_IOCTL_H
56 # include <sys/ioctl.h>
59 #include <mono/metadata/console-io.h>
60 #include <mono/metadata/exception.h>
61 #include "icall-decl.h"
63 static gboolean setup_finished
;
64 static gboolean atexit_called
;
66 /* The string used to return the terminal to its previous state */
67 static gchar
*teardown_str
;
69 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
70 static gchar
*keypad_xmit_str
;
73 /* This is the last state used by Mono, used after a CONT signal is received */
74 static struct termios mono_attr
;
77 /* static void console_restore_signal_handlers (void); */
78 static void console_set_signal_handlers (void);
81 static GENERATE_TRY_GET_CLASS_WITH_CACHE (console
, "System", "Console");
85 mono_console_init (void)
89 /* Make sure the standard file descriptors are opened */
90 fd
= open ("/dev/null", O_RDWR
);
91 while (fd
>= 0 && fd
< 3) {
92 fd
= open ("/dev/null", O_RDWR
);
97 static struct termios initial_attr
;
100 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle
, MonoError
* error
)
102 return isatty (GPOINTER_TO_INT (handle
));
106 set_property (gint property
, gboolean value
)
109 gboolean callset
= FALSE
;
112 if (tcgetattr (STDIN_FILENO
, &attr
) == -1)
115 check
= (attr
.c_lflag
& property
) != 0;
116 if ((value
|| check
) && !(value
&& check
)) {
119 attr
.c_lflag
|= property
;
121 attr
.c_lflag
&= ~property
;
127 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &attr
) == -1)
135 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo
, MonoError
* error
)
137 return set_property (ECHO
, want_echo
);
141 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break
, MonoError
* error
)
143 return set_property (IGNBRK
, !want_break
);
147 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout
, MonoError
* error
)
151 struct timeval
*tvptr
;
157 FD_SET (STDIN_FILENO
, &rfds
);
159 divvy
= div (timeout
, 1000);
160 tv
.tv_sec
= divvy
.quot
;
161 tv
.tv_usec
= divvy
.rem
;
166 ret
= select (STDIN_FILENO
+ 1, &rfds
, NULL
, NULL
, tvptr
);
167 } while (ret
== -1 && errno
== EINTR
);
171 ret
= ioctl (STDIN_FILENO
, FIONREAD
, &nbytes
);
176 return (ret
> 0) ? ret
: 0;
179 static gint32 cols_and_lines
;
183 terminal_get_dimensions (void)
187 int save_errno
= errno
;
189 if (ioctl (STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0){
190 ret
= (ws
.ws_col
<< 16) | ws
.ws_row
;
191 mono_set_errno (save_errno
);
198 terminal_get_dimensions (void)
207 int unused G_GNUC_UNUSED
;
212 if (teardown_str
!= NULL
) {
213 unused
= write (STDOUT_FILENO
, teardown_str
, strlen (teardown_str
));
214 g_free (teardown_str
);
218 tcflush (STDIN_FILENO
, TCIFLUSH
);
219 tcsetattr (STDIN_FILENO
, TCSANOW
, &initial_attr
);
220 set_property (ECHO
, TRUE
);
221 setup_finished
= FALSE
;
225 do_console_cancel_event (void)
227 static MonoMethod
*System_Console_DoConsoleCancelEventBackground_method
= (MonoMethod
*)(intptr_t)-1;
230 if (mono_class_try_get_console_class () == NULL
)
233 if (System_Console_DoConsoleCancelEventBackground_method
== (gpointer
)(intptr_t)-1) {
234 System_Console_DoConsoleCancelEventBackground_method
= mono_class_get_method_from_name_checked (mono_class_try_get_console_class (), "DoConsoleCancelEventInBackground", 0, 0, error
);
235 mono_error_assert_ok (error
);
237 if (System_Console_DoConsoleCancelEventBackground_method
== NULL
)
240 mono_runtime_invoke_checked (System_Console_DoConsoleCancelEventBackground_method
, NULL
, NULL
, error
);
241 mono_error_assert_ok (error
);
244 static int need_cancel
= FALSE
;
245 /* this is executed from the finalizer thread */
247 mono_console_handle_async_ops (void)
251 do_console_cancel_event ();
255 static gboolean in_sigint
;
257 MONO_SIG_HANDLER_FUNC (static, sigint_handler
)
267 mono_gc_finalize_notify ();
268 mono_set_errno (save_errno
);
272 static struct sigaction save_sigcont
, save_sigint
, save_sigwinch
;
274 MONO_SIG_HANDLER_FUNC (static, sigcont_handler
)
276 int unused G_GNUC_UNUSED
;
277 // Ignore error, there is not much we can do in the sigcont handler.
278 tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
);
280 if (keypad_xmit_str
!= NULL
)
281 unused
= write (STDOUT_FILENO
, keypad_xmit_str
, strlen (keypad_xmit_str
));
283 // Call previous handler
284 if (save_sigcont
.sa_sigaction
!= NULL
&&
285 save_sigcont
.sa_sigaction
!= (void *)SIG_DFL
&&
286 save_sigcont
.sa_sigaction
!= (void *)SIG_IGN
)
287 (*save_sigcont
.sa_sigaction
) (MONO_SIG_HANDLER_PARAMS
);
290 MONO_SIG_HANDLER_FUNC (static, sigwinch_handler
)
292 int dims
= terminal_get_dimensions ();
294 cols_and_lines
= dims
;
296 // Call previous handler
297 if (save_sigwinch
.sa_sigaction
!= NULL
&&
298 save_sigwinch
.sa_sigaction
!= (void *)SIG_DFL
&&
299 save_sigwinch
.sa_sigaction
!= (void *)SIG_IGN
)
300 (*save_sigwinch
.sa_sigaction
) (MONO_SIG_HANDLER_PARAMS
);
304 * console_set_signal_handlers:
306 * Installs various signals handlers for the use of the console, as
309 * SIGCONT: this is received after the application has resumed execution
310 * if it was suspended with Control-Z before. This signal handler needs
311 * to resend the terminal sequence to send keyboard in keypad mode (this
312 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
315 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
316 * a thread from the thread pool which notifies all registered cancel_event
319 * SIGWINCH: is used to track changes to the console window when a GUI
320 * terminal is resized. It sets an internal variable that is checked
321 * by System.Console when the Terminfo driver has been activated.
324 console_set_signal_handlers ()
326 #if defined(HAVE_SIGACTION)
327 struct sigaction sigcont
, sigint
, sigwinch
;
329 memset (&sigcont
, 0, sizeof (struct sigaction
));
330 memset (&sigint
, 0, sizeof (struct sigaction
));
331 memset (&sigwinch
, 0, sizeof (struct sigaction
));
334 sigcont
.sa_handler
= (void (*)(int)) sigcont_handler
;
335 sigcont
.sa_flags
= SA_RESTART
;
336 sigemptyset (&sigcont
.sa_mask
);
337 sigaction (SIGCONT
, &sigcont
, &save_sigcont
);
340 sigint
.sa_handler
= (void (*)(int)) sigint_handler
;
341 sigint
.sa_flags
= SA_RESTART
;
342 sigemptyset (&sigint
.sa_mask
);
343 sigaction (SIGINT
, &sigint
, &save_sigint
);
345 // Window size changed
346 sigwinch
.sa_handler
= (void (*)(int)) sigwinch_handler
;
347 sigwinch
.sa_flags
= SA_RESTART
;
348 sigemptyset (&sigwinch
.sa_mask
);
349 sigaction (SIGWINCH
, &sigwinch
, &save_sigwinch
);
353 #if currently_unuused
355 // Currently unused, should we ever call the restore handler?
356 // Perhaps before calling into Process.Start?
359 console_restore_signal_handlers ()
361 sigaction (SIGCONT
, &save_sigcont
, NULL
);
362 sigaction (SIGINT
, &save_sigint
, NULL
);
363 sigaction (SIGWINCH
, &save_sigwinch
, NULL
);
368 set_control_chars (gchar
*control_chars
, const guchar
*cc
)
370 /* The index into the array comes from corlib/System/ControlCharacters.cs */
372 control_chars
[0] = cc
[VINTR
];
375 control_chars
[1] = cc
[VQUIT
];
378 control_chars
[2] = cc
[VERASE
];
381 control_chars
[3] = cc
[VKILL
];
384 control_chars
[4] = cc
[VEOF
];
387 control_chars
[5] = cc
[VTIME
];
390 control_chars
[6] = cc
[VMIN
];
393 control_chars
[7] = cc
[VSWTC
];
396 control_chars
[8] = cc
[VSTART
];
399 control_chars
[9] = cc
[VSTOP
];
402 control_chars
[10] = cc
[VSUSP
];
405 control_chars
[11] = cc
[VEOL
];
408 control_chars
[12] = cc
[VREPRINT
];
411 control_chars
[13] = cc
[VDISCARD
];
414 control_chars
[14] = cc
[VWERASE
];
417 control_chars
[15] = cc
[VLNEXT
];
420 control_chars
[16] = cc
[VEOL2
];
425 ves_icall_System_ConsoleDriver_TtySetup (MonoStringHandle keypad
, MonoStringHandle teardown
, MonoArrayHandleOut control_chars
, int **size
, MonoError
* error
)
427 // FIXME Lock around the globals?
431 dims
= terminal_get_dimensions ();
433 int cols
= 0, rows
= 0;
435 char *str
= g_getenv ("COLUMNS");
440 str
= g_getenv ("LINES");
446 if (cols
!= 0 && rows
!= 0)
447 cols_and_lines
= (cols
<< 16) | rows
;
451 cols_and_lines
= dims
;
454 *size
= &cols_and_lines
;
456 /* 17 is the number of entries set in set_control_chars() above.
457 * NCCS is the total size, but, by now, we only care about those 17 values*/
458 MonoArrayHandle control_chars_arr
= mono_array_new_handle (mono_domain_get (), mono_defaults
.byte_class
, 17, error
);
459 return_val_if_nok (error
, FALSE
);
461 MONO_HANDLE_ASSIGN (control_chars
, control_chars_arr
);
462 if (tcgetattr (STDIN_FILENO
, &initial_attr
) == -1)
465 mono_attr
= initial_attr
;
466 mono_attr
.c_lflag
&= ~(ICANON
);
467 mono_attr
.c_iflag
&= ~(IXON
|IXOFF
);
468 mono_attr
.c_cc
[VMIN
] = 1;
469 mono_attr
.c_cc
[VTIME
] = 0;
471 /* Disable C-y being used as a suspend character on OSX */
472 mono_attr
.c_cc
[VDSUSP
] = 255;
477 ret
= tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
);
479 } while (ret
== -1 && errno
== EINTR
);
485 set_control_chars (MONO_ARRAY_HANDLE_PIN (control_chars_arr
, gchar
, 0, &h
), mono_attr
.c_cc
);
486 mono_gchandle_free_internal (h
);
487 /* If initialized from another appdomain... */
491 keypad_xmit_str
= NULL
;
492 if (!MONO_HANDLE_IS_NULL (keypad
)) {
493 keypad_xmit_str
= mono_string_handle_to_utf8 (keypad
, error
);
494 return_val_if_nok (error
, FALSE
);
497 console_set_signal_handlers ();
498 setup_finished
= TRUE
;
499 if (!atexit_called
) {
500 if (!MONO_HANDLE_IS_NULL (teardown
)) {
501 teardown_str
= mono_string_handle_to_utf8 (teardown
, error
);
502 return_val_if_nok (error
, FALSE
);
505 mono_atexit (tty_teardown
);