#!/usr/bin/ruby # mpipe reads STDIN and pipes it to multiple processes # # Usage: whatever | mpipe.rb common_args -pipe pgm1 -pipe pgm2 pgm2_args # # pgm1 and pgm2 will all receive whatever's STDOUT on their STDIN # All arguments appearing before the first -pipe switch will be passed to # all called programs before their own arguments # # Example of a CVS loginfo entry: # # \/doc\/en ( $CVSROOT/CVSROOT/mpipe.rb %{s} -pipe $CVSROOT/CVSROOT/commitlog-doc.pl -pipe $CVSROOT/CVSROOT/ciabot.pl $USER gentoo cvs-commits@gentoo.org cia@navi.cx ) # # Current limitation: programs that expect a -pipe argument themselves cannot be called # # Target usage: send CVS loginfo to multiple processes # Parse cmd line aPipes=[] curr="" common=""; gotOne=false ARGV.each {|p| if p == '-pipe' gotOne = true # Open current pipe cmd before starting a new one aPipes << IO.popen(curr, "w") if curr.length > 0 # Init next pipe cmd curr = "" else # Add argument to current pipe cmd or to the common args if gotOne then curr << "#{p} #{common if curr.length == 0} " else common << "#{p} " end end } # Last cmd aPipes << IO.popen(curr, "w") if curr.length > 0 # Read STDIN and send it to all opened pipes STDIN.each { |line| aPipes.each { |p| p.puts(line) } } # Close all pipes and exit aPipes.each {|p| p.close_write}