password_input.lua
local sys = require "system"
print [[
This example shows how to disable the "echo" of characters read to the console,
useful for reading secrets from the user.
]]
local function read_secret(...)
local w_oldflags, p_oldflags
if sys.isatty(io.stdin) then
w_oldflags = sys.getconsoleflags(io.stdin)
p_oldflags = sys.tcgetattr(io.stdin)
assert(sys.setconsoleflags(io.stdin, w_oldflags - sys.CIF_ECHO_INPUT))
assert(sys.tcsetattr(io.stdin, sys.TCSANOW, { lflag = p_oldflags.lflag - sys.L_ECHO }))
end
local secret, err = io.stdin:read(...)
if sys.isatty(io.stdin) then
io.stdout:write("\n") sys.setconsoleflags(io.stdin, w_oldflags)
sys.tcsetattr(io.stdin, sys.TCSANOW, p_oldflags)
end
return secret, err
end
io.write("Username: ")
local username = io.stdin:read("*l")
io.write("Password: ")
local password = read_secret("*l")
io.write("Domain : ")
local domain = io.stdin:read("*l")
print("")
print("Here's what we got:")
print(" username: " .. username)
print(" password: " .. password)
print(" domain : " .. domain)