-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect.php
57 lines (55 loc) · 1.25 KB
/
expect.php
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
53
54
55
56
57
<?php
function do_expect($script, $fds) {
foreach($fds as $k => $fd) {
stream_set_blocking($fd, FALSE);
}
$timeout = 3;
foreach($script as $lineno => $line) {
unset($action);
unset($fail);
unset($pattern);
unset($fd);
unset($current);
extract($line);
if(!isset($fd)) $fd = 1;
if($pattern == 'timeout') {
$timeout = $value;
continue;
} elseif($pattern{0} == '/') {
$start = time();
$end = 0;
while(($end - $start) <= $timeout) {
$t = $timeout;
$n = stream_select($r = array($fds[$fd]), $w = NULL,
$e = NULL, $t);
if($n === false) {
return false;
} else {
// print("selected $n<br />\n"); flush();
$end = time();
$d = $timeout - ($end - $start);
if($n == 0) {
if(isset($fail)) {
$r = eval($fail);
if($r !== NULL) return $r;
}
break;
} else {
$current .= fread($r[0], 16);
// print("have $current; $d of $timeout remain<br />\n"); flush();
if(preg_match($pattern, $current)) {
// print("matched $pattern<br />"); flush();
if(isset($action)) {
$r = eval($action);
if($r !== NULL) return $r;
}
break;
}
}
}
}
}
}
return 0;
}
?>