-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d53579a
commit 52fbd2c
Showing
6 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//SPDX-FileCopyrightText: 2022 Ryuichi Ueda ryuichiueda@gmail.com | ||
//SPDX-License-Identifier: BSD-3-Clause | ||
|
||
use crate::{ShellCore, Feeder, Script}; | ||
use super::{Command, Pipe, Redirect}; | ||
use crate::elements::command; | ||
use nix::unistd::Pid; | ||
|
||
#[derive(Debug)] | ||
pub struct IfCommand { | ||
pub text: String, | ||
pub while_script: Option<Script>, | ||
pub do_script: Option<Script>, | ||
pub redirects: Vec<Redirect>, | ||
force_fork: bool, | ||
} | ||
|
||
impl Command for IfCommand { | ||
fn exec(&mut self, core: &mut ShellCore, pipe: &mut Pipe) -> Option<Pid> { | ||
if self.force_fork || pipe.is_connected() { | ||
self.fork_exec(core, pipe) | ||
}else{ | ||
self.nofork_exec(core); | ||
None | ||
} | ||
} | ||
|
||
fn run_command(&mut self, core: &mut ShellCore, _: bool) { | ||
loop { | ||
self.while_script.as_mut() | ||
.expect("SUSH INTERNAL ERROR (no script)") | ||
.exec(core); | ||
|
||
if core.vars["?"] != "0" { | ||
break; | ||
} | ||
|
||
self.do_script.as_mut() | ||
.expect("SUSH INTERNAL ERROR (no script)") | ||
.exec(core); | ||
} | ||
} | ||
|
||
fn get_text(&self) -> String { self.text.clone() } | ||
fn get_redirects(&mut self) -> &mut Vec<Redirect> { &mut self.redirects } | ||
fn set_force_fork(&mut self) { self.force_fork = true; } | ||
} | ||
|
||
impl IfCommand { | ||
fn new() -> IfCommand { | ||
IfCommand { | ||
text: String::new(), | ||
while_script: None, | ||
do_script: None, | ||
redirects: vec![], | ||
force_fork: false, | ||
} | ||
} | ||
|
||
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<IfCommand> { | ||
let mut ans = Self::new(); | ||
if command::eat_inner_script(feeder, core, "while", vec!["do"], &mut ans.while_script) | ||
&& command::eat_inner_script(feeder, core, "do", vec!["done"], &mut ans.do_script) { | ||
ans.text.push_str("while"); | ||
ans.text.push_str(&ans.while_script.as_mut().unwrap().get_text()); | ||
ans.text.push_str("do"); | ||
ans.text.push_str(&ans.do_script.as_mut().unwrap().get_text()); | ||
ans.text.push_str(&feeder.consume(4)); //done | ||
|
||
loop { | ||
command::eat_blank_with_comment(feeder, core, &mut ans.text); | ||
if ! command::eat_redirect(feeder, core, &mut ans.redirects, &mut ans.text){ | ||
break; | ||
} | ||
} | ||
//dbg!("{:?}", &ans); | ||
Some(ans) | ||
}else{ | ||
None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters