[前][次][番号順一覧][スレッド一覧][生データ]

yarv-diff:310

From: ko1 atdot.net
Date: 23 Feb 2006 14:33:04 -0000
Subject: [yarv-diff:310] r475 - in trunk: . lib

Author: aamine
Date: 2006-02-23 23:33:03 +0900 (Thu, 23 Feb 2006)
New Revision: 475

Added:
   trunk/lib/open3.rb
Modified:
   trunk/ChangeLog
Log:
* lib/open3.rb: imported from Ruby CVS trunk HEAD (rev 1.12).


Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2006-02-23 06:15:50 UTC (rev 474)
+++ trunk/ChangeLog	2006-02-23 14:33:03 UTC (rev 475)
@@ -4,6 +4,11 @@
 #  from Mon, 03 May 2004 01:24:19 +0900
 #
 
+2006-02-23(Thu) 23:32:53 +0900  Minero Aoki  <aamine loveruby.net>
+
+	* lib/open3.rb: imported from Ruby CVS trunk HEAD (rev 1.12).
+
+
 2006-02-23(Thu) 15:10:09 +0900  Koichi Sasada  <ko1 atdot.net>
 
 	* eval.c : support rb_frame_self()

Added: trunk/lib/open3.rb
===================================================================
--- trunk/lib/open3.rb	2006-02-23 06:15:50 UTC (rev 474)
+++ trunk/lib/open3.rb	2006-02-23 14:33:03 UTC (rev 475)
@@ -0,0 +1,77 @@
+# open3.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)
+#
+# Usage:
+#	require "open3"
+#
+#      stdin, stdout, stderr = Open3.popen3('nroff -man')
+#  or
+#	include Open3
+#      stdin, stdout, stderr = popen3('nroff -man')
+#
+#  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.
+#
+#  Such as
+#      Open3.popen3('nroff -man') { |stdin, stdout, stderr| ... }
+
+module Open3
+  #[stdin, stdout, stderr] = popen3(command);
+  def popen3(*cmd)
+    pw = IO::pipe   # pipe[0] for read, pipe[1] for write
+    pr = IO::pipe
+    pe = IO::pipe
+
+    pid = fork{
+      # child
+      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)
+      }
+      exit!(0)
+    }
+
+    pw[0].close
+    pr[1].close
+    pe[1].close
+    Process.waitpid(pid)
+    pi = [pw[1], pr[0], pe[0]]
+    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 :popen3
+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


-- 
ML: yarv-diff quickml.atdot.net
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧][生データ]