feat: add very basic ash shell clone
this supports no scripting at all!
This commit is contained in:
parent
7ae11ea80b
commit
182bf45172
6 changed files with 74 additions and 0 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
|
@ -19,4 +19,12 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"boxutils",
|
||||
"coreutils",
|
||||
"shell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shell"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"boxutils",
|
||||
]
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ edition = "2024"
|
|||
[dependencies]
|
||||
boxutils = { path = "./utils" }
|
||||
coreutils = { path = "./coreutils" }
|
||||
shell = { path = "./shell" }
|
||||
|
||||
|
||||
[[bin]]
|
||||
|
|
|
|||
7
shell/Cargo.toml
Normal file
7
shell/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "shell"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
boxutils = { path = "../utils" }
|
||||
56
shell/src/ash.rs
Normal file
56
shell/src/ash.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use boxutils::commands::Command;
|
||||
use std::env;
|
||||
use std::process::Command as stdCommand;
|
||||
use std::io::{self, Write, ErrorKind};
|
||||
use std::path::Path;
|
||||
use std::fmt;
|
||||
|
||||
pub struct Ash;
|
||||
|
||||
impl Command for Ash {
|
||||
fn execute(&self) {
|
||||
loop {
|
||||
let path = env::current_dir();
|
||||
print!(
|
||||
"{} $ ", // TODO: display "#" if root
|
||||
path.expect("unknown").display()
|
||||
);
|
||||
let _ = io::stdout().flush();
|
||||
let mut input = String::new();
|
||||
io::stdin().read_line(&mut input).unwrap();
|
||||
let mut full_cmd = input.trim().split_whitespace();
|
||||
let command = full_cmd.next().unwrap();
|
||||
let mut arguments = full_cmd;
|
||||
match command {
|
||||
"exit" => return,
|
||||
"cd" => {
|
||||
let new_path = arguments.next();
|
||||
let new_path = Path::new(new_path.unwrap());
|
||||
let _ = env::set_current_dir(&new_path).is_ok();
|
||||
},
|
||||
command => {
|
||||
let out = stdCommand::new(command)
|
||||
.args(arguments)
|
||||
.spawn();
|
||||
|
||||
match out {
|
||||
Ok(mut out) => {
|
||||
let _ = out.wait();
|
||||
},
|
||||
Err(err) => match err.kind() {
|
||||
ErrorKind::NotFound => {
|
||||
eprintln!("ash: {}: not found", command);
|
||||
}
|
||||
ErrorKind::PermissionDenied => {
|
||||
eprintln!("ash: {}: permission denied", command);
|
||||
}
|
||||
_ => {
|
||||
eprintln!("ash: uncaught error: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
1
shell/src/lib.rs
Normal file
1
shell/src/lib.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod ash;
|
||||
|
|
@ -8,6 +8,7 @@ pub fn get_registry() -> CommandRegistry {
|
|||
registry.register("cat", Box::new(coreutils::commands::Cat));
|
||||
registry.register("echo", Box::new(coreutils::commands::Echo));
|
||||
registry.register("mkdir", Box::new(coreutils::commands::Mkdir));
|
||||
registry.register("ash", Box::new(shell::ash::Ash));
|
||||
registry.register("box", Box::new(Boxcmd));
|
||||
|
||||
registry
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue