From abfb5aae9e7539cb9cd3c8b3fcc98a7dfa5e6276 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Fri, 8 Mar 2024 14:26:11 +0100 Subject: [PATCH] Explicitly read from `/dev/tty` 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). --- src/lib.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1aa1596..1b5b3d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,16 +25,20 @@ pub fn query_buffer>( timeout_ms: MS, ) -> Result { 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());