Compare commits
8 commits
ad5e96782a
...
b06b0f1c15
Author | SHA1 | Date | |
---|---|---|---|
b06b0f1c15 | |||
7ffbc9a608 | |||
3a2e788c3c | |||
557f196fe9 | |||
529d5fdb81 | |||
a3b2f3da74 | |||
dc0e63b8e6 | |||
056d56b348 |
6 changed files with 65 additions and 21 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
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,24 +12,58 @@ 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() -> Self {
|
pub fn new(argv: Vec<String>) -> Self {
|
||||||
Self { argv: Vec::new() }
|
Self { argv }
|
||||||
}
|
|
||||||
|
|
||||||
pub fn argv(&mut self, argv: Vec<String>) -> &mut Self {
|
|
||||||
self.argv = argv;
|
|
||||||
self
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,14 +87,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(255),
|
Err(_e) => ExitCode::new(u8::MAX),
|
||||||
};
|
};
|
||||||
|
|
||||||
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::new(127)
|
ExitCode::not_found()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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::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());
|
||||||
|
@ -12,19 +13,13 @@ 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 user_input = user_input.unwrap();
|
let command_sequence = parse_command_line(user_input.unwrap());
|
||||||
|
|
||||||
let argv: Vec<String> = user_input
|
if command_sequence.is_some() {
|
||||||
.split_whitespace()
|
current_exit_code = command_sequence.unwrap().spawn();
|
||||||
.map(|s| s.to_string())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if !argv.is_empty() {
|
|
||||||
current_exit_code = UnixProgram::new().argv(argv).spawn();
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!();
|
println!();
|
||||||
|
|
|
@ -2,6 +2,7 @@ mod command;
|
||||||
mod control;
|
mod control;
|
||||||
mod error;
|
mod error;
|
||||||
mod interface;
|
mod interface;
|
||||||
|
mod parser;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
control::run();
|
control::run();
|
||||||
|
|
14
src/parser.rs
Normal file
14
src/parser.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue