From 8ff9dd1e2537b4e9a84af49699a1d2f19201e68d Mon Sep 17 00:00:00 2001 From: Diomidis Spinellis Date: Sun, 10 Aug 2025 01:29:38 +0300 Subject: [PATCH] Implement silent comment command --- src/uu/sed/src/compiler.rs | 20 ++++++++++++++++++-- tests/by-util/test_sed.rs | 6 ++++++ tests/fixtures/sed/output/comment | 1 + tests/fixtures/sed/output/comment_no_silent | 15 +++++++++++++++ tests/fixtures/sed/output/comment_silent | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/sed/output/comment create mode 100644 tests/fixtures/sed/output/comment_no_silent create mode 100644 tests/fixtures/sed/output/comment_silent diff --git a/src/uu/sed/src/compiler.rs b/src/uu/sed/src/compiler.rs index 3db8e78a..a7102d84 100644 --- a/src/uu/sed/src/compiler.rs +++ b/src/uu/sed/src/compiler.rs @@ -78,7 +78,6 @@ pub fn compile( } patch_block_endings(result.clone()); - // TODO: setup append & match structures Ok(result) } @@ -229,8 +228,25 @@ fn compile_sequence( loop { line.eat_spaces(); + + // According to POSIX: "If the first two characters in the script are + // "#n", the default output shall be suppressed". + if !line.eol() + && line.current() == '#' + && lines.get_line_number() == 1 + && line.get_pos() == 0 + { + line.advance(); + if !line.eol() && line.current() == 'n' { + context.quiet = true; + } + // Ignore rest of line + while !line.eol() { + line.advance(); + } + } + if line.eol() || line.current() == '#' { - // TODO: set context.quiet for StringVal starting with #n match lines.next_line()? { None => { return Ok(head); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index 5a8b0db7..e44fb968 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -154,6 +154,12 @@ const LINES1: &str = "input/lines1"; const LINES2: &str = "input/lines2"; const NO_NEW_LINE: &str = "input/no-new-line.txt"; +//////////////////////////////////////////////////////////// +// Comments +check_output!(comment, ["-n", "-e", "4p;# comment", LINES1]); +check_output!(comment_silent, ["-e", "#n", "-e", "4p", LINES1]); +check_output!(comment_no_silent, ["-e", "4p;#n comment", LINES1]); + //////////////////////////////////////////////////////////// // Address ranges: One and two, numeric and pattern check_output!(addr_one_line, ["-n", "-e", "4p", LINES1]); diff --git a/tests/fixtures/sed/output/comment b/tests/fixtures/sed/output/comment new file mode 100644 index 00000000..a7c92f0e --- /dev/null +++ b/tests/fixtures/sed/output/comment @@ -0,0 +1 @@ +l1_4 diff --git a/tests/fixtures/sed/output/comment_no_silent b/tests/fixtures/sed/output/comment_no_silent new file mode 100644 index 00000000..ccaabc22 --- /dev/null +++ b/tests/fixtures/sed/output/comment_no_silent @@ -0,0 +1,15 @@ +l1_1 +l1_2 +l1_3 +l1_4 +l1_4 +l1_5 +l1_6 +l1_7 +l1_8 +l1_9 +l1_10 +l1_11 +l1_12 +l1_13 +l1_14 diff --git a/tests/fixtures/sed/output/comment_silent b/tests/fixtures/sed/output/comment_silent new file mode 100644 index 00000000..a7c92f0e --- /dev/null +++ b/tests/fixtures/sed/output/comment_silent @@ -0,0 +1 @@ +l1_4