Compare commits
8 commits
b06b0f1c15
...
48f25feb6a
Author | SHA1 | Date | |
---|---|---|---|
48f25feb6a | |||
4338ddffaf | |||
4454d10ef5 | |||
f94a247d0f | |||
8d039e3de3 | |||
d7f2de803d | |||
2a0f11cca8 | |||
5f59139c3e |
10 changed files with 115 additions and 105 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.2"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
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
|
||||||
|
|
100
src/command.rs
100
src/command.rs
|
@ -1,100 +0,0 @@
|
||||||
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 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 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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(u8::MAX),
|
|
||||||
};
|
|
||||||
|
|
||||||
exit_code
|
|
||||||
} else {
|
|
||||||
let message = format!("{}: command not found", &program);
|
|
||||||
error::print_error(message);
|
|
||||||
ExitCode::not_found()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
36
src/command/command_sequence.rs
Normal file
36
src/command/command_sequence.rs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
use crate::command::Command;
|
||||||
|
use crate::exit_code::ExitCode;
|
||||||
|
|
||||||
|
pub struct CommandSequence {
|
||||||
|
command: Box<dyn Command>,
|
||||||
|
next_command: Option<Box<CommandSequence>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandSequence {
|
||||||
|
pub fn new(command: impl Command + 'static) -> Self {
|
||||||
|
Self {
|
||||||
|
command: Box::new(command),
|
||||||
|
next_command: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add(&mut self, command: impl Command + 'static) {
|
||||||
|
if self.next_command.is_none() {
|
||||||
|
self.next_command = Some(Box::new(CommandSequence::new(command)));
|
||||||
|
} else {
|
||||||
|
self.next_command.as_mut().unwrap().add(command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command for CommandSequence {
|
||||||
|
fn spawn(&mut self) -> ExitCode {
|
||||||
|
let mut exit_code = self.command.spawn();
|
||||||
|
|
||||||
|
if self.next_command.is_some() {
|
||||||
|
exit_code = self.next_command.as_mut().unwrap().spawn();
|
||||||
|
}
|
||||||
|
|
||||||
|
exit_code
|
||||||
|
}
|
||||||
|
}
|
8
src/command/mod.rs
Normal file
8
src/command/mod.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
pub mod command_sequence;
|
||||||
|
pub mod unix_program;
|
||||||
|
|
||||||
|
use crate::exit_code::ExitCode;
|
||||||
|
|
||||||
|
pub trait Command {
|
||||||
|
fn spawn(&mut self) -> ExitCode;
|
||||||
|
}
|
46
src/command/unix_program.rs
Normal file
46
src/command/unix_program.rs
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
use crate::command::Command;
|
||||||
|
use crate::error::print_error;
|
||||||
|
use crate::exit_code::ExitCode;
|
||||||
|
|
||||||
|
pub struct UnixProgram {
|
||||||
|
command: std::process::Command,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UnixProgram {
|
||||||
|
pub fn new(mut argv: Vec<String>) -> Self {
|
||||||
|
let program = argv.remove(0);
|
||||||
|
let mut command = std::process::Command::new(&program);
|
||||||
|
command.args(argv);
|
||||||
|
|
||||||
|
Self { command }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command for UnixProgram {
|
||||||
|
fn spawn(&mut self) -> ExitCode {
|
||||||
|
let handle = self.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(u8::MAX),
|
||||||
|
};
|
||||||
|
|
||||||
|
exit_code
|
||||||
|
} else {
|
||||||
|
let message = format!(
|
||||||
|
"{}: command not found",
|
||||||
|
self.command.get_program().to_str().unwrap()
|
||||||
|
);
|
||||||
|
print_error(message);
|
||||||
|
ExitCode::not_found()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::command::{Command, ExitCode};
|
use crate::command::Command;
|
||||||
|
use crate::exit_code::ExitCode;
|
||||||
use crate::interface::{get_user_input, print_prompt};
|
use crate::interface::{get_user_input, print_prompt};
|
||||||
use crate::parser::parse_command_line;
|
use crate::parser::parse_command_line;
|
||||||
|
|
||||||
|
|
17
src/exit_code.rs
Normal file
17
src/exit_code.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
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 fn not_found() -> Self {
|
||||||
|
Self { exit_code: 127 }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
mod command;
|
mod command;
|
||||||
mod control;
|
mod control;
|
||||||
mod error;
|
mod error;
|
||||||
|
mod exit_code;
|
||||||
mod interface;
|
mod interface;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::command::{CommandSequence, UnixProgram};
|
use crate::command::command_sequence::CommandSequence;
|
||||||
|
use crate::command::unix_program::UnixProgram;
|
||||||
|
|
||||||
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
||||||
let argv: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
|
let argv: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
|
||||||
|
@ -6,7 +7,7 @@ pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
||||||
if !argv.is_empty() {
|
if !argv.is_empty() {
|
||||||
let command = UnixProgram::new(argv);
|
let command = UnixProgram::new(argv);
|
||||||
let command_sequence = CommandSequence::new(command);
|
let command_sequence = CommandSequence::new(command);
|
||||||
// command_sequence.add(_command);
|
|
||||||
Some(command_sequence)
|
Some(command_sequence)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
Loading…
Reference in a new issue