Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 510 lines (436 sloc) 12.294 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
/*
* console-io.c: ConsoleDriver internal calls for Unix systems.
*
* Author:
* Gonzalo Paniagua Javier (gonzalo@ximian.com)
*
* Copyright (C) 2005-2009 Novell, Inc. (http://www.novell.com)
*/
#if defined(__native_client__)
#include "console-null.c"
#else

#include <config.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <mono/metadata/appdomain.h>
#include <mono/metadata/object-internals.h>
#include <mono/metadata/class-internals.h>
#include <mono/metadata/domain-internals.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/metadata.h>
#include <mono/metadata/threadpool.h>
#include <mono/utils/mono-signal-handler.h>

/* On solaris, curses.h must come before both termios.h and term.h */
#ifdef HAVE_CURSES_H
# include <curses.h>
#endif
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#ifdef HAVE_TERM_H
# include <term.h>
#endif

/* Needed for FIONREAD under solaris */
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif

#include <mono/metadata/console-io.h>
#include <mono/metadata/exception.h>

static gboolean setup_finished;
static gboolean atexit_called;

/* The string used to return the terminal to its previous state */
static gchar *teardown_str;

/* The string used to set the terminal into keypad xmit mode after SIGCONT is received */
static gchar *keypad_xmit_str;

#ifdef HAVE_TERMIOS_H
/* This is the last state used by Mono, used after a CONT signal is received */
static struct termios mono_attr;
#endif

/* static void console_restore_signal_handlers (void); */
static void console_set_signal_handlers (void);

void
mono_console_init (void)
{
int fd;

/* Make sure the standard file descriptors are opened */
fd = open ("/dev/null", O_RDWR);
while (fd >= 0 && fd < 3) {
fd = open ("/dev/null", O_RDWR);
}
close (fd);
}

static struct termios initial_attr;

MonoBoolean
ves_icall_System_ConsoleDriver_Isatty (HANDLE handle)
{
MONO_ARCH_SAVE_REGS;

return isatty (GPOINTER_TO_INT (handle));
}

static MonoBoolean
set_property (gint property, gboolean value)
{
struct termios attr;
gboolean callset = FALSE;
gboolean check;

MONO_ARCH_SAVE_REGS;

if (tcgetattr (STDIN_FILENO, &attr) == -1)
return FALSE;

check = (attr.c_lflag & property) != 0;
if ((value || check) && !(value && check)) {
callset = TRUE;
if (value)
attr.c_lflag |= property;
else
attr.c_lflag &= ~property;
}

if (!callset)
return TRUE;

if (tcsetattr (STDIN_FILENO, TCSANOW, &attr) == -1)
return FALSE;

mono_attr = attr;
return TRUE;
}

MonoBoolean
ves_icall_System_ConsoleDriver_SetEcho (MonoBoolean want_echo)
{

return set_property (ECHO, want_echo);
}

MonoBoolean
ves_icall_System_ConsoleDriver_SetBreak (MonoBoolean want_break)
{
return set_property (IGNBRK, !want_break);
}

gint32
ves_icall_System_ConsoleDriver_InternalKeyAvailable (gint32 timeout)
{
fd_set rfds;
struct timeval tv;
struct timeval *tvptr;
div_t divvy;
int ret, nbytes;

MONO_ARCH_SAVE_REGS;

do {
FD_ZERO (&rfds);
FD_SET (STDIN_FILENO, &rfds);
if (timeout >= 0) {
divvy = div (timeout, 1000);
tv.tv_sec = divvy.quot;
tv.tv_usec = divvy.rem;
tvptr = &tv;
} else {
tvptr = NULL;
}
ret = select (STDIN_FILENO + 1, &rfds, NULL, NULL, tvptr);
} while (ret == -1 && errno == EINTR);

if (ret > 0) {
nbytes = 0;
ret = ioctl (STDIN_FILENO, FIONREAD, &nbytes);
if (ret >= 0)
ret = nbytes;
}

return (ret > 0) ? ret : 0;
}

static gint32 cols_and_lines;

#ifdef TIOCGWINSZ
static int
terminal_get_dimensions (void)
{
struct winsize ws;
int ret;
int save_errno = errno;

if (ioctl (STDIN_FILENO, TIOCGWINSZ, &ws) == 0){
ret = (ws.ws_col << 16) | ws.ws_row;
errno = save_errno;
return ret;
}
return -1;
}
#else
static int
terminal_get_dimensions (void)
{
return -1;
}
#endif

static void
tty_teardown (void)
{
int unused;

MONO_ARCH_SAVE_REGS;

if (!setup_finished)
return;

if (teardown_str != NULL) {
unused = write (STDOUT_FILENO, teardown_str, strlen (teardown_str));
g_free (teardown_str);
teardown_str = NULL;
}

tcflush (STDIN_FILENO, TCIFLUSH);
tcsetattr (STDIN_FILENO, TCSANOW, &initial_attr);
set_property (ECHO, TRUE);
setup_finished = FALSE;
}

static void
do_console_cancel_event (void)
{
static MonoClassField *cancel_handler_field;
MonoDomain *domain = mono_domain_get ();
MonoClass *klass;
MonoDelegate *load_value;
MonoMethod *method;
MonoMethodMessage *msg;
MonoMethod *im;
MonoVTable *vtable;

/* FIXME: this should likely iterate all the domains, instead */
if (!domain->domain)
return;

klass = mono_class_from_name (mono_defaults.corlib, "System", "Console");
if (klass == NULL)
return;

if (cancel_handler_field == NULL) {
cancel_handler_field = mono_class_get_field_from_name (klass, "cancel_handler");
g_assert (cancel_handler_field);
}

vtable = mono_class_vtable_full (domain, klass, FALSE);
if (vtable == NULL)
return;
mono_field_static_get_value (vtable, cancel_handler_field, &load_value);
if (load_value == NULL)
return;

klass = load_value->object.vtable->klass;
method = mono_class_get_method_from_name (klass, "BeginInvoke", -1);
g_assert (method != NULL);
im = mono_get_delegate_invoke (method->klass);
msg = mono_method_call_message_new (method, NULL, im, NULL, NULL);
mono_thread_pool_add ((MonoObject *) load_value, msg, NULL, NULL);
}

static int need_cancel = FALSE;
/* this is executed from the finalizer thread */
void
mono_console_handle_async_ops (void)
{
if (need_cancel) {
need_cancel = FALSE;
do_console_cancel_event ();
}
}

static gboolean in_sigint;

MONO_SIGNAL_HANDLER_FUNC (static, sigint_handler, (int signo))
{
int save_errno;
MONO_ARCH_SAVE_REGS;

if (in_sigint)
return;

in_sigint = TRUE;
save_errno = errno;
need_cancel = TRUE;
mono_gc_finalize_notify ();
errno = save_errno;
in_sigint = FALSE;
}

static struct sigaction save_sigcont, save_sigint, save_sigwinch;

MONO_SIGNAL_HANDLER_FUNC (static, sigcont_handler, (int signo, void *the_siginfo, void *data))
{
int unused;
// Ignore error, there is not much we can do in the sigcont handler.
tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr);

if (keypad_xmit_str != NULL)
unused = write (STDOUT_FILENO, keypad_xmit_str, strlen (keypad_xmit_str));

// Call previous handler
if (save_sigcont.sa_sigaction != NULL &&
save_sigcont.sa_sigaction != (void *)SIG_DFL &&
save_sigcont.sa_sigaction != (void *)SIG_IGN)
(*save_sigcont.sa_sigaction) (signo, the_siginfo, data);
}

MONO_SIGNAL_HANDLER_FUNC (static, sigwinch_handler, (int signo, void *the_siginfo, void *data))
{
int dims = terminal_get_dimensions ();
if (dims != -1)
cols_and_lines = dims;

// Call previous handler
if (save_sigwinch.sa_sigaction != NULL &&
save_sigwinch.sa_sigaction != (void *)SIG_DFL &&
save_sigwinch.sa_sigaction != (void *)SIG_IGN)
(*save_sigwinch.sa_sigaction) (signo, the_siginfo, data);
}

/*
* console_set_signal_handlers:
*
* Installs various signals handlers for the use of the console, as
* follows:
*
* SIGCONT: this is received after the application has resumed execution
* if it was suspended with Control-Z before. This signal handler needs
* to resend the terminal sequence to send keyboard in keypad mode (this
* is the difference between getting a cuu1 code or a kcuu1 code for up-arrow
* for example
*
* SIGINT: invokes the System.Console.DoConsoleCancelEvent method using
* a thread from the thread pool which notifies all registered cancel_event
* listeners.
*
* SIGWINCH: is used to track changes to the console window when a GUI
* terminal is resized. It sets an internal variable that is checked
* by System.Console when the Terminfo driver has been activated.
*/
static void
console_set_signal_handlers ()
{
struct sigaction sigcont, sigint, sigwinch;

memset (&sigcont, 0, sizeof (struct sigaction));
memset (&sigint, 0, sizeof (struct sigaction));
memset (&sigwinch, 0, sizeof (struct sigaction));

// Continuing
sigcont.sa_handler = (void *) sigcont_handler;
sigcont.sa_flags = 0;
sigemptyset (&sigcont.sa_mask);
sigaction (SIGCONT, &sigcont, &save_sigcont);

// Interrupt handler
sigint.sa_handler = (void *) sigint_handler;
sigint.sa_flags = 0;
sigemptyset (&sigint.sa_mask);
sigaction (SIGINT, &sigint, &save_sigint);

// Window size changed
sigwinch.sa_handler = (void *) sigwinch_handler;
sigwinch.sa_flags = 0;
sigemptyset (&sigwinch.sa_mask);
sigaction (SIGWINCH, &sigwinch, &save_sigwinch);
}

#if currently_unuused
//
// Currently unused, should we ever call the restore handler?
// Perhaps before calling into Process.Start?
//
void
console_restore_signal_handlers ()
{
sigaction (SIGCONT, &save_sigcont, NULL);
sigaction (SIGINT, &save_sigint, NULL);
sigaction (SIGWINCH, &save_sigwinch, NULL);
}
#endif

static void
set_control_chars (MonoArray *control_chars, const guchar *cc)
{
/* The index into the array comes from corlib/System/ControlCharacters.cs */
#ifdef VINTR
mono_array_set (control_chars, gchar, 0, cc [VINTR]);
#endif
#ifdef VQUIT
mono_array_set (control_chars, gchar, 1, cc [VQUIT]);
#endif
#ifdef VERASE
mono_array_set (control_chars, gchar, 2, cc [VERASE]);
#endif
#ifdef VKILL
mono_array_set (control_chars, gchar, 3, cc [VKILL]);
#endif
#ifdef VEOF
mono_array_set (control_chars, gchar, 4, cc [VEOF]);
#endif
#ifdef VTIME
mono_array_set (control_chars, gchar, 5, cc [VTIME]);
#endif
#ifdef VMIN
mono_array_set (control_chars, gchar, 6, cc [VMIN]);
#endif
#ifdef VSWTC
mono_array_set (control_chars, gchar, 7, cc [VSWTC]);
#endif
#ifdef VSTART
mono_array_set (control_chars, gchar, 8, cc [VSTART]);
#endif
#ifdef VSTOP
mono_array_set (control_chars, gchar, 9, cc [VSTOP]);
#endif
#ifdef VSUSP
mono_array_set (control_chars, gchar, 10, cc [VSUSP]);
#endif
#ifdef VEOL
mono_array_set (control_chars, gchar, 11, cc [VEOL]);
#endif
#ifdef VREPRINT
mono_array_set (control_chars, gchar, 12, cc [VREPRINT]);
#endif
#ifdef VDISCARD
mono_array_set (control_chars, gchar, 13, cc [VDISCARD]);
#endif
#ifdef VWERASE
mono_array_set (control_chars, gchar, 14, cc [VWERASE]);
#endif
#ifdef VLNEXT
mono_array_set (control_chars, gchar, 15, cc [VLNEXT]);
#endif
#ifdef VEOL2
mono_array_set (control_chars, gchar, 16, cc [VEOL2]);
#endif
}

MonoBoolean
ves_icall_System_ConsoleDriver_TtySetup (MonoString *keypad, MonoString *teardown, MonoArray **control_chars, int **size)
{
int dims;

MONO_ARCH_SAVE_REGS;

dims = terminal_get_dimensions ();
if (dims == -1){
int cols = 0, rows = 0;

const char *str = g_getenv ("COLUMNS");
if (str != NULL)
cols = atoi (str);
str = g_getenv ("LINES");
if (str != NULL)
rows = atoi (str);

if (cols != 0 && rows != 0)
cols_and_lines = (cols << 16) | rows;
else
cols_and_lines = -1;
} else {
cols_and_lines = dims;
}

*size = &cols_and_lines;

/* 17 is the number of entries set in set_control_chars() above.
* NCCS is the total size, but, by now, we only care about those 17 values*/
mono_gc_wbarrier_generic_store (control_chars, (MonoObject*) mono_array_new (mono_domain_get (), mono_defaults.byte_class, 17));
if (tcgetattr (STDIN_FILENO, &initial_attr) == -1)
return FALSE;

mono_attr = initial_attr;
mono_attr.c_lflag &= ~(ICANON);
mono_attr.c_iflag &= ~(IXON|IXOFF);
mono_attr.c_cc [VMIN] = 1;
mono_attr.c_cc [VTIME] = 0;
#ifdef VDSUSP
/* Disable C-y being used as a suspend character on OSX */
mono_attr.c_cc [VDSUSP] = 255;
#endif
if (tcsetattr (STDIN_FILENO, TCSANOW, &mono_attr) == -1)
return FALSE;

set_control_chars (*control_chars, mono_attr.c_cc);
/* If initialized from another appdomain... */
if (setup_finished)
return TRUE;

keypad_xmit_str = keypad != NULL ? mono_string_to_utf8 (keypad) : NULL;

console_set_signal_handlers ();
setup_finished = TRUE;
if (!atexit_called) {
if (teardown != NULL)
teardown_str = mono_string_to_utf8 (teardown);

atexit (tty_teardown);
}

return TRUE;
}
#endif /* #if defined(__native_client__) */
Something went wrong with that request. Please try again.