Compare commits
14 commits
af6e95f703
...
d162a24db0
Author | SHA1 | Date | |
---|---|---|---|
d162a24db0 | |||
a50bea4c60 | |||
9d53bdba0c | |||
a441fc9cb3 | |||
fa9033beea | |||
eaa9622446 | |||
5584f49c87 | |||
fdcaa99feb | |||
1fc2c8adbd | |||
1270576444 | |||
a16ed0e030 | |||
abd857f0e4 | |||
5df428a364 | |||
1e4f8cadd6 |
11 changed files with 186 additions and 60 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.5.0"
|
version = "0.6.0"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "rash"
|
name = "rash"
|
||||||
version = "0.5.0"
|
version = "0.6.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
|
||||||
|
|
|
@ -12,6 +12,7 @@ I want to learn Rust and see how much effort I can put into making an interopera
|
||||||
- [ ] Environment variables
|
- [ ] Environment variables
|
||||||
- [ ] Builtins
|
- [ ] Builtins
|
||||||
- [x] `cd`
|
- [x] `cd`
|
||||||
|
- [x] `:`
|
||||||
- [ ] `set`
|
- [ ] `set`
|
||||||
- [ ] `alias`
|
- [ ] `alias`
|
||||||
- [ ] `type`
|
- [ ] `type`
|
||||||
|
@ -19,3 +20,4 @@ I want to learn Rust and see how much effort I can put into making an interopera
|
||||||
- [ ] PS1
|
- [ ] PS1
|
||||||
- [ ] Execute code from a file
|
- [ ] Execute code from a file
|
||||||
- [ ] Clean error handling
|
- [ ] Clean error handling
|
||||||
|
- [ ] Override Ctrl-C
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
use crate::{error::print_error, exit_code::ExitCode};
|
use crate::error;
|
||||||
|
use crate::error::CdError;
|
||||||
|
use crate::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};
|
||||||
|
@ -17,16 +19,18 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
|
||||||
|
|
||||||
let exit_code: ExitCode;
|
let exit_code: ExitCode;
|
||||||
if path.is_some() {
|
if path.is_some() {
|
||||||
exit_code = match set_current_dir(path.unwrap()) {
|
let path = path.unwrap();
|
||||||
Ok(()) => ExitCode::success(),
|
let result = set_current_dir(&path);
|
||||||
Err(_e) => ExitCode::new(2),
|
|
||||||
};
|
if result.is_err() {
|
||||||
|
let error = CdError::new(&args[0]);
|
||||||
|
error::print(Box::new(error));
|
||||||
|
exit_code = ExitCode::new(2);
|
||||||
} else {
|
} else {
|
||||||
exit_code = ExitCode::success();
|
exit_code = ExitCode::success();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
if exit_code.get() != 0 {
|
exit_code = ExitCode::success();
|
||||||
print_error("lol");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
|
|
|
@ -2,7 +2,8 @@ mod builtins;
|
||||||
pub mod command_builder;
|
pub mod command_builder;
|
||||||
pub mod command_sequence;
|
pub mod command_sequence;
|
||||||
|
|
||||||
use crate::error::print_error;
|
use crate::error;
|
||||||
|
use crate::error::CommandNotFoundError;
|
||||||
use crate::exit_code::ExitCode;
|
use crate::exit_code::ExitCode;
|
||||||
|
|
||||||
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
|
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
|
||||||
|
@ -81,20 +82,9 @@ impl Command for UnixProgram {
|
||||||
|
|
||||||
exit_code
|
exit_code
|
||||||
} else {
|
} else {
|
||||||
let message = format!(
|
let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
|
||||||
"{}: command not found",
|
error::print(Box::new(error));
|
||||||
self.command.get_program().to_str().unwrap()
|
|
||||||
);
|
|
||||||
print_error(message.as_str());
|
|
||||||
ExitCode::not_found()
|
ExitCode::not_found()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Nothing;
|
|
||||||
|
|
||||||
impl Command for Nothing {
|
|
||||||
fn spawn(&mut self) -> ExitCode {
|
|
||||||
ExitCode::success()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
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;
|
||||||
use crate::parser::parse;
|
use crate::parser::parse;
|
||||||
|
use crate::variables::Variables;
|
||||||
|
|
||||||
fn exit(code: &ExitCode) {
|
fn exit(code: &ExitCode) {
|
||||||
let code = i32::from(code.get());
|
let code = i32::from(code.get());
|
||||||
|
@ -11,13 +12,13 @@ fn exit(code: &ExitCode) {
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let mut current_exit_code = ExitCode::new(0);
|
let mut current_exit_code = ExitCode::new(0);
|
||||||
|
let mut variables = Variables::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
print_prompt();
|
|
||||||
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 mut command_sequence = parse(user_input.unwrap(), &variables);
|
||||||
|
|
||||||
if !command_sequence.is_empty() {
|
if !command_sequence.is_empty() {
|
||||||
current_exit_code = command_sequence.spawn();
|
current_exit_code = command_sequence.spawn();
|
||||||
|
|
54
src/error.rs
54
src/error.rs
|
@ -1,9 +1,59 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fmt;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub fn print_error(message: &str) {
|
type Error = Box<dyn Display>;
|
||||||
|
|
||||||
|
pub struct CdError {
|
||||||
|
entry_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CdError {
|
||||||
|
pub fn new(entry_name: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
entry_name: String::from(entry_name),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for CdError {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "cd: no such directory as '{}'", self.entry_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CommandNotFoundError {
|
||||||
|
command: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandNotFoundError {
|
||||||
|
pub fn new(command: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
command: String::from(command),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for CommandNotFoundError {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "{}: command not found", self.command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UnmatchedQuoteError {
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for UnmatchedQuoteError {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
write!(f, "unmatched quote at character {}", self.index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print(error: Error) {
|
||||||
let name = env::args().next().unwrap_or(String::from("rash"));
|
let name = env::args().next().unwrap_or(String::from("rash"));
|
||||||
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
||||||
|
|
||||||
eprintln!("{}: {}", name, message);
|
eprintln!("{}: {}", name, error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,22 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::io::{stdin, stdout};
|
use std::io::{stdin, stdout};
|
||||||
|
|
||||||
pub fn print_prompt() {
|
fn print_prompt() {
|
||||||
print!("$ ");
|
print!("$ ");
|
||||||
stdout().flush().unwrap();
|
stdout().flush().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_input() -> Option<String> {
|
pub fn get_user_input() -> Option<String> {
|
||||||
|
print_prompt();
|
||||||
|
|
||||||
let mut user_input = String::new();
|
let mut user_input = String::new();
|
||||||
|
|
||||||
let bytes_read = stdin()
|
let bytes_read = stdin()
|
||||||
.read_line(&mut user_input)
|
.read_line(&mut user_input)
|
||||||
.expect("error reading user input");
|
.expect("error reading user input");
|
||||||
|
|
||||||
if bytes_read == 0 {
|
match bytes_read {
|
||||||
None
|
0 => None,
|
||||||
} else {
|
_ => Some(user_input),
|
||||||
Some(user_input)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ mod error;
|
||||||
mod exit_code;
|
mod exit_code;
|
||||||
mod interface;
|
mod interface;
|
||||||
mod parser;
|
mod parser;
|
||||||
|
mod variables;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
control::run();
|
control::run();
|
||||||
|
|
|
@ -1,8 +1,25 @@
|
||||||
use crate::command::command_builder::CommandBuilder;
|
use crate::command::command_builder::CommandBuilder;
|
||||||
use crate::command::command_sequence::CommandSequence;
|
use crate::command::command_sequence::CommandSequence;
|
||||||
|
use crate::variables::Variables;
|
||||||
|
|
||||||
|
fn parse_variable(characters: &mut Vec<char>, variables: &Variables) -> String {
|
||||||
|
if characters.is_empty() {
|
||||||
|
String::new()
|
||||||
|
} else {
|
||||||
|
let mut current_char = characters.pop().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();
|
||||||
|
}
|
||||||
|
|
||||||
|
variables.get(var_name.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_quote(characters: &mut Vec<char>) -> String {
|
fn parse_quote(characters: &mut Vec<char>) -> String {
|
||||||
let mut quoted = String::default();
|
let mut quoted = String::new();
|
||||||
|
|
||||||
if !characters.is_empty() {
|
if !characters.is_empty() {
|
||||||
let mut current_char = characters.pop().unwrap();
|
let mut current_char = characters.pop().unwrap();
|
||||||
|
@ -16,42 +33,49 @@ fn parse_quote(characters: &mut Vec<char>) -> String {
|
||||||
quoted
|
quoted
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_backslash(characters: &mut Vec<char>) -> Option<char> {
|
fn parse_backslash(characters: &mut Vec<char>) -> String {
|
||||||
let mut escaped_char: Option<char> = None;
|
let escaped_char = match characters.is_empty() {
|
||||||
|
false => String::from(characters.pop().unwrap()),
|
||||||
if !characters.is_empty() {
|
true => String::new(),
|
||||||
escaped_char = Some(characters.pop().unwrap());
|
};
|
||||||
}
|
|
||||||
|
|
||||||
escaped_char
|
escaped_char
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
|
fn parse_main(
|
||||||
|
characters: &mut Vec<char>,
|
||||||
|
current_arg: &mut String,
|
||||||
|
variables: &Variables,
|
||||||
|
) -> Vec<String> {
|
||||||
if characters.is_empty() {
|
if characters.is_empty() {
|
||||||
vec![]
|
let mut last_arg = vec![];
|
||||||
|
|
||||||
|
if !current_arg.is_empty() {
|
||||||
|
last_arg.push(current_arg.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
last_arg
|
||||||
} else {
|
} else {
|
||||||
let current_char = characters.pop().unwrap();
|
let current_char = characters.pop().unwrap();
|
||||||
|
|
||||||
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::default()));
|
argv.append(&mut parse_main(characters, &mut String::new(), variables));
|
||||||
|
|
||||||
argv
|
argv
|
||||||
} else {
|
} else {
|
||||||
parse_main(characters, current_arg)
|
parse_main(characters, current_arg, variables)
|
||||||
}
|
}
|
||||||
} else if current_char == '\\' {
|
} else if current_char == '\\' {
|
||||||
let escaped_char = parse_backslash(characters);
|
current_arg.push_str(parse_backslash(characters).as_str());
|
||||||
|
parse_main(characters, current_arg, variables)
|
||||||
if escaped_char.is_some() {
|
} else if current_char == '$' {
|
||||||
current_arg.push(escaped_char.unwrap());
|
current_arg.push_str(parse_variable(characters, variables).as_str());
|
||||||
}
|
parse_main(characters, current_arg, variables)
|
||||||
|
|
||||||
parse_main(characters, current_arg)
|
|
||||||
} else if current_char == '\'' {
|
} else if current_char == '\'' {
|
||||||
current_arg.push_str(parse_quote(characters).as_str());
|
current_arg.push_str(parse_quote(characters).as_str());
|
||||||
parse_main(characters, current_arg)
|
parse_main(characters, current_arg, variables)
|
||||||
} else if current_char == ';' {
|
} else if current_char == ';' {
|
||||||
let mut argv: Vec<String> = vec![];
|
let mut argv: Vec<String> = vec![];
|
||||||
|
|
||||||
|
@ -62,17 +86,17 @@ fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<Strin
|
||||||
argv
|
argv
|
||||||
} else {
|
} else {
|
||||||
current_arg.push(current_char);
|
current_arg.push(current_char);
|
||||||
parse_main(characters, current_arg)
|
parse_main(characters, current_arg, variables)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(line: String) -> CommandSequence {
|
pub fn parse(line: String, variables: &Variables) -> 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 mut command_sequence = CommandSequence::new();
|
||||||
|
|
||||||
while !characters.is_empty() {
|
while !characters.is_empty() {
|
||||||
let argv = parse_main(&mut characters, &mut String::default());
|
let argv = parse_main(&mut characters, &mut String::new(), variables);
|
||||||
|
|
||||||
if !argv.is_empty() {
|
if !argv.is_empty() {
|
||||||
let command = CommandBuilder::new(argv).build();
|
let command = CommandBuilder::new(argv).build();
|
||||||
|
@ -82,6 +106,3 @@ pub fn parse(line: String) -> CommandSequence {
|
||||||
|
|
||||||
command_sequence
|
command_sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct UnmatchedQuoteError;
|
|
||||||
|
|
56
src/variables.rs
Normal file
56
src/variables.rs
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env::{self, args};
|
||||||
|
|
||||||
|
pub struct Variables {
|
||||||
|
variables: HashMap<String, String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Variables {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
variables: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, key: &str) -> String {
|
||||||
|
let arg_index = String::from(key).parse::<usize>();
|
||||||
|
|
||||||
|
match arg_index {
|
||||||
|
Ok(index) => match env::args().nth(index) {
|
||||||
|
Some(value) => value,
|
||||||
|
None => String::new(),
|
||||||
|
},
|
||||||
|
Err(_e) => match self.variables.get(key) {
|
||||||
|
Some(value) => value.clone(),
|
||||||
|
None => match env::var(key) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_e) => String::new(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unset(&mut self, key: &str) {
|
||||||
|
let old_value = self.variables.remove(key);
|
||||||
|
|
||||||
|
if old_value.is_none() {
|
||||||
|
env::remove_var(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, key: &str, value: &str) {
|
||||||
|
self.variables
|
||||||
|
.insert(String::from(key), String::from(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn export(&mut self, key: &str) {
|
||||||
|
let var_to_export = self.variables.get(key);
|
||||||
|
|
||||||
|
if var_to_export.is_some() {
|
||||||
|
env::set_var(key, var_to_export.unwrap());
|
||||||
|
self.variables.remove(key);
|
||||||
|
} else {
|
||||||
|
env::set_var(key, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue