OC-PsychOS/modules/lib/shutil.lua

42 lines
992 B
Lua

local shutil = {}
function shutil.genenv()
local et = os.genenv()
for k,v in pairs(shutil) do
if k ~= "genenv" then
et[k] = v
end
end
return et
end
function shutil.exec(line)
local words = {}
for w in line:gmatch("%S+") do table.insert(words,w) end
if _ENV[words[1]] then
local prg = table.remove(words,1)
local r={pcall(_ENV[prg],table.unpack(words))}
if r[1] == true then
table.remove(r,1)
end
return table.unpack(r)
end
local pth = os.getenv("PATH") or "."
for d in pth:gmatch("[^:]+") do
local prg = words[1]
_,lex = pcall(fs.exists,d.."/"..prg..".lua")
_,nex = pcall(fs.exists,d.."/"..prg)
if lex or nex then
table.remove(words,1)
end
if lex then
return pcall(loadfile(d.."/"..prg..".lua"),table.unpack(words))
elseif nex then
return pcall(loadfile(d.."/"..prg),table.unpack(words))
end
end
if line:sub(1,1) == "=" then
line="return "..line:sub(2)
end
local r={pcall(load(line,"shell","bt",_ENV))}
return table.unpack(r)
end