erdos/A/ccp.lua

108 lines
2.3 KiB
Lua

return function(argv, con)
local runs = true
local function parseArgs(ln)
local iq, buf, args = false, '', {}
local qt, esc = nil, false
-- helper function
local function psh()
if #buf > 0 then table.insert(args, buf) end
buf, qt = '', nil
end
for i = 1, #ln + 1 do
local v = ln:sub(i, i)
if ((v == ' ' or v == '\n' or v == '\t') and not iq)
or v == ''
then psh()
elseif esc then esc, buf = false, buf.. v
elseif v == '\\' then esc = true
elseif (not qt or qt == v)
and (v == '"' or v == "'")
then psh()
qt, iq = v, not iq
else buf = buf.. v
end
end
return args
end
local cd = 'A:'
await(function()
con.print(cd.. '>')
con.down()
local ln = con.getln()
local argv = parseArgs(ln)
if not argv[1] then -- nothing
elseif argv[1]:lower() == 'dir' then
local cp, f = 0, fsLs(cd)
for _, v in pairs(f) do
if cp + 14 + #cd >= con.w
then con.print '\n'; cp = 0 end
if cp == 0 then cp = #cd
con.print(cd)
else con.print ' : ' end
local i = fsInfo(cd..v)
local n, ext = v:match('(.+)%.(.+)$')
n = n or v
if #n > 8
then con.print(n:sub(1, 6) .. '~1')
elseif #n < 8
then con.print(n ..(' '):rep(8 - #n))
end
con.print ' '
if i.type == 'directory' then con.print '<D>'
elseif i.type == 'symlink' then con.print '<S>'
elseif i.type == 'other' then con.print '<O>'
elseif ext then
con.print(ext:upper():sub(1, 3))
con.print((' '):rep(3 - #ext))
else con.print ' '
end
cp = cp + 14
end
con.print '\n'
elseif argv[1]:match '^%u:$' then
cd = argv[1]
elseif argv[1]:lower() == 'type' then
con.print(fsRead(argv[2]))
con.print '\n'
elseif argv[1]:lower() == 'exit'
then runs = false
else
local v = (argv[1]:sub(-4) == '.lua'
and argv[1]
or argv[1]..'.lua')
local pat
if fsHas(cd..v) then pat = cd..v
elseif fsHas(v) then pat = v
end
if not pat
then con.println(argv[1].. ': command not found')
else local suc, msg
suc, msg = pcall(select(2,pcall(load, (fsRead(pat)))))
if not suc then con.println 'err'
else suc, msg = pcall(msg, argv, con)
if not suc then con.println(msg) end
end
end
end
return not runs
end)
end