implement component.get for convenience

This commit is contained in:
Izaya 2023-07-31 08:11:24 +10:00
parent 7fa61e115e
commit 36a73b892a
8 changed files with 35 additions and 15 deletions

View File

@ -1,3 +1,7 @@
local preproc = require "preproc"
--local tA = {...}
function preproc.directives.includelib(file,name)
return string.format("package.loaded.%s = (function()\n%s\nend)()", name, preproc.preproc(file))
end
preproc(...)

View File

@ -1 +1 @@
{enabled={"getty","minitel"}}
{enabled={"getty","minitel","fsmanager","rtfsman","partman"}}

8
module/component-get.lua Normal file
View File

@ -0,0 +1,8 @@
function component.get(addr, ctype)
for c in component.list(ctype, true) do
if c:sub(1, addr:len()) == addr then
return c
end
end
return nil, "no such component"
end

View File

@ -6,6 +6,7 @@
--#include "module/io.lua"
--#include "module/devfs.lua"
--#include "module/devfs/syslog.lua"
--#include "module/component-get.lua"
--#include "module/loadfile.lua"
_OSVERSION=_OSVERSION or "PsychOS 2"

View File

@ -32,8 +32,9 @@ function io.write(...) -- Writes its arguments to the default output stream.
io.output():write(...)
end
function print(...) -- Writes each argument to the default output stream, separated by newlines.
function print(...) -- Writes each argument to the default output stream, separated by space.
for k,v in ipairs({...}) do
io.write(tostring(v).."\n")
io.write(tostring(v).." ")
end
io.write("\n")
end

View File

@ -1,5 +1,5 @@
do
local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, event timeout, current PID
local tTasks,nPid,nTimeout,cPid = {},1,0.25,0 -- table of tasks, next process ID, default event timeout, current PID
function os.spawn(f,n) -- function string -- number -- creates a process from function *f* with name *n*
tTasks[nPid] = {
c=coroutine.create(function()
@ -14,6 +14,7 @@ function os.spawn(f,n) -- function string -- number -- creates a process from fu
P=cPid, -- parent PID
t=0, -- CPU time
T=0, -- total uptime
E=nTimeout, -- event timeout
e={} -- environment variables
}
if tTasks[cPid] then
@ -44,18 +45,22 @@ function os.taskInfo(pid) -- number -- table -- returns info on process *pid* as
end
function os.sched() -- the actual scheduler function
os.sched = nil
local sTimeout = nTimeout
while #tTasks > 0 do
local tEv = {computer.pullSignal(nTimeout)}
local tEv = {computer.pullSignal(sTimeout)}
sTimeout = nTimeout
for k,v in pairs(tTasks) do
if coroutine.status(v.c) ~= "dead" then
cPid = k
local sT, sC = os.clock(), computer.uptime()
coroutine.resume(v.c,table.unpack(tEv))
v.t, v.T = v.t + os.clock() - sT, v.T + computer.uptime() - sC
sTimeout=math.min(sTimeout, v.E)
else
tTasks[k] = nil
end
end
sTimeout = nTimeout
end
end
function os.setenv(k,v) -- set's the current process' environment variable *k* to *v*, which is passed to children
@ -69,13 +74,11 @@ function os.getenv(k) -- gets a process' *k* environment variable
end
end
function os.getTimeout()
return nTimeout
return tTasks[cPid].E
end
function os.setTimeout(n)
if type(n) == "number" and n >= 0 then
nTimeout = n
return true
end
return false
function os.setTimeout(n,pid)
assert(type(n) == "number" and n >= 0)
tTasks[pid or cPid].E = n
return true
end
end

View File

@ -1,3 +1,4 @@
--#include "module/ocelot-debug.lua"
do
syslog = {}
syslog.emergency = 0

View File

@ -8,16 +8,17 @@ local function mount(addr)
local w,r = fs.mount(dest,component.proxy(addr))
if not w then
syslog("Failed to mount: "..r)
return false
end
fsmanager.filesystems[addr] = dest
end
for addr, _ in component.list("filesystem") do
mount(addr)
end
function fsmanager.start()
run = true
return os.spawn(function()
for addr, _ in component.list("filesystem") do
mount(addr)
end
while run do
local tE = {coroutine.yield()}
if tE[1] == "component_added" and tE[3] == "filesystem" then
@ -25,6 +26,7 @@ function fsmanager.start()
elseif tE[1] == "component_removed" and fsmanager.filesystems[tE[2]] and tE[3] == "filesystem" then
syslog("Unmounting "..tE[2].." from "..fsmanager.filesystems[tE[2]])
fs.umount(fsmanager.filesystems[tE[2]])
fsmanager.filesystems[tE[2]] = nil
end
end
end,"fsmanager")