@@ -423,30 +423,43 @@ function eachline(cmd::AbstractCmd,stdin)
423
423
end
424
424
eachline (cmd:: AbstractCmd ) = eachline (cmd,DevNull)
425
425
426
- # returns a pipe to read from the last command in the pipelines
427
- readsfrom (cmds:: AbstractCmd ) = readsfrom (cmds, DevNull)
428
- function readsfrom (cmds:: AbstractCmd , stdin :: AsyncStream )
429
- processes = @tmp_rpipe out tmp spawn (false , cmds, (stdin ,tmp,STDERR))
430
- start_reading (out)
431
- (out, processes)
426
+ # return a (Pipe,Process) pair to write/read to/from the pipeline
427
+ function open (cmds:: AbstractCmd , mode:: String = " r" , stdio:: AsyncStream = DevNull)
428
+ if mode == " r"
429
+ processes = @tmp_rpipe out tmp spawn (false , cmds, (stdio,tmp,STDERR))
430
+ start_reading (out)
431
+ (out, processes)
432
+ elseif mode == " w"
433
+ processes = @tmp_wpipe tmp inpipe spawn (false ,cmds, (tmp,stdio,STDERR))
434
+ (inpipe, processes)
435
+ else
436
+ throw (ArgumentError (" mode must be \" r\" or \" w\" , not \" $mode \" " ))
437
+ end
432
438
end
433
439
434
- function writesto (cmds:: AbstractCmd , stdout :: UVStream )
435
- processes = @tmp_wpipe tmp inpipe spawn (false , cmds, (tmp,stdout ,STDERR))
436
- (inpipe, processes)
440
+ function open (f:: Function , cmds:: AbstractCmd , args... )
441
+ io, P = open (cmds, args... )
442
+ ret = try
443
+ f (io)
444
+ catch
445
+ kill (P)
446
+ rethrow ()
447
+ finally
448
+ close (io)
449
+ end
450
+ success (P) || pipeline_error (P)
451
+ return ret
437
452
end
438
- writesto (cmds:: AbstractCmd ) = writesto (cmds, DevNull)
439
453
454
+ # TODO : convert this to use open(cmd, "r+"), with a single read/write pipe
440
455
function readandwrite (cmds:: AbstractCmd )
441
- (out, processes) = @tmp_wpipe tmp inpipe readsfrom (cmds, tmp)
456
+ (out, processes) = @tmp_wpipe tmp inpipe open (cmds, " r " , tmp)
442
457
(out, inpipe, processes)
443
458
end
444
459
445
460
function readbytes (cmd:: AbstractCmd , stdin :: AsyncStream = DevNull)
446
- (out,pc) = readsfrom (cmd, stdin )
447
- if ! success (pc)
448
- pipeline_error (pc)
449
- end
461
+ (out,pc) = open (cmd, " r" , stdin )
462
+ ! success (pc) && pipeline_error (P)
450
463
wait_close (out)
451
464
return takebuf_array (out. buffer)
452
465
end
@@ -455,15 +468,10 @@ function readall(cmd::AbstractCmd, stdin::AsyncStream=DevNull)
455
468
return bytestring (readbytes (cmd, stdin ))
456
469
end
457
470
458
- writeall (cmd:: AbstractCmd , stdin :: String ) = writeall (cmd, stdin , DevNull)
459
- function writeall (cmd:: AbstractCmd , stdin :: String , stdout :: AsyncStream )
460
- (in,pc) = writesto (cmd, stdout )
461
- write (in, stdin )
462
- close (in)
463
- if ! success (pc)
464
- pipeline_error (pc)
471
+ function writeall (cmd:: AbstractCmd , stdin :: String , stdout :: AsyncStream = DevNull)
472
+ open (cmd, " w" , stdout ) do io
473
+ write (io, stdin )
465
474
end
466
- return true
467
475
end
468
476
469
477
function run (cmds:: AbstractCmd ,args... )
0 commit comments