diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2025-01-22 16:51:13 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2025-01-22 16:51:13 +0100 |
| commit | ebda2bb8ac64c57e253007568ce9791ace6e4eb0 (patch) | |
| tree | 501376ab08e36ab90eed6bfc8153b0146b0a7814 | |
| parent | e34829a3f1c6c08b825681e2aa15d738fe075d14 (diff) | |
| download | imp-ebda2bb8ac64c57e253007568ce9791ace6e4eb0.tar.gz | |
Hide password output when adding an account
| -rw-r--r-- | imp.ha | 41 |
1 files changed, 31 insertions, 10 deletions
@@ -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)?; +}; |
