From ebda2bb8ac64c57e253007568ce9791ace6e4eb0 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Wed, 22 Jan 2025 16:51:13 +0100 Subject: Hide password output when adding an account --- imp.ha | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/imp.ha b/imp.ha index 470d568..2aef8ba 100644 --- a/imp.ha +++ b/imp.ha @@ -9,6 +9,7 @@ use bufio; use format::ini; use encoding::utf8; use unix::tty; +use errors; let verbose: bool = false; @@ -126,11 +127,17 @@ export fn main() void = { }; const n = getinput("name: ")!; const u = getinput("user: ")!; - const p = getinput("pass: ")!; - acc.name = n; - acc.user = u; - acc.pass = p; - append(accounts, acc)!; + const p = getinput("pass: ", true)!; + if (n == "" || u == "" || p == "") { + free(n); + free(u); + free(p); + } else { + acc.name = n; + acc.user = u; + acc.pass = p; + append(accounts, acc)!; + }; }; accounts = accs_filter(accounts, accfilter); @@ -372,11 +379,7 @@ fn decrypt(file: str) ([]u8 | os::exec::error | io::error) = { return out; }; -// Gets user input from the tty (supports pipes) -fn getinput(text: str = "") (str | tty::error | io::error | utf8::invalid) = { - let termf = tty::open()?; - defer io::close(termf)!; - fmt::fprintf(termf, "{}", text)!; +fn readin(termf: io::file) (str | io::error | utf8::invalid) = { const scanner = bufio::newscanner(termf); defer bufio::finish(&scanner); const line = match (bufio::scan_line(&scanner)?) { @@ -387,3 +390,21 @@ fn getinput(text: str = "") (str | tty::error | io::error | utf8::invalid) = { }; return strings::dup(line); }; + +// Gets user input from the tty (supports pipes) +fn getinput(text: str = "", noecho: bool = false) (str | tty::error | io::error + | utf8::invalid | errors::error) = { + + let termf = tty::open()?; + defer io::close(termf)!; + + fmt::fprintf(termf, "{}", text)!; + + if (noecho) { + const termios = tty::termios_query(termf)?; + tty::noecho(&termios)?; + defer tty::termios_restore(&termios); + return readin(termf)?; + }; + return readin(termf)?; +}; -- cgit v1.2.3