diff options
Diffstat (limited to 'src/run')
| -rw-r--r-- | src/run/mod.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/run/mod.rs b/src/run/mod.rs index 459c898..106e22c 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -319,6 +319,7 @@ impl Executor { Ast::FunDecl(fd) => self.execute_fun_decl(fd), Ast::VarAssign(va) => self.execute_var_assign(va), Ast::Pipes(pipes) => self.execute_pipeline(pipes, stdin, stdout), + Ast::If(cond) => self.execute_if(cond, stdin, stdout), } } @@ -345,6 +346,31 @@ impl Executor { let cmd = this.execute_pipeline(parse::Pipes { cmds: vec![cmd] }, stdin, stdout); this.exec_loop(cmd, &mut [c1, c2]) } + + fn execute_if( + &mut self, + cond: parse::If<PostExpansion>, + stdin: InputReader, + stdout: OutputWriter, + ) -> SpawnedCmd { + let mut this = self.clone(); + let handle = wait::spawn(move || -> Result<(), ExecError> { + let res = this + .execute_pipeline(cond.condition, stdin.try_clone()?, stdout.try_clone()?) + .join(); + let block = match res { + Ok(_) | Err(ExecError::ExecError(0)) => cond.true_block, + Err(_) => cond.false_block, + }; + for cmd in block.commands { + let cmd = cmd.expand(&mut this)?; + this.execute(cmd, stdin.try_clone()?, stdout.try_clone()?) + .join()?; + } + Ok(()) + }); + SpawnedCmd::Fun(handle) + } } impl parse::Expander for Executor { @@ -556,7 +582,7 @@ pub trait Builtin: Send + Sync { ) -> BuiltinResult; } -pub trait BuiltinClone : Builtin { +pub trait BuiltinClone: Builtin { fn clone_box(&self) -> Box<dyn Builtin>; } |
