Skip to content

Commit

Permalink
Explicitly read from /dev/tty
Browse files Browse the repository at this point in the history
We can not assume that stdin is a tty. This assumption can break very
easily for the using application, e.g if the stdin is piped (which is
not even in the control of the application developer but users).
  • Loading branch information
zeenix committed Mar 8, 2024
1 parent 3186573 commit abfb5aa
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ pub fn query_buffer<MS: Into<u64>>(
timeout_ms: MS,
) -> Result<usize, XQError> {
use mio::{unix::SourceFd, Events, Interest, Poll, Token};
use std::io::{self, Read, Write};
let stdin = io::stdin();
let mut stdin = stdin.lock();
use std::{
fs::File,
io::{self, Read, Write},
os::fd::AsRawFd,
};
let stdout = io::stdout();
let mut stdout = stdout.lock();
write!(stdout, "{}", query)?;
stdout.flush()?;
let mut stdin = File::open("/dev/tty")?;
let mut poll = Poll::new()?;
let mut events = Events::with_capacity(1024);
let mut stdin_fd = SourceFd(&nix::libc::STDIN_FILENO); // fancy way to pass the 0 const
let stdin_raw_fd = stdin.as_raw_fd();
let mut stdin_fd = SourceFd(&stdin_raw_fd); // fancy way to pass the 0 const
poll.registry()
.register(&mut stdin_fd, Token(0), Interest::READABLE)?;
let timeout = std::time::Duration::from_millis(timeout_ms.into());
Expand Down

0 comments on commit abfb5aa

Please sign in to comment.