From 2b69f81111a9477c506697beef18c695d95517af Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Tue, 8 Jan 2019 18:09:04 +1100 Subject: [PATCH] added shell utility programs --- exec/cat.lua | 6 +++++ exec/ed.lua | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ exec/ls.lua | 10 +++++++ 3 files changed, 92 insertions(+) create mode 100644 exec/cat.lua create mode 100644 exec/ed.lua create mode 100644 exec/ls.lua diff --git a/exec/cat.lua b/exec/cat.lua new file mode 100644 index 0000000..3107761 --- /dev/null +++ b/exec/cat.lua @@ -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 diff --git a/exec/ed.lua b/exec/ed.lua new file mode 100644 index 0000000..7ca3937 --- /dev/null +++ b/exec/ed.lua @@ -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 diff --git a/exec/ls.lua b/exec/ls.lua new file mode 100644 index 0000000..db111f6 --- /dev/null +++ b/exec/ls.lua @@ -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