Operating system for OpenComputers
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
11KB

  1. -- shamelessly stolen from plan9k
  2. buffer = {}
  3. function buffer.new(mode, stream) -- string table -- table -- create a new buffer in mode *mode* backed by stream object *stream*
  4. local result = {
  5. mode = {},
  6. stream = stream,
  7. bufferRead = "",
  8. bufferWrite = "",
  9. bufferSize = math.max(512, math.min(8 * 1024, computer.freeMemory() / 8)),
  10. bufferMode = "full",
  11. readTimeout = math.huge
  12. }
  13. mode = mode or "r"
  14. for i = 1, unicode.len(mode) do
  15. result.mode[unicode.sub(mode, i, i)] = true
  16. end
  17. local metatable = {
  18. __index = buffer,
  19. __metatable = "file"
  20. }
  21. return setmetatable(result, metatable)
  22. end
  23. local function badFileDescriptor()
  24. return nil, "bad file descriptor"
  25. end
  26. function buffer:close()
  27. if self.mode.w or self.mode.a then
  28. self:flush()
  29. end
  30. self.closed = true
  31. return self.stream:close()
  32. end
  33. function buffer:flush()
  34. local result, reason = self.stream:write(self.bufferWrite)
  35. if result then
  36. self.bufferWrite = ""
  37. else
  38. if reason then
  39. return nil, reason
  40. else
  41. return badFileDescriptor()
  42. end
  43. end
  44. return self
  45. end
  46. function buffer:lines(...)
  47. local args = table.pack(...)
  48. return function()
  49. local result = table.pack(self:read(table.unpack(args, 1, args.n)))
  50. if not result[1] and result[2] then
  51. error(result[2])
  52. end
  53. return table.unpack(result, 1, result.n)
  54. end
  55. end
  56. function buffer:read(...)
  57. local timeout = computer.uptime() + self.readTimeout
  58. local function readChunk()
  59. if computer.uptime() > timeout then
  60. error("timeout")
  61. end
  62. local result, reason = self.stream:read(self.bufferSize)
  63. if result then
  64. self.bufferRead = self.bufferRead .. result
  65. return self
  66. else -- error or eof
  67. return nil, reason
  68. end
  69. end
  70. local function readBytesOrChars(n)
  71. n = math.max(n, 0)
  72. local len, sub
  73. if self.mode.b then
  74. len = rawlen
  75. sub = string.sub
  76. else
  77. len = unicode.len
  78. sub = unicode.sub
  79. end
  80. local buffer = ""
  81. repeat
  82. if len(self.bufferRead) == 0 then
  83. local result, reason = readChunk()
  84. if not result then
  85. if reason then
  86. return nil, reason
  87. else -- eof
  88. return #buffer > 0 and buffer or nil
  89. end
  90. end
  91. end
  92. local left = n - len(buffer)
  93. buffer = buffer .. sub(self.bufferRead, 1, left)
  94. self.bufferRead = sub(self.bufferRead, left + 1)
  95. until len(buffer) == n
  96. return buffer
  97. end
  98. local function readNumber()
  99. local len, sub
  100. if self.mode.b then
  101. len = rawlen
  102. sub = string.sub
  103. else
  104. len = unicode.len
  105. sub = unicode.sub
  106. end
  107. local buffer = ""
  108. local first = true
  109. local decimal = false
  110. local last = false
  111. local hex = false
  112. local pat = "^[0-9]+"
  113. local minbuf = 3 -- "+0x" (sign + hexadecimal tag)
  114. -- this function is used to read trailing numbers (1e2, 0x1p2, etc)
  115. local function readnum(checksign)
  116. local _buffer = ""
  117. local sign = ""
  118. while true do
  119. if len(self.bufferRead) == 0 then
  120. local result, reason = readChunk()
  121. if not result then
  122. if reason then
  123. return nil, reason
  124. else -- eof
  125. return #_buffer > 0 and (sign .. _buffer) or nil
  126. end
  127. end
  128. end
  129. if checksign then
  130. local _sign = sub(self.bufferRead, 1, 1)
  131. if _sign == "+" or _sign == "-" then
  132. -- "eat" the sign (Rio Lua behaviour)
  133. sign = sub(self.bufferRead, 1, 1)
  134. self.bufferRead = sub(self.bufferRead, 2)
  135. end
  136. checksign = false
  137. else
  138. local x,y = string.find(self.bufferRead, pat)
  139. if not x then
  140. break
  141. else
  142. _buffer = _buffer .. sub(self.bufferRead, 1, y)
  143. self.bufferRead = sub(self.bufferRead, y + 1)
  144. end
  145. end
  146. end
  147. return #_buffer > 0 and (sign .. _buffer) or nil
  148. end
  149. while true do
  150. if len(self.bufferRead) == 0 or len(self.bufferRead) < minbuf then
  151. local result, reason = readChunk()
  152. if not result then
  153. if reason then
  154. return nil, reason
  155. else -- eof
  156. return #buffer > 0 and tonumber(buffer) or nil
  157. end
  158. end
  159. end
  160. -- these ifs are here so we run the buffer check above
  161. if first then
  162. local sign = sub(self.bufferRead, 1, 1)
  163. if sign == "+" or sign == "-" then
  164. -- "eat" the sign (Rio Lua behaviour)
  165. buffer = buffer .. sub(self.bufferRead, 1, 1)
  166. self.bufferRead = sub(self.bufferRead, 2)
  167. end
  168. local hextag = sub(self.bufferRead, 1, 2)
  169. if hextag == "0x" or hextag == "0X" then
  170. pat = "^[0-9A-Fa-f]+"
  171. -- "eat" the 0x, see https://gist.github.com/SoniEx2/570a363d81b743353151
  172. buffer = buffer .. sub(self.bufferRead, 1, 2)
  173. self.bufferRead = sub(self.bufferRead, 3)
  174. hex = true
  175. end
  176. minbuf = 0
  177. first = false
  178. elseif decimal then
  179. local sep = sub(self.bufferRead, 1, 1)
  180. if sep == "." then
  181. buffer = buffer .. sep
  182. self.bufferRead = sub(self.bufferRead, 2)
  183. local temp = readnum(false) -- no sign
  184. if temp then
  185. buffer = buffer .. temp
  186. end
  187. end
  188. if not tonumber(buffer) then break end
  189. decimal = false
  190. last = true
  191. minbuf = 1
  192. elseif last then
  193. local tag = sub(self.bufferRead, 1, 1)
  194. if hex and (tag == "p" or tag == "P") then
  195. local temp = sub(self.bufferRead, 1, 1)
  196. self.bufferRead = sub(self.bufferRead, 2)
  197. local temp2 = readnum(true) -- this eats the next sign if any
  198. if temp2 then
  199. buffer = buffer .. temp .. temp2
  200. end
  201. elseif tag == "e" or tag == "E" then
  202. local temp = sub(self.bufferRead, 1, 1)
  203. self.bufferRead = sub(self.bufferRead, 2)
  204. local temp2 = readnum(true) -- this eats the next sign if any
  205. if temp2 then
  206. buffer = buffer .. temp .. temp2
  207. end
  208. end
  209. break
  210. else
  211. local x,y = string.find(self.bufferRead, pat)
  212. if not x then
  213. minbuf = 1
  214. decimal = true
  215. else
  216. buffer = buffer .. sub(self.bufferRead, 1, y)
  217. self.bufferRead = sub(self.bufferRead, y + 1)
  218. end
  219. end
  220. end
  221. return tonumber(buffer)
  222. end
  223. local function readLine(chop)
  224. if not self.mode.t then
  225. local start = 1
  226. while true do
  227. local l = self.bufferRead:find("\n", start, true)
  228. if l then
  229. local result = self.bufferRead:sub(1, l + (chop and -1 or 0))
  230. self.bufferRead = self.bufferRead:sub(l + 1)
  231. return result
  232. else
  233. start = #self.bufferRead
  234. local result, reason = readChunk()
  235. if not result then
  236. if reason then
  237. return nil, reason
  238. else -- eof
  239. local result = #self.bufferRead > 0 and self.bufferRead or nil
  240. self.bufferRead = ""
  241. return result
  242. end
  243. end
  244. end
  245. end
  246. else
  247. -- ?????
  248. io.write("\27[s\27[8m")
  249. local pos, buffer = 1, ""
  250. local function redraw()
  251. io.write("\27[u")
  252. io.write(buffer.." ")
  253. if pos < 1 then
  254. io.write("\28[D")
  255. else
  256. io.write("\27[u")
  257. io.write(buffer:sub(1,(#buffer-pos)+1))
  258. end
  259. end
  260. while true do
  261. char = readBytesOrChars(1)
  262. if char == "\27" then
  263. if readBytesOrChars(1) == "[" then
  264. local args = {""}
  265. repeat
  266. char = readBytesOrChars(1)
  267. --[[
  268. if char:match("%d") then
  269. args[#args] = args[#args]..char
  270. else
  271. args[#args] = tonumber(args[#args])
  272. args[#args+1] = ""
  273. end
  274. ]]
  275. until not char:match("[%d;]")
  276. if char == "C" then -- right
  277. if pos > 1 then
  278. pos = pos - 1
  279. end
  280. elseif char == "D" then -- left
  281. if pos <= #buffer then
  282. pos = pos + 1
  283. end
  284. end
  285. end
  286. elseif char == "\8" then
  287. if #buffer > 0 and pos <= #buffer then
  288. buffer = buffer:sub(1, (#buffer - pos)) .. buffer:sub((#buffer - pos) + 2)
  289. end
  290. elseif char == "\13" or char == "\10" or char == "\n" then
  291. io.write("\n")
  292. if chop then buffer = buffer .. "\n" end
  293. return buffer
  294. else
  295. buffer = buffer:sub(1, (#buffer - pos) + 1) .. char .. buffer:sub((#buffer - pos) + 2)
  296. end
  297. redraw()
  298. end
  299. end
  300. end
  301. local function readAll()
  302. repeat
  303. local result, reason = readChunk()
  304. if not result and reason then
  305. return nil, reason
  306. end
  307. until not result -- eof
  308. local result = self.bufferRead
  309. self.bufferRead = ""
  310. return result
  311. end
  312. local function read(n, format)
  313. if type(format) == "number" then
  314. return readBytesOrChars(format)
  315. else
  316. if type(format) ~= "string" or unicode.sub(format, 1, 1) ~= "*" then
  317. error("bad argument #" .. n .. " (invalid option)")
  318. end
  319. format = unicode.sub(format, 2, 2)
  320. if format == "n" then
  321. return readNumber()
  322. elseif format == "l" then
  323. return readLine(true)
  324. elseif format == "L" then
  325. return readLine(false)
  326. elseif format == "a" then
  327. return readAll()
  328. else
  329. error("bad argument #" .. n .. " (invalid format)")
  330. end
  331. end
  332. end
  333. if self.mode.w or self.mode.a then
  334. self:flush()
  335. end
  336. local results = {}
  337. local formats = table.pack(...)
  338. if formats.n == 0 then
  339. return readLine(true)
  340. end
  341. for i = 1, formats.n do
  342. local result, reason = read(i, formats[i])
  343. if result then
  344. results[i] = result
  345. elseif reason then
  346. return nil, reason
  347. end
  348. end
  349. return table.unpack(results, 1, formats.n)
  350. end
  351. function buffer:seek(whence, offset)
  352. whence = tostring(whence or "cur")
  353. assert(whence == "set" or whence == "cur" or whence == "end",
  354. "bad argument #1 (set, cur or end expected, got " .. whence .. ")")
  355. offset = offset or 0
  356. checkArg(2, offset, "number")
  357. assert(math.floor(offset) == offset, "bad argument #2 (not an integer)")
  358. if self.mode.w or self.mode.a then
  359. self:flush()
  360. elseif whence == "cur" then
  361. offset = offset - #self.bufferRead
  362. end
  363. local result, reason = self.stream:seek(whence, offset)
  364. if result then
  365. self.bufferRead = ""
  366. return result
  367. else
  368. return nil, reason
  369. end
  370. end
  371. function buffer:setvbuf(mode, size)
  372. mode = mode or self.bufferMode
  373. size = size or self.bufferSize
  374. assert(mode == "no" or mode == "full" or mode == "line",
  375. "bad argument #1 (no, full or line expected, got " .. tostring(mode) .. ")")
  376. assert(mode == "no" or type(size) == "number",
  377. "bad argument #2 (number expected, got " .. type(size) .. ")")
  378. self.bufferMode = mode
  379. self.bufferSize = size
  380. return self.bufferMode, self.bufferSize
  381. end
  382. function buffer:getTimeout()
  383. return self.readTimeout
  384. end
  385. function buffer:setTimeout(value)
  386. self.readTimeout = tonumber(value)
  387. end
  388. function buffer:write(...)
  389. if self.closed then
  390. return badFileDescriptor()
  391. end
  392. local args = table.pack(...)
  393. for i = 1, args.n do
  394. if type(args[i]) == "number" then
  395. args[i] = tostring(args[i])
  396. end
  397. checkArg(i, args[i], "string")
  398. end
  399. for i = 1, args.n do
  400. local arg = args[i]
  401. local result, reason
  402. if self.bufferMode == "full" then
  403. if self.bufferSize - #self.bufferWrite < #arg then
  404. result, reason = self:flush()
  405. if not result then
  406. return nil, reason
  407. end
  408. end
  409. if #arg > self.bufferSize then
  410. result, reason = self.stream:write(arg)
  411. else
  412. self.bufferWrite = self.bufferWrite .. arg
  413. result = self
  414. end
  415. elseif self.bufferMode == "line" then
  416. local l
  417. repeat
  418. local idx = arg:find("\n", (l or 0) + 1, true)
  419. if idx then
  420. l = idx
  421. end
  422. until not idx
  423. if l or #arg > self.bufferSize then
  424. result, reason = self:flush()
  425. if not result then
  426. return nil, reason
  427. end
  428. end
  429. if l then
  430. result, reason = self.stream:write(arg:sub(1, l))
  431. if not result then
  432. return nil, reason
  433. end
  434. arg = arg:sub(l + 1)
  435. end
  436. if #arg > self.bufferSize then
  437. result, reason = self.stream:write(arg)
  438. else
  439. self.bufferWrite = self.bufferWrite .. arg
  440. result = self
  441. end
  442. else -- self.bufferMode == "no"
  443. result, reason = self.stream:write(arg)
  444. end
  445. if not result then
  446. return nil, reason
  447. end
  448. end
  449. return self
  450. end