use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader};
use std::process::ExitCode;
const USAGE: &str = "Usage: strigil <pattern> [<file>] [--ignore-case] [--help] [--version]";
enum Parsed<'a> {
Run(Invocation<'a>),
Help,
Version,
}
struct Invocation<'a> {
pattern: &'a str,
file: Option<&'a str>,
ignore_case: bool,
}
impl<'a> Invocation<'a> {
fn parse(args: &'a [String]) -> Result<Parsed<'a>, String> {
let mut positional: Vec<&'a str> = Vec::new();
let mut ignore_case = false;
let mut help = false;
let mut version = false;
for arg in args {
match arg.as_str() {
"--ignore-case" => ignore_case = true,
"--help" => help = true,
"--version" => version = true,
other => positional.push(other),
}
}
if help {
return Ok(Parsed::Help);
}
if version {
return Ok(Parsed::Version);
}
match positional.as_slice() {
[pattern] => Ok(Parsed::Run(Invocation {
pattern,
file: None,
ignore_case,
})),
[pattern, file] => {
let file = if *file == "-" { None } else { Some(*file) };
Ok(Parsed::Run(Invocation {
pattern,
file,
ignore_case,
}))
}
_ => Err(format!(
"expected a pattern and at most one file (<pattern> [<file>]), got {}",
positional.len()
)),
}
}
}
fn main() -> ExitCode {
let args: Vec<String> = env::args().skip(1).collect();
let invocation = match Invocation::parse(&args) {
Ok(Parsed::Help) => {
print_help();
return ExitCode::SUCCESS;
}
Ok(Parsed::Version) => {
println!("strigil {}", env!("CARGO_PKG_VERSION"));
return ExitCode::SUCCESS;
}
Ok(Parsed::Run(invocation)) => invocation,
Err(message) => {
eprintln!("strigil: {message}");
eprintln!("{USAGE}");
return ExitCode::from(2);
}
};
match run(&invocation) {
Ok(SearchOutcome::MatchFound) | Ok(SearchOutcome::EmptyFile) => ExitCode::SUCCESS,
Ok(SearchOutcome::NoMatch) => ExitCode::from(1),
Err(error) => {
eprintln!("strigil: {error}");
ExitCode::from(3)
}
}
}
fn print_help() {
println!(
"strigil — a minimal, dependency-free grep clone
{USAGE}
Arguments:
<pattern> The literal substring to search for.
<file> The file to read line by line; standard input when omitted or `-`.
Options:
--ignore-case Match case-insensitively (accepted in any position).
--help Print this help and exit.
--version Print the version and exit.
Exit codes:
0 match found, or the input was empty
1 no match, but the input was read successfully
2 usage error
3 I/O error
Environment:
COLOR=always Highlight the first match per line in ANSI red."
);
}
enum SearchOutcome {
MatchFound,
NoMatch,
EmptyFile,
}
fn run(invocation: &Invocation) -> io::Result<SearchOutcome> {
let mut input: Box<dyn BufRead> = match invocation.file {
Some(path) => Box::new(BufReader::new(File::open(path)?)),
None => Box::new(io::stdin().lock()),
};
let is_binary = {
let head = input.fill_buf()?;
head.contains(&0)
};
if is_binary {
return run_binary(&mut *input, invocation);
}
let needle = if invocation.ignore_case {
invocation.pattern.to_lowercase()
} else {
invocation.pattern.to_string()
};
let highlight = match env::var("COLOR") {
Ok(value) => value == "always",
Err(_) => false,
};
let mut matched = false;
let mut lines_read = 0;
for (index, line) in input.lines().enumerate() {
let line = line?;
lines_read += 1;
let haystack = if invocation.ignore_case {
line.to_lowercase()
} else {
line.clone()
};
if let Some(position) = haystack.find(&needle) {
matched = true;
print_match(index + 1, &line, position, needle.len(), highlight);
}
}
Ok(if lines_read == 0 {
SearchOutcome::EmptyFile
} else if matched {
SearchOutcome::MatchFound
} else {
SearchOutcome::NoMatch
})
}
fn run_binary(input: &mut dyn BufRead, invocation: &Invocation) -> io::Result<SearchOutcome> {
let needle = if invocation.ignore_case {
invocation.pattern.to_lowercase().into_bytes()
} else {
invocation.pattern.as_bytes().to_vec()
};
let source = invocation.file.unwrap_or("<standard input>");
if needle.is_empty() {
println!("strigil: {source}: binary file matches");
return Ok(SearchOutcome::MatchFound);
}
let overlap = needle.len() - 1;
let mut window: Vec<u8> = Vec::new();
loop {
let chunk = input.fill_buf()?;
if chunk.is_empty() {
break;
}
window.extend_from_slice(chunk);
if window.windows(needle.len()).any(|w| w == needle) {
println!("strigil: {source}: binary file matches");
return Ok(SearchOutcome::MatchFound);
}
let keep = overlap.min(window.len());
window.drain(..window.len() - keep);
let len = chunk.len();
input.consume(len);
}
Ok(SearchOutcome::NoMatch)
}
fn print_match(line_number: usize, line: &str, position: usize, length: usize, highlight: bool) {
if highlight {
let end = position.saturating_add(length);
if line.is_char_boundary(position) && line.is_char_boundary(end) {
println!(
"{}:{}\x1b[31m{}\x1b[0m{}",
line_number,
&line[..position],
&line[position..end],
&line[end..]
);
return;
}
}
println!("{line_number}:{line}");
}