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.

53 lines
1.3KB

  1. local serial = require "serialization"
  2. local shell = {}
  3. shell.include = {"shutil"}
  4. local function shindex(self,k)
  5. if rawget(self,k) then return rawget(self,k) end
  6. for _,v in pairs(os.getenv("INCLUDE") or shell.include) do
  7. if require(v)[k] then return require(v)[k] end
  8. end
  9. if package.loaded[k] then return package.loaded[k] end
  10. return _G[k]
  11. end
  12. local function formatValue(v)
  13. if type(v) == "table" then
  14. local w, rs = pcall(serial.serialize,v,true)
  15. if w then return rs end
  16. end
  17. return tostring(v)
  18. end
  19. function shell.interactive()
  20. local shenv = setmetatable({}, {__index=shindex})
  21. local run = true
  22. os.setenv("PATH",{"/boot/exec","/pkg/exec"})
  23. function shenv.quit()
  24. run = false
  25. end
  26. while run do
  27. io.write(string.format("\27[32m%s:%s>\27[0m ",os.getenv("HOSTNAME") or "localhost",(os.getenv("PWD") or _VERSION)))
  28. local w,input = pcall(io.read)
  29. if not w then
  30. print("\27[31m^C")
  31. else
  32. if input:sub(1,1) == "=" then
  33. input = "return "..input:sub(2)
  34. end
  35. local f, r = load(input, "shell", "t", shenv)
  36. if not f then
  37. print("\27[31m"..r)
  38. else
  39. local rt = {xpcall(f,debug.traceback)}
  40. local rs = table.remove(rt,1)
  41. if not rs then io.write("\27[31m") end
  42. for k,v in pairs(rt) do
  43. print(formatValue(v))
  44. end
  45. end
  46. end
  47. end
  48. end
  49. return shell