use std::fs::File;
use std::io::{ErrorKind, Read as _, Write as _};
use anyhow::{Context as _, Result, bail};
use rustix::termios::{
LocalModes, OptionalActions, SpecialCodeIndex, Termios, tcgetattr, tcsetattr,
};
use zeroize::Zeroizing;
const READ_CAP: usize = 4 * super::MAX_SECRET;
struct Restore<'a> {
tty: &'a File,
saved: Termios,
}
impl Drop for Restore<'_> {
fn drop(&mut self) {
let _ = tcsetattr(self.tty, OptionalActions::Now, &self.saved);
}
}
fn hidden(saved: &Termios) -> (Termios, Option<u8>) {
let mut hidden = saved.clone();
hidden.local_modes.remove(LocalModes::ECHO);
hidden.local_modes.insert(LocalModes::ECHONL);
let interrupt = saved.special_codes[SpecialCodeIndex::VINTR];
if interrupt == saved.special_codes[SpecialCodeIndex::VEOL] {
return (hidden, None);
}
hidden.local_modes.remove(LocalModes::ISIG);
hidden.special_codes[SpecialCodeIndex::VEOL] = interrupt;
(hidden, Some(interrupt))
}
pub fn read_hidden(tty: &File) -> Result<Zeroizing<Vec<u8>>> {
let saved = tcgetattr(tty).context("the controlling terminal is not a terminal")?;
let (settings, interrupt) = hidden(&saved);
let restore = Restore { tty, saved };
tcsetattr(tty, OptionalActions::Now, &settings).context("turning the terminal's echo off")?;
let mut line = Zeroizing::new(Vec::new());
let mut chunk = Zeroizing::new([0u8; 256]);
let mut reader = tty;
loop {
let n = match reader.read(chunk.as_mut()) {
Ok(n) => n,
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) => return Err(e).context("reading the secret from the terminal"),
};
line.extend_from_slice(&chunk[..n]);
let last = line.last().copied();
if n == 0 || last == Some(b'\n') || last == interrupt || line.len() > READ_CAP {
break;
}
}
drop(restore);
if interrupt.is_some() && line.last().copied() == interrupt {
let _ = (&mut reader).write_all(b"\n");
bail!("cancelled; nothing was saved");
}
if line.last() == Some(&b'\n') {
line.pop();
}
Ok(line)
}