From 66f5d7c7a2e4e199d08cee1ce1699b3905c7c733 Mon Sep 17 00:00:00 2001 From: Matt Liberty Date: Fri, 5 Jun 2026 06:05:32 +0000 Subject: [PATCH] mainUtils: match readline behavior when ABC_USE_READLINE is undefined The non-readline branch of Abc_UtilsGetUsersInput has three behavioral gaps versus the readline branch that break callers driving abc as a coprocess over a pipe (e.g. yosys's passes/techmap/abc.cc, which spawns "abc -s" with piped stdin/stdout and uses read_until_abc_done to wait for "abc NN> " lines): 1. The prompt is written with fprintf() and never flushed. On a pipe stdout is fully buffered, so the prompt never reaches the reader. The reader waits for the prompt, abc waits in fgets(), deadlock. 2. There is no echo of the line read from stdin. readline() emits each character to its output stream; yosys's protocol depends on seeing "abc NN> source ...\n" in the output to advance state. Without an echo it waits forever. 3. EOF on stdin is silently ignored: fgets() returns NULL but the function returns a stale Prompt buffer, causing a tight loop on pipe close. The readline branch exit(0)s on NULL. Fix all three. Echo only when stdin is not a tty -- on a tty the kernel already echoes typed characters during cooked input, so double-echo would be visible to interactive users. Signed-off-by: Matt Liberty --- src/base/main/mainUtils.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/base/main/mainUtils.c b/src/base/main/mainUtils.c index 654d246f0..c1c67049a 100644 --- a/src/base/main/mainUtils.c +++ b/src/base/main/mainUtils.c @@ -18,6 +18,8 @@ ***********************************************************************/ +#include + #include "base/abc/abc.h" #include "mainInt.h" @@ -91,7 +93,13 @@ char * Abc_UtilsGetUsersInput( Abc_Frame_t * pAbc ) { char * pRetValue; fprintf( pAbc->Out, "%s", Prompt ); + fflush( pAbc->Out ); pRetValue = fgets( Prompt, 5000, stdin ); + if ( pRetValue == NULL ) { exit(0); } + if ( !isatty( fileno(stdin) ) ) { + fputs( Prompt, pAbc->Out ); + fflush( pAbc->Out ); + } return Prompt; } #endif