# open4status.rb: Spawn a program like popen, but with stderr, too. You might also # want to use this if you want to bypass the shell. (By passing multiple args, # which IO#popen does not allow). # # It returns an array of stdin, stdout and stderr IO objects. The fourth element # it returns is a Process:Status object so you can determine the exit code # of the program run. # # Usage: # require "open4status" # # stdin, stdout, stderr, status = Open4.popen4status('nroff -man') # or # include Open4 # stdin, stdout, stderr = popen4status('nroff -man') module Open4 #[stdin, stdout, stderr, status] = popen4(command); def popen4status(*cmd) pw = IO::pipe # pipe[0] for read, pipe[1] for write pr = IO::pipe pe = IO::pipe pid = fork{ # child grandchild_pid = fork{ # grandchild pw[1].close STDIN.reopen(pw[0]) pw[0].close pr[0].close STDOUT.reopen(pr[1]) pr[1].close pe[0].close STDERR.reopen(pe[1]) pe[1].close exec(*cmd) } grandchild_status = Process.waitpid2( grandchild_pid ).last exit!(grandchild_status.exitstatus) } pw[0].close pr[1].close pe[1].close status = Process.waitpid2(pid).last pi = [pw[1], pr[0], pe[0], status ] pw[1].sync = true if defined? yield begin return yield(*pi) ensure pi.each{|p| p.close unless p.closed?} end end pi end module_function :popen4status end if $0 == __FILE__ a = Open4.popen4status("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