From d928457f3d8b2966754b68ae03190bfb7411af1d Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Tue, 27 Oct 2026 22:51:22 +0530 Subject: [PATCH 1/3] grep: apply --include and --exclude to files named on the command line They only filtered files found under -r, so "grep --exclude='*.min.js' foo *.js" still searched the minified files. GNU grep skips a command-line file when a pattern matches a suffix of its name: the whole name, or a part that starts right after a path separator. Stdin and missing files are not affected, as in GNU. --- src/lib.rs | 39 ++++++++++++++++++++++++++++++++------- src/searcher.rs | 5 +++++ tests/test_grep.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7fd4174..ff1a99c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -990,20 +990,45 @@ impl GlobSet { Ok(()) } + /// Whether a file found by recursion is skipped, by its base name. pub fn skips(&self, name: &OsStr) -> bool { - let last_match = name.to_str().and_then(|name| { - self.patterns - .iter() - .rev() - .find(|(_, pattern)| pattern.matches(name)) - }); - match last_match { + let name = name.to_str(); + self.skips_if(|pattern| name.is_some_and(|name| pattern.matches(name))) + } + + /// Whether a file named on the command line is skipped. A pattern + /// matches when it matches a suffix of the name: the whole name, or a + /// part that starts right after a path separator. + pub fn skips_command_line_file(&self, path: &Path) -> bool { + let name = path.to_str(); + self.skips_if(|pattern| { + name.is_some_and(|name| suffixes(name).any(|suffix| pattern.matches(suffix))) + }) + } + + fn skips_if(&self, matches: impl Fn(&glob::Pattern) -> bool) -> bool { + match self + .patterns + .iter() + .rev() + .find(|(_, pattern)| matches(pattern)) + { Some((include, _)) => !include, None => self.patterns.first().is_some_and(|(include, _)| *include), } } } +/// `name` and each part of it that starts right after a path separator. +fn suffixes(name: &str) -> impl Iterator { + let after_separator = name + .char_indices() + .filter(|&(_, c)| std::path::is_separator(c)) + .map(|(i, _)| &name[i + 1..]) + .filter(|rest| !rest.is_empty() && !rest.starts_with(std::path::is_separator)); + std::iter::once(name).chain(after_separator) +} + fn is_color_value(value: &str) -> bool { value.bytes().all(|b| b.is_ascii_digit() || b == b';') } diff --git a/src/searcher.rs b/src/searcher.rs index 64c4c40..201cfa8 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -77,6 +77,11 @@ impl<'a> Searcher<'a> { if self.config.device_mode == DeviceMode::Skip && Self::is_special_file(path) { return ControlFlow::Continue(()); } + // Files named on the command line are filtered by --include and + // --exclude too. A missing file is still opened, to report it. + if path.exists() && self.config.file_globs.skips_command_line_file(path) { + return ControlFlow::Continue(()); + } self.process_file(lb, path) } } diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 83a472f..fd1ee9f 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1639,6 +1639,43 @@ fn recursive_include_exclude_last_match_wins() { .no_stdout(); } +#[test] +fn include_exclude_apply_to_command_line_files() { + let (scene, _) = ucmd(); + build_tree(&scene); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=*.log", "grep", "tree/a.txt", "tree/b.log"]) + .succeeds() + .stdout_is("tree/a.txt:grep me\n"); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["-l", "--include=*.log", "grep", "tree/a.txt", "tree/b.log"]) + .succeeds() + .stdout_is("tree/b.log\n"); + + // The pattern can match the whole name or any part after a '/'. + for exclude in ["--exclude=tree/*", "--exclude=sub/c.txt", "--exclude=c.txt"] { + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&[exclude, "grep", "tree/sub/c.txt"]) + .fails_with_code(1) + .no_stdout(); + } + + #[cfg(windows)] + { + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=c.txt", "grep", r"tree\sub\c.txt"]) + .fails_with_code(1) + .no_stdout(); + } + + // A missing file is still reported. + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=missing", "grep", "missing"]) + .fails_with_code(2); +} + #[test] fn recursive_exclude_from_file() { let (scene, mut c) = ucmd(); From 92757bae18d9abfe4ac11f68a93c8b171dd459e4 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Wed, 28 Oct 2026 16:13:14 +0100 Subject: [PATCH 2/3] Custom globbing --- Cargo.lock | 1 - Cargo.toml | 1 - src/glob.rs | 314 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 78 +---------- src/matcher.rs | 32 ++++- src/searcher.rs | 2 +- tests/test_grep.rs | 54 ++++++++ 7 files changed, 397 insertions(+), 85 deletions(-) create mode 100644 src/glob.rs diff --git a/Cargo.lock b/Cargo.lock index 9efa18c..532f354 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1232,7 +1232,6 @@ version = "0.2.0" dependencies = [ "clap", "codspeed-criterion-compat", - "glob", "memchr", "onig", "onig_sys", diff --git a/Cargo.toml b/Cargo.toml index 604c9bf..11033bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,6 @@ path = "src/lib.rs" [dependencies] clap = { version = "4.5", features = ["wrap_help", "cargo", "color"] } -glob = "0.3.1" memchr = "2.7.2" onig = { version = "~6.5.1", default-features = false } onig_sys = { version = "69.9.3", default-features = false } diff --git a/src/glob.rs b/src/glob.rs new file mode 100644 index 0000000..00db74a --- /dev/null +++ b/src/glob.rs @@ -0,0 +1,314 @@ +// This file is part of the uutils grep package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +//! fnmatch-like glob handling. Translates glob patterns to Onig +//! regular expressions, to make `[:class:]` work correctly. + +use crate::matcher::{OnigRegex, find_bracket_subexpr_end}; +use onig::{RegexOptions, Syntax}; +use std::ffi::OsStr; +use std::path::{Path, is_separator}; +use std::str::Chars; +use uucore::error::{UResult, USimpleError}; + +const SEPARATOR: &str = if cfg!(windows) { + r"[/\\]" +} else if cfg!(target_os = "uefi") { + r"\\" +} else { + "/" +}; + +/// Include and exclude patterns, in command-line order. The last matching +/// pattern decides; otherwise, skip only if the first pattern is an include. +#[derive(Default)] +pub struct GlobSet { + patterns: Vec<(bool, OnigRegex)>, +} + +impl GlobSet { + pub fn new() -> Self { + Self::default() + } + + pub fn with_capacity(capacity: usize) -> Self { + Self { + patterns: Vec::with_capacity(capacity), + } + } + + pub(crate) fn add(&mut self, include: bool, pattern: &str) -> UResult<()> { + let regex = OnigRegex::compile( + &translate(pattern), + Syntax::perl_ng(), + RegexOptions::REGEX_OPTION_MULTILINE, + ) + .map_err(|err| { + USimpleError::new(2, format!("invalid glob '{pattern}': {}", err.message)) + })?; + self.patterns.push((include, regex)); + Ok(()) + } + + /// Match a recursive entry's basename, not its path suffixes. + pub fn skips(&self, name: &OsStr) -> bool { + self.skips_if(name, |regex, name| regex.match_at(name, 0, None).is_some()) + } + + /// Match the whole path or a nonempty suffix after a run of separators. + pub fn skips_command_line_file(&self, path: &Path) -> bool { + self.skips_if(path.as_os_str(), |regex, name| { + regex.search(name, 0, None).is_some() + }) + } + + fn skips_if(&self, name: &OsStr, matches: impl Fn(&OnigRegex, &[u8]) -> bool) -> bool { + if self.patterns.is_empty() { + return false; + } + + let name = name.to_str().map(str::as_bytes); + match self + .patterns + .iter() + .rev() + .find(|(_, pattern)| name.is_some_and(|name| matches(pattern, name))) + { + Some((include, _)) => !include, + None => self.patterns[0].0, + } + } +} + +fn translate(pattern: &str) -> String { + // Exclude empty suffixes and positions inside a run of separators, but + // allow any pattern (including a leading separator) at the start of input. + let mut out = format!(r"(?:\A|(?<={SEPARATOR})(?!{SEPARATOR}|\z))"); + let mut chars = pattern.chars(); + while let Some(c) = chars.next() { + match c { + '?' => out.push('.'), + '*' => { + out.push_str(".*"); + while chars.as_str().starts_with('*') { + chars.next(); + } + } + '\\' => push_literal(chars.next().unwrap_or('\\'), &mut out), + '[' => translate_class(&mut chars, &mut out), + _ if is_separator(c) => out.push_str(SEPARATOR), + _ => push_literal(c, &mut out), + } + } + out.push_str(r"\z"); + out +} + +fn push_literal(c: char, out: &mut String) { + if r"\.^$|?*+()[]{}-".contains(c) { + out.push('\\'); + } + out.push(c); +} + +fn translate_class(chars: &mut Chars<'_>, out: &mut String) { + let start = out.len(); + let mut body = chars.clone(); + out.push('['); + if body.as_str().starts_with(['!', '^']) { + body.next(); + out.push('^'); + } + let mut first = true; + while let Some(c) = body.next() { + match c { + ']' if !first => { + out.push(']'); + *chars = body; + return; + } + '\\' => push_literal(body.next().unwrap_or('\\'), out), + '[' => { + let rest = body.as_str(); + if let Some(&delimiter @ (b':' | b'.' | b'=')) = rest.as_bytes().first() + && let Some(end) = find_bracket_subexpr_end(rest.as_bytes(), 1, delimiter) + { + out.push('['); + out.push_str(&rest[..end]); + body = rest[end..].chars(); + } else { + out.push_str(r"\["); + } + } + '/' if cfg!(windows) => out.push_str(r"/\\"), + _ => out.push(c), + } + first = false; + } + + // An unclosed bracket is literal; leave the remaining input unconsumed. + out.truncate(start); + out.push_str(r"\["); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn separator_matches_platform() { + let regex = OnigRegex::compile( + &format!(r"\A{SEPARATOR}\z"), + Syntax::perl_ng(), + RegexOptions::REGEX_OPTION_NONE, + ) + .unwrap_or_else(|err| panic!("{}", err.message)); + for c in ['/', '\\', ':', 'a'] { + assert_eq!( + regex.match_at(c.to_string().as_bytes(), 0, None).is_some(), + is_separator(c), + "{c:?}", + ); + } + } + + fn excluding(pattern: &str) -> GlobSet { + let mut globs = GlobSet::new(); + globs.add(false, pattern).unwrap(); + globs + } + + #[test] + fn gnu_wildcards() { + let cases: &[(&str, &[&str], &[&str])] = &[ + ("*", &["", ".hidden", "a\nb"], &[]), + ("**.txt", &["a.txt", "t/a.txt"], &["a.log"]), + ("***.txt", &["a.txt", "t/a.txt"], &["a.log"]), + ("t/**/a.txt", &["t/x/a.txt", "t/x/y/a.txt"], &["t/a.txt"]), + ("t/*", &["t/a.txt", "t/x/a.txt"], &["x/a.txt"]), + ("q?x", &["q/x", "qax", "q\nx"], &["qx", "qaax"]), + (r"a\*.txt", &["a*.txt"], &["abc.txt"]), + (r"a\?.txt", &["a?.txt"], &["ab.txt"]), + (r"a\[b]", &["a[b]"], &["ab"]), + (r"a\\b", &[r"a\b"], &["ab"]), + (r"\a", &["a"], &[r"\a", "\x07"]), + ("a\\", &["a\\"], &["a"]), + (".^$+(){}|", &[".^$+(){}|"], &[""]), + ]; + assert_cases(cases); + assert_eq!(translate("***.txt"), translate("*.txt")); + } + + #[test] + fn gnu_brackets() { + let cases: &[(&str, &[&str], &[&str])] = &[ + ("[ab]", &["a", "b"], &["[ab]", "c"]), + ("[!a]", &["b", "!"], &["a"]), + ("[^a]", &["b", "^"], &["a"]), + ("[", &["["], &["a"]), + ("[]", &["[]"], &["]"]), + ("[!]", &["[!]"], &["!"]), + ("[abc", &["[abc"], &["a"]), + ("[]]", &["]"], &["[", "[]]"]), + ("[!]]", &["[", "a"], &["]"]), + ("[[]", &["["], &["]"]), + ("[a-z]", &["a", "m", "z"], &["A", "0"]), + ("[-a]", &["-", "a"], &["z"]), + ("[a-]", &["-", "a"], &["z"]), + ("[a&&b]", &["a", "&", "b"], &["c"]), + ("[[:digit:]]", &["0", "9"], &["a", "]"]), + ("[![:digit:]]", &["a", "\n"], &["9"]), + ("[[:alpha:]]", &["a", "\u{e9}"], &["9"]), + (r"[\]]", &["]"], &[r"\", r"\]"]), + (r"[\d]", &["d"], &["9"]), + (r"[a\-z]", &["a", "-", "z"], &["m"]), + ]; + assert_cases(cases); + } + + fn assert_cases(cases: &[(&str, &[&str], &[&str])]) { + for &(pattern, matching, nonmatching) in cases { + let globs = excluding(pattern); + for (names, expected) in [(matching, true), (nonmatching, false)] { + for name in names { + assert_eq!( + globs.skips(OsStr::new(name)), + expected, + "{pattern:?}, {name:?}", + ); + } + } + } + } + + #[test] + fn regex_errors_are_reported() { + for pattern in ["[z-a]", "[[:notaclass:]]"] { + let mut globs = GlobSet::new(); + let err = globs.add(false, pattern).unwrap_err(); + assert!(err.to_string().contains("invalid glob"), "{pattern:?}"); + assert!(globs.patterns.is_empty()); + } + } + + #[test] + fn suffix_boundaries() { + let globs = excluding("a.txt"); + for name in ["a.txt", "t/a.txt", "t//a.txt", "./t/a.txt"] { + assert!(globs.skips_command_line_file(Path::new(name)), "{name}"); + } + for name in ["data.txt", "t/data.txt", "t/a.txt\n", "a.txt/"] { + assert!(!globs.skips_command_line_file(Path::new(name)), "{name}"); + } + assert!(!globs.skips(OsStr::new("t/a.txt"))); + assert!(!excluding("/a.txt").skips_command_line_file(Path::new("t//a.txt"))); + assert!(!excluding("").skips_command_line_file(Path::new("t/"))); + } + + #[cfg(windows)] + #[test] + fn windows_path_separators() { + let globs = excluding("t/*.txt"); + assert!(globs.skips_command_line_file(Path::new(r"t\a.txt"))); + assert!(excluding("a.txt").skips_command_line_file(Path::new(r"t\\a.txt"))); + assert!(excluding("[/]").skips(OsStr::new(r"\"))); + } + + #[test] + fn last_matching_pattern_wins_across_suffixes() { + for include_last in [false, true] { + let mut globs = GlobSet::new(); + globs.add(!include_last, "t/*").unwrap(); + globs.add(include_last, "a.txt").unwrap(); + assert_eq!( + globs.skips_command_line_file(Path::new("t/a.txt")), + !include_last, + ); + assert_eq!(globs.skips(OsStr::new("unmatched")), !include_last); + } + } + + #[test] + fn empty_set_keeps_files() { + let globs = GlobSet::new(); + assert!(!globs.skips(OsStr::new("a"))); + assert!(!globs.skips_command_line_file(Path::new("t/a"))); + } + + #[cfg(unix)] + #[test] + fn non_utf8_names_keep_the_default_action() { + use std::os::unix::ffi::OsStrExt; + + let name = OsStr::from_bytes(b"t/\xff/a.txt"); + assert!(!GlobSet::new().skips_command_line_file(Path::new(name))); + for include in [false, true] { + let mut globs = GlobSet::new(); + globs.add(include, "*.txt").unwrap(); + assert_eq!(globs.skips(name), include); + assert_eq!(globs.skips_command_line_file(Path::new(name)), include); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index ff1a99c..6e975f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ #[doc(hidden)] pub mod context_buffer; +mod glob; #[doc(hidden)] pub mod line_buffer; #[doc(hidden)] @@ -12,12 +13,13 @@ pub mod matcher; mod output; mod searcher; +use crate::glob::GlobSet; use crate::line_buffer::LineBuffer; use crate::matcher::Matcher; use crate::output::OutputWriter; use crate::searcher::Searcher; use clap::{Arg, ArgAction, Command}; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsString; use std::io::{IsTerminal as _, Read}; use std::path::Path; use uucore::error::{ExitCode, UError, UResult, USimpleError, strip_errno}; @@ -78,14 +80,6 @@ pub struct ColorConfig<'a> { pub no_erase: bool, } -/// Include and exclude patterns, in command-line order. As in GNU grep, the -/// last pattern that matches a name decides. If none matches, the name is -/// skipped only when the first pattern is an include. -#[doc(hidden)] -pub struct GlobSet { - patterns: Vec<(bool, glob::Pattern)>, -} - #[doc(hidden)] pub struct Config<'a> { // Searcher @@ -963,72 +957,6 @@ fn expand_num_shorthand(args: impl Iterator) -> Vec { out } -impl Default for GlobSet { - fn default() -> Self { - Self::new() - } -} - -impl GlobSet { - /// Create an empty GlobSet. - pub fn new() -> Self { - Self { - patterns: Vec::new(), - } - } - - pub fn with_capacity(capacity: usize) -> Self { - Self { - patterns: Vec::with_capacity(capacity), - } - } - - fn add(&mut self, include: bool, pattern: &str) -> UResult<()> { - let compiled_pattern = glob::Pattern::new(pattern) - .map_err(|e| USimpleError::new(2, format!("invalid glob '{pattern}': {e}")))?; - self.patterns.push((include, compiled_pattern)); - Ok(()) - } - - /// Whether a file found by recursion is skipped, by its base name. - pub fn skips(&self, name: &OsStr) -> bool { - let name = name.to_str(); - self.skips_if(|pattern| name.is_some_and(|name| pattern.matches(name))) - } - - /// Whether a file named on the command line is skipped. A pattern - /// matches when it matches a suffix of the name: the whole name, or a - /// part that starts right after a path separator. - pub fn skips_command_line_file(&self, path: &Path) -> bool { - let name = path.to_str(); - self.skips_if(|pattern| { - name.is_some_and(|name| suffixes(name).any(|suffix| pattern.matches(suffix))) - }) - } - - fn skips_if(&self, matches: impl Fn(&glob::Pattern) -> bool) -> bool { - match self - .patterns - .iter() - .rev() - .find(|(_, pattern)| matches(pattern)) - { - Some((include, _)) => !include, - None => self.patterns.first().is_some_and(|(include, _)| *include), - } - } -} - -/// `name` and each part of it that starts right after a path separator. -fn suffixes(name: &str) -> impl Iterator { - let after_separator = name - .char_indices() - .filter(|&(_, c)| std::path::is_separator(c)) - .map(|(i, _)| &name[i + 1..]) - .filter(|rest| !rest.is_empty() && !rest.starts_with(std::path::is_separator)); - std::iter::once(name).chain(after_separator) -} - fn is_color_value(value: &str) -> bool { value.bytes().all(|b| b.is_ascii_digit() || b == b';') } diff --git a/src/matcher.rs b/src/matcher.rs index dac786b..bcbecbe 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -372,7 +372,7 @@ impl CompiledPattern { } } -struct OnigRegex { +pub(crate) struct OnigRegex { raw: onig_sys::OnigRegex, } @@ -384,7 +384,11 @@ unsafe impl Send for OnigRegex {} unsafe impl Sync for OnigRegex {} impl OnigRegex { - fn compile(pattern: &str, syntax: &Syntax, options: RegexOptions) -> Result { + pub(crate) fn compile( + pattern: &str, + syntax: &Syntax, + options: RegexOptions, + ) -> Result { let pattern = pattern.as_bytes(); let mut raw = null_mut(); let mut error = onig_sys::OnigErrorInfo { @@ -427,7 +431,12 @@ impl OnigRegex { } } - fn search(&self, line: &[u8], offset: usize, region: Option<&mut Region>) -> Option { + pub(crate) fn search( + &self, + line: &[u8], + offset: usize, + region: Option<&mut Region>, + ) -> Option { debug_assert!(offset <= line.len()); // SAFETY: `offset` is bounded by `line.len()`, all byte pointers are // derived from `line`, and `region_ptr` preserves `onig::Region`'s @@ -448,7 +457,12 @@ impl OnigRegex { onig_match_result(result) } - fn match_at(&self, line: &[u8], offset: usize, region: Option<&mut Region>) -> Option { + pub(crate) fn match_at( + &self, + line: &[u8], + offset: usize, + region: Option<&mut Region>, + ) -> Option { debug_assert!(offset <= line.len()); // SAFETY: `offset` is bounded by `line.len()`, all byte pointers are // derived from `line`, and `region_ptr` preserves `onig::Region`'s @@ -476,9 +490,9 @@ impl Drop for OnigRegex { } } -struct OnigError { +pub(crate) struct OnigError { code: i32, - message: String, + pub(crate) message: String, } impl OnigError { @@ -610,7 +624,11 @@ fn scan_bracket(pattern: &[u8], start: usize) -> (bool, usize) { /// Index just past the `:]`, `.]` or `=]` closing a `[: [. [=` subexpression /// whose body starts at `start`. -fn find_bracket_subexpr_end(pattern: &[u8], start: usize, delimiter: u8) -> Option { +pub(crate) fn find_bracket_subexpr_end( + pattern: &[u8], + start: usize, + delimiter: u8, +) -> Option { (start..pattern.len().saturating_sub(1)) .find(|&i| pattern[i] == delimiter && pattern[i + 1] == b']') .map(|i| i + 2) diff --git a/src/searcher.rs b/src/searcher.rs index 201cfa8..c91e1c6 100644 --- a/src/searcher.rs +++ b/src/searcher.rs @@ -79,7 +79,7 @@ impl<'a> Searcher<'a> { } // Files named on the command line are filtered by --include and // --exclude too. A missing file is still opened, to report it. - if path.exists() && self.config.file_globs.skips_command_line_file(path) { + if self.config.file_globs.skips_command_line_file(path) && path.exists() { return ControlFlow::Continue(()); } self.process_file(lb, path) diff --git a/tests/test_grep.rs b/tests/test_grep.rs index fd1ee9f..2424460 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1676,6 +1676,60 @@ fn include_exclude_apply_to_command_line_files() { .fails_with_code(2); } +#[test] +fn include_exclude_regex_globs() { + let (scene, _) = ucmd(); + build_tree(&scene); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=tree/[ab].?xt", "grep", "tree/a.txt"]) + .fails_with_code(1) + .no_output(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--include=tree/**/c.txt", "grep", "tree/sub/c.txt"]) + .succeeds() + .stdout_is("grep me\n") + .no_stderr(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--include=tree/**/a.txt", "grep", "tree/a.txt"]) + .fails_with_code(1) + .no_output(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&[ + "--include=***.[[:alpha:]][[:alpha:]][[:alpha:]]", + "grep", + "tree/a.txt", + ]) + .succeeds() + .stdout_is("grep me\n") + .no_stderr(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=tree/*", "--include=a.txt", "grep", "tree/a.txt"]) + .succeeds() + .stdout_is("grep me\n") + .no_stderr(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--include=a.txt", "--exclude=tree/*", "grep", "tree/a.txt"]) + .fails_with_code(1) + .no_output(); + + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=[[:notaclass:]]", "grep", "tree/a.txt"]) + .fails_with_code(2) + .stderr_contains("invalid glob"); + + scene.fixtures.write("[abc", "grep me\n"); + let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); + c.args(&["--exclude=[abc", "grep", "[abc"]) + .fails_with_code(1) + .no_output(); +} + #[test] fn recursive_exclude_from_file() { let (scene, mut c) = ucmd(); From 2ff1267d1ae411c203712d0db7e877714bc4a022 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sun, 15 Nov 2026 10:30:56 +0530 Subject: [PATCH 3/3] grep: match [.a.], [=a=], invalid globs and non-UTF-8 paths like GNU [.a.] and [=a=] inside a bracket now stand for the character. An invalid glob such as [z-a] matches nothing instead of exiting with 2. Paths are matched as bytes, so a non-UTF-8 parent directory no longer hides a valid suffix. On Windows, the backslash is added to a class with '/' as its own member, so [/-0] stays a range. --- src/glob.rs | 102 +++++++++++++++++++++++++-------------------- src/lib.rs | 4 +- tests/test_grep.rs | 9 ++-- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/glob.rs b/src/glob.rs index 00db74a..17a9cb3 100644 --- a/src/glob.rs +++ b/src/glob.rs @@ -11,21 +11,14 @@ use onig::{RegexOptions, Syntax}; use std::ffi::OsStr; use std::path::{Path, is_separator}; use std::str::Chars; -use uucore::error::{UResult, USimpleError}; -const SEPARATOR: &str = if cfg!(windows) { - r"[/\\]" -} else if cfg!(target_os = "uefi") { - r"\\" -} else { - "/" -}; +const SEPARATOR: &str = if cfg!(windows) { r"[/\\]" } else { "/" }; /// Include and exclude patterns, in command-line order. The last matching /// pattern decides; otherwise, skip only if the first pattern is an include. #[derive(Default)] pub struct GlobSet { - patterns: Vec<(bool, OnigRegex)>, + patterns: Vec<(bool, Option)>, } impl GlobSet { @@ -39,17 +32,17 @@ impl GlobSet { } } - pub(crate) fn add(&mut self, include: bool, pattern: &str) -> UResult<()> { - let regex = OnigRegex::compile( - &translate(pattern), - Syntax::perl_ng(), - RegexOptions::REGEX_OPTION_MULTILINE, - ) - .map_err(|err| { - USimpleError::new(2, format!("invalid glob '{pattern}': {}", err.message)) - })?; + /// Like GNU, an invalid pattern such as `[z-a]` matches nothing. + pub(crate) fn add(&mut self, include: bool, pattern: &str) { + let regex = translate(pattern).and_then(|regex| { + OnigRegex::compile( + ®ex, + Syntax::perl_ng(), + RegexOptions::REGEX_OPTION_MULTILINE, + ) + .ok() + }); self.patterns.push((include, regex)); - Ok(()) } /// Match a recursive entry's basename, not its path suffixes. @@ -69,12 +62,12 @@ impl GlobSet { return false; } - let name = name.to_str().map(str::as_bytes); + let name = name.as_encoded_bytes(); match self .patterns .iter() .rev() - .find(|(_, pattern)| name.is_some_and(|name| matches(pattern, name))) + .find(|(_, pattern)| pattern.as_ref().is_some_and(|p| matches(p, name))) { Some((include, _)) => !include, None => self.patterns[0].0, @@ -82,7 +75,7 @@ impl GlobSet { } } -fn translate(pattern: &str) -> String { +fn translate(pattern: &str) -> Option { // Exclude empty suffixes and positions inside a run of separators, but // allow any pattern (including a leading separator) at the start of input. let mut out = format!(r"(?:\A|(?<={SEPARATOR})(?!{SEPARATOR}|\z))"); @@ -97,13 +90,13 @@ fn translate(pattern: &str) -> String { } } '\\' => push_literal(chars.next().unwrap_or('\\'), &mut out), - '[' => translate_class(&mut chars, &mut out), + '[' => translate_class(&mut chars, &mut out)?, _ if is_separator(c) => out.push_str(SEPARATOR), _ => push_literal(c, &mut out), } } out.push_str(r"\z"); - out + Some(out) } fn push_literal(c: char, out: &mut String) { @@ -113,8 +106,9 @@ fn push_literal(c: char, out: &mut String) { out.push(c); } -fn translate_class(chars: &mut Chars<'_>, out: &mut String) { +fn translate_class(chars: &mut Chars<'_>, out: &mut String) -> Option<()> { let start = out.len(); + let mut separator = false; let mut body = chars.clone(); out.push('['); if body.as_str().starts_with(['!', '^']) { @@ -125,9 +119,12 @@ fn translate_class(chars: &mut Chars<'_>, out: &mut String) { while let Some(c) = body.next() { match c { ']' if !first => { + if separator { + out.push_str(r"\\"); + } out.push(']'); *chars = body; - return; + return Some(()); } '\\' => push_literal(body.next().unwrap_or('\\'), out), '[' => { @@ -135,14 +132,26 @@ fn translate_class(chars: &mut Chars<'_>, out: &mut String) { if let Some(&delimiter @ (b':' | b'.' | b'=')) = rest.as_bytes().first() && let Some(end) = find_bracket_subexpr_end(rest.as_bytes(), 1, delimiter) { - out.push('['); - out.push_str(&rest[..end]); + if delimiter == b':' { + out.push('['); + out.push_str(&rest[..end]); + } else { + // `[.a.]` and `[=a=]` stand for the character itself. + let mut inner = rest[1..end - 2].chars(); + let (Some(c), None) = (inner.next(), inner.next()) else { + return None; + }; + push_literal(c, out); + } body = rest[end..].chars(); } else { out.push_str(r"\["); } } - '/' if cfg!(windows) => out.push_str(r"/\\"), + '/' if cfg!(windows) => { + out.push('/'); + separator = true; + } _ => out.push(c), } first = false; @@ -151,6 +160,7 @@ fn translate_class(chars: &mut Chars<'_>, out: &mut String) { // An unclosed bracket is literal; leave the remaining input unconsumed. out.truncate(start); out.push_str(r"\["); + Some(()) } #[cfg(test)] @@ -176,7 +186,7 @@ mod tests { fn excluding(pattern: &str) -> GlobSet { let mut globs = GlobSet::new(); - globs.add(false, pattern).unwrap(); + globs.add(false, pattern); globs } @@ -224,6 +234,9 @@ mod tests { (r"[\]]", &["]"], &[r"\", r"\]"]), (r"[\d]", &["d"], &["9"]), (r"[a\-z]", &["a", "-", "z"], &["m"]), + ("[[.a.]]", &["a"], &[".]", "[.a.]"]), + ("[[=a=]]", &["a"], &["=]", "[=a=]"]), + ("[[.-.]]", &["-"], &["a"]), ]; assert_cases(cases); } @@ -244,12 +257,11 @@ mod tests { } #[test] - fn regex_errors_are_reported() { - for pattern in ["[z-a]", "[[:notaclass:]]"] { - let mut globs = GlobSet::new(); - let err = globs.add(false, pattern).unwrap_err(); - assert!(err.to_string().contains("invalid glob"), "{pattern:?}"); - assert!(globs.patterns.is_empty()); + fn invalid_patterns_match_nothing() { + for pattern in ["[z-a]", "[[:notaclass:]]", "[[.ab.]]"] { + for name in ["a", "z", "ab", pattern] { + assert!(!excluding(pattern).skips(OsStr::new(name)), "{pattern:?}"); + } } } @@ -274,14 +286,15 @@ mod tests { assert!(globs.skips_command_line_file(Path::new(r"t\a.txt"))); assert!(excluding("a.txt").skips_command_line_file(Path::new(r"t\\a.txt"))); assert!(excluding("[/]").skips(OsStr::new(r"\"))); + assert!(excluding("[/-0].txt").skips(OsStr::new("0.txt"))); } #[test] fn last_matching_pattern_wins_across_suffixes() { for include_last in [false, true] { let mut globs = GlobSet::new(); - globs.add(!include_last, "t/*").unwrap(); - globs.add(include_last, "a.txt").unwrap(); + globs.add(!include_last, "t/*"); + globs.add(include_last, "a.txt"); assert_eq!( globs.skips_command_line_file(Path::new("t/a.txt")), !include_last, @@ -299,16 +312,13 @@ mod tests { #[cfg(unix)] #[test] - fn non_utf8_names_keep_the_default_action() { + fn non_utf8_names_match() { use std::os::unix::ffi::OsStrExt; - let name = OsStr::from_bytes(b"t/\xff/a.txt"); - assert!(!GlobSet::new().skips_command_line_file(Path::new(name))); - for include in [false, true] { - let mut globs = GlobSet::new(); - globs.add(include, "*.txt").unwrap(); - assert_eq!(globs.skips(name), include); - assert_eq!(globs.skips_command_line_file(Path::new(name)), include); + let name = OsStr::from_bytes(b"t\xff/a.txt"); + for pattern in ["*.txt", "a.txt", "t?/a.txt"] { + assert!(excluding(pattern).skips_command_line_file(Path::new(name))); } + assert!(excluding("*.txt").skips(OsStr::from_bytes(b"\xff.txt"))); } } diff --git a/src/lib.rs b/src/lib.rs index 6e975f3..950629f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -359,12 +359,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { globs.sort_by_key(|(i, _, _)| *i); let mut file_globs = GlobSet::with_capacity(globs.len()); for (_, include, pattern) in &globs { - file_globs.add(*include, pattern)?; + file_globs.add(*include, pattern); } let exclude_dir_globs = { let mut patterns = GlobSet::with_capacity(exclude_dir.len()); for pattern in exclude_dir { - patterns.add(false, pattern)?; + patterns.add(false, pattern); } patterns }; diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 2424460..74f6847 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -1673,7 +1673,9 @@ fn include_exclude_apply_to_command_line_files() { // A missing file is still reported. let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); c.args(&["--exclude=missing", "grep", "missing"]) - .fails_with_code(2); + .fails_with_code(2) + .no_stdout() + .stderr_contains("missing: "); } #[test] @@ -1720,8 +1722,9 @@ fn include_exclude_regex_globs() { let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep")); c.args(&["--exclude=[[:notaclass:]]", "grep", "tree/a.txt"]) - .fails_with_code(2) - .stderr_contains("invalid glob"); + .succeeds() + .stdout_is("grep me\n") + .no_stderr(); scene.fixtures.write("[abc", "grep me\n"); let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));