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
3 changes: 3 additions & 0 deletions src/sed/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ pub struct ProcessingContext {
// Other context
/// Currently processed input file name (not script) in quoted form
pub input_name: String,
/// Currently processed input file name as supplied, for commands that
/// print it directly.
pub raw_input_name: String,
/// Current input line number
pub line_number: usize,
/// True if this is the last address of a range
Expand Down
4 changes: 4 additions & 0 deletions src/sed/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,10 @@ fn get_cmd_spec(
n_addr: 2,
handler: compile_empty_command,
}),
'F' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_empty_command,
}),
'l' => Ok(CommandSpec {
n_addr: 2,
handler: compile_number_command,
Expand Down
1 change: 1 addition & 0 deletions src/sed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn build_context(matches: &ArgMatches) -> ProcessingContext {

// Other context
input_name: "<stdin>".to_string(),
raw_input_name: "-".to_string(),
line_number: 0,
last_address: false,
last_line: false,
Expand Down
5 changes: 5 additions & 0 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,10 @@ fn process_file(
pattern.clear();
break;
}
'F' => {
let separator = if context.null_data { '\0' } else { '\n' };
output.write_str(format!("{}{}", context.raw_input_name, separator))?;
}
'g' => {
// Replace pattern with the contents of the hold space.
pattern.set_to_string(context.hold.content.clone(), context.hold.has_newline);
Expand Down Expand Up @@ -705,6 +709,7 @@ pub fn process_all_files(
}

context.input_name = path.quote().to_string();
context.raw_input_name = path.to_string_lossy().to_string();
process_file(commands.clone(), &mut reader, output, context)?;

// Handle any N command remains.
Expand Down
35 changes: 35 additions & 0 deletions tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,41 @@ check_output!(pattern_next_output, ["-e", r"4n", LINES1]);
check_output!(pattern_next_no_output, ["-n", "-e", r"4n", LINES1]);
check_output!(pattern_next_print_output, ["-e", r"4n;p", LINES1]);
check_output!(pattern_next_print_no_output, ["-n", "-e", r"4n;p", LINES1]);

#[test]
fn pattern_print_filename_with_f_command() {
new_ucmd!()
.args(&["-n", "F", LINES1])
.succeeds()
.stdout_is("input/lines1\n".repeat(14));
}

#[test]
fn pattern_print_filename_with_f_command_stdin() {
new_ucmd!()
.args(&["-n", "F"])
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("-\n-\n");
}

#[test]
fn pattern_print_filename_with_f_command_null_data() {
new_ucmd!()
.args(&["-z", "-n", "F"])
.pipe_in("a\0b\0")
.succeeds()
.stdout_is("-\0");
}

#[test]
fn pattern_print_filename_with_f_command_is_non_posix() {
new_ucmd!()
.args(&["--posix", "F"])
.fails()
.code_is(1)
.stderr_is("sed: <script argument 1>:1:1: error: invalid command code `F'\n");
}
check_output!(pattern_quit, [r"5q", LINES1]);
check_output!(pattern_quit_2, [r"5q", LINES1, LINES2]);
check_output!(pattern_re_reuse, ["-n", r"/_1/p;//p", LINES1]);
Expand Down
Loading