made io.stderr an extra stream that applies a red foreground color (if possible); writing errors to stderr in programs; quoting and escaping vars in list displayed by set.lua

This commit is contained in:
Florian Nücke 2014-02-19 00:37:54 +01:00
parent 2b4124356f
commit f7324515b9
30 changed files with 93 additions and 60 deletions

View File

@ -6,7 +6,7 @@ local internet = require("internet")
local shell = require("shell")
if not component.isAvailable("internet") then
io.write("This program requires an internet card to run.")
io.stderr:write("This program requires an internet card to run.")
return
end
@ -16,7 +16,7 @@ local args, options = shell.parse(...)
local function get(pasteId, filename)
local f, reason = io.open(filename, "w")
if not f then
io.write("Failed opening file for writing: ", reason)
io.stderr:write("Failed opening file for writing: ", reason)
return
end
@ -37,7 +37,7 @@ local function get(pasteId, filename)
else
f:close()
fs.remove(filename)
io.write("HTTP request failed: ", response, "\n")
io.stderr:write("HTTP request failed: ", response, "\n")
end
end
@ -61,7 +61,7 @@ function run(pasteId, ...)
local success, reason = shell.execute(tmpFile, nil, ...)
if not success then
io.write(reason)
io.stderr:write(reason)
end
fs.remove(tmpFile)
end
@ -73,14 +73,14 @@ function put(path)
if configFile then
local result, reason = pcall(configFile)
if not result then
io.write("Failed loading config: ", reason)
io.stderr:write("Failed loading config: ", reason)
end
end
config.key = config.key or "fd92bd40a84c127eeb6804b146793c97"
local file, reason = io.open(path, "r")
if not file then
io.write("Failed opening file for reading: ", reason)
io.stderr:write("Failed opening file for reading: ", reason)
return
end
@ -112,7 +112,8 @@ function put(path)
io.write('Run "pastebin get ', pasteId, '" to download anywhere.')
end
else
io.write("failed: ", response)
io.write("failed.\n")
io.stderr:write(response)
end
end
@ -127,7 +128,7 @@ elseif command == "get" then
local path = shell.resolve(args[3])
if fs.exists(path) then
if not options.f or not os.remove(path) then
io.write("file already exists")
io.stderr:write("file already exists")
return
end
end

View File

@ -5,7 +5,7 @@ local shell = require("shell")
local text = require("text")
if not component.isAvailable("internet") then
io.write("This program requires an internet card to run.")
io.stderr:write("This program requires an internet card to run.")
return
end
@ -33,21 +33,21 @@ if not filename then
end
filename = text.trim(filename)
if filename == "" then
io.write("could not infer filename, please specify one")
io.stderr:write("could not infer filename, please specify one")
return
end
filename = shell.resolve(filename)
if fs.exists(filename) then
if not options.f or not os.remove(filename) then
io.write("file already exists")
io.stderr:write("file already exists")
return
end
end
local f, reason = io.open(filename, "wb")
if not f then
io.write("Failed opening file for writing: ", reason)
io.stderr:write("failed opening file for writing: ", reason)
return
end
@ -68,7 +68,10 @@ if result then
io.write("Saved data to ", filename, "\n")
end
else
if not options.q then
io.write("failed.\n")
end
f:close()
fs.remove(filename)
io.write("HTTP request failed: ", response, "\n")
io.stderr:write("HTTP request failed: ", response, "\n")
end

View File

@ -5,7 +5,8 @@ local shell = require("shell")
local sides = require("sides")
if not computer.isRobot() then
error("can only run on robots", 0)
io.stderr:write("can only run on robots")
return
end
local args, options = shell.parse(...)
@ -17,7 +18,8 @@ end
local size = tonumber(args[1])
if not size then
error("invalid size", 0)
io.stderr:write("invalid size")
return
end
local r = component.computer

View File

@ -11,7 +11,7 @@ elseif #args == 1 then
if value then
io.write(value)
else
io.write("no such alias")
io.stderr:write("no such alias")
end
else
shell.setAlias(args[1], args[2])

View File

@ -12,7 +12,7 @@ else
for i = 1, #args do
local file, reason = io.open(shell.resolve(args[i]))
if not file then
io.write(reason)
io.stderr:write(reason)
return
end
repeat

View File

@ -6,6 +6,6 @@ if #args == 0 then
else
local result, reason = shell.setWorkingDirectory(shell.resolve(args[1]))
if not result then
io.write(reason)
io.stderr:write(reason)
end
end

View File

@ -14,9 +14,10 @@ if fs.isDirectory(to) then
to = to .. "/" .. fs.name(from)
end
if fs.exists(to) and not options.f then
error("target file exists")
io.stderr:write("target file exists")
return
end
local result, reason = fs.copy(from, to)
if not result then
io.write(reason)
io.stderr:write(reason)
end

