From 71752832f6946e847a193ce11e921c92bba4d028 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 24 Apr 2026 16:01:51 -0500 Subject: [PATCH] Fix "echo | cat" exiting in interactive mode due to too-lazy unredirect(). --- tests/sh.test | 6 ++++-- toys/pending/sh.c | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/sh.test b/tests/sh.test index e3c238ca..c551962e 100644 --- a/tests/sh.test +++ b/tests/sh.test @@ -5,7 +5,7 @@ # TODO make fake pty wrapper for test infrastructure # testing "name" "command" "result" "infile" "stdin" -# texpect "name" "command" [R]I/O/E"string" X[ERR] +# txpect "name" "command" [R]I/O/E"string" X[ERR] # Use "bash" name for host, "sh" for toybox. (/bin/sh is often defective.) [ -z "$SH" ] && { [ -z "$TEST_HOST" ] && SH="sh" || export SH="bash" ; } @@ -192,6 +192,8 @@ testing "exec2" "exec echo hello; echo $?" "hello\n" "" "" # ; | && || testing "semicolon" "echo one;echo two" "one\ntwo\n" "" "" testing "simple pipe" "echo hello | cat" "hello\n" "" "" +shxpect "interactive simple pipe" I$'echo hello | cat\n' O$'hello\n' E"$P" \ + I$'echo second | cat\n' O$'second\n' E"$P" I$'exit 3' X3 testing "&&" "true && echo hello" "hello\n" "" "" testing "&&2" "false && echo hello" "" "" "" testing "||" "true || echo hello" "" "" "" @@ -425,7 +427,7 @@ testing "IFS combinations" \ 'IFS=" x"; A=" x " B=" x" C="x " D=x E=" "; for i in $A $B $C $D L$A L$B L$C L$D $A= $B= $C= $D= L$A= L$B= L$C= L$D=; do echo -n {$i}; done' \ "{}{}{}{}{L}{L}{L}{L}{}{=}{}{=}{}{=}{}{=}{L}{=}{L}{=}{L}{=}{L}{=}" "" "" -$BROKEN testing "! isn't special" "echo !" "!\n" "" "" +testing "! alone isn't special" "echo !" "!\n" "" "" testing "! by itself" '!; echo $?' "1\n" "" "" testing "! true" '! true; echo $?' "1\n" "" "" testing "! ! true" '! ! true; echo $?' "0\n" "" "" diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 664f9cf4..e09f2050 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -1274,7 +1274,7 @@ if (DEBUG) dprintf(2, "%d redir %d to %d\n", getpid(), from, to); if (from >= 0 && to != dup2(from, to)) { if (hfd >= 0) close(hfd); - return 1; + return 1; // filehandle exhaustion } } else { hfd = to; @@ -1287,7 +1287,7 @@ if (DEBUG) dprintf(2, "%d redir %d to %d\n", getpid(), from, to); rr[2*cnt-1] = hfd; rr[2*cnt] = to; - return 0; + return 0; // success } // restore displaced filehandles, closing high filehandles they were copied to @@ -4274,6 +4274,11 @@ advance: toys.exitval = wait_pipeline(pplist); llist_traverse(pplist, (void *)free_process); } + + if (TT.ff) { + unredirect(TT.ff->blk->urd); + TT.ff->blk->urd = 0; + } } // set variable -- 2.39.5