#!/usr/bin/ruby # vim: et sw=2 VER="(7afb555)" ME="bldx: " $chdr="1;36" $cinf="1;37" $cadd="1;32" $cdel="1;31" $incl=[] $excl=[] $fld=nil $opt=nil def in_list(s,list) for i in list if i.match(s) return true end end return false end def regexfix2(str) tmp=[] for t in str.scan(/(_)|(\|)|([^_\|]*)/).flatten.compact tmp.push(t) if not t.empty? end res="" while t=tmp.shift case t when("_") then res+=(tmp.empty? or tmp[0]=="|") ? '([ ."\[\]\(\)\{\}]+|$)' : '[ ."\[\]\(\)\{\}]+' when("|") then res+="|" else res+="#{t}" end end return res end class Block Line=Struct.new :port, :del, :add def initialize(h) @hdr=h @fld=nil @sep=nil @flds=[] @ft=[] @la=[] @lst=[] end def hdr(h) @hdr=h end def fld(f,s) @fld=f @sep=s tmp=@fld.scan(/[^\t]*\t*/) tmp.delete_if { |t| t.empty? } tc=0 for t in tmp @flds<n i=1+(n-i) return if i>=tmp.length str=tmp[i] z=@la.last else port=tmp[0] i=@flds.index($fld) return if i>=tmp.length str=tmp[i] if !z=find(port) z=Line.new(port,[],[]) @la<n i=1+(n-i) return if i>=tmp.length str=tmp[i] else i=@flds.index($fld) return if i>=tmp.length str=tmp[i] end return if str.start_with?("!") arr=[] for v in str.split(",") if v.include?("-") t=v.split("-") t[0].to_i.upto(t[1].to_i) { |x| arr<y.to_i end @lst=combine(@lst) c=$chdr out.puts "\e[#{c}m%s\e[m" % @hdr out.puts "#{$fld}: %s" % @lst.join(",") return end return if prepare<1 c=$chdr out.puts "\e[#{c}m%s\e[m" % @hdr for l in @la next if l.del.empty? and l.add.empty? c=$cinf out.puts "\e[#{c}mport: %s\e[m" % l.port c=$cdel out.puts "del: \e[#{c}m%s\e[m" % l.del.join(",") if not l.del.empty? c=$cadd out.puts "add: \e[#{c}m%s\e[m" % l.add.join(",") if not l.add.empty? end return end private def find(port) for l in @la return l if l.port==port end return nil end def combine(list) arr=[] u=nil for v in list if u and v.to_i==u+1 u+=1 next end if u and arr.last.to_iy.to_i end l.add.sort! do |x,y| next -1 if x.start_with?("+") or x.start_with?("*") next 1 if y.start_with?("+") or y.start_with?("*") next x.to_i<=>y.to_i end l.del=combine(l.del) l.add=combine(l.add) end return cnt end end def usage me=ME.delete(":") puts "Usage: "+me+"" puts " -B - include blocks matching RegEx " puts " -S - skip blocks matching RegEx " puts " -l - make list from input (aggregation)" exit end def p_diff blocks=[] bl=fld=nil inbl=false igbl=false for l in $stdin l.chop! next if l.empty? st=l.slice!(0,1) if inbl if l.start_with?("-----") blocks<=ARGV.length $fld=ARGV[i] if $opt blocks=p_data else blocks=p_diff end exit if blocks.empty? if $istty out=IO.popen("less -fqRFX","w") else out=$stdout end begin blocks.each { |b| b.output(out) } rescue Errno::EPIPE end out.close if out!=$stdout