2015-12-16 00:31:54 -05:00
|
|
|
# frozen_string_literal: false
|
1999-08-13 01:37:52 -04:00
|
|
|
$expect_verbose = false
|
|
|
|
|
2013-02-06 09:31:32 -05:00
|
|
|
# Expect library adds the IO instance method #expect, which does similar act to
|
|
|
|
# tcl's expect extension.
|
|
|
|
#
|
|
|
|
# In order to use this method, you must require expect:
|
|
|
|
#
|
|
|
|
# require 'expect'
|
|
|
|
#
|
|
|
|
# Please see #expect for usage.
|
1999-08-13 01:37:52 -04:00
|
|
|
class IO
|
2013-02-06 09:31:32 -05:00
|
|
|
# call-seq:
|
|
|
|
# IO#expect(pattern,timeout=9999999) -> Array
|
|
|
|
# IO#expect(pattern,timeout=9999999) { |result| ... } -> nil
|
|
|
|
#
|
|
|
|
# Reads from the IO until the given +pattern+ matches or the +timeout+ is over.
|
|
|
|
#
|
2011-08-26 18:22:37 -04:00
|
|
|
# It returns an array with the read buffer, followed by the matches.
|
|
|
|
# If a block is given, the result is yielded to the block and returns nil.
|
|
|
|
#
|
2013-02-06 09:31:32 -05:00
|
|
|
# When called without a block, it waits until the input that matches the
|
|
|
|
# given +pattern+ is obtained from the IO or the time specified as the
|
|
|
|
# timeout passes. An array is returned when the pattern is obtained from the
|
|
|
|
# IO. The first element of the array is the entire string obtained from the
|
|
|
|
# IO until the pattern matches, followed by elements indicating which the
|
|
|
|
# pattern which matched to the anchor in the regular expression.
|
|
|
|
#
|
2011-08-26 18:22:37 -04:00
|
|
|
# The optional timeout parameter defines, in seconds, the total time to wait
|
|
|
|
# for the pattern. If the timeout expires or eof is found, nil is returned
|
|
|
|
# or yielded. However, the buffer in a timeout session is kept for the next
|
|
|
|
# expect call. The default timeout is 9999999 seconds.
|
1999-08-13 01:37:52 -04:00
|
|
|
def expect(pat,timeout=9999999)
|
|
|
|
buf = ''
|
|
|
|
case pat
|
|
|
|
when String
|
|
|
|
e_pat = Regexp.new(Regexp.quote(pat))
|
|
|
|
when Regexp
|
|
|
|
e_pat = pat
|
2010-04-09 11:04:54 -04:00
|
|
|
else
|
2013-04-15 22:31:55 -04:00
|
|
|
raise TypeError, "unsupported pattern class: #{pat.class}"
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
2010-04-09 11:07:34 -04:00
|
|
|
@unusedBuf ||= ''
|
1999-08-13 01:37:52 -04:00
|
|
|
while true
|
2010-04-09 11:07:34 -04:00
|
|
|
if not @unusedBuf.empty?
|
|
|
|
c = @unusedBuf.slice!(0).chr
|
|
|
|
elsif !IO.select([self],nil,nil,timeout) or eof? then
|
1999-08-13 01:37:52 -04:00
|
|
|
result = nil
|
2010-04-09 11:07:34 -04:00
|
|
|
@unusedBuf = buf
|
1999-08-13 01:37:52 -04:00
|
|
|
break
|
2010-04-09 11:07:34 -04:00
|
|
|
else
|
|
|
|
c = getc.chr
|
1999-08-13 01:37:52 -04:00
|
|
|
end
|
|
|
|
buf << c
|
|
|
|
if $expect_verbose
|
|
|
|
STDOUT.print c
|
|
|
|
STDOUT.flush
|
|
|
|
end
|
2002-08-13 05:21:18 -04:00
|
|
|
if mat=e_pat.match(buf) then
|
|
|
|
result = [buf,*mat.to_a[1..-1]]
|
1999-08-13 01:37:52 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2001-07-02 04:46:28 -04:00
|
|
|
if block_given? then
|
1999-08-13 01:37:52 -04:00
|
|
|
yield result
|
|
|
|
else
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|