Skip to content

Commit

Permalink
Change to use ExecError
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 18, 2025
1 parent 1228cf4 commit ad18a08
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/elements/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod r#while;
pub mod r#if;

use crate::{ShellCore, Feeder, Script};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::utils::exit;
Expand Down Expand Up @@ -43,7 +44,9 @@ pub trait Command {
Ok(ForkResult::Child) => {
core.initialize_as_subshell(Pid::from_raw(0), pipe.pgid);
io::connect(pipe, self.get_redirects(), core);
self.run(core, true);
if let Err(e) = self.run(core, true) {
exec::print_error(e, core);
}
exit::normal(core)
},
Ok(ForkResult::Parent { child } ) => {
Expand All @@ -56,12 +59,15 @@ pub trait Command {
}

fn nofork_exec(&mut self, core: &mut ShellCore) -> Result<Option<Pid>, ExecError> {
let mut result = Ok(());
if self.get_redirects().iter_mut().all(|r| r.connect(true, core)){
self.run(core, false);
result = self.run(core, false);
}else{
core.db.set_param("?", "1").unwrap();
}
self.get_redirects().iter_mut().rev().for_each(|r| r.restore());

result?;
Ok(None)
}

Expand Down
2 changes: 1 addition & 1 deletion src/elements/command/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct IfCommand {
impl Command for IfCommand {
fn run(&mut self, core: &mut ShellCore, _: bool) -> Result<(), ExecError> {
for i in 0..self.if_elif_scripts.len() {
self.if_elif_scripts[i].exec(core);
self.if_elif_scripts[i].exec(core)?;
if core.db.get_param("?").unwrap() == "0" {
self.then_scripts[i].exec(core)?;
return Ok(());
Expand Down
4 changes: 2 additions & 2 deletions src/elements/command/while.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ impl Command for WhileCommand {
loop {
self.while_script.as_mut()
.expect("SUSH INTERNAL ERROR (no script)")
.exec(core);
.exec(core)?;

if core.db.get_param("?").unwrap() != "0" {
break;
}

self.do_script.as_mut()
.expect("SUSH INTERNAL ERROR (no script)")
.exec(core);
.exec(core)?;
}
Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion src/elements/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use super::pipeline::Pipeline;
use crate::{Feeder, ShellCore};
use crate::core::jobtable::JobEntry;
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::utils::exit;
Expand Down Expand Up @@ -71,7 +72,9 @@ impl Job {
match unsafe{unistd::fork()} {
Ok(ForkResult::Child) => {
core.initialize_as_subshell(Pid::from_raw(0), pgid);
self.exec(core, false);
if let Err(e) = self.exec(core, false) {
exec::print_error(e, core);
}
exit::normal(core)
},
Ok(ForkResult::Parent { child } ) => {
Expand Down
2 changes: 1 addition & 1 deletion src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Script {
impl Script {
pub fn exec(&mut self, core: &mut ShellCore) -> Result<(), ExecError> {
for (job, end) in self.jobs.iter_mut().zip(self.job_ends.iter()) {
job.exec(core, end == "&");
job.exec(core, end == "&")?;
}
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod exec;
pub mod input;
pub mod parse;

/*
use crate::ShellCore;
use nix::sys::signal::Signal;
use nix::unistd::Pid;
Expand Down Expand Up @@ -35,3 +36,4 @@ pub fn signaled(pid: Pid, signal: Signal, coredump: bool) -> i32 {
}
128+signal as i32
}
*/
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::{env, process};
use crate::core::ShellCore;
use crate::utils::exit;
use crate::elements::script::Script;
use crate::error::exec;
use crate::error::input::InputError;
use crate::feeder::Feeder;
use utils::file_check;
Expand Down Expand Up @@ -54,7 +55,9 @@ fn main_loop(core: &mut ShellCore) {
}

if let Ok(Some(mut s)) = Script::parse(&mut feeder, core){
s.exec(core);
if let Err(e) = s.exec(core) {
exec::print_error(e, core);
}
}
core.sigint.store(false, Relaxed);
}
Expand Down

0 comments on commit ad18a08

Please sign in to comment.