Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ fn read_error(label: impl std::fmt::Display, err: &std::io::Error) -> Box<dyn UE
USimpleError::new(2, format!("{label}: {}", strip_errno(err)))
}

/// Read a file named by `-f` or `--exclude-from`, where `-` means standard
/// input. The label for an error message is returned together with the result,
/// so both operands go through one `-` check.
fn read_pattern_file(path: &str) -> (&str, std::io::Result<String>) {
if path == "-" {
let mut buf = String::new();
(
"(standard input)",
std::io::stdin().read_to_string(&mut buf).map(|_| buf),
)
} else {
(path, std::fs::read_to_string(path))
}
}

#[uucore::main(no_signals)]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = expand_num_shorthand(args);
Expand Down Expand Up @@ -241,16 +256,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

for path in file_pattern {
let contents = if *path == "-" {
let mut buf = String::new();
std::io::stdin()
.read_to_string(&mut buf)
.map_err(|err| read_error("(standard input)", &err))?;
buf
} else {
std::fs::read_to_string(path).map_err(|err| read_error(path, &err))?
};
pattern_strings.push(contents);
let (label, contents) = read_pattern_file(path);
pattern_strings.push(contents.map_err(|err| read_error(label, &err))?);
}
for contents in &pattern_strings {
if !contents.is_empty() {
Expand Down Expand Up @@ -353,7 +360,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
matches.indices_of("exclude_from"),
) {
for (i, path) in indices.zip(paths) {
let contents = std::fs::read_to_string(path).map_err(|err| read_error(path, &err))?;
let (label, contents) = read_pattern_file(path);
let contents = contents.map_err(|err| read_error(label, &err))?;
for line in contents.lines() {
let trimmed = line.trim();
if !trimmed.is_empty() {
Expand Down
13 changes: 13 additions & 0 deletions tests/test_grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,19 @@ fn recursive_exclude_from_file() {
.stdout_does_not_contain("b.log");
}

#[test]
fn exclude_from_dash_reads_standard_input() {
// GNU grep reads the exclude list from standard input when the operand is
// `-`; uu_grep used to look for a file literally named `-`.
let (scene, mut c) = ucmd();
build_tree(&scene);
c.args(&["-r", "-l", "--exclude-from=-", "grep", "tree"])
.pipe_in("*.log\n")
.succeeds()
.stdout_contains("a.txt")
.stdout_does_not_contain("b.log");
}

#[cfg(all(unix, not(target_os = "cygwin")))]
#[test]
fn dereference_recursive_follows_symlinks() {
Expand Down
Loading