From e193542108079dc52bed46b3a2f37c88a546c4cf Mon Sep 17 00:00:00 2001 From: MCJack123 Date: Sat, 7 Dec 2019 20:45:39 -0500 Subject: [PATCH] Rewrote lua command --- README.md | 1 + cash.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5b70303..9e83beb 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ A Bourne-compatible shell for ComputerCraft. * Background jobs * rc files * Restorable history +* Job control, pausing * Partial CCKernel2 support * Full compatibility with CraftOS shell.lua diff --git a/cash.lua b/cash.lua index 945efb2..6e62833 100644 --- a/cash.lua +++ b/cash.lua @@ -249,14 +249,58 @@ builtins = { else while table.maxn(jobs) ~= 0 do sleep(0.1) end end end, lua = function(...) - if #({...}) > 0 then - local f, err = loadstring("return " .. table.concat({...}, " ")) - if f then - setfenv(f, setmetatable({shell = shell, multishell = multishell, package = pack, require = require}, {__index = _ENV})) - local r = {pcall(f)} - table.remove(r, 1) - print(table.unpack(r)) - else printError(err) end + if #({...}) > 0 then + if fs.exists(shell.resolve(...)) then + local args = {...} + table.remove(args, 1) + shell.run(shell.resolve(...), table.unpack(args)) + else + local s = table.concat({...}, " ") + local tEnv = setmetatable({shell = shell, multishell = multishell, package = pack, require = require, _echo = function(...) return ... end}, {__index = _ENV}) + local nForcePrint = 0 + local func, e = load( s, "lua", "t", tEnv ) + local func2, e2 = load( "return _echo("..s..");", "lua", "t", tEnv ) + if not func then + if func2 then + func = func2 + e = nil + nForcePrint = 1 + end + else + if func2 then + func = func2 + end + end + if func then + local tResults = table.pack( pcall( func ) ) + if tResults[1] then + local n = 1 + while n < tResults.n or (n <= nForcePrint) do + local value = tResults[ n + 1 ] + if type( value ) == "table" then + local metatable = getmetatable( value ) + if type(metatable) == "table" and type(metatable.__tostring) == "function" then + print( tostring( value ) ) + else + local ok, serialised = pcall( textutils.serialise, value ) + if ok then + print( serialised ) + else + print( tostring( value ) ) + end + end + else + print( tostring( value ) ) + end + n = n + 1 + end + else + printError( tResults[2] ) + end + else + printError( e ) + end + end else shell.run("/rom/programs/lua.lua") end end, cat = function(...)