Posted to tcl by aspect at Sun Jan 19 16:10:07 GMT 2014view raw

  1.  
  2. foreach {0r 0w} [chan pipe] break
  3. foreach {1r 1w} [chan pipe] break
  4. foreach {2r 2w} [chan pipe] break
  5.  
  6. fconfigure stdin -blocking no
  7. fconfigure $1r -blocking no
  8. fconfigure $2r -blocking no
  9. fconfigure $0w -buffering none
  10.  
  11.  
  12. set cmd {grep-v {error}}
  13.  
  14. fileevent stdin readable [list {*}$cmd stdin $0w]
  15. fileevent $1r readable [list {*}$cmd $1r stdout]
  16. fileevent $2r readable [list {*}$cmd $2r stderr]
  17.  
  18. proc cat {in out {cond {}}} {
  19. set s [read $in]
  20. if {$s ne {}} {
  21. if {$cond eq {} || [uplevel 1 $cond [list $s]]} {
  22. puts -nonewline $out $s
  23. }
  24. } elseif {[eof $in]} {
  25. incr ::eof
  26. }
  27. }
  28.  
  29. proc not {args} {expr ![uplevel 1 $args]}
  30.  
  31. proc grep {s in out} {cat $in $out [list regexp $s]}
  32. proc grep-v {s in out} {cat $in $out [list not regexp $s]}
  33.  
  34. exec {*}$argv <@$0r >@$1w 2>@$2w &
  35. vwait eof
  36.