added shell utility programs

This commit is contained in:
Izaya 2019-01-08 18:09:04 +11:00
parent 9ee9b5d6a2
commit 2b69f81111
3 changed files with 92 additions and 0 deletions

6
exec/cat.lua Normal file
View File

@ -0,0 +1,6 @@
local tA = {...}
for _,fn in ipairs(tA) do
local f = io.open(fn,"rb")
io.write(f:read("*a"))
f:close()
end

76
exec/ed.lua Normal file
View File

@ -0,0 +1,76 @@
local tA = {...}
local fn = tA[1]
local b,C,p = {},{},1
local function sC()
if p > #b then
p = #b
end
if p < 1 then
p = 1
end
end
function C.i()
p=p-1
sC()
while true do
io.write(tostring(p).."] ")
l = io.read()
if l == "." then break end
table.insert(b,p,l)
p=p+1
end
end
function C.l(s,e)
for i = s or 1, e or #b do
print(string.format("%4d\t %s",i,b[i]))
end
end
function C.a()
p=p+1
C.i()
end
function C.p(n)
p=tonumber(n) or p
sC()
end
function C.d(n)
n=tonumber(n) or 1
for i = 1, n do
print(table.remove(b,p,i))
end
end
function C.r(f)
local f = fs.open(f)
if f then
for l in f:read("*a"):gmatch("[^\n]+") do
table.insert(b,p,l)
p=p+1
end
f:close()
end
end
function C.w(f)
local f=fs.open(f,"wb")
if f then
for _,l in ipairs(b) do
f:write(l.."\n")
end
f:close()
end
end
if fn then
C.r(fn)
end
while true do
io.write("ed> ")
local l,c = io.read(),{}
for w in l:gmatch("%S+") do
c[#c+1] = w
end
local e=table.remove(c,1)
if e == "q" then
break
elseif C[e] then
C[e](table.unpack(c))
end
end

10
exec/ls.lua Normal file
View File

@ -0,0 +1,10 @@
local tA = {...}
tA[1] = tA[1] or "."
for _,d in ipairs(tA) do
if #tA > 1 then
print(d..":")
end
for _,f in ipairs(fs.list(d)) do
print(" "..f)
end
end