From 88af6a29d23a296bfd8f946fe18f9e97fdd379c9 Mon Sep 17 00:00:00 2001 From: Adam Sampson Date: Wed, 12 Mar 2008 14:38:58 +0000 Subject: [PATCH] Abstract CCSP setup into a function, and set up terminal properly. If stdin is in use, it'll now be put into non-canonical mode. This is almost the same as libkrocif's behaviour, except I've set VTIME to 0, since I don't think there's a good reason to have a terminal read complete if no characters are available. This also sets CCSP's branding to "Tock", so error messages now start "Tock: ". --- backends/GenerateC.hs | 4 +-- support/tock_support_cif.h | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/backends/GenerateC.hs b/backends/GenerateC.hs index d4a3f0c..e5b993a 100644 --- a/backends/GenerateC.hs +++ b/backends/GenerateC.hs @@ -186,14 +186,14 @@ cgenTopLevel s sequence_ [tell [" ", func, "_kill (wptr, &", kc, ");\n"] | (func, kc) <- zip funcs killChans] + let uses_stdin = if TLPIn `elem` (map snd tlpChans) then "true" else "false" tell [" LightProcBarrierWait (wptr, &bar);\n\ \\n\ \ Shutdown (wptr);\n\ \}\n\ \\n\ \int main (int argc, char *argv[]) {\n\ - \ if (!ccsp_init ())\n\ - \ return 1;\n\ + \ tock_init_ccsp (", uses_stdin, ");\n\ \\n\ \ Workspace p = ProcAllocInitial (0, "] genName tlpName diff --git a/support/tock_support_cif.h b/support/tock_support_cif.h index 8bb7a39..aa96449 100644 --- a/support/tock_support_cif.h +++ b/support/tock_support_cif.h @@ -21,6 +21,10 @@ #include #include +#include +#include +#include +#include //{{{ occam_stop #define occam_stop(pos, nargs, format, args...) \ @@ -109,4 +113,60 @@ 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; + } + //}}} + + ccsp_default_exit_handler (status, core); +} + +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); + } + } + //}}} + + if (!ccsp_init ()) + exit (1); + + ccsp_set_exit_handler (tock_exit_handler); +} +//}}} + #endif