erdos/sysapps/ersh.lua

66 lines
1.4 KiB
Lua

return function(argv, con)
if argv[2] == '-v'
then
con.println 'Er2Shell 0.5'
return
end
local sh = require 'src.baseshell' (con)
function sh:prompt()
return ('%d > '):format(self.env.code)
end
function sh:run(ln)
local argv = self:parseArgs(ln)
if not argv[1] then -- nothing
elseif argv[1] == 'exit' then
self.runs = false
elseif argv[1] == 'which' then
local fnd, snd = self:which(argv[2])
if not fnd then self.env.code = 1
else self.env.code = 0
return fnd..snd
end
elseif argv[1] == 'env' then
local ls = ''
for k, v in pairs(self.env) do
k, v = tostring(k), tostring(v)
ls=ls.. ('$%s=%s\n')
:format(k, v:match '%s' and '"'..v..'"' or v)
end
self.env.code = 0
return ls:sub(1, -2)
else
local fnd, snd = self:which(argv[1])
if not fnd then
self.env.code = 127
return argv[1].. ': command not found'
else self.env.code = 0
local succ, msg = pcall(select(2, pcall(loadfile, fnd..snd)))
if not succ then
print(msg)
self.env.code = 126
return argv[1].. ': file error'
else
local succ, msg = pcall(msg, argv, con, self.env)
if not succ then
self.env.code = self.env.code ~= 0 and self.env.code or 1
return argv[1].. ': '.. tostring(msg)
else self.env.code = tonumber(msg) or 0
end
end
end
end
end
sh:loop()
end