1999-08-13 01:37:52 -04:00
|
|
|
$expect_verbose = false
|
|
|
|
|
|
|
|
class IO
|
|
|
|
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
|
|
|
|
|