Compare commits

..

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

6 changed files with 21 additions and 65 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

@ -12,58 +12,24 @@ 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(argv: Vec<String>) -> Self {
Self { argv }
pub fn new() -> Self {
Self { argv: Vec::new() }
}
pub fn argv(&mut self, argv: Vec<String>) -> &mut Self {
self.argv = argv;
self
}
}
@ -87,14 +53,14 @@ impl Command for UnixProgram {
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(u8::MAX),
Err(_e) => ExitCode::new(255),
};
exit_code
} else {
let message = format!("{}: command not found", &program);
error::print_error(message);
ExitCode::not_found()
ExitCode::new(127)
}
}
}

View file

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

View file

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

View file

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