From c7e9b19ac1f878ac11cbb647c0ce79ffaee90312 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Fri, 19 Apr 2024 11:18:09 +0200 Subject: [PATCH] builtins : ajout unset --- src/command/builtins.rs | 17 ++++++++++++++--- src/command/command_sequence.rs | 10 +++++++--- src/command/mod.rs | 12 +++++++----- src/control.rs | 4 ++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/command/builtins.rs b/src/command/builtins.rs index e06b35a..522e63a 100644 --- a/src/command/builtins.rs +++ b/src/command/builtins.rs @@ -2,11 +2,12 @@ use crate::error; use crate::error::CdError; use crate::error::IllegalNumberError; use crate::exit_code::ExitCode; +use crate::variables::Variables; use std::env; use std::{env::set_current_dir, path::PathBuf}; -pub(super) fn cd(args: &Vec, exit_code: &mut ExitCode) { +pub(super) fn cd(args: &Vec, _variables: &mut Variables, exit_code: &mut ExitCode) { let path: Option; if !args.is_empty() { @@ -31,11 +32,11 @@ pub(super) fn cd(args: &Vec, exit_code: &mut ExitCode) { } } -pub(super) fn colon(_args: &Vec, exit_code: &mut ExitCode) { +pub(super) fn colon(_args: &Vec, _variables: &mut Variables, exit_code: &mut ExitCode) { exit_code.set_success(); } -pub(crate) fn exit(args: &Vec, exit_code: &mut ExitCode) { +pub(crate) fn exit(args: &Vec, _variables: &mut Variables, exit_code: &mut ExitCode) { let raw_exit_code: Result; if args.is_empty() { @@ -54,3 +55,13 @@ pub(crate) fn exit(args: &Vec, exit_code: &mut ExitCode) { error::print(Box::new(raw_exit_code.unwrap_err())); } } + +pub(super) fn unset(args: &Vec, variables: &mut Variables, exit_code: &mut ExitCode) { + if !args.is_empty() { + for variable_name in args.iter() { + variables.unset(variable_name); + } + } + + exit_code.set_success(); +} diff --git a/src/command/command_sequence.rs b/src/command/command_sequence.rs index 083fbd0..8eab94d 100644 --- a/src/command/command_sequence.rs +++ b/src/command/command_sequence.rs @@ -1,5 +1,6 @@ use super::Command; use crate::exit_code::ExitCode; +use crate::variables::Variables; pub struct CommandSequence { command: Option>, @@ -34,12 +35,15 @@ impl CommandSequence { } impl Command for CommandSequence { - fn spawn(&mut self, exit_code: &mut ExitCode) { + fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode) { if self.command.is_some() { - self.command.as_mut().unwrap().spawn(exit_code); + self.command.as_mut().unwrap().spawn(variables, exit_code); if self.next_command.is_some() { - self.next_command.as_mut().unwrap().spawn(exit_code); + self.next_command + .as_mut() + .unwrap() + .spawn(variables, exit_code); } } } diff --git a/src/command/mod.rs b/src/command/mod.rs index e78cff0..59b60d0 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -5,14 +5,15 @@ pub mod command_sequence; use crate::error; use crate::error::CommandNotFoundError; use crate::exit_code::ExitCode; +use crate::variables::Variables; -type BuiltinFunction = fn(&Vec, &mut ExitCode); +type BuiltinFunction = fn(&Vec, &mut Variables, &mut ExitCode); #[derive(Debug)] pub struct NoSuchBuiltinError; pub trait Command { - fn spawn(&mut self, exit_code: &mut ExitCode); + fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode); } pub struct Builtin { @@ -28,6 +29,7 @@ impl Builtin { let function: Option = match program.as_str() { "cd" => Some(builtins::cd), "exit" => Some(builtins::exit), + "unset" => Some(builtins::unset), ":" => Some(builtins::colon), _ => None, }; @@ -44,8 +46,8 @@ impl Builtin { } impl Command for Builtin { - fn spawn(&mut self, exit_code: &mut ExitCode) { - (self.function)(&self.args, exit_code) + fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode) { + (self.function)(&self.args, variables, exit_code) } } @@ -65,7 +67,7 @@ impl UnixProgram { } impl Command for UnixProgram { - fn spawn(&mut self, exit_code: &mut ExitCode) { + fn spawn(&mut self, _variables: &mut Variables, exit_code: &mut ExitCode) { let handle = self.command.spawn(); if handle.is_ok() { diff --git a/src/control.rs b/src/control.rs index 5254d7f..155adbf 100644 --- a/src/control.rs +++ b/src/control.rs @@ -16,11 +16,11 @@ pub fn run() { let mut command_sequence = parse(user_input.unwrap(), &variables); if !command_sequence.is_empty() { - command_sequence.spawn(&mut current_exit_code); + command_sequence.spawn(&mut variables, &mut current_exit_code); } } else { println!(); - builtins::exit(&vec![], &mut current_exit_code); + builtins::exit(&vec![], &mut variables, &mut current_exit_code); } } }