Compare commits
No commits in common. "b06b0f1c150a00bf20118705acf5877b2762c9f3" and "ad5e96782a0c1c08d6fc3ec45db0b51630cd88cd" have entirely different histories.
b06b0f1c15
...
ad5e96782a
6 changed files with 21 additions and 65 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.2.1"
|
version = "0.2.0"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.2.1"
|
version = "0.2.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
|
||||||
|
|
|
@ -12,58 +12,24 @@ impl ExitCode {
|
||||||
pub fn get(&self) -> u8 {
|
pub fn get(&self) -> u8 {
|
||||||
self.exit_code
|
self.exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn not_found() -> Self {
|
|
||||||
Self { exit_code: 127 }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Command {
|
pub trait Command {
|
||||||
fn spawn(&self) -> ExitCode;
|
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 {
|
pub struct UnixProgram {
|
||||||
argv: Vec<String>,
|
argv: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnixProgram {
|
impl UnixProgram {
|
||||||
pub fn new(argv: Vec<String>) -> Self {
|
pub fn new() -> Self {
|
||||||
Self { argv }
|
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) {
|
let exit_code = match u8::try_from(exit_code) {
|
||||||
Ok(code) => ExitCode::new(code),
|
Ok(code) => ExitCode::new(code),
|
||||||
Err(_e) => ExitCode::new(u8::MAX),
|
Err(_e) => ExitCode::new(255),
|
||||||
};
|
};
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
} else {
|
} else {
|
||||||
let message = format!("{}: command not found", &program);
|
let message = format!("{}: command not found", &program);
|
||||||
error::print_error(message);
|
error::print_error(message);
|
||||||
ExitCode::not_found()
|
ExitCode::new(127)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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::interface::{get_user_input, print_prompt};
|
||||||
use crate::parser::parse_command_line;
|
|
||||||
|
|
||||||
fn exit(code: &ExitCode) {
|
fn exit(code: &ExitCode) {
|
||||||
let code = i32::from(code.get());
|
let code = i32::from(code.get());
|
||||||
|
@ -13,13 +12,19 @@ pub fn run() {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print_prompt();
|
print_prompt();
|
||||||
|
|
||||||
let user_input = get_user_input();
|
let user_input = get_user_input();
|
||||||
|
|
||||||
if user_input.is_some() {
|
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() {
|
let argv: Vec<String> = user_input
|
||||||
current_exit_code = command_sequence.unwrap().spawn();
|
.split_whitespace()
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if !argv.is_empty() {
|
||||||
|
current_exit_code = UnixProgram::new().argv(argv).spawn();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!();
|
println!();
|
||||||
|
|
|
@ -2,7 +2,6 @@ mod command;
|
||||||
mod control;
|
mod control;
|
||||||
mod error;
|
mod error;
|
||||||
mod interface;
|
mod interface;
|
||||||
mod parser;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
control::run();
|
control::run();
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in a new issue