Compare commits

...

3 commits

4 changed files with 55 additions and 9 deletions

View file

@ -18,6 +18,45 @@ pub trait Command {
fn spawn(&self) -> ExitCode; fn spawn(&self) -> ExitCode;
} }
pub struct CommandSequence {
command: Option<Box<dyn Command>>,
next_command: Option<Box<dyn Command>>,
}
impl CommandSequence {
pub fn new() -> Self {
Self {
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(&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_ref().unwrap().spawn();
}
exit_code
}
}
pub struct UnixProgram { pub struct UnixProgram {
argv: Vec<String>, argv: Vec<String>,
} }

View file

@ -1,5 +1,6 @@
use crate::command::{Command, ExitCode, UnixProgram}; use crate::command::{Command, ExitCode};
use crate::interface::{get_user_input, print_prompt}; use crate::interface::{get_user_input, print_prompt};
use crate::parser::parse_command_line;
fn exit(code: &ExitCode) { fn exit(code: &ExitCode) {
let code = i32::from(code.get()); let code = i32::from(code.get());
@ -17,15 +18,9 @@ pub fn run() {
if user_input.is_some() { if user_input.is_some() {
let user_input = user_input.unwrap(); let user_input = user_input.unwrap();
let command_sequence = parse_command_line(user_input);
let argv: Vec<String> = user_input current_exit_code = command_sequence.spawn();
.split_whitespace()
.map(|s| s.to_string())
.collect();
if !argv.is_empty() {
current_exit_code = UnixProgram::new().argv(argv).spawn();
}
} else { } else {
println!(); println!();
exit(&current_exit_code); exit(&current_exit_code);

View file

@ -2,6 +2,7 @@ mod command;
mod control; mod control;
mod error; mod error;
mod interface; mod interface;
mod parser;
fn main() { fn main() {
control::run(); control::run();

11
src/parser.rs Normal file
View file

@ -0,0 +1,11 @@
use crate::command::{CommandSequence, UnixProgram};
pub fn parse_command_line(line: String) -> CommandSequence {
let /*mut*/ command_sequence = CommandSequence::new();
let argv = line.split_whitespace().map(|s| s.to_string()).collect();
let _command = UnixProgram::new().argv(argv);
// command_sequence.add(command);
command_sequence
}