File tree 3 files changed +36
-1
lines changed
3 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,12 @@ use std::io::Write;
5
5
use std:: process;
6
6
7
7
fn run ( ) -> Result < ( ) , Box < dyn Error > > {
8
+ if env:: var ( "lines-from-stdin" ) . as_deref ( ) == Ok ( "1" ) {
9
+ for line in std:: io:: stdin ( ) . lines ( ) {
10
+ println ! ( "read line from stdin: {}" , line. unwrap( ) ) ;
11
+ }
12
+ }
13
+
8
14
if let Ok ( text) = env:: var ( "stdout" ) {
9
15
println ! ( "{}" , text) ;
10
16
}
Original file line number Diff line number Diff line change @@ -292,6 +292,29 @@ impl TryCmd {
292
292
stdout. pop ( ) ;
293
293
}
294
294
295
+ let mut stdin = None ;
296
+ if cmdline. first ( ) . is_some_and ( |s| s == "echo" ) {
297
+ // No full piping / POSIX shell implemented, but this is a frequent and
298
+ // important tool
299
+ //
300
+ // If no pipe is found, this is not processed any further: echo is then treated
301
+ // as the binary, as it would have been if there were no special handling.
302
+ if let Some ( index) = cmdline. iter ( ) . position ( |s| s == & "|" ) {
303
+ let ( echo_part, actual_command) = cmdline. split_at ( index) ;
304
+ let echo_args = & echo_part[ 1 ..] ;
305
+ if echo_args. first ( ) . is_some_and ( |f| f == "-n" )
306
+ || echo_args. iter ( ) . any ( |s| s. contains ( "\\ " ) )
307
+ {
308
+ return Err (
309
+ "Behavior of echo with -n or backslashes is not defined in POSIX"
310
+ . into ( ) ,
311
+ ) ;
312
+ }
313
+ stdin = Some ( crate :: Data :: text ( format ! ( "{}\n " , echo_args. join( " " ) ) ) ) ;
314
+ cmdline = actual_command[ 1 ..] . into ( ) ;
315
+ }
316
+ } ;
317
+
295
318
let mut env = Env :: default ( ) ;
296
319
297
320
let bin = loop {
@@ -310,7 +333,7 @@ impl TryCmd {
310
333
bin : Some ( Bin :: Name ( bin) ) ,
311
334
args : cmdline,
312
335
env,
313
- stdin : None ,
336
+ stdin,
314
337
stderr_to_stdout : true ,
315
338
expected_status_source,
316
339
expected_status,
Original file line number Diff line number Diff line change
1
+ ```
2
+ $ echo "hello" "second argument" | \
3
+ > lines-from-stdin=1 bin-fixture
4
+ read line from stdin: hello second argument
5
+
6
+ ```
You can’t perform that action at this time.
0 commit comments