Skip to content

Commit

Permalink
add bash quoting to escpae
Browse files Browse the repository at this point in the history
  • Loading branch information
trou committed Oct 23, 2024
1 parent 7b663b9 commit e78f066
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/escapeapp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ use clap::{arg, Command};
enum EscType {
#[default]
Default,
Shell,
PosixShell,
Bash,
BashSingle,
}

const SHELL_CHARS: &[u8; 2] = b"'$";
const SHELL_CHARS: &[u8; 4] = b"`$\"\\";
const BASH_CHARS: &[u8; 5] = b"`$\"\\!";

trait SliceEsc {
fn escape(&self, esc_type: &EscType) -> Vec<u8>;
fn escape_chars(&self, chars: &[u8]) -> Vec<u8>;
fn escape_bash_single(&self) -> Vec<u8>;
}

impl SliceEsc for [u8] {
fn escape(&self, esc_type: &EscType) -> Vec<u8> {
match esc_type {
EscType::Default => self.escape_ascii().collect(),
EscType::Shell => self.escape_chars(SHELL_CHARS),
EscType::PosixShell => self.escape_chars(SHELL_CHARS),
EscType::Bash => self.escape_chars(BASH_CHARS),
EscType::BashSingle => self.escape_bash_single(),
}
}

Expand All @@ -34,6 +40,19 @@ impl SliceEsc for [u8] {
}
res
}

fn escape_bash_single(&self) -> Vec<u8> {
let mut res = Vec::<u8>::with_capacity(self.len());
let mut parts = self.split(|b| *b == b'\'').peekable();
while let Some(part) = parts.next() {
res.extend_from_slice(part);
if !parts.peek().is_none() {
// https://stackoverflow.com/a/1250279
res.extend_from_slice(b"'\"'\"'")
}
}
res
}
}

pub struct EscapeApplet {
Expand Down

0 comments on commit e78f066

Please sign in to comment.