diff options
author | Rory Dudley | 2024-02-16 19:21:58 -0700 |
---|---|---|
committer | Rory Dudley | 2024-02-16 19:21:58 -0700 |
commit | 508550c4d5c1b8933decfb47fc02481959ab0c65 (patch) | |
tree | ec83275b7c303fa1fbe5e30feae77981836af2b8 /src/main.rs | |
download | dwarvish-508550c4d5c1b8933decfb47fc02481959ab0c65.tar.gz |
Initial commit
An extremely miniamal shell. It is capable of forking processes, and
passing arguments to them, but that's pretty much it.
Notes
Notes:
This is pretty much a prototype, to see how easily something like a
shell could be implemented in Rust.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c592e51 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,86 @@ +use std::fs; +use std::io; +use std::io::Write; +use std::process::Command; + +fn eval(paths: &[&str], prompt: &str) { + let mut bins: Vec<String> = Vec::new(); + + for path in paths { + let files = fs::read_dir(path).expect("Unable to read files in your path"); + for file in files { + bins.push(file.unwrap().path().display().to_string()); + } + } + + loop { + // Output the prompt + io::stdout().flush().unwrap(); + print!("{}", prompt); + io::stdout().flush().unwrap(); + + // Wait for user input + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Unable to evaluate the input string"); + let input = input.trim(); + + // Check if user wants to exit the shell + if input == "exit" || input == "quit" { + break; + } + + // Parse command and arguments + let mut split = input.split(' '); + let cmd = match split.next() { + Some(str) if str.trim().is_empty() => continue, + Some(str) => str.trim(), + None => continue, + }; + + // Parse arguments + let mut args = vec![]; + loop { + let next = split.next(); + match next { + Some(str) => args.push(str), + None => break, + } + } + + // Check if the command exists + let cmd = match bins.iter().find(|b| b.split("/").last().unwrap() == cmd) { + Some(cmd) => cmd, + None => { + println!("Command not found"); + continue; + } + }; + + // Run the command (and wait for it to finish) + let mut child = match Command::new(cmd).args(args).spawn() { + Ok(ch) => ch, + Err(_) => { + println!("Unable to fork"); + continue; + } + }; + child.wait().unwrap(); + } +} + +fn main() { + let paths = [ + "/bin", + "/sbin", + "/usr/bin", + "/usr/sbin", + "/usr/local/bin", + "/usr/local/sbin", + ]; + + let prompt = "|> "; + + eval(&paths, prompt); +} |