2003-09-16 21:37:34 +04:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# = open3.rb: Popen, but with stderr, too
|
1999-08-13 09:45:20 +04:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# Author:: Yukihiro Matsumoto
|
|
|
|
# Documentation:: Konrad Meyer
|
|
|
|
#
|
|
|
|
# Open3 gives you access to stdin, stdout, and stderr when running other
|
|
|
|
# programs.
|
|
|
|
#
|
|
|
|
|
2004-12-15 19:01:11 +03:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# Open3 grants you access to stdin, stdout, and stderr when running another
|
|
|
|
# program. Example:
|
2004-12-15 19:01:11 +03:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# require "open3"
|
2006-08-04 22:05:50 +04:00
|
|
|
# include Open3
|
|
|
|
#
|
|
|
|
# stdin, stdout, stderr = popen3('nroff -man')
|
|
|
|
#
|
2008-04-26 08:03:59 +04:00
|
|
|
# If the exit status of the child process is required, Open3.popen3w is usable.
|
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# 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:
|
2006-08-04 22:05:50 +04:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# require "open3"
|
2006-08-04 22:05:50 +04:00
|
|
|
#
|
|
|
|
# Open3.popen3('nroff -man') { |stdin, stdout, stderr| ... }
|
2006-10-09 18:49:49 +04:00
|
|
|
#
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
module Open3
|
2006-10-09 18:49:49 +04:00
|
|
|
#
|
|
|
|
# Open stdin, stdout, and stderr streams and start external executable.
|
2008-04-26 08:03:59 +04:00
|
|
|
#
|
2006-10-09 18:49:49 +04:00
|
|
|
# Non-block form:
|
|
|
|
#
|
2008-04-26 08:03:59 +04:00
|
|
|
# stdin, stdout, stderr = Open3.popen3(cmd)
|
|
|
|
# ...
|
|
|
|
# stdin.close # stdin, stdout and stderr should be closed in this form.
|
|
|
|
# stdout.close
|
|
|
|
# stderr.close
|
2006-10-09 18:49:49 +04:00
|
|
|
#
|
|
|
|
# Block form:
|
|
|
|
#
|
|
|
|
# require 'open3'
|
|
|
|
#
|
|
|
|
# Open3.popen3(cmd) { |stdin, stdout, stderr| ... }
|
2008-04-26 08:03:59 +04:00
|
|
|
# # stdin, stdout and stderr is closed automatically in this form.
|
2006-10-09 18:49:49 +04:00
|
|
|
#
|
2008-04-26 08:03:59 +04:00
|
|
|
# The parameter +cmd+ is passed directly to Kernel#spawn.
|
2006-10-09 18:49:49 +04:00
|
|
|
#
|
2000-10-25 08:48:41 +04:00
|
|
|
def popen3(*cmd)
|
2008-04-26 08:03:59 +04:00
|
|
|
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.
|
2008-04-26 15:25:40 +04:00
|
|
|
# The thread has a thread variable :pid which is the pid of the started
|
|
|
|
# process.
|
2008-04-26 08:03:59 +04:00
|
|
|
#
|
|
|
|
# Non-block form:
|
|
|
|
#
|
|
|
|
# stdin, stdout, stderr, wait_thr = Open3.popen3w(cmd)
|
2008-04-26 15:25:40 +04:00
|
|
|
# pid = wait_thr[:pid] # pid of the started process.
|
2008-04-26 08:03:59 +04:00
|
|
|
# ...
|
|
|
|
# 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)
|
1999-08-13 09:45:20 +04:00
|
|
|
pw = IO::pipe # pipe[0] for read, pipe[1] for write
|
|
|
|
pr = IO::pipe
|
|
|
|
pe = IO::pipe
|
|
|
|
|
2008-04-26 08:03:59 +04:00
|
|
|
pid = spawn(*cmd, STDIN=>pw[0], STDOUT=>pr[1], STDERR=>pe[1])
|
|
|
|
wait_thr = Process.detach(pid)
|
|
|
|
wait_thr[:pid] = pid
|
2000-10-20 20:37:01 +04:00
|
|
|
pw[0].close
|
|
|
|
pr[1].close
|
|
|
|
pe[1].close
|
2008-04-26 08:03:59 +04:00
|
|
|
pi = [pw[1], pr[0], pe[0], wait_thr]
|
2002-01-29 10:16:09 +03:00
|
|
|
pw[1].sync = true
|
2000-10-20 20:37:01 +04:00
|
|
|
if defined? yield
|
2000-10-25 08:48:41 +04:00
|
|
|
begin
|
2000-11-08 08:29:37 +03:00
|
|
|
return yield(*pi)
|
2000-10-25 08:48:41 +04:00
|
|
|
ensure
|
2008-04-26 08:03:59 +04:00
|
|
|
[pw[1], pr[0], pe[0]].each{|p| p.close unless p.closed?}
|
|
|
|
wait_thr.join
|
2000-10-25 08:48:41 +04:00
|
|
|
end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2000-10-20 20:37:01 +04:00
|
|
|
pi
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2008-04-26 08:03:59 +04:00
|
|
|
module_function :popen3w
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
if $0 == __FILE__
|
|
|
|
a = Open3.popen3("nroff -man")
|
|
|
|
Thread.start do
|
2000-02-25 06:51:23 +03:00
|
|
|
while line = gets
|
|
|
|
a[0].print line
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
a[0].close
|
|
|
|
end
|
2000-02-25 06:51:23 +03:00
|
|
|
while line = a[1].gets
|
|
|
|
print ":", line
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
end
|