| 1 | #![cfg(feature = "std")] |
| 2 | |
| 3 | use std::process::Stdio; |
| 4 | |
| 5 | use io_process::{ |
| 6 | command::Command, |
| 7 | coroutines::{ |
| 8 | spawn_then_wait::{SpawnThenWait, SpawnThenWaitResult}, |
| 9 | spawn_then_wait_with_output::{SpawnThenWaitWithOutput, SpawnThenWaitWithOutputResult}, |
| 10 | }, |
| 11 | runtimes::std::handle, |
| 12 | }; |
| 13 | |
| 14 | fn main() { |
| 15 | env_logger::init(); |
| 16 | |
| 17 | let mut command = Command::new("/bin/sh"); |
| 18 | command.arg("-c"); |
| 19 | command.arg("read line; echo $line"); |
| 20 | command.stdin(Stdio::inherit()); |
| 21 | command.stdout(Stdio::piped()); |
| 22 | println!("spawn 1: {command:#?}"); |
| 23 | println!(); |
| 24 | println!("What is your name? "); |
| 25 | |
| 26 | let mut arg = None; |
| 27 | let mut spawn = SpawnThenWait::new(command); |
| 28 | |
| 29 | let (status, stdout) = loop { |
| 30 | match spawn.resume(arg.take()) { |
| 31 | SpawnThenWaitResult::Ok(output) => break (output.status, output.stdout.unwrap()), |
| 32 | SpawnThenWaitResult::Io(io) => arg = Some(handle(io).unwrap()), |
| 33 | SpawnThenWaitResult::Err(err) => panic!("{err}"), |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | println!(); |
| 38 | println!("status: {status:#?}"); |
| 39 | println!(); |
| 40 | |
| 41 | let mut command = Command::new("cat"); |
| 42 | command.arg("-E"); |
| 43 | command.stdin(stdout); |
| 44 | |
| 45 | println!("command 2: {command:#?}"); |
| 46 | println!(); |
| 47 | |
| 48 | let mut arg = None; |
| 49 | let mut spawn = SpawnThenWaitWithOutput::new(command); |
| 50 | |
| 51 | let output = loop { |
| 52 | match spawn.resume(arg) { |
| 53 | SpawnThenWaitWithOutputResult::Ok(output) => break output, |
| 54 | SpawnThenWaitWithOutputResult::Io(io) => arg = Some(handle(io).unwrap()), |
| 55 | SpawnThenWaitWithOutputResult::Err(err) => panic!("{err}"), |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | println!("output: {output:#?}") |
| 60 | } |