From d3d31a12709c45ac95ebe0f13137fa9b5a75de35 Mon Sep 17 00:00:00 2001 From: Lilly Rosaline Date: Fri, 1 Oct 2021 00:03:14 -0500 Subject: [PATCH] fix compilation and underflow issues, still doesn't work though --- src/lib.rs | 72 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5eabb24..0298c39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,91 +12,109 @@ mod tests { } /// Parses the command line arguments into a list of parameters. -pub fn parse_args(input: &str, mode: u8) -> Vec<&str> { - let args = &mut input.clone(); +pub fn parse_args(input: &str, mode: u8) -> Vec { + let args = &mut input.clone().chars().into_iter(); + let mut vargs: Vec = vec![]; + for arg in args { + vargs.push(arg); + } + let args: &mut [char] = &mut vargs.as_mut_slice(); match mode { /// Returns exact input string. - 0x00 => {vec![args]}, + 0x00 => {vec![input.to_string()]}, /// 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; + let mut singlequotes: Vec = vec![]; + let mut doublequotes: Vec = vec![]; + let mut variables: Vec = vec![]; + let mut escaped_spaces: Vec = vec![]; + let mut extraneous_backslashes: Vec = vec![]; + let mut marked_for_removal: Vec = vec![]; // Begin by marking characters that have special behavior. - for (i, c) in args.chars().enumerate() { + for (i, c) in args.iter().enumerate() { match c { '\'' => { - if args[i - 1] == '\\' && args[i - 2] != '\\' { + if args[if i != 0 {i - 1} else {0}] == '\\' && args[if i != 0 {i - 2} else {0}] != '\\' { singlequotes.push(i); } }, '\"' => { - if args[i - 1] == '\\' && args[i - 2] != '\\' { + if args[if i != 0 {i - 1} else {0}] == '\\' && args[if i != 0 {i - 2} else {0}] != '\\' { doublequotes.push(i); } } '$' => { - if args[i - 1] == '\\' && args[i - 2] != '\\' { + if args[if i != 0 {i - 1} else {0}] == '\\' && args[if i != 0 {i - 2} else {0}] != '\\' { variables.push(i); } } ' ' => { - if args[i - 1] == '\\' && args[i - 2] != '\\' { + if args[if i != 0 {i - 1} else {0}] == '\\' && args[if i != 0 {i - 2} else {0}] != '\\' { escaped_spaces.push(i); } } '\\' => { - if args[i - i] != '\\' && args[i + 1] == ' ' | '\n' { + 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); } } _ => {} }; + } + let mut quoted = false; let mut singlequoted = false; + let mut doublequoted = false; + for i in singlequotes { - if !singlequoted { + if !singlequoted && !quoted { singlequoted = true; + quoted = true; marked_for_removal.push(i); } else { singlequoted = false; + if !doublequoted { + quoted = false; + } } } - let mut doublequoted = false; for i in doublequotes { - if !doublequoted { + if !doublequoted && !quoted { doublequoted = true; + quoted = true; marked_for_removal.push(i); } else { doublequoted = false; + if !singlequoted { + quoted = false; + } } } for i in escaped_spaces { - marked_for_removal.push(i - 1); + 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.insert(i, char::from(0xFF)); + args[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()); + let mut last: usize = 0; + for (i, c) in args.iter().enumerate() { + if c == &char::from(0xFF) { + let mut output: String = "".to_string(); + args.to_vec().get(last + 1..if i != 0 {i - 1} else {0}).unwrap().iter().map(|x| output.push(*x)); + arguments.push(output); + last = i; } } return arguments; - } - }, + }, /// 0x10 => { - + vec![] }, _ => panic!("Unexpected input") }