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.

31 lines
729 B

  1. use std::process::Command;
  2. use crate::error::ShellError;
  3. use crate::error::ShellError::Execute;
  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(Execute(err.to_string()));
  20. }
  21. Ok(())
  22. }
  23. Err(err) => Err(Execute(err.to_string())),
  24. }
  25. }