|
@ -1,29 +1,28 @@ |
|
|
use std::{
|
|
|
|
|
|
io::{self},
|
|
|
|
|
|
process::{ChildStdout, Command, Stdio},
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::error::ShellError;
|
|
|
|
|
|
use crate::error::ShellError::Execute;
|
|
|
use crate::parse::parse_line;
|
|
|
use crate::parse::parse_line;
|
|
|
|
|
|
|
|
|
pub fn execute<S: Into<Stdio>>(mut command: Command, stdin: S) -> io::Result<(ChildStdout, Stdio)> {
|
|
|
|
|
|
command.stdin(stdin);
|
|
|
|
|
|
command.stdout(Stdio::piped());
|
|
|
|
|
|
|
|
|
|
|
|
let handle = command.spawn()?;
|
|
|
|
|
|
|
|
|
|
|
|
let stdout = handle.stdout.unwrap();
|
|
|
|
|
|
let stderr = match handle.stderr {
|
|
|
|
|
|
Some(err) => Stdio::from(err),
|
|
|
|
|
|
None => Stdio::null(),
|
|
|
|
|
|
};
|
|
|
|
|
|
Ok((stdout, stderr))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn interpret(line: &str) -> io::Result<()> {
|
|
|
|
|
|
|
|
|
pub fn interpret(line: &str) -> Result<(), ShellError> {
|
|
|
let (keyword, args) = parse_line(line);
|
|
|
let (keyword, args) = parse_line(line);
|
|
|
|
|
|
|
|
|
let mut command = Command::new(keyword);
|
|
|
let mut command = Command::new(keyword);
|
|
|
command.args(args);
|
|
|
command.args(args);
|
|
|
let (mut stdout, _) = execute(command, Stdio::null())?;
|
|
|
|
|
|
io::copy(&mut stdout, &mut io::stdout())?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
execute(command)?;
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn execute(mut command: Command) -> Result<(), ShellError> {
|
|
|
|
|
|
match command.spawn() {
|
|
|
|
|
|
Ok(mut child) => {
|
|
|
|
|
|
if let Err(err) = child.wait() {
|
|
|
|
|
|
return Err(Execute(err.to_string()));
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(err) => Err(Execute(err.to_string())),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|