Skip to content

Commit

Permalink
Fix exec Escaping Issues (sharkdp#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Oct 31, 2017
1 parent 0d3568f commit fb63be5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
43 changes: 18 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ lazy_static = "0.2.9"
num_cpus = "1.6.2"
regex = "0.2"
regex-syntax = "0.4"
shell-escape = "0.1.3"

[target.'cfg(all(unix, not(target_os = "redox")))'.dependencies]
libc = "0.2"
Expand Down
32 changes: 32 additions & 0 deletions src/exec/escape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::borrow::Cow;

#[cfg(windows)]
const ESCAPE_CHAR: u8 = b'^';

#[cfg(not(windows))]
const ESCAPE_CHAR: u8 = b'\\';

fn needs_escape(byte: u8) -> bool {
byte < 43 || (byte > 58 && byte < 65) || (byte > 90 && byte < 97) || (byte > 122 && byte <= 127)
}

pub fn escape<'a>(input: &'a str) -> Cow<'a, str> {
let chars_to_escape = input
.as_bytes()
.iter()
.filter(|&&x| needs_escape(x))
.count();
if chars_to_escape == 0 {
Cow::Borrowed(input)
} else {
let mut output = Vec::with_capacity(input.len() + chars_to_escape);
for &character in input.as_bytes() {
if needs_escape(character) {
output.push(ESCAPE_CHAR);
}
output.push(character);
}
let output = unsafe { String::from_utf8_unchecked(output) };
Cow::Owned(output)
}
}
4 changes: 2 additions & 2 deletions src/exec/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use std::path::MAIN_SEPARATOR;
use std::borrow::Cow;

use shell_escape::escape;
use super::escape;

/// A builder for efficiently generating input strings.
///
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<'a> Input<'a> {
}

pub fn get(&'a self) -> Cow<'a, str> {
escape(Cow::Borrowed(self.data))
escape(self.data)
}

#[cfg(test)]
Expand Down
2 changes: 2 additions & 0 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// according to those terms.

// TODO: Possible optimization could avoid pushing characters on a buffer.
mod escape;
mod ticket;
mod token;
mod job;
Expand All @@ -20,6 +21,7 @@ use self::input::Input;
use self::ticket::CommandTicket;
use self::token::Token;
pub use self::job::job;
pub use self::escape::escape;

/// Signifies that a placeholder token was found
const PLACE: u8 = 1;
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extern crate libc;
extern crate num_cpus;
extern crate regex;
extern crate regex_syntax;
extern crate shell_escape;
#[cfg(windows)]
extern crate windows;

Expand Down

0 comments on commit fb63be5

Please sign in to comment.