moved the execute part of lush into shutil

This commit is contained in:
Izaya 2017-10-08 00:24:01 +11:00
parent 3a3e83691a
commit bbead5d252
2 changed files with 33 additions and 34 deletions

View File

@ -5,45 +5,13 @@ spawn("lua shell", function() print(pcall(function()
_ENV = shutil.genenv()
coroutine.yield()
log(pcall(login))
print(_VERSION)
local function shexec(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
print("lush v1/".._VERSION)
while true do
local didexec = false
write((os.getenv("PWD") or "").."> ")
local inp=readln()
if not inp then break end
local rt = {shexec(inp)}
local rt = {shutil.exec(inp)}
if rt[1] == true then
table.remove(rt,1)
end

View File

@ -8,3 +8,34 @@ function shutil.genenv()
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