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]]
name = "rash"
version = "0.2.0"
version = "0.2.1"

View file

@ -1,6 +1,6 @@
[package]
name = "rash"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
# 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 {
fn spawn(&self) -> ExitCode;
fn spawn(&mut self) -> ExitCode;
}
pub struct CommandSequence {
command: Option<Box<dyn Command>>,
command: Box<dyn Command>,
next_command: Option<Box<dyn Command>>,
}
impl CommandSequence {
pub fn new() -> Self {
pub fn new(command: impl Command + 'static) -> Self {
Self {
command: None,
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 = ExitCode::new(0);
if self.command.is_some() {
exit_code = self.command.as_ref().unwrap().spawn();
}
fn spawn(&mut 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 = self.next_command.as_mut().unwrap().spawn();
}
exit_code
@ -62,29 +49,22 @@ impl Command for CommandSequence {
}
pub struct UnixProgram {
argv: Vec<String>,
command: std::process::Command,
}
impl UnixProgram {
pub fn new() -> Self {
Self { argv: Vec::new() }
}
pub fn new(mut argv: Vec<String>) -> Self {
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.argv = argv;
self
Self { command }
}
}
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();
fn spawn(&mut self) -> ExitCode {
let handle = self.command.spawn();
if handle.is_ok() {
let exit_code = handle
@ -101,7 +81,10 @@ impl Command for UnixProgram {
exit_code
} 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);
ExitCode::not_found()
}

View file

@ -13,14 +13,14 @@ pub fn run() {
loop {
print_prompt();
let user_input = get_user_input();
if user_input.is_some() {
let user_input = user_input.unwrap();
let command_sequence = parse_command_line(user_input);
let command_sequence = parse_command_line(user_input.unwrap());
current_exit_code = command_sequence.spawn();
if command_sequence.is_some() {
current_exit_code = command_sequence.unwrap().spawn();
}
} else {
println!();
exit(&current_exit_code);

View file

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