Skip to content

Add F command (print current input filename) #393

@sylvestre

Description

@sylvestre

Bug

The GNU F command — print the current input filename, followed by a
newline — is not implemented.

Reproduction

$ echo abc | /usr/bin/sed -n 'F' /etc/hostname
/etc/hostname

$ echo abc | ./target/release/sed -n 'F' /etc/hostname
sed: <script argument 1>:1:1: error: invalid command code `F'

What it should do

From GNU sed manual:

F Print out the file name of the current input file (with a trailing newline).

Behaviour notes:

  • Output goes to stdout (like =, p).
  • For stdin, GNU prints -.
  • Honors -z (NUL-separated output) — see nulldata.sh.
  • Accepts an address range like other GNU extensions.
  • Rejected under --posix (already handled by the existing
    bad_command(OPT) path; once F is added, the --posix rejection
    test in compile-errors will keep working as expected).

Suspected place to add it

src/sed/compiler.rs:1276get_cmd_spec:

'F' => Ok(CommandSpec {
    n_addr: 2,
    handler: compile_empty_command,   // F takes no args
}),

…plus a corresponding execution case in src/sed/processor.rs (search
for the match cmd.code near processor.rs:635). The processor
already has access to the current input filename via the same path that
-i uses to compute backup names — see src/sed/in_place.rs for how
filenames are tracked.

Affected GNU testsuite tests

follow-symlinks, nulldata. Also unblocks the F parts of
follow-symlinks-stdin.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions