Skip to content
Permalink
Browse files Browse the repository at this point in the history
Do not resolve executables as relative path from current directory (#658
)

* Refactor: child pager process creation

* Protect calls to Command::new behind grep_cli::resolve_binary

* Move less env-var setting into less-specific branch
  • Loading branch information
dandavison committed Jul 10, 2021
1 parent 2826bdd commit f01846b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 66 deletions.
57 changes: 57 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -23,6 +23,7 @@ box_drawing = "0.1.2"
bytelines = "2.2.2"
console = "0.14.1"
dirs-next = "2.0.0"
grep-cli = "0.1.6"
itertools = "0.10.1"
lazy_static = "1.4"
pathdiff = "0.2.0"
Expand Down
8 changes: 6 additions & 2 deletions src/bat_utils/less.rs
@@ -1,8 +1,12 @@
use std::process::Command;

pub fn retrieve_less_version() -> Option<usize> {
let cmd = Command::new("less").arg("--version").output().ok()?;
parse_less_version(&cmd.stdout)
if let Ok(less_path) = grep_cli::resolve_binary("less") {
let cmd = Command::new(less_path).arg("--version").output().ok()?;
parse_less_version(&cmd.stdout)
} else {
None
}
}

fn parse_less_version(output: &[u8]) -> Option<usize> {
Expand Down
155 changes: 92 additions & 63 deletions src/bat_utils/output.rs
Expand Up @@ -77,78 +77,35 @@ impl OutputType {
let pagerflags =
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;

match pagerflags.split_first() {
Ok(match pagerflags.split_first() {
Some((pager_name, args)) => {
let pager_path = PathBuf::from(pager_name);

let is_less = pager_path.file_stem() == Some(&OsString::from("less"));

let mut process = if is_less {
let mut p = Command::new(&pager_path);
if args.is_empty() || replace_arguments_to_less {
p.args(vec!["--RAW-CONTROL-CHARS"]);

// Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
// versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
//
// See: http://www.greenwoodsoftware.com/less/news.530.html
//
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
// is not needed anymore.
match retrieve_less_version() {
None => {
p.arg("--no-init");
}
Some(version)
if (version < 530 || (cfg!(windows) && version < 558)) =>
{
p.arg("--no-init");
}
_ => {}
}

if quit_if_one_screen {
p.arg("--quit-if-one-screen");
}
} else {
p.args(args);
}
p.env("LESSCHARSET", "UTF-8");
p
let process = if is_less {
_make_process_from_less_path(
pager_path,
args,
replace_arguments_to_less,
quit_if_one_screen,
config,
)
} else {
if pager_path.file_stem() == Some(&OsString::from("delta")) {
eprintln!(
"\
It looks like you have set delta as the value of $PAGER. \
This would result in a non-terminating recursion. \
delta is not an appropriate value for $PAGER \
(but it is an appropriate value for $GIT_PAGER)."
);
std::process::exit(1);
}
let mut p = Command::new(&pager_path);
p.args(args);
p
_make_process_from_pager_path(pager_path, args)
};
if is_less && config.navigate {
if let Ok(hist_file) =
navigate::copy_less_hist_file_and_append_navigate_regexp(config)
{
process.env("LESSHISTFILE", hist_file);
if config.show_themes {
process.arg("+n");
}
}
if let Some(mut process) = process {
process
.stdin(Stdio::piped())
.spawn()
.map(OutputType::Pager)
.unwrap_or_else(|_| OutputType::stdout())
} else {
OutputType::stdout()
}
Ok(process
.env("LESSANSIENDCHARS", "mK")
.stdin(Stdio::piped())
.spawn()
.map(OutputType::Pager)
.unwrap_or_else(|_| OutputType::stdout()))
}
None => Ok(OutputType::stdout()),
}
None => OutputType::stdout(),
})
}

fn stdout() -> Self {
Expand All @@ -166,6 +123,78 @@ delta is not an appropriate value for $PAGER \
}
}

fn _make_process_from_less_path(
less_path: PathBuf,
args: &[String],
replace_arguments_to_less: bool,
quit_if_one_screen: bool,
config: &config::Config,
) -> Option<Command> {
if let Ok(less_path) = grep_cli::resolve_binary(less_path) {
let mut p = Command::new(&less_path);
if args.is_empty() || replace_arguments_to_less {
p.args(vec!["--RAW-CONTROL-CHARS"]);

// Passing '--no-init' fixes a bug with '--quit-if-one-screen' in older
// versions of 'less'. Unfortunately, it also breaks mouse-wheel support.
//
// See: http://www.greenwoodsoftware.com/less/news.530.html
//
// For newer versions (530 or 558 on Windows), we omit '--no-init' as it
// is not needed anymore.
match retrieve_less_version() {
None => {
p.arg("--no-init");
}
Some(version) if (version < 530 || (cfg!(windows) && version < 558)) => {
p.arg("--no-init");
}
_ => {}
}

if quit_if_one_screen {
p.arg("--quit-if-one-screen");
}
} else {
p.args(args);
}
p.env("LESSCHARSET", "UTF-8");
p.env("LESSANSIENDCHARS", "mK");
if config.navigate {
if let Ok(hist_file) = navigate::copy_less_hist_file_and_append_navigate_regexp(config)
{
p.env("LESSHISTFILE", hist_file);
if config.show_themes {
p.arg("+n");
}
}
}
Some(p)
} else {
None
}
}

fn _make_process_from_pager_path(pager_path: PathBuf, args: &[String]) -> Option<Command> {
if pager_path.file_stem() == Some(&OsString::from("delta")) {
eprintln!(
"\
It looks like you have set delta as the value of $PAGER. \
This would result in a non-terminating recursion. \
delta is not an appropriate value for $PAGER \
(but it is an appropriate value for $GIT_PAGER)."
);
std::process::exit(1);
}
if let Ok(pager_path) = grep_cli::resolve_binary(pager_path) {
let mut p = Command::new(&pager_path);
p.args(args);
Some(p)
} else {
None
}
}

impl Drop for OutputType {
fn drop(&mut self) {
if let OutputType::Pager(ref mut command) = *self {
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Expand Up @@ -132,7 +132,11 @@ You can also use delta to diff two files: `delta file_A file_B`."
let diff_command = "git";
let minus_file = minus_file.unwrap_or_else(die);
let plus_file = plus_file.unwrap_or_else(die);
let mut diff_process = process::Command::new(PathBuf::from(diff_command))
let diff_command_path = match grep_cli::resolve_binary(PathBuf::from(diff_command)) {
Ok(path) => path,
Err(_) => return config.error_exit_code,
};
let mut diff_process = process::Command::new(diff_command_path)
.args(&["diff", "--no-index"])
.args(&[minus_file, plus_file])
.stdout(process::Stdio::piped())
Expand Down

0 comments on commit f01846b

Please sign in to comment.