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]]
|
[[package]]
|
||||||
name = "pts_tester"
|
name = "psh"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
83
src/main.rs
83
src/main.rs
@ -1,15 +1,9 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, Write},
|
env::{current_dir, set_current_dir}, io::{self, Write}, path::{Component, Path, PathBuf}, process::Command
|
||||||
path::{Component, Path, PathBuf}, process::Command,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
|
||||||
struct Ls {
|
|
||||||
path: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Cat {
|
struct Cat {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
@ -20,31 +14,13 @@ struct Cd {
|
|||||||
path: PathBuf,
|
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() {
|
fn main() {
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
|
|
||||||
let mut cwd = Path::new("/").to_owned();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print!(
|
print!(
|
||||||
"[root@mikros-dev {}]$ ",
|
"[root@mikros-dev {}]$ ",
|
||||||
cwd.components()
|
current_dir().unwrap().components()
|
||||||
.last()
|
.last()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_os_str()
|
.as_os_str()
|
||||||
@ -62,35 +38,7 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
match cmd[0].as_str() {
|
match cmd[0].as_str() {
|
||||||
"pwd" => println!("{}", cwd.to_string_lossy()),
|
"pwd" => println!("{}", current_dir().unwrap().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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"cat" => {
|
"cat" => {
|
||||||
let cmd = match Cat::try_parse_from(&cmd) {
|
let cmd = match Cat::try_parse_from(&cmd) {
|
||||||
Ok(cmd) => cmd,
|
Ok(cmd) => cmd,
|
||||||
@ -99,14 +47,7 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let abs_path = if cmd.path.is_relative() {
|
let contents = match std::fs::read(&cmd.path) {
|
||||||
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) {
|
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("cat: {}: {}", cmd.path.display(), e);
|
println!("cat: {}: {}", cmd.path.display(), e);
|
||||||
@ -128,23 +69,13 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let abs_path = if cmd.path.is_relative() {
|
match set_current_dir(&cmd.path) {
|
||||||
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) {
|
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("cd: {}: {}", cmd.path.display(), e);
|
println!("cd: {}: {}", cmd.path.display(), e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
cwd = abs_path;
|
|
||||||
}
|
}
|
||||||
"exit" => {
|
"exit" => {
|
||||||
break;
|
break;
|
||||||
@ -152,8 +83,8 @@ fn main() {
|
|||||||
"help" => {
|
"help" => {
|
||||||
println!("Commands:");
|
println!("Commands:");
|
||||||
println!("pwd");
|
println!("pwd");
|
||||||
println!("ls");
|
|
||||||
println!("cat");
|
println!("cat");
|
||||||
|
println!("cd");
|
||||||
println!("exit");
|
println!("exit");
|
||||||
println!("help");
|
println!("help");
|
||||||
}
|
}
|
||||||
@ -163,7 +94,7 @@ fn main() {
|
|||||||
} else {
|
} else {
|
||||||
format!("/bin/{}", cmd[0])
|
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,
|
Ok(x) => x,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("psh: {}: {}", cmd[0], e);
|
println!("psh: {}: {}", cmd[0], e);
|
||||||
|
Loading…
Reference in New Issue
Block a user