Compare commits

...

8 commits

6 changed files with 65 additions and 21 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -12,24 +12,58 @@ impl ExitCode {
pub fn get(&self) -> u8 {
self.exit_code
}
pub fn not_found() -> Self {
Self { exit_code: 127 }
}
}
pub trait Command {
fn spawn(&self) -> ExitCode;
}
pub struct CommandSequence {
command: Box<dyn Command>,
next_command: Option<Box<dyn Command>>,
}
impl CommandSequence {
pub fn new(command: impl Command + 'static) -> Self {
Self {
command: Box::new(command),
next_command: None,
}
}
/*
pub fn add(&self, command: &dyn Command) {
if self.command.is_none() {
let my_box = Box::new(command.to_owned());
self.command = Some(my_box);
}
}
*/
}
impl Command for CommandSequence {
fn spawn(&self) -> ExitCode {
let mut exit_code = self.command.spawn();
if self.next_command.is_some() {
exit_code = self.next_command.as_ref().unwrap().spawn();
}
exit_code
}
}
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
pub fn new(argv: Vec<String>) -> Self {
Self { argv }
}
}
@ -53,14 +87,14 @@ impl Command for UnixProgram {
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(255),
Err(_e) => ExitCode::new(u8::MAX),
};
exit_code
} else {
let message = format!("{}: command not found", &program);
error::print_error(message);
ExitCode::new(127)
ExitCode::not_found()
}
}
}

View file

@ -1,5 +1,6 @@
use crate::command::{Command, ExitCode, UnixProgram};
use crate::command::{Command, ExitCode};
use crate::interface::{get_user_input, print_prompt};
use crate::parser::parse_command_line;
fn exit(code: &ExitCode) {
let code = i32::from(code.get());
@ -12,19 +13,13 @@ pub fn run() {
loop {
print_prompt();
let user_input = get_user_input();
if user_input.is_some() {
let user_input = user_input.unwrap();
let command_sequence = parse_command_line(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();
if command_sequence.is_some() {
current_exit_code = command_sequence.unwrap().spawn();
}
} else {
println!();

View file

@ -2,6 +2,7 @@ mod command;
mod control;
mod error;
mod interface;
mod parser;
fn main() {
control::run();

14
src/parser.rs Normal file
View file

@ -0,0 +1,14 @@
use crate::command::{CommandSequence, UnixProgram};
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
let argv: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
if !argv.is_empty() {
let command = UnixProgram::new(argv);
let command_sequence = CommandSequence::new(command);
// command_sequence.add(_command);
Some(command_sequence)
} else {
None
}
}