From 01369c13da70e4213378d9bef7f5196f4506c7e4 Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Wed, 7 Dec 2011 10:01:07 -0800 Subject: [PATCH] Added sim_ttisatty to support reasonable behaviour (i.e. avoid in infinite loop) in the main command input loop when EOF is detected and input is coming from a file (or a null device: /dev/null or NUL:) This may happen when a simulator is running in a background process. --- scp.c | 5 +++-- sim_console.c | 39 +++++++++++++++++++++++++++++++++++++++ sim_console.h | 1 + 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/scp.c b/scp.c index e35d15a6..7f80fbfb 100644 --- a/scp.c +++ b/scp.c @@ -778,8 +778,9 @@ while (stat != SCPE_EXIT) { /* in case exit */ cptr = (*sim_vm_read) (cbuf, CBUFSIZE, stdin); } else cptr = read_line_p ("sim> ", cbuf, CBUFSIZE, stdin);/* read with prmopt*/ - if (cptr == NULL) /* ignore EOF */ - continue; + if (cptr == NULL) /* EOF? */ + if (sim_ttisatty()) continue; /* ignore tty EOF */ + else break; /* otherwise exit */ if (*cptr == 0) /* ignore blank */ continue; sub_args (cbuf, gbuf, CBUFSIZE, argv); diff --git a/sim_console.c b/sim_console.c index 03fc07e8..bf023c49 100644 --- a/sim_console.c +++ b/sim_console.c @@ -23,6 +23,12 @@ used in advertising or otherwise to promote the sale, use or other dealings in this Software without prior written authorization from Robert M Supnik. + 07-Dec-11 MP Added sim_ttisatty to support reasonable behaviour (i.e. + avoid in infinite loop) in the main command input + loop when EOF is detected and input is coming from + a file (or a null device: /dev/null or NUL:) This may + happen when a simulator is running in a background + process. 17-Apr-11 MP Cleaned up to support running in a background/detached process 20-Jan-11 MP Fixed support for BREAK key on Windows to account @@ -98,6 +104,7 @@ sim_ttrun - called to put terminal into run state sim_ttcmd - called to return terminal to command state sim_ttclose - called once before the simulator exits + sim_ttisatty - called to determine if running interactively sim_os_poll_kbd - poll for keyboard input sim_os_putchar - output character to console @@ -867,6 +874,11 @@ t_stat sim_ttclose (void) return sim_ttcmd (); } +t_bool sim_ttisatty (void) +{ +return isatty (fileno (stdin)); +} + t_stat sim_os_poll_kbd (void) { unsigned int status, term[2]; @@ -986,6 +998,13 @@ t_stat sim_ttclose (void) return SCPE_OK; } +t_bool sim_ttisatty (void) +{ +DWORD Mode; + +return (std_input) && (std_input != INVALID_HANDLE_VALUE) && GetConsoleMode (std_input, &Mode); +} + t_stat sim_os_poll_kbd (void) { int c = -1; @@ -1063,6 +1082,11 @@ t_stat sim_ttclose (void) return SCPE_OK; } +t_bool sim_ttisatty (void) +{ +return 1; +} + t_stat sim_os_poll_kbd (void) { int c; @@ -1259,6 +1283,11 @@ t_stat sim_ttclose (void) return SCPE_OK; } +t_bool sim_ttisatty (void) +{ +return 1; +} + t_stat sim_os_poll_kbd (void) { int c; @@ -1355,6 +1384,11 @@ t_stat sim_ttclose (void) return sim_ttcmd (); } +t_bool sim_ttisatty (void) +{ +return isatty (0); +} + t_stat sim_os_poll_kbd (void) { int status; @@ -1462,6 +1496,11 @@ t_stat sim_ttclose (void) return sim_ttcmd (); } +t_bool sim_ttisatty(void) +{ +return isatty (fileno (stdin)); +} + t_stat sim_os_poll_kbd (void) { int status; diff --git a/sim_console.h b/sim_console.h index 6c64dd31..78d51dd7 100644 --- a/sim_console.h +++ b/sim_console.h @@ -83,6 +83,7 @@ t_stat sim_ttinit (void); t_stat sim_ttrun (void); t_stat sim_ttcmd (void); t_stat sim_ttclose (void); +t_bool sim_ttisatty(void); t_stat sim_os_poll_kbd (void); t_stat sim_os_putchar (int32 out); int32 sim_tt_inpcvt (int32 c, uint32 mode);