34 Commits
Author SHA1 Message Date
Sylvestre Ledru 5c98e4d68f docs: move extensions and incompatibilities to docs/src/extensions.md
Start an mdBook under docs/, laid out as in coreutils, and move the list of
extensions and incompatibilities out of the README into it. The README keeps
a pointer.

A Documentation workflow builds the book with mdbook 0.5.0, the version the
uutils website uses, whenever docs/ changes, and uploads it as an artifact.
mdbook 0.5 rejects the `multilingual` field coreutils still carries (the
website strips it before building), so the book leaves it out.
2026-09-26 20:05:52 +02:00
renovate[bot] 76086c8d9a Update dawidd6/action-download-artifact action to v25 2026-09-26 07:14:18 +02:00
Sichen ab2f0a82d8 in-place editing: treat each file as a separate input
sed -i with more than one file treated them as one continuous stream:
line numbers continued across files, $ matched only the last line of
the last file, and N could join lines from different files. GNU sed
documents -i as implying -s, so enable separate-file processing for
--in-place as well as --separate.

There was a second bug when automatic output was suppressed. At the end
of a file, process_file cleared a pending N action only when
!context.quiet. With -n, the action carried into the next file and
joined with its first line. Clear the action in either mode, but write
the pattern space only when automatic output is enabled.

The same bug can be reproduced without -i. With f1 containing a1, a2,
a3 and f2 containing b1, b2, b3:

sed -n -s -e 'N;s/\n/+/;p' f1 f2

Before this change it prints a1+a2, a3+b1, b2+b3. GNU prints a1+a2,
b1+b2.

Add coverage for line numbers, $, ranges, N across file boundaries,
hold space, Q, backup suffixes, and -n.
2026-09-25 15:16:35 +02:00
Jorge Polanco e5474abccf sed: implement the GNU R command (read one line from a file)
The R command reads one line at a time from a named file. All R commands
naming the same file share a single cursor so they read successive lines,
matching GNU sed. Rejected under --sandbox like the other file commands.

