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>
39 /* On solaris, curses.h must come before both termios.h and term.h */
50 /* Needed for FIONREAD under solaris */
51 #ifdef HAVE_SYS_FILIO_H
52 # include <sys/filio.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 # include <sys/ioctl.h>
58 #include <mono/metadata/console-io.h>
59 #include <mono/metadata/exception.h>
61 static gboolean setup_finished
;
62 static gboolean atexit_called
;
64 /* The string used to return the terminal to its previous state */
65 static gchar
*teardown_str
;
67 /* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
68 static gchar
*keypad_xmit_str
;
71 /* This is the last state used by Mono, used after a CONT signal is received */
72 static struct termios mono_attr
;
75 /* static void console_restore_signal_handlers (void); */
76 static void console_set_signal_handlers (void);
79 mono_console_init (void)
83 /* Make sure the standard file descriptors are opened */
84 fd
= open ("/dev/null", O_RDWR
);
85 while (fd
>= 0 && fd
< 3) {
86 fd
= open ("/dev/null", O_RDWR
);
91 static struct termios initial_attr
;
94 ves_icall_System_ConsoleDriver_Isatty (HANDLE handle
, MonoError
* error
)
96 return isatty (GPOINTER_TO_INT (handle
));
100 set_property (gint property
, gboolean value
)
103 gboolean callset
= FALSE
;
106 if (tcgetattr (STDIN_FILENO
, &attr
) == -1)
109 check
= (attr
.c_lflag
& property
) != 0;
110 if ((value
|| check
) && !(value
&& check
)) {
113 attr
.c_lflag
|= property
;
115 attr
.c_lflag
&= ~property
;
121 if (tcsetattr (STDIN_FILENO
, TCSANOW
, &attr
) == -1)
129 ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo
, MonoError
* error
)
131 return set_property (ECHO
, want_echo
);
135 ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break
, MonoError
* error
)
137 return set_property (IGNBRK
, !want_break
);
141 ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout
, MonoError
* error
)
145 struct timeval
*tvptr
;
151 FD_SET (STDIN_FILENO
, &rfds
);
153 divvy
= div (timeout
, 1000);
154 tv
.tv_sec
= divvy
.quot
;
155 tv
.tv_usec
= divvy
.rem
;
160 ret
= select (STDIN_FILENO
+ 1, &rfds
, NULL
, NULL
, tvptr
);
161 } while (ret
== -1 && errno
== EINTR
);
165 ret
= ioctl (STDIN_FILENO
, FIONREAD
, &nbytes
);
170 return (ret
> 0) ? ret
: 0;
173 static gint32 cols_and_lines
;
177 terminal_get_dimensions (void)
181 int save_errno
= errno
;
183 if (ioctl (STDIN_FILENO
, TIOCGWINSZ
, &ws
) == 0){
184 ret
= (ws
.ws_col
<< 16) | ws
.ws_row
;
192 terminal_get_dimensions (void)
201 int unused G_GNUC_UNUSED
;
206 if (teardown_str
!= NULL
) {
207 unused
= write (STDOUT_FILENO
, teardown_str
, strlen (teardown_str
));
208 g_free (teardown_str
);
212 tcflush (STDIN_FILENO
, TCIFLUSH
);
213 tcsetattr (STDIN_FILENO
, TCSANOW
, &initial_attr
);
214 set_property (ECHO
, TRUE
);
215 setup_finished
= FALSE
;
219 do_console_cancel_event (void)
221 static MonoMethod
*System_Console_DoConsoleCancelEventBackground_method
= ((gpointer
)-1);
224 if (mono_defaults
.console_class
== NULL
)
227 if (System_Console_DoConsoleCancelEventBackground_method
== ((gpointer
)-1))
228 System_Console_DoConsoleCancelEventBackground_method
= mono_class_get_method_from_name (mono_defaults
.console_class
, "DoConsoleCancelEventInBackground", 0);
229 if (System_Console_DoConsoleCancelEventBackground_method
== NULL
)
232 mono_runtime_invoke_checked (System_Console_DoConsoleCancelEventBackground_method
, NULL
, NULL
, error
);
233 mono_error_assert_ok (error
);
236 static int need_cancel
= FALSE
;
237 /* this is executed from the finalizer thread */
239 mono_console_handle_async_ops (void)
243 do_console_cancel_event ();
247 static gboolean in_sigint
;
249 MONO_SIG_HANDLER_FUNC (static, sigint_handler
)
259 mono_gc_finalize_notify ();
264 static struct sigaction save_sigcont
, save_sigint
, save_sigwinch
;
266 MONO_SIG_HANDLER_FUNC (static, sigcont_handler
)
268 int unused G_GNUC_UNUSED
;
269 // Ignore error, there is not much we can do in the sigcont handler.
270 tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
);
272 if (keypad_xmit_str
!= NULL
)
273 unused
= write (STDOUT_FILENO
, keypad_xmit_str
, strlen (keypad_xmit_str
));
275 // Call previous handler
276 if (save_sigcont
.sa_sigaction
!= NULL
&&
277 save_sigcont
.sa_sigaction
!= (void *)SIG_DFL
&&
278 save_sigcont
.sa_sigaction
!= (void *)SIG_IGN
)
279 (*save_sigcont
.sa_sigaction
) (MONO_SIG_HANDLER_PARAMS
);
282 MONO_SIG_HANDLER_FUNC (static, sigwinch_handler
)
284 int dims
= terminal_get_dimensions ();
286 cols_and_lines
= dims
;
288 // Call previous handler
289 if (save_sigwinch
.sa_sigaction
!= NULL
&&
290 save_sigwinch
.sa_sigaction
!= (void *)SIG_DFL
&&
291 save_sigwinch
.sa_sigaction
!= (void *)SIG_IGN
)
292 (*save_sigwinch
.sa_sigaction
) (MONO_SIG_HANDLER_PARAMS
);
296 * console_set_signal_handlers:
298 * Installs various signals handlers for the use of the console, as
301 * SIGCONT: this is received after the application has resumed execution
302 * if it was suspended with Control-Z before. This signal handler needs
303 * to resend the terminal sequence to send keyboard in keypad mode (this
304 * is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
307 * SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
308 * a thread from the thread pool which notifies all registered cancel_event
311 * SIGWINCH: is used to track changes to the console window when a GUI
312 * terminal is resized. It sets an internal variable that is checked
313 * by System.Console when the Terminfo driver has been activated.
316 console_set_signal_handlers ()
318 #if defined(HAVE_SIGACTION)
319 struct sigaction sigcont
, sigint
, sigwinch
;
321 memset (&sigcont
, 0, sizeof (struct sigaction
));
322 memset (&sigint
, 0, sizeof (struct sigaction
));
323 memset (&sigwinch
, 0, sizeof (struct sigaction
));
326 sigcont
.sa_handler
= (void (*)(int)) sigcont_handler
;
327 sigcont
.sa_flags
= SA_RESTART
;
328 sigemptyset (&sigcont
.sa_mask
);
329 sigaction (SIGCONT
, &sigcont
, &save_sigcont
);
332 sigint
.sa_handler
= (void (*)(int)) sigint_handler
;
333 sigint
.sa_flags
= SA_RESTART
;
334 sigemptyset (&sigint
.sa_mask
);
335 sigaction (SIGINT
, &sigint
, &save_sigint
);
337 // Window size changed
338 sigwinch
.sa_handler
= (void (*)(int)) sigwinch_handler
;
339 sigwinch
.sa_flags
= SA_RESTART
;
340 sigemptyset (&sigwinch
.sa_mask
);
341 sigaction (SIGWINCH
, &sigwinch
, &save_sigwinch
);
345 #if currently_unuused
347 // Currently unused, should we ever call the restore handler?
348 // Perhaps before calling into Process.Start?
351 console_restore_signal_handlers ()
353 sigaction (SIGCONT
, &save_sigcont
, NULL
);
354 sigaction (SIGINT
, &save_sigint
, NULL
);
355 sigaction (SIGWINCH
, &save_sigwinch
, NULL
);
360 set_control_chars (gchar
*control_chars
, const guchar
*cc
)
362 /* The index into the array comes from corlib/System/ControlCharacters.cs */
364 control_chars
[0] = cc
[VINTR
];
367 control_chars
[1] = cc
[VQUIT
];
370 control_chars
[2] = cc
[VERASE
];
373 control_chars
[3] = cc
[VKILL
];
376 control_chars
[4] = cc
[VEOF
];
379 control_chars
[5] = cc
[VTIME
];
382 control_chars
[6] = cc
[VMIN
];
385 control_chars
[7] = cc
[VSWTC
];
388 control_chars
[8] = cc
[VSTART
];
391 control_chars
[9] = cc
[VSTOP
];
394 control_chars
[10] = cc
[VSUSP
];
397 control_chars
[11] = cc
[VEOL
];
400 control_chars
[12] = cc
[VREPRINT
];
403 control_chars
[13] = cc
[VDISCARD
];
406 control_chars
[14] = cc
[VWERASE
];
409 control_chars
[15] = cc
[VLNEXT
];
412 control_chars
[16] = cc
[VEOL2
];
417 ves_icall_System_ConsoleDriver_TtySetup (MonoStringHandle keypad
, MonoStringHandle teardown
, MonoArrayHandleOut control_chars
, int **size
, MonoError
* error
)
419 // FIXME Lock around the globals?
423 dims
= terminal_get_dimensions ();
425 int cols
= 0, rows
= 0;
427 char *str
= g_getenv ("COLUMNS");
432 str
= g_getenv ("LINES");
438 if (cols
!= 0 && rows
!= 0)
439 cols_and_lines
= (cols
<< 16) | rows
;
443 cols_and_lines
= dims
;
446 *size
= &cols_and_lines
;
448 /* 17 is the number of entries set in set_control_chars() above.
449 * NCCS is the total size, but, by now, we only care about those 17 values*/
450 MonoArrayHandle control_chars_arr
= mono_array_new_handle (mono_domain_get (), mono_defaults
.byte_class
, 17, error
);
451 return_val_if_nok (error
, FALSE
);
453 MONO_HANDLE_ASSIGN (control_chars
, control_chars_arr
);
454 if (tcgetattr (STDIN_FILENO
, &initial_attr
) == -1)
457 mono_attr
= initial_attr
;
458 mono_attr
.c_lflag
&= ~(ICANON
);
459 mono_attr
.c_iflag
&= ~(IXON
|IXOFF
);
460 mono_attr
.c_cc
[VMIN
] = 1;
461 mono_attr
.c_cc
[VTIME
] = 0;
463 /* Disable C-y being used as a suspend character on OSX */
464 mono_attr
.c_cc
[VDSUSP
] = 255;
469 ret
= tcsetattr (STDIN_FILENO
, TCSANOW
, &mono_attr
);
471 } while (ret
== -1 && errno
== EINTR
);
477 set_control_chars (MONO_ARRAY_HANDLE_PIN (control_chars_arr
, gchar
, 0, &h
), mono_attr
.c_cc
);
478 mono_gchandle_free (h
);
479 /* If initialized from another appdomain... */
483 keypad_xmit_str
= NULL
;
484 if (!MONO_HANDLE_IS_NULL (keypad
)) {
485 keypad_xmit_str
= mono_string_handle_to_utf8 (keypad
, error
);
486 return_val_if_nok (error
, FALSE
);
489 console_set_signal_handlers ();
490 setup_finished
= TRUE
;
491 if (!atexit_called
) {
492 if (!MONO_HANDLE_IS_NULL (teardown
)) {
493 teardown_str
= mono_string_handle_to_utf8 (teardown
, error
);
494 return_val_if_nok (error
, FALSE
);
497 mono_atexit (tty_teardown
);