You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.0 KiB
41 lines
1.0 KiB
use std::io::ErrorKind::NotFound;
|
|
use std::process::Command;
|
|
|
|
use crate::builtins::{execute_builtin, is_builtin};
|
|
use crate::error::ShellError;
|
|
use crate::parse::parse_line;
|
|
|
|
pub fn interpret(line: &str) -> Result<(), ShellError> {
|
|
if line.is_empty() {
|
|
return Err(ShellError::EmptyLine);
|
|
}
|
|
let (keyword, args) = parse_line(line);
|
|
|
|
if is_builtin(keyword) {
|
|
execute_builtin(keyword, &args)?;
|
|
} else {
|
|
let mut command = Command::new(keyword);
|
|
command.args(args);
|
|
|
|
execute(command)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn execute(mut command: Command) -> Result<(), ShellError> {
|
|
match command.spawn() {
|
|
Ok(mut child) => {
|
|
if let Err(err) = child.wait() {
|
|
return Err(ShellError::ExecuteFailure(err.to_string()));
|
|
}
|
|
Ok(())
|
|
}
|
|
Err(err) => {
|
|
if err.kind() == NotFound {
|
|
return Err(ShellError::NotFound(command));
|
|
}
|
|
Err(ShellError::ExecuteFailure(err.to_string()))
|
|
}
|
|
}
|
|
}
|