Skip to content

Commit 4dac953

Browse files
committed
Fix
1 parent 80ecb07 commit 4dac953

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

error

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,4 @@
125125
./test_fixed.bash
126126
./test_fixed.bash
127127
./test_fixed.bash
128+
./test_fixed.bash

src/core/file_descs.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,9 @@ impl FileDescriptors {
100100
}
101101

102102
if unsafe{dup2(from, to)} < 0 {
103-
return Err(ExecError::Other("dup2 error".to_string()));
103+
//return Err(ExecError::Other("dup2 error".to_string()));
104+
return Err(ExecError:: BadFd(from));
104105
}
105-
/*
106-
if let Err(e) = unistd::dup2(from, to) {
107-
return match e {
108-
Errno::EBADF => Err(ExecError::BadFd(to)),
109-
_ => Err(ExecError::Other("dup2 Unknown error".to_string())),
110-
};
111-
}*/
112106

113107
Ok(())
114108
}

src/core/jobtable.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,15 @@ impl JobEntry {
230230
}
231231

232232
impl ShellCore {
233+
fn close_coproc(&mut self, pos: usize) {
234+
self.job_table[pos].coproc_fds
235+
.iter().for_each(|fd| {self.fds.close(*fd);});
236+
237+
let name = self.job_table[pos].coproc_name.clone().unwrap();
238+
let _ = self.db.unset(&name, None);
239+
let _ = self.db.unset(&(name + "_PID"), None);
240+
}
241+
233242
pub fn jobtable_check_status(&mut self) -> Result<(), ExecError> {
234243
if self.is_subshell {
235244
return Ok(());
@@ -244,14 +253,9 @@ impl ShellCore {
244253
}
245254

246255
if table.coproc_name.is_some()
247-
&& (table.display_status == "Done" || table.display_status == "Killed") {
248-
table.coproc_fds
249-
.iter()
250-
.for_each(|fd| {self.fds.close(*fd);});
251-
252-
let name = table.coproc_name.clone().unwrap();
253-
let _ = self.db.unset(&name, None);
254-
let _ = self.db.unset(&(name + "_PID"), None);
256+
&& (table.display_status == "Done"
257+
|| table.display_status == "Killed") {
258+
self.close_coproc(i);
255259
}
256260
}
257261

src/elements/command/coproc.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33

44
use super::{Command, Pipe, Redirect};
55
use crate::elements::command;
6-
use crate::elements::command::{BraceCommand, IfCommand, ParenCommand, WhileCommand};
6+
use crate::elements::command::{
7+
BraceCommand, IfCommand, ParenCommand, SimpleCommand, WhileCommand
8+
};
79
use crate::error::exec::ExecError;
810
use crate::error::parse::ParseError;
911
use crate::utils;
1012
use crate::{Feeder, ShellCore};
1113
use nix::unistd::Pid;
1214
use nix::sys::wait::WaitStatus;
15+
//use std::{thread, time};
1316
use crate::core::jobtable::JobEntry;
1417

1518
#[derive(Debug, Clone, Default)]
@@ -87,6 +90,8 @@ impl Command for Coprocess {
8790
core.job_table.push(entry);
8891
core.tty_fd = backup;
8992

93+
//thread::sleep(time::Duration::from_millis(100));
94+
//core.jobtable_check_status()?;
9095
Ok(None)
9196
}
9297

@@ -162,20 +167,42 @@ impl Coprocess {
162167
Ok(())
163168
}
164169

170+
fn parse_simple_command(feeder: &mut Feeder,
171+
core: &mut ShellCore
172+
) -> Result<Option<Self>, ParseError> {
173+
let mut ans = Self::default();
174+
ans.text += &feeder.consume(6);
175+
command::eat_blank_with_comment(feeder, core, &mut ans.text);
176+
177+
ans.command = if let Some(a) = SimpleCommand::parse(feeder, core)? {
178+
ans.text += &a.get_text();
179+
ans.name = "COPROC".to_string();
180+
Some(a)
181+
}else{
182+
return Err(ParseError::UnexpectedSymbol("coproc".to_string()));
183+
};
184+
185+
Ok(Some(ans))
186+
}
187+
165188
pub fn parse(feeder: &mut Feeder, core: &mut ShellCore) -> Result<Option<Self>, ParseError> {
166189
if ! feeder.starts_with("coproc") {
167190
return Ok(None);
168191
}
169192
let mut ans = Self::default();
170193
ans.lineno = feeder.lineno;
171194

195+
feeder.set_backup();
196+
172197
ans.eat_header(feeder, core)?;
173198
ans.eat_body(feeder, core)?;
174199

175200
if ans.command.is_some() {
201+
feeder.pop_backup();
176202
Ok(Some(ans))
177203
} else {
178-
Ok(None)
204+
feeder.rewind();
205+
Self::parse_simple_command(feeder, core)
179206
}
180207
}
181208
}

src/elements/io/redirect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ impl Redirect {
146146
core.fds.share(right_fd, self.left_fd)
147147
}
148148

149-
fn redirect_input_fd(&mut self, restore: bool, core: &mut ShellCore) -> Result<(), ExecError> {
149+
fn redirect_input_fd(&mut self, restore: bool,
150+
core: &mut ShellCore) -> Result<(), ExecError> {
150151
if self.right.text == "-" {
151152
self.set_left_fd(0);
152153
core.fds.close(self.left_fd);

0 commit comments

Comments
 (0)