Compare commits

..

15 commits

8 changed files with 53 additions and 25 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.5.0"

View file

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

@ -7,14 +7,15 @@ I want to learn Rust and see how much effort I can put into making an interopera
# Roadmap # Roadmap
- [x] Execute commands - [x] Execute commands
- [ ] Parse commands - [x] Parse commands
- [ ] Variables - [ ] Variables
- [ ] Environment variables - [ ] Environment variables
- [ ] Builtins - [ ] Builtins
- [ ] `cd` - [x] `cd`
- [ ] `set` - [ ] `set`
- [ ] `alias` - [ ] `alias`
- [ ] `type` - [ ] `type`
- [ ] Command-line flags - [ ] Command-line flags
- [ ] PS1 - [ ] PS1
- [ ] Execute code from a file - [ ] Execute code from a file
- [ ] Clean error handling

View file

@ -3,7 +3,7 @@ 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 { pub(super) fn cd(args: &Vec<String>) -> ExitCode {
let path: Option<PathBuf>; let path: Option<PathBuf>;
if !args.is_empty() { if !args.is_empty() {
@ -31,3 +31,7 @@ pub fn cd(args: &Vec<String>) -> ExitCode {
exit_code exit_code
} }
pub(super) fn colon(_args: &Vec<String>) -> ExitCode {
ExitCode::success()
}

View file

@ -1,4 +1,4 @@
use crate::command::Command; use super::Command;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
pub struct CommandSequence { pub struct CommandSequence {

View file

@ -24,9 +24,15 @@ impl Builtin {
let mut args = argv.clone(); let mut args = argv.clone();
let program = args.remove(0); let program = args.remove(0);
if program == "cd" { let function: Option<BuiltinFunction> = match program.as_str() {
"cd" => Some(builtins::cd),
":" => Some(builtins::colon),
_ => None,
};
if function.is_some() {
Ok(Self { Ok(Self {
function: builtins::cd, function: function.unwrap(),
args, args,
}) })
} else { } else {

View file

@ -1,7 +1,8 @@
use std::env;
use std::path::Path; use std::path::Path;
pub fn print_error(message: &str) { pub fn print_error(message: &str) {
let name = std::env::args().next().unwrap(); 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, message);

View file

@ -1,20 +1,29 @@
use crate::command::command_builder::CommandBuilder; use crate::command::command_builder::CommandBuilder;
use crate::command::command_sequence::CommandSequence; use crate::command::command_sequence::CommandSequence;
fn parse_quote(characters: &mut Vec<char>) -> Result<String, UnmatchedQuoteError> { fn parse_quote(characters: &mut Vec<char>) -> String {
if characters.is_empty() { let mut quoted = String::default();
Err(UnmatchedQuoteError)
} else { if !characters.is_empty() {
let mut quoted_arg = String::default();
let mut current_char = characters.pop().unwrap(); let mut current_char = characters.pop().unwrap();
while !characters.is_empty() && current_char != '\'' { while !characters.is_empty() && current_char != '\'' {
quoted_arg.push(current_char); quoted.push(current_char);
current_char = characters.pop().unwrap(); current_char = characters.pop().unwrap();
} }
Ok(quoted_arg)
} }
quoted
}
fn parse_backslash(characters: &mut Vec<char>) -> Option<char> {
let mut escaped_char: Option<char> = None;
if !characters.is_empty() {
escaped_char = Some(characters.pop().unwrap());
}
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) -> Vec<String> {
@ -26,24 +35,31 @@ 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 parse_main(characters, &mut String::default()));
argv argv
} else { } else {
parse_main(characters, current_arg) parse_main(characters, current_arg)
} }
/*
} else if current_char == '\\' { } else if current_char == '\\' {
current_arg.push(characters.pop().unwrap()); let escaped_char = parse_backslash(characters);
if escaped_char.is_some() {
current_arg.push(escaped_char.unwrap());
}
parse_main(characters, current_arg) parse_main(characters, current_arg)
*/
} else if current_char == '\'' { } else if current_char == '\'' {
let mut argv = vec![parse_quote(characters).unwrap()]; current_arg.push_str(parse_quote(characters).as_str());
argv.append(&mut parse_main(characters, &mut String::default())); parse_main(characters, current_arg)
} else if current_char == ';' {
let mut argv: Vec<String> = vec![];
if !current_arg.is_empty() {
argv.push(current_arg.clone());
}
argv argv
} else if current_char == ';' {
vec![]
} else { } else {
current_arg.push(current_char); current_arg.push(current_char);
parse_main(characters, current_arg) parse_main(characters, current_arg)