Skip to content

Feat: Introduce e subsitution#413

Open
PranavRJoshi wants to merge 7 commits intouutils:mainfrom
PranavRJoshi:main
Open

Feat: Introduce e subsitution#413
PranavRJoshi wants to merge 7 commits intouutils:mainfrom
PranavRJoshi:main

Conversation

@PranavRJoshi
Copy link
Copy Markdown

This PR introduces e substitution mentioned in #404.

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.

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>
@sylvestre
Copy link
Copy Markdown
Contributor

please run rustfmt

Comment thread src/sed/processor.rs Outdated
// Execute the pattern space as a shell command if the 'e' flag is set
if sub.execute {
let cmd_str = pattern.as_str()?.to_string();
let output_bytes = std::process::Command::new("/bin/sh")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/bin/sh ?
it won't work on windows

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I make it conditionally compile on UNIX-like system only and warn out the insufficient capabilities on Windows, or do I instead execute the cmd.exe for Windows? The latter one would still require conditional compilation, so i can add #[cfg] for UNIX-like and Windows.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it is a cross platform project. this should be a tier-1 feature on Windows too

@codecov
Copy link
Copy Markdown

codecov Bot commented May 6, 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 (5856b37) to head (ea2cd88).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/sed/processor.rs 0.00% 30 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #413      +/-   ##
==========================================
- 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.

@sylvestre
Copy link
Copy Markdown
Contributor

please also add tests in tests/by-util/test_sed.rs

@PranavRJoshi
Copy link
Copy Markdown
Author

Thanks for the guidance, will work into it.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 6, 2026

Merging this PR will not alter performance

✅ 11 untouched benchmarks


Comparing PranavRJoshi:main (ea2cd88) with main (f783eb9)

Open in CodSpeed

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>
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.

2 participants