Includes unit and integration tests. Makes the GNU sed testsuite `cmd-R`
test pass.
2026-09-25 09:00:25 +02:00
renovate[bot] 961835e509 Update Rust crate rustix to v1.1.5 2026-09-25 07:14:17 +02:00
renovate[bot] 4a73dfb60c Update Rust crate clap_complete to v4.6.11 2026-09-25 07:13:41 +02:00
renovate[bot] a1a146a720 Update Rust crate clap to v4.6.7 2026-09-25 07:12:53 +02:00
Daniel Hofstetter c46dd6dab6 Bump cargo-dist from 0.30.3 to 0.33.0 (#556) 2026-09-21 14:17:18 +02:00
renovate[bot] fcab8f1ca7 Update Rust crate rand to v0.10.3 2026-09-21 07:34:32 +02:00
renovate[bot] cffecd151f Update Rust crate uutests to 0.12.0 2026-09-18 07:40:05 +02:00
renovate[bot] 0339313d49 Update Rust crate uucore to 0.12.0 2026-09-18 07:27:53 +02:00
renovate[bot] cffcc0c806 Update Rust crate divan to v5.0.2 2026-09-18 07:18:34 +02:00
renovate[bot] 643266bc93 Update Rust crate uufuzz to 0.12.0 2026-09-18 07:11:50 +02:00
renovate[bot] 81cf88e601 Update Rust crate console to v0.16.6 2026-09-13 09:44:23 +02:00
Daniel Hofstetter cc5e214878 Bump chacha20 from 0.10.0 to 0.10.2 (#553)
because version 0.10.0 got yanked
2026-09-09 11:32:26 +02:00
Daniel Hofstetter e4a923ac98 Bump getrandom from 0.4.2 to 0.4.3 (#554) 2026-09-09 11:31:56 +02:00
renovate[bot] 77f442ddc2 Update Rust crate uutests to 0.11.0 2026-09-01 15:18:47 +02:00
renovate[bot] dc2ec59ce7 Update Rust crate uufuzz to 0.11.0 2026-09-01 15:05:30 +02:00
renovate[bot] 08b8594355 Update Rust crate uucore to 0.11.0 2026-09-01 13:54:34 +02:00
Diomidis Spinellis f1c5813aa4 Make Unicode listing GNU-sed compatible (#537)
Adjust `l` command to list Unicode characters by default as
octal byte sequences of the corresponding UTF-8 bytes.
A new -U --uutil-extensions option allows listing them
with the common Unicode \u and \U hex escapes.
2026-08-29 23:24:09 +02:00
Sylvestre Ledru 32537dd5d9 ci: generate multibyte locales for the GNU testsuite (#539)
Without en_US.UTF-8, ru_RU.UTF-8 and el_GR.iso88597 the testsuite silently
skips eight multibyte tests, hiding both regressions and improvements.
Also make require_el_iso88597_locale_ probe locale -a instead of always
skipping.
2026-08-27 08:22:48 +02:00
Diomidis SpinellisandMukunda Katta b038f8fb2d Implement GNU W (write pattern first line) command (#531)
While at it, also fix the w command on patterns missing a newline.

Co-authored-by: Mukunda Katta <mukunda.vjcs6@gmail.com>
2026-08-26 16:29:57 +02:00
Diomidis Spinellis b39a9eaa05 Add mandelbrot.sed to integration tests and update goals
Updated README to reflect the addition of mandelbrot.sed in integration tests and improved GNU sed compatibility goals.
2026-08-23 09:37:22 +03:00
renovate[bot] 7be1f77dda Update dawidd6/action-download-artifact action to v24 2026-08-21 10:02:43 +02:00
Diomidis SpinellisandSylvestre Ledru e858951814 Add tests for non-pattern print on deletion
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
2026-08-21 09:51:57 +02:00
xtqqczze a7a303352e fix(deps): limit the feature of sha2 (#534) 2026-08-20 21:08:37 +02:00
Diomidis Spinellis 76efad4aa1 Fix failing test under Windows (#529)
Replace `NamedTempFile` with a regular file inside a `TempDir`, so the
file is closed and can be renamed or replaced by in-place editing on
Windows. `TempDir` also simplifies cleanup by automatically removing
the file, backup, and directory when dropped.
2026-08-20 10:07:47 +02:00
Diomidis Spinellis 79e5df0df8 Document F command extension 2026-08-19 14:32:26 +03:00
Diomidis Spinellis 3f72f93c5d Support F (output filename) command (#528)
This also requires storing input_name as PathBuf to avoid lossy
conversions when outputting an OS-specific byte sequence representing
the file name.
2026-08-19 12:20:27 +02:00
renovate[bot] d837ddf398 Update Rust crate similar to v3.2.0 2026-08-17 13:53:54 +02:00
renovate[bot] b1ca27e82d Update dawidd6/action-download-artifact action to v23 2026-08-16 10:06:49 +02:00
renovate[bot] ce711ac531 Update Rust crate clap_mangen to v0.3.3 2026-08-13 08:11:59 +02:00
renovate[bot] 618a3f7c39 Update Rust crate ctor to v1.0.13 2026-08-10 08:16:11 +02:00
Sylvestre Ledru 079eb9340e Add cargo-binstall support (#520)
Point cargo-binstall at the archives published by dist in the GitHub
releases, so 'cargo binstall sed' fetches a prebuilt binary instead of
building from source.
2026-08-09 10:00:46 +02:00
27 changed files with 1073 additions and 468 deletions

No files matched your search

+14 -1
View File
@@ -49,6 +49,19 @@ jobs:
# Omit installing part of https://github.com/actions/runner-images/tree/main/images/ubuntu
### Locales
# Several testsuite scripts guard themselves with require_*_locale_ and
# silently skip when the locale is missing, which hides both regressions
# and improvements in the multibyte code paths.
- name: Generate the locales required by the testsuite
shell: bash
run: |
## Generate the locales required by the testsuite
sudo localedef -i en_US -f UTF-8 en_US.UTF-8
sudo localedef -i ru_RU -f UTF-8 ru_RU.UTF-8
sudo localedef -i el_GR -f ISO-8859-7 el_GR.iso88597
locale -a
### Build
- name: Build Rust sed binary
shell: bash
@@ -134,7 +147,7 @@ jobs:
persist-credentials: false
- name: Retrieve reference artifacts
uses: dawidd6/action-download-artifact@v21
uses: dawidd6/action-download-artifact@v25
continue-on-error: true
with:
workflow: GnuTests.yml
+41
View File
@@ -0,0 +1,41 @@
name: Documentation
on:
pull_request:
paths:
- 'docs/**'
- '.github/workflows/documentation.yml'
push:
branches:
- main
paths:
- 'docs/**'
- '.github/workflows/documentation.yml'
permissions:
contents: read
jobs:
build-doc:
name: Build the mdBook documentation
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Install mdbook
uses: taiki-e/install-action@v2
with:
tool: mdbook@0.5.0
- name: Build documentation
run: mdbook build docs
- name: Upload documentation
uses: actions/upload-artifact@v7
with:
name: documentation
path: docs/book
+1 -1
View File
@@ -64,7 +64,7 @@ jobs:
# we specify bash to get pipefail; it guards against the `curl` command
# failing. otherwise `sh` won't catch that `curl` returned non-0
shell: bash
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.30.3/cargo-dist-installer.sh | sh"
run: "curl --proto '=https' --tlsv1.2 -LsSf https://github.com/axodotdev/cargo-dist/releases/download/v0.33.0/cargo-dist-installer.sh | sh"
- name: Cache dist
uses: actions/upload-artifact@v7
with:
Generated
+51 -338
View File
File diff suppressed because it is too large. Load diff
+20 -3
View File
@@ -20,6 +20,19 @@ rust-version = "1.88"
build = "build.rs"
# cargo-binstall: fetch the archives published by `dist` in the GitHub releases
# instead of building from source. The layout below has to stay in sync with
# `dist-workspace.toml` (targets) and the archives it produces:
# sed-<target>.tar.xz (zip on Windows), containing a sed-<target>/ directory.
# Note that the release tags carry no `v` prefix, hence `{ version }`.
[package.metadata.binstall]
pkg-url = "{ repo }/releases/download/{ version }/{ name }-{ target }.{ archive-suffix }"
bin-dir = "{ name }-{ target }/{ bin }{ binary-ext }"
pkg-fmt = "txz"
[package.metadata.binstall.overrides.x86_64-pc-windows-msvc]
pkg-fmt = "zip"
[features]
default = ["feat_common_core"]
@@ -45,11 +58,11 @@ phf_codegen = "0.14.0"
predicates = "3.1.3"
rand = { version = "0.10.0" }
regex = "1.10.4"
sha2 = "0.11"
sha2 = { version = "0.11.0", default-features = false, features = ["alloc"] }
tempfile = "3.10.1"
terminal_size = "0.4.2"
textwrap = { version = "0.16.1", features = ["terminal_size"] }
uucore = { version = "0.10.0", features = ["libc"] }
uucore = { version = "0.12.0", features = ["libc"] }
rustix = "1.1.4"
xattr = "1.3.1"
@@ -83,7 +96,7 @@ regex = { workspace = true }
sha2 = { workspace = true }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["entries", "process", "signals", "benchmark"] }
uutests = "0.10.0"
uutests = "0.12.0"
[target.'cfg(unix)'.dev-dependencies]
xattr = { workspace = true }
@@ -117,6 +130,10 @@ inherits = "release"
opt-level = "z"
strip = true
# The profile that 'dist' will build with
[profile.dist]
inherits = "release"
[lints.clippy]
all = { level = "warn", priority = -1 }
+15 -64
View File
@@ -15,22 +15,30 @@ and other extensions.
## Status
At this state _sed_ implements all [POSIX features](https://pubs.opengroup.org/onlinepubs/9799919799/)
and can run correctly the two complex scripts of its integration tests:
[hanoi.sed](https://github.com/uutils/sed/blob/main/tests/fixtures/sed/script/hanoi.sed) (solves the Towers of Hanoi puzzle) and
and can run correctly the three complex scripts of its integration tests:
[hanoi.sed](https://github.com/uutils/sed/blob/main/tests/fixtures/sed/script/hanoi.sed) (solves the Towers of Hanoi puzzle),
[mandelbrot.sed](https://github.com/uutils/sed/blob/main/tests/fixtures/sed/script/mandelbrot.sed), (draws the Mandelbrot set) and
[math.sed](https://github.com/uutils/sed/blob/main/tests/fixtures/sed/script/math.sed) (implements an arbitrary precision integer math calculator).
The performance of this Rust implementation is now better than the GNU and FreeBSD implementations for most benchmarked cases.
Further work aims to:
* Adjust buffering on terminal output to match current implementations,
* Implement more GNU extensions,
* Improve performance where possible.
* improve GNU _sed_ compatibility, especially on the regular expression front,
* implement more GNU extensions, and
* improve performance where possible.
## Installation and Use
We provide a Linux x86_64 binary archive from the main branch at
https://github.com/uutils/sed/releases/tag/latest-commit .
If you have [cargo-binstall](https://github.com/cargo-bins/cargo-binstall),
the released binaries can be installed directly with:
```bash
cargo binstall sed
```
For other platforms, ensure you have Rust installed on your system. You can install Rust through [rustup](https://rustup.rs/).
Clone the repository and build the project using Cargo:
@@ -80,65 +88,8 @@ cargo test
```
## Extensions and incompatibilities
### Supported GNU extensions
* Command-line arguments can be specified in long (`--`) form.
* Spaces can precede a regular expression modifier.
* `I` can be used in as a synonym for the `i` (case insensitive) substitution
flag.
* `M` and `m` substitution flags allow multi-line matching.
* In addition to `\n`, other escape sequences (octal, hex, C) are supported
in the strings of the `y` command.
Under POSIX these yield undefined behavior.
* The `a`, `c`, and `i` commands do not require an initial backslash,
allow text to appear on the same line, and support escape sequences
in the specified text.
* The `a`, `i`, `=`, `l`, `q` and `r` commands support address range as an extension to POSIX.
* The substitution command replacement group `\0` is a synonym for &.
* A `Q` command (optionally followed by an exit code) quits immediately.
* The `q` command can be optionally followed by an exit code.
* The `l` command can be optionally followed by the output width.
* The `--follow-symlinks` option for in-place editing.
* The `--sandbox` option that limits potentially destructive commands.
* 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.
* Address steps can be specified in the form of start~step and start,~step
ranges.
* Address 0 can be used in the `r` command to prepend a file.
### Supported BSD and GNU extensions
* The second address in a range can be specified as a relative address with +N.
* In-place editing of file with the `-i` flag.
### New extensions
* Unicode characters can be specified in regular expression pattern, replacement
and transliteration sequences using `\uXXXX` or `\UXXXXXXXX` sequences.
* The `l` command lists Unicode characters using the `\uXXXX` and `\UXXXXXXXX`
sequences.
### Incompatibilities
* Similarly to GNU _sed_, input is processed as raw bytes or as valid UTF-8
(this includes 7-bit ASCII) based on the locale as specified by the
`LC_ALL`, `LC_CTYPE`, and `LANG` environment variables,
with the default being byte processing.
However, in contrast with GNU _sed_, other locales (e.g. ISO-8859-1)
are not supported. If the input is in another code page or encoding
and requires locale-specific processing (e.g. ignore/map case,
character classes), consider converting it through UTF-8 to ensure
the correct handling of locale-specific regular expressions.
This _sed_ program can also handle arbitrary byte sequences
if no part of the input requires treating it as a Rust String.
* Back-references aren't supported when input is processed as bytes
(`LC_ALL=C`).
* The command will report an error and fail if duplicate labels are found
in the script.
This matches the BSD behavior. The GNU version accepts duplicate labels.
* The last line (`$`) address is interpreted as the last non-empty line of
the last file. If files specified in subsequent arguments until the last
one are empty, then the last line condition will never be triggered.
This behavior is consistent with the
[original implementation](https://github.com/dspinellis/unix-history-repo/blob/Research-V7/usr/src/cmd/sed/sed1.c#L665).
* Labels are parsed for alphanumeric characters. The BSD version parses them
until the end of the line, preventing ; to be used as a separator.
The GNU, BSD and new extensions _sed_ supports, and where it differs from GNU
_sed_, are listed in [docs/src/extensions.md](docs/src/extensions.md).
## GNU test suite compatibility
+2 -2
View File
@@ -4,7 +4,7 @@ members = ["cargo:."]
# Config for 'dist'
[dist]
# The preferred dist version to use in CI (Cargo.toml SemVer syntax)
cargo-dist-version = "0.30.3"
cargo-dist-version = "0.33.0"
# CI backends to support
ci = "github"
# The installers to generate for each app
@@ -13,5 +13,5 @@ installers = []
targets = ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl", "x86_64-pc-windows-msvc"]
# Which actions to run on pull requests
pr-run-mode = "plan"
# Allow dirty workflows to ignore out-of-date contents
# Skip checking whether the specified configuration files are up to date
allow-dirty = ["ci"]
+1
View File
@@ -0,0 +1 @@
book
+7
View File
@@ -0,0 +1,7 @@
[book]
language = "en"
src = "src"
title = "uutils sed Documentation"
[output.html]
git-repository-url = "https://github.com/uutils/sed/tree/main/docs/src"
+5
View File
@@ -0,0 +1,5 @@
# Summary
[Introduction](index.md)
* [Extensions and incompatibilities](extensions.md)
+73
View File
@@ -0,0 +1,73 @@
# Extensions and incompatibilities
The main goal of the project is compatibility with GNU _sed_, but _sed_ also
supports features that GNU _sed_ does not, and differs from it in a few places.
Below is a list of these extensions and incompatibilities.
## Supported GNU extensions
* Command-line arguments can be specified in long (`--`) form.
* Spaces can precede a regular expression modifier.
* `I` can be used in as a synonym for the `i` (case insensitive) substitution
flag.
* `M` and `m` substitution flags allow multi-line matching.
* In addition to `\n`, other escape sequences (octal, hex, C) are supported
in the strings of the `y` command.
Under POSIX these yield undefined behavior.
* The `a`, `c`, and `i` commands do not require an initial backslash,
allow text to appear on the same line, and support escape sequences
in the specified text.
* The `a`, `i`, `=`, `l`, `q` and `r` commands support address range as an extension to POSIX.
* The substitution command replacement group `\0` is a synonym for &.
* An `F` command outputs the name of the file currently being processed.
* A `Q` command (optionally followed by an exit code) quits immediately.
* The `q` command can be optionally followed by an exit code.
* A `W` command writes to a file the pattern's first line.
* An `R` command reads one line at a time from a file.
* The `l` command can be optionally followed by the output width.
* The `--follow-symlinks` option for in-place editing.
* The `--sandbox` option that limits potentially destructive commands.
* 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.
* Address steps can be specified in the form of start~step and start,~step
ranges.
* Address 0 can be used in the `r` command to prepend a file.
## Supported BSD and GNU extensions
* The second address in a range can be specified as a relative address with +N.
* In-place editing of file with the `-i` flag.
## New extensions
* Unicode characters can be specified in regular expression pattern, replacement
and transliteration sequences using `\uXXXX` or `\UXXXXXXXX` sequences.
## Incompatible extensions
The `-U` or `--uutil-extensions` option enables useful extensions or bug fixes
that aren't compatible with GNU sed or POSIX.
* The `l` command lists Unicode characters using the `\uXXXX` and `\UXXXXXXXX`
escapes rather than as octal UTF-8 byte sequences.
## Incompatibilities
* Similarly to GNU _sed_, input is processed as raw bytes or as valid UTF-8
(this includes 7-bit ASCII) based on the locale as specified by the
`LC_ALL`, `LC_CTYPE`, and `LANG` environment variables,
with the default being byte processing.
However, in contrast with GNU _sed_, other locales (e.g. ISO-8859-1)
are not supported. If the input is in another code page or encoding
and requires locale-specific processing (e.g. ignore/map case,
character classes), consider converting it through UTF-8 to ensure
the correct handling of locale-specific regular expressions.
This _sed_ program can also handle arbitrary byte sequences
if no part of the input requires treating it as a Rust String.
* Back-references aren't supported when input is processed as bytes
(`LC_ALL=C`).
* The command will report an error and fail if duplicate labels are found
in the script.
This matches the BSD behavior. The GNU version accepts duplicate labels.
* The last line (`$`) address is interpreted as the last non-empty line of
the last file. If files specified in subsequent arguments until the last
one are empty, then the last line condition will never be triggered.
This behavior is consistent with the
[original implementation](https://github.com/dspinellis/unix-history-repo/blob/Research-V7/usr/src/cmd/sed/sed1.c#L665).
* Labels are parsed for alphanumeric characters. The BSD version parses them
until the end of the line, preventing ; to be used as a separator.
+10
View File
@@ -0,0 +1,10 @@
# uutils sed
_sed_ is a Rust reimplementation of the
[sed utility](https://pubs.opengroup.org/onlinepubs/9799919799/utilities/sed.html)
with some [GNU sed](https://www.gnu.org/software/sed/manual/sed.html),
[FreeBSD sed](https://man.freebsd.org/cgi/man.cgi?sed(1)),
and other extensions.
It is part of the [uutils](https://uutils.github.io/) project. The source code
is on [GitHub](https://github.com/uutils/sed).
+14 -15
View File
@@ -245,9 +245,9 @@ checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "console"
version = "0.16.4"
version = "0.16.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fe5f465a4f6fee88fad41b85d990f84c835335e85b5d9e6e63e0d06d28cba7c"
checksum = "e96a4956774c13c126a8b5af4daa79384f4d826534c95a02d76afb39e2ab64e3"
dependencies = [
"encode_unicode",
"libc",
@@ -820,9 +820,9 @@ checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "rand"
version = "0.10.2"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80"
checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
dependencies = [
"chacha20",
"getrandom 0.4.2",
@@ -897,7 +897,7 @@ checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
[[package]]
name = "sed"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"clap",
"clap_complete",
@@ -985,9 +985,9 @@ checksum = "703d5c7ef118737c72f1af64ad2f6f8c5e1921f818cdcb97b8fe6fc69bf66214"
[[package]]
name = "similar"
version = "3.1.2"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85ee016af5d736b69fc89e19254540fa4b5f5492853fb5503920f084011c78b6"
checksum = "4f66ca1f7aca2474dc10c942eb22feffc897735f54cd1db90138c2fddb490987"
dependencies = [
"bstr",
]
@@ -1034,7 +1034,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom 0.4.2",
"getrandom 0.3.4",
"once_cell",
"rustix",
"windows-sys",
@@ -1152,9 +1152,9 @@ checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "uucore"
version = "0.10.0"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e97dd0fd8bb2ed12affba9f956c3b94b8ad3f9432746717475420e916ae0b8d4"
checksum = "f24a5910ebbf2c0baf486a9125705f8e026a8f7741a3ea8cf5eca3c9257c9bdf"
dependencies = [
"bigdecimal",
"clap",
@@ -1167,7 +1167,6 @@ dependencies = [
"os_display",
"procfs",
"rustc-hash",
"rustix",
"thiserror",
"unic-langid",
"unicode-width",
@@ -1192,9 +1191,9 @@ dependencies = [
[[package]]
name = "uucore_procs"
version = "0.10.0"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "907f1b828046ea42cdb4bd898f05b48ab873a41937bf75ebe12f09a5bf411f58"
checksum = "50aa1f3bba55168888ae6f1f2714ce58897d8020d742d21f474f01e86468fedd"
dependencies = [
"proc-macro2",
"quote",
@@ -1202,9 +1201,9 @@ dependencies = [
[[package]]
name = "uufuzz"
version = "0.10.0"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72d4c5bac0f8ccabaa8e8ccbf2724446f8b007c06ad343fabfe1a1525a4ff4f2"
checksum = "7147da7bd2de08e6647116e005b0992cc602e34c34972793844a45c4e8648dd9"
dependencies = [
"console",
"rand",
+2 -2
View File
@@ -14,8 +14,8 @@ libc = "0.2.153"
tempfile = "3.15.0"
rand = { version = "0.10.0" }
similar = "3.0.0"
uucore = { version = "0.10.0", features = ["libc"] }
uufuzz = "0.10.0"
uucore = { version = "0.12.0", features = ["libc"] }
uufuzz = "0.12.0"
sed = { path = ".." }
+8 -2
View File
@@ -10,6 +10,7 @@
use crate::sed::error_handling::{ScriptLocation, runtime_error};
use crate::sed::fast_regex::{Captures, Match, Regex};
use crate::sed::named_reader::NamedReader;
use crate::sed::named_writer::NamedWriter;
use crate::sed::script_char_provider::ScriptCharProvider;
use crate::sed::script_line_provider::ScriptLineProvider;
@@ -38,10 +39,11 @@ pub struct ProcessingContext {
pub sandbox: bool,
pub unbuffered: bool,
pub null_data: bool,
pub uutil_extensions: bool,
// Other context
/// Currently processed input file name (not script) in quoted form
pub input_name: String,
/// Currently processed input file name (not script)
pub input_name: PathBuf,
/// Current input line number
pub line_number: usize,
/// True if this is the last address of a range
@@ -66,6 +68,9 @@ pub struct ProcessingContext {
pub parsed_block_nesting: usize,
/// Command associated with each label
pub label_to_command_map: HashMap<String, Rc<RefCell<Command>>>,
/// Shared `R` readers keyed by file path, so that all `R` commands naming
/// the same file advance a single cursor, matching GNU sed.
pub named_readers: HashMap<PathBuf, Rc<RefCell<NamedReader>>>,
/// Commands with a (latchable and resetable) address range
pub range_commands: Vec<Rc<RefCell<Command>>>,
/// True if a substitution was made as specified in the t command
@@ -367,6 +372,7 @@ pub enum CommandData {
BranchTarget(Option<Rc<RefCell<Command>>>), // Commands for 'b', 't', 'T', '{'
Label(Option<String>), // Label name for 'b', 't', 'T', ':'
Path(PathBuf), // File path for 'r'
NamedReader(Rc<RefCell<NamedReader>>), // Successive file lines for 'R'
NamedWriter(Rc<RefCell<NamedWriter>>), // File output for 'w'
Number(usize), // Number for 'l', 'q', 'Q' (GNU)
Substitution(Box<Substitution>), // Substitute command 's'
+60
View File
@@ -18,6 +18,7 @@ use crate::sed::delimited_parser::{
};
use crate::sed::error_handling::{ScriptLocation, compilation_error, semantic_error};
use crate::sed::fast_regex::Regex;
use crate::sed::named_reader::NamedReader;
use crate::sed::named_writer::NamedWriter;
use crate::sed::script_char_provider::ScriptCharProvider;
use crate::sed::script_line_provider::{ScriptLineProvider, ScriptValue};
@@ -1122,6 +1123,28 @@ fn compile_read_file_command(
Ok(CommandHandling::Continue)
}
// Handles R
fn compile_read_line_command(
lines: &mut ScriptLineProvider,
line: &mut ScriptCharProvider,
cmd: &mut Command,
context: &mut ProcessingContext,
) -> UResult<CommandHandling> {
if context.sandbox {
return compilation_error(lines, line, ERR_SANDBOX);
}
let path = read_file_path(lines, line)?;
// Reuse a reader keyed by path so all `R` commands on the same file share
// one cursor (see `ProcessingContext::named_readers`).
let reader = context
.named_readers
.entry(path.clone())
.or_insert_with(|| NamedReader::new(path))
.clone();
cmd.data = CommandData::NamedReader(reader);
Ok(CommandHandling::Continue)
}
// Handles w
fn compile_write_file_command(
lines: &mut ScriptLineProvider,
@@ -1632,6 +1655,14 @@ fn get_cmd_spec(
n_addr: 2,
handler: compile_execute_command,
}),
'F' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_empty_command,
}),
'R' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_read_line_command,
}),
'r' => Ok(CommandSpec {
n_addr: if posix { 1 } else { 2 },
handler: compile_read_file_command,
@@ -1644,6 +1675,10 @@ fn get_cmd_spec(
n_addr: 2,
handler: compile_label_command,
}),
'W' if !posix => Ok(CommandSpec {
n_addr: 2,
handler: compile_write_file_command,
}),
'w' => Ok(CommandSpec {
n_addr: 2,
handler: compile_write_file_command,
@@ -3013,6 +3048,31 @@ mod tests {
assert!(err.to_string().contains(ERR_SANDBOX));
}
// compile_read_line_command (R)
#[test]
fn test_compile_read_line_command_rejected_under_sandbox() {
let (mut lines, mut chars) = make_providers("R input.txt");
let mut cmd = Command::default();
let mut context = ctx();
context.sandbox = true;
let err =
compile_read_line_command(&mut lines, &mut chars, &mut cmd, &mut context).unwrap_err();
assert!(err.to_string().contains(ERR_SANDBOX));
}
#[test]
fn test_compile_read_line_command_sets_named_reader() {
let (mut lines, mut chars) = make_providers("R input.txt");
let mut cmd = Command::default();
let mut context = ctx();
let handling =
compile_read_line_command(&mut lines, &mut chars, &mut cmd, &mut context).unwrap();
assert!(matches!(handling, CommandHandling::Continue));
assert!(matches!(cmd.data, CommandData::NamedReader(_)));
}
// compile_write_file_command
#[test]
fn test_compile_write_file_command_rejected_under_sandbox() {
+2 -1
View File
@@ -14,6 +14,7 @@ use crate::sed::script_line_provider::ScriptLineProvider;
use std::rc::Rc;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError};
#[derive(Clone, Debug)]
@@ -108,7 +109,7 @@ pub fn input_runtime_error<T>(
location.input_name,
location.line_number,
location.column_number,
context.input_name,
context.input_name.quote(),
context.line_number,
msg.to_string()
),
+13 -2
View File
@@ -15,6 +15,7 @@ pub mod error_handling;
pub mod fast_io;
pub mod fast_regex;
pub mod in_place;
pub mod named_reader;
pub mod named_writer;
pub mod processor;
pub mod script_char_provider;
@@ -114,6 +115,11 @@ pub fn uu_app() -> Command {
arg!(-s --separate "Consider files as separate rather than as a long stream."),
arg!(--sandbox "Operate in a sandbox by disabling e/r/w commands."),
arg!(-u --unbuffered "Load minimal input data and flush output buffers regularly."),
Arg::new("uutil-extensions")
.short('U')
.long("uutil-extensions")
.help("Enable incompatible extensions.")
.action(clap::ArgAction::SetTrue),
Arg::new("null-data")
.short('z')
.long("null-data")
@@ -239,16 +245,17 @@ fn build_context(matches: &ArgMatches) -> UResult<ProcessingContext> {
length: matches.get_one::<u32>("length").map_or(70, |v| *v as usize),
quiet: matches.get_flag("quiet"),
posix: matches.get_flag("posix"),
separate: matches.get_flag("separate"),
separate: matches.get_flag("separate") || matches.contains_id("in-place"),
sandbox: matches.get_flag("sandbox"),
unbuffered: matches.get_flag("unbuffered"),
null_data: matches.get_flag("null-data"),
uutil_extensions: matches.get_flag("uutil-extensions"),
// Environment
character_mode: character_mode_for_locale(&locale)?,
// Other context
input_name: "<stdin>".to_string(),
input_name: PathBuf::from("-"),
line_number: 0,
last_address: false,
last_line: false,
@@ -262,6 +269,7 @@ fn build_context(matches: &ArgMatches) -> UResult<ProcessingContext> {
},
parsed_block_nesting: 0,
label_to_command_map: HashMap::new(),
named_readers: HashMap::new(),
range_commands: Vec::new(),
substitution_made: false,
append_elements: Vec::new(),
@@ -389,6 +397,7 @@ mod tests {
assert!(!ctx.sandbox);
assert!(!ctx.unbuffered);
assert!(!ctx.null_data);
assert!(!ctx.uutil_extensions);
}
#[test]
@@ -406,6 +415,7 @@ mod tests {
"-s",
"--sandbox",
"-u",
"-U",
"-z",
]);
@@ -424,6 +434,7 @@ mod tests {
assert!(ctx.sandbox);
assert!(ctx.unbuffered);
assert!(ctx.null_data);
assert!(ctx.uutil_extensions);
}
#[test]
+101
View File
@@ -0,0 +1,101 @@
// An abstraction for input files read one line at a time by the `R` command
//
// SPDX-License-Identifier: MIT
// Copyright (c) 2025 Diomidis Spinellis
//
// This file is part of the uutils sed package.
// It is licensed under the MIT License.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
use std::cell::RefCell;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::rc::Rc;
#[derive(Debug)]
/// State of the file backing an `R` command, opened lazily on first use.
enum State {
Unopened,
Open(BufReader<File>),
Exhausted,
}
#[derive(Debug)]
/// Reader that yields successive lines of a file for the GNU `R` command.
/// The file is opened on first use; a file that cannot be opened or read is
/// treated as having no more lines, matching GNU sed (no error is raised).
pub struct NamedReader {
path: PathBuf,
state: State,
}
impl NamedReader {
/// Create a reader for `path` without opening it yet.
pub fn new(path: PathBuf) -> Rc<RefCell<Self>> {
Rc::new(RefCell::new(NamedReader {
path,
state: State::Unopened,
}))
}
/// Return the next line of the file, including its trailing newline if
/// present, or `None` once the file is exhausted or could not be read.
pub fn next_line(&mut self) -> Option<Vec<u8>> {
if matches!(self.state, State::Unopened) {
self.state = match File::open(&self.path) {
Ok(file) => State::Open(BufReader::new(file)),
Err(_) => State::Exhausted,
};
}
let State::Open(reader) = &mut self.state else {
return None;
};
let mut line = Vec::new();
match reader.read_until(b'\n', &mut line) {
Ok(0) | Err(_) => {
self.state = State::Exhausted;
None
}
Ok(_) => Some(line),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn yields_successive_lines_then_none() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"one\ntwo\n").unwrap();
let reader = NamedReader::new(file.path().to_path_buf());
assert_eq!(reader.borrow_mut().next_line(), Some(b"one\n".to_vec()));
assert_eq!(reader.borrow_mut().next_line(), Some(b"two\n".to_vec()));
assert_eq!(reader.borrow_mut().next_line(), None);
assert_eq!(reader.borrow_mut().next_line(), None);
}
#[test]
fn last_line_without_newline_is_preserved() {
let mut file = NamedTempFile::new().unwrap();
file.write_all(b"abc").unwrap();
let reader = NamedReader::new(file.path().to_path_buf());
assert_eq!(reader.borrow_mut().next_line(), Some(b"abc".to_vec()));
assert_eq!(reader.borrow_mut().next_line(), None);
}
#[test]
fn missing_file_yields_no_lines() {
let reader = NamedReader::new(PathBuf::from("/nonexistent/xyzzy-42-does-not-exist"));
assert_eq!(reader.borrow_mut().next_line(), None);
}
}
+31 -7
View File
@@ -55,16 +55,22 @@ impl NamedWriter {
Ok(writer)
}
/// Write a line to the file with a newline, returning descriptive errors.
pub fn write_line(&mut self, line: &str) -> UResult<()> {
self.write_line_bytes(line.as_bytes())
/// Write String to the file, possibly with a newline, returning errors.
pub fn write_line(&mut self, line: &str, newline: bool) -> UResult<()> {
self.write_line_bytes(line.as_bytes(), newline)
}
/// Write bytes to the file with a newline, returning descriptive errors.
pub fn write_line_bytes(&mut self, line: &[u8]) -> UResult<()> {
/// Write bytes to the file, possibly with a newline, returning errors.
pub fn write_line_bytes(&mut self, line: &[u8], newline: bool) -> UResult<()> {
self.writer
.write_all(line)
.and_then(|()| self.writer.write_all(b"\n"))
.and_then(|()| {
if newline {
self.writer.write_all(b"\n")
} else {
Ok(())
}
})
.map_err(|e| {
runtime_error::<()>(
&self.location,
@@ -109,9 +115,27 @@ mod tests {
let path = file.path().to_path_buf();
let writer = NamedWriter::new(path.clone(), ScriptLocation::default()).unwrap();
writer.borrow_mut().write_line_bytes(b"a\xE9").unwrap();
writer
.borrow_mut()
.write_line_bytes(b"a\xE9", true)
.unwrap();
writer.borrow_mut().flush().unwrap();
assert_eq!(fs::read(path).unwrap(), b"a\xE9\n");
}
#[test]
fn test_write_line_bytes_appends_no_newline() {
let file = NamedTempFile::new().unwrap();
let path = file.path().to_path_buf();
let writer = NamedWriter::new(path.clone(), ScriptLocation::default()).unwrap();
writer
.borrow_mut()
.write_line_bytes(b"a\xE9", false)
.unwrap();
writer.borrow_mut().flush().unwrap();
assert_eq!(fs::read(path).unwrap(), b"a\xE9");
}
}
Loaded 20 of 27 files, more files were not shown because too many files have changed in this diff. Show more