@@ -86,7 +86,9 @@ module Open3
86
86
# Open3.popen3([env, ] command_line, options = {}) {|stdin, stdout, stderr, wait_thread| ... } -> object
87
87
# Open3.popen3([env, ] exe_path, *args, options = {}) {|stdin, stdout, stderr, wait_thread| ... } -> object
88
88
#
89
- # Basically a wrapper for Process.spawn that:
89
+ # Basically a wrapper for
90
+ # {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
91
+ # that:
90
92
#
91
93
# - Creates a child process, by calling Process.spawn with the given arguments.
92
94
# - Creates streams +stdin+, +stdout+, and +stderr+,
@@ -235,7 +237,9 @@ def popen3(*cmd, &block)
235
237
# Open3.popen2([env, ] command_line, options = {}) {|stdin, stdout, wait_thread| ... } -> object
236
238
# Open3.popen2([env, ] exe_path, *args, options = {}) {|stdin, stdout, wait_thread| ... } -> object
237
239
#
238
- # Basically a wrapper for Process.spawn that:
240
+ # Basically a wrapper for
241
+ # {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
242
+ # that:
239
243
#
240
244
# - Creates a child process, by calling Process.spawn with the given arguments.
241
245
# - Creates streams +stdin+ and +stdout+,
@@ -372,7 +376,9 @@ def popen2(*cmd, &block)
372
376
# Open3.popen2e([env, ] command_line, options = {}) {|stdin, stdout_and_stderr, wait_thread| ... } -> object
373
377
# Open3.popen2e([env, ] exe_path, *args, options = {}) {|stdin, stdout_and_stderr, wait_thread| ... } -> object
374
378
#
375
- # Basically a wrapper for Process.spawn that:
379
+ # Basically a wrapper for
380
+ # {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
381
+ # that:
376
382
#
377
383
# - Creates a child process, by calling Process.spawn with the given arguments.
378
384
# - Creates streams +stdin+, +stdout_and_stderr+,
@@ -953,43 +959,62 @@ def pipeline_rw(*cmds, &block)
953
959
end
954
960
module_function :pipeline_rw
955
961
956
- # Open3.pipeline_r starts a list of commands as a pipeline with a pipe
957
- # which connects to stdout of the last command.
958
- #
959
- # Open3.pipeline_r(cmd1, cmd2, ... [, opts]) {|last_stdout, wait_threads|
960
- # ...
961
- # }
962
+ # :call-seq:
963
+ # Open3.pipeline_r([env, ] *cmds, options = {}) -> [last_stdout, wait_threads]
962
964
#
963
- # last_stdout, wait_threads = Open3.pipeline_r(cmd1, cmd2, ... [, opts])
964
- # ...
965
- # last_stdout.close
965
+ # Basically a wrapper for
966
+ # {Process.spawn}[https://docs.ruby-lang.org/en/master/Process.html#method-c-spawn]
967
+ # that:
966
968
#
967
- # Each cmd is a string or an array.
968
- # If it is an array, the elements are passed to Process.spawn.
969
+ # - Creates a child process for each of the given +cmds+
970
+ # by calling Process.spawn.
971
+ # - Pipes the +stdout+ from each child to the +stdin+ of the next child,
972
+ # or, for the last child, to the caller's +stdout+.
973
+ # - Waits for all child processes to exit.
969
974
#
970
- # cmd:
971
- # commandline command line string which is passed to a shell
972
- # [env, commandline, opts] command line string which is passed to a shell
973
- # [env, cmdname, arg1, ..., opts] command name and one or more arguments (no shell)
974
- # [env, [cmdname, argv0], arg1, ..., opts] command name and arguments including argv[0] (no shell)
975
+ # With no block given, returns a 2-element array containing:
975
976
#
976
- # Note that env and opts are optional, as for Process.spawn.
977
+ # - The +stdout+ stream of the last child process.
978
+ # - An array of the wait threads for all of the child processes.
977
979
#
978
980
# Example:
979
981
#
980
- # Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz",
981
- # [{"LANG"=>"C"}, "grep", "GET /favicon.ico"],
982
- # "logresolve") {|o, ts|
983
- # o.each_line {|line|
984
- # ...
985
- # }
986
- # }
982
+ # Open3.pipeline_r('ls', 'grep R')
983
+ # # => [#<IO:fd 5>, [#<Process::Waiter:0x00005638280167b8 sleep>, #<Process::Waiter:0x0000563828015480 dead>]]
987
984
#
988
- # Open3.pipeline_r("yes", "head -10") {|o, ts|
989
- # p o.read #=> "y\ny\ny\ny\ny\ny\ny\ny\ny\ny\n"
990
- # p ts[0].value #=> #<Process::Status: pid 24910 SIGPIPE (signal 13)>
991
- # p ts[1].value #=> #<Process::Status: pid 24913 exit 0>
992
- # }
985
+ # With a block given, calls the block with the +stdout+ stream
986
+ # of the last child process:
987
+ #
988
+ # Open3.pipeline_r('ls', 'grep R') {|x| puts x.read }
989
+ #
990
+ # Output:
991
+ #
992
+ # CONTRIBUTING.md
993
+ # Rakefile
994
+ # README.md
995
+ #
996
+ # Like Process.spawn, this method has potential security vulnerabilities
997
+ # if called with untrusted input;
998
+ # see {Command Injection}[rdoc-ref:command_injection.rdoc].
999
+ #
1000
+ # Unlike Process.spawn, this method waits for the child processes to exit
1001
+ # before returning, so the caller need not do so.
1002
+ #
1003
+ # If the first argument is a hash, it becomes leading argument +env+
1004
+ # in each call to Process.spawn;
1005
+ # see {Execution Environment}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Environment].
1006
+ #
1007
+ # If the last argument is a hash, it becomes trailing argument +options+
1008
+ # in each call to Process.spawn;
1009
+ # see {Execution Options}[https://docs.ruby-lang.org/en/master/Process.html#module-Process-label-Execution+Options].
1010
+ #
1011
+ # Each remaining argument in +cmds+ is one of:
1012
+ #
1013
+ # - A +command_line+: a string that begins with a shell reserved word
1014
+ # or special built-in, or contains one or more metacharacters.
1015
+ # - An +exe_path+: the string path to an executable to be called.
1016
+ # - An array containing a +command_line+ or an +exe_path+,
1017
+ # along with zero or more string arguments for the command.
993
1018
#
994
1019
def pipeline_r ( *cmds , &block )
995
1020
if Hash === cmds . last
0 commit comments