fix compilation and underflow issues, still doesn't work though
This commit is contained in:
parent
d8b8c7a48f
commit
d3d31a1270
1 changed files with 45 additions and 27 deletions
72
src/lib.rs
72
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<String> {
|
||||
let args = &mut input.clone().chars().into_iter();
|
||||
let mut vargs: Vec<char> = 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<String> = vec![];
|
||||
let mut singlequotes: Vec<usize>;
|
||||
let mut doublequotes: Vec<usize>;
|
||||
let mut variables: Vec<usize>;
|
||||
let mut escaped_spaces: Vec<usize>;
|
||||
let mut extraneous_backslashes: Vec<usize>;
|
||||
let mut marked_for_removal: Vec<usize>;
|
||||
let mut singlequotes: Vec<usize> = vec![];
|
||||
let mut doublequotes: Vec<usize> = vec![];
|
||||
let mut variables: Vec<usize> = vec![];
|
||||
let mut escaped_spaces: Vec<usize> = vec![];
|
||||
let mut extraneous_backslashes: Vec<usize> = vec![];
|
||||
let mut marked_for_removal: Vec<usize> = 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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue