Compare commits
14 commits
Author | SHA1 | Date | |
---|---|---|---|
d7dda4c734 | |||
8c517ae8c7 | |||
a05ce8981c | |||
13555cafb3 | |||
006dc28466 | |||
6c570cf724 | |||
c1171aa0b2 | |||
fb09cfc8c6 | |||
1e72ff63f8 | |||
c7e9b19ac1 | |||
5fca142fea | |||
8906ac7c70 | |||
74fb3573be | |||
2337bd3127 |
14 changed files with 273 additions and 188 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "rash"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rash"
|
||||
version = "0.6.0"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
112
src/command/builtin.rs
Normal file
112
src/command/builtin.rs
Normal file
|
@ -0,0 +1,112 @@
|
|||
use super::Command;
|
||||
use crate::error;
|
||||
use crate::error::CdError;
|
||||
use crate::error::IllegalNumberError;
|
||||
use crate::exit_code::ExitCode;
|
||||
use crate::variables::Variables;
|
||||
|
||||
use std::{env::set_current_dir, path::PathBuf};
|
||||
|
||||
type BuiltinFunction = fn(&Vec<String>, &mut Variables, &mut ExitCode);
|
||||
|
||||
fn cd(args: &Vec<String>, variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
let path: Option<PathBuf>;
|
||||
|
||||
if !args.is_empty() {
|
||||
path = Some(PathBuf::from(&args[0]));
|
||||
} else {
|
||||
path = match variables.get("HOME").as_str() {
|
||||
"" => None,
|
||||
path => Some(PathBuf::from(path)),
|
||||
};
|
||||
}
|
||||
|
||||
exit_code.set_success();
|
||||
if path.is_some() {
|
||||
let path = path.unwrap();
|
||||
let result = set_current_dir(&path);
|
||||
|
||||
if result.is_err() {
|
||||
let error = CdError::new(&args[0]);
|
||||
error::print(Box::new(error));
|
||||
exit_code.set(2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn colon(_args: &Vec<String>, _variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
exit_code.set_success();
|
||||
}
|
||||
|
||||
fn exit(args: &Vec<String>, _variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
if !args.is_empty() {
|
||||
let raw_exit_code = match args[0].parse::<usize>() {
|
||||
Ok(parsed) => Ok(u8::try_from(parsed % 256).unwrap()),
|
||||
Err(_e) => Err(IllegalNumberError::new("exit", args[0].as_str())),
|
||||
};
|
||||
|
||||
if raw_exit_code.is_ok() {
|
||||
exit_code.set(raw_exit_code.unwrap());
|
||||
} else {
|
||||
error::print(Box::new(raw_exit_code.unwrap_err()));
|
||||
exit_code.set(2);
|
||||
()
|
||||
}
|
||||
}
|
||||
|
||||
crate::control::exit(exit_code);
|
||||
}
|
||||
|
||||
fn unset(args: &Vec<String>, variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
for variable_name in args.iter() {
|
||||
variables.unset(variable_name);
|
||||
}
|
||||
|
||||
exit_code.set_success();
|
||||
}
|
||||
|
||||
fn export(args: &Vec<String>, variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
for variable in args.iter() {
|
||||
let assignation: Vec<&str> = variable.split('=').collect();
|
||||
|
||||
if assignation.len() == 2 {
|
||||
variables.set(&assignation[0], &assignation[1]);
|
||||
}
|
||||
|
||||
variables.export(&assignation[0]);
|
||||
}
|
||||
|
||||
exit_code.set_success();
|
||||
}
|
||||
|
||||
pub struct Builtin {
|
||||
function: BuiltinFunction,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
impl Builtin {
|
||||
pub fn new(argv: &Vec<String>) -> Result<Self, ()> {
|
||||
let mut args = argv.clone();
|
||||
let program = args.remove(0);
|
||||
|
||||
let function: Option<BuiltinFunction> = match program.as_str() {
|
||||
"cd" => Some(cd),
|
||||
"exit" => Some(exit),
|
||||
"unset" => Some(unset),
|
||||
"export" => Some(export),
|
||||
":" => Some(colon),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
match function {
|
||||
Some(function) => Ok(Self { function, args }),
|
||||
None => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Command for Builtin {
|
||||
fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
(self.function)(&self.args, variables, exit_code)
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
use crate::error;
|
||||
use crate::error::CdError;
|
||||
use crate::exit_code::ExitCode;
|
||||
|
||||
use std::env;
|
||||
use std::{env::set_current_dir, path::PathBuf};
|
||||
|
||||
pub(super) fn cd(args: &Vec<String>) -> ExitCode {
|
||||
let path: Option<PathBuf>;
|
||||
|
||||
if !args.is_empty() {
|
||||
path = Some(PathBuf::from(&args[0]));
|
||||
} else {
|
||||
path = match env::var("HOME") {
|
||||
Ok(var) => Some(PathBuf::from(var)),
|
||||
Err(_e) => None,
|
||||
};
|
||||
}
|
||||
|
||||
let exit_code: ExitCode;
|
||||
if path.is_some() {
|
||||
let path = path.unwrap();
|
||||
let result = set_current_dir(&path);
|
||||
|
||||
if result.is_err() {
|
||||
let error = CdError::new(&args[0]);
|
||||
error::print(Box::new(error));
|
||||
exit_code = ExitCode::new(2);
|
||||
} else {
|
||||
exit_code = ExitCode::success();
|
||||
}
|
||||
} else {
|
||||
exit_code = ExitCode::success();
|
||||
}
|
||||
|
||||
exit_code
|
||||
}
|
||||
|
||||
pub(super) fn colon(_args: &Vec<String>) -> ExitCode {
|
||||
ExitCode::success()
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
use super::Builtin;
|
||||
use super::Command;
|
||||
use super::UnixProgram;
|
||||
|
||||
pub struct CommandBuilder {
|
||||
argv: Vec<String>,
|
||||
}
|
||||
|
||||
impl CommandBuilder {
|
||||
pub fn new(argv: Vec<String>) -> Self {
|
||||
Self { argv }
|
||||
}
|
||||
|
||||
pub fn build(&self) -> Box<dyn Command> {
|
||||
let builtin = Builtin::new(&self.argv);
|
||||
let command: Box<dyn Command>;
|
||||
|
||||
if builtin.is_err() {
|
||||
command = Box::from(UnixProgram::new(&self.argv));
|
||||
} else {
|
||||
command = Box::from(builtin.unwrap());
|
||||
}
|
||||
|
||||
command
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
use super::Command;
|
||||
use crate::exit_code::ExitCode;
|
||||
use crate::variables::Variables;
|
||||
|
||||
pub struct CommandSequence {
|
||||
command: Option<Box<dyn Command>>,
|
||||
|
@ -34,17 +35,16 @@ impl CommandSequence {
|
|||
}
|
||||
|
||||
impl Command for CommandSequence {
|
||||
fn spawn(&mut self) -> ExitCode {
|
||||
let mut exit_code = ExitCode::success();
|
||||
|
||||
fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
if self.command.is_some() {
|
||||
exit_code = self.command.as_mut().unwrap().spawn();
|
||||
self.command.as_mut().unwrap().spawn(variables, exit_code);
|
||||
|
||||
if self.next_command.is_some() {
|
||||
exit_code = self.next_command.as_mut().unwrap().spawn();
|
||||
self.next_command
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.spawn(variables, exit_code);
|
||||
}
|
||||
}
|
||||
|
||||
exit_code
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,90 +1,35 @@
|
|||
mod builtins;
|
||||
pub mod command_builder;
|
||||
pub mod builtin;
|
||||
pub mod command_sequence;
|
||||
pub mod unix_program;
|
||||
|
||||
use crate::error;
|
||||
use crate::error::CommandNotFoundError;
|
||||
use crate::exit_code::ExitCode;
|
||||
|
||||
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NoSuchBuiltinError;
|
||||
use crate::variables::Variables;
|
||||
use builtin::Builtin;
|
||||
use unix_program::UnixProgram;
|
||||
|
||||
pub trait Command {
|
||||
fn spawn(&mut self) -> ExitCode;
|
||||
fn spawn(&mut self, variables: &mut Variables, exit_code: &mut ExitCode);
|
||||
}
|
||||
|
||||
pub struct Builtin {
|
||||
function: BuiltinFunction,
|
||||
args: Vec<String>,
|
||||
pub struct CommandBuilder {
|
||||
argv: Vec<String>,
|
||||
}
|
||||
|
||||
impl Builtin {
|
||||
pub fn new(argv: &Vec<String>) -> Result<Self, NoSuchBuiltinError> {
|
||||
let mut args = argv.clone();
|
||||
let program = args.remove(0);
|
||||
impl CommandBuilder {
|
||||
pub fn new(argv: Vec<String>) -> Self {
|
||||
Self { argv }
|
||||
}
|
||||
|
||||
let function: Option<BuiltinFunction> = match program.as_str() {
|
||||
"cd" => Some(builtins::cd),
|
||||
":" => Some(builtins::colon),
|
||||
_ => None,
|
||||
};
|
||||
pub fn build(&self) -> Box<dyn Command> {
|
||||
let builtin = Builtin::new(&self.argv);
|
||||
let command: Box<dyn Command>;
|
||||
|
||||
if function.is_some() {
|
||||
Ok(Self {
|
||||
function: function.unwrap(),
|
||||
args,
|
||||
})
|
||||
if builtin.is_err() {
|
||||
command = Box::from(UnixProgram::new(&self.argv));
|
||||
} else {
|
||||
Err(NoSuchBuiltinError)
|
||||
}
|
||||
}
|
||||
command = Box::from(builtin.unwrap());
|
||||
}
|
||||
|
||||
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 error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
|
||||
error::print(Box::new(error));
|
||||
ExitCode::not_found()
|
||||
}
|
||||
command
|
||||
}
|
||||
}
|
||||
|
|
44
src/command/unix_program.rs
Normal file
44
src/command/unix_program.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use super::Command;
|
||||
use crate::error;
|
||||
use crate::error::CommandNotFoundError;
|
||||
use crate::exit_code::ExitCode;
|
||||
use crate::variables::Variables;
|
||||
|
||||
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, _variables: &mut Variables, exit_code: &mut ExitCode) {
|
||||
let handle = self.command.spawn();
|
||||
|
||||
if handle.is_ok() {
|
||||
let raw_exit_code = handle
|
||||
.unwrap()
|
||||
.wait()
|
||||
.expect("error waiting for the child")
|
||||
.code()
|
||||
.unwrap_or(1);
|
||||
|
||||
exit_code.set(match u8::try_from(raw_exit_code) {
|
||||
Ok(code) => code,
|
||||
Err(_e) => u8::MAX,
|
||||
});
|
||||
} else {
|
||||
let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
|
||||
error::print(Box::new(error));
|
||||
exit_code.set_command_not_found();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,28 +4,27 @@ use crate::interface::get_user_input;
|
|||
use crate::parser::parse;
|
||||
use crate::variables::Variables;
|
||||
|
||||
fn exit(code: &ExitCode) {
|
||||
let code = i32::from(code.get());
|
||||
pub fn exit(exit_code: &mut ExitCode) {
|
||||
println!("exit");
|
||||
std::process::exit(code);
|
||||
std::process::exit(i32::from(exit_code.get()));
|
||||
}
|
||||
|
||||
pub fn run() {
|
||||
let mut current_exit_code = ExitCode::new(0);
|
||||
let mut current_exit_code = ExitCode::new();
|
||||
let mut variables = Variables::new();
|
||||
|
||||
loop {
|
||||
let user_input = get_user_input();
|
||||
let user_input = get_user_input("$ ");
|
||||
|
||||
if user_input.is_some() {
|
||||
let mut command_sequence = parse(user_input.unwrap(), &variables);
|
||||
|
||||
if !command_sequence.is_empty() {
|
||||
current_exit_code = command_sequence.spawn();
|
||||
command_sequence.spawn(&mut variables, &mut current_exit_code);
|
||||
}
|
||||
} else {
|
||||
println!();
|
||||
exit(¤t_exit_code);
|
||||
exit(&mut current_exit_code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
28
src/error.rs
28
src/error.rs
|
@ -5,6 +5,7 @@ use std::path::Path;
|
|||
|
||||
type Error = Box<dyn Display>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CdError {
|
||||
entry_name: String,
|
||||
}
|
||||
|
@ -23,6 +24,7 @@ impl Display for CdError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CommandNotFoundError {
|
||||
command: String,
|
||||
}
|
||||
|
@ -41,6 +43,7 @@ impl Display for CommandNotFoundError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct UnmatchedQuoteError {
|
||||
index: usize,
|
||||
}
|
||||
|
@ -51,6 +54,31 @@ impl Display for UnmatchedQuoteError {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct IllegalNumberError {
|
||||
command_invoked: String,
|
||||
illegal_number: String,
|
||||
}
|
||||
|
||||
impl IllegalNumberError {
|
||||
pub fn new(command_invoked: &str, illegal_number: &str) -> Self {
|
||||
Self {
|
||||
command_invoked: String::from(command_invoked),
|
||||
illegal_number: String::from(illegal_number),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for IllegalNumberError {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}: '{}' is not a valid number",
|
||||
self.command_invoked, self.illegal_number
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print(error: Error) {
|
||||
let name = env::args().next().unwrap_or(String::from("rash"));
|
||||
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
||||
|
|
|
@ -3,19 +3,23 @@ pub struct ExitCode {
|
|||
}
|
||||
|
||||
impl ExitCode {
|
||||
pub fn new(exit_code: u8) -> Self {
|
||||
Self { exit_code }
|
||||
pub fn new() -> Self {
|
||||
Self { exit_code: 0 }
|
||||
}
|
||||
|
||||
pub fn get(&self) -> u8 {
|
||||
self.exit_code
|
||||
}
|
||||
|
||||
pub fn not_found() -> Self {
|
||||
Self { exit_code: 127 }
|
||||
pub fn set(&mut self, new_code: u8) {
|
||||
self.exit_code = new_code;
|
||||
}
|
||||
|
||||
pub fn success() -> Self {
|
||||
Self { exit_code: 0 }
|
||||
pub fn set_success(&mut self) {
|
||||
self.exit_code = 0;
|
||||
}
|
||||
|
||||
pub fn set_command_not_found(&mut self) {
|
||||
self.exit_code = 127;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,13 +1,9 @@
|
|||
use std::io::Write;
|
||||
use std::io::{stdin, stdout};
|
||||
|
||||
fn print_prompt() {
|
||||
print!("$ ");
|
||||
pub fn get_user_input(prompt: &str) -> Option<String> {
|
||||
print!("{}", prompt);
|
||||
stdout().flush().unwrap();
|
||||
}
|
||||
|
||||
pub fn get_user_input() -> Option<String> {
|
||||
print_prompt();
|
||||
|
||||
let mut user_input = String::new();
|
||||
|
||||
|
|
|
@ -1,49 +1,73 @@
|
|||
use crate::command::command_builder::CommandBuilder;
|
||||
use crate::command::command_sequence::CommandSequence;
|
||||
use crate::command::CommandBuilder;
|
||||
use crate::interface::get_user_input;
|
||||
use crate::variables::Variables;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
fn parse_variable(characters: &mut Vec<char>, variables: &Variables) -> String {
|
||||
fn parse_variable(characters: &mut VecDeque<char>, variables: &Variables) -> String {
|
||||
if characters.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
let mut current_char = characters.pop().unwrap();
|
||||
let mut current_char = characters.pop_front().unwrap();
|
||||
let mut var_name = String::new();
|
||||
|
||||
while !characters.is_empty() && !current_char.is_whitespace() {
|
||||
var_name.push(current_char);
|
||||
current_char = characters.pop().unwrap();
|
||||
current_char = characters.pop_front().unwrap();
|
||||
}
|
||||
|
||||
if current_char.is_whitespace() {
|
||||
characters.push_front(current_char);
|
||||
}
|
||||
|
||||
variables.get(var_name.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_quote(characters: &mut Vec<char>) -> String {
|
||||
fn parse_quote(characters: &mut VecDeque<char>) -> String {
|
||||
let mut quoted = String::new();
|
||||
|
||||
if !characters.is_empty() {
|
||||
let mut current_char = characters.pop().unwrap();
|
||||
let mut current_char = characters.pop_front().unwrap();
|
||||
|
||||
while !characters.is_empty() && current_char != '\'' {
|
||||
while current_char != '\'' {
|
||||
quoted.push(current_char);
|
||||
current_char = characters.pop().unwrap();
|
||||
|
||||
if characters.is_empty() {
|
||||
let mut new_characters: VecDeque<char> =
|
||||
get_user_input("> ").unwrap().chars().collect();
|
||||
characters.append(&mut new_characters);
|
||||
}
|
||||
|
||||
current_char = characters.pop_front().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
quoted
|
||||
}
|
||||
|
||||
fn parse_backslash(characters: &mut Vec<char>) -> String {
|
||||
let escaped_char = match characters.is_empty() {
|
||||
false => String::from(characters.pop().unwrap()),
|
||||
true => String::new(),
|
||||
};
|
||||
fn parse_backslash(characters: &mut VecDeque<char>) -> String {
|
||||
let next_character: char;
|
||||
|
||||
escaped_char
|
||||
if !characters.is_empty() {
|
||||
next_character = characters.pop_front().unwrap();
|
||||
|
||||
if next_character == '\n' {
|
||||
let mut new_characters: VecDeque<char> =
|
||||
get_user_input("> ").unwrap().chars().collect();
|
||||
characters.append(&mut new_characters);
|
||||
|
||||
String::new()
|
||||
} else {
|
||||
String::from(next_character)
|
||||
}
|
||||
} else {
|
||||
String::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_main(
|
||||
characters: &mut Vec<char>,
|
||||
characters: &mut VecDeque<char>,
|
||||
current_arg: &mut String,
|
||||
variables: &Variables,
|
||||
) -> Vec<String> {
|
||||
|
@ -56,7 +80,7 @@ fn parse_main(
|
|||
|
||||
last_arg
|
||||
} else {
|
||||
let current_char = characters.pop().unwrap();
|
||||
let current_char = characters.pop_front().unwrap();
|
||||
|
||||
if current_char.is_whitespace() {
|
||||
if !current_arg.is_empty() {
|
||||
|
@ -92,7 +116,7 @@ fn parse_main(
|
|||
}
|
||||
|
||||
pub fn parse(line: String, variables: &Variables) -> CommandSequence {
|
||||
let mut characters: Vec<char> = line.chars().rev().collect();
|
||||
let mut characters: VecDeque<char> = line.chars().collect();
|
||||
let mut command_sequence = CommandSequence::new();
|
||||
|
||||
while !characters.is_empty() {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
use std::env::{self, args};
|
||||
use std::env;
|
||||
|
||||
pub struct Variables {
|
||||
variables: HashMap<String, String>,
|
||||
|
|
Loading…
Reference in a new issue