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.

83 lines
1.6KB

  1. do
  2. io = {}
  3. function io.type(fh)
  4. if type(fh) ~= "table" then return nil end
  5. if fh.state == "open" then
  6. return "file"
  7. elseif fh.state == "closed" then
  8. return "closed file"
  9. end
  10. return nil
  11. end
  12. function io.read(buf, n)
  13. n = n or buf
  14. buf = buf or io.input()
  15. print("bread",type(buf),n)
  16. if not buf.aread then return nil end
  17. if not buf.abmode then
  18. buffer.write(buf,buf.fh:read(buf.m - buf.b:len()))
  19. end
  20. local rv = buffer.read(buf,n)
  21. buffer.write(buf,buf.fh:read(buf.m - buf.b:len()))
  22. return rv
  23. end
  24. function io.write(buf, d)
  25. d = d or buf
  26. buf = buf or io.output()
  27. print("bwrite",type(buf),d)
  28. if not buf.awrite then return nil end
  29. if buf.b:len() + d:len() > buf.m then
  30. buf.fh:write(buffer.read(buf,buf.m))
  31. end
  32. local rv = buffer.write(buf,d)
  33. if not buf.abmode then
  34. buf.fh:write(buffer.read(buf,buf.m))
  35. end
  36. return rv
  37. end
  38. function io.close(fh)
  39. fh.fh.close()
  40. fh.state = "closed"
  41. end
  42. function io.flush()
  43. end
  44. function io.open(fname,mode)
  45. mode=mode or "r"
  46. local buf = buffer.new()
  47. buf.fh, er = fs.open(fname,mode)
  48. if not buf.fh then
  49. error(er)
  50. end
  51. buf.state = "open"
  52. buf.aread = mode:match("r")
  53. buf.awrite = mode:match("w") or mode:match("a")
  54. setmetatable(buf,{__index=io})
  55. return buf
  56. end
  57. function print(...)
  58. for k,v in ipairs({...}) do
  59. io.write(string.format("%s\n",tostring(v)))
  60. end
  61. end
  62. io.stdin = io.open("/dev/null")
  63. io.stdout = io.open("/dev/null","w")
  64. function io.input(fname)
  65. if not fname then return os.getenv("STDIN") or io.stdin end
  66. os.setenv("STDIN",io.open(fname))
  67. end
  68. function io.output(fname)
  69. if not fname then return os.getenv("STDOUT") or io.stdout end
  70. os.setenv("STDOUT",io.open(fname,"w"))
  71. end
  72. end