From 8c07f0aef69719024fac0a199367b214b7b8ce9c Mon Sep 17 00:00:00 2001 From: Ryuichi Ueda Date: Sun, 7 Jan 2024 11:44:03 +0900 Subject: [PATCH] Clean --- src/core.rs | 20 ++++++++++++++++++-- src/core/builtins.rs | 9 --------- src/core/builtins/cd.rs | 2 +- src/core/builtins/pwd.rs | 3 +-- src/core/builtins/utils.rs | 10 +--------- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/core.rs b/src/core.rs index 06d4f288..0a640e7e 100644 --- a/src/core.rs +++ b/src/core.rs @@ -6,7 +6,7 @@ pub mod jobtable; use std::collections::HashMap; use std::os::fd::RawFd; -use std::{process, env, path}; +use std::{io, env, path, process}; use nix::{fcntl, unistd}; use nix::sys::{signal, wait}; use nix::sys::signal::{Signal, SigHandler}; @@ -27,7 +27,7 @@ pub struct ShellCore { pub is_subshell: bool, pub tty_fd: RawFd, pub job_table: Vec, - pub tcwd: Option, // the_current_working_directory + tcwd: Option, // the_current_working_directory } fn is_interactive() -> bool { @@ -194,4 +194,20 @@ impl ShellCore { Err(err) => eprintln!("pwd: error retrieving current directory: {:?}", err), } } + + pub fn get_current_directory(&mut self) -> Option { + if self.tcwd.is_none() { + self.init_current_directory(); + } + self.tcwd.clone() + } + + + pub fn set_current_directory(&mut self, path: &path::PathBuf) -> Result<(), io::Error> { + let res = env::set_current_dir(path); + if res.is_ok() { + self.tcwd = Some(path.clone()); + } + res + } } diff --git a/src/core/builtins.rs b/src/core/builtins.rs index 6246c9a7..b5ee3bd7 100644 --- a/src/core/builtins.rs +++ b/src/core/builtins.rs @@ -7,7 +7,6 @@ mod pwd; mod utils; use crate::ShellCore; -use std::{io, env, path}; impl ShellCore { pub fn set_builtins(&mut self) { @@ -18,14 +17,6 @@ impl ShellCore { self.builtins.insert("pwd".to_string(), pwd::pwd); self.builtins.insert("true".to_string(), true_); } - - pub fn set_current_directory(&mut self, path: &path::PathBuf) -> Result<(), io::Error> { - let res = env::set_current_dir(path); - if res.is_ok() { - self.tcwd = Some(path.clone()); - } - res - } } pub fn exit(core: &mut ShellCore, args: &mut Vec) -> i32 { diff --git a/src/core/builtins/cd.rs b/src/core/builtins/cd.rs index 7a502214..3ca70b57 100644 --- a/src/core/builtins/cd.rs +++ b/src/core/builtins/cd.rs @@ -44,7 +44,7 @@ fn cd_oldpwd(core: &mut ShellCore, args: &mut Vec) -> i32 { } fn set_oldpwd(core: &mut ShellCore) { - if let Some(old) = utils::get_current_directory(core) { + if let Some(old) = core.get_current_directory() { core.vars.insert("OLDPWD".to_string(), old.display().to_string()); }; } diff --git a/src/core/builtins/pwd.rs b/src/core/builtins/pwd.rs index 7e93e266..873d1865 100644 --- a/src/core/builtins/pwd.rs +++ b/src/core/builtins/pwd.rs @@ -3,7 +3,6 @@ //SPDX-License-Identifier: BSD-3-Clause use crate::ShellCore; -use super::utils; pub fn pwd(core: &mut ShellCore, args: &mut Vec) -> i32 { if args.len() == 1 || &args[1][..1] != "-" { // $ pwd, $ pwd aaa @@ -22,7 +21,7 @@ pub fn pwd(core: &mut ShellCore, args: &mut Vec) -> i32 { } fn show_pwd(core: &mut ShellCore, physical: bool) -> i32 { - if let Some(mut path) = utils::get_current_directory(core) { + if let Some(mut path) = core.get_current_directory() { if physical && path.is_symlink() { if let Ok(c) = path.canonicalize() { path = c; diff --git a/src/core/builtins/utils.rs b/src/core/builtins/utils.rs index 1caa8014..8805c01f 100644 --- a/src/core/builtins/utils.rs +++ b/src/core/builtins/utils.rs @@ -4,14 +4,6 @@ use crate::ShellCore; use std::path::{Path, PathBuf, Component}; -use std::path; - -pub fn get_current_directory(core: &mut ShellCore) -> Option { - if core.tcwd.is_none() { - core.init_current_directory(); - } - core.tcwd.clone() -} pub fn make_absolute_path(core: &mut ShellCore, path_str: &str) -> PathBuf { let path = Path::new(&path_str); @@ -31,7 +23,7 @@ pub fn make_absolute_path(core: &mut ShellCore, path_str: &str) -> PathBuf { } } } else { // current - if let Some(tcwd) = get_current_directory(core) { + if let Some(tcwd) = core.get_current_directory() { absolute.push(tcwd); absolute.push(path); };