Moved the code for setting up the terminal into tock_support.h so that C++CSP can use it too

This commit is contained in:
Neil Brown 2008-03-24 23:45:31 +00:00
parent c37e183879
commit e8adfb51fa
2 changed files with 55 additions and 38 deletions

View File

@ -26,6 +26,10 @@
#include <float.h>
#include <stdio.h>
#include <math.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
//{{{ 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

View File

@ -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);