Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.1KB

  1. function loadfile(p) -- reads file *p* and returns a function if possible
  2. local f = io.open(p,"rb")
  3. local c = f:read("*a")
  4. f:close()
  5. return load(c,p,"t")
  6. end
  7. function runfile(p,...) -- runs file *p* with arbitrary arguments in the current thread
  8. return loadfile(p)(...)
  9. end
  10. function os.spawnfile(p,n,...) -- spawns a new process from file *p* with name *n*, with arguments following *n*.
  11. local tA = {...}
  12. return os.spawn(function() local res={pcall(loadfile(p), table.unpack(tA))} computer.pushSignal("process_finished", os.pid(), table.unpack(res)) dprint(table.concat(res)) end,n or p)
  13. end
  14. _G.libs = {computer=computer,component=component}
  15. function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
  16. if not _G.libs[f] then
  17. local lib = os.getenv("LIB") or "/boot/lib"
  18. for d in lib:gmatch("[^\n]+") do
  19. if fs.exists(d.."/"..f) then
  20. _G.libs[f] = runfile(d.."/"..f)
  21. elseif fs.exists(d.."/"..f..".lua") then
  22. _G.libs[f] = runfile(d.."/"..f..".lua")
  23. end
  24. end
  25. end
  26. if _G.libs[f] then
  27. return _G.libs[f]
  28. end
  29. error("library not found: "..f)
  30. end