Compare commits
No commits in common. "af6e95f703b7b28d511feef0dbe959c50d8ef4c1" and "142d7211bc786502b4d1c5491aef8f46c1ca1e69" have entirely different histories.
af6e95f703
...
142d7211bc
8 changed files with 25 additions and 53 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "rash"
|
||||
version = "0.5.0"
|
||||
version = "0.4.0"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rash"
|
||||
version = "0.5.0"
|
||||
version = "0.4.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
@ -7,15 +7,14 @@ I want to learn Rust and see how much effort I can put into making an interopera
|
|||
# Roadmap
|
||||
|
||||
- [x] Execute commands
|
||||
- [x] Parse commands
|
||||
- [ ] Parse commands
|
||||
- [ ] Variables
|
||||
- [ ] Environment variables
|
||||
- [ ] Builtins
|
||||
- [x] `cd`
|
||||
- [ ] `cd`
|
||||
- [ ] `set`
|
||||
- [ ] `alias`
|
||||
- [ ] `type`
|
||||
- [ ] Command-line flags
|
||||
- [ ] PS1
|
||||
- [ ] Execute code from a file
|
||||
- [ ] Clean error handling
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::{error::print_error, exit_code::ExitCode};
|
|||
use std::env;
|
||||
use std::{env::set_current_dir, path::PathBuf};
|
||||
|
||||
pub(super) fn cd(args: &Vec<String>) -> ExitCode {
|
||||
pub fn cd(args: &Vec<String>) -> ExitCode {
|
||||
let path: Option<PathBuf>;
|
||||
|
||||
if !args.is_empty() {
|
||||
|
@ -31,7 +31,3 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
|
|||
|
||||
exit_code
|
||||
}
|
||||
|
||||
pub(super) fn colon(_args: &Vec<String>) -> ExitCode {
|
||||
ExitCode::success()
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use super::Command;
|
||||
use crate::command::Command;
|
||||
use crate::exit_code::ExitCode;
|
||||
|
||||
pub struct CommandSequence {
|
||||
|
|
|
@ -24,15 +24,9 @@ impl Builtin {
|
|||
let mut args = argv.clone();
|
||||
let program = args.remove(0);
|
||||
|
||||
let function: Option<BuiltinFunction> = match program.as_str() {
|
||||
"cd" => Some(builtins::cd),
|
||||
":" => Some(builtins::colon),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if function.is_some() {
|
||||
if program == "cd" {
|
||||
Ok(Self {
|
||||
function: function.unwrap(),
|
||||
function: builtins::cd,
|
||||
args,
|
||||
})
|
||||
} else {
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn print_error(message: &str) {
|
||||
let name = env::args().next().unwrap_or(String::from("rash"));
|
||||
let name = std::env::args().next().unwrap();
|
||||
let name = Path::new(&name).file_name().unwrap().to_str().unwrap();
|
||||
|
||||
eprintln!("{}: {}", name, message);
|
||||
|
|
|
@ -1,29 +1,20 @@
|
|||
use crate::command::command_builder::CommandBuilder;
|
||||
use crate::command::command_sequence::CommandSequence;
|
||||
|
||||
fn parse_quote(characters: &mut Vec<char>) -> String {
|
||||
let mut quoted = String::default();
|
||||
|
||||
if !characters.is_empty() {
|
||||
fn parse_quote(characters: &mut Vec<char>) -> Result<String, UnmatchedQuoteError> {
|
||||
if characters.is_empty() {
|
||||
Err(UnmatchedQuoteError)
|
||||
} else {
|
||||
let mut quoted_arg = String::default();
|
||||
let mut current_char = characters.pop().unwrap();
|
||||
|
||||
while !characters.is_empty() && current_char != '\'' {
|
||||
quoted.push(current_char);
|
||||
quoted_arg.push(current_char);
|
||||
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> {
|
||||
|
@ -35,31 +26,24 @@ fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<Strin
|
|||
if current_char.is_whitespace() {
|
||||
if !current_arg.is_empty() {
|
||||
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::from("")));
|
||||
|
||||
argv
|
||||
} else {
|
||||
parse_main(characters, current_arg)
|
||||
}
|
||||
/*
|
||||
} else if current_char == '\\' {
|
||||
let escaped_char = parse_backslash(characters);
|
||||
|
||||
if escaped_char.is_some() {
|
||||
current_arg.push(escaped_char.unwrap());
|
||||
}
|
||||
|
||||
current_arg.push(characters.pop().unwrap());
|
||||
parse_main(characters, current_arg)
|
||||
*/
|
||||
} else if current_char == '\'' {
|
||||
current_arg.push_str(parse_quote(characters).as_str());
|
||||
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());
|
||||
}
|
||||
let mut argv = vec![parse_quote(characters).unwrap()];
|
||||
argv.append(&mut parse_main(characters, &mut String::default()));
|
||||
|
||||
argv
|
||||
} else if current_char == ';' {
|
||||
vec![]
|
||||
} else {
|
||||
current_arg.push(current_char);
|
||||
parse_main(characters, current_arg)
|
||||
|
|
Loading…
Reference in a new issue