Skip to content

Commit

Permalink
Change structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 31, 2023
1 parent b270684 commit 5f15c80
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
35 changes: 3 additions & 32 deletions src/core/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,23 @@
//SPDX-FileCopyrightText: 2023 @caro@mi.shellgei.org
//SPDX-License-Identifier: BSD-3-Clause

mod utils;
mod cd;
mod pwd;
mod utils;

use crate::ShellCore;

impl ShellCore {
pub fn set_builtins(&mut self) {
self.builtins.insert(":".to_string(), true_);
self.builtins.insert("cd".to_string(), cd);
self.builtins.insert("cd".to_string(), cd::cd);
self.builtins.insert("exit".to_string(), exit);
self.builtins.insert("false".to_string(), false_);
self.builtins.insert("pwd".to_string(), pwd::pwd);
self.builtins.insert("true".to_string(), true_);
}
}

pub fn cd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() > 2 {
eprintln!("sush: cd: too many arguments");
return 1;
}

if args.len() == 1 { //only "cd"
let var = "~".to_string();
args.push(var);
}else if args.len() == 2 && args[1] == "-" { // cd -
if let Some(old) = core.vars.get("OLDPWD") {
println!("{}", &old);
args[1] = old.to_string();
}
};

if let Some(old) = core.get_current_directory().clone() {
core.vars.insert("OLDPWD".to_string(), old.display().to_string());
};

let path = utils::make_canonical_path(utils::make_absolute_path(core, &args[1]));
if core.set_current_directory(&path).is_ok() {
core.vars.insert("PWD".to_string(), path.display().to_string());
0
}else{
eprintln!("sush: cd: {:?}: No such file or directory", &path);
1
}
}

pub fn exit(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
eprintln!("exit");
if args.len() > 1 {
Expand Down
36 changes: 36 additions & 0 deletions src/core/builtins/cd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//SPDX-FileCopyrightText: 2023 Ryuichi Ueda <ryuichiueda@gmail.com>
//SPDX-FileCopyrightText: 2023 @caro@mi.shellgei.org
//SPDX-License-Identifier: BSD-3-Clause

use crate::ShellCore;
use super::utils;

pub fn cd(core: &mut ShellCore, args: &mut Vec<String>) -> i32 {
if args.len() > 2 {
eprintln!("sush: cd: too many arguments");
return 1;
}

if args.len() == 1 { //only "cd"
let var = "~".to_string();
args.push(var);
}else if args.len() == 2 && args[1] == "-" { // cd -
if let Some(old) = core.vars.get("OLDPWD") {
println!("{}", &old);
args[1] = old.to_string();
}
};

if let Some(old) = core.get_current_directory().clone() {
core.vars.insert("OLDPWD".to_string(), old.display().to_string());
};

let path = utils::make_canonical_path(utils::make_absolute_path(core, &args[1]));
if core.set_current_directory(&path).is_ok() {
core.vars.insert("PWD".to_string(), path.display().to_string());
0
}else{
eprintln!("sush: cd: {:?}: No such file or directory", &path);
1
}
}

0 comments on commit 5f15c80

Please sign in to comment.