fn forward_stdin_if_piped(child: &mut Child) -> Option<RawTerminal<Stdout>> {
    let mut child_stdin = child.stdin.take()?;
    let child_id = child.id();
    spawn(move || {
        let our_stdin = stdin();
        let mut our_stdin = our_stdin.lock();
        loop {
            let buffer = our_stdin.fill_buf().expect("Failed to read stdin.");
            if buffer.is_empty() {
                // Our stdin was closed. We interpret this as a signal to exit,
                // because pressing Ctrl+C to trigger an exit is no longer
                // possible.
                break;
            }
            // In raw mode, pressing Ctrl+C will send a '3' byte to stdin ("end
            // of message" ASCII value) instead of sending SIGINT. Identify that
            // case, and exit if it occurs.
            if buffer.contains(&3) {
                break;
            }
            match child_stdin.write(buffer) {
                // A BrokenPipe error occurs when the child has exited. Exit
                // without sending SIGINT.
                Err(error) if error.kind() == ErrorKind::BrokenPipe => return,

                Err(error) => panic!("Failed to forward stdin: {}", error),
                Ok(bytes) => our_stdin.consume(bytes),
            }
        }
        // Send SIGINT to the child, telling it to exit. After the child exits,
        // the main loop will detect the exit and we will shut down cleanly.
        //
        // Safety: Sending SIGINT to a process is a safe operation -- kill is
        // marked unsafe because it is a FFI function.
        unsafe {
            kill(child_id as pid_t, SIGINT);
        }
    });
    Some(
        stdout()
            .into_raw_mode()
            .expect("Failed to set terminal to raw mode."),
    )
}
// If child's stdin is piped, this sets the terminal to raw mode and spawns a
// thread that forwards our stdin to child's stdin. The thread sends SIGINT to
// the child if Ctrl+C is pressed. Returns a RawTerminal, which reverts the
// terminal to its previous configuration on drop.
If child's stdin is piped, this sets the terminal to raw mode and spawns a
thread that forwards our stdin to child's stdin. The thread sends SIGINT to
the child if Ctrl+C is pressed. Returns a RawTerminal, which reverts the
terminal to its previous configuration on drop.
If child ' s stdin is piped this sets the terminal to raw mode and spawns a thread that forwards our stdin to child ' s stdin . The thread sends SIGINT to the child if Ctrl + C is pressed . Returns a RawTerminal which reverts the terminal to its previous configuration on drop .
fn forward_stdin_if_piped(child: &mut Child) -> Option<RawTerminal<Stdout>> {
    let mut child_stdin = child.stdin.take()?;
    let child_id = child.id();
    spawn(move || {
        let our_stdin = stdin();
        let mut our_stdin = our_stdin.lock();
        loop {
            let buffer = our_stdin.fill_buf().expect("Failed to read stdin.");
            if buffer.is_empty() {
                // Our stdin was closed. We interpret this as a signal to exit,
                // because pressing Ctrl+C to trigger an exit is no longer
                // possible.
                break;
            }
            // In raw mode, pressing Ctrl+C will send a '3' byte to stdin ("end
            // of message" ASCII value) instead of sending SIGINT. Identify that
            // case, and exit if it occurs.
            if buffer.contains(&3) {
                break;
            }
            match child_stdin.write(buffer) {
                // A BrokenPipe error occurs when the child has exited. Exit
                // without sending SIGINT.
                Err(error) if error.kind() == ErrorKind::BrokenPipe => return,
                Err(error) => panic!("Failed to forward stdin: {}", error),
                Ok(bytes) => our_stdin.consume(bytes),
            }
        }
        // Send SIGINT to the child, telling it to exit. After the child exits,
        // the main loop will detect the exit and we will shut down cleanly.
        //
        // Safety: Sending SIGINT to a process is a safe operation -- kill is
        // marked unsafe because it is a FFI function.
        unsafe {
            kill(child_id as pid_t, SIGINT);
        }
    });
    Some(
        stdout()
            .into_raw_mode()
            .expect("Failed to set terminal to raw mode."),
    )
}
fn forward_stdin_if_piped ( child : & mut Child ) -> Option < RawTerminal < Stdout > > { let mut child_stdin = child . stdin . take ( ) ? ; let child_id = child . id ( ) ; spawn ( move | | { let our_stdin = stdin ( ) ; let mut our_stdin = our_stdin . lock ( ) ; loop { let buffer = our_stdin . fill_buf ( ) . expect ( " " ) ; if buffer . is_empty ( ) { // Our stdin was closed. We interpret this as a signal to exit, // because pressing Ctrl+C to trigger an exit is no longer // possible. break ; } // In raw mode, pressing Ctrl+C will send a '3' byte to stdin ("end // of message" ASCII value) instead of sending SIGINT. Identify that // case, and exit if it occurs. if buffer . contains ( & 3 ) { break ; } match child_stdin . write ( buffer ) { // A BrokenPipe error occurs when the child has exited. Exit // without sending SIGINT. Err ( error ) if error . kind ( ) == ErrorKind :: BrokenPipe => return , Err ( error ) => panic ! ( " " error ) , Ok ( bytes ) => our_stdin . consume ( bytes ) , } } // Send SIGINT to the child, telling it to exit. After the child exits, // the main loop will detect the exit and we will shut down cleanly. // // Safety: Sending SIGINT to a process is a safe operation -- kill is // marked unsafe because it is a FFI function. unsafe { kill ( child_id as pid_t , SIGINT ) ; } } ) ; Some ( stdout ( ) . into_raw_mode ( ) . expect ( " " ) , ) }
If child's stdin is piped, this sets the terminal to raw mode and spawns a
thread that forwards our stdin to child's stdin.
If child ' s stdin is piped this sets the terminal to raw mode and spawns a thread that forwards our stdin to child ' s stdin .
child
&mut Child
child
&mut Child