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

  1. use std::io::ErrorKind::NotFound;
  2. use std::process::Command;
  3. use crate::builtins::{execute_builtin, is_builtin};
  4. use crate::error::ShellError;
  5. use crate::parse::parse_line;
  6. pub fn interpret(line: &str) -> Result<(), ShellError> {
  7. if line.is_empty() {
  8. return Err(ShellError::EmptyLine);
  9. }
  10. let (keyword, args) = parse_line(line);
  11. if is_builtin(keyword) {
  12. execute_builtin(keyword, &args)?;
  13. } else {
  14. let mut command = Command::new(keyword);
  15. command.args(args);
  16. execute(command)?;
  17. }
  18. Ok(())
  19. }
  20. fn execute(mut command: Command) -> Result<(), ShellError> {
  21. match command.spawn() {
  22. Ok(mut child) => {
  23. if let Err(err) = child.wait() {
  24. return Err(ShellError::ExecuteFailure(err.to_string()));
  25. }
  26. Ok(())
  27. }
  28. Err(err) => {
  29. if err.kind() == NotFound {
  30. return Err(ShellError::NotFound(command));
  31. }
  32. Err(ShellError::ExecuteFailure(err.to_string()))
  33. }
  34. }
  35. }