Fix OpenOS '/bin/sh cmd' failing due to lack of env table#3196
Fix OpenOS '/bin/sh cmd' failing due to lack of env table#31960x0ade wants to merge 4 commits intoMightyPirates:master-MC1.7.10from
Conversation
/bin/sh.lua should be able to execute a command passed as an argument, but failed to do so as sh.execute (in /lib/sh.lua) expects an env table. Meanwhile, /bin/source.lua passes said environment table when starting the sh process manually.
| cenv = cargs[1] | ||
| cargsStart = 2 | ||
| end | ||
| local result = table.pack(sh.execute(cenv, table.unpack(cargs, cargsStart))) |
There was a problem hiding this comment.
This could be shortened a bit.
local cenv = _ENV
if type(cargs[1]) == "table" then
-- sh can also run as a manually started process (see /bin/source.lua)
cenv = table.remove(cargs, 1)
end
local result = table.pack(sh.execute(cenv, table.unpack(cargs)))Also, what about using {...} instead of table.pack(...)?
There was a problem hiding this comment.
Same as with skipping the arg with table.unpack instead of using table.remove, I figured I'd just stick to what's already being used and used table.pack instead of {...}, mixing multiple ways to achieve the same thing in the same chunk of code.
There was a problem hiding this comment.
They are not completely identical. Using {...} would pack all arguments into a table, whereas table.pack actually also adds the additional key "n". Not that it matters in this case.
There was a problem hiding this comment.
TIL 😅 anyway, I've just pushed another commit implementing your proposed changes.
|
first of all, i greatly dislike that /bin/sh takes a table as an arg |
|
nope i dont care |
…. will not support #3196 at this time
…. will not support #3196 at this time
|
Well, guess this is not so simple to fix. |
/bin/sh.luashould be able to execute a command passed as an argument, but currently fails to do so in interactive shells assh.execute(in/lib/sh.lua) expects an environment table as the first parameter:That environment table is given when running commands in the interactive shell, but is missing from the non-interactive
sh.executecall.Meanwhile,
/bin/source.luapasses an environment table as the first argument to/bin/sh.luawhen starting theshprocess manually.As such, I've decided to add a simple check if the first parameter is a table. If it is, it's assumed to be the environment table and won't be passed on. Otherwise
_ENVwill be used and all arguments will be passed on.This should maintain compatibility with any existing usages of
/bin/sh.luaas a child process. Admittedly, I've only tested it locally by manipulating a copy of/bin/sh.luaafter installing OpenOS onto an OC computer.(Sidenote: I was not sure whether to remove the env table from the argument list, or whether to skip it when unpacking the arg list. I went with the second option as I thought it'd be preferred, given how returning does the same.)