From 74fb3573be2473f45387da367b2405435632fc7a Mon Sep 17 00:00:00 2001 From: Ahurac Date: Fri, 19 Apr 2024 10:24:13 +0200 Subject: [PATCH] =?UTF-8?q?exit=5Fcode=20:=20passage=20en=20mode=20r=C3=A9?= =?UTF-8?q?f=C3=A9rence=20mutable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/builtins.rs | 12 +++++------- src/command/command_sequence.rs | 10 +++------- src/command/mod.rs | 24 +++++++++++------------- src/control.rs | 2 +- src/exit_code.rs | 12 ++++++++---- 5 files changed, 28 insertions(+), 32 deletions(-) diff --git a/src/command/builtins.rs b/src/command/builtins.rs index 48dc08d..848daa0 100644 --- a/src/command/builtins.rs +++ b/src/command/builtins.rs @@ -5,7 +5,7 @@ use crate::exit_code::ExitCode; use std::env; use std::{env::set_current_dir, path::PathBuf}; -pub(super) fn cd(args: &Vec) -> ExitCode { +pub(super) fn cd(args: &Vec, exit_code: &mut ExitCode) { let path: Option; if !args.is_empty() { @@ -17,7 +17,7 @@ pub(super) fn cd(args: &Vec) -> ExitCode { }; } - let mut exit_code = ExitCode::success(); + exit_code.set_success(); if path.is_some() { let path = path.unwrap(); let result = set_current_dir(&path); @@ -25,13 +25,11 @@ pub(super) fn cd(args: &Vec) -> ExitCode { if result.is_err() { let error = CdError::new(&args[0]); error::print(Box::new(error)); - exit_code = ExitCode::new(2); + exit_code.set(2); } } - - exit_code } -pub(super) fn colon(_args: &Vec) -> ExitCode { - ExitCode::success() +pub(super) fn colon(_args: &Vec, exit_code: &mut ExitCode) { + exit_code.set_success(); } diff --git a/src/command/command_sequence.rs b/src/command/command_sequence.rs index d8f49a4..083fbd0 100644 --- a/src/command/command_sequence.rs +++ b/src/command/command_sequence.rs @@ -34,17 +34,13 @@ impl CommandSequence { } impl Command for CommandSequence { - fn spawn(&mut self) -> ExitCode { - let mut exit_code = ExitCode::success(); - + fn spawn(&mut self, exit_code: &mut ExitCode) { if self.command.is_some() { - exit_code = self.command.as_mut().unwrap().spawn(); + self.command.as_mut().unwrap().spawn(exit_code); if self.next_command.is_some() { - exit_code = self.next_command.as_mut().unwrap().spawn(); + self.next_command.as_mut().unwrap().spawn(exit_code); } } - - exit_code } } diff --git a/src/command/mod.rs b/src/command/mod.rs index 2d15b19..7e450ed 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -6,13 +6,13 @@ use crate::error; use crate::error::CommandNotFoundError; use crate::exit_code::ExitCode; -type BuiltinFunction = fn(&Vec) -> ExitCode; +type BuiltinFunction = fn(&Vec, &mut ExitCode); #[derive(Debug)] pub struct NoSuchBuiltinError; pub trait Command { - fn spawn(&mut self) -> ExitCode; + fn spawn(&mut self, exit_code: &mut ExitCode); } pub struct Builtin { @@ -43,8 +43,8 @@ impl Builtin { } impl Command for Builtin { - fn spawn(&mut self) -> ExitCode { - (self.function)(&self.args) + fn spawn(&mut self, exit_code: &mut ExitCode) { + (self.function)(&self.args, exit_code) } } @@ -64,27 +64,25 @@ impl UnixProgram { } impl Command for UnixProgram { - fn spawn(&mut self) -> ExitCode { + fn spawn(&mut self, exit_code: &mut ExitCode) { let handle = self.command.spawn(); if handle.is_ok() { - let exit_code = handle + let raw_exit_code = handle .unwrap() .wait() .expect("error waiting for the child") .code() .unwrap_or(1); - let exit_code = match u8::try_from(exit_code) { - Ok(code) => ExitCode::new(code), - Err(_e) => ExitCode::new(u8::MAX), - }; - - exit_code + exit_code.set(match u8::try_from(raw_exit_code) { + Ok(code) => code, + Err(_e) => u8::MAX, + }); } else { let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap()); error::print(Box::new(error)); - ExitCode::not_found() + exit_code.set_command_not_found(); } } } diff --git a/src/control.rs b/src/control.rs index 01cbbba..22107d8 100644 --- a/src/control.rs +++ b/src/control.rs @@ -21,7 +21,7 @@ pub fn run() { let mut command_sequence = parse(user_input.unwrap(), &variables); if !command_sequence.is_empty() { - current_exit_code = command_sequence.spawn(); + command_sequence.spawn(&mut current_exit_code); } } else { println!(); diff --git a/src/exit_code.rs b/src/exit_code.rs index 54f6501..79af0c3 100644 --- a/src/exit_code.rs +++ b/src/exit_code.rs @@ -11,11 +11,15 @@ impl ExitCode { self.exit_code } - pub fn not_found() -> Self { - Self { exit_code: 127 } + pub fn set(&mut self, new_code: u8) { + self.exit_code = new_code; } - pub fn success() -> Self { - Self { exit_code: 0 } + pub fn set_success(&mut self) { + self.exit_code = 0; + } + + pub fn set_command_not_found(&mut self) { + self.exit_code = 127; } }