Compare commits
No commits in common. "bb5598aee78f11f6beefb6036a01103a3243718c" and "62b2dd806558083f296f1996fa4f456f242d38a2" have entirely different histories.
bb5598aee7
...
62b2dd8065
1 changed files with 1 additions and 48 deletions
|
@ -1,52 +1,8 @@
|
||||||
use crate::command::command_builder::CommandBuilder;
|
use crate::command::command_builder::CommandBuilder;
|
||||||
use crate::command::command_sequence::CommandSequence;
|
use crate::command::command_sequence::CommandSequence;
|
||||||
|
|
||||||
fn parse_quote(characters: &mut Vec<char>) -> Result<String, UnmatchedQuoteError> {
|
|
||||||
if characters.is_empty() {
|
|
||||||
Err(UnmatchedQuoteError)
|
|
||||||
} else {
|
|
||||||
let mut quoted_arg = String::default();
|
|
||||||
let mut current_char = characters.pop().unwrap();
|
|
||||||
|
|
||||||
while !characters.is_empty() && current_char != '\'' {
|
|
||||||
quoted_arg.push(current_char);
|
|
||||||
current_char = characters.pop().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(quoted_arg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_argv(characters: &mut Vec<char>, current_arg: &mut String) -> Vec<String> {
|
|
||||||
if characters.is_empty() {
|
|
||||||
vec![]
|
|
||||||
} else {
|
|
||||||
let current_char = characters.pop().unwrap();
|
|
||||||
|
|
||||||
if current_char.is_whitespace() {
|
|
||||||
if !current_arg.is_empty() {
|
|
||||||
let mut argv: Vec<String> = vec![current_arg.clone()];
|
|
||||||
argv.append(&mut build_argv(characters, &mut String::from("")));
|
|
||||||
|
|
||||||
argv
|
|
||||||
} else {
|
|
||||||
build_argv(characters, current_arg)
|
|
||||||
}
|
|
||||||
} else if current_char == '\'' {
|
|
||||||
let mut argv = vec![parse_quote(characters).unwrap()];
|
|
||||||
argv.append(&mut build_argv(characters, &mut String::default()));
|
|
||||||
|
|
||||||
argv
|
|
||||||
} else {
|
|
||||||
current_arg.push(current_char);
|
|
||||||
build_argv(characters, current_arg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
||||||
let mut characters: Vec<char> = line.chars().rev().collect();
|
let argv: Vec<String> = line.split_whitespace().map(|s| s.to_string()).collect();
|
||||||
let argv = build_argv(&mut characters, &mut String::default());
|
|
||||||
|
|
||||||
if !argv.is_empty() {
|
if !argv.is_empty() {
|
||||||
let command = CommandBuilder::new(argv).build();
|
let command = CommandBuilder::new(argv).build();
|
||||||
|
@ -55,6 +11,3 @@ pub fn parse_command_line(line: String) -> Option<CommandSequence> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct UnmatchedQuoteError;
|
|
||||||
|
|
Loading…
Reference in a new issue