builtins : ajout unset

This commit is contained in:
Ahurac 2024-04-19 11:18:09 +02:00
parent 5fca142fea
commit c7e9b19ac1
4 changed files with 30 additions and 13 deletions

View file

@ -2,11 +2,12 @@ use crate::error;
use crate::error::CdError; use crate::error::CdError;
use crate::error::IllegalNumberError; use crate::error::IllegalNumberError;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
use crate::variables::Variables;
use std::env; use std::env;
use std::{env::set_current_dir, path::PathBuf}; use std::{env::set_current_dir, path::PathBuf};
pub(super) fn cd(args: &Vec<String>, exit_code: &mut ExitCode) { pub(super) fn cd(args: &Vec<String>, _variables: &mut Variables, exit_code: &mut ExitCode) {
let path: Option<PathBuf>; let path: Option<PathBuf>;
if !args.is_empty() { if !args.is_empty() {
@ -31,11 +32,11 @@ pub(super) fn cd(args: &Vec<String>, exit_code: &mut ExitCode) {
} }
} }
pub(super) fn colon(_args: &Vec<String>, exit_code: &mut ExitCode) { pub(super) fn colon(_args: &Vec<String>, _variables: &mut Variables, exit_code: &mut ExitCode) {
exit_code.set_success(); exit_code.set_success();
} }
pub(crate) fn exit(args: &Vec<String>, exit_code: &mut ExitCode) { pub(crate) fn exit(args: &Vec<String>, _variables: &mut Variables, exit_code: &mut ExitCode) {
let raw_exit_code: Result<i32, IllegalNumberError>; let raw_exit_code: Result<i32, IllegalNumberError>;
if args.is_empty() { if args.is_empty() {
@ -54,3 +55,13 @@ pub(crate) fn exit(args: &Vec<String>, exit_code: &mut ExitCode) {
error::print(Box::new(raw_exit_code.unwrap_err())); error::print(Box::new(raw_exit_code.unwrap_err()));
} }
} }
pub(super) fn unset(args: &Vec<String>, 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();
}

View file

@ -1,5 +1,6 @@
use super::Command; use super::Command;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
use crate::variables::Variables;
pub struct CommandSequence { pub struct CommandSequence {
command: Option<Box<dyn Command>>, command: Option<Box<dyn Command>>,
@ -34,12 +35,15 @@ impl CommandSequence {
} }
impl Command for 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() { 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() { 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);
} }
} }
} }

View file

@ -5,14 +5,15 @@ pub mod command_sequence;
use crate::error; use crate::error;
use crate::error::CommandNotFoundError; use crate::error::CommandNotFoundError;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
use crate::variables::Variables;
type BuiltinFunction = fn(&Vec<String>, &mut ExitCode); type BuiltinFunction = fn(&Vec<String>, &mut Variables, &mut ExitCode);
#[derive(Debug)] #[derive(Debug)]
pub struct NoSuchBuiltinError; pub struct NoSuchBuiltinError;
pub trait Command { 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 { pub struct Builtin {
@ -28,6 +29,7 @@ impl Builtin {
let function: Option<BuiltinFunction> = match program.as_str() { let function: Option<BuiltinFunction> = match program.as_str() {
"cd" => Some(builtins::cd), "cd" => Some(builtins::cd),
"exit" => Some(builtins::exit), "exit" => Some(builtins::exit),
"unset" => Some(builtins::unset),
":" => Some(builtins::colon), ":" => Some(builtins::colon),
_ => None, _ => None,
}; };
@ -44,8 +46,8 @@ impl Builtin {
} }
impl Command for Builtin { impl Command for Builtin {
fn spawn(&mut self, exit_code: &mut ExitCode) { fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode) {
(self.function)(&self.args, exit_code) (self.function)(&self.args, variables, exit_code)
} }
} }
@ -65,7 +67,7 @@ impl UnixProgram {
} }
impl Command for 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(); let handle = self.command.spawn();
if handle.is_ok() { if handle.is_ok() {

View file

@ -16,11 +16,11 @@ pub fn run() {
let mut command_sequence = parse(user_input.unwrap(), &variables); let mut command_sequence = parse(user_input.unwrap(), &variables);
if !command_sequence.is_empty() { if !command_sequence.is_empty() {
command_sequence.spawn(&mut current_exit_code); command_sequence.spawn(&mut variables, &mut current_exit_code);
} }
} else { } else {
println!(); println!();
builtins::exit(&vec![], &mut current_exit_code); builtins::exit(&vec![], &mut variables, &mut current_exit_code);
} }
} }
} }