%w(lib ../lib . ..).each{|d| $:.unshift d} require 'session' #require 'timeout' STDOUT.sync = true STDERR.sync = true ## a timeout method which does not stop all threads! #class TimeoutError < StandardError; end #def timeout n ##{{{ # ret = nil # cid = fork # unless cid # trap('SIGQUIT'){ exit! } # sleep n # begin; Process.kill 'SIGUSR1', Process.ppid; rescue Errno::Exception; end # exit! # else # begin # handler = trap('SIGUSR1'){throw :timeout, TimeoutError.new} # thrown = catch(:timeout){ ret = yield } # if TimeoutError === thrown # display # STDIN.gets # raise thrown # end # ensure # begin; Process.kill 'SIGQUIT', cid; rescue Exception; end # begin; Process.wait; rescue Exception => e; end # trap 'SIGUSR1', handler if defined? handler # end # end # ret ##}}} #end class TimeoutError < StandardError; end def timeout n #{{{ ret = nil cid = fork unless cid trap('SIGQUIT'){ exit! } sleep n begin; Process.kill 'SIGUSR1', Process.ppid; rescue Errno::Exception; end exit! else begin handler = trap('SIGUSR1'){throw :timeout, TimeoutError.new} thrown = catch(:timeout){ ret = yield } if TimeoutError === thrown display STDIN.gets raise thrown end ensure begin; Process.kill 'SIGQUIT', cid; rescue Exception; end begin; Process.wait; rescue Exception => e; end trap 'SIGUSR1', handler if defined? handler end end ret #}}} end def display #{{{ puts "$command < #{ $command.inspect }> " puts "$iodat < #{ $iodat.inspect }> " puts "$selecting < #{ $selecting.inspect }> " # puts "$command < #{ $command.inspect }> " # puts "$buffer < #{ $buffer.inspect }> " # puts "$err < #{ $err.inspect }> " # puts "$out < #{ $out.inspect }> " # puts "$selecting < #{ $selecting.inspect }> " # puts "$iodat_name < #{ $iodat_name.inspect }> " # puts "$reading < #{ $reading.inspect }> " # puts "$buf < #{ $buf.inspect }> " # puts "$lines < #{ $lines.inspect }> " # puts "$line < #{ $line.inspect }> " # puts "$getting_status < #{ $getting_status.inspect }> " true #}}} end i = 0 idl = Session::IDL.new loop do begin #timeout(1) { out = ''; err = '' idl.execute 'printf, -1, 42', :stdout => out, :stderr => err raise unless '42' == out.strip out = ''; err = '' idl.execute 'printf, -2, \'forty-two\'', :stdout => out, :stderr => err raise unless 'forty-two' == err.strip out = ''; err = '' idl.execute 'foo', :stdout => out, :stderr => err raise unless %r/undefined procedure/io.match err #} p i if (i % 128 == 0) rescue Exception => e display raise end i = (i += 1) % 1024 end