Skip to content

Add v command (version assertion) #397

@sylvestre

Description

@sylvestre

Bug

The GNU v command — assert that the running sed is at least a given
version — is not implemented. It is essentially a no-op (succeeds if
the version is high enough, errors otherwise) and is commonly used at
the top of GNU sed scripts as a safety check.

Reproduction

$ echo a | /usr/bin/sed 'v4.0'
a                  # accepted, prints input as usual

$ echo a | ./target/release/sed 'v4.0'
sed: <script argument 1>:1:1: error: invalid command code `v'

Error case (asserting a future version):

$ echo a | /usr/bin/sed 'v9.0'
sed: -e expression #1, char 4: expected newer version of sed

$ echo a | ./target/release/sed 'v9.0'
sed: <script argument 1>:1:1: error: invalid command code `v'

What it should do

From the GNU manual:

v VERSION This command does nothing, but makes sed fail if GNU
extensions are not supported, simply because other implementations
of sed do not understand it. […] If specified, VERSION must be no
newer than the version of GNU sed currently parsing the script;
otherwise, the script will be aborted.

Argument is optional. When absent, the command succeeds (just an
extension assertion). When present, parse MAJOR[.MINOR[.PATCH]] and
compare to a constant in the source.

A reasonable choice for the "current version" constant is 4.8 or
4.9 (matching what GNU advertises) — pick whichever lets the most
real-world scripts run unmodified, since the comparison is "no newer
than".

Suspected place to add it

src/sed/compiler.rs:1276get_cmd_spec. Add:

'v' => Ok(CommandSpec {
    n_addr: 0,
    handler: compile_version_command,   // new
}),

The handler reads the rest of the line as an optional version string
and stashes the version comparison result. Execution is a no-op
(the version check happens at compile time and just errors out then).

Rejected under --posix.

Affected GNU testsuite tests

compile-tests, stdin-prog (uses v9 in a probe), and any GNU
script in the wild that starts with v.

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