Compare commits

..

No commits in common. "d7f2de803d28e1f65ac2ce9e6c88fd3a8f300eb5" and "557f196fe92d575fa8d3448012012975508ab33b" have entirely different histories.

5 changed files with 49 additions and 35 deletions

2
Cargo.lock generated
View file

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

View file

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

View file

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

View file

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