|
@ -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());
|
|
|
|
|
|
|
|
|
pub fn interpret(line: &str) -> Result<(), ShellError> {
|
|
|
|
|
|
let (keyword, args) = parse_line(line);
|
|
|
|
|
|
|
|
|
|
|
|
let mut command = Command::new(keyword);
|
|
|
|
|
|
command.args(args);
|
|
|
|
|
|
|
|
|
let handle = command.spawn()?;
|
|
|
|
|
|
|
|
|
execute(command)?;
|
|
|
|
|
|
|
|
|
let stdout = handle.stdout.unwrap();
|
|
|
|
|
|
let stderr = match handle.stderr {
|
|
|
|
|
|
Some(err) => Stdio::from(err),
|
|
|
|
|
|
None => Stdio::null(),
|
|
|
|
|
|
};
|
|
|
|
|
|
Ok((stdout, stderr))
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
pub fn interpret(line: &str) -> io::Result<()> {
|
|
|
|
|
|
let (keyword, args) = parse_line(line);
|
|
|
|
|
|
let mut command = Command::new(keyword);
|
|
|
|
|
|
command.args(args);
|
|
|
|
|
|
let (mut stdout, _) = execute(command, Stdio::null())?;
|
|
|
|
|
|
io::copy(&mut stdout, &mut io::stdout())?;
|
|
|
|
|
|
|
|
|
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(())
|
|
|
Ok(())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Err(err) => Err(Execute(err.to_string())),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|