View File

@ -12,7 +12,7 @@ else
for i = 1, #args do
local proxy, path = fs.get(args[i])
if not proxy then
io.write(args[i], ": no such file or directory\n")
io.stderr:write(args[i], ": no such file or directory\n")
else
mounts[path] = proxy
end

View File

@ -22,7 +22,7 @@ local filename = shell.resolve(args[1])
local readonly = options.r or fs.get(filename) == nil or fs.get(filename).isReadOnly()
if fs.isDirectory(filename) or readonly and not fs.exists(filename) then
io.write("file not found")
io.stderr:write("file not found")
return
end

View File

@ -15,15 +15,15 @@ else
proxy, reaons = fs.get(args[1])
end
if not proxy then
io.write(reason)
io.stderr:write(reason)
return
end
if #args < 2 then
io.write(proxy.getLabel() or "no label")
io.stderr:write(proxy.getLabel() or "no label")
else
local result, reason = proxy.setLabel(args[2])
if not result then
io.write(reason or "could not set label")
io.stderr:write(reason or "could not set label")
end
end

View File

@ -18,5 +18,5 @@ end
local result, reason = fs.link(target, linkpath)
if not result then
io.write(reason)
io.stderr:write(reason)
end

View File

@ -46,7 +46,7 @@ while term.isAvailable() do
if type(result[2]) == "table" and result[2].reason == "terminated" then
os.exit(result[2].code)
end
io.write(tostring(result[2]), "\n")
io.stderr:write(tostring(result[2]), "\n")
else
for i = 2, result.n do
io.write(text.serialize(result[i], true), "\t")
@ -56,6 +56,6 @@ while term.isAvailable() do
end
end
else
io.write(reason, "\n")
io.stderr:write(reason, "\n")
end
end

View File

@ -16,4 +16,4 @@ for path in string.gmatch(os.getenv("MANPATH"), "[^:]+") do
os.exit()
end
end
io.write("No manual entry for " .. topic)
io.stderr:write("No manual entry for " .. topic)

View File

@ -18,6 +18,6 @@ for i = 1, #args do
reason = "unknown reason"
end
end
io.write(path, ": ", reason, "\n")
io.stderr:write(path, ": ", reason, "\n")
end
end

View File

@ -12,7 +12,7 @@ end
local file, reason = io.open(shell.resolve(args[1]))
if not file then
io.write(reason)
io.stderr:write(reason)
return
end

View File

@ -18,11 +18,11 @@ end
local proxy, reason = fs.proxy(args[1])
if not proxy then
io.write(reason)
io.stderr:write(reason)
return
end
local result, reason = fs.mount(proxy, shell.resolve(args[2]))
if not result then
io.write(reason)
io.stderr:write(reason)
end

View File

@ -15,11 +15,11 @@ if fs.isDirectory(to) then
end
if fs.exists(to) then
if not options.f then
io.write("target file exists")
io.stderr:write("target file exists")
end
fs.remove(to)
end
local result, reason = os.rename(from, to)
if not result then
io.write(reason or "unknown error")
io.stderr:write(reason or "unknown error")
end

View File

@ -13,7 +13,7 @@ local componentType = args[1]
if #args > 1 then
local address = args[2]
if not component.get(address) then
io.write("no component with this address")
io.stderr:write("no component with this address")
return
else
component.setPrimary(componentType, address)
@ -23,5 +23,5 @@ end
if component.isAvailable(componentType) then
io.write(component.getPrimary(componentType).address)
else
io.write("no primary component for this type")
io.stderr:write("no primary component for this type")
end

View File

@ -3,6 +3,10 @@ local component = require("component")
local shell = require("shell")
local sides = require("sides")
if not component.isAvailable("redstone") then
io.stderr:write("This program requires a redstone card or redstone I/O block.")
return
end
local rs = component.redstone
local args, options = shell.parse(...)
@ -17,7 +21,8 @@ end
local side = sides[args[1]]
if not side then
error("Invalid side.")
io.stderr:write("invalid side")
return
end
if type(side) == "string" then
side = sides[side]
@ -25,11 +30,13 @@ end
if options.b then
if not rs.setBundledOutput then
error("Bundled redstone not available.")
io.stderr:write("bundled redstone not available")
return
end
local color = colors[args[2]]
if not color then
error("Invalid color.")
io.stderr:write("invalid color")
return
end
if type(color) == "string" then
color = colors[color]

View File

@ -17,12 +17,13 @@ end
local w = tonumber(args[1])
local h = tonumber(args[2])
if not w or not h then
io.write("invalid width or height")
io.stderr:write("invalid width or height")
return
end
local result, reason = component.gpu.setResolution(w, h)
if not result then
io.write(reason)
io.stderr:write(reason)
return
end
term.clear()

