Compare commits

..

No commits in common. "142d7211bc786502b4d1c5491aef8f46c1ca1e69" and "bb5598aee78f11f6beefb6036a01103a3243718c" have entirely different histories.

9 changed files with 109 additions and 140 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "rash" name = "rash"
version = "0.4.0" version = "0.3.0"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "rash" name = "rash"
version = "0.4.0" version = "0.3.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

@ -1,9 +1,39 @@
use super::Command;
use crate::{error::print_error, exit_code::ExitCode}; use crate::{error::print_error, exit_code::ExitCode};
use std::env; use std::env;
use std::{env::set_current_dir, path::PathBuf}; use std::{env::set_current_dir, path::PathBuf};
pub fn cd(args: &Vec<String>) -> ExitCode { type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
pub struct Builtin {
function: BuiltinFunction,
args: Vec<String>,
}
impl Builtin {
pub fn new(argv: &Vec<String>) -> Result<Self, NoSuchBuiltinError> {
let mut args = argv.clone();
let program = args.remove(0);
if program == "cd" {
Ok(Self { function: cd, args })
} else {
Err(NoSuchBuiltinError)
}
}
}
impl Command for Builtin {
fn spawn(&mut self) -> ExitCode {
(self.function)(&self.args)
}
}
#[derive(Debug)]
pub struct NoSuchBuiltinError;
fn cd(args: &Vec<String>) -> ExitCode {
let path: Option<PathBuf>; let path: Option<PathBuf>;
if !args.is_empty() { if !args.is_empty() {

View file

@ -1,6 +1,6 @@
use super::Builtin; use super::builtin::Builtin;
use super::unix_program::UnixProgram;
use super::Command; use super::Command;
use super::UnixProgram;
pub struct CommandBuilder { pub struct CommandBuilder {
argv: Vec<String>, argv: Vec<String>,

View file

@ -2,47 +2,34 @@ use crate::command::Command;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
pub struct CommandSequence { pub struct CommandSequence {
command: Option<Box<dyn Command>>, command: Box<dyn Command>,
next_command: Option<Box<CommandSequence>>, next_command: Option<Box<CommandSequence>>,
} }
impl CommandSequence { impl CommandSequence {
pub fn new() -> Self { pub fn new(command: Box<dyn Command>) -> Self {
Self { Self {
command: None, command,
next_command: None, next_command: None,
} }
} }
#[allow(dead_code)]
pub fn add(&mut self, command: Box<dyn Command>) { pub fn add(&mut self, command: Box<dyn Command>) {
if self.command.is_none() { if self.next_command.is_none() {
self.command = Some(command); self.next_command = Some(Box::new(Self::new(command)));
} else if self.next_command.is_none() {
let new_command = Self {
command: Some(command),
next_command: None,
};
self.next_command = Some(Box::new(new_command));
} else { } else {
self.next_command.as_mut().unwrap().add(command); self.next_command.as_mut().unwrap().add(command);
} }
} }
pub fn is_empty(&self) -> bool {
self.command.is_none()
}
} }
impl Command for CommandSequence { impl Command for CommandSequence {
fn spawn(&mut self) -> ExitCode { fn spawn(&mut self) -> ExitCode {
let mut exit_code = ExitCode::success(); let mut exit_code = self.command.spawn();
if self.command.is_some() { if self.next_command.is_some() {
exit_code = self.command.as_mut().unwrap().spawn(); exit_code = self.next_command.as_mut().unwrap().spawn();
if self.next_command.is_some() {
exit_code = self.next_command.as_mut().unwrap().spawn();
}
} }
exit_code exit_code

View file

@ -1,94 +1,10 @@
mod builtins; pub mod builtin;
pub mod command_builder; pub mod command_builder;
pub mod command_sequence; pub mod command_sequence;
pub mod unix_program;
use crate::error::print_error;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
#[derive(Debug)]
pub struct NoSuchBuiltinError;
pub trait Command { pub trait Command {
fn spawn(&mut self) -> ExitCode; fn spawn(&mut self) -> ExitCode;
} }
pub struct Builtin {
function: BuiltinFunction,
args: Vec<String>,
}
impl Builtin {
pub fn new(argv: &Vec<String>) -> Result<Self, NoSuchBuiltinError> {
let mut args = argv.clone();
let program = args.remove(0);
if program == "cd" {
Ok(Self {
function: builtins::cd,
args,
})
} else {
Err(NoSuchBuiltinError)
}
}
}
impl Command for Builtin {
fn spawn(&mut self) -> ExitCode {
(self.function)(&self.args)
}
}
pub struct UnixProgram {
command: std::process::Command,
}
impl UnixProgram {
pub fn new(argv: &Vec<String>) -> Self {
let mut argv = argv.clone();
let program = argv.remove(0);
let mut command = std::process::Command::new(&program);
command.args(argv);
Self { command }
}
}
impl Command for UnixProgram {
fn spawn(&mut self) -> ExitCode {
let handle = self.command.spawn();
if handle.is_ok() {
let exit_code = handle
.unwrap()
.wait()
.expect("error waiting for the child")
.code()
.unwrap_or(1);
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(u8::MAX),
};
exit_code
} else {
let message = format!(
"{}: command not found",
self.command.get_program().to_str().unwrap()
);
print_error(message.as_str());
ExitCode::not_found()
}
}
}
struct Nothing;
impl Command for Nothing {
fn spawn(&mut self) -> ExitCode {
ExitCode::success()
}
}

View file

@ -0,0 +1,47 @@
use crate::command::Command;
use crate::error::print_error;
use crate::exit_code::ExitCode;
pub struct UnixProgram {
command: std::process::Command,
}
impl UnixProgram {
pub fn new(argv: &Vec<String>) -> Self {
let mut argv = argv.clone();
let program = argv.remove(0);
let mut command = std::process::Command::new(&program);
command.args(argv);
Self { command }
}
}
impl Command for UnixProgram {
fn spawn(&mut self) -> ExitCode {
let handle = self.command.spawn();
if handle.is_ok() {
let exit_code = handle
.unwrap()
.wait()
.expect("error waiting for the child")
.code()
.unwrap_or(1);
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(u8::MAX),
};
exit_code
} else {
let message = format!(
"{}: command not found",
self.command.get_program().to_str().unwrap()
);
print_error(message.as_str());
ExitCode::not_found()
}
}
}

View file

@ -1,7 +1,7 @@
use crate::command::Command; use crate::command::Command;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
use crate::interface::{get_user_input, print_prompt}; use crate::interface::{get_user_input, print_prompt};
use crate::parser::parse; 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,10 +17,10 @@ pub fn run() {
let user_input = get_user_input(); let user_input = get_user_input();
if user_input.is_some() { if user_input.is_some() {
let mut command_sequence = parse(user_input.unwrap()); let command_sequence = parse_command_line(user_input.unwrap());
if !command_sequence.is_empty() { if command_sequence.is_some() {
current_exit_code = command_sequence.spawn(); current_exit_code = command_sequence.unwrap().spawn();
} }
} else { } else {
println!(); println!();

View file

@ -17,7 +17,7 @@ fn parse_quote(characters: &mut Vec<char>) -> Result<String, UnmatchedQuoteError
} }
} }
fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> { fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
if characters.is_empty() { if characters.is_empty() {
vec![] vec![]
} else { } else {
@ -26,45 +26,34 @@ fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<Strin
if current_char.is_whitespace() { if current_char.is_whitespace() {
if !current_arg.is_empty() { if !current_arg.is_empty() {
let mut argv: Vec<String> = vec![current_arg.clone()]; let mut argv: Vec<String> = vec![current_arg.clone()];
argv.append(&mut parse_main(characters, &mut String::from(""))); argv.append(&mut build_argv(characters, &mut String::from("")));
argv argv
} else { } else {
parse_main(characters, current_arg) build_argv(characters, current_arg)
} }
/*
} else if current_char == '\\' {
current_arg.push(characters.pop().unwrap());
parse_main(characters, current_arg)
*/
} else if current_char == '\'' { } else if current_char == '\'' {
let mut argv = vec![parse_quote(characters).unwrap()]; let mut argv = vec![parse_quote(characters).unwrap()];
argv.append(&mut parse_main(characters, &mut String::default())); argv.append(&mut build_argv(characters, &mut String::default()));
argv argv
} else if current_char == ';' {
vec![]
} else { } else {
current_arg.push(current_char); current_arg.push(current_char);
parse_main(characters, current_arg) build_argv(characters, current_arg)
} }
} }
} }
pub fn parse(line: String) -> CommandSequence { pub fn parse_command_line(line: String) -> Option<CommandSequence> {
let mut characters: Vec<char> = line.chars().rev().collect(); let mut characters: Vec<char> = line.chars().rev().collect();
let mut command_sequence = CommandSequence::new(); let argv = build_argv(&mut characters, &mut String::default());
while !characters.is_empty() { if !argv.is_empty() {
let argv = parse_main(&mut characters, &mut String::default()); let command = CommandBuilder::new(argv).build();
Some(CommandSequence::new(command))
if !argv.is_empty() { } else {
let command = CommandBuilder::new(argv).build(); None
command_sequence.add(command);
}
} }
command_sequence
} }
#[derive(Debug)] #[derive(Debug)]