Skip to content
Draft
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
9 changes: 9 additions & 0 deletions src/sed/fast_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ impl OutputBuffer {
)))
}

/// Schedule the specified text for output unchanged, after any
/// deferred newline.
pub fn write_raw(&mut self, bytes: &[u8]) -> io::Result<()> {
self.flush_pending_newline()?;
#[cfg(unix)]
self.flush_mmap(WriteRange::Complete)?;
self.out.write_all(bytes)
}

/// Copy the specified file to the output.
pub fn copy_file(&mut self, path: &PathBuf) -> io::Result<()> {
// Flush mmap writes, if any.
Expand Down
44 changes: 30 additions & 14 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,15 @@ fn transliterate(

/// Output any data queued for output at the end of the cycle.
fn flush_appends(output: &mut OutputBuffer, context: &mut ProcessingContext) -> UResult<()> {
if !context.append_elements.is_empty() {
// As in GNU sed, end a line that lacks its newline before the queued
// output, even when an `r` file is empty or unreadable.
output.flush_pending_newline()?;
}
for elem in &context.append_elements {
match elem {
AppendElement::Text(text) => {
output.write_bytes(text.as_ref())?;
output.write_raw(text.as_ref())?;
}
AppendElement::Path(path) => {
output.copy_file(path)?;
Expand Down Expand Up @@ -567,9 +572,7 @@ fn list(
) -> UResult<()> {
// Special case for an empty pattern space
if line.is_empty() {
if line.is_newline_terminated() {
output.write_str("$\n")?;
}
output.write_str("$\n")?;
return Ok(());
}

Expand Down Expand Up @@ -654,12 +657,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 @@ -737,7 +741,7 @@ fn process_file(
}
CommandData::Text(cmd_bytes) => {
let shell_out = shell_stdout(cmd_bytes.to_vec(), &command, context)?;
output.write_bytes(&shell_out)?;
output.write_raw(&shell_out)?;
}
_ => panic!("invalid 'e' command data"),
},
Expand Down Expand Up @@ -779,7 +783,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 +803,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 @@ -819,6 +833,8 @@ fn process_file(
);
context.stop_processing = true;
context.quiet = true;
// Like GNU sed, discard text queued by `a`, `r` and `R`.
context.append_elements.clear();
break;
}
'R' => {
Expand Down Expand Up @@ -943,9 +959,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 +1021,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
160 changes: 160 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 Expand Up @@ -3090,3 +3124,129 @@ fn test_posix_reject_flags() {
.code_is(1)
.stderr_is("sed: <script argument 1>:1:7: error: unknown option to 's'\n");
}

/// `r` first writes a newline that the line lacks, even when the file is
/// missing, and then copies the file unchanged, as GNU sed does.
#[test]
fn read_file_after_line_without_newline() -> std::io::Result<()> {
let dir = tempfile::tempdir()?;
let (one, two, text) = (
dir.path().join("1"),
dir.path().join("2"),
dir.path().join("text"),
);
fs::write(&one, "a")?;
fs::write(&two, "b\n")?;
fs::write(&text, "X\nY")?;
new_ucmd!()
.arg(format!("1r {}", text.display()))
.arg(&one)
.arg(&two)
.succeeds()
.stdout_is("a\nX\nYb\n");

let missing = dir.path().join("missing");
new_ucmd!()
.arg(format!("r {}", missing.display()))
.pipe_in("x")
.succeeds()
.stdout_is("x\n");
// `n` reads the next line without starting a new cycle, so `r` is not
// applied to it.
new_ucmd!()
.args(&["-e", &format!("r {}", missing.display()), "-e", "n"])
.pipe_in("x\ny")
.succeeds()
.stdout_is("x\ny");

// Text queued by `R` that lacks a newline is not ended before `r`.
let unterminated = dir.path().join("unterminated");
fs::write(&unterminated, "R")?;
new_ucmd!()
.args(&["-e", &format!("R {}", unterminated.display())])
.args(&["-e", &format!("r {}", text.display())])
.pipe_in("x\ny\n")
.succeeds()
.stdout_is("x\nRX\nYy\nX\nY");
new_ucmd!()
.args(&["-n", "-e", &format!("R {}", unterminated.display())])
.args(&["-e", &format!("r {}", text.display())])
.pipe_in("x\ny\n")
.succeeds()
.stdout_is("RX\nYX\nY");
Ok(())
}

/// `Q` discards text queued by `a`, `r` and `R`, as GNU sed does.
#[test]
fn quit_silently_discards_appended_text() -> std::io::Result<()> {
new_ucmd!()
.args(&["-e", "a A", "-e", "Q"])
.pipe_in("x\n")
.succeeds()
.stdout_is("");

let dir = tempfile::tempdir()?;
let (one, two, text) = (
dir.path().join("1"),
dir.path().join("2"),
dir.path().join("text"),
);
fs::write(&one, "a")?;
fs::write(&two, "b\n")?;
fs::write(&text, "X\n")?;
new_ucmd!()
.args(&["-e", &format!("R {}", text.display()), "-e", "Q"])
.pipe_in("x\n")
.succeeds()
.stdout_is("");
new_ucmd!()
.args(&["-e", &format!("2r {}", text.display()), "-e", "2Q"])
.arg(&one)
.arg(&two)
.succeeds()
.stdout_is("a");
Ok(())
}

/// `R` copies a line lacking a newline unchanged, as GNU sed does.
#[test]
fn read_one_line_without_newline_is_copied_unchanged() -> std::io::Result<()> {
let temp = NamedTempFile::new()?;
fs::write(temp.path(), "x")?;
new_ucmd!()
.arg(format!("R {}", temp.path().display()))
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("a\nxb\n");
Ok(())
}

/// The output of the `e` command is copied unchanged, as GNU sed does.
#[cfg(unix)]
#[test]
fn execute_command_output_is_copied_unchanged() {
new_ucmd!()
.arg("1e printf hi")
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("hia\nb\n");
// A line that lacks its newline is ended before the output, even when
// there is none.
new_ucmd!()
.args(&["-n", "p;e true"])
.pipe_in("a")
.succeeds()
.stdout_is("a\n");
}

/// `l` shows an empty pattern space as `$`, even when the line lacked a
/// newline, as GNU sed does.
#[test]
fn list_empty_line_without_newline() {
new_ucmd!()
.args(&["-n", "s/a//;l"])
.pipe_in("a")
.succeeds()
.stdout_is("$\n");
}
Loading