Sha256: 1a5c8a4f60a020fe219f9315c5ef9d29d0e10ba6b70145010d1413d784504147

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# Copyright:: Copyright (c) 2005 Nicolas Pouillard. All rights reserved.
# License::   GNU General Public License (GPL).
# Revision::  $Id: pipe.rb 221 2005-05-09 12:40:57Z ertai $

module Commands

  class Pipe < Command

    def initialize ( *cmds )
      @cmds = cmds.map { |x| x.dup }
    end

    def run ( *a )
      pids = []
      ([nil] + @cmds).zip(@cmds + [nil]).each do |cmd1, cmd2|
        next if cmd1.nil? or cmd2.nil?
        rd, wr = IO.pipe
        cmd1.output = wr
        cmd2.input = rd
        pids << fork do
          rd.close
          cmd1.exec
        end
        wr.close
      end
      pids << fork do
        @cmds.last.exec
      end
      pids.each { |pid| Process.waitpid pid }
    end

    def input= ( arg )
      @cmds.first.input = arg
    end

    def input
      @cmds.first.input
    end

    def output= ( arg )
      @cmds.last.output = arg
    end

    def output
      @cmds.last.output
    end

    def error= ( arg )
      @cmds.last.error = arg
    end

    def error
      @cmds.last.error
    end

    def to_sh
      strs = @cmds.map { |cmd| "(#{cmd.to_sh})" }
      "(#{strs.join(' | ')})#{sh_args}"
    end

  end # class Pipe

end # module Commands

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vcs-0.2.148 ruby_ex/commands/pipe.rb