Skip to content

Commit

Permalink
Merge branch 'sd/202408_0' into sd/202408_1
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 10, 2024
2 parents 64e8204 + 2fc40a1 commit 1768c56
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 37 deletions.
36 changes: 23 additions & 13 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,6 @@ pub trait Command {
fn set_force_fork(&mut self);
}

pub fn eat_blank_with_comment(feeder: &mut Feeder, core: &mut ShellCore, ans_text: &mut String) -> bool {
let blank_len = feeder.scanner_blank(core);
if blank_len == 0 {
return false;
}
*ans_text += &feeder.consume(blank_len);

let comment_len = feeder.scanner_comment();
*ans_text += &feeder.consume(comment_len);
true
}

pub fn eat_inner_script(feeder: &mut Feeder, core: &mut ShellCore,
left: &str, right: Vec<&str>, ans: &mut Option<Script>) -> bool {
if ! feeder.starts_with(left) {
Expand All @@ -85,7 +73,19 @@ pub fn eat_inner_script(feeder: &mut Feeder, core: &mut ShellCore,
! ans.is_none()
}

pub fn eat_redirect(feeder: &mut Feeder, core: &mut ShellCore,
fn eat_blank_with_comment(feeder: &mut Feeder, core: &mut ShellCore, ans_text: &mut String) -> bool {
let blank_len = feeder.scanner_blank(core);
if blank_len == 0 {
return false;
}
*ans_text += &feeder.consume(blank_len);

let comment_len = feeder.scanner_comment();
*ans_text += &feeder.consume(comment_len);
true
}

fn eat_redirect(feeder: &mut Feeder, core: &mut ShellCore,
ans: &mut Vec<Redirect>, ans_text: &mut String) -> bool {
if let Some(r) = Redirect::parse(feeder, core) {
*ans_text += &r.text.clone();
Expand All @@ -96,6 +96,16 @@ pub fn eat_redirect(feeder: &mut Feeder, core: &mut ShellCore,
}
}

pub fn eat_redirects(feeder: &mut Feeder, core: &mut ShellCore,
ans_redirects: &mut Vec<Redirect>, ans_text: &mut String) {
loop {
eat_blank_with_comment(feeder, core, ans_text);
if ! eat_redirect(feeder, core, ans_redirects, ans_text){
break;
}
}
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Box<dyn Command>> {
if let Some(a) = SimpleCommand::parse(feeder, core){ Some(Box::new(a)) }
else if let Some(a) = ParenCommand::parse(feeder, core) { Some(Box::new(a)) }
Expand Down
8 changes: 1 addition & 7 deletions src/elements/command/brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ impl BraceCommand {
ans.text.push_str(&ans.script.as_ref().unwrap().get_text());
ans.text.push_str(&feeder.consume(1));

loop {
command::eat_blank_with_comment(feeder, core, &mut ans.text);
if ! command::eat_redirect(feeder, core, &mut ans.redirects, &mut ans.text){
break;
}
}

command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text);
Some(ans)
}else{
None
Expand Down
8 changes: 1 addition & 7 deletions src/elements/command/paren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ impl ParenCommand {
ans.text.push_str(&ans.script.as_ref().unwrap().get_text());
ans.text.push_str(&feeder.consume(1));

loop {
command::eat_blank_with_comment(feeder, core, &mut ans.text);
if ! command::eat_redirect(feeder, core, &mut ans.redirects, &mut ans.text){
break;
}
}

command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text);
Some(ans)
}else{
None
Expand Down
5 changes: 2 additions & 3 deletions src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ impl SimpleCommand {
feeder.set_backup();

loop {
command::eat_blank_with_comment(feeder, core, &mut ans.text);
if ! command::eat_redirect(feeder, core, &mut ans.redirects, &mut ans.text)
&& ! Self::eat_word(feeder, &mut ans, core) {
command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text);
if ! Self::eat_word(feeder, &mut ans, core) {
break;
}
}
Expand Down
8 changes: 1 addition & 7 deletions src/elements/command/while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ impl WhileCommand {
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);
command::eat_redirects(feeder, core, &mut ans.redirects, &mut ans.text);
Some(ans)
}else{
None
Expand Down
4 changes: 4 additions & 0 deletions test/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,8 @@ res=$($com <<< 'touch /tmp/rusty_bash ; while [ -f /tmp/rusty_bash ] ; do echo w
res=$($com <<< 'rm -f /tmp/rusty_bash ; while [ -f /tmp/rusty_bash ] ; do echo wait ; rm /tmp/rusty_bash ; done')
[ "$res" == "" ] || err $LINENO

res=$($com <<< 'touch /tmp/rusty_bash ; while [ -f /tmp/rusty_bash ] ; do echo wait ; rm /tmp/rusty_bash ; done > /tmp/rusty_bash1'; cat /tmp/rusty_bash1 ; cat /tmp/rusty_bash1 )
[ "$res" == "wait
wait" ] || err $LINENO

echo OK $0

0 comments on commit 1768c56

Please sign in to comment.