Feat: Introduce e subsitution#413
Conversation
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>
|
please run rustfmt |
| // 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") |
There was a problem hiding this comment.
/bin/sh ?
it won't work on windows
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
nope, it is a cross platform project. this should be a tier-1 feature on Windows too
Codecov Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
please also add tests in tests/by-util/test_sed.rs |
|
Thanks for the guidance, will work into it. |
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>
This PR introduces
esubstitution mentioned in #404.What this does
Implements the
esubstitute flag, which executes the result of the substitution as a shell command and replaces the pattern space with stdout (minus trailing newline).Changes
src/sed/command.rs: newexecute: boolfield onSubstitutionsrc/sed/compiler.rs:'e'arm incompile_subst_flags; rejected at compile time under--posixor--sandbox.src/sed/processor.rs: shell execution via/bin/sh -cafter substitutionTests
cargo testrun passes.