|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Matt Liberty <mliberty@precisioninno.com> |
| 3 | +Date: Fri, 30 May 2026 00:00:00 +0000 |
| 4 | +Subject: [PATCH] mainUtils: match readline behavior when ABC_USE_READLINE is |
| 5 | + undefined |
| 6 | + |
| 7 | +The non-readline branch of Abc_UtilsGetUsersInput has three behavioral |
| 8 | +gaps versus the readline branch that break callers driving abc as a |
| 9 | +coprocess over a pipe (e.g. yosys's techmap/abc.cc, which waits for the |
| 10 | +"abc NN> " prompt with the source command echoed back via |
| 11 | +read_until_abc_done()): |
| 12 | + |
| 13 | + 1. The prompt is written with fprintf() but never flushed. With stdout |
| 14 | + fully buffered (pipe), the prompt never reaches the reader; the |
| 15 | + reader waits for the prompt, abc waits in fgets(), deadlock. |
| 16 | + |
| 17 | + 2. There is no echo of the line read from stdin. readline() displays |
| 18 | + each character as it is typed (and on a pipe, readline's stream |
| 19 | + handling still emits the line + newline to its output). Without |
| 20 | + that, yosys never sees the "abc NN> source ..." pattern it requires |
| 21 | + to advance state. |
| 22 | + |
| 23 | + 3. EOF on stdin is silently ignored: fgets() returns NULL, the |
| 24 | + function returns a stale Prompt buffer, and the caller loops. The |
| 25 | + readline branch exits(0) when readline() returns NULL. |
| 26 | + |
| 27 | +Fix all three. Echo only when stdin is not a tty -- on a tty the kernel |
| 28 | +already echoes typed characters, and double-echo would be visible. |
| 29 | + |
| 30 | +Upstreaming to YosysHQ/abc and berkeley-abc/abc. |
| 31 | + |
| 32 | +Signed-off-by: Matt Liberty <mliberty@precisioninno.com> |
| 33 | +--- |
| 34 | + src/base/main/mainUtils.c | 9 ++++++++- |
| 35 | + 1 file changed, 8 insertions(+), 1 deletion(-) |
| 36 | + |
| 37 | +diff --git a/src/base/main/mainUtils.c b/src/base/main/mainUtils.c |
| 38 | +--- a/src/base/main/mainUtils.c |
| 39 | ++++ b/src/base/main/mainUtils.c |
| 40 | +@@ -18,6 +18,8 @@ |
| 41 | + |
| 42 | + ***********************************************************************/ |
| 43 | + |
| 44 | ++#include <unistd.h> |
| 45 | ++ |
| 46 | + #include "base/abc/abc.h" |
| 47 | + #include "mainInt.h" |
| 48 | + |
| 49 | +@@ -91,7 +93,13 @@ |
| 50 | + { |
| 51 | + char * pRetValue; |
| 52 | + fprintf( pAbc->Out, "%s", Prompt ); |
| 53 | ++ fflush( pAbc->Out ); |
| 54 | + pRetValue = fgets( Prompt, 5000, stdin ); |
| 55 | ++ if ( pRetValue == NULL ) { exit(0); } |
| 56 | ++ if ( !isatty( fileno(stdin) ) ) { |
| 57 | ++ fputs( Prompt, pAbc->Out ); |
| 58 | ++ fflush( pAbc->Out ); |
| 59 | ++ } |
| 60 | + return Prompt; |
| 61 | + } |
| 62 | + #endif |
0 commit comments