-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.eeprom.lua
More file actions
152 lines (129 loc) · 3.27 KB
/
test.eeprom.lua
File metadata and controls
152 lines (129 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
-------------------------------------------------------------------------------
local io = {}
function io.close(file)
return (file or io.output()):close()
end
function io.flush()
return io.output():flush()
end
function io.lines(filename, ...)
if filename then
local file, reason = io.open(filename)
if not file then
error(reason, 2)
end
local args = table.pack(...)
return function()
local result = table.pack(file:read(table.unpack(args, 1, args.n)))
if not result[1] then
if result[2] then
error(result[2], 2)
else -- eof
file:close()
return nil
end
end
return table.unpack(result, 1, result.n)
end
else
return io.input():lines()
end
end
function io.open(path, mode)
-- These requires are not on top because this is a bootstrapped file.
local resolved_path = require("shell").resolve(path)
local stream, result = require("filesystem").open(resolved_path, mode)
if stream then
return require("buffer").new(mode, stream)
else
return nil, result
end
end
function io.stream(fd,file,mode)
checkArg(1,fd,'number')
assert(fd>=0,'fd must be >= 0. 0 is input, 1 is stdout, 2 is stderr')
local dio = require("process").info().data.io
if file then
if type(file) == "string" then
local result, reason = io.open(file, mode)
if not result then
error(reason, 2)
end
file = result
elseif not io.type(file) then
error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
end
dio[fd] = file
end
return dio[fd]
end
function io.input(file)
return io.stream(0, file, 'r')
end
function io.output(file)
return io.stream(1, file,'w')
end
function io.error(file)
return io.stream(2, file,'w')
end
function io.popen(prog, mode, env)
return require("pipe").popen(prog, mode, env)
end
function io.read(...)
return io.input():read(...)
end
function io.tmpfile()
local name = os.tmpname()
if name then
return io.open(name, "a")
end
end
function io.type(object)
if type(object) == "table" then
if getmetatable(object) == "file" then
if object.stream.handle then
return "file"
else
return "closed file"
end
end
end
return nil
end
function io.write(...)
return io.output():write(...)
end
local dup_mt = {__index = function(dfd, key)
local fd_value = dfd.fd[key]
if key ~= "close" and type(fd_value) ~= "function" then return fd_value end
return function(self, ...)
if key == "close" or self._closed then self._closed = true return end
return fd_value(self.fd, ...)
end
end, __newindex = function(dfd, key, value)
dfd.fd[key] = value
end}
function io.dup(fd)
return setmetatable({fd=fd,_closed=false}, dup_mt)
end
-------------------------------------------------------------------------------
local stream = io.stderr
local write = stream.write
write = function(_, msg)
require("event").onError(msg)
end
write('', 'Hello World')
-------------------------------------------------------------------------------
function print(...)
local args = table.pack(...)
local stdout = io.stdout
local pre = ""
for i = 1, args.n do
stdout:write(pre, (assert(tostring(args[i]), "'tostring' must return a string to 'print'")))
pre = "\t"
end
stdout:write("\n")
stdout:flush()
end
-------------------------------------------------------------------------------
print('Hello World!')