Use std cwd/path canonicalization and remove builtin ls
This commit is contained in:
parent
84b1765591
commit
f176bdce91
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -119,7 +119,7 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pts_tester"
|
||||
name = "psh"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
|
83
src/main.rs
83
src/main.rs
@ -1,15 +1,9 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
path::{Component, Path, PathBuf}, process::Command,
|
||||
env::{current_dir, set_current_dir}, io::{self, Write}, path::{Component, Path, PathBuf}, process::Command
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Ls {
|
||||
path: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Cat {
|
||||
path: PathBuf,
|
||||
@ -20,31 +14,13 @@ struct Cd {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
fn canonicalize_path(path: &Path) -> PathBuf {
|
||||
path.components()
|
||||
.fold(PathBuf::from("/"), |mut buf, component| match component {
|
||||
Component::Prefix(_) => unreachable!(),
|
||||
Component::RootDir | Component::Normal(_) => {
|
||||
buf.push(component);
|
||||
buf
|
||||
}
|
||||
Component::CurDir => buf,
|
||||
Component::ParentDir => {
|
||||
buf.pop();
|
||||
buf
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut line = String::new();
|
||||
|
||||
let mut cwd = Path::new("/").to_owned();
|
||||
|
||||
loop {
|
||||
print!(
|
||||
"[root@mikros-dev {}]$ ",
|
||||
cwd.components()
|
||||
current_dir().unwrap().components()
|
||||
.last()
|
||||
.unwrap()
|
||||
.as_os_str()
|
||||
@ -62,35 +38,7 @@ fn main() {
|
||||
continue;
|
||||
};
|
||||
match cmd[0].as_str() {
|
||||
"pwd" => println!("{}", cwd.to_string_lossy()),
|
||||
"ls" => {
|
||||
let cmd = match Ls::try_parse_from(&cmd) {
|
||||
Ok(cmd) => cmd,
|
||||
Err(err) => {
|
||||
err.print().unwrap();
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let cmd_path = cmd.path.clone().unwrap_or(PathBuf::new());
|
||||
let abs_path = if cmd_path.is_relative() {
|
||||
let mut abs_path = cwd.clone();
|
||||
abs_path.push(cmd_path.clone());
|
||||
canonicalize_path(&abs_path)
|
||||
} else {
|
||||
cmd_path.clone()
|
||||
};
|
||||
let dir = match std::fs::read_dir(&abs_path) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
println!("ls: {}: {}", cmd_path.display(), e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
for entry in dir {
|
||||
let entry = entry.unwrap();
|
||||
println!("{}", entry.file_name().to_string_lossy());
|
||||
}
|
||||
}
|
||||
"pwd" => println!("{}", current_dir().unwrap().to_string_lossy()),
|
||||
"cat" => {
|
||||
let cmd = match Cat::try_parse_from(&cmd) {
|
||||
Ok(cmd) => cmd,
|
||||
@ -99,14 +47,7 @@ fn main() {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let abs_path = if cmd.path.is_relative() {
|
||||
let mut abs_path = cwd.clone();
|
||||
abs_path.push(cmd.path.clone());
|
||||
canonicalize_path(&abs_path)
|
||||
} else {
|
||||
cmd.path.clone()
|
||||
};
|
||||
let contents = match std::fs::read(&abs_path) {
|
||||
let contents = match std::fs::read(&cmd.path) {
|
||||
Ok(val) => val,
|
||||
Err(e) => {
|
||||
println!("cat: {}: {}", cmd.path.display(), e);
|
||||
@ -128,23 +69,13 @@ fn main() {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let abs_path = if cmd.path.is_relative() {
|
||||
let mut abs_path = cwd.clone();
|
||||
abs_path.push(cmd.path.clone());
|
||||
canonicalize_path(&abs_path)
|
||||
} else {
|
||||
cmd.path.clone()
|
||||
};
|
||||
// Try to open the given directory, if it fails it's not a valid directory.
|
||||
// Use Metadata instead once it's implemented.
|
||||
match std::fs::read_dir(&abs_path) {
|
||||
match set_current_dir(&cmd.path) {
|
||||
Ok(_) => (),
|
||||
Err(e) => {
|
||||
println!("cd: {}: {}", cmd.path.display(), e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
cwd = abs_path;
|
||||
}
|
||||
"exit" => {
|
||||
break;
|
||||
@ -152,8 +83,8 @@ fn main() {
|
||||
"help" => {
|
||||
println!("Commands:");
|
||||
println!("pwd");
|
||||
println!("ls");
|
||||
println!("cat");
|
||||
println!("cd");
|
||||
println!("exit");
|
||||
println!("help");
|
||||
}
|
||||
@ -163,7 +94,7 @@ fn main() {
|
||||
} else {
|
||||
format!("/bin/{}", cmd[0])
|
||||
};
|
||||
let mut proc = match Command::new(path).spawn() {
|
||||
let mut proc = match Command::new(path).args(&cmd[1..]).spawn() {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
println!("psh: {}: {}", cmd[0], e);
|
||||
|
Loading…
Reference in New Issue
Block a user