View File

@ -9,6 +9,6 @@ end
for i = 1, #args do
local path = shell.resolve(args[i])
if not os.remove(path) then
io.write(path, ": no such file, or permission denied\n")
io.stderr:write(path, ": no such file, or permission denied\n")
end
end

View File

@ -2,7 +2,7 @@ local args = {...}
if #args < 1 then
for k,v in pairs(os.getenv()) do
io.write(k..'='..v..'\n')
io.write(k, "='", string.gsub(v, "'", [['"'"']]), "'\n")
end
else
local count = 1

View File

@ -9,7 +9,7 @@ local args, options = shell.parse(...)
local history = {}
if options.v or not shell.running(2) then
print(_OSVERSION .. " (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
io.write(_OSVERSION .. " (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)\n")
end
while true do
@ -19,7 +19,7 @@ while true do
end
term.clear()
if options.v then
print(_OSVERSION .. " (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)")
io.write(_OSVERSION .. " (" .. math.floor(computer.totalMemory() / 1024) .. "k RAM)\n")
end
end
while term.isAvailable() do
@ -28,7 +28,7 @@ while true do
component.gpu.setForeground(foreground)
local command = term.read(history)
if not command then
print("exit")
io.write("exit\n")
return -- eof
end
while #history > 10 do
@ -40,10 +40,10 @@ while true do
elseif command ~= "" then
local result, reason = os.execute(command)
if not result then
print(reason)
io.stderr:write(reason .. "\n")
elseif term.getCursor() > 1 then
print()
io.write("\n")
end
end
end
end
end

View File

@ -21,16 +21,16 @@ else
if proxy then
proxy = reason -- = path
if proxy ~= path then
io.write("not a mount point")
io.stderr:write("not a mount point")
return
end
end
end
if not proxy then
io.write(reason)
io.stderr:write(reason)
return
end
if not fs.umount(proxy) then
io.write("nothing to unmount here")
io.stderr:write("nothing to unmount here")
end

View File

@ -8,7 +8,7 @@ end
local result = shell.getAlias(args[1])
if not result then
io.write("no such alias")
io.stderr:write("no such alias")
else
shell.setAlias(args[1], nil)
io.write("alias removed: ", args[1], " -> ", result)

View File

@ -1,9 +1,9 @@
local args = {...}
if #args < 1 then
print "You have to specify which arguments to unset!"
print("Usage: unset <varname>[ <varname2> [...]]")
else
for _, k in ipairs(args) do
os.setenv(k, nil)
end
end
end

View File

@ -9,5 +9,5 @@ end
local result, reason = computer.addUser(args[1])
if not result then
io.write(reason)
io.stderr:write(reason)
end

View File

@ -8,5 +8,5 @@ if #args < 1 then
end
if not computer.removeUser(args[1]) then
io.write("no such user")
io.stderr:write("no such user")
end

View File

@ -16,6 +16,6 @@ for i = 1, #args do
if result then
io.write(result, "\n")
else
io.write(args[i], ": ", reason, "\n")
io.stderr:write(args[i], ": ", reason, "\n")
end
end

View File

@ -3,6 +3,7 @@ local term = require("term")
local stdinStream = {handle="stdin"}
local stdoutStream = {handle="stdout"}
local stderrStream = {handle="stderr"}
local stdinHistory = {}
local function badFileDescriptor()
@ -13,6 +14,7 @@ function stdinStream:close()
return nil, "cannot close standard file"
end
stdoutStream.close = stdinStream.close
stderrStream.close = stdinStream.close
function stdinStream:read(n)
local result = term.read(stdinHistory)
@ -27,16 +29,32 @@ function stdoutStream:write(str)
return self
end
function stderrStream:write(str)
local component = require("component")
if component.isAvailable("gpu") and component.gpu.getDepth() > 1 then
local foreground = component.gpu.getForeground()
component.gpu.setForeground(0xFF0000)
term.write(str, true)
component.gpu.setForeground(foreground)
else
term.write(str, true)
end
return self
end
stdinStream.seek = badFileDescriptor
stdinStream.write = badFileDescriptor
stdoutStream.read = badFileDescriptor
stdoutStream.seek = badFileDescriptor
stderrStream.read = badFileDescriptor
stderrStream.seek = badFileDescriptor
io.stdin = buffer.new("r", stdinStream)
io.stdout = buffer.new("w", stdoutStream)
io.stderr = io.stdout
io.stderr = buffer.new("w", stderrStream)
io.stdout:setvbuf("no")
io.stderr:setvbuf("no")
io.input(io.stdin)
io.output(io.stdout)