Skip to content

Commit

Permalink
Add return type to Script
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 18, 2025
1 parent 78d72b2 commit 6a0be4c
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn eval(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {

core.eval_level += 1;
match Script::parse(&mut feeder, core, false){
Ok(Some(mut s)) => s.exec(core),
Ok(Some(mut s)) => {let _ = s.exec(core); },
Err(e) => parse::print_error(e, core),
_ => {},
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/builtins/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn source(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
}

match Script::parse(&mut feeder, core, false){
Ok(Some(mut s)) => s.exec(core),
Ok(Some(mut s)) => {let _ = s.exec(core); },
Err(e) => parse::print_error(e, core),
_ => {},
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/command/brace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct BraceCommand {
impl Command for BraceCommand {
fn run(&mut self, core: &mut ShellCore, _: bool) {
match self.script {
Some(ref mut s) => s.exec(core),
Some(ref mut s) => {let _ = s.exec(core); },
_ => exit::internal(" (ParenCommand::exec)"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/command/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Command for CaseCommand {
};

if glob::parse_and_compare(&w, &p, extglob) || next {
e.1.exec(core);
let _ = e.1.exec(core);

if e.2 == ";;" {
return;
Expand Down
7 changes: 4 additions & 3 deletions src/elements/command/for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use crate::{ShellCore, Feeder, Script};
use crate::error::exec;
use crate::error::parse::ParseError;
use super::{Command, Redirect};
use crate::elements::command;
Expand Down Expand Up @@ -56,7 +57,7 @@ impl ForCommand {
match w.eval(core) {
Ok(mut ws) => ans.append(&mut ws),
Err(e) => {
error::print(&e, core);
exec::print_error(e, core);
return None;
},
}
Expand Down Expand Up @@ -90,7 +91,7 @@ impl ForCommand {
continue;
}

self.do_script.as_mut().unwrap().exec(core);
let _ = self.do_script.as_mut().unwrap().exec(core);

if core.break_counter > 0 {
core.break_counter -= 1;
Expand Down Expand Up @@ -127,7 +128,7 @@ impl ForCommand {
return ok;
}

self.do_script.as_mut().unwrap().exec(core);
let _ = self.do_script.as_mut().unwrap().exec(core);

if core.break_counter > 0 {
core.break_counter -= 1;
Expand Down
6 changes: 3 additions & 3 deletions src/elements/command/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ pub struct IfCommand {
impl Command for IfCommand {
fn run(&mut self, core: &mut ShellCore, _: bool) {
for i in 0..self.if_elif_scripts.len() {
self.if_elif_scripts[i].exec(core);
let _ = self.if_elif_scripts[i].exec(core);
if core.db.exit_status == 0 {
self.then_scripts[i].exec(core);
let _ = self.then_scripts[i].exec(core);
return;
}
}

match self.else_script.as_mut() {
Some(s) => s.exec(core),
Some(s) => {let _ = s.exec(core); },
_ => {},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/elements/command/paren.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Command for ParenCommand {
}

match self.script {
Some(ref mut s) => s.exec(core),
Some(ref mut s) => {let _ = s.exec(core); },
_ => exit::internal(" (ParenCommand::exec)"),
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/elements/command/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

pub mod parser;

use crate::{error, proc_ctrl, ShellCore};
use crate::{proc_ctrl, ShellCore};
use crate::error::exec;
use crate::error::exec::ExecError;
use crate::utils::exit;
use super::{Command, Pipe, Redirect};
Expand Down Expand Up @@ -35,7 +36,7 @@ impl Command for SimpleCommand {

self.args.clear();
let mut words = self.words.to_vec();
if ! words.iter_mut().all(|w| self.set_arg(w, core)){
if ! words.iter_mut().all(|w| self.set_arg(w, core).is_ok()){
core.word_eval_error = true;
return None;
}
Expand Down Expand Up @@ -133,18 +134,18 @@ impl SimpleCommand {
Ok(())
}

fn set_arg(&mut self, word: &mut Word, core: &mut ShellCore) -> bool {
fn set_arg(&mut self, word: &mut Word, core: &mut ShellCore) -> Result<(), ExecError> {
match word.eval(core) {
Ok(ws) => {
self.args.extend(ws);
true
Ok(())
},
Err(e) => {
error::print(&e, core);
exec::print_error(e.clone(), core);
if ! core.sigint.load(Relaxed) {
core.db.exit_status = 1;
}
false
Err(e)
},
}
}
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 @@ -20,7 +20,7 @@ impl Command for WhileCommand {
core.loop_level += 1;
loop {
core.suspend_e_option = true;
self.while_script.as_mut().unwrap().exec(core);
let _ = self.while_script.as_mut().unwrap().exec(core);

core.suspend_e_option = false;
if core.db.exit_status != 0 {
Expand All @@ -33,7 +33,7 @@ impl Command for WhileCommand {
continue;
}

self.do_script.as_mut().unwrap().exec(core);
let _ = self.do_script.as_mut().unwrap().exec(core);

if core.break_counter > 0 {
core.break_counter -= 1;
Expand Down
4 changes: 2 additions & 2 deletions src/elements/io/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::fs::{File, OpenOptions};
use std::os::fd::{IntoRawFd, RawFd};
use std::io::Error;
use crate::error;
use crate::error::exec;
use crate::elements::io;
use crate::elements::word::Word;
use crate::{Feeder, ShellCore};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Redirect {
let args = match self.right.eval(core) {
Ok(v) => v,
Err(e) => {
error::print(&e, core);
exec::print_error(e, core);
return false;
},
};
Expand Down
16 changes: 9 additions & 7 deletions src/elements/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::pipeline::Pipeline;
use crate::{proc_ctrl, Feeder, ShellCore};
use crate::core::jobtable::JobEntry;
use crate::utils::exit;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use nix::sys::wait::WaitStatus;
use nix::unistd;
Expand All @@ -18,26 +19,26 @@ pub struct Job {
}

impl Job {
pub fn exec(&mut self, core: &mut ShellCore, bg: bool) {
pub fn exec(&mut self, core: &mut ShellCore, bg: bool) -> Result<(), ExecError> {
let pgid = match core.is_subshell {
true => unistd::getpgrp(),
false => Pid::from_raw(0),
};

match bg {
true => self.exec_bg(core, pgid),
false => self.exec_fg(core, pgid),
false => self.exec_fg(core, pgid)?,
}
Ok(())
}

fn exec_fg(&mut self, core: &mut ShellCore, pgid: Pid) {
fn exec_fg(&mut self, core: &mut ShellCore, pgid: Pid) -> Result<(), ExecError> {
let mut do_next = true;
let susp_e_option = core.suspend_e_option;
for (pipeline, end) in self.pipelines.iter_mut().zip(self.pipeline_ends.iter()) {
/*
if core.word_eval_error {
return;
}*/
return Err(ExecError::Other("word evaluation error".to_string()));
}

core.suspend_e_option = susp_e_option || end == "&&" || end == "||";

Expand All @@ -50,6 +51,7 @@ impl Job {
}
do_next = (core.db.exit_status == 0) == (end == "&&");
}
Ok(())
}

fn check_stop(core: &mut ShellCore, text: &str,
Expand Down Expand Up @@ -98,7 +100,7 @@ impl Job {
match unsafe{unistd::fork()} {
Ok(ForkResult::Child) => {
core.initialize_as_subshell(Pid::from_raw(0), pgid);
self.exec(core, false);
let _ = self.exec(core, false);
exit::normal(core)
},
Ok(ForkResult::Parent { child } ) => {
Expand Down
9 changes: 6 additions & 3 deletions src/elements/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//SPDX-License-Identifier: BSD-3-Clause

use super::job::Job;
use crate::error::exec::ExecError;
use crate::error::parse::ParseError;
use crate::{Feeder, ShellCore};

Expand All @@ -19,13 +20,15 @@ pub struct Script {
}

impl Script {
pub fn exec(&mut self, core: &mut ShellCore) {
pub fn exec(&mut self, core: &mut ShellCore) -> Result<(), ExecError> {
for (job, end) in self.jobs.iter_mut().zip(self.job_ends.iter()) {
/*
if core.word_eval_error {
return;
}
job.exec(core, end == "&");
}*/
job.exec(core, end == "&")?;
}
Ok(())
}

pub fn get_text(&self) -> String { self.text.clone() }
Expand Down
2 changes: 1 addition & 1 deletion src/elements/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<Vec<Box::<dyn Subword>>> for Word {
}

impl Word {
pub fn eval(&mut self, core: &mut ShellCore) -> Result<Vec<String>, String> {
pub fn eval(&mut self, core: &mut ShellCore) -> Result<Vec<String>, ExecError> {
let ws_after_brace_exp = match core.db.flags.contains('B') {
true => brace_expansion::eval(&mut self.clone()),
false => vec![self.clone()],
Expand Down
2 changes: 1 addition & 1 deletion src/error/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::ShellCore;
use crate::error::parse::ParseError;

#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum ExecError {
Internal,
ArrayIndexInvalid(String),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn main_loop(core: &mut ShellCore) {
core.sigint.store(false, Relaxed);
match Script::parse(&mut feeder, core, false){
Ok(Some(mut s)) => {
s.exec(core);
let _ = s.exec(core);
set_history(core, &s.get_text());
},
Err(e) => {
Expand Down Expand Up @@ -192,7 +192,7 @@ fn run_and_exit_c_option(args: &Vec<String>, c_parts: &Vec<String>) {

let mut feeder = Feeder::new(&c_parts[1]);
match Script::parse(&mut feeder, &mut core, false){
Ok(Some(mut s)) => s.exec(&mut core),
Ok(Some(mut s)) => {let _ = s.exec(&mut core);},
Err(e) => parse::print_error(e, &mut core),
_ => {},
}
Expand Down
2 changes: 1 addition & 1 deletion src/proc_ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn run_command_not_found(arg: &String, core: &mut ShellCore) -> ! {
let s = "command_not_found_handle ".to_owned() + &arg.clone();
let mut f = Feeder::new(&s);
match Script::parse(&mut f, core, false) {
Ok(Some(mut script)) => script.exec(core),
Ok(Some(mut script)) => {let _ = script.exec(core);},
Err(e) => parse::print_error(e, core),
_ => {},
}
Expand Down
4 changes: 2 additions & 2 deletions test/ok
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
./test_script.bash
./test_options.bash
./test_redirects.bash
./test_glob.bash
./test_redirects.bash
./test_brace.bash
./test_builtins.bash
./test_others.bash
./test_calculation.bash
./test_parameters.bash
./test_compound.bash
./test_parameters.bash
./test_job.bash

0 comments on commit 6a0be4c

Please sign in to comment.