ext/pty/lib/expect.rb


DEFINITIONS

This source file includes following functions.


   1  $expect_verbose = false
   2  
   3  class IO
   4    def expect(pat,timeout=9999999)
   5      buf = ''
   6      case pat
   7      when String
   8        e_pat = Regexp.new(Regexp.quote(pat))
   9      when Regexp
  10        e_pat = pat
  11      end
  12      while true
  13        if IO.select([self],nil,nil,timeout).nil? then
  14          result = nil
  15          break
  16        end
  17        c = getc.chr
  18        buf << c
  19        if $expect_verbose
  20          STDOUT.print c
  21          STDOUT.flush
  22        end
  23        if mat=e_pat.match(buf) then
  24          result = [buf,*mat.to_a[1..-1]]
  25          break
  26        end
  27      end
  28      if block_given? then
  29        yield result
  30      else
  31        return result
  32      end
  33      nil
  34    end
  35  end
  36