From b02576d3a00f182394be4bc41a26de50e4b64078 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sat, 30 Mar 2024 20:22:47 -0600 Subject: Add the 'unset' built-in command Add the 'unset' command to remove global environment variable definitions from the shell. --- src/poem/anthology.rs | 5 ++++- src/poem/anthology/export.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/poem/anthology.rs b/src/poem/anthology.rs index 94b5f6b..2a2f62a 100644 --- a/src/poem/anthology.rs +++ b/src/poem/anthology.rs @@ -7,7 +7,9 @@ use crate::compose::Environment; use crate::poem::Verse; /// A static list of all the built-in commands -static INDEX: [&str; 6] = ["alias", "cd", "exit", "export", "source", "unalias"]; +static INDEX: [&str; 7] = [ + "alias", "cd", "exit", "export", "source", "unalias", "unset", +]; /// Lookup the index of a built-in command /// @@ -30,6 +32,7 @@ pub fn incant(verse: &Verse, index: usize, env: &mut Environment) -> i32 { "export" => export::incant(verse), "source" => source::incant(verse, env), "unalias" => alias::unincant(verse, &mut env.aliases), + "unset" => export::unincant(verse), _ => unreachable!(), } } diff --git a/src/poem/anthology/export.rs b/src/poem/anthology/export.rs index 76dfd99..a61935c 100644 --- a/src/poem/anthology/export.rs +++ b/src/poem/anthology/export.rs @@ -21,3 +21,18 @@ pub fn incant(verse: &Verse) -> i32 { 0 } + +pub fn unincant(verse: &Verse) -> i32 { + match verse.clause() { + Some(clause) => { + for stanza in clause { + env::remove_var(stanza); + } + } + None => { + eprintln!("unset: not enough arguments"); + return 1; + } + } + 0 +} -- cgit v1.2.3