Skip to content

Commit

Permalink
tr: generate an error if the input is a directory
Browse files Browse the repository at this point in the history
tested by tests/misc/read-errors
  • Loading branch information
sylvestre committed Nov 16, 2024
1 parent 1d9e162 commit 9e4b9fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
17 changes: 14 additions & 3 deletions src/uu/tr/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{
io::{BufRead, Write},
ops::Not,
};
use uucore::error::UError;
use uucore::error::{UError, UResult, USimpleError};

#[derive(Debug, Clone)]
pub enum BadSequence {
Expand Down Expand Up @@ -577,15 +577,25 @@ impl SymbolTranslator for SqueezeOperation {
}
}

pub fn translate_input<T, R, W>(input: &mut R, output: &mut W, mut translator: T)
pub fn translate_input<T, R, W>(input: &mut R, output: &mut W, mut translator: T) -> UResult<()>
where
T: SymbolTranslator,
R: BufRead,
W: Write,
{
let mut buf = Vec::new();
let mut output_buf = Vec::new();
while let Ok(length) = input.read_until(b'\n', &mut buf) {
loop {
let length = match input.read_until(b'\n', &mut buf) {
Ok(0) => break, // EOF reached
Ok(n) => n,
Err(e) => {
return Err(USimpleError::new(
1,
format!("{}: read error: {}", uucore::util_name(), e),
));
}
};
if length == 0 {
break;
} else {
Expand All @@ -596,4 +606,5 @@ where
buf.clear();
output_buf.clear();
}
Ok(())
}
10 changes: 5 additions & 5 deletions src/uu/tr/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,24 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let delete_op = DeleteOperation::new(set1);
let squeeze_op = SqueezeOperation::new(set2);
let op = delete_op.chain(squeeze_op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
} else {
let op = DeleteOperation::new(set1);
translate_input(&mut locked_stdin, &mut buffered_stdout, op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
}
} else if squeeze_flag {
if sets_len < 2 {
let op = SqueezeOperation::new(set1);
translate_input(&mut locked_stdin, &mut buffered_stdout, op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
} else {
let translate_op = TranslateOperation::new(set1, set2.clone())?;
let squeeze_op = SqueezeOperation::new(set2);
let op = translate_op.chain(squeeze_op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
}
} else {
let op = TranslateOperation::new(set1, set2)?;
translate_input(&mut locked_stdin, &mut buffered_stdout, op);
translate_input(&mut locked_stdin, &mut buffered_stdout, op)?;
}
Ok(())
}
Expand Down
5 changes: 5 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
}

#[test]
fn test_invalid_input() {
new_ucmd!().args(&["1", "1", "<", "."]).fails().code_is(1);
}

#[test]
fn test_to_upper() {
new_ucmd!()
Expand Down

0 comments on commit 9e4b9fe

Please sign in to comment.