Compare commits

...

11 commits

8 changed files with 138 additions and 32 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

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

66
src/command.rs Normal file
View file

@ -0,0 +1,66 @@
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)
}
}
}

34
src/control.rs Normal file
View file

@ -0,0 +1,34 @@
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);
}
}
}

8
src/error.rs Normal file
View file

@ -0,0 +1,8 @@
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);
}

21
src/interface.rs Normal file
View file

@ -0,0 +1,21 @@
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,31 +1,8 @@
use std::io;
use std::io::Write;
fn exit() {
println!("exit");
std::process::exit(0);
}
mod command;
mod control;
mod error;
mod interface;
fn main() {
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);
}
}
control::run();
}