From c4c2f3cbb2252427ea71a128a97927ac07284e67 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Mon, 9 Nov 2026 22:50:42 +0530 Subject: [PATCH 1/3] grep: name the -f file and line of a pattern that fails to compile GNU grep reports a syntax error in a pattern read with -f FILE as "FILE:LINE: message" ("-:LINE:" for -f -), so the user can tell which line of the pattern file to fix. Patterns from -e or the command line and errors from the PCRE backend keep the plain message. Fixes #179 --- src/lib.rs | 16 +++++++++++++--- src/matcher.rs | 24 +++++++++++++++++++----- tests/test_grep.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7fd4174..bcc3782 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,11 @@ pub struct Config<'a> { // Matcher pub patterns: &'a [&'a str], + /// For each entry of `patterns`, the `-f` file (`-` for standard input) + /// and 1-based line it was read from, or `None` for `-e` and positional + /// patterns. GNU grep names this location when such a pattern fails to + /// compile. + pub pattern_origins: &'a [Option<(&'a str, usize)>], pub regex_mode: RegexMode, pub ignore_case: bool, pub invert_match: bool, @@ -233,10 +238,12 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // given (`-e` / `-f`). An empty `-f` file is legitimate and simply matches nothing. let mut pattern_strings = Vec::new(); let mut patterns = Vec::new(); + let mut pattern_origins = Vec::new(); { for expr in regexp { for line in expr.split('\n') { patterns.push(line); + pattern_origins.push(None); } } @@ -250,13 +257,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { } else { std::fs::read_to_string(path).map_err(|err| read_error(path, &err))? }; - pattern_strings.push(contents); + pattern_strings.push((path.as_str(), contents)); } - for contents in &pattern_strings { + for (path, contents) in &pattern_strings { if !contents.is_empty() { let body = contents.strip_suffix('\n').unwrap_or(contents); - for line in body.split('\n') { + for (index, line) in body.split('\n').enumerate() { patterns.push(line); + pattern_origins.push(Some((*path, index + 1))); } } } @@ -267,6 +275,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .ok_or_else(|| USimpleError::new(2, "pattern must be valid UTF-8".to_string()))?; for line in pat.split('\n') { patterns.push(line); + pattern_origins.push(None); } } } @@ -426,6 +435,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Matcher patterns: &patterns, + pattern_origins: &pattern_origins, regex_mode, ignore_case, invert_match, diff --git a/src/matcher.rs b/src/matcher.rs index 5efacc3..4896523 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -30,8 +30,9 @@ pub struct Matcher<'a> { impl<'a> Matcher<'a> { pub fn compile(config: &'a Config<'a>) -> UResult { let mut patterns = Vec::with_capacity(config.patterns.len()); - for raw in config.patterns { - patterns.push(CompiledPattern::compile(raw, config)?); + for (index, raw) in config.patterns.iter().enumerate() { + let origin = config.pattern_origins.get(index).copied().flatten(); + patterns.push(CompiledPattern::compile(raw, origin, config)?); } // If we can reduce the whole pattern set to literal needles, keep a @@ -262,7 +263,8 @@ struct CompiledPattern { } impl CompiledPattern { - fn compile(pattern: &str, config: &Config) -> UResult { + /// `origin` is the `-f` file and line the pattern came from, if any. + fn compile(pattern: &str, origin: Option<(&str, usize)>, config: &Config) -> UResult { let mut syntax = *match config.regex_mode { RegexMode::Fixed => Syntax::asis(), RegexMode::Basic => Syntax::grep(), @@ -325,10 +327,21 @@ impl CompiledPattern { options |= RegexOptions::REGEX_OPTION_MULTILINE; } + // Like GNU grep, a regex syntax error in a pattern read with `-f FILE` + // names the file and line (`FILE:LINE: message`). GNU's PCRE backend + // reports its errors without this location. + let location = match origin { + Some((file, line)) if config.regex_mode != RegexMode::Perl => { + format!("{file}:{line}: ") + } + _ => String::new(), + }; + fn compile_with( pattern: &str, syntax: &Syntax, options: RegexOptions, + location: &str, ) -> UResult { OnigRegex::compile(pattern, syntax, options).map_err(|err| { // A reversed range like `[b-a]` is ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS. @@ -338,15 +351,16 @@ impl CompiledPattern { ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS => "Invalid range end".to_string(), _ => format!("invalid pattern \"{pattern}\": {}", err.message), }; - USimpleError::new(2, message) + USimpleError::new(2, format!("{location}{message}")) }) } - let leftmost = compile_with(pattern, &syntax, options)?; + let leftmost = compile_with(pattern, &syntax, options, &location)?; let longest_anchored = compile_with( pattern, &syntax, options | RegexOptions::REGEX_OPTION_FIND_LONGEST, + &location, )?; Ok(Self { leftmost, diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 75961f7..3c0df42 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -164,6 +164,49 @@ fn reversed_range_endpoints_match_gnu_message() { } } +#[test] +fn pattern_file_compile_error_names_file_and_line() { + // GNU grep prefixes a syntax error in a pattern read with `-f FILE` with + // `FILE:LINE: `, so the user knows which line of the file to fix. + let (scene, mut c) = ucmd(); + scene.fixtures.write("pats", "foo\n[z-a]\n"); + c.args(&["-f", "pats"]) + .pipe_in("foo\n") + .fails_with_code(2) + .no_stdout() + .stderr_is("grep: pats:2: Invalid range end\n"); + + // `-f -` reads standard input, which GNU names `-`. + let (_s, mut c) = ucmd(); + c.args(&["-f", "-"]) + .pipe_in("x\n[z-a]\n") + .fails_with_code(2) + .stderr_is("grep: -:2: Invalid range end\n"); + + // The line is counted per file, also in extended mode. + let (scene, mut c) = ucmd(); + scene.fixtures.write("ok", "foo\nbar\n"); + scene.fixtures.write("bad", "[z-a]\n"); + c.args(&["-E", "-f", "ok", "-f", "bad"]) + .pipe_in("foo\n") + .fails_with_code(2) + .stderr_is("grep: bad:1: Invalid range end\n"); + + // Patterns from `-e` or the command line get no prefix, and neither do + // errors from the PCRE backend. + let (_s, mut c) = ucmd(); + c.args(&["-e", "[z-a]"]) + .pipe_in("foo\n") + .fails_with_code(2) + .stderr_is("grep: Invalid range end\n"); + let (scene, mut c) = ucmd(); + scene.fixtures.write("pcre", "(\n"); + c.args(&["-P", "-f", "pcre"]) + .pipe_in("foo\n") + .fails_with_code(2) + .stderr_does_not_contain("pcre:1:"); +} + #[test] fn unreadable_pattern_file_is_error() { // An unreadable -f / --exclude-from file is a usage error (exit 2), not the From 45eb07d305af42d540419ae7ea81f7b4ccdf16de Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Tue, 10 Nov 2026 01:23:45 +0530 Subject: [PATCH 2/3] grep: keep each pattern with its -f origin, format the location only on error Store patterns as (text, origin) pairs so the two can't drift apart, pass the origin to compile_with and build the FILE:LINE prefix only when a pattern fails to compile. Add an empty line and a duplicate to the test fixture so they are counted as lines, and ignore the stdin write error in cases where grep exits before reading its input. --- src/lib.rs | 39 ++++++++++++++++++++++++--------------- src/matcher.rs | 30 ++++++++++++++++-------------- tests/test_grep.rs | 10 ++++++++-- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bcc3782..f5bd008 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -87,6 +87,16 @@ pub struct GlobSet { } #[doc(hidden)] +/// A pattern to match, together with where it came from. +#[derive(Clone, Copy, Debug)] +pub struct Pattern<'a> { + pub text: &'a str, + /// The `-f` file (`-` for standard input) and 1-based line the pattern was + /// read from, or `None` for `-e` and positional patterns. GNU grep names + /// this location when such a pattern fails to compile. + pub origin: Option<(&'a str, usize)>, +} + pub struct Config<'a> { // Searcher pub directory_mode: DirectoryMode, @@ -104,12 +114,7 @@ pub struct Config<'a> { pub has_context: bool, // Matcher - pub patterns: &'a [&'a str], - /// For each entry of `patterns`, the `-f` file (`-` for standard input) - /// and 1-based line it was read from, or `None` for `-e` and positional - /// patterns. GNU grep names this location when such a pattern fails to - /// compile. - pub pattern_origins: &'a [Option<(&'a str, usize)>], + pub patterns: &'a [Pattern<'a>], pub regex_mode: RegexMode, pub ignore_case: bool, pub invert_match: bool, @@ -238,12 +243,13 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // given (`-e` / `-f`). An empty `-f` file is legitimate and simply matches nothing. let mut pattern_strings = Vec::new(); let mut patterns = Vec::new(); - let mut pattern_origins = Vec::new(); { for expr in regexp { for line in expr.split('\n') { - patterns.push(line); - pattern_origins.push(None); + patterns.push(Pattern { + text: line, + origin: None, + }); } } @@ -263,8 +269,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { if !contents.is_empty() { let body = contents.strip_suffix('\n').unwrap_or(contents); for (index, line) in body.split('\n').enumerate() { - patterns.push(line); - pattern_origins.push(Some((*path, index + 1))); + patterns.push(Pattern { + text: line, + origin: Some((*path, index + 1)), + }); } } } @@ -274,8 +282,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { .to_str() .ok_or_else(|| USimpleError::new(2, "pattern must be valid UTF-8".to_string()))?; for line in pat.split('\n') { - patterns.push(line); - pattern_origins.push(None); + patterns.push(Pattern { + text: line, + origin: None, + }); } } } @@ -435,7 +445,6 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // Matcher patterns: &patterns, - pattern_origins: &pattern_origins, regex_mode, ignore_case, invert_match, @@ -470,7 +479,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { // An empty pattern matches every line; with `-v`, GNU grep selects no lines // and exits as "no match" without reading any input files. - if invert_match && patterns.iter().any(|pattern| pattern.is_empty()) { + if invert_match && patterns.iter().any(|pattern| pattern.text.is_empty()) { return Err(ExitCode::new(1)); } diff --git a/src/matcher.rs b/src/matcher.rs index 4896523..17c1eee 100644 --- a/src/matcher.rs +++ b/src/matcher.rs @@ -30,9 +30,12 @@ pub struct Matcher<'a> { impl<'a> Matcher<'a> { pub fn compile(config: &'a Config<'a>) -> UResult { let mut patterns = Vec::with_capacity(config.patterns.len()); - for (index, raw) in config.patterns.iter().enumerate() { - let origin = config.pattern_origins.get(index).copied().flatten(); - patterns.push(CompiledPattern::compile(raw, origin, config)?); + for pattern in config.patterns { + patterns.push(CompiledPattern::compile( + pattern.text, + pattern.origin, + config, + )?); } // If we can reduce the whole pattern set to literal needles, keep a @@ -40,7 +43,7 @@ impl<'a> Matcher<'a> { let needles: Option>> = config .patterns .iter() - .map(|p| plain_literal(p, config.ignore_case, config.regex_mode)) + .map(|p| plain_literal(p.text, config.ignore_case, config.regex_mode)) .collect(); let literal_searchers = needles.filter(|n| !n.is_empty()).map(|n| { n.iter() @@ -330,18 +333,13 @@ impl CompiledPattern { // Like GNU grep, a regex syntax error in a pattern read with `-f FILE` // names the file and line (`FILE:LINE: message`). GNU's PCRE backend // reports its errors without this location. - let location = match origin { - Some((file, line)) if config.regex_mode != RegexMode::Perl => { - format!("{file}:{line}: ") - } - _ => String::new(), - }; + let origin = origin.filter(|_| config.regex_mode != RegexMode::Perl); fn compile_with( pattern: &str, syntax: &Syntax, options: RegexOptions, - location: &str, + origin: Option<(&str, usize)>, ) -> UResult { OnigRegex::compile(pattern, syntax, options).map_err(|err| { // A reversed range like `[b-a]` is ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS. @@ -351,16 +349,20 @@ impl CompiledPattern { ONIGERR_EMPTY_RANGE_IN_CHAR_CLASS => "Invalid range end".to_string(), _ => format!("invalid pattern \"{pattern}\": {}", err.message), }; - USimpleError::new(2, format!("{location}{message}")) + let message = match origin { + Some((file, line)) => format!("{file}:{line}: {message}"), + None => message, + }; + USimpleError::new(2, message) }) } - let leftmost = compile_with(pattern, &syntax, options, &location)?; + let leftmost = compile_with(pattern, &syntax, options, origin)?; let longest_anchored = compile_with( pattern, &syntax, options | RegexOptions::REGEX_OPTION_FIND_LONGEST, - &location, + origin, )?; Ok(Self { leftmost, diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 3c0df42..22ecc42 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -169,12 +169,15 @@ fn pattern_file_compile_error_names_file_and_line() { // GNU grep prefixes a syntax error in a pattern read with `-f FILE` with // `FILE:LINE: `, so the user knows which line of the file to fix. let (scene, mut c) = ucmd(); - scene.fixtures.write("pats", "foo\n[z-a]\n"); + // Empty lines and duplicate patterns still count as lines. + scene.fixtures.write("pats", "foo\n\nfoo\n[z-a]\n"); c.args(&["-f", "pats"]) .pipe_in("foo\n") + // grep fails before reading standard input. + .ignore_stdin_write_error() .fails_with_code(2) .no_stdout() - .stderr_is("grep: pats:2: Invalid range end\n"); + .stderr_is("grep: pats:4: Invalid range end\n"); // `-f -` reads standard input, which GNU names `-`. let (_s, mut c) = ucmd(); @@ -189,6 +192,7 @@ fn pattern_file_compile_error_names_file_and_line() { scene.fixtures.write("bad", "[z-a]\n"); c.args(&["-E", "-f", "ok", "-f", "bad"]) .pipe_in("foo\n") + .ignore_stdin_write_error() .fails_with_code(2) .stderr_is("grep: bad:1: Invalid range end\n"); @@ -197,12 +201,14 @@ fn pattern_file_compile_error_names_file_and_line() { let (_s, mut c) = ucmd(); c.args(&["-e", "[z-a]"]) .pipe_in("foo\n") + .ignore_stdin_write_error() .fails_with_code(2) .stderr_is("grep: Invalid range end\n"); let (scene, mut c) = ucmd(); scene.fixtures.write("pcre", "(\n"); c.args(&["-P", "-f", "pcre"]) .pipe_in("foo\n") + .ignore_stdin_write_error() .fails_with_code(2) .stderr_does_not_contain("pcre:1:"); } From e2e64e07fb8493db33456c2f34fa220c527d7552 Mon Sep 17 00:00:00 2001 From: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com> Date: Wed, 11 Nov 2026 08:30:41 +0530 Subject: [PATCH 3/3] tests: expect grep.exe as the uucore error prefix on Windows Errors returned through uucore are prefixed with the binary name as invoked, which is grep.exe on Windows, so the exact stderr checks in pattern_file_compile_error_names_file_and_line failed there. Build the expected message with the platform's name. --- tests/test_grep.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_grep.rs b/tests/test_grep.rs index 22ecc42..002e686 100644 --- a/tests/test_grep.rs +++ b/tests/test_grep.rs @@ -164,6 +164,13 @@ fn reversed_range_endpoints_match_gnu_message() { } } +/// Prefix of an error reported through uucore, which names the binary as +/// invoked: `grep.exe` on Windows. +fn uucore_error(message: &str) -> String { + let name = if cfg!(windows) { "grep.exe" } else { "grep" }; + format!("{name}: {message}\n") +} + #[test] fn pattern_file_compile_error_names_file_and_line() { // GNU grep prefixes a syntax error in a pattern read with `-f FILE` with @@ -177,14 +184,14 @@ fn pattern_file_compile_error_names_file_and_line() { .ignore_stdin_write_error() .fails_with_code(2) .no_stdout() - .stderr_is("grep: pats:4: Invalid range end\n"); + .stderr_is(uucore_error("pats:4: Invalid range end")); // `-f -` reads standard input, which GNU names `-`. let (_s, mut c) = ucmd(); c.args(&["-f", "-"]) .pipe_in("x\n[z-a]\n") .fails_with_code(2) - .stderr_is("grep: -:2: Invalid range end\n"); + .stderr_is(uucore_error("-:2: Invalid range end")); // The line is counted per file, also in extended mode. let (scene, mut c) = ucmd(); @@ -194,7 +201,7 @@ fn pattern_file_compile_error_names_file_and_line() { .pipe_in("foo\n") .ignore_stdin_write_error() .fails_with_code(2) - .stderr_is("grep: bad:1: Invalid range end\n"); + .stderr_is(uucore_error("bad:1: Invalid range end")); // Patterns from `-e` or the command line get no prefix, and neither do // errors from the PCRE backend. @@ -203,7 +210,7 @@ fn pattern_file_compile_error_names_file_and_line() { .pipe_in("foo\n") .ignore_stdin_write_error() .fails_with_code(2) - .stderr_is("grep: Invalid range end\n"); + .stderr_is(uucore_error("Invalid range end")); let (scene, mut c) = ucmd(); scene.fixtures.write("pcre", "(\n"); c.args(&["-P", "-f", "pcre"])