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.

35 lines
1.4KB

  1. function loadfile(p) -- string -- function -- 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,...) -- string -- -- runs file *p* with arbitrary arguments in the current thread
  8. return loadfile(p)(...)
  9. end
  10. function os.spawnfile(p,n,...) -- string string -- number -- 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.package = {}
  15. package.loaded = {computer=computer,component=component,fs=fs,buffer=buffer}
  16. function require(f,force) -- string boolean -- table -- searches for a library with name *f* and returns what the library returns, if possible. if *force* is set, loads the library even if it is cached
  17. if not package.loaded[f] or force then
  18. local lib = os.getenv("LIB") or "/boot/lib"
  19. for d in lib:gmatch("[^\n]+") do
  20. if fs.exists(d.."/"..f) then
  21. package.loaded[f] = runfile(d.."/"..f)
  22. elseif fs.exists(d.."/"..f..".lua") then
  23. package.loaded[f] = runfile(d.."/"..f..".lua")
  24. end
  25. end
  26. end
  27. if package.loaded[f] then
  28. return package.loaded[f]
  29. end
  30. error("library not found: "..f)
  31. end
  32. function reload(f) -- string -- table -- Reloads library *f* from disk into memory.
  33. return require(f,true)
  34. end