@@ -49,15 +49,20 @@ local fileMeta = {
49
49
for i = 1 , n do
50
50
local format = data [i ] or " l"
51
51
format = checkType (format , " string" ):gsub (" %*" , " " ):sub (1 , 1 ) -- "*" is not needed after Lua 5.1 - lets be friendly
52
+
53
+ local res , msg
52
54
if format == " l" then
53
- returns [ # returns + 1 ] = handle .readLine ()
55
+ res , msg = handle .readLine ()
54
56
elseif format == " a" then
55
- returns [ # returns + 1 ] = handle .readAll ()
57
+ res , msg = handle .readAll ()
56
58
elseif format == " r" then
57
- returns [ # returns + 1 ] = handle .read () -- Binary only
59
+ res , msg = handle .read () -- Binary only
58
60
else
59
61
error (" (invalid format" , 2 )
60
62
end
63
+
64
+ if not res then return res , msg end
65
+ returns [# returns + 1 ] = res
61
66
end
62
67
63
68
return unpack (returns )
@@ -109,24 +114,24 @@ return function(env)
109
114
end
110
115
111
116
do -- Setup standard outputs
112
- local function void () end
113
- local function close () return nil , " cannot close standard file" end
114
- local function read () return nil , " bad file descriptor" end
117
+ local function voidStub () end
118
+ local function closeStub () return nil , " cannot close standard file" end
119
+ local function readStub () return nil , " bad file descriptor" end
115
120
116
121
env .stdout = setmetatable ({
117
122
__handle = {
118
- close = close ,
119
- flush = void ,
120
- read = read , readLine = read , readAll = read ,
123
+ close = closeStub ,
124
+ flush = voidStub ,
125
+ read = readStub , readLine = readStub , readAll = readStub ,
121
126
write = function (arg ) ansi .write (arg ) end ,
122
127
}
123
128
}, fileMeta )
124
129
125
130
env .stderr = setmetatable ({
126
131
__handle = {
127
- close = close ,
128
- flush = void ,
129
- read = read , readLine = read , readAll = read ,
132
+ close = closeStub ,
133
+ flush = voidStub ,
134
+ read = readStub , readLine = readStub , readAll = readStub ,
130
135
write = function (arg )
131
136
local c = term .isColor ()
132
137
if c then term .setTextColor (colors .red ) end
@@ -138,8 +143,8 @@ return function(env)
138
143
139
144
env .stdin = setmetatable ({
140
145
__handle = {
141
- close = close ,
142
- flush = void ,
146
+ close = closeStub ,
147
+ flush = voidStub ,
143
148
read = function () return string.byte (os .pullEvent (" char" )) end ,
144
149
readLine = read , readAll = read ,
145
150
write = function () error (" cannot write to input" , 3 ) end ,
0 commit comments