From: pete@... (Peter J. Kernan) Date: 2001-10-27T00:33:01+09:00 Subject: [ruby-talk:23468] expect.rb Child_changed I am seeing a race condition w/the following program. Any guidance would be appreciated. Here is a toy proof-of-concept program, wherein files containing commands are fed in turn to an interactive program. Here is a simple interactive program, ----------------------------reverse.rb ----------------------------------- #!/usr/bin/env ruby loop { print "reverse> " repl = $stdin.gets exit unless repl repl.chomp! case repl when /^\s*exit\s*$/, nil exit else puts repl.reverse end } -------------------------------------------------------------------------- here is the driver program ----------------------------reverse-expect.rb ---------------------------- #!/usr/bin/env ruby require 'expect' require 'pty' exit unless ARGV[0] ARGV.each {|file| input = File.open(file) catch (:done) do PTY.spawn("./reverse.rb") do |r,w| w.sync = true loop { r.expect("reverse> ") { |out| $stdout.print out s = input.gets case s.strip when "exit" begin w.print s rescue $stdout.puts s throw :done end else w.print s end } } end end } -------------------------------------------------------------------------- the program may be called as in ~/tmp> ./reverse-expect.rb zip-frags [...snipped output...] reverse> I'm using my X-RAY VISION to obtain a rare glimpse of the eht fo espmilg erar a niatbo ot NOISIV YAR-X ym gnisu m'I reverse> INNER WORKINGS of this POTATO!! !!OTATOP siht fo SGNIKROW RENNI reverse> exit but occasionally (order 10%) i see e.g. reverse> INNER WORKINGS of this POTATO!! !!OTATOP siht fo SGNIKROW RENNI reverse> /usr/local/lib/ruby/1.6/expect.rb:17:in `getc': \ Child_changed: 9162 (RuntimeError) from /usr/local/lib/ruby/1.6/expect.rb:17:in `expect' from ./reverse-expect.rb:15 from ./reverse-expect.rb:15:in `loop' i am now trying the Expecter class Matz posted in [ruby-talk:01767,6436] to see if i can write the program i want using his technique, but independently of the success of that effort it would be useful to know whether i am using expect.rb incorrectly in this instance. ~/tmp> ruby -v ruby 1.6.4 (2001-06-04) [i686-linux] Pete -- screwbean: a shrub or small tree found in the US Desert Southwest