Commit Graph
104 Commits
Author SHA1 Message Date
Sylvestre Ledru 3bdf96d08d sed: keep script position for errors at end of script
An error found once the script is exhausted, like an unterminated
command, was reported as ":0:N" with no input name. Keep the last input
name and line number so the error points at the right line.
2026-09-27 18:50:16 +02:00
Sylvestre Ledru 72ed709088 sed: fix panics and zero steps in ~ and , addresses
A range address that ended early made the parser panic: `1~` read past
the end of the line, and `1~p`, `1,~p` or `1,p` handed a command letter
to the address parser, which only knew how to panic on it. A missing
~step now counts as 0, and a comma with no address after it is reported
as an unexpected `,'. The address parser returns an error instead of
panicking.

A step of 0 was not handled either: `2,~0` ran to the end of the input
instead of selecting line 2 alone, and `0~0`, which can select no line,
was accepted.
2026-09-27 16:44:36 +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
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
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 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
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 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
Mukunda Rao Katta 91465e566b fix: identify uutils in cli output (#435) 2026-08-09 00:55:37 +02:00
Diomidis Spinellisandmukunda katta a5d219764f feat: add T command (branch on no-substitution) (#508)
* feat: add T command (branch on no-substitution)

Implements the GNU `T LABEL` command: branch to LABEL if no s///
substitution has succeeded since the last input line was read or the
last t/T was taken. It is the inverse of `t` and shares the same
address and label parsing.

Wires 'T' into get_cmd_spec, resolve_branch_targets, and the processor
branch logic, resetting the substitution flag on evaluation just like
't'. Adds integration tests (simple branch + flag-clear) and a cmd-spec
unit test.

Closes #395

Co-authored-by: Diomidis Spinellis <dds@aueb.gr>

* ci: re-trigger checks (windows runner crates.io download flake)

* Disable T in POSIX mode

---------

Co-authored-by: mukunda katta <mukunda.vjcs6@gmail.com>
2026-08-02 17:47:18 +03:00
Develandpre-commit-ci[bot] 397ab86302 Implement v command #397 (#420)
* implement version command

* add tests for version option

* fix patch comparsion

* absurd extreme comparsion fix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* accept missing minor or patch

* guard 'v' with if posix

* add a few tests

* add test_default_version

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* check if minor is empty and push zero instead of shadowing

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-01 18:18:05 +03:00
Mukunda Rao Katta 2288789f33 fix: allow closing brace as command terminator (#427) 2026-08-01 14:03:34 +03:00
Pranav Ram Joshi 84ae1b07a7 Implement e (execute) command (#481)
* chore: fix typo

* add: standalone e command

Implement the 'e' command. No argument implies execution of pattern space and any respective argument is to run with the output written to stream. Rejected under --posix and --sandbox.

Also stop silently swallowing shell stderr for e and s///e.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* add: e command tests

Includes 17 new tests. Cover both forms of the new 'e' command: with-argument execution (including shell side effects, addressing, ';' not separating the argument, escape decoding, and backslash continuation) and the no-argument pattern-space form. Also cover --posix/--sandbox rejection and stderr surfacing on command failure.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: gate LF-related tests to UNIIX only

* address e command review

Validate shell output as utf-8 and error out instead of lossily converting it. Make CRLF strip conditionally compile to Windows only. Rename execute_pattern_as_shell to execute_pattern_as_shell_command. Add tests for non-utf-8 error paths.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* add: Windows tests for e command with argument

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* fix: broken pipe race in sandbox e command test

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* Apply s/// p and e flags in written order

GNU sed applies the 'p' and 'e' substitution flags in the order they appear. 'pe' prints the pre-execution text then executes, while 'ep' executes then prints the result. Record whether 'p' precedes 'e' and apply them accordingly.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

---------

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
2026-08-01 13:29:42 +03:00
Diomidis Spinellis e9792a8ef9 Improve GNU compatibility through byte processing (#487)
The original Rust implementation assummed a UTF-8 environment, as is
common in modern systems.  This commit improves GNU sed compatibility
by changing most processing to run on bytes rather than Rust's Unicode
characters and strings. This change also improves the handling of binary
files. The commit adds a simple POSIX-compatible locale configuration
to support UTF-8 processing when specified. While at it, also fix
script-specified file handling to take place through OsString and
add a few missing unit tests.

The change fixes two failing GNU test suite cases.
2026-07-26 19:44:54 +02:00
Diomidis SpinellisandMukunda Katta f3a79c5847 Support multiline substitution flag (#482)
While at it fix . matching of embedded newlines.

This commit increases GNU passed tests by 2.

Co-authored-by: Mukunda Katta <mukunda.vjcs6@gmail.com>
2026-07-05 10:48:39 +02:00
Diomidis Spinellis a9e5fe778c Add sandbox checks (#477)
Following the introduction of the first sandbox check in b14272c1
complete the checks for the remaining commands.
2026-07-03 16:55:50 +02:00
Develandpre-commit-ci[bot] 8cb7090bbc --posix should reject GNU substitute flags i/I #401 (#419)
* reject 'i', 'I', 'm', 'M' and 'E' when --posix flag is set

* add test for issue 401

* formatting

* add a test for 'm' flag in test_posix_reject_flags

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update compiler.rs

* cargo fmt

* add a const variable with the err msg

* Update compiler.rs

* cargo fmt

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-01 19:30:25 +03:00
Mukunda Rao Katta 2a5ab5a9a2 feat: add z command (#428)
* feat: add z command

* test: avoid stdin race in z POSIX rejection
2026-07-01 12:51:11 +03:00
Pranav Ram Joshi b14272c1d7 Add e substitution flag
* feat: introduce e substituion for sed

When the s command is requested for sed, the e substitution flag enables execution of the result of substitution as a shell command. It matches the GNU sed behavior.

On src/sed/command.rs, add 'execute' field to 'Substitution' structure.

On src/sed/compiler.rs, handle 'e' in 'compile_subst_flags' and reject it if --posix or --sandbox flag is used in sed.

On src/sed/processor.rs, spawn /bin/sh with -c flag for a successful match and replace the pattern space with the command's standard output stream.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: comply with rustfmt

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: support for Windows command-line

Instead of calling bourne shell, define a new function: shell_command() with two definitions; conditionally compiling to UNIX-like and Windows. On UNIX-like, executes the bourne shell, whereas command prompt is executed on Windows.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: check newline for Windows

Unix-like system only uses the newline character whereas Windows uses the carriage return as well as newline. Hence, when working on Windows, we need to truncate the last two characters.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* add: 'e' flag tests

Nine new tests have been introduced in regards to new 'e' substitution option. 'test_subst_e_flag_multiline_outuput' is conditionally compiled as printf command is not guaranteed to be present on all Windows systems.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: fallback for shell_command

Executing a shell or a command prompt requires appropriate platforms. WASM does not currently support it.

Also fix a linting issue.

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* chore: linting cleanup

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

---------

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
2026-07-01 11:18:02 +03:00
Diomidis SpinellisandPranavRJoshi dcd2df848a Implement zero-address r command to insert file before first line (#474)
* feat: support zero-address file insertion

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>

* Polish the handling of 0r

- Use common constant for error message.
- Simplify command check at compile and runtime.
- Separate runtime handling into a function.

---------

Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
Co-authored-by: PranavRJoshi <pranavrjoshi1@gmail.com>
2026-07-01 08:36:03 +02:00
Sylvestre Ledru b90c599fac sed: fix brace quantifier delimiter and {,n} bound edge cases (#463) 2026-06-14 16:09:18 +02:00
Loukas Papadopoulos d9cd53883c Add validations for brace quantifiers {m}, {m,} and {m,n} (#328) 2026-06-14 12:08:11 +02:00
Devel 508d74fe4a Show usage when invoked without argments
* exit with code 1 when no arguments
* add a test for no arguments.
* Update test_sed.rs
* cargo fmt
* pass -n for missing script arguments test

Issue: #274
PR: #409
2026-06-02 18:37:35 +03:00
mukunda katta 142e70bd13 fix: reject empty text commands 2026-05-15 13:11:51 -07:00