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
5 changes: 3 additions & 2 deletions src/sed/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ pub enum SpaceFlag {
pub struct InputAction {
/// Next command to execute (rather than commands from start)
pub next_command: Option<Rc<RefCell<Command>>>,
/// Data to prepend to the read contents
pub prepend: Vec<u8>,
/// Data to prepend to the read contents (`N`), or `None` to replace
/// the pattern space with them (`n`)
pub prepend: Option<Vec<u8>>,
}

#[cfg(test)]
Expand Down
29 changes: 20 additions & 9 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,13 @@ fn process_file(
// Set the script command from which to start.
let mut current: Option<Rc<RefCell<Command>>> =
if let Some(action) = context.input_action.take() {
// Continue processing the `N` command.
let mut combined_lines = action.prepend;
combined_lines.push(b'\n');
combined_lines.extend_from_slice(pattern.as_bytes());
// Continue processing the `N` or `n` command.
if let Some(mut combined_lines) = action.prepend {
combined_lines.push(b'\n');
combined_lines.extend_from_slice(pattern.as_bytes());

pattern.set_to_bytes(combined_lines, pattern.is_newline_terminated());
pattern.set_to_bytes(combined_lines, pattern.is_newline_terminated());
}
action.next_command
} else {
// Start from the script top.
Expand Down Expand Up @@ -779,7 +780,17 @@ fn process_file(
list(output, &pattern, width, &command.location, context)?;
}
'n' => {
break;
// Print the pattern space and continue with the next
// command once the next line is read, as `N` does.
if !context.quiet {
write_chunk(output, context, &pattern)?;
}
flush_appends(output, context)?;
context.input_action = Some(InputAction {
next_command: command.next.clone(),
prepend: None,
});
continue 'lines;
}
'N' => {
flush_appends(output, context)?;
Expand All @@ -789,7 +800,7 @@ fn process_file(
// to perform when the next line is read.
context.input_action = Some(InputAction {
next_command: command.next.clone(),
prepend: pattern.as_bytes().to_vec(),
prepend: Some(pattern.as_bytes().to_vec()),
});
continue 'lines;
}
Expand Down Expand Up @@ -943,9 +954,9 @@ fn process_file(
// Take it even with -n, so that it does not carry over to the next file.
if context.separate
&& let Some(action) = context.input_action.take()
&& let Some(mut pending) = action.prepend
&& !context.quiet
{
let mut pending = action.prepend;
pending.push(b'\n');
output.write_bytes(&pending)?;
if context.unbuffered {
Expand Down Expand Up @@ -1005,8 +1016,8 @@ pub fn process_all_files(
&& !context.separate
&& !context.quiet
&& let Some(action) = context.input_action.take()
&& let Some(mut pending) = action.prepend
{
let mut pending = action.prepend;
pending.push(b'\n');
output.write_bytes(&pending)?;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,40 @@ check_output!(pattern_quit_2, [r"5q", LINES1, LINES2]);
check_output!(pattern_re_reuse, ["-n", r"/_1/p;//p", LINES1]);
check_output!(pattern_subst_re_reuse, ["-n", r"/_1/p;s//-N/p", LINES1]);

#[test]
fn test_next_continues_with_following_command() {
new_ucmd!()
.args(&["n;d"])
.pipe_in("1\n2\n3\n4\n")
.succeeds()
.stdout_is("1\n3\n");
new_ucmd!()
.args(&["-n", "-e", "a A", "-e", "n;p"])
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("A\nb\n");
// At the end of input, `n` ends processing.
new_ucmd!()
.args(&["n;q5"])
.pipe_in("1\n")
.succeeds()
.stdout_is("1\n");
}

#[test]
fn test_next_reads_across_files_unless_separate() {
new_ucmd!()
.args(&["n;s/^/X/", "-", "input/two-lines.txt"])
.pipe_in("a\n")
.succeeds()
.stdout_is("a\nXline one\nline two\n");
new_ucmd!()
.args(&["-s", "n;s/^/X/", "-", "input/two-lines.txt"])
.pipe_in("a\n")
.succeeds()
.stdout_is("a\nline one\nXline two\n");
}

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