feat: add very basic ash shell clone

this supports no scripting at all!
This commit is contained in:
user0-07161 2025-03-21 21:21:48 +01:00
parent 7ae11ea80b
commit 182bf45172
6 changed files with 74 additions and 0 deletions

7
shell/Cargo.toml Normal file
View 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
View 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
View file

@ -0,0 +1 @@
pub mod ash;