55use nix:: fcntl:: { OFlag , open} ;
66use nix:: libc:: STDERR_FILENO ;
77use nix:: pty:: { PtyMaster , grantpt, posix_openpt, unlockpt} ;
8- pub use nix:: sys:: { signal, wait} ;
98use nix:: sys:: { stat, termios} ;
109use nix:: unistd:: {
1110 ForkResult , Pid , close, dup, dup2_stderr, dup2_stdin, dup2_stdout, fork, setsid,
@@ -18,6 +17,10 @@ use std::os::unix::process::CommandExt;
1817use std:: process:: Command ;
1918use std:: { thread, time} ;
2019
20+ pub use nix:: sys:: { signal, wait} ;
21+ pub use signal:: Signal ;
22+ pub use wait:: WaitStatus ;
23+
2124/// Start a process in a forked tty to interact with it like you would
2225/// within a terminal
2326///
@@ -42,8 +45,7 @@ use std::{thread, time};
4245/// # fn main() {
4346///
4447/// let mut process = PtyProcess::new(Command::new("cat")).expect("could not execute cat");
45- /// let fd = dup(&process.pty).unwrap();
46- /// let f = File::from(fd);
48+ /// let f = process.get_file_handle().unwrap();
4749/// let mut writer = LineWriter::new(&f);
4850/// let mut reader = BufReader::new(&f);
4951/// process.exit().expect("could not terminate process");
@@ -161,7 +163,7 @@ impl PtyProcess {
161163 /// # Example
162164 /// ```rust,no_run
163165 ///
164- /// use rexpect::process::{self, wait:: WaitStatus};
166+ /// use rexpect::process::{self, WaitStatus};
165167 /// use std::process::Command;
166168 ///
167169 /// # fn main() {
@@ -173,26 +175,26 @@ impl PtyProcess {
173175 /// # }
174176 /// ```
175177 ///
176- pub fn status ( & self ) -> Option < wait :: WaitStatus > {
178+ pub fn status ( & self ) -> Option < WaitStatus > {
177179 wait:: waitpid ( self . child_pid , Some ( wait:: WaitPidFlag :: WNOHANG ) ) . ok ( )
178180 }
179181
180182 /// Wait until process has exited (non-blocking).
181183 ///
182184 /// If the process doesn't terminate this will block forever.
183- pub fn wait ( & self ) -> Result < wait :: WaitStatus , Error > {
185+ pub fn wait ( & self ) -> Result < WaitStatus , Error > {
184186 wait:: waitpid ( self . child_pid , None ) . map_err ( Error :: from)
185187 }
186188
187189 /// Regularly exit the process (blocking).
188190 ///
189191 /// This method is blocking until the process is dead
190- pub fn exit ( & mut self ) -> Result < wait :: WaitStatus , Error > {
192+ pub fn exit ( & mut self ) -> Result < WaitStatus , Error > {
191193 self . kill ( signal:: SIGTERM )
192194 }
193195
194196 /// Kill the process with a specific signal (non-blocking).
195- pub fn signal ( & mut self , sig : signal :: Signal ) -> Result < ( ) , Error > {
197+ pub fn signal ( & mut self , sig : Signal ) -> Result < ( ) , Error > {
196198 signal:: kill ( self . child_pid , sig) . map_err ( Error :: from)
197199 }
198200
@@ -206,26 +208,26 @@ impl PtyProcess {
206208 ///
207209 /// If `kill_timeout` is set and a repeated sending of signal does not result in the process
208210 /// being killed, then `kill -9` is sent after the `kill_timeout` duration has elapsed.
209- pub fn kill ( & mut self , sig : signal :: Signal ) -> Result < wait :: WaitStatus , Error > {
211+ pub fn kill ( & mut self , sig : Signal ) -> Result < WaitStatus , Error > {
210212 let start = time:: Instant :: now ( ) ;
211213 loop {
212214 match signal:: kill ( self . child_pid , sig) {
213215 Ok ( _) => { }
214216 // process was already killed before -> ignore
215217 Err ( nix:: errno:: Errno :: ESRCH ) => {
216- return Ok ( wait :: WaitStatus :: Exited ( Pid :: from_raw ( 0 ) , 0 ) ) ;
218+ return Ok ( WaitStatus :: Exited ( Pid :: from_raw ( 0 ) , 0 ) ) ;
217219 }
218220 Err ( e) => return Err ( Error :: from ( e) ) ,
219221 }
220222
221223 match self . status ( ) {
222- Some ( status) if status != wait :: WaitStatus :: StillAlive => return Ok ( status) ,
224+ Some ( status) if status != WaitStatus :: StillAlive => return Ok ( status) ,
223225 Some ( _) | None => thread:: sleep ( time:: Duration :: from_millis ( 100 ) ) ,
224226 }
225227 // kill -9 if timeout is reached
226228 if let Some ( timeout) = self . kill_timeout {
227229 if start. elapsed ( ) > timeout {
228- signal:: kill ( self . child_pid , signal :: Signal :: SIGKILL ) . map_err ( Error :: from) ?;
230+ signal:: kill ( self . child_pid , Signal :: SIGKILL ) . map_err ( Error :: from) ?;
229231 }
230232 }
231233 }
@@ -234,7 +236,7 @@ impl PtyProcess {
234236
235237impl Drop for PtyProcess {
236238 fn drop ( & mut self ) {
237- if let Some ( wait :: WaitStatus :: StillAlive ) = self . status ( ) {
239+ if let Some ( WaitStatus :: StillAlive ) = self . status ( ) {
238240 self . exit ( ) . expect ( "cannot exit" ) ;
239241 }
240242 }
@@ -243,7 +245,7 @@ impl Drop for PtyProcess {
243245#[ cfg( test) ]
244246mod tests {
245247 use super :: * ;
246- use nix:: sys:: { signal , wait} ;
248+ use nix:: sys:: wait;
247249 use std:: io:: { BufRead , BufReader , LineWriter , Write } ;
248250
249251 #[ test]
@@ -263,7 +265,7 @@ mod tests {
263265 thread:: sleep ( time:: Duration :: from_millis ( 100 ) ) ;
264266 writer. write_all ( & [ 3 ] ) ?; // send ^C
265267 writer. flush ( ) ?;
266- let should = wait :: WaitStatus :: Signaled ( process. child_pid , signal :: Signal :: SIGINT , false ) ;
268+ let should = WaitStatus :: Signaled ( process. child_pid , Signal :: SIGINT , false ) ;
267269 assert_eq ! ( should, wait:: waitpid( process. child_pid, None ) . unwrap( ) ) ;
268270 Ok ( ( ) )
269271 }
0 commit comments