Skip to content

Commit

Permalink
Fix redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Feb 2, 2024
1 parent b2130ed commit 036de1d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
40 changes: 23 additions & 17 deletions src/elements/io/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use std::fs::{File, OpenOptions};
use std::os::fd::{IntoRawFd, RawFd};
use std::io::Error;
use crate::elements::io;
use crate::elements::word::Word;
use crate::{Feeder, ShellCore};

#[derive(Debug, Clone)]
pub struct Redirect {
pub text: String,
pub symbol: String,
pub right: String,
pub right: Word,
pub left: String,
left_fd: RawFd,
left_backup: RawFd,
Expand All @@ -20,6 +21,11 @@ pub struct Redirect {

impl Redirect {
pub fn connect(&mut self, restore: bool) -> bool {
if self.right.eval().len() != 1 {
eprintln!("sush: {}: ambiguous redirect", self.right.text);
return false;
}

match self.symbol.as_str() {
"<" => self.redirect_simple_input(restore),
">" => self.redirect_simple_output(restore),
Expand Down Expand Up @@ -53,31 +59,31 @@ impl Redirect {
result
},
_ => {
eprintln!("sush: {}: {}", &self.right, Error::last_os_error().kind());
eprintln!("sush: {}: {}", &self.right.text, Error::last_os_error().kind());
false
},
}
}

fn redirect_simple_input(&mut self, restore: bool) -> bool {
self.set_left_fd(0);
self.connect_to_file(File::open(&self.right), restore)
self.connect_to_file(File::open(&self.right.text), restore)
}

fn redirect_simple_output(&mut self, restore: bool) -> bool {
self.set_left_fd(1);
self.connect_to_file(File::create(&self.right), restore)
self.connect_to_file(File::create(&self.right.text), restore)
}

fn redirect_append(&mut self, restore: bool) -> bool {
self.set_left_fd(1);
self.connect_to_file(OpenOptions::new().create(true)
.write(true).append(true).open(&self.right), restore)
.write(true).append(true).open(&self.right.text), restore)
}

fn redirect_both_output(&mut self, restore: bool) -> bool {
self.left_fd = 1;
if ! self.connect_to_file(File::create(&self.right), restore){
if ! self.connect_to_file(File::create(&self.right.text), restore){
return false;
}

Expand All @@ -101,7 +107,7 @@ impl Redirect {
Redirect {
text: String::new(),
symbol: String::new(),
right: String::new(),
right: Word::new(),
left: String::new(),
left_fd: -1,
left_backup: -1,
Expand All @@ -124,14 +130,14 @@ impl Redirect {
let blank_len = feeder.scanner_blank(core);
ans.text += &feeder.consume(blank_len);

match feeder.scanner_word(core) {
0 => false,
n => {
ans.right = feeder.consume(n);
ans.text += &ans.right.clone();
true
},
}
let w = match Word::parse(feeder, core) {
Some(w) => w,
_ => return false,
};

ans.text += &w.text.clone();
ans.right = w;
true
}

fn eat_left(feeder: &mut Feeder, ans: &mut Self, core: &mut ShellCore) -> bool {
Expand All @@ -153,8 +159,8 @@ impl Redirect {
let mut ans = Self::new();
feeder.set_backup(); //追加

if Self::eat_left(feeder, &mut ans, core) && //追加
Self::eat_symbol(feeder, &mut ans, core) && //ifを除去
if Self::eat_left(feeder, &mut ans, core) &&
Self::eat_symbol(feeder, &mut ans, core) &&
Self::eat_right(feeder, &mut ans, core) {
feeder.pop_backup();
Some(ans)
Expand Down
2 changes: 1 addition & 1 deletion src/elements/subword/unquoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{ShellCore, Feeder};

#[derive(Debug)]
#[derive(Debug,Clone)]
pub struct UnquotedSubword {
pub text: String,
}
Expand Down
6 changes: 5 additions & 1 deletion src/elements/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{Feeder, ShellCore};
use crate::elements::subword::unquoted::UnquotedSubword;

#[derive(Debug)]
#[derive(Debug,Clone)]
pub struct Word {
pub text: String,
pub subwords: Vec<UnquotedSubword>,
Expand All @@ -18,6 +18,10 @@ impl Word {
}
}

pub fn eval(&mut self) -> Vec<Word> {
vec![self.clone()]
}

pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Word> {
let mut ans = Word::new();
while let Some(sw) = UnquotedSubword::parse(feeder, core) {
Expand Down

0 comments on commit 036de1d

Please sign in to comment.