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.

58 line
1.4KB

  1. local serial = require "serialization"
  2. local event = require "event"
  3. print(pcall(function()
  4. local shenv = {}
  5. function shenv.quit()
  6. os.setenv("run",nil)
  7. end
  8. shenv.cd = os.chdir
  9. shenv.mkdir = fs.makeDirectory
  10. shenv.cp = fs.copy
  11. local function findPath(name)
  12. path = os.getenv("PATH") or "/boot/exec"
  13. for l in path:gmatch("[^\n]+") do
  14. if fs.exists(l.."/"..name) then
  15. return l.."/"..name
  16. elseif fs.exists(l.."/"..name..".lua") then
  17. return l.."/"..name..".lua"
  18. end
  19. end
  20. end
  21. setmetatable(shenv,{__index=function(_,k)
  22. local fp = findPath(k)
  23. if _G[k] then
  24. return _G[k]
  25. elseif _G.libs[k] then
  26. return _G.libs[k]
  27. elseif fp then
  28. return function(...)
  29. local tA = {...}
  30. local pid = os.spawnfile(fp,fp,table.unpack(tA))
  31. local tE = {event.pull("process_finished",pid)}
  32. if tE[1] == true then
  33. table.remove(tE,1)
  34. end
  35. return table.unpack(tE)
  36. end
  37. end
  38. end})
  39. print(_VERSION)
  40. os.setenv("run",true)
  41. while os.getenv("run") do
  42. io.write(string.format("%s:%s> ",os.getenv("HOSTNAME") or "localhost",(os.getenv("PWD") or _VERSION)))
  43. local input=io.read()
  44. if input:sub(1,1) == "=" then
  45. input = "return "..input:sub(2)
  46. end
  47. tResult = {pcall(load(input,"shell","t",shenv))}
  48. if tResult[1] == true then table.remove(tResult,1) end
  49. for k,v in pairs(tResult) do
  50. if type(v) == "table" then
  51. print(serial.serialize(v,true))
  52. else
  53. print(v)
  54. end
  55. end
  56. end
  57. end))