2019-01-08 18:01:07 +11:00
|
|
|
function loadfile(p) -- reads file *p* and returns a function if possible
|
2018-11-03 03:05:41 +11:00
|
|
|
local f = fs.open(p,"rb")
|
|
|
|
local c = f:read("*a")
|
|
|
|
f:close()
|
|
|
|
return load(c,p,"t")
|
|
|
|
end
|
2019-01-08 18:01:07 +11:00
|
|
|
function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread
|
|
|
|
return loadfile(p)(...)
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|
2019-12-16 14:37:29 +11:00
|
|
|
function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
|
|
|
|
local tA = {...}
|
|
|
|
return os.spawn(function() computer.pushSignal("process_finished", os.pid(), pcall(loadfile(p), table.unpack(tA))) end,n or p)
|
2019-01-08 18:01:07 +11:00
|
|
|
end
|
2019-12-19 16:14:48 +11:00
|
|
|
_G.libs = {computer=computer,component=component}
|
2019-01-08 18:01:07 +11:00
|
|
|
function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
|
|
|
|
local lib = os.getenv("LIB") or "/boot/lib"
|
|
|
|
for d in lib:gmatch("[^\n]+") do
|
|
|
|
if fs.exists(d.."/"..f) then
|
2019-12-19 16:14:48 +11:00
|
|
|
_G.libs[f] = runfile(d.."/"..f)
|
2019-01-08 18:01:07 +11:00
|
|
|
elseif fs.exists(d.."/"..f..".lua") then
|
2019-12-19 16:14:48 +11:00
|
|
|
_G.libs[f] = runfile(d.."/"..f..".lua")
|
2019-01-08 18:01:07 +11:00
|
|
|
end
|
|
|
|
end
|
2019-12-19 16:14:48 +11:00
|
|
|
if _G.libs[f] then
|
|
|
|
return _G.libs[f]
|
|
|
|
else
|
|
|
|
error("library not found: "..f)
|
|
|
|
end
|
2018-11-03 03:05:41 +11:00
|
|
|
end
|