1999-08-13 01:37:52 -04:00
|
|
|
$expect_verbose = false
|
|
|
|
|
|
|
|
class IO
|
2010-04-09 11:04:40 -04:00
|
|
|
# Reads ios until pattern matches or the timeout is over. 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. The optional timeout parameter defines,
|
|
|
|
# in seconds, the total time to wait for pattern. If it is over of eof is found, it
|
|
|
|
# returns/yields nil. 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
|
|
|
|
end
|
|
|
|
while true
|
2008-02-17 20:17:44 -05:00
|
|
|
if !IO.select([self],nil,nil,timeout) or eof? then
|
1999-08-13 01:37:52 -04:00
|
|
|
result = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
c = getc.chr
|
|
|
|
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
|
|
|
|
|