uutils/sed

Add `v` command (version assertion)

Open

#397 建立於 2026年4月30日

在 GitHub 查看
 (0 留言) (0 反應) (0 負責人)Rust (23 fork)github user discovery
good first issue

倉庫指標

Star
 (91 star)
PR 合併指標
 (平均合併 6天 9小時) (30 天內合併 25 個 PR)

描述

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.

貢獻者指南