Compare commits
No commits in common. "a50bea4c6041f49db06b3ed11fd5265e60d64963" and "eaa9622446a9d8e9bc58917a8f69b698ce1388ec" have entirely different histories.
a50bea4c60
...
eaa9622446
5 changed files with 22 additions and 113 deletions
|
@ -2,7 +2,6 @@ use crate::command::Command;
|
||||||
use crate::exit_code::ExitCode;
|
use crate::exit_code::ExitCode;
|
||||||
use crate::interface::get_user_input;
|
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());
|
||||||
|
@ -12,13 +11,12 @@ 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 {
|
||||||
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(), &variables);
|
let mut command_sequence = parse(user_input.unwrap());
|
||||||
|
|
||||||
if !command_sequence.is_empty() {
|
if !command_sequence.is_empty() {
|
||||||
current_exit_code = command_sequence.spawn();
|
current_exit_code = command_sequence.spawn();
|
||||||
|
|
10
src/error.rs
10
src/error.rs
|
@ -41,16 +41,6 @@ impl Display for CommandNotFoundError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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();
|
||||||
|
|
|
@ -4,7 +4,6 @@ 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,22 +1,5 @@
|
||||||
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::new();
|
let mut quoted = String::new();
|
||||||
|
@ -33,49 +16,41 @@ fn parse_quote(characters: &mut Vec<char>) -> String {
|
||||||
quoted
|
quoted
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_backslash(characters: &mut Vec<char>) -> String {
|
fn parse_backslash(characters: &mut Vec<char>) -> Option<char> {
|
||||||
let escaped_char = match characters.is_empty() {
|
let escaped_char = match characters.is_empty() {
|
||||||
false => String::from(characters.pop().unwrap()),
|
false => Some(characters.pop().unwrap()),
|
||||||
true => String::new(),
|
true => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
escaped_char
|
escaped_char
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_main(
|
fn parse_main(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
|
||||||
characters: &mut Vec<char>,
|
|
||||||
current_arg: &mut String,
|
|
||||||
variables: &Variables,
|
|
||||||
) -> Vec<String> {
|
|
||||||
if characters.is_empty() {
|
if characters.is_empty() {
|
||||||
let mut last_arg = vec![];
|
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::new(), variables));
|
argv.append(&mut parse_main(characters, &mut String::new()));
|
||||||
|
|
||||||
argv
|
argv
|
||||||
} else {
|
} else {
|
||||||
parse_main(characters, current_arg, variables)
|
parse_main(characters, current_arg)
|
||||||
}
|
}
|
||||||
} else if current_char == '\\' {
|
} else if current_char == '\\' {
|
||||||
current_arg.push_str(parse_backslash(characters).as_str());
|
let escaped_char = parse_backslash(characters);
|
||||||
parse_main(characters, current_arg, variables)
|
|
||||||
} else if current_char == '$' {
|
if escaped_char.is_some() {
|
||||||
current_arg.push_str(parse_variable(characters, variables).as_str());
|
current_arg.push(escaped_char.unwrap());
|
||||||
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, variables)
|
parse_main(characters, current_arg)
|
||||||
} else if current_char == ';' {
|
} else if current_char == ';' {
|
||||||
let mut argv: Vec<String> = vec![];
|
let mut argv: Vec<String> = vec![];
|
||||||
|
|
||||||
|
@ -86,17 +61,17 @@ fn parse_main(
|
||||||
argv
|
argv
|
||||||
} else {
|
} else {
|
||||||
current_arg.push(current_char);
|
current_arg.push(current_char);
|
||||||
parse_main(characters, current_arg, variables)
|
parse_main(characters, current_arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse(line: String, variables: &Variables) -> CommandSequence {
|
pub fn parse(line: String) -> 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::new(), variables);
|
let argv = parse_main(&mut characters, &mut String::new());
|
||||||
|
|
||||||
if !argv.is_empty() {
|
if !argv.is_empty() {
|
||||||
let command = CommandBuilder::new(argv).build();
|
let command = CommandBuilder::new(argv).build();
|
||||||
|
@ -106,3 +81,6 @@ pub fn parse(line: String, variables: &Variables) -> CommandSequence {
|
||||||
|
|
||||||
command_sequence
|
command_sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct UnmatchedQuoteError;
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
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