Skip to content

Commit f5021b8

Browse files
committed
Merge branch 'sd/202412_4' into sd/202412_5
2 parents e09bfeb + d7fcf09 commit f5021b8

File tree

6 files changed

+64
-80
lines changed

6 files changed

+64
-80
lines changed

src/elements/io/redirect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl Redirect {
114114
Redirect {
115115
text: String::new(),
116116
symbol: String::new(),
117-
right: Word::new(),
117+
right: Word::new(vec![]),
118118
left: String::new(),
119119
left_fd: -1,
120120
left_backup: -1,

src/elements/job.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ pub struct Job {
1616

1717
impl Job {
1818
pub fn exec(&mut self, core: &mut ShellCore, bg: bool) {
19-
let pgid = if core.is_subshell { //17〜21行目を追加
20-
unistd::getpgrp() //自身のPGID
21-
}else{
22-
Pid::from_raw(0)
19+
let pgid = match core.is_subshell {
20+
true => unistd::getpgrp(),
21+
false => Pid::from_raw(0),
2322
};
2423

25-
if bg {
26-
self.exec_bg(core, pgid);
27-
}else{
28-
self.exec_fg(core, pgid);
24+
match bg {
25+
true => self.exec_bg(core, pgid),
26+
false => self.exec_fg(core, pgid),
2927
}
3028
}
3129

@@ -136,6 +134,9 @@ impl Job {
136134
}
137135
}
138136
}
137+
138+
let com_num = feeder.scanner_comment();
139+
ans.text += &feeder.consume(com_num);
139140

140141
if ans.pipelines.len() > 0 {
141142
// dbg!("{:?}", &ans); // デバッグ用にansの内容を出力

src/elements/script.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,26 @@ impl Script {
5252
len != 0
5353
}
5454

55-
fn check_nest(feeder: &mut Feeder, jobnum: usize) -> Status {
55+
fn check_nest(&self, feeder: &mut Feeder) -> Status {
5656
let nest = feeder.nest.last().expect("SUSHI INTERNAL ERROR (empty nest)");
5757

58-
match ( nest.1.iter().find(|e| feeder.starts_with(e)), jobnum ) {
58+
if nest.0 == "" && feeder.len() == 0 {
59+
return Status::NormalEnd;
60+
}
61+
62+
match ( nest.1.iter().find(|e| feeder.starts_with(e)), self.jobs.len() ) {
5963
( Some(end), 0 ) => return Status::UnexpectedSymbol(end.to_string()),
6064
( Some(_), _) => return Status::NormalEnd,
6165
( None, _) => {},
6266
}
6367

64-
let ng_ends = vec!["(", ")", "}", "then", "else", "if", "fi", "elif", "do", "done", "while", "||", "&&", "|", "&", ";"];
65-
match ( ng_ends.iter().find(|e| feeder.starts_with(e)), nest.1.len() ) {
66-
(Some(end), _) => return Status::UnexpectedSymbol(end.to_string()),
67-
(None, 0) => return Status::NormalEnd,
68-
(None, _) => return Status::NeedMoreLine,
68+
if feeder.len() > 0 {
69+
let remaining = feeder.consume(feeder.len());
70+
let first_token = remaining.split(" ").nth(0).unwrap().to_string();
71+
return Status::UnexpectedSymbol(first_token);
6972
}
73+
74+
Status::NeedMoreLine
7075
}
7176

7277
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Script> {
@@ -76,10 +81,8 @@ impl Script {
7681
while Self::eat_job(feeder, core, &mut ans)
7782
&& Self::eat_job_end(feeder, &mut ans) {}
7883

79-
match Self::check_nest(feeder, ans.jobs.len()){
80-
Status::NormalEnd => {
81-
return Some(ans)
82-
},
84+
match ans.check_nest(feeder){
85+
Status::NormalEnd => return Some(ans),
8386
Status::UnexpectedSymbol(s) => {
8487
eprintln!("Unexpected token: {}", s);
8588
core.set_param("?", "2");

src/elements/word.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ impl Word {
3030
Some( Self::make_args(&mut ws) )
3131
}
3232

33-
pub fn make_args(words: &mut Vec<Word>) -> Vec<String> {
33+
fn make_args(words: &mut Vec<Word>) -> Vec<String> {
3434
words.iter_mut()
3535
.map(|w| w.make_unquoted_word())
3636
.filter(|w| *w != None)
3737
.map(|w| w.unwrap())
3838
.collect()
3939
}
4040

41-
pub fn make_unquoted_word(&mut self) -> Option<String> {
41+
fn make_unquoted_word(&mut self) -> Option<String> {
4242
let sw: Vec<Option<String>> = self.subwords.iter_mut()
4343
.map(|s| s.make_unquoted_string()) //""や''はNoneにならずに空文字として残る
4444
.filter(|s| *s != None)
@@ -51,32 +51,27 @@ impl Word {
5151
Some(sw.into_iter().map(|s| s.unwrap()).collect::<String>())
5252
}
5353

54-
pub fn new() -> Word {
54+
pub fn new(subwords: Vec<Box::<dyn Subword>>) -> Word {
5555
Word {
56-
text: String::new(),
57-
subwords: vec![],
56+
text: subwords.iter().map(|s| s.get_text()).collect(),
57+
subwords: subwords,
5858
}
5959
}
6060

61-
fn push(&mut self, subword: &Box<dyn Subword>) {
62-
self.text += &subword.get_text().to_string();
63-
self.subwords.push(subword.clone());
64-
}
65-
6661
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Option<Word> {
6762
if feeder.starts_with("#") {
6863
return None;
6964
}
7065

71-
let mut ans = Word::new();
66+
let mut subwords = vec![];
7267
while let Some(sw) = subword::parse(feeder, core) {
73-
ans.push(&sw);
68+
subwords.push(sw);
7469
}
7570

76-
if ans.text.len() == 0 {
77-
None
78-
}else{
79-
Some(ans)
71+
let ans = Word::new(subwords);
72+
match ans.text.len() {
73+
0 => None,
74+
_ => Some(ans),
8075
}
8176
}
8277
}

src/elements/word/brace_expansion.rs

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//SPDX-License-Identifier: BSD-3-Clause
33

44
use crate::elements::subword::Subword;
5-
use crate::elements::word::Word;
5+
use super::Word;
66

77
fn after_dollar(s: &str) -> bool {
88
s == "$" || s == "$$"
@@ -14,21 +14,19 @@ pub fn eval(word: &mut Word) -> Vec<Word> {
1414
let mut skip_until = 0;
1515
for i in open_brace_pos(word) {
1616
if i < skip_until { //ブレース展開の終わりまで処理をスキップ
17-
continue;
17+
continue;
1818
}
1919

20-
if let Some(d) = parse(&word.subwords[i..]) {
21-
let shift_d: Vec<usize> = d.iter().map(|e| e+i).collect();
22-
23-
if i > 0 && after_dollar(word.subwords[i-1].get_text()) {
24-
skip_until = *shift_d.last().unwrap();
25-
continue;
26-
}
20+
let d = parse(&word.subwords[i..], i);
21+
if d.len() <= 2 {
22+
continue;
23+
}
2724

28-
return expand(&word.subwords, &shift_d);
25+
match i > 0 && after_dollar(word.subwords[i-1].get_text()) {
26+
true => skip_until = *d.last().unwrap(),
27+
false => return expand(&word.subwords, &d),
2928
}
3029
}
31-
3230
vec![word.clone()]
3331
}
3432

@@ -52,56 +50,43 @@ fn open_brace_pos(w: &Word) -> Vec<usize> {
5250
.collect()
5351
}
5452

55-
pub fn parse(subwords: &[Box<dyn Subword>]) -> Option<Vec<usize>> {
53+
fn parse(subwords: &[Box<dyn Subword>], start: usize) -> Vec<usize> {
5654
let mut stack = vec![];
5755
for sw in subwords {
58-
stack.push(Some(sw.get_text()));
56+
stack.push(sw.get_text());
5957
if sw.get_text() == "}" {
60-
match get_delimiters(&mut stack) {
61-
Some(ds) => return Some(ds),
62-
_ => {},
58+
let ds = get_delimiters(&mut stack, start);
59+
if ds.len() > 2 {
60+
return ds;
6361
}
6462
}
6563
}
66-
None
64+
vec![]
6765
}
6866

69-
fn get_delimiters(stack: &mut Vec<Option<&str>>) -> Option<Vec<usize>> {
70-
let mut comma_pos = vec![];
67+
fn get_delimiters(stack: &mut Vec<&str>, start: usize) -> Vec<usize> {
68+
let mut delimiter_pos = vec![start, stack.len()-1+start];
7169
for i in (1..stack.len()-1).rev() {
72-
if stack[i] == Some(",") {
73-
comma_pos.push(i);
74-
}else if stack[i] == Some("{") { // find an inner brace expcomma_posion
75-
stack[i..].iter_mut().for_each(|e| *e = None);
76-
return None;
70+
if stack[i] == "," {
71+
delimiter_pos.insert(1, start+i);
72+
}else if stack[i] == "{" { // find an inner brace expcomma_posion
73+
stack[i..].iter_mut().for_each(|e| *e = "");
74+
return vec![];
7775
}
7876
}
79-
80-
if comma_pos.len() > 0 {
81-
comma_pos.reverse();
82-
comma_pos.insert(0, 0); // add "{" pos
83-
comma_pos.push(stack.len()-1); // add "}" pos
84-
Some(comma_pos)
85-
}else{
86-
None
87-
}
77+
delimiter_pos
8878
}
8979

90-
pub fn expand(subwords: &Vec<Box<dyn Subword>>, delimiters: &Vec<usize>) -> Vec<Word> {
80+
fn expand(subwords: &Vec<Box<dyn Subword>>, delimiters: &Vec<usize>) -> Vec<Word> {
9181
let left = &subwords[..delimiters[0]];
9282
let mut right = subwords[(delimiters.last().unwrap()+1)..].to_vec();
9383
invalidate_brace(&mut right);
9484

9585
let mut ans = vec![];
96-
let mut from = delimiters[0] + 1;
97-
for to in &delimiters[1..] {
98-
let mut w = Word::new();
99-
w.subwords.extend(left.to_vec());
100-
w.subwords.extend(subwords[from..*to].to_vec());
101-
w.subwords.extend(right.to_vec());
102-
w.text = w.subwords.iter().map(|s| s.get_text()).collect();
86+
for i in 0..(delimiters.len()-1) {
87+
let center = &subwords[ (delimiters[i]+1)..delimiters[i+1] ];
88+
let mut w = Word::new([ left, &center, &right ].concat() );
10389
ans.append(&mut eval(&mut w));
104-
from = *to + 1;
10590
}
10691
ans
10792
}

src/feeder/scanner.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ impl Feeder {
125125
}
126126

127127
pub fn scanner_name(&mut self, core: &mut ShellCore) -> usize {
128-
let c = self.remaining.chars().nth(0).unwrap_or('0');
129-
if '0' <= c && c <= '9' {
128+
let head = self.remaining.chars().nth(0).unwrap_or('0');
129+
if '0' <= head && head <= '9' {
130130
return 0;
131131
}
132132

0 commit comments

Comments
 (0)