Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/sed/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct ProcessingContext {
pub null_data: bool,

// Other context
/// Currently processed input file name (not script) in quoted form
pub input_name: String,
/// Currently processed input file name (not script)
pub input_name: PathBuf,
/// 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 @@ -1632,6 +1632,10 @@ fn get_cmd_spec(
n_addr: 2,
handler: compile_execute_command,
}),
'F' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_empty_command,
}),
'r' => Ok(CommandSpec {
n_addr: if posix { 1 } else { 2 },
handler: compile_read_file_command,
Expand Down
3 changes: 2 additions & 1 deletion src/sed/error_handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::sed::script_line_provider::ScriptLineProvider;

use std::rc::Rc;

use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -108,7 +109,7 @@ pub fn input_runtime_error<T>(
location.input_name,
location.line_number,
location.column_number,
context.input_name,
context.input_name.quote(),
context.line_number,
msg.to_string()
),
Expand Down
2 changes: 1 addition & 1 deletion src/sed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ fn build_context(matches: &ArgMatches) -> UResult<ProcessingContext> {
character_mode: character_mode_for_locale(&locale)?,

// Other context
input_name: "<stdin>".to_string(),
input_name: PathBuf::from("-"),
line_number: 0,
last_address: false,
last_line: false,
Expand Down
14 changes: 10 additions & 4 deletions src/sed/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ fn process_file(
}
_ => panic!("invalid 'e' command data"),
},
'F' => {
// Output current input file name.
let mut bytes = context.input_name.as_os_str().as_encoded_bytes().to_vec();
Comment thread
dspinellis marked this conversation as resolved.
bytes.push(b'\n');
output.write_bytes(&bytes)?;
Comment thread
dspinellis marked this conversation as resolved.
}
'g' => {
// Replace pattern with the contents of the hold space.
pattern.set_to_bytes(context.hold.content.clone(), context.hold.has_newline);
Expand Down Expand Up @@ -945,11 +951,11 @@ pub fn process_all_files(
let mut in_place = InPlace::new(context.clone());
let last_file_index = files.len() - 1;

for (index, path) in files.iter().enumerate() {
for (index, path) in files.into_iter().enumerate() {
context.last_file = index == last_file_index;
let mut reader = LineReader::open(path)
let mut reader = LineReader::open(&path)
.map_err_context(|| format!("error opening input file {}", path.quote()))?;
let output = in_place.begin(path)?;
let output = in_place.begin(&path)?;

if context.separate || index == 0 {
context.line_number = 0;
Expand All @@ -960,7 +966,7 @@ pub fn process_all_files(
context.hold.has_newline = true;
}

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

// Handle any N command remains.
Expand Down
24 changes: 23 additions & 1 deletion tests/by-util/test_sed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ fn write_two_files() -> std::io::Result<()> {
}

////////////////////////////////////////////////////////////
// =, l commands
// =, l, F commands
check_output!(number_continuous, ["/l2_/=", LINES1, LINES2]);
check_output!(number_separate, ["-s", "/l._8/=", LINES1, LINES2]);
check_output!(number_range, ["-e", "10,12=", LINES1]);
Expand All @@ -1754,6 +1754,28 @@ check_output!(number_range_out_of_bounds, ["-e", "47,60=", LINES1]);
check_output!(list_ascii, ["-n", "l 60", "input/ascii"]);
check_output!(list_empty, ["-n", "l 60", "input/empty"]);

check_output!(filename_file, ["-n", r"F", LINES1]);
// Non-ASCII filename
check_output!(filename_αρχείο1, [r"F", "input/αρχείο1"]);

Comment thread
dspinellis marked this conversation as resolved.
#[test]
fn filename_stdin() {
new_ucmd!()
.args(&["-n", "F"])
.pipe_in("a\nb\n")
.succeeds()
.stdout_is("-\n-\n");
}

#[test]
fn filename_non_posix() {
new_ucmd!()
.args(&["--posix", "F"])
.fails()
.code_is(1)
.stderr_contains("invalid command code");
}

/// List Unicode input under an explicit UTF-8 locale.
#[test]
fn list_unicode() {
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures/sed/input/αρχείο1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
l1_1
l1_2
l1_3
l1_4
l1_5
l1_6
l1_7
l1_8
l1_9
l1_10
l1_11
l1_12
l1_13
l1_14
14 changes: 14 additions & 0 deletions tests/fixtures/sed/output/filename_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
input/lines1
28 changes: 28 additions & 0 deletions tests/fixtures/sed/output/filename_αρχείο1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
input/αρχείο1
l1_1
input/αρχείο1
l1_2
input/αρχείο1
l1_3
input/αρχείο1
l1_4
input/αρχείο1
l1_5
input/αρχείο1
l1_6
input/αρχείο1
l1_7
input/αρχείο1
l1_8
input/αρχείο1
l1_9
input/αρχείο1
l1_10
input/αρχείο1
l1_11
input/αρχείο1
l1_12
input/αρχείο1
l1_13
input/αρχείο1
l1_14
Loading