Skip to content

0r FILE (zero-address r) not accepted #402

@sylvestre

Description

@sylvestre

Bug

The zero-address form 0rFILE is not accepted. GNU treats it as a
synonym for "read FILE before the first input line" — useful for
prepending a file's contents.

Reproduction

$ printf "X\nY\n" > /tmp/r
$ printf "1\n2\n3\n" | /usr/bin/sed '0r /tmp/r'
X
Y
1
2
3

$ printf "1\n2\n3\n" | ./target/release/sed '0r /tmp/r'
sed: <script argument 1>:1:2: error: address 0 requires a second address

The README already documents partial support for address 0:

Address 0 can be used to specify an address range that is already
active on line 1 and can finish with the specified regular expression.

…so 0,/re/ works. The remaining case is 0FILE for the r command
(plus the symmetric 0R once issue #4 lands).

What it should do

0r FILE ≡ "read FILE and queue its contents for output before the
first input line is printed". Different from 1r FILE, which reads
after line 1 is processed.

GNU rejects 0,4r FILE (numeric range starting at 0 — only
regex-second-addresses are allowed for 0,). That rejection is
already present in our parser; we just need to special-case 0 as a
single address for r (and R).

Suspected place to add it

The address parser (search for "address 0 requires" in
src/sed/compiler.rs). Today it errors as soon as a 0 first-address
is seen without a ,. Allow the bare 0 form when the following
command is r (or R), translating it to "execute the read once
before line 1, then deactivate".

Affected GNU testsuite tests

cmd-0r.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions