Skip to content

Add R command (read one line from file) #394

@sylvestre

Description

@sylvestre

Bug

The GNU R command (read one line from a file each time it is
executed, queue it for output after the current cycle) is not
implemented. It is the single-line counterpart to r.

Reproduction

$ printf "x\ny\n" > a; printf "1\n2\n" > b
$ /usr/bin/sed -e '1Rb' -e '2Rb' a
x
1
y
2

$ ./target/release/sed -e '1Rb' a
sed: <script argument 1>:1:2: error: invalid command code `R'

What it should do

From the GNU manual:

R FILENAME Queue a line of FILENAME to be read and inserted into
the output stream at the end of the current cycle, or when the next
input line is read. Note that if FILENAME cannot be read, or if its
end is reached, no line is appended, without any error indication.

Each execution reads exactly one line (until newline) and queues it.
Reading a non-existent file is silently ignored, and reaching EOF on
subsequent calls is silently ignored.

Suspected place to add it

src/sed/compiler.rs:1276get_cmd_spec. There is already an r
command using compile_read_file_command at line 1333. Add:

'R' => Ok(CommandSpec {
    n_addr: 2,
    handler: compile_read_file_command,   // same handler, different exec semantics
}),

Then in processor.rs, the execution side needs to remember a per-R
file handle and read one line per invocation. The existing r command
slurps the whole file at the end of the cycle; R instead does
per-cycle line reads.

Affected GNU testsuite tests

cmd-R.

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