diff --git a/support/tock_support.h b/support/tock_support.h index 1c3cb2a..da93ee3 100644 --- a/support/tock_support.h +++ b/support/tock_support.h @@ -26,6 +26,10 @@ #include #include #include +#include +#include +#include + //{{{ mostneg/mostpos #define occam_mostneg_bool false @@ -355,4 +359,53 @@ static inline double occam_DSQRT (double v, const char *pos) { } //}}} +//{{{ Terminal handling +static bool tock_uses_tty; +static struct termios tock_saved_termios; + +static void tock_restore_terminal() occam_unused; +static void tock_restore_terminal() +{ + //{{{ restore terminal + if (tock_uses_tty) { + if (tcsetattr (0, TCSAFLUSH, &tock_saved_termios) != 0) { + fprintf (stderr, "Tock: tcsetattr failed\n"); + exit (1); + } + + tock_uses_tty = false; + } + //}}} +} + +static void tock_configure_terminal(bool) occam_unused; +static void tock_configure_terminal(bool uses_stdin) +{ + //{{{ configure terminal + tock_uses_tty = uses_stdin && isatty (0); + if (tock_uses_tty) { + struct termios term; + + if (tcgetattr (0, &term) != 0) { + fprintf (stderr, "Tock: tcgetattr failed\n"); + exit (1); + } + tock_saved_termios = term; + + // Disable canonicalised input and echoing. + term.c_lflag &= ~(ICANON | ECHO); + // Satisfy a read request when one character is available. + term.c_cc[VMIN] = 1; + // Block read requests until VMIN characters are available. + term.c_cc[VTIME] = 0; + + if (tcsetattr (0, TCSANOW, &term) != 0) { + fprintf (stderr, "Tock: tcsetattr failed\n"); + exit (1); + } + } + //}}} +} + + #endif diff --git a/support/tock_support_cif.h b/support/tock_support_cif.h index 43703f5..86c47ab 100644 --- a/support/tock_support_cif.h +++ b/support/tock_support_cif.h @@ -121,22 +121,9 @@ static void tock_tlp_output_kill (Workspace wptr, Channel *kill) { //}}} //{{{ CCSP startup and terminal handling -static bool tock_uses_tty; -static struct termios tock_saved_termios; - static void tock_exit_handler (int status, word core) occam_unused; static void tock_exit_handler (int status, word core) { - //{{{ restore terminal - if (tock_uses_tty) { - if (tcsetattr (0, TCSAFLUSH, &tock_saved_termios) != 0) { - fprintf (stderr, "Tock: tcsetattr failed\n"); - exit (1); - } - - tock_uses_tty = false; - } - //}}} - + tock_restore_terminal(); ccsp_default_exit_handler (status, core); } @@ -144,30 +131,7 @@ static void tock_init_ccsp (bool uses_stdin) occam_unused; static void tock_init_ccsp (bool uses_stdin) { ccsp_set_branding ("Tock"); - //{{{ configure terminal - tock_uses_tty = uses_stdin && isatty (0); - if (tock_uses_tty) { - struct termios term; - - if (tcgetattr (0, &term) != 0) { - fprintf (stderr, "Tock: tcgetattr failed\n"); - exit (1); - } - tock_saved_termios = term; - - // Disable canonicalised input and echoing. - term.c_lflag &= ~(ICANON | ECHO); - // Satisfy a read request when one character is available. - term.c_cc[VMIN] = 1; - // Block read requests until VMIN characters are available. - term.c_cc[VTIME] = 0; - - if (tcsetattr (0, TCSANOW, &term) != 0) { - fprintf (stderr, "Tock: tcsetattr failed\n"); - exit (1); - } - } - //}}} + tock_configure_terminal(uses_stdin); if (!ccsp_init ()) exit (1);