2020-03-30 19:36:26 +02:00
|
|
|
#![cfg(feature = "paging")]
|
|
|
|
|
2021-01-10 22:49:06 +01:00
|
|
|
use std::ffi::OsStr;
|
2019-12-23 09:54:18 +01:00
|
|
|
use std::process::Command;
|
|
|
|
|
2022-09-04 00:02:08 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-05-04 07:56:38 +02:00
|
|
|
pub enum LessVersion {
|
|
|
|
Less(usize),
|
|
|
|
BusyBox,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<LessVersion> {
|
2021-07-12 20:03:31 +02:00
|
|
|
let resolved_path = grep_cli::resolve_binary(less_path.as_ref()).ok()?;
|
|
|
|
|
|
|
|
let cmd = Command::new(resolved_path).arg("--version").output().ok()?;
|
2022-05-04 07:56:38 +02:00
|
|
|
if cmd.status.success() {
|
|
|
|
parse_less_version(&cmd.stdout)
|
|
|
|
} else {
|
|
|
|
parse_less_version_busybox(&cmd.stderr)
|
|
|
|
}
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-04 07:56:38 +02:00
|
|
|
fn parse_less_version(output: &[u8]) -> Option<LessVersion> {
|
2021-09-10 21:52:09 +02:00
|
|
|
if !output.starts_with(b"less ") {
|
|
|
|
return None;
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|
2021-09-10 21:52:09 +02:00
|
|
|
|
|
|
|
let version = std::str::from_utf8(&output[5..]).ok()?;
|
|
|
|
let end = version.find(|c: char| !c.is_ascii_digit())?;
|
2022-05-04 07:56:38 +02:00
|
|
|
Some(LessVersion::Less(version[..end].parse::<usize>().ok()?))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_less_version_busybox(output: &[u8]) -> Option<LessVersion> {
|
|
|
|
match std::str::from_utf8(output) {
|
|
|
|
Ok(version) if version.contains("BusyBox ") => Some(LessVersion::BusyBox),
|
|
|
|
_ => None,
|
|
|
|
}
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_487() {
|
|
|
|
let output = b"less 487 (GNU regular expressions)
|
|
|
|
Copyright (C) 1984-2016 Mark Nudelman
|
|
|
|
|
|
|
|
less comes with NO WARRANTY, to the extent permitted by law.
|
|
|
|
For information about the terms of redistribution,
|
|
|
|
see the file named README in the less distribution.
|
|
|
|
Homepage: http://www.greenwoodsoftware.com/less";
|
|
|
|
|
2022-05-04 07:56:38 +02:00
|
|
|
assert_eq!(Some(LessVersion::Less(487)), parse_less_version(output));
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 13:00:39 +01:00
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_529() {
|
|
|
|
let output = b"less 529 (Spencer V8 regular expressions)
|
|
|
|
Copyright (C) 1984-2017 Mark Nudelman
|
|
|
|
|
|
|
|
less comes with NO WARRANTY, to the extent permitted by law.
|
|
|
|
For information about the terms of redistribution,
|
|
|
|
see the file named README in the less distribution.
|
|
|
|
Homepage: http://www.greenwoodsoftware.com/less";
|
|
|
|
|
2022-05-04 07:56:38 +02:00
|
|
|
assert_eq!(Some(LessVersion::Less(529)), parse_less_version(output));
|
2019-12-23 13:00:39 +01:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:54:18 +01:00
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_551() {
|
|
|
|
let output = b"less 551 (PCRE regular expressions)
|
|
|
|
Copyright (C) 1984-2019 Mark Nudelman
|
|
|
|
|
|
|
|
less comes with NO WARRANTY, to the extent permitted by law.
|
|
|
|
For information about the terms of redistribution,
|
|
|
|
see the file named README in the less distribution.
|
|
|
|
Home page: http://www.greenwoodsoftware.com/less";
|
|
|
|
|
2022-05-04 07:56:38 +02:00
|
|
|
assert_eq!(Some(LessVersion::Less(551)), parse_less_version(output));
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|
|
|
|
|
2021-05-02 20:58:12 +02:00
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_581_2() {
|
|
|
|
let output = b"less 581.2 (PCRE2 regular expressions)
|
|
|
|
Copyright (C) 1984-2021 Mark Nudelman
|
|
|
|
|
|
|
|
less comes with NO WARRANTY, to the extent permitted by law.
|
|
|
|
For information about the terms of redistribution,
|
|
|
|
see the file named README in the less distribution.
|
|
|
|
Home page: https://greenwoodsoftware.com/less";
|
|
|
|
|
2022-05-04 07:56:38 +02:00
|
|
|
assert_eq!(Some(LessVersion::Less(581)), parse_less_version(output));
|
2021-05-02 20:58:12 +02:00
|
|
|
}
|
|
|
|
|
2019-12-23 09:54:18 +01:00
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_wrong_program() {
|
|
|
|
let output = b"more from util-linux 2.34";
|
|
|
|
|
|
|
|
assert_eq!(None, parse_less_version(output));
|
2022-05-04 07:56:38 +02:00
|
|
|
assert_eq!(None, parse_less_version_busybox(output));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_busybox() {
|
|
|
|
let output = b"pkg/less: unrecognized option '--version'
|
|
|
|
BusyBox v1.35.0 (2022-04-21 10:38:11 EDT) multi-call binary.
|
|
|
|
|
|
|
|
Usage: less [-EFIMmNSRh~] [FILE]...
|
|
|
|
|
|
|
|
View FILE (or stdin) one screenful at a time
|
|
|
|
|
|
|
|
-E Quit once the end of a file is reached
|
|
|
|
-F Quit if entire file fits on first screen
|
|
|
|
-I Ignore case in all searches
|
|
|
|
-M,-m Display status line with line numbers
|
|
|
|
and percentage through the file
|
|
|
|
-N Prefix line number to each line
|
|
|
|
-S Truncate long lines
|
|
|
|
-R Remove color escape codes in input
|
|
|
|
-~ Suppress ~s displayed past EOF";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Some(LessVersion::BusyBox),
|
|
|
|
parse_less_version_busybox(output)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_less_version_invalid_utf_8() {
|
|
|
|
let output = b"\xff";
|
|
|
|
|
|
|
|
assert_eq!(None, parse_less_version(output));
|
|
|
|
assert_eq!(None, parse_less_version_busybox(output));
|
2019-12-23 09:54:18 +01:00
|
|
|
}
|