summaryrefslogtreecommitdiff
path: root/lib/open3.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit62e648e148b3cb9f96dcce808c55c02b7ccb4486 (patch)
tree9708892ece92e860d81559ab55e6b1f9400d7ffc /lib/open3.rb
parentaeb049c573be4dc24dd20650f40e4777e0f698cf (diff)
ruby 1.3 cycle
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/open3.rb')
-rw-r--r--lib/open3.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/open3.rb b/lib/open3.rb
new file mode 100644
index 0000000000..e8ba2783da
--- /dev/null
+++ b/lib/open3.rb
@@ -0,0 +1,55 @@
+# Usage:
+# require "open3"
+#
+# in, out, err = Open3.popen3('nroff -man')
+# or
+# include Open3
+# in, out, err = popen3('nroff -man')
+#
+
+module Open3
+ #[stdin, stdout, stderr] = popen3(command);
+ def popen3(cmd)
+ pw = pipe # pipe[0] for read, pipe[1] for write
+ pr = pipe
+ pe = pipe
+
+ pid = fork
+ if pid == nil then # child
+ 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
+ else
+ pw[0].close
+ pr[1].close
+ pe[1].close
+ pi = [ pw[1], pr[0], pe[0] ]
+ end
+ end
+ module_function :popen3
+end
+
+if $0 == __FILE__
+ a = Open3.popen3("nroff -man")
+ Thread.start do
+ while gets
+ a[0].print $_
+ end
+ a[0].close
+ end
+ while a[1].gets
+ print ":", $_
+ end
+end
+