/
expect.rb
53 lines (42 loc) · 1.24 KB
/
expect.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This is originally from the pty extension that comes with MRI
# by default.
$expect_verbose = false
class IO
# 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.
def expect(pat,timeout=9999999)
case pat
when String
pat = Regexp.new Regexp.quote(pat)
when Regexp
# Nothing, we're good.
else
raise ArgumentError, "accepts a String or Regexp only"
end
buf = ''
verbose = $expect_verbose
while true
if !IO.select([self], nil, nil, timeout) or eof?
result = nil
break
end
c = getc.chr
buf << c
if verbose
STDOUT.print c
STDOUT.flush
end
if match = pat.match(buf)
result = [buf, *match.to_a[1..-1]]
break
end
end
return result unless block_given?
yield result
nil
end
end