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

View file

@ -1,6 +1,6 @@
[package]
name = "rash"
version = "0.2.1"
version = "0.2.0"
edition = "2021"
# 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 {
fn spawn(&mut self) -> ExitCode;
fn spawn(&self) -> ExitCode;
}
pub struct CommandSequence {
command: Box<dyn Command>,
command: Option<Box<dyn Command>>,
next_command: Option<Box<dyn Command>>,
}
impl CommandSequence {
pub fn new(command: impl Command + 'static) -> Self {
pub fn new() -> Self {
Self {
command: Box::new(command),
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 {
fn spawn(&mut self) -> ExitCode {
let mut exit_code = self.command.spawn();
fn spawn(&self) -> ExitCode {
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() {
exit_code = self.next_command.as_mut().unwrap().spawn();
exit_code = self.next_command.as_ref().unwrap().spawn();
}
exit_code
@ -49,22 +62,29 @@ impl Command for CommandSequence {
}
pub struct UnixProgram {
command: std::process::Command,
argv: Vec<String>,
}
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);
pub fn new() -> Self {
Self { argv: Vec::new() }
}
Self { command }
pub fn argv(&mut self, argv: Vec<String>) -> &mut Self {
self.argv = argv;
self
}
}
impl Command for UnixProgram {
fn spawn(&mut self) -> ExitCode {
let handle = self.command.spawn();
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
@ -81,10 +101,7 @@ impl Command for UnixProgram {
exit_code
} else {
let message = format!(
"{}: command not found",
self.command.get_program().to_str().unwrap()
);
let message = format!("{}: command not found", &program);
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 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.unwrap().spawn();
}
current_exit_code = command_sequence.spawn();
} else {
println!();
exit(&current_exit_code);

View file

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