diff --git a/src/lib/builtins/mod.rs b/src/lib/builtins/mod.rs index 45f32ba2317f10e732995db0f3c80fb43a42e7e3..4a6da3f7e9c4fdd561434bc1f677b880a493a592 100644 --- a/src/lib/builtins/mod.rs +++ b/src/lib/builtins/mod.rs @@ -25,7 +25,7 @@ use std::{ use parser::Terminator; use shell::{ - self, fork_function::fork_function, job_control::{JobControl, ProcessState}, status::*, variables::VariableType, + self, fork_function::fork_function, job_control::{JobControl, ProcessState}, status::*, FlowLogic, Shell, ShellHistory, }; use sys; @@ -163,8 +163,8 @@ pub fn builtin_cd(args: &[String], shell: &mut Shell) -> i32 { let current_dir = cwd.to_str().unwrap_or("?"); if pwd != current_dir { - shell.set_variable("OLDPWD", VariableType::Str(pwd.clone())); - shell.set_variable("PWD", VariableType::Str(current_dir.into())); + env::set_var("OLDPWD", pwd); + env::set_var("PWD", current_dir); } fork_function(shell, "CD_CHANGE", &["ion"]); } @@ -250,7 +250,7 @@ fn builtin_popd(args: &[String], shell: &mut Shell) -> i32 { if check_help(args, MAN_POPD) { return SUCCESS; } - match shell.directory_stack.popd(args, &mut shell.variables) { + match shell.directory_stack.popd(args) { Ok(()) => SUCCESS, Err(why) => { let stderr = io::stderr(); diff --git a/src/lib/shell/directory_stack.rs b/src/lib/shell/directory_stack.rs index 74623888c8695587cc9197f46731f01120c1521d..dbaca704f0453606ffff8f25a3dac9faca31116e 100644 --- a/src/lib/shell/directory_stack.rs +++ b/src/lib/shell/directory_stack.rs @@ -1,9 +1,9 @@ use super::{ - status::{FAILURE, SUCCESS}, variables::{Variables, VariableType}, + status::{FAILURE, SUCCESS}, + variables::Variables, }; use std::{ - borrow::Cow, collections::VecDeque, env::{current_dir, home_dir, set_current_dir}, - env, + borrow::Cow, collections::VecDeque, env::{self, current_dir, home_dir, set_current_dir}, path::PathBuf, }; @@ -186,12 +186,14 @@ impl DirectoryStack { } } - fn get_previous_dir(&self, variables: &Variables) -> Option<String> { - let previous_pwd = variables.get_str_or_empty("OLDPWD"); - if previous_pwd == "?" || previous_pwd == "" { - None - } else { - Some(previous_pwd) + fn get_previous_dir(&self) -> Option<String> { + match env::var("OLDPWD") { + Ok(previous_pwd) => if previous_pwd == "?" || previous_pwd == "" { + None + } else { + Some(previous_pwd) + }, + Err(_) => None, } } @@ -199,10 +201,9 @@ impl DirectoryStack { &mut self, variables: &Variables, ) -> Result<(), Cow<'static, str>> { - match self.get_previous_dir(variables) { + match self.get_previous_dir() { Some(prev) => { self.dirs.remove(0); - let prev = prev.to_string(); println!("{}", prev); self.change_and_push_dir(&prev, variables) } @@ -224,13 +225,14 @@ impl DirectoryStack { ) } - fn update_env_variables(&mut self, variables: &mut Variables) { + fn update_env_variables(&mut self) { // Update $OLDPWD - let old_pwd = variables.get_str_or_empty("PWD"); - if old_pwd.is_empty() { - variables.set_variable("OLDPWD", VariableType::Str("?".into())); - } else { - variables.set_variable("OLDPWD", VariableType::Str(old_pwd)); + if let Ok(old_pwd) = env::var("PWD") { + if old_pwd.is_empty() { + env::set_var("OLDPWD", "?"); + } else { + env::set_var("OLDPWD", &old_pwd); + } } // Update $PWD @@ -324,18 +326,14 @@ impl DirectoryStack { } }; - self.update_env_variables(variables); + self.update_env_variables(); self.print_dirs(); Ok(()) } /// Attempts to set the current directory to the directory stack's previous directory, /// and then removes the front directory from the stack. - pub(crate) fn popd<I: IntoIterator>( - &mut self, - args: I, - variables: &mut Variables, - ) -> Result<(), Cow<'static, str>> + pub(crate) fn popd<I: IntoIterator>(&mut self, args: I) -> Result<(), Cow<'static, str>> where I::Item: AsRef<str>, { @@ -394,7 +392,7 @@ impl DirectoryStack { ))); } - self.update_env_variables(variables); + self.update_env_variables(); self.print_dirs(); Ok(()) }