Skip to content

Commit

Permalink
Add if to reserved words
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 19, 2023
1 parent d53579a commit 52fbd2c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

* [old main branch](https://github.com/shellgei/rusty_bash/tree/old_main)

[![Rust](https://github.com/shellgei/rusty_bash/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/shellgei/rusty_bash/actions/workflows/test.yml)
[![Rust](https://github.com/shellgei/rusty_bash/actions/workflows/test.yml/badge.svg?branch=dev-compounds)](https://github.com/shellgei/rusty_bash/actions/workflows/test.yml)
![](https://img.shields.io/github/license/shellgei/rusty_bash)

## What's this?
Expand Down
3 changes: 3 additions & 0 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ pub mod simple;
pub mod paren;
pub mod brace;
pub mod r#while;
pub mod r#if;

use crate::{ShellCore, Feeder, Script};
use self::simple::SimpleCommand;
use self::paren::ParenCommand;
use self::brace::BraceCommand;
use self::r#while::WhileCommand;
use self::r#if::IfCommand;
use std::fmt;
use std::fmt::Debug;
use super::{io, Pipe};
Expand Down Expand Up @@ -99,5 +101,6 @@ pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Box<dyn Comman
else if let Some(a) = ParenCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = BraceCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = WhileCommand::parse(feeder, core) { Some(Box::new(a)) }
else if let Some(a) = IfCommand::parse(feeder, core) { Some(Box::new(a)) }
else{ None }
}
82 changes: 82 additions & 0 deletions src/elements/command/if.rs
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
}
}
}
2 changes: 1 addition & 1 deletion src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use nix::errno::Errno;

fn reserved(w: &str) -> bool {
match w {
"{" | "}" | "while" | "do" | "done" => true,
"{" | "}" | "while" | "if" | "do" | "done" => true,
_ => false,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Script {
( None, _) => {},
}

let ng_ends = vec![")", "}", "then", "else", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&"];
let ng_ends = vec![")", "}", "then", "else", "if", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&"];
match ( ng_ends.iter().find(|e| feeder.starts_with(e)), nest.1.len() ) {
(Some(end), _) => return Status::UnexpectedSymbol(end.to_string()),
(None, 0) => return Status::NormalEnd,
Expand Down
5 changes: 5 additions & 0 deletions test/test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,9 @@ 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

### IF TEST ###

res=$($com <<< 'if [ "a" == "a" ] ; then echo aa; fi')
[ "$res" = "aa" ] || err $LINENO

echo OK $0

0 comments on commit 52fbd2c

Please sign in to comment.