From 34b18351e2e0bcaf27b8567499ce4728393ab507 Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Tue, 1 Feb 2022 21:57:52 +0100 Subject: [PATCH 1/3] cat: cleanup write_tab_to_end duplication of logic The logic for '\n' and '\r' about the number of written characters was duplicated --- src/uu/cat/src/cat.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index e7fd31497..3c89d8434 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -560,13 +560,12 @@ fn write_tab_to_end(mut in_buf: &[u8], writer: &mut W) -> usize { { Some(p) => { writer.write_all(&in_buf[..p]).unwrap(); - if in_buf[p] == b'\n' { - return count + p; - } else if in_buf[p] == b'\t' { + if in_buf[p] == b'\t' { writer.write_all(b"^I").unwrap(); in_buf = &in_buf[p + 1..]; count += p + 1; } else { + // b'\n' or b'\r' return count + p; } } From 46769245327e81375cd22a31cb91b51b09e21a4a Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Tue, 1 Feb 2022 22:02:20 +0100 Subject: [PATCH 2/3] cat: cat_path does not need to parse InputType for stdin itself This type is already handled by get_input_type, so we can unify the handling --- src/uu/cat/src/cat.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index 3c89d8434..e0e6ed7f0 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -325,15 +325,15 @@ fn cat_path( state: &mut OutputState, out_info: Option<&FileInformation>, ) -> CatResult<()> { - if path == "-" { - let stdin = io::stdin(); - let mut handle = InputHandle { - reader: stdin, - is_interactive: atty::is(atty::Stream::Stdin), - }; - return cat_handle(&mut handle, options, state); - } match get_input_type(path)? { + InputType::StdIn => { + let stdin = io::stdin(); + let mut handle = InputHandle { + reader: stdin, + is_interactive: atty::is(atty::Stream::Stdin), + }; + cat_handle(&mut handle, options, state) + } InputType::Directory => Err(CatError::IsDirectory), #[cfg(unix)] InputType::Socket => { From 3bbfe00791b7874bb9e94bcf37e5c068f836a451 Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Tue, 1 Feb 2022 22:16:05 +0100 Subject: [PATCH 3/3] cat: write_nonprint_to_end: be more explicit about printing '?' --- src/uu/cat/src/cat.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/uu/cat/src/cat.rs b/src/uu/cat/src/cat.rs index e0e6ed7f0..498e5e8ad 100644 --- a/src/uu/cat/src/cat.rs +++ b/src/uu/cat/src/cat.rs @@ -588,10 +588,10 @@ fn write_nonprint_to_end(in_buf: &[u8], writer: &mut W, tab: &[u8]) -> 9 => writer.write_all(tab), 0..=8 | 10..=31 => writer.write_all(&[b'^', byte + 64]), 32..=126 => writer.write_all(&[byte]), - 127 => writer.write_all(&[b'^', byte - 64]), + 127 => writer.write_all(&[b'^', b'?']), 128..=159 => writer.write_all(&[b'M', b'-', b'^', byte - 64]), 160..=254 => writer.write_all(&[b'M', b'-', byte - 128]), - _ => writer.write_all(&[b'M', b'-', b'^', 63]), + _ => writer.write_all(&[b'M', b'-', b'^', b'?']), } .unwrap(); count += 1;