Operating system for OpenComputers
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

476 wiersze
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"..buffer.." \27[u\27["..tostring((#buffer-pos)+1).."C")
  252. end
  253. while true do
  254. char = readBytesOrChars(1)
  255. if char == "\27" then
  256. if readBytesOrChars(1) == "[" then
  257. local args = {""}
  258. repeat
  259. char = readBytesOrChars(1)
  260. --[[
  261. if char:match("%d") then
  262. args[#args] = args[#args]..char
  263. else
  264. args[#args] = tonumber(args[#args])
  265. args[#args+1] = ""
  266. end
  267. ]]
  268. until not char:match("[%d;]")
  269. if char == "C" then -- right
  270. if pos > 1 then
  271. pos = pos - 1
  272. end
  273. elseif char == "D" then -- left
  274. if pos <= #buffer then
  275. pos = pos + 1
  276. end
  277. end
  278. end
  279. elseif char == "\8" then
  280. if #buffer > 0 and pos <= #buffer then
  281. buffer = buffer:sub(1, (#buffer - pos)) .. buffer:sub((#buffer - pos) + 2)
  282. end
  283. elseif char == "\13" or char == "\10" or char == "\n" then
  284. io.write("\n")
  285. if chop then buffer = buffer .. "\n" end
  286. return buffer
  287. else
  288. buffer = buffer:sub(1, (#buffer - pos) + 1) .. char .. buffer:sub((#buffer - pos) + 2)
  289. end
  290. redraw()
  291. end
  292. end
  293. end
  294. local function readAll()
  295. repeat
  296. local result, reason = readChunk()
  297. if not result and reason then
  298. return nil, reason
  299. end
  300. until not result -- eof
  301. local result = self.bufferRead
  302. self.bufferRead = ""
  303. return result
  304. end
  305. local function read(n, format)
  306. if type(format) == "number" then
  307. return readBytesOrChars(format)
  308. else
  309. if type(format) ~= "string" or unicode.sub(format, 1, 1) ~= "*" then
  310. error("bad argument #" .. n .. " (invalid option)")
  311. end
  312. format = unicode.sub(format, 2, 2)
  313. if format == "n" then
  314. return readNumber()
  315. elseif format == "l" then
  316. return readLine(true)
  317. elseif format == "L" then
  318. return readLine(false)
  319. elseif format == "a" then
  320. return readAll()
  321. else
  322. error("bad argument #" .. n .. " (invalid format)")
  323. end
  324. end
  325. end
  326. if self.mode.w or self.mode.a then
  327. self:flush()
  328. end
  329. local results = {}
  330. local formats = table.pack(...)
  331. if formats.n == 0 then
  332. return readLine(true)
  333. end
  334. for i = 1, formats.n do
  335. local result, reason = read(i, formats[i])
  336. if result then
  337. results[i] = result
  338. elseif reason then
  339. return nil, reason
  340. end
  341. end
  342. return table.unpack(results, 1, formats.n)
  343. end
  344. function buffer:seek(whence, offset)
  345. whence = tostring(whence or "cur")
  346. assert(whence == "set" or whence == "cur" or whence == "end",
  347. "bad argument #1 (set, cur or end expected, got " .. whence .. ")")
  348. offset = offset or 0
  349. checkArg(2, offset, "number")
  350. assert(math.floor(offset) == offset, "bad argument #2 (not an integer)")
  351. if self.mode.w or self.mode.a then
  352. self:flush()
  353. elseif whence == "cur" then
  354. offset = offset - #self.bufferRead
  355. end
  356. local result, reason = self.stream:seek(whence, offset)
  357. if result then
  358. self.bufferRead = ""
  359. return result
  360. else
  361. return nil, reason
  362. end
  363. end
  364. function buffer:setvbuf(mode, size)
  365. mode = mode or self.bufferMode
  366. size = size or self.bufferSize
  367. assert(mode == "no" or mode == "full" or mode == "line",
  368. "bad argument #1 (no, full or line expected, got " .. tostring(mode) .. ")")
  369. assert(mode == "no" or type(size) == "number",
  370. "bad argument #2 (number expected, got " .. type(size) .. ")")
  371. self.bufferMode = mode
  372. self.bufferSize = size
  373. return self.bufferMode, self.bufferSize
  374. end
  375. function buffer:getTimeout()
  376. return self.readTimeout
  377. end
  378. function buffer:setTimeout(value)
  379. self.readTimeout = tonumber(value)
  380. end
  381. function buffer:write(...)
  382. if self.closed then
  383. return badFileDescriptor()
  384. end
  385. local args = table.pack(...)
  386. for i = 1, args.n do
  387. if type(args[i]) == "number" then
  388. args[i] = tostring(args[i])
  389. end
  390. checkArg(i, args[i], "string")
  391. end
  392. for i = 1, args.n do
  393. local arg = args[i]
  394. local result, reason
  395. if self.bufferMode == "full" then
  396. if self.bufferSize - #self.bufferWrite < #arg then
  397. result, reason = self:flush()
  398. if not result then
  399. return nil, reason
  400. end
  401. end
  402. if #arg > self.bufferSize then
  403. result, reason = self.stream:write(arg)
  404. else
  405. self.bufferWrite = self.bufferWrite .. arg
  406. result = self
  407. end
  408. elseif self.bufferMode == "line" then
  409. local l
  410. repeat
  411. local idx = arg:find("\n", (l or 0) + 1, true)
  412. if idx then
  413. l = idx
  414. end
  415. until not idx
  416. if l or #arg > self.bufferSize then
  417. result, reason = self:flush()
  418. if not result then
  419. return nil, reason
  420. end
  421. end
  422. if l then
  423. result, reason = self.stream:write(arg:sub(1, l))
  424. if not result then
  425. return nil, reason
  426. end
  427. arg = arg:sub(l + 1)
  428. end
  429. if #arg > self.bufferSize then
  430. result, reason = self.stream:write(arg)
  431. else
  432. self.bufferWrite = self.bufferWrite .. arg
  433. result = self
  434. end
  435. else -- self.bufferMode == "no"
  436. result, reason = self.stream:write(arg)
  437. end
  438. if not result then
  439. return nil, reason
  440. end
  441. end
  442. return self
  443. end