Compare commits

..

6 commits

5 changed files with 35 additions and 49 deletions

2
Cargo.lock generated
View file

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

View file

@ -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

View file

@ -19,42 +19,29 @@ impl ExitCode {
} }
pub trait Command { pub trait Command {
fn spawn(&self) -> ExitCode; fn spawn(&mut self) -> ExitCode;
} }
pub struct CommandSequence { pub struct CommandSequence {
command: Option<Box<dyn Command>>, command: Box<dyn Command>,
next_command: Option<Box<dyn Command>>, next_command: Option<Box<dyn Command>>,
} }
impl CommandSequence { impl CommandSequence {
pub fn new() -> Self { pub fn new(command: impl Command + 'static) -> Self {
Self { Self {
command: None, command: Box::new(command),
next_command: None, 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 { impl Command for CommandSequence {
fn spawn(&self) -> ExitCode { fn spawn(&mut self) -> ExitCode {
let mut exit_code = ExitCode::new(0); let mut exit_code = self.command.spawn();
if self.command.is_some() {
exit_code = self.command.as_ref().unwrap().spawn();
}
if self.next_command.is_some() { if self.next_command.is_some() {
exit_code = self.next_command.as_ref().unwrap().spawn(); exit_code = self.next_command.as_mut().unwrap().spawn();
} }
exit_code exit_code
@ -62,29 +49,22 @@ impl Command for CommandSequence {
} }
pub struct UnixProgram { pub struct UnixProgram {
argv: Vec<String>, command: std::process::Command,
} }
impl UnixProgram { impl UnixProgram {
pub fn new() -> Self { pub fn new(mut argv: Vec<String>) -> Self {
Self { argv: Vec::new() } let program = argv.remove(0);
} let mut command = std::process::Command::new(&program);
command.args(argv);
pub fn argv(&mut self, argv: Vec<String>) -> &mut Self { Self { command }
self.argv = argv;
self
} }
} }
impl Command for UnixProgram { impl Command for UnixProgram {
fn spawn(&self) -> ExitCode { fn spawn(&mut self) -> ExitCode {
let mut argv = self.argv.clone(); let handle = self.command.spawn();
let program = argv.remove(0);
let mut command = std::process::Command::new(&program);
command.args(argv);
let handle = command.spawn();
if handle.is_ok() { if handle.is_ok() {
let exit_code = handle let exit_code = handle
@ -101,7 +81,10 @@ impl Command for UnixProgram {
exit_code exit_code
} else { } else {
let message = format!("{}: command not found", &program); let message = format!(
"{}: command not found",
self.command.get_program().to_str().unwrap()
);
error::print_error(message); error::print_error(message);
ExitCode::not_found() ExitCode::not_found()
} }

View file

@ -13,14 +13,14 @@ 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 command_sequence = parse_command_line(user_input);
current_exit_code = command_sequence.spawn(); if command_sequence.is_some() {
current_exit_code = command_sequence.unwrap().spawn();
}
} else { } else {
println!(); println!();
exit(&current_exit_code); exit(&current_exit_code);

View file

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