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.

36 lines
898 B

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