Skip to content

Commit

Permalink
Clean
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Jan 7, 2024
1 parent 764fc51 commit 8c07f0a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
20 changes: 18 additions & 2 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -27,7 +27,7 @@ pub struct ShellCore {
pub is_subshell: bool,
pub tty_fd: RawFd,
pub job_table: Vec<JobEntry>,
pub tcwd: Option<path::PathBuf>, // the_current_working_directory
tcwd: Option<path::PathBuf>, // the_current_working_directory
}

fn is_interactive() -> bool {
Expand Down Expand Up @@ -194,4 +194,20 @@ impl ShellCore {
Err(err) => eprintln!("pwd: error retrieving current directory: {:?}", err),
}
}

pub fn get_current_directory(&mut self) -> Option<path::PathBuf> {
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
}
}
9 changes: 0 additions & 9 deletions src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod pwd;
mod utils;

use crate::ShellCore;
use std::{io, env, path};

impl ShellCore {
pub fn set_builtins(&mut self) {
Expand All @@ -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<String>) -> i32 {
Expand Down
2 changes: 1 addition & 1 deletion src/core/builtins/cd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn cd_oldpwd(core: &mut ShellCore, args: &mut Vec<String>) -> 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());
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/core/builtins/pwd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> i32 {
if args.len() == 1 || &args[1][..1] != "-" { // $ pwd, $ pwd aaa
Expand All @@ -22,7 +21,7 @@ pub fn pwd(core: &mut ShellCore, args: &mut Vec<String>) -> 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;
Expand Down
10 changes: 1 addition & 9 deletions src/core/builtins/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<path::PathBuf> {
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);
Expand All @@ -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);
};
Expand Down

0 comments on commit 8c07f0a

Please sign in to comment.