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.

25 lines
863B

  1. function loadfile(p) -- reads file *p* and returns a function if possible
  2. local f = fs.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*
  11. dprint(p,n)
  12. return os.spawn(function() xpcall(loadfile(p),function(e) dprint(e.."\n"..debug.traceback()) end) end,n or p)
  13. end
  14. function require(f) -- searches for a library with name *f* and returns what the library returns, if possible
  15. local lib = os.getenv("LIB") or "/boot/lib"
  16. for d in lib:gmatch("[^\n]+") do
  17. if fs.exists(d.."/"..f) then
  18. return runfile(d.."/"..f)
  19. elseif fs.exists(d.."/"..f..".lua") then
  20. return runfile(d.."/"..f..".lua")
  21. end
  22. end
  23. error("library not found: "..f)
  24. end