

set pi [open "|[info nameofexecutable] 2>@ stderr" r+]

fconfigure $pi    -translation binary -buffering line
fconfigure stdout -translation binary -buffering line

puts $pi {
    fconfigure stdout -translation binary -buffering line
    puts stderr Waiting...
    after 1000
    foreach x {a b c} {
	puts stderr Looping...
	puts $x
	after 500
    }
    proc bye args {
	if {[gets stdin line]<0} {
	    puts stderr "CHILD: EOF detected, exiting"
	    exit
	} else {
	    puts stderr "CHILD: ignoring line: $line"
	}
    }
    puts stderr Now-sleeping-forever
    fileevent stdin readable bye
    vwait forever
}

proc done args {
    puts stderr DONE
    exit
}

fcopy $pi stdout -size 6 -command done
vwait forever
