Skip to content

Add e substitution flag#413#424

Open
PranavRJoshi wants to merge 8 commits into
uutils:mainfrom
PranavRJoshi:feat/execute-flag
Open

Add e substitution flag#413#424
PranavRJoshi wants to merge 8 commits into
uutils:mainfrom
PranavRJoshi:feat/execute-flag

Conversation

@PranavRJoshi
Copy link
Copy Markdown

This PR introduces e substitution mentioned in #404 and supersedes #413.

What this does

Implements the e substitute flag, which executes the result of the substitution as a shell command and replaces the pattern space with stdout (minus trailing newline).

$ echo a | ./target/release/sed  's/.*/echo hi/e'
hi

$ echo a | ./target/release/sed --posix 's/.*/echo hi/e'
sed: <script argument 1>:1:14: error: the 'e' substitute flag is not allowed with --posix or --sandbox

Changes

  • src/sed/command.rs: new execute: bool field on Substitution
  • src/sed/compiler.rs: 'e' arm in compile_subst_flags; rejected at compile time under --posix or --sandbox.
  • src/sed/processor.rs: shell execution via /bin/sh -c after substitution

Tests

  • Three new unit tests for the flag parser covering the intended path, --posix rejection, and --sandbox rejection.
  • Full cargo test run passes.
  • GNU testsuite tests still in progress.

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>
Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
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>
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>
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>
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>
Signed-off-by: PranavRJoshi <pranavrjoshi1@gmail.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

❌ Patch coverage is 58.90411% with 30 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.87%. Comparing base (f083fa0) to head (18dd718).

Files with missing lines Patch % Lines
src/sed/processor.rs 0.00% 30 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #424      +/-   ##
==========================================
- Coverage   82.20%   81.87%   -0.34%     
==========================================
  Files          13       13              
  Lines        5542     5604      +62     
  Branches      310      314       +4     
==========================================
+ Hits         4556     4588      +32     
- Misses        983     1013      +30     
  Partials        3        3              
Flag Coverage Δ
macos_latest 82.61% <63.23%> (-0.29%) ⬇️
ubuntu_latest 82.72% <63.23%> (-0.28%) ⬇️
windows_latest 0.00% <0.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 13, 2026

Merging this PR will improve performance by 3.1%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 10 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
genome_subst 215.5 ms 209 ms +3.1%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing PranavRJoshi:feat/execute-flag (18dd718) with main (f083fa0)

Open in CodSpeed

@PranavRJoshi
Copy link
Copy Markdown
Author

PranavRJoshi commented May 13, 2026

Was looking into GNU's testsuite and the test files that was mentioned in the issue. The file subst-options.sh does not seem to have dedicated e flag tests. I did manage to find some tests for e flag in execute-tests.sh file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant