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.
|
/// Parses the command line arguments into a list of parameters.
|
||||||
pub fn parse_args(input: &str, mode: u8) -> Vec<&str> {
|
pub fn parse_args(input: &str, mode: u8) -> Vec<String> {
|
||||||
let args = &mut input.clone();
|
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 {
|
match mode {
|
||||||
/// Returns exact input string.
|
/// Returns exact input string.
|
||||||
0x00 => {vec![args]},
|
0x00 => {vec![input.to_string()]},
|
||||||
/// Seperates all strings
|
/// Seperates all strings
|
||||||
0x07 => {
|
0x07 => {
|
||||||
let mut arguments: Vec<String> = vec![];
|
let mut arguments: Vec<String> = vec![];
|
||||||
let mut singlequotes: Vec<usize>;
|
let mut singlequotes: Vec<usize> = vec![];
|
||||||
let mut doublequotes: Vec<usize>;
|
let mut doublequotes: Vec<usize> = vec![];
|
||||||
let mut variables: Vec<usize>;
|
let mut variables: Vec<usize> = vec![];
|
||||||
let mut escaped_spaces: Vec<usize>;
|
let mut escaped_spaces: Vec<usize> = vec![];
|
||||||
let mut extraneous_backslashes: Vec<usize>;
|
let mut extraneous_backslashes: Vec<usize> = vec![];
|
||||||
let mut marked_for_removal: Vec<usize>;
|
let mut marked_for_removal: Vec<usize> = vec![];
|
||||||
|
|
||||||
|
|
||||||
// Begin by marking characters that have special behavior.
|
// Begin by marking characters that have special behavior.
|
||||||
for (i, c) in args.chars().enumerate() {
|
for (i, c) in args.iter().enumerate() {
|
||||||
match c {
|
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);
|
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);
|
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);
|
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);
|
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);
|
extraneous_backslashes.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
let mut quoted = false;
|
||||||
let mut singlequoted = false;
|
let mut singlequoted = false;
|
||||||
|
let mut doublequoted = false;
|
||||||
|
|
||||||
for i in singlequotes {
|
for i in singlequotes {
|
||||||
if !singlequoted {
|
if !singlequoted && !quoted {
|
||||||
singlequoted = true;
|
singlequoted = true;
|
||||||
|
quoted = true;
|
||||||
marked_for_removal.push(i);
|
marked_for_removal.push(i);
|
||||||
} else {
|
} else {
|
||||||
singlequoted = false;
|
singlequoted = false;
|
||||||
|
if !doublequoted {
|
||||||
|
quoted = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mut doublequoted = false;
|
|
||||||
for i in doublequotes {
|
for i in doublequotes {
|
||||||
if !doublequoted {
|
if !doublequoted && !quoted {
|
||||||
doublequoted = true;
|
doublequoted = true;
|
||||||
|
quoted = true;
|
||||||
marked_for_removal.push(i);
|
marked_for_removal.push(i);
|
||||||
} else {
|
} else {
|
||||||
doublequoted = false;
|
doublequoted = false;
|
||||||
|
if !singlequoted {
|
||||||
|
quoted = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i in escaped_spaces {
|
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 {
|
for i in extraneous_backslashes {
|
||||||
marked_for_removal.push(i);
|
marked_for_removal.push(i);
|
||||||
}
|
}
|
||||||
for i in marked_for_removal {
|
for i in marked_for_removal {
|
||||||
args.insert(i, char::from(0xFF));
|
args[i] = char::from(0xFF);
|
||||||
}
|
}
|
||||||
for (i, c) in args.chars().enumerate() {
|
let mut last: usize = 0;
|
||||||
if c == char::from(0xFF) {
|
for (i, c) in args.iter().enumerate() {
|
||||||
arguments.push(args.split_at(i).0.to_string());
|
if c == &char::from(0xFF) {
|
||||||
arguments.push(args.split_at(i).1.to_string());
|
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;
|
return arguments;
|
||||||
}
|
},
|
||||||
},
|
|
||||||
///
|
///
|
||||||
0x10 => {
|
0x10 => {
|
||||||
|
vec![]
|
||||||
},
|
},
|
||||||
_ => panic!("Unexpected input")
|
_ => panic!("Unexpected input")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue