diff --git a/src/lib.rs b/src/lib.rs index d314dca..5eabb24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,21 +1,98 @@ +use std::str::Chars; + #[cfg(test)] mod tests { use crate::*; #[test] fn tests() { - dbg!(parse_args("Hello world".to_string(), 0x00)); + dbg!(parse_args("Hello world", 0x00)); + dbg!(parse_args("\"Hello world\'\"", 0x07)); } } /// Parses the command line arguments into a list of parameters. -pub fn parse_args(args: String, mode: u8) -> Vec { +pub fn parse_args(input: &str, mode: u8) -> Vec<&str> { + let args = &mut input.clone(); match mode { /// Returns exact input string. 0x00 => {vec![args]}, /// Seperates all strings 0x07 => { + let mut arguments: Vec = vec![]; + let mut singlequotes: Vec; + let mut doublequotes: Vec; + let mut variables: Vec; + let mut escaped_spaces: Vec; + let mut extraneous_backslashes: Vec; + let mut marked_for_removal: Vec; + + // Begin by marking characters that have special behavior. + for (i, c) in args.chars().enumerate() { + match c { + '\'' => { + if args[i - 1] == '\\' && args[i - 2] != '\\' { + singlequotes.push(i); + } + }, + '\"' => { + if args[i - 1] == '\\' && args[i - 2] != '\\' { + doublequotes.push(i); + } + } + '$' => { + if args[i - 1] == '\\' && args[i - 2] != '\\' { + variables.push(i); + } + } + ' ' => { + if args[i - 1] == '\\' && args[i - 2] != '\\' { + escaped_spaces.push(i); + } + } + '\\' => { + if args[i - i] != '\\' && args[i + 1] == ' ' | '\n' { + extraneous_backslashes.push(i); + } + } + _ => {} + }; + let mut singlequoted = false; + for i in singlequotes { + if !singlequoted { + singlequoted = true; + marked_for_removal.push(i); + } else { + singlequoted = false; + } + } + let mut doublequoted = false; + for i in doublequotes { + if !doublequoted { + doublequoted = true; + marked_for_removal.push(i); + } else { + doublequoted = false; + } + } + for i in escaped_spaces { + marked_for_removal.push(i - 1); + } + for i in extraneous_backslashes { + marked_for_removal.push(i); + } + for i in marked_for_removal { + args.insert(i, char::from(0xFF)); + } + for (i, c) in args.chars().enumerate() { + if c == char::from(0xFF) { + arguments.push(args.split_at(i).0.to_string()); + arguments.push(args.split_at(i).1.to_string()); + } + } + return arguments; + } }, /// 0x10 => {