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())) } } }