Compare commits
4 Commits
d1ce119192
...
ff79dae5cb
Author | SHA1 | Date |
---|---|---|
Lilly Rosaline | ff79dae5cb | 3 years ago |
Lilly Rosaline | f16073b955 | 3 years ago |
Lilly Rosaline | 315d97b358 | 3 years ago |
Lilly Rosaline | 952c01abfc | 3 years ago |
@ -1,140 +1,50 @@
|
|||||||
use std::str::Chars;
|
use std::str::Chars;
|
||||||
|
use std::iter::FromIterator;
|
||||||
|
use std::process::exit;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn tests() {
|
fn test_00() {
|
||||||
dbg!(parse_args("Hello world", 0x00));
|
dbg!(parse_args("Hello world", 0x00));
|
||||||
dbg!(parse_args("\"Hello world\'\"", 0x07));
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_01() {
|
||||||
|
dbg!(parse_args("Hello world", 0x01));
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_02() {
|
||||||
|
dbg!(parse_args("Hello worLd", 0x02));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parses the command line arguments into a list of parameters.
|
/// Parses the command line arguments into a list of parameters.
|
||||||
|
/// The following parsing modes can be utilized:
|
||||||
|
/// 00: Returns exactly what was inputted.
|
||||||
|
/// 01: Returns the contents, split by whitespace indiscriminately.
|
||||||
|
/// 02: Returns the contents, split by whitespace indiscriminately and made lowercase.
|
||||||
pub fn parse_args(input: &str, mode: u8) -> Vec<String> {
|
pub fn parse_args(input: &str, mode: u8) -> Vec<String> {
|
||||||
dbg!(&input);
|
let split_input: Vec<_> = input.chars().map(|arg| arg.clone()).collect(); // Converts from the input string to a vector of chars.
|
||||||
|
dbg!(&split_input);
|
||||||
let args = &mut input.clone().chars().into_iter();
|
|
||||||
let mut vargs: Vec<char> = vec![];
|
|
||||||
for arg in args {
|
|
||||||
vargs.push(arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
dbg!(&vargs);
|
|
||||||
let args: &mut [char] = &mut vargs.as_mut_slice();
|
|
||||||
match mode {
|
match mode {
|
||||||
/// Returns exact input string.
|
0x00 => {vec![String::from_iter(split_input.iter())]}, // Simply converts the vector of chars into a string, and puts it as the first element.
|
||||||
0x00 => {vec![input.to_string()]},
|
0x01 => {
|
||||||
/// Seperates all strings
|
return input
|
||||||
0x07 => {
|
.to_string()
|
||||||
let mut arguments: Vec<String> = vec![];
|
.split_ascii_whitespace()
|
||||||
// This is all the stuff that will be split, marked, preprocessed, or removed
|
.map(|x| x.to_string())
|
||||||
let mut singlequotes: Vec<usize> = vec![];
|
.collect();
|
||||||
let mut doublequotes: Vec<usize> = vec![];
|
}
|
||||||
let mut variables: Vec<usize> = vec![];
|
0x02 => {
|
||||||
let mut escaped_spaces: Vec<usize> = vec![];
|
return input
|
||||||
let mut extraneous_backslashes: Vec<usize> = vec![];
|
.to_string()
|
||||||
|
.to_ascii_lowercase()
|
||||||
let mut marked_for_removal: Vec<usize> = vec![];
|
.split_ascii_whitespace()
|
||||||
|
.map(|x| x.to_string())
|
||||||
|
.collect();
|
||||||
// Begin by marking characters that have special behavior.
|
}
|
||||||
for (i, c) in args.iter().enumerate() {
|
|
||||||
// This absolutely cursed line prevents array underflows, while making sure that the character is not escaped
|
|
||||||
let escaped: bool = if i != 0 {args[i - 1] == '\\'} else {false} && if i > 1 {args[i - 2] == '\\' }else {false};
|
|
||||||
dbg!(&escaped);
|
|
||||||
dbg!(&i);
|
|
||||||
dbg!(&c);
|
|
||||||
|
|
||||||
match c {
|
|
||||||
'\'' => {
|
|
||||||
if escaped {
|
|
||||||
singlequotes.push(i);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'\"' => {
|
|
||||||
if escaped {
|
|
||||||
doublequotes.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'$' => {
|
|
||||||
if escaped {
|
|
||||||
variables.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
' ' => {
|
|
||||||
if escaped {
|
|
||||||
escaped_spaces.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'\\' => {
|
|
||||||
if args[if i != 0 {i - 1} else {0}] != '\\' && (args[if i != args.len() {i + 1} else {args.len()}] == ' ' || args[if i != args.len() {i + 1} else {args.len()}] == '\n') {
|
|
||||||
extraneous_backslashes.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
dbg!(&singlequotes);
|
|
||||||
dbg!(&doublequotes);
|
|
||||||
dbg!(&variables);
|
|
||||||
dbg!(&escaped_spaces);
|
|
||||||
dbg!(&extraneous_backslashes);
|
|
||||||
let mut quoted = false;
|
|
||||||
let mut singlequoted = false;
|
|
||||||
let mut doublequoted = false;
|
|
||||||
|
|
||||||
for i in singlequotes {
|
|
||||||
if !singlequoted && !quoted {
|
|
||||||
singlequoted = true;
|
|
||||||
quoted = true;
|
|
||||||
marked_for_removal.push(i);
|
|
||||||
} else {
|
|
||||||
singlequoted = false;
|
|
||||||
if !doublequoted {
|
|
||||||
quoted = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for i in doublequotes {
|
|
||||||
if !doublequoted && !quoted {
|
|
||||||
doublequoted = true;
|
|
||||||
quoted = true;
|
|
||||||
marked_for_removal.push(i);
|
|
||||||
} else {
|
|
||||||
doublequoted = false;
|
|
||||||
if !singlequoted {
|
|
||||||
quoted = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//TODO handle variables here
|
|
||||||
for i in escaped_spaces {
|
|
||||||
marked_for_removal.push(if i != 0 {i - 1} else {0});
|
|
||||||
}
|
|
||||||
for i in extraneous_backslashes {
|
|
||||||
marked_for_removal.push(i);
|
|
||||||
}
|
|
||||||
for i in marked_for_removal {
|
|
||||||
args[i] = char::from(0xFF);
|
|
||||||
}
|
|
||||||
dbg!(&args);
|
|
||||||
let mut last: usize = 0;
|
|
||||||
for (i, c) in args.iter().enumerate() {
|
|
||||||
let mut output: String = "".to_string();
|
|
||||||
for o in args.to_vec().get(last + 1..if i != 0 {i - 1} else {0}).unwrap().iter() {output.push(*o)};
|
|
||||||
dbg!(&output);
|
|
||||||
arguments.push(output);
|
|
||||||
last = i;
|
|
||||||
dbg!(&arguments);
|
|
||||||
}
|
|
||||||
return arguments;
|
|
||||||
},
|
|
||||||
///
|
|
||||||
0x10 => {
|
|
||||||
vec![]
|
|
||||||
},
|
|
||||||
_ => panic!("Unexpected input")
|
_ => panic!("Unexpected input")
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue