From 35d93e2a9350149eef1ea188a39e4d605442c44b Mon Sep 17 00:00:00 2001 From: payonel Date: Thu, 21 Jul 2016 09:38:16 -0700 Subject: [PATCH] fix piping of redirected stdout e.g. Assume you have a script that print to stdout as well as stderr: [test.lua] print("foo") io.stderr:write("bar") Then run this script, redirecting [2] to stdout, and [1] to dev null, then piping to grep ./test.lua 2>&1 >/dev/null | grep baz This test should print NOTHING to the terminal, "foo" went to dev null, "bar" when to stdout, and grep matched on "baz", thus should not print. But the bug I found was that [2] was redirected to terminal tty stdout and then AFTER that the piping code added a pipe on [1], but [2] was already pointing to tty. The fix is to move the piping before redirections are created, so that when [2] redirects to stdout, stdout is already the pipe. --- .../opencomputers/loot/openos/lib/sh.lua | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua index 0ebb906cd..f6f6c48ba 100644 --- a/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua +++ b/src/main/resources/assets/opencomputers/loot/openos/lib/sh.lua @@ -251,28 +251,33 @@ function sh.internal.createThreads(commands, eargs, env) end, name) threads[i] = thread - - if thread then - -- smart check if ios should be loaded - if tx.first(args, function(token) return token == "<" or token:find(">") end) then - args, reason = sh.internal.buildCommandRedirects(thread, args) - end - end - - if not args or not thread then + + if not thread then for i,t in ipairs(threads) do process.internal.close(t) end return nil, reason end - process.info(thread).data.args = tx.concat(args, eargs or {}) + process.info(thread).data.args = args end if #threads > 1 then sh.internal.buildPipeChain(threads) end + for i = 1, #threads do + local thread = threads[i] + local args = process.info(thread).data.args + + -- smart check if ios should be loaded + if tx.first(args, function(token) return token == "<" or token:find(">") end) then + args, reason = sh.internal.buildCommandRedirects(thread, args) + end + + process.info(thread).data.args = tx.concat(args, eargs or {}) + end + return threads end