Compare commits
3 Commits
fd9e4ad88a
...
d59cc53340
Author | SHA1 | Date | |
---|---|---|---|
d59cc53340 | |||
cae7c916ae | |||
b465aebbdb |
113
lib/doc.lua
Normal file
113
lib/doc.lua
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
local doc = {}
|
||||||
|
doc.searchers = {}
|
||||||
|
doc.tctab = {
|
||||||
|
["string"] = 31,
|
||||||
|
["table"] = 32,
|
||||||
|
["number"] = 33,
|
||||||
|
["boolean"] = 35,
|
||||||
|
["function"] = 36
|
||||||
|
}
|
||||||
|
|
||||||
|
function doc.parsefile(path) -- string -- table -- parses file from *path* to return a documentation table
|
||||||
|
local fdoc = {}
|
||||||
|
local f = io.open(path)
|
||||||
|
local lines = {}
|
||||||
|
for l in f:read("*a"):gmatch("[^\n]+") do
|
||||||
|
if l:find("function") and not l:find("local") then
|
||||||
|
lines[#lines+1] = l
|
||||||
|
end
|
||||||
|
end
|
||||||
|
for k,v in pairs(lines) do
|
||||||
|
local name, args, desc = v:match("function%s+(.+)%s*%((.*)%)%s*%-%-%s*(.+)")
|
||||||
|
if name and args and desc then
|
||||||
|
local fd = {["description"]=desc or desc,["args"]={},["atypes"]={}}
|
||||||
|
for word in args:gmatch("[^%s,]+") do
|
||||||
|
fd.args[#fd.args+1] = {word}
|
||||||
|
fd.atypes[word] = "unknown"
|
||||||
|
end
|
||||||
|
local argtypes, outtypes, description = desc:match("(.-)%-%-(.-)%-%-%s*(.+)")
|
||||||
|
if argtypes and outtypes and description then
|
||||||
|
local wc = 1
|
||||||
|
for word in argtypes:gmatch("%S+") do
|
||||||
|
fd.args[wc][2] = word
|
||||||
|
fd.atypes[fd.args[wc][1]] = word
|
||||||
|
wc = wc + 1
|
||||||
|
end
|
||||||
|
local wc = 1
|
||||||
|
for word in outtypes:gmatch("%S+") do
|
||||||
|
fd.outtypes = fd.outtypes or {}
|
||||||
|
fd.outtypes[#fd.outtypes+1] = word
|
||||||
|
end
|
||||||
|
fd.description = description
|
||||||
|
end
|
||||||
|
fdoc[name] = fd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return fdoc
|
||||||
|
end
|
||||||
|
|
||||||
|
function doc.format(fdoc) -- table -- string -- returns VT100 formatted documentation from documentation table *fdoc*
|
||||||
|
local rs = "" -- string to return
|
||||||
|
for fname,finfo in pairs(fdoc) do
|
||||||
|
if rs:len() > 0 then rs = rs .. "\n\n" end
|
||||||
|
local as = "" -- string containing arguments for a given function, with colours for type
|
||||||
|
for k,v in ipairs(finfo.args) do
|
||||||
|
local c = doc.tctab[v[2]] or 0
|
||||||
|
if k > 1 then
|
||||||
|
as = as .. ", "
|
||||||
|
end
|
||||||
|
if v[2] then
|
||||||
|
as = string.format("%s%s: \27[%im%s\27[0m",as,v[2],c,v[1])
|
||||||
|
else
|
||||||
|
as = string.format("%s\27[%im%s\27[0m",as,c,v[1])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local rv = ""
|
||||||
|
if finfo.outtypes then
|
||||||
|
rv = ": "
|
||||||
|
for k,v in ipairs(finfo.outtypes) do
|
||||||
|
if k > 1 then
|
||||||
|
rv = rv .. ", "
|
||||||
|
end
|
||||||
|
local c = doc.tctab[v] or 0
|
||||||
|
rv = string.format("%s\27[%im%s\27[0m",rv,c,v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local nd = finfo.description
|
||||||
|
for k,v in pairs(finfo.atypes) do
|
||||||
|
local c = doc.tctab[v] or 7
|
||||||
|
nd=nd:gsub("%*"..k.."%*","\27["..tostring(c).."m"..k.."\27[0m")
|
||||||
|
end
|
||||||
|
rs = string.format("%s\27[36m%s\27[0m(%s)%s\n%s",rs,fname,as,rv,nd)
|
||||||
|
end
|
||||||
|
return rs
|
||||||
|
end
|
||||||
|
|
||||||
|
function doc.searchers.lib(name) -- string -- string string -- Tries to find a documentation from a library with *name*. Returns either a string of documentation, or false and a reason.
|
||||||
|
local lib = os.getenv("LIB") or "/boot/lib"
|
||||||
|
local dt
|
||||||
|
for d in lib:gmatch("[^\n]+") do
|
||||||
|
if fs.exists(d.."/"..name) then
|
||||||
|
dt = doc.parsefile(d.."/"..name)
|
||||||
|
elseif fs.exists(d.."/"..name..".lua") then
|
||||||
|
dt = doc.parsefile(d.."/"..name..".lua")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not dt then return false, "unable to find documentation for "..tostring(name) end
|
||||||
|
return doc.format(dt)
|
||||||
|
end
|
||||||
|
|
||||||
|
function doc.docs(topic) -- string -- boolean -- Displays the documentation for *topic*, returning true, or errors. Also callable as just doc().
|
||||||
|
local lib = os.getenv("LIB") or "/boot/lib"
|
||||||
|
local dt
|
||||||
|
for k,v in pairs(doc.searchers) do
|
||||||
|
dt=v(topic)
|
||||||
|
if dt then
|
||||||
|
print(dt)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
error("unable to find documentation for "..tostring(name))
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable(doc,{__call=function(_,topic) return doc.docs(topic) end})
|
@ -5,7 +5,7 @@ local ufs = require "unionfs"
|
|||||||
local rpc = require "rpc"
|
local rpc = require "rpc"
|
||||||
local netutil = {}
|
local netutil = {}
|
||||||
|
|
||||||
function netutil.importfs(host,rpath,lpath) -- import filesystem *rpath* from *host* and attach it to *lpath*
|
function netutil.importfs(host,rpath,lpath) -- string string string -- boolean -- Import filesystem *rpath* from *host* and attach it to *lpath*.
|
||||||
local px = rpc.proxy(host,rpath.."_")
|
local px = rpc.proxy(host,rpath.."_")
|
||||||
function px.getLabel()
|
function px.getLabel()
|
||||||
return host..":"..rpath
|
return host..":"..rpath
|
||||||
@ -14,7 +14,7 @@ function netutil.importfs(host,rpath,lpath) -- import filesystem *rpath* from *h
|
|||||||
return fs.mount(lpath,px)
|
return fs.mount(lpath,px)
|
||||||
end
|
end
|
||||||
|
|
||||||
function netutil.exportfs(path) -- export the directory *path* over RPC
|
function netutil.exportfs(path) -- string -- boolean -- Export the directory *path* over RPC.
|
||||||
local path = "/"..table.concat(fs.segments(path),"/")
|
local path = "/"..table.concat(fs.segments(path),"/")
|
||||||
local px = ufs.create(path)
|
local px = ufs.create(path)
|
||||||
for k,v in pairs(px) do
|
for k,v in pairs(px) do
|
||||||
@ -24,7 +24,7 @@ function netutil.exportfs(path) -- export the directory *path* over RPC
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function netutil.ping(addr,times,timeout, silent) -- Request acknowledgment from *addr*, waiting *timeout* seconds each try, and try *times* times. If *silent* is true, don't print status. Returns true if there was at least one successful ping, the number of successes, the number of failures, and the average round trip time.
|
function netutil.ping(addr,times,timeout,silent) -- string number number boolean -- boolean number number number -- Request acknowledgment from *addr*, waiting *timeout* seconds each try, and try *times* times. If *silent* is true, don't print status. Returns true if there was at least one successful ping, the number of successes, the number of failures, and the average round trip time.
|
||||||
local times, timeout = times or 5, timeout or 30
|
local times, timeout = times or 5, timeout or 30
|
||||||
local success, fail, time, avg = 0, 0, 0, 0
|
local success, fail, time, avg = 0, 0, 0, 0
|
||||||
for i = 1, times do
|
for i = 1, times do
|
||||||
@ -46,7 +46,7 @@ function netutil.ping(addr,times,timeout, silent) -- Request acknowledgment from
|
|||||||
return success > 0, success, fail, avg
|
return success > 0, success, fail, avg
|
||||||
end
|
end
|
||||||
|
|
||||||
function netutil.nc(host,port)
|
function netutil.nc(host,port) -- string number -- boolean -- Starts an interactive Minitel socket connection to *host* on *port*, primarily for remote login. Returns whether the attempt was successful.
|
||||||
port = port or 22
|
port = port or 22
|
||||||
local socket = minitel.open(host,port)
|
local socket = minitel.open(host,port)
|
||||||
if not socket then return false end
|
if not socket then return false end
|
||||||
@ -66,6 +66,7 @@ function netutil.nc(host,port)
|
|||||||
socket:write(b.."\n")
|
socket:write(b.."\n")
|
||||||
end
|
end
|
||||||
until socket.state ~= "open"
|
until socket.state ~= "open"
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
return netutil
|
return netutil
|
||||||
|
@ -2,6 +2,7 @@ local component = require "component"
|
|||||||
local fs = require "fs"
|
local fs = require "fs"
|
||||||
local shell = require "shell"
|
local shell = require "shell"
|
||||||
local ed = require "ed"
|
local ed = require "ed"
|
||||||
|
local doc = require "doc"
|
||||||
local shutil = {}
|
local shutil = {}
|
||||||
shutil.ed = ed.interactive
|
shutil.ed = ed.interactive
|
||||||
shutil.vi = ed.visual
|
shutil.vi = ed.visual
|
||||||
|
Loading…
Reference in New Issue
Block a user