Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenOS '/bin/sh cmd' failing due to lack of env table #3196

Open
wants to merge 4 commits into
base: master-MC1.7.10
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/resources/assets/opencomputers/loot/openos/bin/sh.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ if #args == 0 then
end
else
-- execute command.
local result = table.pack(sh.execute(...))
local cargs = table.pack(...)
-- sh can run as a shell command (no env table)
local cenv = _ENV
local cargsStart = 1
if type(cargs[1]) == "table" then
-- sh can also run as a manually started process (see /bin/source.lua)
cenv = cargs[1]
cargsStart = 2
end
local result = table.pack(sh.execute(cenv, table.unpack(cargs, cargsStart)))
Copy link
Contributor

@Vexatos Vexatos Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(...)?

Copy link
Author

@0x0ade 0x0ade Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@Vexatos Vexatos Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL 😅 anyway, I've just pushed another commit implementing your proposed changes.

Copy link
Contributor

@Vexatos Vexatos Dec 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is up to @payonel anyway.

if not result[1] then
error(result[2], 0)
end
Expand Down