aboutsummaryrefslogtreecommitdiffstats
path: root/lib/open3.rb
blob: 5efcb173cc8a437930e4a4b6cfe4fad73e9bfe90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#
# = open3.rb: Popen, but with stderr, too
#
# Author:: Yukihiro Matsumoto
# Documentation:: Konrad Meyer
#
# Open3 gives you access to stdin, stdout, and stderr when running other
# programs.
#

#
# Open3 grants you access to stdin, stdout, and stderr when running another
# program. Example:
#
#   require "open3"
#   include Open3
#   
#   stdin, stdout, stderr = popen3('nroff -man')
#
# If the exit status of the child process is required, Open3.popen3w is usable.
#
# Open3.popen3 can also take a block which will receive stdin, stdout and
# stderr as parameters.  This ensures stdin, stdout and stderr are closed
# once the block exits. Example:
#
#   require "open3"
#
#   Open3.popen3('nroff -man') { |stdin, stdout, stderr| ... }
#

module Open3
  # 
  # Open stdin, stdout, and stderr streams and start external executable.
  #
  # Non-block form:
  #   
  #   stdin, stdout, stderr = Open3.popen3(cmd)
  #   ...
  #   stdin.close  # stdin, stdout and stderr should be closed in this form.
  #   stdout.close
  #   stderr.close
  #
  # Block form:
  #
  #   require 'open3'
  #
  #   Open3.popen3(cmd) { |stdin, stdout, stderr| ... }
  #   # stdin, stdout and stderr is closed automatically in this form.
  #
  # The parameter +cmd+ is passed directly to Kernel#spawn.
  #
  def popen3(*cmd)
    if defined? yield
      popen3w(*cmd) {|stdin, stdout, stderr, wait_thr|
        yield stdin, stdout, stderr
      }
    else
      stdin, stdout, stderr, wait_thr = popen3w(*cmd)
      return stdin, stdout, stderr
    end
  end
  module_function :popen3

  # 
  # Open stdin, stdout, and stderr streams and start external executable.
  # In addition, a thread for waiting the started process is noticed.
  # The thread has a thread variable :pid which is the pid of the started
  # process.
  #
  # Non-block form:
  #   
  #   stdin, stdout, stderr, wait_thr = Open3.popen3w(cmd)
  #   pid = wait_thr[:pid]  # pid of the started process.
  #   ...
  #   stdin.close  # stdin, stdout and stderr should be closed in this form.
  #   stdout.close
  #   stderr.close
  #   exit_status = wait_thr.value  # Process::Status object returned.
  #
  # Block form:
  #
  #   Open3.popen3w(cmd) { |stdin, stdout, stderr, wait_thr| ... }
  #
  # The parameter +cmd+ is passed directly to Kernel#spawn.
  #
  def popen3w(*cmd)
    pw = IO::pipe   # pipe[0] for read, pipe[1] for write
    pr = IO::pipe
    pe = IO::pipe

    pid = spawn(*cmd, STDIN=>pw[0], STDOUT=>pr[1], STDERR=>pe[1])
    wait_thr = Process.detach(pid)
    wait_thr[:pid] = pid
    pw[0].close
    pr[1].close
    pe[1].close
    pi = [pw[1], pr[0], pe[0], wait_thr]
    pw[1].sync = true
    if defined? yield
      begin
	return yield(*pi)
      ensure
	[pw[1], pr[0], pe[0]].each{|p| p.close unless p.closed?}
        wait_thr.join
      end
    end
    pi
  end
  module_function :popen3w
end

if $0 == __FILE__
  a = Open3.popen3("nroff -man")
  Thread.start do
    while line = gets
      a[0].print line
    end
    a[0].close
  end
  while line = a[1].gets
    print ":", line
  end
end