Compare commits

..

No commits in common. "ad5e96782a0c1c08d6fc3ec45db0b51630cd88cd" and "443d5d50a9b874764dd80e9e30fefcf21771ccd0" have entirely different histories.

8 changed files with 32 additions and 138 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "rash" name = "rash"
version = "0.2.0" version = "0.1.0"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "rash" name = "rash"
version = "0.2.0" version = "0.1.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -6,8 +6,7 @@ I want to learn Rust and see how much effort I can put into making an interopera
# Roadmap # Roadmap
- [x] Execute commands - [ ] Execute commands
- [ ] Parse commands
- [ ] Variables - [ ] Variables
- [ ] Environment variables - [ ] Environment variables
- [ ] Builtins - [ ] Builtins
@ -15,5 +14,6 @@ I want to learn Rust and see how much effort I can put into making an interopera
- [ ] `set` - [ ] `set`
- [ ] `alias` - [ ] `alias`
- [ ] `type` - [ ] `type`
- [ ] Parse commands
- [ ] Command-line flags - [ ] Command-line flags
- [ ] PS1 - [ ] PS1

View file

@ -1,66 +0,0 @@
use crate::error;
pub struct ExitCode {
exit_code: u8,
}
impl ExitCode {
pub fn new(exit_code: u8) -> Self {
Self { exit_code }
}
pub fn get(&self) -> u8 {
self.exit_code
}
}
pub trait Command {
fn spawn(&self) -> ExitCode;
}
pub struct UnixProgram {
argv: Vec<String>,
}
impl UnixProgram {
pub fn new() -> Self {
Self { argv: Vec::new() }
}
pub fn argv(&mut self, argv: Vec<String>) -> &mut Self {
self.argv = argv;
self
}
}
impl Command for UnixProgram {
fn spawn(&self) -> ExitCode {
let mut argv = self.argv.clone();
let program = argv.remove(0);
let mut command = std::process::Command::new(&program);
command.args(argv);
let handle = command.spawn();
if handle.is_ok() {
let exit_code = handle
.unwrap()
.wait()
.expect("error waiting for the child")
.code()
.unwrap_or(1);
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(255),
};
exit_code
} else {
let message = format!("{}: command not found", &program);
error::print_error(message);
ExitCode::new(127)
}
}
}

View file

@ -1,34 +0,0 @@
use crate::command::{Command, ExitCode, UnixProgram};
use crate::interface::{get_user_input, print_prompt};
fn exit(code: &ExitCode) {
let code = i32::from(code.get());
println!("exit");
std::process::exit(code);
}
pub fn run() {
let mut current_exit_code = ExitCode::new(0);
loop {
print_prompt();
let user_input = get_user_input();
if user_input.is_some() {
let user_input = user_input.unwrap();
let argv: Vec<String> = user_input
.split_whitespace()
.map(|s| s.to_string())
.collect();
if !argv.is_empty() {
current_exit_code = UnixProgram::new().argv(argv).spawn();
}
} else {
println!();
exit(&current_exit_code);
}
}
}

View file

@ -1,8 +0,0 @@
use std::path::Path;
pub fn print_error(message: String) {
let name = std::env::args().next().unwrap();
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
eprintln!("{}: {}", name, message);
}

View file

@ -1,21 +0,0 @@
use std::io::Write;
use std::io::{stdin, stdout};
pub fn print_prompt() {
print!("$ ");
stdout().flush().unwrap();
}
pub fn get_user_input() -> Option<String> {
let mut user_input = String::new();
let bytes_read = stdin()
.read_line(&mut user_input)
.expect("error reading user input");
if bytes_read == 0 {
None
} else {
Some(user_input)
}
}

View file

@ -1,8 +1,31 @@
mod command; use std::io;
mod control; use std::io::Write;
mod error;
mod interface; fn exit() {
println!("exit");
std::process::exit(0);
}
fn main() { fn main() {
control::run(); let mut user_input = String::new();
let mut bytes_read;
loop {
print!("$ ");
io::stdout().flush().unwrap();
user_input.clear();
bytes_read = io::stdin()
.read_line(&mut user_input)
.expect("error reading user input");
if bytes_read == 0 {
println!();
exit();
} else {
let user_input = user_input.trim();
println!("{}", user_input);
}
}
} }